use of org.apache.ignite.internal.sql.command.SqlDropUserCommand in project ignite by apache.
the class DdlStatementsProcessor method runDdlStatement.
/**
* Run DDL statement.
*
* @param sql Original SQL.
* @param cmd Command.
* @return Result.
* @throws IgniteCheckedException On error.
*/
@SuppressWarnings("unchecked")
public FieldsQueryCursor<List<?>> runDdlStatement(String sql, SqlCommand cmd) throws IgniteCheckedException {
IgniteInternalFuture fut = null;
try {
if (cmd instanceof SqlCreateIndexCommand) {
SqlCreateIndexCommand cmd0 = (SqlCreateIndexCommand) cmd;
GridH2Table tbl = idx.dataTable(cmd0.schemaName(), cmd0.tableName());
if (tbl == null)
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd0.tableName());
assert tbl.rowDescriptor() != null;
isDdlSupported(tbl);
QueryIndex newIdx = new QueryIndex();
newIdx.setName(cmd0.indexName());
newIdx.setIndexType(cmd0.spatial() ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
LinkedHashMap<String, Boolean> flds = new LinkedHashMap<>();
// Let's replace H2's table and property names by those operated by GridQueryProcessor.
GridQueryTypeDescriptor typeDesc = tbl.rowDescriptor().type();
for (SqlIndexColumn col : cmd0.columns()) {
GridQueryProperty prop = typeDesc.property(col.name());
if (prop == null)
throw new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, col.name());
flds.put(prop.name(), !col.descending());
}
newIdx.setFields(flds);
newIdx.setInlineSize(cmd0.inlineSize());
fut = ctx.query().dynamicIndexCreate(tbl.cacheName(), cmd.schemaName(), typeDesc.tableName(), newIdx, cmd0.ifNotExists(), cmd0.parallel());
} else if (cmd instanceof SqlDropIndexCommand) {
SqlDropIndexCommand cmd0 = (SqlDropIndexCommand) cmd;
GridH2Table tbl = idx.dataTableForIndex(cmd0.schemaName(), cmd0.indexName());
if (tbl != null) {
isDdlSupported(tbl);
fut = ctx.query().dynamicIndexDrop(tbl.cacheName(), cmd0.schemaName(), cmd0.indexName(), cmd0.ifExists());
} else {
if (cmd0.ifExists())
fut = new GridFinishedFuture();
else
throw new SchemaOperationException(SchemaOperationException.CODE_INDEX_NOT_FOUND, cmd0.indexName());
}
} else if (cmd instanceof SqlAlterTableCommand) {
SqlAlterTableCommand cmd0 = (SqlAlterTableCommand) cmd;
GridH2Table tbl = idx.dataTable(cmd0.schemaName(), cmd0.tableName());
if (tbl == null) {
ctx.cache().createMissingQueryCaches();
tbl = idx.dataTable(cmd0.schemaName(), cmd0.tableName());
}
if (tbl == null) {
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd0.tableName());
}
Boolean logging = cmd0.logging();
assert logging != null : "Only LOGGING/NOLOGGING are supported at the moment.";
IgniteCluster cluster = ctx.grid().cluster();
if (logging) {
boolean res = cluster.enableWal(tbl.cacheName());
if (!res)
throw new IgniteSQLException("Logging already enabled for table: " + cmd0.tableName());
} else {
boolean res = cluster.disableWal(tbl.cacheName());
if (!res)
throw new IgniteSQLException("Logging already disabled for table: " + cmd0.tableName());
}
fut = new GridFinishedFuture();
} else if (cmd instanceof SqlCreateUserCommand) {
SqlCreateUserCommand addCmd = (SqlCreateUserCommand) cmd;
ctx.authentication().addUser(addCmd.userName(), addCmd.password());
} else if (cmd instanceof SqlAlterUserCommand) {
SqlAlterUserCommand altCmd = (SqlAlterUserCommand) cmd;
ctx.authentication().updateUser(altCmd.userName(), altCmd.password());
} else if (cmd instanceof SqlDropUserCommand) {
SqlDropUserCommand dropCmd = (SqlDropUserCommand) cmd;
ctx.authentication().removeUser(dropCmd.userName());
} else
throw new IgniteSQLException("Unsupported DDL operation: " + sql, IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (fut != null)
fut.get();
return H2Utils.zeroCursor();
} catch (SchemaOperationException e) {
throw convert(e);
} catch (IgniteSQLException e) {
throw e;
} catch (Exception e) {
throw new IgniteSQLException(e.getMessage(), e);
}
}
use of org.apache.ignite.internal.sql.command.SqlDropUserCommand in project ignite by apache.
the class SqlParserUserSelfTest method parseValidateDrop.
/**
* Parse and validate SQL script.
*
* @param sql SQL.
* @param expUserName Expected user name.
* @return Command.
*/
private static SqlDropUserCommand parseValidateDrop(String sql, String expUserName) {
SqlDropUserCommand cmd = (SqlDropUserCommand) new SqlParser(null, sql).nextCommand();
assertEquals(expUserName, cmd.userName());
return cmd;
}
use of org.apache.ignite.internal.sql.command.SqlDropUserCommand 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);
}
}
}
Aggregations