Java Code Examples for com.google.rpc.Code#FAILED_PRECONDITION

The following examples show how to use com.google.rpc.Code#FAILED_PRECONDITION . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CloudSpannerConnection.java    From spanner-jdbc with MIT License 6 votes vote down vote up
@Override
public int setBatchReadOnly(boolean batchReadOnly) throws SQLException {
  checkClosed();
  if (batchReadOnly != this.batchReadOnly) {
    if (getAutoCommit()) {
      throw new CloudSpannerSQLException(
          "The connection is currently in auto-commit mode. Please turn off auto-commit before changing batch read-only mode.",
          Code.FAILED_PRECONDITION);
    }
    if (getTransaction().isRunning()) {
      throw new CloudSpannerSQLException(
          "There is currently a transaction running. Commit or rollback the running transaction before changing batch read-only mode.",
          Code.FAILED_PRECONDITION);
    }
  }
  this.batchReadOnly = batchReadOnly;
  return 1;
}
 
Example 2
Source File: CloudSpannerConnection.java    From spanner-jdbc with MIT License 6 votes vote down vote up
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
  checkClosed();
  if (readOnly != this.readOnly) {
    if (getTransaction().isRunning()) {
      throw new CloudSpannerSQLException(
          "There is currently a transaction running. Commit or rollback the running transaction before changing read-only mode.",
          Code.FAILED_PRECONDITION);
    }
    if (isBatchReadOnly()) {
      throw new CloudSpannerSQLException(
          "The connection is currently in batch read-only mode. Please turn off batch read-only before changing read-only mode.",
          Code.FAILED_PRECONDITION);
    }
  }
  this.readOnly = readOnly;
}
 
Example 3
Source File: CloudSpannerConnection.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private void checkSavepointPossible() throws SQLException {
  checkClosed();
  if (getAutoCommit())
    throw new CloudSpannerSQLException("Savepoints are not supported in autocommit mode",
        Code.FAILED_PRECONDITION);
  if (isReadOnly() || isBatchReadOnly())
    throw new CloudSpannerSQLException("Savepoints are not supported in read-only mode",
        Code.FAILED_PRECONDITION);
}
 
Example 4
Source File: CloudSpannerTransaction.java    From spanner-jdbc with MIT License 5 votes vote down vote up
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);
}
 
Example 5
Source File: CloudSpannerPooledConnection.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  final String methodName = method.getName();
  // From Object
  if (method.getDeclaringClass().equals(Object.class)) {
    return handleInvokeObjectMethod(proxy, method, args);
  }

  // All the rest is from the Statement interface
  if (methodName.equals("isClosed")) {
    return st == null || st.isClosed();
  }
  if (methodName.equals("close")) {
    return handleInvokeClose();
  }
  if (st == null || st.isClosed()) {
    throw new CloudSpannerSQLException("Statement has been closed.", Code.FAILED_PRECONDITION);
  }
  if (methodName.equals("getConnection")) {
    return con.getProxy(); // the proxied connection, not a physical
                           // connection
  }

  // Delegate the call to the proxied Statement.
  try {
    return method.invoke(st, args);
  } catch (final InvocationTargetException ite) {
    final Throwable te = ite.getTargetException();
    if (te instanceof SQLException) {
      fireConnectionError((SQLException) te); // Tell listeners
                                              // about exception
                                              // if it's fatal
    }
    throw te;
  }
}
 
Example 6
Source File: CloudSpannerXAConnection.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  if (state != STATE_IDLE) {
    String methodName = method.getName();
    if (methodName.equals("commit") || methodName.equals("rollback")
        || methodName.equals("setSavePoint")
        || (methodName.equals("setAutoCommit") && (Boolean) args[0])) {
      throw new CloudSpannerSQLException(
          CloudSpannerXAException.COMMIT_METHODS_NOT_ALLOWED_WHEN_ACTIVE,
          Code.FAILED_PRECONDITION);
    }
  }
  try {
    /*
     * If the argument to equals-method is also a wrapper, present the original unwrapped
     * connection to the underlying equals method.
     */
    if (method.getName().equals("equals")) {
      Object arg = args[0];
      if (Proxy.isProxyClass(arg.getClass())) {
        InvocationHandler h = Proxy.getInvocationHandler(arg);
        if (h instanceof ConnectionHandler) {
          // unwrap argument
          args = new Object[] {((ConnectionHandler) h).con};
        }
      }
    }

    return method.invoke(con, args);
  } catch (InvocationTargetException ex) {
    throw ex.getTargetException();
  }
}
 
Example 7
Source File: CloudSpannerXAConnection.java    From spanner-jdbc with MIT License 5 votes vote down vote up
/**
 * Preconditions: 1. Flags is one of TMSUCCESS, TMFAIL, TMSUSPEND 2. xid != null 3. Connection is
 * associated with transaction xid
 *
 * Implementation deficiency preconditions: 1. Flags is not TMSUSPEND
 *
 * Postconditions: 1. connection is disassociated from the transaction.
 * 
 * @see XAResource#end(Xid, int)
 */
@Override
public void end(Xid xid, int flags) throws XAException {
  if (logger.logDebug()) {
    debug("ending transaction xid = " + xid);
  }

  // Check preconditions

  if (flags != XAResource.TMSUSPEND && flags != XAResource.TMFAIL
      && flags != XAResource.TMSUCCESS) {
    throw new CloudSpannerXAException(CloudSpannerXAException.INVALID_FLAGS,
        Code.INVALID_ARGUMENT, XAException.XAER_INVAL);
  }

  if (xid == null) {
    throw new CloudSpannerXAException(CloudSpannerXAException.XID_NOT_NULL, Code.INVALID_ARGUMENT,
        XAException.XAER_INVAL);
  }

  if (state != STATE_ACTIVE || !currentXid.equals(xid)) {
    throw new CloudSpannerXAException(CloudSpannerXAException.END_WITHOUT_START,
        Code.FAILED_PRECONDITION, XAException.XAER_PROTO);
  }

  // Check implementation deficiency preconditions
  if (flags == XAResource.TMSUSPEND) {
    throw new CloudSpannerXAException(CloudSpannerXAException.SUSPEND_NOT_IMPLEMENTED,
        Code.UNIMPLEMENTED, XAException.XAER_RMERR);
  }

  // We ignore TMFAIL. It's just a hint to the RM. We could roll back
  // immediately
  // if TMFAIL was given.

  // All clear. We don't have any real work to do.
  state = STATE_ENDED;
}
 
Example 8
Source File: AbstractCloudSpannerStatement.java    From spanner-jdbc with MIT License 5 votes vote down vote up
protected long writeMutations(Mutations mutations) throws SQLException {
  if (connection.isReadOnly()) {
    throw new CloudSpannerSQLException(NO_MUTATIONS_IN_READ_ONLY_MODE_EXCEPTION,
        Code.FAILED_PRECONDITION);
  }
  if (mutations.isWorker()) {
    ConversionResult result = mutations.getWorker().call();
    if (result.getException() != null) {
      if (result.getException() instanceof SQLException)
        throw (SQLException) result.getException();
      if (result.getException() instanceof SpannerException)
        throw new CloudSpannerSQLException((SpannerException) result.getException());
      throw new CloudSpannerSQLException(result.getException().getMessage(), Code.UNKNOWN,
          result.getException());
    }
  } else {

    if (connection.getAutoCommit()) {
      dbClient.readWriteTransaction().run(new TransactionCallable<Void>() {

        @Override
        public Void run(TransactionContext transaction) throws Exception {
          transaction.buffer(mutations.getMutations());
          return null;
        }
      });
    } else {
      connection.getTransaction().buffer(mutations.getMutations());
    }
  }
  return mutations.getNumberOfResults();
}
 
Example 9
Source File: CloudSpannerSavepoint.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Override
public int getSavepointId() throws SQLException {
  if (isNamed())
    throw new CloudSpannerSQLException("This is a named savepoint", Code.FAILED_PRECONDITION);
  return id;
}
 
Example 10
Source File: CloudSpannerPreparedStatement.java    From spanner-jdbc with MIT License 4 votes vote down vote up
private Mutations createMutations(String sql, boolean forceUpdate,
    boolean generateParameterMetaData) throws SQLException {
  try {
    if (getConnection().isReadOnly()) {
      throw new CloudSpannerSQLException(NO_MUTATIONS_IN_READ_ONLY_MODE_EXCEPTION,
          Code.FAILED_PRECONDITION);
    }
    if (isDDLStatement()) {
      throw new CloudSpannerSQLException(
          "Cannot create mutation for DDL statement. Expected INSERT, UPDATE or DELETE",
          Code.INVALID_ARGUMENT);
    }
    Statement statement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
    if (statement instanceof Insert) {
      Insert insertStatement = (Insert) statement;
      if (generateParameterMetaData || insertStatement.getSelect() == null)
        return new Mutations(createInsertMutation(insertStatement, generateParameterMetaData));
      return new Mutations(createInsertWithSelectStatement(insertStatement, forceUpdate));
    } else if (statement instanceof Update) {
      Update updateStatement = (Update) statement;
      if (updateStatement.getSelect() != null)
        throw new CloudSpannerSQLException(
            "UPDATE statement using SELECT is not supported. Try to re-write the statement as an INSERT INTO ... SELECT A, B, C FROM TABLE WHERE ... ON DUPLICATE KEY UPDATE",
            Code.INVALID_ARGUMENT);
      if (updateStatement.getTables().size() > 1)
        throw new CloudSpannerSQLException(
            "UPDATE statement using multiple tables is not supported. Try to re-write the statement as an INSERT INTO ... SELECT A, B, C FROM TABLE WHERE ... ON DUPLICATE KEY UPDATE",
            Code.INVALID_ARGUMENT);

      if (generateParameterMetaData || isSingleRowWhereClause(
          getConnection()
              .getTable(unquoteIdentifier(updateStatement.getTables().get(0).getName())),
          updateStatement.getWhere()))
        return new Mutations(createUpdateMutation(updateStatement, generateParameterMetaData));
      // Translate into an 'INSERT ... SELECT ... ON DUPLICATE KEY
      // UPDATE'-statement
      String insertSQL = createInsertSelectOnDuplicateKeyUpdateStatement(updateStatement);
      return createMutations(insertSQL, true, false);
    } else if (statement instanceof Delete) {
      Delete deleteStatement = (Delete) statement;
      if (generateParameterMetaData || deleteStatement.getWhere() == null
          || isSingleRowWhereClause(
              getConnection().getTable(unquoteIdentifier(deleteStatement.getTable().getName())),
              deleteStatement.getWhere()))
        return new Mutations(createDeleteMutation(deleteStatement, generateParameterMetaData));
      return new Mutations(createDeleteWorker(deleteStatement));
    } else {
      throw new CloudSpannerSQLException(
          "Unrecognized or unsupported SQL-statment: Expected one of INSERT, UPDATE or DELETE. Please note that batching of prepared statements is not supported for SELECT-statements.",
          Code.INVALID_ARGUMENT);
    }
  } catch (JSQLParserException | IllegalArgumentException | TokenMgrException e) {
    throw new CloudSpannerSQLException(PARSE_ERROR + sql + ": " + e.getLocalizedMessage(),
        Code.INVALID_ARGUMENT, e);
  }
}
 
Example 11
Source File: CloudSpannerPreparedStatement.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Override
public boolean execute(String sql, String[] columnNames) throws SQLException {
  throw new CloudSpannerSQLException(METHOD_NOT_ON_PREPARED_STATEMENT, Code.FAILED_PRECONDITION);
}
 
Example 12
Source File: CloudSpannerPreparedStatement.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Override
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
  throw new CloudSpannerSQLException(METHOD_NOT_ON_PREPARED_STATEMENT, Code.FAILED_PRECONDITION);
}
 
Example 13
Source File: CloudSpannerPreparedStatement.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Override
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
  throw new CloudSpannerSQLException(METHOD_NOT_ON_PREPARED_STATEMENT, Code.FAILED_PRECONDITION);
}
 
Example 14
Source File: CloudSpannerPreparedStatement.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Override
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
  throw new CloudSpannerSQLException(METHOD_NOT_ON_PREPARED_STATEMENT, Code.FAILED_PRECONDITION);
}
 
Example 15
Source File: CloudSpannerPreparedStatement.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Override
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
  throw new CloudSpannerSQLException(METHOD_NOT_ON_PREPARED_STATEMENT, Code.FAILED_PRECONDITION);
}
 
Example 16
Source File: CloudSpannerPreparedStatement.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Override
public int executeUpdate(String sql) throws SQLException {
  throw new CloudSpannerSQLException(METHOD_NOT_ON_PREPARED_STATEMENT, Code.FAILED_PRECONDITION);
}
 
Example 17
Source File: CloudSpannerPreparedStatement.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Override
public ResultSet executeQuery(String sql) throws SQLException {
  throw new CloudSpannerSQLException(
      "The executeQuery(String sql)-method may not be called on a PreparedStatement",
      Code.FAILED_PRECONDITION);
}
 
Example 18
Source File: CloudSpannerSavepoint.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Override
public String getSavepointName() throws SQLException {
  if (!isNamed())
    throw new CloudSpannerSQLException("This is not a named savepoint", Code.FAILED_PRECONDITION);
  return name;
}
 
Example 19
Source File: CloudSpannerArray.java    From spanner-jdbc with MIT License 4 votes vote down vote up
private void checkFree() throws SQLException {
  if (data == null) {
    throw new CloudSpannerSQLException(FREE_EXCEPTION, Code.FAILED_PRECONDITION);
  }
}
 
Example 20
Source File: AbstractCloudSpannerConnection.java    From spanner-jdbc with MIT License 4 votes vote down vote up
protected void checkClosed() throws SQLException {
  if (isClosed()) {
    throw new CloudSpannerSQLException(CONNECTION_CLOSED, Code.FAILED_PRECONDITION);
  }
}