Java Code Examples for com.alibaba.druid.sql.ast.statement.SQLColumnDefinition

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLColumnDefinition. These examples are extracted from open source projects. 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 Project: Mycat2   Source File: GlobalTableUtil.java    License: GNU General Public License v3.0 6 votes vote down vote up
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
@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 Project: Mycat2   Source File: RouterUtil.java    License: GNU General Public License v3.0 6 votes vote down vote up
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
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
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 Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 6 votes vote down vote up
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 Project: Mycat2   Source File: GlobalTableUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
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 Project: dble   Source File: TableMeta.java    License: GNU General Public License v2.0 5 votes vote down vote up
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 Project: dble   Source File: MetaHelper.java    License: GNU General Public License v2.0 5 votes vote down vote up
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 Project: dble   Source File: TableHandler.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}