com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement Java Examples
The following examples show how to use
com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement.
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: GlobalTableUtil.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
private static boolean hasGlobalColumn(SQLStatement statement){ for (SQLTableElement tableElement : ((SQLCreateTableStatement)statement).getTableElementList()) { SQLName sqlName = null; if (tableElement instanceof SQLColumnDefinition) { sqlName = ((SQLColumnDefinition)tableElement).getName(); } if (sqlName != null) { String simpleName = sqlName.getSimpleName(); simpleName = StringUtil.removeBackquote(simpleName); if (tableElement instanceof SQLColumnDefinition && GLOBAL_TABLE_MYCAT_COLUMN.equalsIgnoreCase(simpleName)) { return true; } } } return false; }
Example #2
Source File: DruidCreateTableParser.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
@Override public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException { MySqlCreateTableStatement createStmt = (MySqlCreateTableStatement)stmt; if(createStmt.getQuery() != null) { String msg = "create table from other table not supported :" + stmt; LOGGER.warn(msg); throw new SQLNonTransientException(msg); } String tableName = StringUtil.removeBackquote(createStmt.getTableSource().toString().toUpperCase()); if(schema.getTables().containsKey(tableName)) { TableConfig tableConfig = schema.getTables().get(tableName); AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm(); if(algorithm instanceof SlotFunction){ SQLColumnDefinition column = new SQLColumnDefinition(); column.setDataType(new SQLCharacterDataType("int")); column.setName(new SQLIdentifierExpr("_slot")); column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改")); ((SQLCreateTableStatement)stmt).getTableElementList().add(column); String sql = createStmt.toString(); rrs.setStatement(sql); ctx.setSql(sql); } } ctx.addTable(tableName); }
Example #3
Source File: RouterUtil.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
private static String changeCreateTable(SchemaConfig schema,String tableName,String sql) { if (schema.getTables().containsKey(tableName)) { MySqlStatementParser parser = new MySqlStatementParser(sql); SQLStatement insertStatement = parser.parseStatement(); if (insertStatement instanceof MySqlCreateTableStatement) { TableConfig tableConfig = schema.getTables().get(tableName); AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm(); if (algorithm instanceof SlotFunction) { SQLColumnDefinition column = new SQLColumnDefinition(); column.setDataType(new SQLCharacterDataType("int")); column.setName(new SQLIdentifierExpr("_slot")); column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改")); ((SQLCreateTableStatement) insertStatement).getTableElementList().add(column); return insertStatement.toString(); } } } return sql; }
Example #4
Source File: DruidMysqlCreateTableTest.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
private static boolean hasColumn(SQLStatement statement){ for (SQLTableElement tableElement : ((SQLCreateTableStatement)statement).getTableElementList()) { SQLName sqlName = null; if (tableElement instanceof SQLColumnDefinition) { sqlName = ((SQLColumnDefinition)tableElement).getName(); } if (sqlName != null) { String simpleName = sqlName.getSimpleName(); simpleName = StringUtil.removeBackquote(simpleName); if (tableElement instanceof SQLColumnDefinition && "_slot".equalsIgnoreCase(simpleName)) { return true; } } } return false; }
Example #5
Source File: DruidMysqlCreateTableTest.java From dble with GNU General Public License v2.0 | 6 votes |
private static boolean hasColumn(SQLStatement statement) { for (SQLTableElement tableElement : ((SQLCreateTableStatement) statement).getTableElementList()) { SQLName sqlName = null; if (tableElement instanceof SQLColumnDefinition) { sqlName = ((SQLColumnDefinition) tableElement).getName(); } if (sqlName != null) { String simpleName = sqlName.getSimpleName(); simpleName = StringUtil.removeBackQuote(simpleName); if (tableElement instanceof SQLColumnDefinition && "_slot".equalsIgnoreCase(simpleName)) { return true; } } } return false; }
Example #6
Source File: GlobalTableUtil.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
static String addColumnIfCreate(String sql, SQLStatement statement) { if (isCreate(statement) && sql.trim().toUpperCase().startsWith("CREATE TABLE ") && !hasGlobalColumn(statement)) { SQLColumnDefinition column = new SQLColumnDefinition(); column.setDataType(new SQLCharacterDataType("bigint")); column.setName(new SQLIdentifierExpr(GLOBAL_TABLE_MYCAT_COLUMN)); column.setComment(new SQLCharExpr("全局表保存修改时间戳的字段名")); ((SQLCreateTableStatement)statement).getTableElementList().add(column); } return statement.toString(); }
Example #7
Source File: GlobalTableUtil.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
private static SQLExprTableSource getDDLTableSource(SQLStatement statement) { SQLExprTableSource source = null; if (statement instanceof SQLAlterTableStatement) { source = ((SQLAlterTableStatement)statement).getTableSource(); } else if (isCreate(statement)) { source = ((SQLCreateTableStatement)statement).getTableSource(); } return source; }
Example #8
Source File: MetaHelper.java From dble with GNU General Public License v2.0 | 5 votes |
static TableMeta initTableMeta(String table, String sql, long timeStamp) { if (sql == null) { return null; } try { SQLStatementParser parser = new DbleCreateTableParser(sql); SQLCreateTableStatement createStatement = parser.parseCreateTable(); return MetaHelper.initTableMeta(table, createStatement, timeStamp); } catch (Exception e) { LOGGER.warn("sql[" + sql + "] parser error:", e); AlertUtil.alertSelf(AlarmCode.GET_TABLE_META_FAIL, Alert.AlertLevel.WARN, "sql[" + sql + "] parser error:" + e.getMessage(), null); return null; } }
Example #9
Source File: MetaHelper.java From dble with GNU General Public License v2.0 | 5 votes |
private static TableMeta initTableMeta(String table, SQLCreateTableStatement createStatement, long timeStamp) { TableMeta tableMeta = new TableMeta(); tableMeta.setTableName(table); tableMeta.setVersion(timeStamp); tableMeta.setCreateSql(createStatement.toString()); List<TableMeta.ColumnMeta> columns = new ArrayList<>(createStatement.getTableElementList().size()); for (SQLTableElement tableElement : createStatement.getTableElementList()) { if (tableElement instanceof SQLColumnDefinition) { columns.add(new TableMeta.ColumnMeta((SQLColumnDefinition) tableElement)); } } tableMeta.setColumns(columns); return tableMeta; }
Example #10
Source File: GlobalTableUtil.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
private static boolean isCreate(SQLStatement statement) { return statement instanceof SQLCreateTableStatement; }