use of nl.topicus.jdbc.MetaDataStore.TableKeyMetaData in project spanner-jdbc by olavloite.
the class AbstractCloudSpannerStatement method createInsertSelectOnDuplicateKeyUpdateStatement.
/**
* Transform the given UPDATE-statement into an "INSERT INTO TAB1 (...)
* SELECT ... FROM TAB1 WHERE ... ON DUPLICATE KEY UPDATE"
*
* @param update
* The UPDATE-statement
* @return An SQL-statement equal to the UPDATE-statement but in INSERT form
* @throws SQLException
* if a database exception occurs while getting the table meta
* data or if the statement tries to update the primary key
* value
*/
protected String createInsertSelectOnDuplicateKeyUpdateStatement(Update update) throws SQLException {
String tableName = unquoteIdentifier(update.getTables().get(0).getName());
TableKeyMetaData table = getConnection().getTable(tableName);
List<String> keyColumns = table.getKeyColumns();
List<String> updateColumns = update.getColumns().stream().map(Column::getColumnName).map(String::toUpperCase).collect(Collectors.toList());
List<String> quotedKeyColumns = keyColumns.stream().map(this::quoteIdentifier).collect(Collectors.toList());
List<String> quotedAndQualifiedKeyColumns = keyColumns.stream().map(x -> quoteIdentifier(tableName) + "." + quoteIdentifier(x)).collect(Collectors.toList());
List<String> quotedUpdateColumns = updateColumns.stream().map(this::quoteIdentifier).collect(Collectors.toList());
List<String> expressions = update.getExpressions().stream().map(Object::toString).collect(Collectors.toList());
if (updateColumns.stream().anyMatch(keyColumns::contains)) {
String invalidCols = updateColumns.stream().filter(keyColumns::contains).collect(Collectors.joining());
throw new CloudSpannerSQLException("UPDATE of a primary key value is not allowed, cannot UPDATE the column(s) " + invalidCols, Code.INVALID_ARGUMENT);
}
StringBuilder res = new StringBuilder();
res.append("INSERT INTO ").append(quoteIdentifier(tableName)).append("\n(");
res.append(String.join(", ", quotedKeyColumns)).append(", ");
res.append(String.join(", ", quotedUpdateColumns)).append(")");
res.append("\nSELECT ").append(String.join(", ", quotedAndQualifiedKeyColumns)).append(", ");
res.append(String.join(", ", expressions));
res.append("\nFROM ").append(quoteIdentifier(tableName));
if (update.getWhere() != null)
res.append("\n").append("WHERE ").append(update.getWhere().toString());
res.append("\nON DUPLICATE KEY UPDATE");
return res.toString();
}
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.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(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