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);
}
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);
}
}
}
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);
}
}
}
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());
}
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());
}
Aggregations