Refactor internals DataReader remove resetColumnPosition()

- Move the reset of position to the DataReader.next() method
- Tidy up use of wasNull() in RsetDataReader
This commit is contained in:
rbygrave committed 2021-06-09 12:57:56 +12:00
1 parent 612295fb9d
commit 4613f63c66
7 files changed
+25 -71

No files matched your search

@@ -1,13 +1,8 @@
package io.ebeaninternal.server.core;
import io.ebean.EbeanServer;
import io.ebean.Transaction;
import io.ebean.util.JdbcClose;
import io.ebeaninternal.api.BindParams;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiSqlBinding;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.api.*;
import io.ebeaninternal.server.lib.Str;
import io.ebeaninternal.server.persist.Binder;
import io.ebeaninternal.server.persist.TrimLogSql;
@@ -83,11 +78,6 @@ public abstract class AbstractSqlQueryRequest {
return transaction.isLogSql();
}
/**
* Set the resultSet and associated query plan if known.
*/
abstract void setResultSet(ResultSet resultSet, Object queryPlanKey) throws SQLException;
/**
* Return the bindLog for this request.
*/
@@ -95,12 +85,15 @@ public abstract class AbstractSqlQueryRequest {
return bindLog;
}
/**
* Set the resultSet and associated query plan if known.
*/
abstract void setResultSet(ResultSet resultSet, Object queryPlanKey) throws SQLException;
/**
* Return true if we can navigate to the next row.
*/
public boolean next() throws SQLException {
return resultSet.next();
}
public abstract boolean next() throws SQLException;
protected abstract void requestComplete();
@@ -113,7 +106,6 @@ public abstract class AbstractSqlQueryRequest {
JdbcClose.close(pstmt);
}
/**
* Prepare the SQL taking into account named bind parameters.
*/
@@ -110,9 +110,12 @@ public final class DtoQueryRequest<T> extends AbstractSqlQueryRequest {
return queryEngine.findList(this);
}
public boolean next() throws SQLException {
return dataReader.next();
}
@SuppressWarnings("unchecked")
public T readNextBean() throws SQLException {
dataReader.resetColumnPosition();
return (T) plan.readRow(dataReader);
}
@@ -139,6 +139,11 @@ public final class RelationalQueryRequest extends AbstractSqlQueryRequest {
rows++;
}
@Override
public boolean next() throws SQLException {
return resultSet.next();
}
public <T> List<T> mapList(RowMapper<T> mapper) throws SQLException {
List<T> list = new ArrayList<>();
while (next()) {
@@ -486,14 +486,11 @@ public class CQuery<T> implements DbReadContext, CancelableQuery, SpiProfileTran
* Return true if we can move to the next resultSet row.
*/
private boolean moveToNextRow() throws SQLException {
if (!dataReader.next()) {
noMoreRows = true;
return false;
}
rowCount++;
dataReader.resetColumnPosition();
return true;
}
@@ -502,7 +499,6 @@ public class CQuery<T> implements DbReadContext, CancelableQuery, SpiProfileTran
}
boolean readBean() throws SQLException {
boolean result = hasNext();
updateExecutionStatistics();
return result;
@@ -111,7 +111,6 @@ class CQueryFetchSingleAttribute implements SpiProfileTransactionEvent {
value = new CountedValue<>(value, dataReader.getLong());
}
result.add(value);
dataReader.resetColumnPosition();
rowCount++;
}
@@ -43,12 +43,8 @@ public class RsetDataReader implements DataReader {
@Override
public boolean next() throws SQLException {
return rset.next();
}
@Override
public void resetColumnPosition() {
pos = 0;
return rset.next();
}
@Override
@@ -75,7 +71,6 @@ public class RsetDataReader implements DataReader {
return rset.getBigDecimal(pos());
}
@Override
public InputStream getBinaryStream() throws SQLException {
return rset.getBinaryStream(pos());
@@ -84,19 +79,13 @@ public class RsetDataReader implements DataReader {
@Override
public Boolean getBoolean() throws SQLException {
boolean v = rset.getBoolean(pos());
if (rset.wasNull()) {
return null;
}
return v;
return rset.wasNull() ? null : v;
}
@Override
public Byte getByte() throws SQLException {
byte v = rset.getByte(pos());
if (rset.wasNull()) {
return null;
}
return v;
return rset.wasNull() ? null : v;
}
@Override
@@ -117,62 +106,42 @@ public class RsetDataReader implements DataReader {
@Override
public Double getDouble() throws SQLException {
double v = rset.getDouble(pos());
if (rset.wasNull()) {
return null;
}
return v;
return rset.wasNull() ? null : v;
}
@Override
public Float getFloat() throws SQLException {
float v = rset.getFloat(pos());
if (rset.wasNull()) {
return null;
}
return v;
return rset.wasNull() ? null : v;
}
@Override
public Integer getInt() throws SQLException {
int v = rset.getInt(pos());
if (rset.wasNull()) {
return null;
}
return v;
return rset.wasNull() ? null : v;
}
@Override
public Long getLong() throws SQLException {
long v = rset.getLong(pos());
if (rset.wasNull()) {
return null;
}
return v;
return rset.wasNull() ? null : v;
}
public Ref getRef() throws SQLException {
return rset.getRef(pos());
}
@Override
public Short getShort() throws SQLException {
short s = rset.getShort(pos());
if (rset.wasNull()) {
return null;
}
return s;
return rset.wasNull() ? null : s;
}
@Override
public String getString() throws SQLException {
return rset.getString(pos());
}
@Override
public Time getTime() throws SQLException {
Calendar cal = dataTimeZone.getTimeZone();
@@ -196,14 +165,10 @@ public class RsetDataReader implements DataReader {
@Override
public String getStringFromStream() throws SQLException {
Reader reader = rset.getCharacterStream(pos());
if (reader == null) {
return null;
}
return readStringLob(reader);
return reader == null ? null : readStringLob(reader);
}
protected String readStringLob(Reader reader) throws SQLException {
char[] buffer = new char[clobBufferSize];
int readLength;
StringBuilder out = new StringBuilder(stringInitialSize);
@@ -215,7 +180,6 @@ public class RsetDataReader implements DataReader {
} catch (IOException e) {
throw new SQLException("IOException reading Clob " + e.getMessage());
}
return out.toString();
}
@@ -226,19 +190,16 @@ public class RsetDataReader implements DataReader {
}
protected byte[] getBinaryLob(InputStream in) throws SQLException {
if (in == null) {
return null;
}
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
byte[] buf = new byte[bufferSize];
int len;
while ((len = in.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, len);
}
byte[] data = out.toByteArray();
if (data.length == 0) {
data = null;
}