Search in sources :

Example 6 with SqlCommand

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

the class SqlParser method processCreate.

/**
 * Process CREATE keyword.
 *
 * @return Command.
 */
private SqlCommand processCreate() {
    if (lex.shift() && lex.tokenType() == SqlLexerTokenType.DEFAULT) {
        SqlCommand cmd = null;
        switch(lex.token()) {
            case INDEX:
                cmd = new SqlCreateIndexCommand();
                break;
            case SPATIAL:
                if (lex.shift() && matchesKeyword(lex, INDEX))
                    cmd = new SqlCreateIndexCommand().spatial(true);
                else
                    throw errorUnexpectedToken(lex, INDEX);
                break;
            case USER:
                cmd = new SqlCreateUserCommand();
                break;
        }
        if (cmd != null)
            return cmd.parse(lex);
        errorUnsupportedIfMatchesKeyword(lex, HASH, PRIMARY, UNIQUE);
    }
    throw errorUnexpectedToken(lex, INDEX, SPATIAL, USER);
}
Also used : SqlCreateUserCommand(org.apache.ignite.internal.sql.command.SqlCreateUserCommand) SqlCreateIndexCommand(org.apache.ignite.internal.sql.command.SqlCreateIndexCommand) SqlCommand(org.apache.ignite.internal.sql.command.SqlCommand)

Example 7 with SqlCommand

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

the class SqlParser method nextCommand0.

/**
 * Get next command.
 *
 * @return Command or {@code null} if end of script is reached.
 */
private SqlCommand nextCommand0() {
    while (true) {
        if (!lex.shift()) {
            lastCmdEndPos = -1;
            return null;
        }
        switch(lex.tokenType()) {
            case SEMICOLON:
                // Empty command, skip.
                continue;
            case DEFAULT:
                SqlCommand cmd = null;
                int curCmdBegin = lex.tokenPosition();
                switch(lex.token()) {
                    case BEGIN:
                        cmd = processBegin();
                        break;
                    case COMMIT:
                        cmd = processCommit();
                        break;
                    case CREATE:
                        cmd = processCreate();
                        break;
                    case DROP:
                        cmd = processDrop();
                        break;
                    case ROLLBACK:
                        cmd = processRollback();
                        break;
                    case START:
                        cmd = processStart();
                        break;
                    case COPY:
                        try {
                            cmd = processCopy();
                            break;
                        } catch (SqlParseException e) {
                            throw new SqlStrictParseException(e);
                        }
                    case SET:
                        cmd = processSet();
                        break;
                    case ALTER:
                        cmd = processAlter();
                        break;
                    case KILL:
                        cmd = processKill();
                        break;
                    case HELP:
                        cmd = processHelp();
                        break;
                    case SHOW:
                        cmd = processShow();
                        break;
                    case GRANT:
                        cmd = processGrant();
                        break;
                    case REVOKE:
                        cmd = processRevoke();
                        break;
                    case ANALYZE:
                        cmd = processAnalyze();
                        break;
                    case REFRESH:
                        cmd = processRefresh();
                        break;
                }
                if (cmd != null) {
                    int curCmdEnd = lex.position();
                    // If there is something behind the command, this is a syntax error.
                    if (lex.shift() && lex.tokenType() != SqlLexerTokenType.SEMICOLON)
                        throw errorUnexpectedToken(lex);
                    lastCmdBeginPos = curCmdBegin;
                    lastCmdEndPos = curCmdEnd;
                    return cmd;
                } else
                    throw errorUnexpectedToken(lex, BEGIN, COMMIT, CREATE, DROP, ROLLBACK, COPY, SET, ALTER, START, KILL);
            case QUOTED:
            case MINUS:
            case DOT:
            case COMMA:
            case PARENTHESIS_LEFT:
            case PARENTHESIS_RIGHT:
            default:
                throw errorUnexpectedToken(lex);
        }
    }
}
Also used : SqlCommand(org.apache.ignite.internal.sql.command.SqlCommand)

Example 8 with SqlCommand

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

the class IgniteH2Indexing method tryQueryDistributedSqlFieldsNative.

/**
 * Try executing query using native facilities.
 *
 * @param schemaName Schema name.
 * @param sql Query.
 * @param cliCtx Client context, or {@code null} if not applicable.
 * @return Result or {@code null} if cannot parse/process this query.
 */
private List<FieldsQueryCursor<List<?>>> tryQueryDistributedSqlFieldsNative(String schemaName, String sql, @Nullable SqlClientContext cliCtx) {
    // Heuristic check for fast return.
    if (!INTERNAL_CMD_RE.matcher(sql.trim()).find())
        return null;
    // Parse.
    SqlCommand cmd;
    try {
        SqlParser parser = new SqlParser(schemaName, sql);
        cmd = parser.nextCommand();
        // No support for multiple commands for now.
        if (parser.nextCommand() != null)
            return null;
        // CREATE/ALTER/DROP USER
        if (!(cmd instanceof SqlCreateIndexCommand || cmd instanceof SqlDropIndexCommand || cmd instanceof SqlAlterTableCommand || cmd instanceof SqlBulkLoadCommand || cmd instanceof SqlSetStreamingCommand || cmd instanceof SqlCreateUserCommand || cmd instanceof SqlAlterUserCommand || cmd instanceof SqlDropUserCommand))
            return null;
    } 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);
    }
    // Execute.
    if (cmd instanceof SqlBulkLoadCommand) {
        FieldsQueryCursor<List<?>> cursor = dmlProc.runNativeDmlStatement(sql, cmd);
        return Collections.singletonList(cursor);
    } else if (cmd instanceof SqlSetStreamingCommand) {
        if (cliCtx == null)
            throw new IgniteSQLException("SET STREAMING command can only be executed from JDBC or ODBC driver.");
        SqlSetStreamingCommand setCmd = (SqlSetStreamingCommand) cmd;
        boolean on = setCmd.isTurnOn();
        if (on)
            cliCtx.enableStreaming(setCmd.allowOverwrite(), setCmd.flushFrequency(), setCmd.perNodeBufferSize(), setCmd.perNodeParallelOperations());
        else
            cliCtx.disableStreaming();
        return Collections.singletonList(H2Utils.zeroCursor());
    } else {
        try {
            FieldsQueryCursor<List<?>> cursor = ddlProc.runDdlStatement(sql, cmd);
            return Collections.singletonList(cursor);
        } catch (IgniteCheckedException e) {
            throw new IgniteSQLException("Failed to execute DDL statement [stmt=" + sql + "]: " + e.getMessage(), e);
        }
    }
}
Also used : SqlBulkLoadCommand(org.apache.ignite.internal.sql.command.SqlBulkLoadCommand) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SqlCreateIndexCommand(org.apache.ignite.internal.sql.command.SqlCreateIndexCommand) SqlParser(org.apache.ignite.internal.sql.SqlParser) SqlAlterTableCommand(org.apache.ignite.internal.sql.command.SqlAlterTableCommand) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SqlDropIndexCommand(org.apache.ignite.internal.sql.command.SqlDropIndexCommand) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlCreateUserCommand(org.apache.ignite.internal.sql.command.SqlCreateUserCommand) SqlDropUserCommand(org.apache.ignite.internal.sql.command.SqlDropUserCommand) SqlSetStreamingCommand(org.apache.ignite.internal.sql.command.SqlSetStreamingCommand) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ArrayList(java.util.ArrayList) List(java.util.List) SqlAlterUserCommand(org.apache.ignite.internal.sql.command.SqlAlterUserCommand) SqlCommand(org.apache.ignite.internal.sql.command.SqlCommand)

Example 9 with SqlCommand

use of org.apache.ignite.internal.sql.command.SqlCommand 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 10 with SqlCommand

use of org.apache.ignite.internal.sql.command.SqlCommand 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

SqlCommand (org.apache.ignite.internal.sql.command.SqlCommand)10 SQLException (java.sql.SQLException)4 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)4 SqlCreateIndexCommand (org.apache.ignite.internal.sql.command.SqlCreateIndexCommand)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 IgniteException (org.apache.ignite.IgniteException)3 SqlBeginTransactionCommand (org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand)3 SqlCommitTransactionCommand (org.apache.ignite.internal.sql.command.SqlCommitTransactionCommand)3 SqlCreateUserCommand (org.apache.ignite.internal.sql.command.SqlCreateUserCommand)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CacheException (javax.cache.CacheException)2 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)2 SqlParseException (org.apache.ignite.internal.sql.SqlParseException)2 SqlParser (org.apache.ignite.internal.sql.SqlParser)2 SqlAlterTableCommand (org.apache.ignite.internal.sql.command.SqlAlterTableCommand)2 SqlAlterUserCommand (org.apache.ignite.internal.sql.command.SqlAlterUserCommand)2 SqlBulkLoadCommand (org.apache.ignite.internal.sql.command.SqlBulkLoadCommand)2 SqlDropIndexCommand (org.apache.ignite.internal.sql.command.SqlDropIndexCommand)2 SqlDropUserCommand (org.apache.ignite.internal.sql.command.SqlDropUserCommand)2