Search in sources :

Example 16 with CloudSpannerSQLException

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();
}
Also used : ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 17 with CloudSpannerSQLException

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);
}
Also used : Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) TokenMgrError(net.sf.jsqlparser.parser.TokenMgrError) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) ReadContext(com.google.cloud.spanner.ReadContext) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 18 with CloudSpannerSQLException

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");
}
Also used : CloudSpannerXADataSource(nl.topicus.jdbc.CloudSpannerXADataSource) CloudSpannerXAConnection(nl.topicus.jdbc.xa.CloudSpannerXAConnection) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) SQLException(java.sql.SQLException) XAException(javax.transaction.xa.XAException)

Aggregations

CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)18 Select (net.sf.jsqlparser.statement.select.Select)6 SQLException (java.sql.SQLException)5 JSQLParserException (net.sf.jsqlparser.JSQLParserException)5 Code (com.google.rpc.Code)4 Statement (net.sf.jsqlparser.statement.Statement)4 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)4 SpannerException (com.google.cloud.spanner.SpannerException)3 TokenMgrError (net.sf.jsqlparser.parser.TokenMgrError)3 Column (net.sf.jsqlparser.schema.Column)3 SubSelect (net.sf.jsqlparser.statement.select.SubSelect)3 CloudSpannerConnection (nl.topicus.jdbc.CloudSpannerConnection)3 CloudSpannerDriver (nl.topicus.jdbc.CloudSpannerDriver)3 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)2 ReadContext (com.google.cloud.spanner.ReadContext)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2