Search in sources :

Example 6 with CloudSpannerSQLException

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

the class CloudSpannerPooledConnection method fireConnectionError.

/**
 * Fires a connection error event, but only if we think the exception is
 * fatal.
 *
 * @param e
 *            the SQLException to consider
 */
private void fireConnectionError(SQLException e) {
    Code code = Code.UNKNOWN;
    if (e instanceof CloudSpannerSQLException) {
        code = ((CloudSpannerSQLException) e).getCode();
    }
    if (!isFatalState(code)) {
        return;
    }
    fireConnectionFatalError(e);
}
Also used : CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) Code(com.google.rpc.Code)

Example 7 with CloudSpannerSQLException

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

the class CloudSpannerPooledConnection method getConnection.

/**
 * Gets a handle for a client to use. This is a wrapper around the physical
 * connection, so the client can call close and it will just return the
 * connection to the pool without really closing the physical connection.
 *
 * <p>
 * According to the JDBC 2.0 Optional Package spec (6.2.3), only one client
 * may have an active handle to the connection at a time, so if there is a
 * previous handle active when this is called, the previous one is forcibly
 * closed and its work rolled back.
 * </p>
 */
@Override
public ICloudSpannerConnection getConnection() throws SQLException {
    if (con == null) {
        // Before throwing the exception, let's notify the registered
        // listeners about the error
        SQLException sqlException = new CloudSpannerSQLException("This PooledConnection has already been closed.", Code.FAILED_PRECONDITION);
        fireConnectionFatalError(sqlException);
        throw sqlException;
    }
    // eliminate bad pooled connections.
    try {
        // Package spec section 6.2.3
        if (last != null) {
            last.close();
            if (!con.getAutoCommit()) {
                rollbackAndIgnoreException();
            }
            con.clearWarnings();
        }
        /*
			 * In XA-mode, autocommit is handled in PGXAConnection, because it
			 * depends on whether an XA-transaction is open or not
			 */
        if (!isXA) {
            con.setAutoCommit(autoCommit);
        }
    } catch (SQLException sqlException) {
        fireConnectionFatalError(sqlException);
        throw (SQLException) sqlException.fillInStackTrace();
    }
    ConnectionHandler handler = new ConnectionHandler(con);
    last = handler;
    ICloudSpannerConnection proxyCon = (ICloudSpannerConnection) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { Connection.class, ICloudSpannerConnection.class }, handler);
    last.setProxy(proxyCon);
    return proxyCon;
}
Also used : CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 8 with CloudSpannerSQLException

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

the class CloudSpannerResultSet method getArray.

@Override
public Array getArray(int columnIndex) throws SQLException {
    if (isNull(columnIndex))
        return null;
    Type type = resultSet.getColumnType(columnIndex - 1);
    if (type.getCode() != Code.ARRAY)
        throw new CloudSpannerSQLException("Column with index " + columnIndex + " does not contain an array", com.google.rpc.Code.INVALID_ARGUMENT);
    CloudSpannerDataType dataType = CloudSpannerDataType.getType(type.getArrayElementType().getCode());
    List<? extends Object> elements = dataType.getArrayElements(resultSet, columnIndex - 1);
    return CloudSpannerArray.createArray(dataType, elements);
}
Also used : Type(com.google.cloud.spanner.Type) CloudSpannerDataType(nl.topicus.jdbc.CloudSpannerDataType) CloudSpannerDataType(nl.topicus.jdbc.CloudSpannerDataType) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 9 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 | TokenMgrError e) {
            throw new CloudSpannerSQLException(PARSE_ERROR + sql + ": " + e.getLocalizedMessage(), Code.INVALID_ARGUMENT, e);
        }
    }
    if (!ddl && statement instanceof Select) {
        lastResultSet = executeQuery();
        lastUpdateCount = -1;
        return true;
    } else {
        lastUpdateCount = executeUpdate();
        lastResultSet = null;
        return false;
    }
}
Also used : Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) Select(net.sf.jsqlparser.statement.select.Select) TokenMgrError(net.sf.jsqlparser.parser.TokenMgrError) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 10 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)

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