com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement Java Examples

The following examples show how to use com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement. 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: DruidCreateTableParser.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@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 #2
Source File: RouterUtil.java    From Mycat2 with 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 #3
Source File: TableHandler.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public SQLStatement preHandle(DumpFileContext context, String stmt) throws SQLSyntaxErrorException {
    SQLStatement sqlStatement = RouteStrategyFactory.getRouteStrategy().parserSQL(stmt);
    String tableName;
    if (sqlStatement instanceof MySqlCreateTableStatement) {
        tableName = StringUtil.removeBackQuote(((MySqlCreateTableStatement) sqlStatement).getTableSource().getName().getSimpleName());
        context.setTable(tableName);
        if (context.getTableType() == TableType.DEFAULT) {
            return null;
        }
        boolean isChanged = preHandleCreateTable(context, sqlStatement);
        return isChanged ? sqlStatement : null;
    } else if (sqlStatement instanceof SQLDropTableStatement) {
        tableName = StringUtil.removeBackQuote(((SQLDropTableStatement) sqlStatement).getTableSources().get(0).getName().getSimpleName());
        context.setTable(tableName);
    } else if (sqlStatement instanceof MySqlLockTableStatement) {
        tableName = StringUtil.removeBackQuote(((MySqlLockTableStatement) sqlStatement).getTableSource().getName().getSimpleName());
        context.setTable(tableName);
    }
    return null;
}
 
Example #4
Source File: TableHandler.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
/**
 * pre handle create table statement
 *
 * @param context
 * @param sqlStatement
 * @return whether statement is changed
 */
private boolean preHandleCreateTable(DumpFileContext context, SQLStatement sqlStatement) {
    TableConfig tableConfig = context.getTableConfig();
    List<SQLTableElement> columns = ((MySqlCreateTableStatement) sqlStatement).getTableElementList();
    boolean isChanged = false;
    if (tableConfig.isAutoIncrement() || tableConfig.getPartitionColumn() != null) {
        // check columns for sharing column index or increment column index
        isChanged = checkColumns(context, columns);
        // partition column check
        if (tableConfig.getPartitionColumn() != null && context.getPartitionColumnIndex() == -1) {
            throw new DumpException("can't find partition column in create.");
        }
        // increment column check
        if (tableConfig.isAutoIncrement() && context.getIncrementColumnIndex() == -1) {
            throw new DumpException("can't find increment column in create.");
        }
    }
    return isChanged;
}
 
Example #5
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public boolean visit(MySqlCreateTableStatement x) {
    SQLName sqlName=  x.getName();
    if(sqlName!=null)
    {
        String table = sqlName.toString();
        if(table.startsWith("`"))
        {
            table=table.substring(1,table.length()-1);
        }
        setCurrentTable(table);
    }
    return false;
}
 
Example #6
Source File: DruidParserFactory.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public static DruidParser create(SchemaConfig schema, SQLStatement statement, SchemaStatVisitor visitor)
{
    DruidParser parser = null;
    if (statement instanceof SQLSelectStatement)
    {
        if(schema.isNeedSupportMultiDBType())
        {
            parser = getDruidParserForMultiDB(schema, statement, visitor);

        }

        if (parser == null)
        {
            parser = new DruidSelectParser();
        }
    } else if (statement instanceof MySqlInsertStatement)
    {
        parser = new DruidInsertParser();
    } else if (statement instanceof MySqlDeleteStatement)
    {
        parser = new DruidDeleteParser();
    } else if (statement instanceof MySqlCreateTableStatement)
    {
        parser = new DruidCreateTableParser();
    } else if (statement instanceof MySqlUpdateStatement)
    {
        parser = new DruidUpdateParser();
    } else if (statement instanceof SQLAlterTableStatement)
    {
        parser = new DruidAlterTableParser();
    } else if (statement instanceof MySqlLockTableStatement) {
    	parser = new DruidLockTableParser();
    } else
    {
        parser = new DefaultDruidParser();
    }

    return parser;
}
 
Example #7
Source File: DbleCreateTableParser.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
private boolean parseTableOptionCharsetOrCollate(MySqlCreateTableStatement stmt) {
    if (lexer.identifierEquals("CHARACTER")) {
        lexer.nextToken();
        accept(Token.SET);
        if (lexer.token() == Token.EQ) {
            lexer.nextToken();
        }
        stmt.getTableOptions().put("CHARACTER SET", this.exprParser.expr());
        return true;
    }

    if (lexer.identifierEquals("CHARSET")) {
        lexer.nextToken();
        if (lexer.token() == Token.EQ) {
            lexer.nextToken();
        }
        stmt.getTableOptions().put("CHARSET", this.exprParser.expr());
        return true;
    }

    if (lexer.identifierEquals("COLLATE")) {
        lexer.nextToken();
        if (lexer.token() == Token.EQ) {
            lexer.nextToken();
        }
        stmt.getTableOptions().put("COLLATE", this.exprParser.expr());
        return true;
    }

    return false;
}