use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project Mycat_plus by coderczp.
the class DruidCreateTableParser method statementParse.
@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);
}
use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project Mycat_plus by coderczp.
the class RouterUtil method changeCreateTable.
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;
}
use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project Mycat_plus by coderczp.
the class GlobalTableUtil method addColumnIfCreate.
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();
}
use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project dble by actiontech.
the class ItemCharTypecast method toExpression.
@Override
public SQLExpr toExpression() {
SQLCastExpr cast = new SQLCastExpr();
cast.setExpr(args.get(0).toExpression());
SQLCharacterDataType dataType = new SQLCharacterDataType(SQLCharacterDataType.CHAR_TYPE_CHAR);
cast.setDataType(dataType);
if (castLength >= 0) {
dataType.addArgument(new SQLIntegerExpr(castLength));
}
if (charSetName != null) {
dataType.setName(charSetName);
}
cast.setDataType(dataType);
return cast;
}
use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project dble by actiontech.
the class MySQLItemVisitor method endVisit.
@Override
public void endVisit(SQLCastExpr x) {
Item a = getItem(x.getExpr());
SQLDataType dataType = x.getDataType();
if (dataType instanceof SQLCharacterDataType) {
SQLCharacterDataType charType = (SQLCharacterDataType) dataType;
String upType = charType.getName().toUpperCase();
List<Integer> args = changeExprListToInt(charType.getArguments());
String charSetName = charType.getCharSetName();
if (upType.equals("CHAR")) {
int len = -1;
if (args.size() > 0) {
len = args.get(0);
}
item = new ItemCharTypecast(a, len, charSetName);
} else if (charSetName == null) {
int len = -1;
if (args.size() > 0) {
len = args.get(0);
}
item = new ItemNCharTypecast(a, len);
} else {
throw new MySQLOutPutException(ErrorCode.ER_PARSE_ERROR, "", "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'character set " + charSetName + ")'");
}
} else {
CastType castType = getCastType((SQLDataTypeImpl) dataType);
item = ItemCreate.getInstance().createFuncCast(a, castType);
}
initName(x);
}
Aggregations