use of com.alibaba.druid.sql.ast.statement.SQLBlockStatement in project Mycat_plus by coderczp.
the class SqlAstParser method parseStatementDialect.
private boolean parseStatementDialect(List<StatementHolder> statementList) {
if (lexer.token() == Token.KILL) {
SQLStatement stmt = parseKill();
statementList.add(new StatementHolder(Type.KILL, stmt));
return true;
}
if (identifierEquals("PREPARE")) {
MySqlPrepareStatement stmt = parsePrepare();
statementList.add(new StatementHolder(Type.PREPARE, stmt));
return true;
}
if (identifierEquals("EXECUTE")) {
MySqlExecuteStatement stmt = parseExecute();
statementList.add(new StatementHolder(Type.EXECUTE, stmt));
return true;
}
if (identifierEquals("DEALLOCATE")) {
MysqlDeallocatePrepareStatement stmt = parseDeallocatePrepare();
statementList.add(new StatementHolder(Type.DEALLOCATE, stmt));
return true;
}
if (identifierEquals("LOAD")) {
SQLStatement stmt = parseLoad();
statementList.add(new StatementHolder(Type.LOAD_DATA_INFILE_SQL, stmt));
return true;
}
if (lexer.token() == Token.REPLACE) {
MySqlReplaceStatement stmt = parseReplicate();
statementList.add(new StatementHolder(Type.REPLACE, stmt));
return true;
}
if (identifierEquals("START")) {
SQLStartTransactionStatement stmt = parseStart();
statementList.add(new StatementHolder(Type.START, stmt));
return true;
}
if (lexer.token() == Token.SHOW) {
SQLStatement stmt = parseShow();
statementList.add(new StatementHolder(Type.SHOW, stmt));
return true;
}
if (lexer.token() == Token.EXPLAIN) {
SQLStatement stmt = this.parseExplain();
statementList.add(new StatementHolder(Type.EXPLAIN, stmt));
return true;
}
if (identifierEquals("BINLOG")) {
SQLStatement stmt = parseBinlog();
statementList.add(new StatementHolder(Type.BINLOG, stmt));
return true;
}
if (identifierEquals("RESET")) {
SQLStatement stmt = parseReset();
statementList.add(new StatementHolder(Type.RESET, stmt));
return true;
}
if (lexer.token() == Token.ANALYZE) {
SQLStatement stmt = parseAnalyze();
statementList.add(new StatementHolder(Type.ANALYZE, stmt));
return true;
}
if (lexer.token() == Token.OPTIMIZE) {
SQLStatement stmt = parseOptimize();
statementList.add(new StatementHolder(Type.OPTIMIZE, stmt));
return true;
}
if (identifierEquals("HELP")) {
lexer.nextToken();
MySqlHelpStatement stmt = new MySqlHelpStatement();
stmt.setContent(this.exprParser.primary());
statementList.add(new StatementHolder(Type.HELP, stmt));
return true;
}
if (lexer.token() == Token.DESC || identifierEquals("DESCRIBE")) {
SQLStatement stmt = parseDescribe();
statementList.add(new StatementHolder(Type.DESCRIBE, stmt));
return true;
}
if (lexer.token() == Token.LOCK) {
lexer.nextToken();
String val = lexer.stringVal();
boolean isLockTables = "TABLES".equalsIgnoreCase(val) && lexer.token() == Token.IDENTIFIER;
boolean isLockTable = "TABLE".equalsIgnoreCase(val) && lexer.token() == Token.TABLE;
if (isLockTables || isLockTable) {
lexer.nextToken();
} else {
setErrorEndPos(lexer.pos());
throw new ParserException("syntax error, expect TABLES or TABLE, actual " + lexer.token());
}
MySqlLockTableStatement stmt = new MySqlLockTableStatement();
stmt.setTableSource(this.exprParser.name());
if (identifierEquals("READ")) {
lexer.nextToken();
if (identifierEquals("LOCAL")) {
lexer.nextToken();
stmt.setLockType(LockType.READ_LOCAL);
} else {
stmt.setLockType(LockType.READ);
}
} else if (identifierEquals("WRITE")) {
stmt.setLockType(LockType.WRITE);
} else if (identifierEquals("LOW_PRIORITY")) {
lexer.nextToken();
acceptIdentifier("WRITE");
stmt.setLockType(LockType.LOW_PRIORITY_WRITE);
} else {
throw new ParserException("syntax error, expect READ or WRITE, actual " + lexer.token());
}
if (lexer.token() == Token.HINT) {
stmt.setHints(this.exprParser.parseHints());
}
statementList.add(new StatementHolder(Type.LOCK, stmt));
return true;
}
if (identifierEquals("UNLOCK")) {
lexer.nextToken();
String val = lexer.stringVal();
boolean isUnLockTables = "TABLE".equalsIgnoreCase(val) && lexer.token() == Token.IDENTIFIER;
boolean isUnLockTable = "TABLE".equalsIgnoreCase(val) && lexer.token() == Token.TABLE;
MySqlUnlockTablesStatement stmt = new MySqlUnlockTablesStatement();
statementList.add(new StatementHolder(Type.UNLOCK, stmt));
if (isUnLockTables || isUnLockTable) {
lexer.nextToken();
} else {
setErrorEndPos(lexer.pos());
throw new ParserException("syntax error, expect TABLES or TABLE, actual " + lexer.token());
}
return true;
}
if (lexer.token() == Token.HINT) {
List<SQLCommentHint> hints = this.exprParser.parseHints();
boolean tddlSelectHints = false;
if (hints.size() == 1 && statementList.size() == 0 && lexer.token() == Token.SELECT) {
SQLCommentHint hint = hints.get(0);
String hintText = hint.getText();
if (hintText.startsWith("+TDDL")) {
tddlSelectHints = true;
}
}
if (tddlSelectHints) {
SQLSelectStatement stmt = (SQLSelectStatement) this.parseStatement();
stmt.setHeadHints(hints);
statementList.add(new StatementHolder(Type.SELECT_WITH_HINT, stmt));
return true;
}
MySqlHintStatement stmt = new MySqlHintStatement();
stmt.setHints(hints);
statementList.add(new StatementHolder(Type.HINT, stmt));
return true;
}
if (lexer.token() == Token.BEGIN) {
SQLBlockStatement stmt = this.parseBlock();
statementList.add(new StatementHolder(Type.BEGIN, stmt));
return true;
}
return false;
}
use of com.alibaba.druid.sql.ast.statement.SQLBlockStatement in project Mycat2 by MyCATApache.
the class SQLCreateProcedureHandler method onExecute.
@Override
@SneakyThrows
protected Future<Void> onExecute(SQLRequest<SQLCreateProcedureStatement> request, MycatDataContext dataContext, Response response) {
SQLCreateProcedureStatement ast = request.getAst();
if (ast.getName() instanceof SQLIdentifierExpr) {
String defaultSchema = dataContext.getDefaultSchema();
if (defaultSchema != null) {
ast.setName(new SQLPropertyExpr(defaultSchema, ((SQLIdentifierExpr) ast.getName()).getName()));
}
}
if (!(ast.getName() instanceof SQLPropertyExpr)) {
throw new IllegalArgumentException("unknown schema:");
}
SQLPropertyExpr pNameExpr = (SQLPropertyExpr) ast.getName();
String schemaName = SQLUtils.normalize(pNameExpr.getOwnerName().toLowerCase());
String pName = SQLUtils.normalize(pNameExpr.getName().toLowerCase());
List<SQLParameter> sqlParameters = Optional.ofNullable(ast.getParameters()).orElse(Collections.emptyList());
Map<SQLParameter.ParameterType, List<SQLParameter>> parameterTypeListMap = sqlParameters.stream().collect(Collectors.groupingBy(k -> k.getParamType()));
SQLBlockStatement block = (SQLBlockStatement) ast.getBlock();
if (dataContext.getDefaultSchema() != null) {
block.accept(new MySqlASTVisitorAdapter() {
@Override
public void endVisit(SQLExprTableSource x) {
resolveSQLExprTableSource(x, dataContext);
}
});
}
Map<String, Collection<String>> collect = TableCollector.collect(dataContext.getDefaultSchema(), block);
int resultSetCount = getResultSetCount(block);
List<TableHandler> tableHandlers = getTableHandlers(block);
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
NormalProcedureConfig normalProcedureConfig = new NormalProcedureConfig();
normalProcedureConfig.setCreateProcedureSQL(ast.toString());
NormalBackEndProcedureInfoConfig normalBackEndProcedureInfoConfig = new NormalBackEndProcedureInfoConfig();
normalBackEndProcedureInfoConfig.setProcedureName(pName);
normalBackEndProcedureInfoConfig.setSchemaName(schemaName);
normalBackEndProcedureInfoConfig.setTargetName(MetadataManager.getPrototype());
normalProcedureConfig.setLocality(normalBackEndProcedureInfoConfig);
try (MycatRouterConfigOps ops = ConfigUpdater.getOps()) {
ops.addProcedure(schemaName, pName, normalProcedureConfig);
ops.commit();
}
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
connection.executeUpdate(ast.toString(), false);
}
return response.sendOk();
}
Aggregations