com.alibaba.druid.sql.ast.statement.SQLColumnDefinition Java Examples
The following examples show how to use
com.alibaba.druid.sql.ast.statement.SQLColumnDefinition.
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: ElasticSqlExprParser.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
public SQLColumnDefinition parseColumn() { SQLColumnDefinition column = new SQLColumnDefinition(); column.setDbType(dbType); column.setName(name()); column.setDataType(parseDataType()); if (lexer.identifierEquals(FnvHash.Constants.GENERATED)) { lexer.nextToken(); acceptIdentifier("ALWAYS"); accept(Token.AS); accept(Token.LPAREN); SQLExpr expr = this.expr(); accept(Token.RPAREN); column.setGeneratedAlawsAs(expr); } return parseColumnRest(column); }
Example #7
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 #8
Source File: TableMeta.java From dble with GNU General Public License v2.0 | 5 votes |
public ColumnMeta(SQLColumnDefinition def) { this.name = StringUtil.removeBackAndDoubleQuote(def.getName().getSimpleName()); this.dataType = def.getDataType().getName(); for (SQLColumnConstraint constraint : def.getConstraints()) { if (constraint instanceof SQLNotNullConstraint) { this.canNull = false; break; } } }
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: TableHandler.java From dble with GNU General Public License v2.0 | 5 votes |
/** * if there are create statement in dump file, we check increment column and sharding column index for insert statement. * * @param context * @param columns * @return whether column is changed */ private boolean checkColumns(DumpFileContext context, List<SQLTableElement> columns) { SQLTableElement column; TableConfig tableConfig = context.getTableConfig(); boolean isAutoIncrement = tableConfig.isAutoIncrement(); boolean isChanged = false; for (int j = 0; j < columns.size(); j++) { column = columns.get(j); if (!(columns.get(j) instanceof SQLColumnDefinition)) { continue; } String columnName = StringUtil.removeBackQuote(((SQLColumnDefinition) column).getNameAsString()); // find index of increment column if (isAutoIncrement && columnName.equalsIgnoreCase(tableConfig.getIncrementColumn())) { // check data type of increment column // if the column is increment column, data type must be bigint. SQLColumnDefinition columnDef = (SQLColumnDefinition) column; if (!columnDef.getDataType().getName().equals("bigint")) { context.addError("data type of increment column isn't bigint, dble replaced it by itself."); columnDef.setDataType(new SQLCharacterDataType("bigint")); isChanged = true; } context.setIncrementColumnIndex(j); } // find index of partition column if (columnName.equalsIgnoreCase(tableConfig.getPartitionColumn())) { context.setPartitionColumnIndex(j); } } return isChanged; }