Search in sources :

Example 1 with CloudSpannerSQLException

use of nl.topicus.jdbc.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.

the class CloudSpannerConnection method setReadOnly.

@Override
public void setReadOnly(boolean readOnly) throws SQLException {
    checkClosed();
    if (transaction.isRunning())
        throw new CloudSpannerSQLException("There is currently a transaction running. Commit or rollback the running transaction before changing read-only mode.", Code.FAILED_PRECONDITION);
    this.readOnly = readOnly;
}
Also used : CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 2 with CloudSpannerSQLException

use of nl.topicus.jdbc.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.

the class CloudSpannerStatement method execute.

@Override
public boolean execute(String sql) throws SQLException {
    String[] sqlTokens = getTokens(sql);
    CustomDriverStatement custom = getCustomDriverStatement(sqlTokens);
    if (custom != null)
        return custom.execute(sqlTokens);
    Statement statement = null;
    boolean ddl = isDDLStatement(sqlTokens);
    if (!ddl) {
        try {
            statement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
        } catch (JSQLParserException | TokenMgrException e) {
            throw new CloudSpannerSQLException("Error while parsing sql statement " + sql + ": " + e.getLocalizedMessage(), Code.INVALID_ARGUMENT, e);
        }
    }
    if (!ddl && statement instanceof Select) {
        determineForceSingleUseReadContext((Select) statement);
        if (!isForceSingleUseReadContext() && getConnection().isBatchReadOnly()) {
            List<Partition> partitions = partitionQuery(com.google.cloud.spanner.Statement.of(sql));
            currentResultSets = new ArrayList<>(partitions.size());
            for (Partition p : partitions) {
                currentResultSets.add(new CloudSpannerPartitionResultSet(this, getBatchReadOnlyTransaction(), p, sql));
            }
        } else {
            try (ReadContext context = getReadContext()) {
                com.google.cloud.spanner.ResultSet rs = context.executeQuery(com.google.cloud.spanner.Statement.of(sql));
                currentResultSets = Arrays.asList(new CloudSpannerResultSet(this, rs, sql));
                currentResultSetIndex = 0;
                lastUpdateCount = -1;
            }
        }
        return true;
    } else {
        lastUpdateCount = executeUpdate(sql);
        currentResultSetIndex = 0;
        currentResultSets = null;
        return false;
    }
}
Also used : Partition(com.google.cloud.spanner.Partition) PreparedStatement(java.sql.PreparedStatement) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) CloudSpannerPartitionResultSet(nl.topicus.jdbc.resultset.CloudSpannerPartitionResultSet) TokenMgrException(net.sf.jsqlparser.parser.TokenMgrException) ReadContext(com.google.cloud.spanner.ReadContext) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 3 with CloudSpannerSQLException

use of nl.topicus.jdbc.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.

the class CloudSpannerPreparedStatement method execute.

@Override
public boolean execute() throws SQLException {
    CustomDriverStatement custom = getCustomDriverStatement(sqlTokens);
    if (custom != null)
        return custom.execute(sqlTokens);
    Statement statement = null;
    boolean ddl = isDDLStatement();
    if (!ddl) {
        try {
            statement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
        } catch (JSQLParserException | TokenMgrException e) {
            throw new CloudSpannerSQLException(PARSE_ERROR + sql + ": " + e.getLocalizedMessage(), Code.INVALID_ARGUMENT, e);
        }
    }
    if (!ddl && statement instanceof Select) {
        determineForceSingleUseReadContext((Select) statement);
        com.google.cloud.spanner.Statement.Builder builder = createSelectBuilder(statement, sql);
        if (!isForceSingleUseReadContext() && getConnection().isBatchReadOnly()) {
            List<Partition> partitions = partitionQuery(builder.build());
            currentResultSets = new ArrayList<>(partitions.size());
            for (Partition p : partitions) {
                currentResultSets.add(new CloudSpannerPartitionResultSet(this, getBatchReadOnlyTransaction(), p, sql));
            }
        } else {
            try (ReadContext context = getReadContext()) {
                com.google.cloud.spanner.ResultSet rs = context.executeQuery(builder.build());
                currentResultSets = Arrays.asList(new CloudSpannerResultSet(this, rs, sql));
                currentResultSetIndex = 0;
                lastUpdateCount = -1;
            }
        }
        return true;
    } else {
        lastUpdateCount = executeUpdate();
        currentResultSets = null;
        currentResultSetIndex = 0;
        return false;
    }
}
Also used : Partition(com.google.cloud.spanner.Partition) PreparedStatement(java.sql.PreparedStatement) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) CloudSpannerPartitionResultSet(nl.topicus.jdbc.resultset.CloudSpannerPartitionResultSet) TokenMgrException(net.sf.jsqlparser.parser.TokenMgrException) 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 4 with CloudSpannerSQLException

use of nl.topicus.jdbc.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.

the class CloudSpannerPreparedStatement method createInsertWithSelectStatement.

private InsertWorker createInsertWithSelectStatement(Insert insert, boolean forceUpdate) throws SQLException {
    Select select = insert.getSelect();
    if (select == null) {
        throw new CloudSpannerSQLException("Insert statement must contain a select statement", Code.INVALID_ARGUMENT);
    }
    boolean isDuplicate = insert.isUseDuplicate();
    InsertWorker.DMLOperation mode;
    if (forceUpdate)
        mode = DMLOperation.UPDATE;
    else if (isDuplicate)
        mode = DMLOperation.ONDUPLICATEKEYUPDATE;
    else
        mode = DMLOperation.INSERT;
    return new InsertWorker(getConnection(), select, insert, getParameterStore(), getConnection().isAllowExtendedMode(), mode);
}
Also used : PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 5 with CloudSpannerSQLException

use of nl.topicus.jdbc.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.

the class CloudSpannerTransaction method setSavepoint.

public void setSavepoint(Savepoint savepoint) throws SQLException {
    Preconditions.checkNotNull(savepoint);
    checkTransaction();
    if (transactionThread == null)
        throw new CloudSpannerSQLException(SAVEPOINTS_NOT_IN_READ_ONLY, Code.FAILED_PRECONDITION);
    transactionThread.setSavepoint(savepoint);
}
Also used : CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Aggregations

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