use of com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement in project dble by actiontech.
the class DruidCreateIndexParser method visitorParse.
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
SQLCreateIndexStatement createStmt = (SQLCreateIndexStatement) stmt;
SQLTableSource tableSource = createStmt.getTable();
if (tableSource instanceof SQLExprTableSource) {
String schemaName = schema == null ? null : schema.getName();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, (SQLExprTableSource) tableSource);
String statement = RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema());
rrs.setStatement(statement);
if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
RouterUtil.routeToSingleDDLNode(schemaInfo, rrs);
return schemaInfo.getSchemaConfig();
}
RouterUtil.routeToDDLNode(schemaInfo, rrs);
return schemaInfo.getSchemaConfig();
} else {
String msg = "The DDL is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
}
use of com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement in project druid by alibaba.
the class SQLServerCreateIndexTest_4 method test_0.
public void test_0() throws Exception {
String sql = "CREATE UNIQUE NONCLUSTERED INDEX ACT_IDX_EVENT_DEF_UNIQ " + "ON FLW_EVENT_DEFINITION(KEY_, VERSION_, TENANT_ID_)";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLCreateIndexStatement stmt = (SQLCreateIndexStatement) statementList.get(0);
assertEquals(1, statementList.size());
assertEquals(//
"CREATE UNIQUE NONCLUSTERED INDEX ACT_IDX_EVENT_DEF_UNIQ ON FLW_EVENT_DEFINITION (KEY_, VERSION_, TENANT_ID_)", SQLUtils.toSQLString(stmt, DbType.sqlserver));
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
assertEquals(1, visitor.getTables().size());
assertEquals(3, visitor.getColumns().size());
assertEquals(0, visitor.getConditions().size());
assertTrue(visitor.containsColumn("FLW_EVENT_DEFINITION", "KEY_"));
assertTrue(visitor.containsColumn("FLW_EVENT_DEFINITION", "VERSION_"));
assertTrue(visitor.containsColumn("FLW_EVENT_DEFINITION", "TENANT_ID_"));
}
use of com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement in project canal by alibaba.
the class DruidDdlParser method parse.
public static List<DdlResult> parse(String queryString, String schmeaName) {
List<SQLStatement> stmtList = null;
try {
stmtList = SQLUtils.parseStatements(queryString, JdbcConstants.MYSQL, false);
} catch (ParserException e) {
// 可能存在一些SQL是不支持的,比如存储过程
DdlResult ddlResult = new DdlResult();
ddlResult.setType(EventType.QUERY);
return Arrays.asList(ddlResult);
}
List<DdlResult> ddlResults = new ArrayList<>();
for (SQLStatement statement : stmtList) {
if (statement instanceof SQLCreateTableStatement) {
DdlResult ddlResult = new DdlResult();
SQLCreateTableStatement createTable = (SQLCreateTableStatement) statement;
processName(ddlResult, schmeaName, createTable.getName(), false);
ddlResult.setType(EventType.CREATE);
ddlResults.add(ddlResult);
} else if (statement instanceof SQLAlterTableStatement) {
SQLAlterTableStatement alterTable = (SQLAlterTableStatement) statement;
if (alterTable.getTableOptions().size() > 0) {
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, alterTable.getName(), false);
ddlResult.setType(EventType.ALTER);
ddlResults.add(ddlResult);
}
for (SQLAlterTableItem item : alterTable.getItems()) {
if (item instanceof SQLAlterTableRename) {
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, alterTable.getName(), true);
processName(ddlResult, schmeaName, ((SQLAlterTableRename) item).getToName(), false);
ddlResult.setType(EventType.RENAME);
ddlResults.add(ddlResult);
} else if (item instanceof SQLAlterTableAddIndex) {
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, alterTable.getName(), false);
ddlResult.setType(EventType.CINDEX);
ddlResults.add(ddlResult);
} else if (item instanceof SQLAlterTableDropIndex || item instanceof SQLAlterTableDropKey) {
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, alterTable.getName(), false);
ddlResult.setType(EventType.DINDEX);
ddlResults.add(ddlResult);
} else if (item instanceof SQLAlterTableAddConstraint) {
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, alterTable.getName(), false);
SQLConstraint constraint = ((SQLAlterTableAddConstraint) item).getConstraint();
if (constraint instanceof SQLUnique) {
ddlResult.setType(EventType.CINDEX);
ddlResults.add(ddlResult);
}
} else if (item instanceof SQLAlterTableDropConstraint) {
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, alterTable.getName(), false);
ddlResult.setType(EventType.DINDEX);
ddlResults.add(ddlResult);
} else {
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, alterTable.getName(), false);
ddlResult.setType(EventType.ALTER);
ddlResults.add(ddlResult);
}
}
} else if (statement instanceof SQLDropTableStatement) {
SQLDropTableStatement dropTable = (SQLDropTableStatement) statement;
for (SQLExprTableSource tableSource : dropTable.getTableSources()) {
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, tableSource.getExpr(), false);
ddlResult.setType(EventType.ERASE);
ddlResults.add(ddlResult);
}
} else if (statement instanceof SQLCreateIndexStatement) {
SQLCreateIndexStatement createIndex = (SQLCreateIndexStatement) statement;
SQLTableSource tableSource = createIndex.getTable();
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, ((SQLExprTableSource) tableSource).getExpr(), false);
ddlResult.setType(EventType.CINDEX);
ddlResults.add(ddlResult);
} else if (statement instanceof SQLDropIndexStatement) {
SQLDropIndexStatement dropIndex = (SQLDropIndexStatement) statement;
SQLExprTableSource tableSource = dropIndex.getTableName();
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, tableSource.getExpr(), false);
ddlResult.setType(EventType.DINDEX);
ddlResults.add(ddlResult);
} else if (statement instanceof SQLTruncateStatement) {
SQLTruncateStatement truncate = (SQLTruncateStatement) statement;
for (SQLExprTableSource tableSource : truncate.getTableSources()) {
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, tableSource.getExpr(), false);
ddlResult.setType(EventType.TRUNCATE);
ddlResults.add(ddlResult);
}
} else if (statement instanceof MySqlRenameTableStatement) {
MySqlRenameTableStatement rename = (MySqlRenameTableStatement) statement;
for (Item item : rename.getItems()) {
DdlResult ddlResult = new DdlResult();
processName(ddlResult, schmeaName, item.getName(), true);
processName(ddlResult, schmeaName, item.getTo(), false);
ddlResult.setType(EventType.RENAME);
ddlResults.add(ddlResult);
}
} else if (statement instanceof SQLInsertStatement) {
DdlResult ddlResult = new DdlResult();
SQLInsertStatement insert = (SQLInsertStatement) statement;
processName(ddlResult, schmeaName, insert.getTableName(), false);
ddlResult.setType(EventType.INSERT);
ddlResults.add(ddlResult);
} else if (statement instanceof SQLUpdateStatement) {
DdlResult ddlResult = new DdlResult();
SQLUpdateStatement update = (SQLUpdateStatement) statement;
// 拿到的表名可能为null,比如update a,b set a.id=x
processName(ddlResult, schmeaName, update.getTableName(), false);
ddlResult.setType(EventType.UPDATE);
ddlResults.add(ddlResult);
} else if (statement instanceof SQLDeleteStatement) {
DdlResult ddlResult = new DdlResult();
SQLDeleteStatement delete = (SQLDeleteStatement) statement;
// 拿到的表名可能为null,比如delete a,b from a where a.id = b.id
processName(ddlResult, schmeaName, delete.getTableName(), false);
ddlResult.setType(EventType.DELETE);
ddlResults.add(ddlResult);
} else if (statement instanceof SQLCreateDatabaseStatement) {
SQLCreateDatabaseStatement create = (SQLCreateDatabaseStatement) statement;
DdlResult ddlResult = new DdlResult();
ddlResult.setType(EventType.QUERY);
processName(ddlResult, create.getDatabaseName(), null, false);
ddlResults.add(ddlResult);
} else if (statement instanceof SQLDropDatabaseStatement) {
SQLDropDatabaseStatement drop = (SQLDropDatabaseStatement) statement;
DdlResult ddlResult = new DdlResult();
ddlResult.setType(EventType.QUERY);
processName(ddlResult, drop.getDatabaseName(), null, false);
ddlResults.add(ddlResult);
}
}
return ddlResults;
}
use of com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement in project druid by alibaba.
the class SQLServerCreateIndexTest method test_0.
public void test_0() throws Exception {
String sql = "CREATE UNIQUE INDEX [unique_schema_migrations] ON [schema_migrations] ([version])";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLCreateIndexStatement stmt = (SQLCreateIndexStatement) statementList.get(0);
Assert.assertEquals(1, statementList.size());
//
Assert.assertEquals(//
"CREATE UNIQUE INDEX [unique_schema_migrations] ON [schema_migrations] ([version])", SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(1, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("schema_migrations")));
Assert.assertTrue(visitor.getColumns().contains(new Column("schema_migrations", "version")));
}
use of com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement in project druid by alibaba.
the class SQLServerCreateIndexTest_3 method test_0.
public void test_0() throws Exception {
String sql = //
"CREATE NONCLUSTERED INDEX IX_SalesPerson_SalesQuota_SalesYTD" + " ON Sales.SalesPerson (SalesQuota, SalesYTD);";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLCreateIndexStatement stmt = (SQLCreateIndexStatement) statementList.get(0);
Assert.assertEquals(1, statementList.size());
Assert.assertEquals(//
"CREATE NONCLUSTERED INDEX IX_SalesPerson_SalesQuota_SalesYTD" + //
" ON Sales.SalesPerson (SalesQuota, SalesYTD);", SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("Sales.SalesPerson")));
Assert.assertTrue(visitor.getColumns().contains(new Column("Sales.SalesPerson", "SalesQuota")));
Assert.assertTrue(visitor.getColumns().contains(new Column("Sales.SalesPerson", "SalesYTD")));
}
Aggregations