Skip to content

Commit

Permalink
[#5771] MockResultSet should call Converter to transform MockResult's…
Browse files Browse the repository at this point in the history
… <U> back to <T>
  • Loading branch information
lukaseder committed Jan 5, 2017
1 parent e176fd9 commit d759e2d
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions jOOQ/src/main/java/org/jooq/tools/jdbc/MockResultSet.java
Expand Up @@ -60,8 +60,11 @@
import java.util.Calendar; import java.util.Calendar;
import java.util.Map; import java.util.Map;


import org.jooq.Converter;
import org.jooq.Converters;
import org.jooq.Field; import org.jooq.Field;
import org.jooq.Result; import org.jooq.Result;
import org.jooq.tools.Convert;


/** /**
* A mock result set. * A mock result set.
Expand All @@ -77,7 +80,7 @@ public class MockResultSet extends JDBC41ResultSet implements ResultSet, Seriali
private static final long serialVersionUID = -2292216936424437750L; private static final long serialVersionUID = -2292216936424437750L;


private final int maxRows; private final int maxRows;
Result<?> result; Result<?> result;
private transient int index; private transient int index;
private transient boolean wasNull; private transient boolean wasNull;


Expand Down Expand Up @@ -109,38 +112,40 @@ public boolean isWrapperFor(Class<?> iface) throws SQLException {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------


private int size() { private int size() {
if (maxRows == 0) { if (maxRows == 0)
return result.size(); return result.size();
} else
else {
return Math.min(maxRows, result.size()); return Math.min(maxRows, result.size());
}
} }


void checkNotClosed() throws SQLException { void checkNotClosed() throws SQLException {
if (result == null) { if (result == null)
throw new SQLException("ResultSet is already closed"); throw new SQLException("ResultSet is already closed");
}
} }


private void checkInRange() throws SQLException { private void checkInRange() throws SQLException {
checkNotClosed(); checkNotClosed();


if (index <= 0 || index > result.size()) { if (index <= 0 || index > result.size())
throw new SQLException("ResultSet index is at an illegal position : " + index); throw new SQLException("ResultSet index is at an illegal position : " + index);
}
} }


private void checkField(String columnLabel) throws SQLException { private Field<?> field(String columnLabel) throws SQLException {
if (result.field(columnLabel) == null) { Field<?> field = result.field(columnLabel);

if (field == null)
throw new SQLException("Unknown column label : " + columnLabel); throw new SQLException("Unknown column label : " + columnLabel);
}
return field;
} }


private void checkField(int columnIndex) throws SQLException { private Field<?> field(int columnIndex) throws SQLException {
if (result.field(columnIndex - 1) == null) { Field<?> field = result.field(columnIndex - 1);

if (field == null)
throw new SQLException("Unknown column index : " + columnIndex); throw new SQLException("Unknown column index : " + columnIndex);
}
return field;
} }


@Override @Override
Expand Down Expand Up @@ -233,28 +238,24 @@ public boolean last() throws SQLException {
@Override @Override
public boolean isFirst() throws SQLException { public boolean isFirst() throws SQLException {
checkNotClosed(); checkNotClosed();

return (size() > 0 && index == 1); return (size() > 0 && index == 1);
} }


@Override @Override
public boolean isBeforeFirst() throws SQLException { public boolean isBeforeFirst() throws SQLException {
checkNotClosed(); checkNotClosed();

return (size() > 0 && index == 0); return (size() > 0 && index == 0);
} }


@Override @Override
public boolean isLast() throws SQLException { public boolean isLast() throws SQLException {
checkNotClosed(); checkNotClosed();

return (size() > 0 && index == size()); return (size() > 0 && index == size());
} }


@Override @Override
public boolean isAfterLast() throws SQLException { public boolean isAfterLast() throws SQLException {
checkNotClosed(); checkNotClosed();

return (size() > 0 && index > size()); return (size() > 0 && index > size());
} }


Expand Down Expand Up @@ -292,9 +293,8 @@ public int findColumn(String columnLabel) throws SQLException {
checkNotClosed(); checkNotClosed();


Field<?> field = result.field(columnLabel); Field<?> field = result.field(columnLabel);
if (field == null) { if (field == null)
throw new SQLException("No such column : " + columnLabel); throw new SQLException("No such column : " + columnLabel);
}


return result.fieldsRow().indexOf(field) + 1; return result.fieldsRow().indexOf(field) + 1;
} }
Expand All @@ -303,9 +303,8 @@ public int findColumn(String columnLabel) throws SQLException {
public void setFetchDirection(int direction) throws SQLException { public void setFetchDirection(int direction) throws SQLException {


// Fetch direction is not supported // Fetch direction is not supported
if (direction != ResultSet.FETCH_FORWARD) { if (direction != ResultSet.FETCH_FORWARD)
throw new SQLException("Fetch direction can only be FETCH_FORWARD"); throw new SQLException("Fetch direction can only be FETCH_FORWARD");
}
} }


@Override @Override
Expand Down Expand Up @@ -363,18 +362,18 @@ public Statement getStatement() throws SQLException {


private <T> T get(String columnLabel, Class<T> type) throws SQLException { private <T> T get(String columnLabel, Class<T> type) throws SQLException {
checkInRange(); checkInRange();
checkField(columnLabel);


T value = result.get(index - 1).get(columnLabel, type); Converter<?, ?> converter = Converters.inverse(field(columnLabel).getConverter());
T value = Convert.convert(result.get(index - 1).get(columnLabel, converter), type);
wasNull = (value == null); wasNull = (value == null);
return value; return value;
} }


private <T> T get(int columnIndex, Class<T> type) throws SQLException { private <T> T get(int columnIndex, Class<T> type) throws SQLException {
checkInRange(); checkInRange();
checkField(columnIndex);


T value = result.get(index - 1).get(columnIndex - 1, type); Converter<?, ?> converter = Converters.inverse(field(columnIndex).getConverter());
T value = Convert.convert(result.get(index - 1).get(columnIndex - 1, converter), type);
wasNull = (value == null); wasNull = (value == null);
return value; return value;
} }
Expand Down

0 comments on commit d759e2d

Please sign in to comment.