use of nl.topicus.jdbc.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.
the class CloudSpannerPreparedStatement method createInsertMutation.
private Mutation createInsertMutation(Insert insert, boolean generateParameterMetaData) throws SQLException {
ItemsList items = insert.getItemsList();
if (generateParameterMetaData && items == null && insert.getSelect() != null) {
// Just initialize the parameter meta data of the select statement
createSelectBuilder(insert.getSelect(), insert.getSelect().toString());
return null;
}
if (!(items instanceof ExpressionList)) {
throw new CloudSpannerSQLException("Insert statement must specify a list of values", Code.INVALID_ARGUMENT);
}
if (insert.getColumns() == null || insert.getColumns().isEmpty()) {
throw new CloudSpannerSQLException("Insert statement must specify a list of column names", Code.INVALID_ARGUMENT);
}
List<Expression> expressions = ((ExpressionList) items).getExpressions();
String table = unquoteIdentifier(insert.getTable().getFullyQualifiedName());
getParameterStore().setTable(table);
WriteBuilder builder;
if (insert.isUseDuplicate()) {
/**
* Do an insert-or-update. BUT: Cloud Spanner does not support
* supplying different values for the insert and update statements,
* meaning that only the values specified in the INSERT part of the
* statement will be considered. Anything specified in the 'ON
* DUPLICATE KEY UPDATE ...' statement will be ignored.
*/
if (this.forceUpdate)
builder = Mutation.newUpdateBuilder(table);
else
builder = Mutation.newInsertOrUpdateBuilder(table);
} else {
/**
* Just do an insert and throw an error if a row with the specified
* key alread exists.
*/
builder = Mutation.newInsertBuilder(table);
}
int index = 0;
for (Column col : insert.getColumns()) {
String columnName = unquoteIdentifier(col.getFullyQualifiedName());
expressions.get(index).accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.set(columnName), columnName));
index++;
}
return builder.build();
}
use of nl.topicus.jdbc.exception.CloudSpannerSQLException 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.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.
the class XATester method testXA.
public void testXA(String projectId, String instanceId, String database, String pvtKeyPath) throws SQLException {
log.info("Starting XA tests");
int originalLogLevel = CloudSpannerDriver.getLogLevel();
CloudSpannerDriver.setLogLevel(CloudSpannerDriver.DEBUG);
CloudSpannerXADataSource ds = new CloudSpannerXADataSource();
ds.setProjectId(projectId);
ds.setInstanceId(instanceId);
ds.setDatabase(database);
ds.setPvtKeyPath(pvtKeyPath);
ds.setAllowExtendedMode(true);
try (CloudSpannerXAConnection xaConnection = ds.getXAConnection()) {
testXATransaction(xaConnection, CommitMode.TwoPhase);
testXARollback(xaConnection);
deleteTestRow(xaConnection);
testXARecover(xaConnection);
deleteTestRow(xaConnection);
} catch (Exception e) {
throw new CloudSpannerSQLException("Exception occurred during XA tests", Code.INTERNAL, e);
} finally {
CloudSpannerDriver.setLogLevel(originalLogLevel);
}
log.info("Finished XA tests");
}
Aggregations