com.alibaba.fastsql.sql.ast.SQLStatement Java Examples
The following examples show how to use
com.alibaba.fastsql.sql.ast.SQLStatement.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MycatDBSharedServerImpl.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
@Override public Iterator<RowBaseIterator> executeSqls(String sql, MycatDBContext dbContext) { List<SQLStatement> statements = SQLUtils.parseStatements(sql, DbType.mysql); List<MycatSQLPrepareObject> collect = new ArrayList<>(); for (SQLStatement statement : statements) { MycatSQLPrepareObject query = prepare(sql, null, statement, dbContext); collect.add(query); } Iterator<MycatSQLPrepareObject> iterator = collect.iterator(); return new Iterator<RowBaseIterator>() { @Override public boolean hasNext() { return iterator.hasNext(); } @Override public RowBaseIterator next() { return iterator.next().plan(Collections.emptyList()).run(); } }; }
Example #2
Source File: MycatDBSharedServerImpl.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
public MycatSQLPrepareObject query(String sql, SQLStatement sqlStatement, MycatDBContext dbContext) { boolean ddl = sqlStatement instanceof SQLSelectStatement || sqlStatement instanceof MySqlInsertStatement || sqlStatement instanceof MySqlUpdateStatement || sqlStatement instanceof MySqlDeleteStatement; if (ddl) { return prepare(sql, null, sqlStatement, dbContext); } if (sqlStatement instanceof SQLCommitStatement) return commit(sql, dbContext); if (sqlStatement instanceof SQLRollbackStatement) return (rollback(sql, dbContext)); if (sqlStatement instanceof SQLSetStatement) { return setStatement(sql, (SQLSetStatement) sqlStatement, dbContext); } if (sqlStatement instanceof SQLUseStatement) { String normalize = SQLUtils.normalize(((SQLUseStatement) sqlStatement).getDatabase().getSimpleName()); return use(sql, normalize, dbContext); } if (sqlStatement instanceof MySqlExplainStatement) { return explain(sql, (MySqlExplainStatement) sqlStatement, dbContext); } return null; }
Example #3
Source File: MycatDBSharedServerImpl.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
@NotNull private MycatSQLPrepareObject complieQuery(String sql, Long id, SQLStatement sqlStatement, MycatDBContext dataContext) { SQLSelect select = ((SQLSelectStatement) sqlStatement).getSelect(); SQLSelectQuery query = select.getQuery(); SqlNode sqlNode; boolean forUpdate = false; if (query instanceof SQLSelectQueryBlock) { SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query; forUpdate = queryBlock.isForUpdate(); } MycatCalciteMySqlNodeVisitor calciteMySqlNodeVisitor = new MycatCalciteMySqlNodeVisitor(); sqlStatement.accept(calciteMySqlNodeVisitor); sqlNode = calciteMySqlNodeVisitor.getSqlNode(); return new FastMycatCalciteSQLPrepareObject(id, sql, sqlNode, null, null, forUpdate, dataContext); }
Example #4
Source File: MycatDBSharedServerImpl.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
@NotNull private MycatSQLPrepareObject getMycatPrepareObject( MycatDBContext uponDBContext, String templateSql, Long id, SQLStatement sqlStatement, int variantRefCount, Function<ParseContext, Iterator<TextUpdateInfo>> accept) { return new MycatDelegateSQLPrepareObject(id, uponDBContext, templateSql, new MycatTextUpdatePrepareObject(id, variantRefCount, (prepareObject, params) -> { StringBuilder out = new StringBuilder(); SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, DbType.mysql); visitor.setInputParameters(params); sqlStatement.accept(visitor); String sql = out.toString(); ParseContext parseContext = new ParseContext(); parseContext.setSql(sql); return accept.apply(parseContext); }, uponDBContext)); }
Example #5
Source File: ExplainSQLHandler.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
@Override protected ExecuteCode onExecute(SQLRequest<MySqlExplainStatement> request, MycatDataContext dataContext, Response response) { MySqlExplainStatement ast = request.getAst(); if(ast.isDescribe()){ response.proxyShow(ast); return ExecuteCode.PERFORMED; } SQLStatement statement = ast.getStatement(); MycatRequest mycatRequest = request.getRequest(); MycatRequest request1 = MycatRequest.builder() .sessionId(mycatRequest.getSessionId()) .text(statement.toString()) .context(mycatRequest.getContext()) .userSpace(mycatRequest.getUserSpace()).build(); MycatdbCommand.INSTANCE.explain(request1,dataContext,response); return ExecuteCode.PERFORMED; }
Example #6
Source File: ReceiverImpl.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
@Override public void evalSimpleSql(SQLStatement sql) { //没有处理的sql,例如没有替换事务状态,自动提交状态的sql,随机发到后端会返回该随机的服务器状态 String target = session.isBindMySQLSession() ? session.getMySQLSession().getDatasource().getName() : ReplicaSelectorRuntime.INSTANCE.getDatasourceNameByRandom(); ExplainDetail detail = getExplainDetail(target, sql.toString(), QUERY); if (this.explainMode) { sendExplain(null, detail.toExplain()); } else { if (detail.needStartTransaction) {//需要事务就开启事务 session.getDataContext().getTransactionSession().begin(); } block(session -> { try (DefaultConnection connection = JdbcRuntime.INSTANCE.getConnection(target)) { try (RowBaseIterator rowBaseIterator = connection.executeQuery(sql.toString())) { sendResultSet(()->rowBaseIterator, () -> { throw new UnsupportedOperationException(); }); } } }); } }
Example #7
Source File: GlobalTable.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
@Override public Function<ParseContext, Iterator<TextUpdateInfo>> deleteHandler() { return new Function<ParseContext, Iterator<TextUpdateInfo>>() { @Override public Iterator<TextUpdateInfo> apply(ParseContext parseContext) { SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(parseContext.getSql()); MySqlDeleteStatement sqlStatement1 = (MySqlDeleteStatement) sqlStatement; SQLExprTableSource tableSource = (SQLExprTableSource)sqlStatement1.getTableSource(); return updateHandler(tableSource, sqlStatement1); } }; }
Example #8
Source File: ParseHelper.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
default public MySqlSelectQueryBlock unWapperToQueryBlock(SQLStatement sqlStatement) { if (sqlStatement instanceof SQLSelectStatement) { SQLSelectQuery query = ((SQLSelectStatement) sqlStatement).getSelect().getQuery(); if (query instanceof MySqlSelectQueryBlock) { return (MySqlSelectQueryBlock) query; } } return null; }
Example #9
Source File: PreProcesssorTest.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
private String process(String defaultSchema, String sql) { SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql); Scope scope = new Scope(defaultSchema); sqlStatement.accept(scope); scope.build(); PreProcesssor preProcesssor = new PreProcesssor(defaultSchema); sqlStatement.accept(preProcesssor); return sqlStatement.toString(); }
Example #10
Source File: TableCollector.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
public static Map<String, Collection<String>> collect(String defaultSchema, String sql) { Map<String, Collection<String>> collectionMap = new HashMap<>(); try { SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql); sqlStatement.accept(new MySqlASTVisitorAdapter() { @Override public boolean visit(SQLExprTableSource x) { String schema = x.getSchema(); String tableName = x.getTableName(); if (schema == null) { schema = defaultSchema; } if (schema == null) { throw new UnsupportedOperationException("please use schema"); } schema = SQLUtils.normalize(schema); tableName = SQLUtils.normalize(tableName); Collection<String> strings = collectionMap.computeIfAbsent(schema, s -> new HashSet<>()); strings.add(tableName); return super.visit(x); } }); } catch (Throwable ignored) { } return collectionMap; }
Example #11
Source File: MycatSqlUtil.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
public static String getCalciteSQL(SQLStatement sqlStatement) { SQLSelectQueryBlock queryBlock = ((SQLSelectStatement) sqlStatement).getSelect().getQueryBlock(); MycatCalciteMySqlNodeVisitor calciteMySqlNodeVisitor = new MycatCalciteMySqlNodeVisitor(); sqlStatement.accept(calciteMySqlNodeVisitor); SqlNode sqlNode = calciteMySqlNodeVisitor.getSqlNode(); return sqlNode.toSqlString(MysqlSqlDialect.DEFAULT).getSql(); }
Example #12
Source File: GlobalTable.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
@Override public Function<ParseContext, Iterator<TextUpdateInfo>> updateHandler() { return new Function<ParseContext, Iterator<TextUpdateInfo>>() { @Override public Iterator<TextUpdateInfo> apply(ParseContext s) { SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(s.getSql()); MySqlUpdateStatement sqlStatement1 = (MySqlUpdateStatement) sqlStatement; SQLExprTableSource tableSource = (SQLExprTableSource)sqlStatement1.getTableSource(); return updateHandler(tableSource, sqlStatement1); } }; }
Example #13
Source File: GlobalTable.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
@Override public Function<ParseContext, Iterator<TextUpdateInfo>> insertHandler() { return new Function<ParseContext, Iterator<TextUpdateInfo>>() { @Override public Iterator<TextUpdateInfo> apply(ParseContext s) { SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(s.getSql()); MySqlInsertStatement sqlStatement1 = (MySqlInsertStatement) sqlStatement; SQLExprTableSource tableSource = sqlStatement1.getTableSource(); return updateHandler(tableSource, sqlStatement1); } }; }
Example #14
Source File: HBTQueryConvertor.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
private RelDataType tryGetRelDataTypeByParse(String targetName, String sql) { try { RelDataType relDataType; MycatCalcitePlanner planner = MycatCalciteSupport.INSTANCE.createPlanner(context); SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql); SqlNode parse = planner.parse(MycatSqlUtil.getCalciteSQL(sqlStatement)); parse = parse.accept(new SqlShuttle() { @Override public SqlNode visit(SqlIdentifier id) { if (id.names.size() == 2) { String schema = id.names.get(0); String table = id.names.get(1); MycatLogicTable logicTable = context.getLogicTable(targetName, schema, table); if (logicTable!=null) { TableHandler table1 = logicTable.getTable(); return new SqlIdentifier(Arrays.asList(table1.getSchemaName(), table1.getTableName()), SqlParserPos.ZERO); } } return super.visit(id); } }); parse = planner.validate(parse); relDataType = planner.convert(parse).getRowType(); return relDataType; } catch (Throwable e) { log.warn("", e); } return null; }
Example #15
Source File: MemoryTableMeta.java From canal with Apache License 2.0 | 5 votes |
@Override public TableMeta find(String schema, String table) { List<String> keys = Arrays.asList(schema, table); TableMeta tableMeta = tableMetas.get(keys); if (tableMeta == null) { synchronized (this) { tableMeta = tableMetas.get(keys); if (tableMeta == null) { Schema schemaRep = repository.findSchema(schema); if (schemaRep == null) { return null; } SchemaObject data = schemaRep.findTable(table); if (data == null) { return null; } SQLStatement statement = data.getStatement(); if (statement == null) { return null; } if (statement instanceof SQLCreateTableStatement) { tableMeta = parse((SQLCreateTableStatement) statement); } if (tableMeta != null) { if (table != null) { tableMeta.setTable(table); } if (schema != null) { tableMeta.setSchema(schema); } tableMetas.put(keys, tableMeta); } } } } return tableMeta; }
Example #16
Source File: MemoryTableMeta.java From canal-1.1.3 with Apache License 2.0 | 5 votes |
@Override public TableMeta find(String schema, String table) { List<String> keys = Arrays.asList(schema, table); TableMeta tableMeta = tableMetas.get(keys); if (tableMeta == null) { synchronized (this) { tableMeta = tableMetas.get(keys); if (tableMeta == null) { Schema schemaRep = repository.findSchema(schema); if (schemaRep == null) { return null; } SchemaObject data = schemaRep.findTable(table); if (data == null) { return null; } SQLStatement statement = data.getStatement(); if (statement == null) { return null; } if (statement instanceof SQLCreateTableStatement) { tableMeta = parse((SQLCreateTableStatement) statement); } if (tableMeta != null) { if (table != null) { tableMeta.setTable(table); } if (schema != null) { tableMeta.setSchema(schema); } tableMetas.put(keys, tableMeta); } } } } return tableMeta; }
Example #17
Source File: MycatDBSharedServerImpl.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
private PrepareObject prepare(String sql, Long id, MycatDBContext dbContext) { SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);//不支持多语句的预处理 return prepare(sql, id, sqlStatement, dbContext); }
Example #18
Source File: SQL2ResultSetUtil.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
public static MycatRowMetaDataImpl getMycatRowMetaData(String createTableStmtText) { List<SQLStatement> statements = SQLUtils.parseStatements(createTableStmtText, DbType.mysql); MySqlCreateTableStatement mySqlCreateTableStatement = (MySqlCreateTableStatement) statements.get(statements.size() - 1); String tableName = mySqlCreateTableStatement.getTableSource().computeAlias(); return new MycatRowMetaDataImpl(mySqlCreateTableStatement.getColumnDefinitions(), "", tableName); }
Example #19
Source File: ParseContext.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
public SQLStatement getSqlStatement() { return sqlStatement; }
Example #20
Source File: ParseContext.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
public static ParseContext of(String defaultSchema,SQLStatement sqlStatement) { return new ParseContext(defaultSchema,sqlStatement); }
Example #21
Source File: ParseContext.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
public ParseContext(String defaultSchema,SQLStatement sqlStatement) { this.defaultSchema = defaultSchema; this.sqlStatement = sqlStatement; }
Example #22
Source File: UpdateSQLHandler.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
public static void updateHandler(SQLStatement sql, MycatDataContext dataContext, SQLExprTableSource tableSource, Response receiver) { MycatDBClientMediator mycatDBClientMediator = MycatDBs.createClient(dataContext); String schemaName = Optional.ofNullable(tableSource.getSchema() == null ? dataContext.getDefaultSchema() : tableSource.getSchema()) .map(i-> SQLUtils.normalize(i)).orElse(null); String tableName = SQLUtils.normalize(tableSource.getTableName()); SchemaHandler schemaHandler; Optional<Map<String, SchemaHandler>> handlerMapOptional = Optional.ofNullable(mycatDBClientMediator) .map(i -> i.config()) .map(i -> i.getSchemaMap()); Optional<String> targetNameOptional = Optional.ofNullable(RootHelper.INSTANCE) .map(i -> i.getConfigProvider()) .map(i -> i.currentConfig()) .map(i -> i.getMetadata()) .map(i -> i.getPrototype()) .map(i -> i.getTargetName()); if (!handlerMapOptional.isPresent()) { if (targetNameOptional.isPresent()) { receiver.proxyUpdate(targetNameOptional.get(), Objects.toString(sql)); return; } else { receiver.sendError(new MycatException("Unable to route:" + sql)); return; } } else { Map<String, SchemaHandler> handlerMap = handlerMapOptional.get(); schemaHandler = Optional.ofNullable(handlerMap.get(schemaName)) .orElseGet(() -> { if (mycatDBClientMediator.getSchema() == null) { throw new MycatException("unknown schema:"+schemaName);//可能schemaName有值,但是值名不是配置的名字 } return handlerMap.get(mycatDBClientMediator.getSchema()); }); if (schemaHandler == null) { receiver.sendError(new MycatException("Unable to route:" + sql)); return; } } String defaultTargetName = schemaHandler.defaultTargetName(); Map<String, TableHandler> tableMap = schemaHandler.logicTables(); TableHandler tableHandler = tableMap.get(tableName); ///////////////////////////////common/////////////////////////////// if (tableHandler == null) { receiver.proxyUpdate(defaultTargetName, sql.toString()); return; } String string = sql.toString(); if (sql instanceof MySqlInsertStatement) { switch (tableHandler.getType()) { case SHARDING: receiver.multiInsert(string, tableHandler.insertHandler().apply(new ParseContext(sql.toString()))); break; case GLOBAL: receiver.multiGlobalInsert(string, tableHandler.insertHandler().apply(new ParseContext(sql.toString()))); break; } } else if (sql instanceof com.alibaba.fastsql.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) { switch (tableHandler.getType()) { case SHARDING: receiver.multiUpdate(string, tableHandler.deleteHandler().apply(new ParseContext(sql.toString()))); break; case GLOBAL: receiver.multiGlobalUpdate(string, tableHandler.deleteHandler().apply(new ParseContext(sql.toString()))); break; } } else if (sql instanceof com.alibaba.fastsql.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) { switch (tableHandler.getType()) { case SHARDING: receiver.multiUpdate(string, tableHandler.updateHandler().apply(new ParseContext(sql.toString()))); break; case GLOBAL: receiver.multiGlobalUpdate(string, tableHandler.deleteHandler().apply(new ParseContext(sql.toString()))); break; } } else { throw new UnsupportedOperationException("unsupported statement:" + sql); } }
Example #23
Source File: ReceiverImpl.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
@Override public void proxyShow(SQLStatement statement) { proxyDDL(statement); }
Example #24
Source File: ReceiverImpl.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
@Override public void proxyDDL(SQLStatement statement) { String replicaDataSource = ReplicaSelectorRuntime.INSTANCE.getPrototypeOrFirstReplicaDataSource(); ExplainDetail detail = getExplainDetail(replicaDataSource, statement.toString(), QUERY_MASTER); this.execute(detail); }
Example #25
Source File: MycatDBSharedServerImpl.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
@NotNull private int getVariantRefCount(SQLStatement sqlStatement) { SQLVariantRefExprCounter sqlVariantRefExprCounter = new SQLVariantRefExprCounter(); sqlStatement.accept(sqlVariantRefExprCounter); return sqlVariantRefExprCounter.getCount(); }
Example #26
Source File: Response.java From Mycat2 with GNU General Public License v3.0 | votes |
void proxyShow(SQLStatement statement);
Example #27
Source File: Response.java From Mycat2 with GNU General Public License v3.0 | votes |
void proxyDDL(SQLStatement statement);