use of nl.topicus.jdbc.MetaDataStore.TableKeyMetaData in project spanner-jdbc by olavloite.
the class DeleteWorker method createSQL.
@Override
protected String createSQL() throws SQLException {
TableKeyMetaData table = connection.getTable(CloudSpannerDriver.unquoteIdentifier(getTable().getName()));
List<String> keyCols = table.getKeyColumns().stream().map(CloudSpannerDriver::quoteIdentifier).collect(Collectors.toList());
StringBuilder sql = new StringBuilder("DELETE FROM ").append(CloudSpannerDriver.quoteIdentifier(delete.getTable().getName())).append(" WHERE ");
boolean first = true;
for (String key : keyCols) {
if (!first)
sql.append(" AND ");
sql.append(key).append("=?");
first = false;
}
return sql.toString();
}
use of nl.topicus.jdbc.MetaDataStore.TableKeyMetaData in project spanner-jdbc by olavloite.
the class DeleteWorker method createSelect.
private static Select createSelect(CloudSpannerConnection connection, Delete delete) throws SQLException {
TableKeyMetaData table = connection.getTable(CloudSpannerDriver.unquoteIdentifier(delete.getTable().getName()));
List<String> keyCols = table.getKeyColumns().stream().map(x -> CloudSpannerDriver.quoteIdentifier(delete.getTable().getName()) + "." + CloudSpannerDriver.quoteIdentifier(x)).collect(Collectors.toList());
StringBuilder sql = new StringBuilder();
sql.append("SELECT ").append(String.join(", ", keyCols));
sql.append("\nFROM ").append(CloudSpannerDriver.quoteIdentifier(delete.getTable().getName()));
sql.append("\nWHERE ").append(delete.getWhere().toString());
try {
return (Select) CCJSqlParserUtil.parse(sql.toString());
} catch (JSQLParserException e) {
throw new CloudSpannerSQLException("Could not parse generated SELECT statement: " + sql, Code.INVALID_ARGUMENT);
}
}
use of nl.topicus.jdbc.MetaDataStore.TableKeyMetaData in project spanner-jdbc by olavloite.
the class MetaDataStoreTest method testClear.
@Test
public void testClear() throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
for (int i = 0; i < 2; i++) {
MetaDataStore subject = new MetaDataStore(CloudSpannerTestObjects.createConnection());
TableKeyMetaData fooUpperCase = subject.getTable("FOO");
TableKeyMetaData fooLowerCase = subject.getTable("foo");
TableKeyMetaData bar = subject.getTable("Bar");
assertNotNull(fooLowerCase);
assertNotNull(fooUpperCase);
assertNotNull(bar);
assertEquals(fooLowerCase, fooUpperCase);
Assert.assertNotEquals(fooLowerCase, bar);
Field field = MetaDataStore.class.getDeclaredField("tables");
field.setAccessible(true);
@SuppressWarnings("unchecked") Map<String, TableKeyMetaData> map = (Map<String, TableKeyMetaData>) field.get(subject);
assertEquals(2, map.size());
subject.clearTable("Foo");
assertEquals(1, map.size());
subject.clear();
assertEquals(0, map.size());
}
}
use of nl.topicus.jdbc.MetaDataStore.TableKeyMetaData in project spanner-jdbc by olavloite.
the class CloudSpannerParameterMetaDataTest method createStatement.
private CloudSpannerPreparedStatement createStatement(String sql) throws SQLException {
CloudSpannerConnection connection = mock(CloudSpannerConnection.class);
CloudSpannerDatabaseMetaData metadata = mock(CloudSpannerDatabaseMetaData.class);
CloudSpannerResultSet columns = mock(CloudSpannerResultSet.class);
TableKeyMetaData table = mock(TableKeyMetaData.class);
when(table.getKeyColumns()).thenReturn(Arrays.asList("COL1"));
when(connection.getTable("FOO")).thenReturn(table);
when(connection.getMetaData()).thenAnswer(new Returns(metadata));
when(metadata.getColumns(null, null, "FOO", null)).thenReturn(columns);
final ColumnsNextAnswer next = new ColumnsNextAnswer();
when(columns.next()).thenAnswer(next);
when(columns.getString("COLUMN_NAME")).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return Arrays.asList("COL1", "COL2", "COL3").get(next.nextCalled - 1);
}
});
when(columns.getInt("COLUMN_SIZE")).thenAnswer(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return Arrays.asList(8, 50, 100).get(next.nextCalled - 1);
}
});
when(columns.getInt("DATA_TYPE")).thenAnswer(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return Arrays.asList(Types.BIGINT, Types.NVARCHAR, Types.NVARCHAR).get(next.nextCalled - 1);
}
});
when(columns.getInt("NULLABLE")).thenAnswer(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return Arrays.asList(ParameterMetaData.parameterNoNulls, ParameterMetaData.parameterNoNulls, ParameterMetaData.parameterNullable).get(next.nextCalled - 1);
}
});
return new CloudSpannerPreparedStatement(sql, connection, null);
}
use of nl.topicus.jdbc.MetaDataStore.TableKeyMetaData in project spanner-jdbc by olavloite.
the class CloudSpannerTestObjects method createConnection.
public static CloudSpannerConnection createConnection() throws SQLException {
CloudSpannerConnection connection = Mockito.mock(CloudSpannerConnection.class);
Mockito.doCallRealMethod().when(connection).setAutoCommit(Mockito.anyBoolean());
Mockito.when(connection.getAutoCommit()).thenCallRealMethod();
connection.setAutoCommit(false);
Mockito.doCallRealMethod().when(connection).setBatchReadOnly(Mockito.anyBoolean());
Mockito.when(connection.isBatchReadOnly()).thenCallRealMethod();
Mockito.doCallRealMethod().when(connection).setReadOnly(Mockito.anyBoolean());
Mockito.when(connection.isReadOnly()).thenCallRealMethod();
Mockito.when(connection.isAllowExtendedMode()).thenAnswer(new Returns(true));
Mockito.when(connection.createArrayOf(Mockito.anyString(), Mockito.any())).thenCallRealMethod();
CloudSpannerDatabaseMetaData metadata = createMetaData();
Mockito.when(connection.getMetaData()).thenReturn(metadata);
CloudSpannerTransaction transaction = Mockito.mock(CloudSpannerTransaction.class);
Mockito.when(transaction.executeQuery(Mockito.any())).thenReturn(Mockito.mock(com.google.cloud.spanner.ResultSet.class));
Mockito.when(transaction.partitionQuery(Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(mock(Partition.class), mock(Partition.class), mock(Partition.class)));
Mockito.when(connection.getTransaction()).thenReturn(transaction);
TableKeyMetaData tableFoo = Mockito.mock(TableKeyMetaData.class);
Mockito.when(tableFoo.getKeyColumns()).thenAnswer(new Returns(Arrays.asList("ID")));
Mockito.when(connection.getTable(Mockito.matches(Pattern.compile("FOO", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)))).thenAnswer(new Returns(tableFoo));
TableKeyMetaData tableBar = Mockito.mock(TableKeyMetaData.class);
Mockito.when(tableBar.getKeyColumns()).thenAnswer(new Returns(Arrays.asList("ID1", "ID2")));
Mockito.when(connection.getTable(Mockito.matches(Pattern.compile("BAR", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)))).thenAnswer(new Returns(tableBar));
Mockito.when(connection.getLogger()).thenAnswer(new Returns(new Logger()));
mockXAMethods(connection);
return connection;
}
Aggregations