use of nl.topicus.jdbc.resultset.CloudSpannerResultSet in project spanner-jdbc by olavloite.
the class InsertWorkerTest method createMocks.
private void createMocks(CloudSpannerConnection connection, String selectSQL, long count, String updateSQL, boolean throwExceptionOnUpdate) throws SQLException {
when(connection.createCopyConnection()).thenAnswer(new Answer<CloudSpannerConnection>() {
@Override
public CloudSpannerConnection answer(InvocationOnMock invocation) throws Throwable {
CloudSpannerConnection copy = CloudSpannerTestObjects.createConnection();
createMocks(copy, selectSQL, count, updateSQL);
return copy;
}
});
CloudSpannerPreparedStatement countStatement = mock(CloudSpannerPreparedStatement.class);
CloudSpannerResultSet countResultSet = mock(CloudSpannerResultSet.class);
when(countResultSet.next()).thenReturn(true, false);
when(countResultSet.getLong(1)).thenReturn(count);
when(countStatement.executeQuery()).thenReturn(countResultSet);
when(connection.prepareStatement("SELECT COUNT(*) AS C FROM (" + selectSQL + ") Q")).thenReturn(countStatement);
CloudSpannerPreparedStatement selectStatement = mock(CloudSpannerPreparedStatement.class);
CloudSpannerResultSet selectResultSet = mock(CloudSpannerResultSet.class);
CloudSpannerResultSetMetaData metadata = mock(CloudSpannerResultSetMetaData.class);
when(metadata.getColumnCount()).thenReturn(3);
when(selectResultSet.next()).then(new Answer<Boolean>() {
private long called = 0;
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
called++;
if (called <= count)
return true;
return false;
}
});
when(selectResultSet.getObject(1)).then(new Returns(1L));
when(selectResultSet.getObject(2)).then(new Returns("TWO"));
when(selectResultSet.getObject(3)).then(new Returns("TO"));
when(selectResultSet.getMetaData()).thenReturn(metadata);
when(selectStatement.executeQuery()).thenReturn(selectResultSet);
when(connection.prepareStatement(selectSQL)).thenReturn(selectStatement);
CloudSpannerPreparedStatement updateStatement = mock(CloudSpannerPreparedStatement.class);
if (throwExceptionOnUpdate)
when(updateStatement.executeUpdate()).thenThrow(SQLException.class);
else
when(updateStatement.executeUpdate()).thenReturn(1);
when(connection.prepareStatement(updateSQL)).thenReturn(updateStatement);
}
use of nl.topicus.jdbc.resultset.CloudSpannerResultSet in project spanner-jdbc by olavloite.
the class CloudSpannerPreparedStatement method executeQuery.
@Override
public ResultSet executeQuery() throws SQLException {
CustomDriverStatement custom = getCustomDriverStatement(sqlTokens);
if (custom != null && custom.isQuery()) {
return custom.executeQuery(sqlTokens);
}
Statement statement;
try {
statement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
} catch (JSQLParserException | TokenMgrError e) {
throw new CloudSpannerSQLException(PARSE_ERROR + sql + ": " + e.getLocalizedMessage(), Code.INVALID_ARGUMENT, e);
}
if (statement instanceof Select) {
determineForceSingleUseReadContext((Select) statement);
com.google.cloud.spanner.Statement.Builder builder = createSelectBuilder(statement, sql);
try (ReadContext context = getReadContext()) {
com.google.cloud.spanner.ResultSet rs = context.executeQuery(builder.build());
return new CloudSpannerResultSet(this, rs);
}
}
throw new CloudSpannerSQLException("SQL statement not suitable for executeQuery. Expected SELECT-statement.", Code.INVALID_ARGUMENT);
}
use of nl.topicus.jdbc.resultset.CloudSpannerResultSet in project spanner-jdbc by olavloite.
the class CloudSpannerTestObjects method mockXAMethods.
private static void mockXAMethods(CloudSpannerConnection connection) throws SQLException {
String checkTable = null;
try {
Field field = CloudSpannerXAConnection.class.getDeclaredField("CHECK_TABLE_EXISTENCE");
field.setAccessible(true);
checkTable = (String) field.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
CloudSpannerPreparedStatement ps = Mockito.mock(CloudSpannerPreparedStatement.class);
CloudSpannerResultSet rs = Mockito.mock(CloudSpannerResultSet.class);
Mockito.when(rs.next()).thenReturn(true, false);
Mockito.when(connection.prepareStatement(checkTable)).thenAnswer(new Returns(ps));
Mockito.when(ps.executeQuery()).thenAnswer(new Returns(rs));
}
Aggregations