Search in sources :

Example 1 with TableKeyMetaData

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();
}
Also used : TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData)

Example 2 with TableKeyMetaData

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);
    }
}
Also used : TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) CCJSqlParserUtil(net.sf.jsqlparser.parser.CCJSqlParserUtil) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Table(net.sf.jsqlparser.schema.Table) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) Collectors(java.util.stream.Collectors) JSQLParserException(net.sf.jsqlparser.JSQLParserException) TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) SQLException(java.sql.SQLException) List(java.util.List) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerDriver(nl.topicus.jdbc.CloudSpannerDriver) Code(com.google.rpc.Code) Delete(net.sf.jsqlparser.statement.delete.Delete) JSQLParserException(net.sf.jsqlparser.JSQLParserException) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 3 with TableKeyMetaData

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());
    }
}
Also used : TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) Field(java.lang.reflect.Field) Map(java.util.Map) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Example 4 with TableKeyMetaData

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);
}
Also used : CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) CloudSpannerDatabaseMetaData(nl.topicus.jdbc.CloudSpannerDatabaseMetaData) Returns(org.mockito.internal.stubbing.answers.Returns) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 5 with TableKeyMetaData

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;
}
Also used : TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) Returns(org.mockito.internal.stubbing.answers.Returns) CloudSpannerDatabaseMetaData(nl.topicus.jdbc.CloudSpannerDatabaseMetaData) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) ResultSet(java.sql.ResultSet) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) CloudSpannerTransaction(nl.topicus.jdbc.transaction.CloudSpannerTransaction) Logger(nl.topicus.jdbc.Logger)

Aggregations

TableKeyMetaData (nl.topicus.jdbc.MetaDataStore.TableKeyMetaData)7 CloudSpannerConnection (nl.topicus.jdbc.CloudSpannerConnection)4 Code (com.google.rpc.Code)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Table (net.sf.jsqlparser.schema.Table)2 Select (net.sf.jsqlparser.statement.select.Select)2 CloudSpannerDatabaseMetaData (nl.topicus.jdbc.CloudSpannerDatabaseMetaData)2 CloudSpannerDriver (nl.topicus.jdbc.CloudSpannerDriver)2 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)2 CloudSpannerResultSet (nl.topicus.jdbc.resultset.CloudSpannerResultSet)2 UnitTest (nl.topicus.jdbc.test.category.UnitTest)2 Test (org.junit.Test)2 Returns (org.mockito.internal.stubbing.answers.Returns)2 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)1 DatabaseClient (com.google.cloud.spanner.DatabaseClient)1 Partition (com.google.cloud.spanner.Partition)1 PartitionOptions (com.google.cloud.spanner.PartitionOptions)1