Search in sources :

Example 1 with SqlBeginTransactionCommand

use of org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand in project ignite by apache.

the class QueryParser method parseNative.

/**
 * Tries to parse sql query text using native parser. Only first (leading) sql command of the multi-statement is
 * actually parsed.
 *
 * @param schemaName Schema name.
 * @param qry which sql text to parse.
 * @param remainingAllowed Whether multiple statements are allowed.
 * @return Command or {@code null} if cannot parse this query.
 */
@SuppressWarnings("IfMayBeConditional")
@Nullable
private QueryParserResult parseNative(String schemaName, SqlFieldsQuery qry, boolean remainingAllowed) {
    String sql = qry.getSql();
    // Heuristic check for fast return.
    if (!INTERNAL_CMD_RE.matcher(sql.trim()).find())
        return null;
    try {
        SqlParser parser = new SqlParser(schemaName, sql);
        SqlCommand nativeCmd = parser.nextCommand();
        assert nativeCmd != null : "Empty query. Parser met end of data";
        if (!(nativeCmd instanceof SqlCreateIndexCommand || nativeCmd instanceof SqlDropIndexCommand || nativeCmd instanceof SqlBeginTransactionCommand || nativeCmd instanceof SqlCommitTransactionCommand || nativeCmd instanceof SqlRollbackTransactionCommand || nativeCmd instanceof SqlBulkLoadCommand || nativeCmd instanceof SqlAlterTableCommand || nativeCmd instanceof SqlSetStreamingCommand || nativeCmd instanceof SqlCreateUserCommand || nativeCmd instanceof SqlAlterUserCommand || nativeCmd instanceof SqlDropUserCommand || nativeCmd instanceof SqlKillQueryCommand || nativeCmd instanceof SqlKillComputeTaskCommand || nativeCmd instanceof SqlKillServiceCommand || nativeCmd instanceof SqlKillTransactionCommand || nativeCmd instanceof SqlKillScanQueryCommand || nativeCmd instanceof SqlKillContinuousQueryCommand || nativeCmd instanceof SqlAnalyzeCommand || nativeCmd instanceof SqlRefreshStatitsicsCommand || nativeCmd instanceof SqlDropStatisticsCommand))
            return null;
        SqlFieldsQuery newQry = cloneFieldsQuery(qry).setSql(parser.lastCommandSql());
        QueryDescriptor newPlanKey = queryDescriptor(schemaName, newQry);
        SqlFieldsQuery remainingQry = null;
        if (!F.isEmpty(parser.remainingSql())) {
            checkRemainingAllowed(remainingAllowed);
            remainingQry = cloneFieldsQuery(qry).setSql(parser.remainingSql()).setArgs(qry.getArgs());
        }
        QueryParserResultCommand cmd = new QueryParserResultCommand(nativeCmd, null, false);
        return new QueryParserResult(newPlanKey, queryParameters(newQry), remainingQry, // Currently none of native statements supports parameters.
        Collections.emptyList(), null, null, cmd);
    } catch (SqlStrictParseException e) {
        throw new IgniteSQLException(e.getMessage(), e.errorCode(), e);
    } catch (Exception e) {
        // Cannot parse, return.
        if (log.isDebugEnabled())
            log.debug("Failed to parse SQL with native parser [qry=" + sql + ", err=" + e + ']');
        if (!IgniteSystemProperties.getBoolean(IgniteSystemProperties.IGNITE_SQL_PARSER_DISABLE_H2_FALLBACK))
            return null;
        int code = IgniteQueryErrorCode.PARSING;
        if (e instanceof SqlParseException)
            code = ((SqlParseException) e).code();
        throw new IgniteSQLException("Failed to parse DDL statement: " + sql + ": " + e.getMessage(), code, e);
    }
}
Also used : SqlBulkLoadCommand(org.apache.ignite.internal.sql.command.SqlBulkLoadCommand) SqlStrictParseException(org.apache.ignite.internal.sql.SqlStrictParseException) SqlAlterTableCommand(org.apache.ignite.internal.sql.command.SqlAlterTableCommand) SqlKillContinuousQueryCommand(org.apache.ignite.internal.sql.command.SqlKillContinuousQueryCommand) SqlCommitTransactionCommand(org.apache.ignite.internal.sql.command.SqlCommitTransactionCommand) SqlRollbackTransactionCommand(org.apache.ignite.internal.sql.command.SqlRollbackTransactionCommand) SqlCreateUserCommand(org.apache.ignite.internal.sql.command.SqlCreateUserCommand) SqlKillScanQueryCommand(org.apache.ignite.internal.sql.command.SqlKillScanQueryCommand) SqlAnalyzeCommand(org.apache.ignite.internal.sql.command.SqlAnalyzeCommand) SqlAlterUserCommand(org.apache.ignite.internal.sql.command.SqlAlterUserCommand) SqlCommand(org.apache.ignite.internal.sql.command.SqlCommand) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SqlCreateIndexCommand(org.apache.ignite.internal.sql.command.SqlCreateIndexCommand) SqlDropStatisticsCommand(org.apache.ignite.internal.sql.command.SqlDropStatisticsCommand) SqlParser(org.apache.ignite.internal.sql.SqlParser) SqlKillComputeTaskCommand(org.apache.ignite.internal.sql.command.SqlKillComputeTaskCommand) SqlRefreshStatitsicsCommand(org.apache.ignite.internal.sql.command.SqlRefreshStatitsicsCommand) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlStrictParseException(org.apache.ignite.internal.sql.SqlStrictParseException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SqlDropIndexCommand(org.apache.ignite.internal.sql.command.SqlDropIndexCommand) SqlKillTransactionCommand(org.apache.ignite.internal.sql.command.SqlKillTransactionCommand) SqlDropUserCommand(org.apache.ignite.internal.sql.command.SqlDropUserCommand) SqlKillServiceCommand(org.apache.ignite.internal.sql.command.SqlKillServiceCommand) SqlKillQueryCommand(org.apache.ignite.internal.sql.command.SqlKillQueryCommand) SqlSetStreamingCommand(org.apache.ignite.internal.sql.command.SqlSetStreamingCommand) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SqlBeginTransactionCommand(org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with SqlBeginTransactionCommand

use of org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand in project ignite by apache.

the class CommandProcessor method processTxCommand.

/**
 * Process transactional command.
 * @param cmd Command.
 * @param params Parameters.
 * @throws IgniteCheckedException if failed.
 */
private void processTxCommand(SqlCommand cmd, QueryParameters params) throws IgniteCheckedException {
    NestedTxMode nestedTxMode = params.nestedTxMode();
    GridNearTxLocal tx = tx(ctx);
    if (cmd instanceof SqlBeginTransactionCommand) {
        if (!mvccEnabled(ctx))
            throw new IgniteSQLException("MVCC must be enabled in order to start transaction.", IgniteQueryErrorCode.MVCC_DISABLED);
        if (tx != null) {
            if (nestedTxMode == null)
                nestedTxMode = NestedTxMode.DEFAULT;
            switch(nestedTxMode) {
                case COMMIT:
                    doCommit(tx);
                    txStart(ctx, params.timeout());
                    break;
                case IGNORE:
                    log.warning("Transaction has already been started, ignoring BEGIN command.");
                    break;
                case ERROR:
                    throw new IgniteSQLException("Transaction has already been started.", IgniteQueryErrorCode.TRANSACTION_EXISTS);
                default:
                    throw new IgniteSQLException("Unexpected nested transaction handling mode: " + nestedTxMode.name());
            }
        } else
            txStart(ctx, params.timeout());
    } else if (cmd instanceof SqlCommitTransactionCommand) {
        // Do nothing if there's no transaction.
        if (tx != null)
            doCommit(tx);
    } else {
        assert cmd instanceof SqlRollbackTransactionCommand;
        // Do nothing if there's no transaction.
        if (tx != null)
            doRollback(tx);
    }
}
Also used : SqlRollbackTransactionCommand(org.apache.ignite.internal.sql.command.SqlRollbackTransactionCommand) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) NestedTxMode(org.apache.ignite.internal.processors.query.NestedTxMode) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) SqlBeginTransactionCommand(org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand) SqlCommitTransactionCommand(org.apache.ignite.internal.sql.command.SqlCommitTransactionCommand)

Example 3 with SqlBeginTransactionCommand

use of org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand in project ignite by apache.

the class SqlParserMultiStatementSelfTest method testComments.

/**
 * Check that comments between statements work.
 */
@Test
public void testComments() {
    String beginSql = "BEGIN  ;" + "  \n";
    String noteSql = "  -- Let's start an empty transaction; $1M idea!\n";
    String commitSql = "COMMIT;" + ";;";
    String sql = beginSql + noteSql + commitSql;
    SqlParser parser = new SqlParser("schema", sql);
    SqlCommand begin = parser.nextCommand();
    assertTrue(begin instanceof SqlBeginTransactionCommand);
    assertEquals("BEGIN", parser.lastCommandSql());
    assertEquals("  \n" + noteSql + commitSql, parser.remainingSql());
    SqlCommand commit = parser.nextCommand();
    assertTrue(commit instanceof SqlCommitTransactionCommand);
    assertEquals("COMMIT", parser.lastCommandSql());
    assertEquals(";;", parser.remainingSql());
    SqlCommand emptyCmd = parser.nextCommand();
    assertEquals(null, emptyCmd);
    assertEquals(null, parser.lastCommandSql());
    assertEquals(null, parser.remainingSql());
}
Also used : SqlBeginTransactionCommand(org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand) SqlCommitTransactionCommand(org.apache.ignite.internal.sql.command.SqlCommitTransactionCommand) SqlCommand(org.apache.ignite.internal.sql.command.SqlCommand) Test(org.junit.Test)

Example 4 with SqlBeginTransactionCommand

use of org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand in project ignite by apache.

the class SqlParserMultiStatementSelfTest method testEmptyStatements.

/**
 * Check that empty statements don't affect regular ones.
 */
@Test
public void testEmptyStatements() {
    String sql = ";;;CREATE INDEX TEST on TABLE1(id)  ; ;   BEGIN   ;;;";
    SqlParser parser = new SqlParser("schema", sql);
    // Haven't parse anything yet.
    assertEquals(null, parser.lastCommandSql());
    assertEquals(sql, parser.remainingSql());
    SqlCommand create = parser.nextCommand();
    assertTrue(create instanceof SqlCreateIndexCommand);
    assertEquals("CREATE INDEX TEST on TABLE1(id)", parser.lastCommandSql());
    assertEquals(" ;   BEGIN   ;;;", parser.remainingSql());
    SqlCommand begin = parser.nextCommand();
    assertTrue(begin instanceof SqlBeginTransactionCommand);
    assertEquals("BEGIN", parser.lastCommandSql());
    assertEquals(";;", parser.remainingSql());
    SqlCommand emptyCmd = parser.nextCommand();
    assertEquals(null, emptyCmd);
    assertEquals(null, parser.lastCommandSql());
    assertEquals(null, parser.remainingSql());
}
Also used : SqlCreateIndexCommand(org.apache.ignite.internal.sql.command.SqlCreateIndexCommand) SqlBeginTransactionCommand(org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand) SqlCommand(org.apache.ignite.internal.sql.command.SqlCommand) Test(org.junit.Test)

Aggregations

SqlBeginTransactionCommand (org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand)4 SqlCommand (org.apache.ignite.internal.sql.command.SqlCommand)3 SqlCommitTransactionCommand (org.apache.ignite.internal.sql.command.SqlCommitTransactionCommand)3 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)2 SqlCreateIndexCommand (org.apache.ignite.internal.sql.command.SqlCreateIndexCommand)2 SqlRollbackTransactionCommand (org.apache.ignite.internal.sql.command.SqlRollbackTransactionCommand)2 Test (org.junit.Test)2 SQLException (java.sql.SQLException)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)1 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)1 NestedTxMode (org.apache.ignite.internal.processors.query.NestedTxMode)1 SqlParseException (org.apache.ignite.internal.sql.SqlParseException)1 SqlParser (org.apache.ignite.internal.sql.SqlParser)1 SqlStrictParseException (org.apache.ignite.internal.sql.SqlStrictParseException)1 SqlAlterTableCommand (org.apache.ignite.internal.sql.command.SqlAlterTableCommand)1 SqlAlterUserCommand (org.apache.ignite.internal.sql.command.SqlAlterUserCommand)1 SqlAnalyzeCommand (org.apache.ignite.internal.sql.command.SqlAnalyzeCommand)1 SqlBulkLoadCommand (org.apache.ignite.internal.sql.command.SqlBulkLoadCommand)1 SqlCreateUserCommand (org.apache.ignite.internal.sql.command.SqlCreateUserCommand)1