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

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement. 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: 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 7
Source Project: Mycat2   Source File: GlobalTableUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
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 Project: dble   Source File: MetaHelper.java    License: GNU General Public License v2.0 5 votes vote down vote up
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 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: Mycat2   Source File: GlobalTableUtil.java    License: GNU General Public License v3.0 4 votes vote down vote up
private static boolean isCreate(SQLStatement statement) {
	return statement instanceof SQLCreateTableStatement;
}