use of com.alibaba.druid.sql.ast.SQLPartition in project druid by alibaba.
the class MySqlStatementParser method parseAlterTable.
protected SQLStatement parseAlterTable(boolean ignore) {
lexer.nextToken();
SQLAlterTableStatement stmt = new SQLAlterTableStatement(getDbType());
stmt.setIgnore(ignore);
stmt.setName(this.exprParser.name());
for (; ; ) {
if (lexer.token() == Token.DROP) {
parseAlterDrop(stmt);
} else if (lexer.token() == Token.TRUNCATE) {
lexer.nextToken();
accept(Token.PARTITION);
SQLAlterTableTruncatePartition item = new SQLAlterTableTruncatePartition();
if (lexer.token() == Token.ALL) {
item.getPartitions().add(new SQLIdentifierExpr("ALL"));
lexer.nextToken();
} else {
this.exprParser.names(item.getPartitions(), item);
}
stmt.addItem(item);
} else if (identifierEquals("ADD")) {
lexer.nextToken();
if (lexer.token() == Token.COLUMN) {
lexer.nextToken();
parseAlterTableAddColumn(stmt);
} else if (lexer.token() == Token.INDEX) {
SQLAlterTableAddIndex item = parseAlterTableAddIndex();
item.setParent(stmt);
stmt.addItem(item);
} else if (lexer.token() == Token.UNIQUE) {
SQLAlterTableAddIndex item = parseAlterTableAddIndex();
item.setParent(stmt);
stmt.addItem(item);
} else if (lexer.token() == Token.PRIMARY) {
SQLPrimaryKey primaryKey = this.exprParser.parsePrimaryKey();
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
stmt.addItem(item);
} else if (lexer.token() == Token.KEY) {
// throw new ParserException("TODO " + lexer.token() +
// " " + lexer.stringVal());
SQLAlterTableAddIndex item = parseAlterTableAddIndex();
item.setParent(stmt);
stmt.addItem(item);
} else if (lexer.token() == Token.CONSTRAINT) {
lexer.nextToken();
SQLName constraintName = this.exprParser.name();
if (lexer.token() == Token.PRIMARY) {
SQLPrimaryKey primaryKey = ((MySqlExprParser) this.exprParser).parsePrimaryKey();
primaryKey.setName(constraintName);
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
item.setParent(stmt);
stmt.addItem(item);
} else if (lexer.token() == Token.FOREIGN) {
MysqlForeignKey fk = this.getExprParser().parseForeignKey();
fk.setName(constraintName);
fk.setHasConstraint(true);
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(fk);
stmt.addItem(item);
} else if (lexer.token() == Token.UNIQUE) {
SQLUnique unique = this.exprParser.parseUnique();
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(unique);
stmt.addItem(item);
} else {
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
}
} else if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
SQLAlterTableAddPartition item = new SQLAlterTableAddPartition();
if (identifierEquals("PARTITIONS")) {
lexer.nextToken();
item.setPartitionCount(this.exprParser.integerExpr());
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLPartition partition = this.getExprParser().parsePartition();
accept(Token.RPAREN);
item.addPartition(partition);
}
stmt.addItem(item);
} else if (identifierEquals(FULLTEXT)) {
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
} else if (identifierEquals(SPATIAL)) {
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
} else {
parseAlterTableAddColumn(stmt);
}
} else if (lexer.token() == Token.ALTER) {
lexer.nextToken();
if (lexer.token() == Token.COLUMN) {
lexer.nextToken();
}
MySqlAlterTableAlterColumn alterColumn = new MySqlAlterTableAlterColumn();
alterColumn.setColumn(this.exprParser.name());
if (lexer.token() == Token.SET) {
lexer.nextToken();
accept(Token.DEFAULT);
alterColumn.setDefaultExpr(this.exprParser.expr());
} else {
accept(Token.DROP);
accept(Token.DEFAULT);
alterColumn.setDropDefault(true);
}
stmt.addItem(alterColumn);
} else if (identifierEquals("CHANGE")) {
lexer.nextToken();
if (lexer.token() == Token.COLUMN) {
lexer.nextToken();
}
MySqlAlterTableChangeColumn item = new MySqlAlterTableChangeColumn();
item.setColumnName(this.exprParser.name());
item.setNewColumnDefinition(this.exprParser.parseColumn());
if (identifierEquals("AFTER")) {
lexer.nextToken();
item.setAfterColumn(this.exprParser.name());
} else if (identifierEquals("FIRST")) {
lexer.nextToken();
if (lexer.token() == Token.IDENTIFIER) {
item.setFirstColumn(this.exprParser.name());
} else {
item.setFirst(true);
}
}
stmt.addItem(item);
} else if (identifierEquals("MODIFY")) {
lexer.nextToken();
if (lexer.token() == Token.COLUMN) {
lexer.nextToken();
}
boolean paren = false;
if (lexer.token() == Token.LPAREN) {
paren = true;
lexer.nextToken();
}
for (; ; ) {
MySqlAlterTableModifyColumn item = new MySqlAlterTableModifyColumn();
item.setNewColumnDefinition(this.exprParser.parseColumn());
if (identifierEquals("AFTER")) {
lexer.nextToken();
item.setAfterColumn(this.exprParser.name());
} else if (identifierEquals("FIRST")) {
lexer.nextToken();
if (lexer.token() == Token.IDENTIFIER) {
item.setFirstColumn(this.exprParser.name());
} else {
item.setFirst(true);
}
}
stmt.addItem(item);
if (paren && lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
if (paren) {
accept(Token.RPAREN);
}
} else if (lexer.token() == Token.DISABLE) {
lexer.nextToken();
if (lexer.token() == Token.CONSTRAINT) {
lexer.nextToken();
SQLAlterTableDisableConstraint item = new SQLAlterTableDisableConstraint();
item.setConstraintName(this.exprParser.name());
stmt.addItem(item);
} else {
acceptIdentifier("KEYS");
SQLAlterTableDisableKeys item = new SQLAlterTableDisableKeys();
stmt.addItem(item);
}
} else if (lexer.token() == Token.ENABLE) {
lexer.nextToken();
if (lexer.token() == Token.CONSTRAINT) {
lexer.nextToken();
SQLAlterTableEnableConstraint item = new SQLAlterTableEnableConstraint();
item.setConstraintName(this.exprParser.name());
stmt.addItem(item);
} else {
acceptIdentifier("KEYS");
SQLAlterTableEnableKeys item = new SQLAlterTableEnableKeys();
stmt.addItem(item);
}
} else if (identifierEquals("RENAME")) {
lexer.nextToken();
if (lexer.token() == Token.TO || lexer.token() == Token.AS) {
lexer.nextToken();
}
MySqlRenameTableStatement renameStmt = new MySqlRenameTableStatement();
MySqlRenameTableStatement.Item item = new MySqlRenameTableStatement.Item();
item.setName(stmt.getTableSource().getExpr());
item.setTo(this.exprParser.name());
renameStmt.addItem(item);
return renameStmt;
} else if (lexer.token() == Token.ORDER) {
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
} else if (identifierEquals("CONVERT")) {
lexer.nextToken();
accept(Token.TO);
acceptIdentifier("CHARACTER");
accept(Token.SET);
SQLAlterTableConvertCharSet item = new SQLAlterTableConvertCharSet();
SQLExpr charset = this.exprParser.primary();
item.setCharset(charset);
if (identifierEquals("COLLATE")) {
lexer.nextToken();
SQLExpr collate = this.exprParser.primary();
item.setCollate(collate);
}
stmt.addItem(item);
} else if (lexer.token() == Token.DEFAULT) {
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
} else if (identifierEquals("DISCARD")) {
lexer.nextToken();
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
SQLAlterTableDiscardPartition item = new SQLAlterTableDiscardPartition();
if (lexer.token() == Token.ALL) {
lexer.nextToken();
item.getPartitions().add(new SQLIdentifierExpr("ALL"));
} else {
this.exprParser.names(item.getPartitions(), item);
}
stmt.addItem(item);
} else {
accept(Token.TABLESPACE);
MySqlAlterTableDiscardTablespace item = new MySqlAlterTableDiscardTablespace();
stmt.addItem(item);
}
} else if (lexer.token() == Token.CHECK) {
lexer.nextToken();
accept(Token.PARTITION);
SQLAlterTableCheckPartition item = new SQLAlterTableCheckPartition();
if (lexer.token() == Token.ALL) {
lexer.nextToken();
item.getPartitions().add(new SQLIdentifierExpr("ALL"));
} else {
this.exprParser.names(item.getPartitions(), item);
}
stmt.addItem(item);
} else if (identifierEquals("IMPORT")) {
lexer.nextToken();
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
SQLAlterTableImportPartition item = new SQLAlterTableImportPartition();
if (lexer.token() == Token.ALL) {
lexer.nextToken();
item.getPartitions().add(new SQLIdentifierExpr("ALL"));
} else {
this.exprParser.names(item.getPartitions(), item);
}
stmt.addItem(item);
} else {
accept(Token.TABLESPACE);
MySqlAlterTableImportTablespace item = new MySqlAlterTableImportTablespace();
stmt.addItem(item);
}
} else if (lexer.token() == Token.ANALYZE) {
lexer.nextToken();
accept(Token.PARTITION);
SQLAlterTableAnalyzePartition item = new SQLAlterTableAnalyzePartition();
if (lexer.token() == Token.ALL) {
lexer.nextToken();
item.getPartitions().add(new SQLIdentifierExpr("ALL"));
} else {
this.exprParser.names(item.getPartitions(), item);
}
stmt.addItem(item);
} else if (identifierEquals("FORCE")) {
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
} else if (identifierEquals("COALESCE")) {
lexer.nextToken();
accept(Token.PARTITION);
SQLAlterTableCoalescePartition item = new SQLAlterTableCoalescePartition();
SQLIntegerExpr countExpr = this.exprParser.integerExpr();
item.setCount(countExpr);
stmt.addItem(item);
} else if (identifierEquals("REORGANIZE")) {
lexer.nextToken();
accept(Token.PARTITION);
SQLAlterTableReOrganizePartition item = new SQLAlterTableReOrganizePartition();
this.exprParser.names(item.getNames(), item);
accept(Token.INTO);
accept(Token.LPAREN);
for (; ; ) {
SQLPartition partition = this.getExprParser().parsePartition();
item.addPartition(partition);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
accept(Token.RPAREN);
stmt.addItem(item);
} else if (identifierEquals("EXCHANGE")) {
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
} else if (lexer.token() == Token.OPTIMIZE) {
lexer.nextToken();
accept(Token.PARTITION);
SQLAlterTableOptimizePartition item = new SQLAlterTableOptimizePartition();
if (lexer.token() == Token.ALL) {
lexer.nextToken();
item.getPartitions().add(new SQLIdentifierExpr("ALL"));
} else {
this.exprParser.names(item.getPartitions(), item);
}
stmt.addItem(item);
} else if (identifierEquals("REBUILD")) {
lexer.nextToken();
accept(Token.PARTITION);
SQLAlterTableRebuildPartition item = new SQLAlterTableRebuildPartition();
if (lexer.token() == Token.ALL) {
lexer.nextToken();
item.getPartitions().add(new SQLIdentifierExpr("ALL"));
} else {
this.exprParser.names(item.getPartitions(), item);
}
stmt.addItem(item);
} else if (identifierEquals("REPAIR")) {
lexer.nextToken();
accept(Token.PARTITION);
SQLAlterTableRepairPartition item = new SQLAlterTableRepairPartition();
if (lexer.token() == Token.ALL) {
lexer.nextToken();
item.getPartitions().add(new SQLIdentifierExpr("ALL"));
} else {
this.exprParser.names(item.getPartitions(), item);
}
stmt.addItem(item);
} else if (identifierEquals("REMOVE")) {
lexer.nextToken();
acceptIdentifier("PARTITIONING");
stmt.setRemovePatiting(true);
} else if (identifierEquals("UPGRADE")) {
lexer.nextToken();
acceptIdentifier("PARTITIONING");
stmt.setUpgradePatiting(true);
} else if (identifierEquals("ALGORITHM")) {
lexer.nextToken();
accept(Token.EQ);
stmt.addItem(new MySqlAlterTableOption("ALGORITHM", lexer.stringVal()));
lexer.nextToken();
} else if (identifierEquals(ENGINE)) {
lexer.nextToken();
accept(Token.EQ);
stmt.addItem(new MySqlAlterTableOption(ENGINE, lexer.stringVal()));
lexer.nextToken();
} else if (identifierEquals(AUTO_INCREMENT)) {
lexer.nextToken();
accept(Token.EQ);
stmt.addItem(new MySqlAlterTableOption(AUTO_INCREMENT, lexer.integerValue()));
lexer.nextToken();
} else if (identifierEquals(COLLATE2)) {
lexer.nextToken();
accept(Token.EQ);
stmt.addItem(new MySqlAlterTableOption(COLLATE2, lexer.stringVal()));
lexer.nextToken();
} else if (identifierEquals("PACK_KEYS")) {
lexer.nextToken();
accept(Token.EQ);
if (identifierEquals("PACK")) {
lexer.nextToken();
accept(Token.ALL);
stmt.addItem(new MySqlAlterTableOption("PACK_KEYS", "PACK ALL"));
} else {
stmt.addItem(new MySqlAlterTableOption("PACK_KEYS", lexer.stringVal()));
lexer.nextToken();
}
} else if (identifierEquals(CHARACTER)) {
lexer.nextToken();
accept(Token.SET);
accept(Token.EQ);
MySqlAlterTableCharacter item = new MySqlAlterTableCharacter();
item.setCharacterSet(this.exprParser.primary());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
acceptIdentifier(COLLATE2);
accept(Token.EQ);
item.setCollate(this.exprParser.primary());
}
stmt.addItem(item);
} else if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
accept(Token.EQ);
}
stmt.addItem(new MySqlAlterTableOption("COMMENT", '\'' + lexer.stringVal() + '\''));
lexer.nextToken();
} else if (lexer.token() == Token.UNION) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
accept(Token.LPAREN);
SQLTableSource tableSrc = this.createSQLSelectParser().parseTableSource();
stmt.getTableOptions().put("UNION", tableSrc);
accept(Token.RPAREN);
} else if (identifierEquals("ROW_FORMAT")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
if (lexer.token() == Token.DEFAULT || lexer.token() == Token.IDENTIFIER) {
SQLIdentifierExpr rowFormat = new SQLIdentifierExpr(lexer.stringVal());
lexer.nextToken();
stmt.getTableOptions().put("ROW_FORMAT", rowFormat);
} else {
throw new ParserException("illegal syntax.");
}
} else {
break;
}
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
return stmt;
}
use of com.alibaba.druid.sql.ast.SQLPartition in project druid by alibaba.
the class MySqlCreateTableParser method parseCrateTable.
public MySqlCreateTableStatement parseCrateTable(boolean acceptCreate) {
if (acceptCreate) {
accept(Token.CREATE);
}
MySqlCreateTableStatement stmt = new MySqlCreateTableStatement();
if (identifierEquals("TEMPORARY")) {
lexer.nextToken();
stmt.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
}
accept(Token.TABLE);
if (lexer.token() == Token.IF || identifierEquals("IF")) {
lexer.nextToken();
accept(Token.NOT);
accept(Token.EXISTS);
stmt.setIfNotExiists(true);
}
stmt.setName(this.exprParser.name());
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
SQLName name = this.exprParser.name();
stmt.setLike(name);
}
if (lexer.token() == (Token.LPAREN)) {
lexer.nextToken();
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
SQLName name = this.exprParser.name();
stmt.setLike(name);
} else {
for (; ; ) {
if (//
lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_CHARS) {
SQLColumnDefinition column = this.exprParser.parseColumn();
stmt.getTableElementList().add(column);
} else if (//
lexer.token() == Token.CONSTRAINT || //
lexer.token() == Token.PRIMARY || lexer.token() == Token.UNIQUE) {
SQLTableConstraint constraint = this.parseConstraint();
stmt.getTableElementList().add(constraint);
} else if (lexer.token() == (Token.INDEX)) {
lexer.nextToken();
MySqlTableIndex idx = new MySqlTableIndex();
if (lexer.token() == Token.IDENTIFIER) {
if (!"USING".equalsIgnoreCase(lexer.stringVal())) {
idx.setName(this.exprParser.name());
}
}
if (identifierEquals("USING")) {
lexer.nextToken();
idx.setIndexType(lexer.stringVal());
lexer.nextToken();
}
accept(Token.LPAREN);
for (; ; ) {
idx.addColumn(this.exprParser.expr());
if (!(lexer.token() == (Token.COMMA))) {
break;
} else {
lexer.nextToken();
}
}
accept(Token.RPAREN);
if (identifierEquals("USING")) {
lexer.nextToken();
idx.setIndexType(lexer.stringVal());
lexer.nextToken();
}
stmt.getTableElementList().add(idx);
} else if (lexer.token() == (Token.KEY)) {
stmt.getTableElementList().add(parseConstraint());
} else if (lexer.token() == (Token.PRIMARY)) {
SQLTableConstraint pk = parseConstraint();
pk.setParent(stmt);
stmt.getTableElementList().add(pk);
} else if (lexer.token() == (Token.FOREIGN)) {
SQLForeignKeyConstraint fk = this.getExprParser().parseForeignKey();
fk.setParent(stmt);
stmt.getTableElementList().add(fk);
} else if (lexer.token() == Token.CHECK) {
SQLCheck check = this.exprParser.parseCheck();
stmt.getTableElementList().add(check);
} else {
SQLColumnDefinition column = this.exprParser.parseColumn();
stmt.getTableElementList().add(column);
}
if (!(lexer.token() == (Token.COMMA))) {
break;
} else {
lexer.nextToken();
}
}
}
accept(Token.RPAREN);
}
for (; ; ) {
if (identifierEquals("ENGINE")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("ENGINE", this.exprParser.expr());
continue;
}
if (identifierEquals("AUTO_INCREMENT")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("AUTO_INCREMENT", this.exprParser.expr());
continue;
}
if (identifierEquals("AVG_ROW_LENGTH")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("AVG_ROW_LENGTH", this.exprParser.expr());
continue;
}
if (lexer.token() == Token.DEFAULT) {
lexer.nextToken();
parseTableOptionCharsetOrCollate(stmt);
continue;
}
if (parseTableOptionCharsetOrCollate(stmt)) {
continue;
}
if (identifierEquals("CHECKSUM")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("CHECKSUM", this.exprParser.expr());
continue;
}
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("COMMENT", this.exprParser.expr());
continue;
}
if (identifierEquals("CONNECTION")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("CONNECTION", this.exprParser.expr());
continue;
}
if (identifierEquals("DATA")) {
lexer.nextToken();
acceptIdentifier("DIRECTORY");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("DATA DIRECTORY", this.exprParser.expr());
continue;
}
if (identifierEquals("DELAY_KEY_WRITE")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("DELAY_KEY_WRITE", this.exprParser.expr());
continue;
}
if (identifierEquals("INDEX")) {
lexer.nextToken();
acceptIdentifier("DIRECTORY");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("INDEX DIRECTORY", this.exprParser.expr());
continue;
}
if (identifierEquals("INSERT_METHOD")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("INSERT_METHOD", this.exprParser.expr());
continue;
}
if (identifierEquals("KEY_BLOCK_SIZE")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("KEY_BLOCK_SIZE", this.exprParser.expr());
continue;
}
if (identifierEquals("MAX_ROWS")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("MAX_ROWS", this.exprParser.expr());
continue;
}
if (identifierEquals("MIN_ROWS")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("MIN_ROWS", this.exprParser.expr());
continue;
}
if (identifierEquals("PACK_KEYS")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("PACK_KEYS", this.exprParser.expr());
continue;
}
if (identifierEquals("PASSWORD")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("PASSWORD", this.exprParser.expr());
continue;
}
if (identifierEquals("ROW_FORMAT")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("ROW_FORMAT", this.exprParser.expr());
continue;
}
if (identifierEquals("STATS_AUTO_RECALC")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("STATS_AUTO_RECALC", this.exprParser.expr());
continue;
}
if (identifierEquals("STATS_PERSISTENT")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("STATS_PERSISTENT", this.exprParser.expr());
continue;
}
if (identifierEquals("STATS_SAMPLE_PAGES")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
stmt.getTableOptions().put("STATS_SAMPLE_PAGES", this.exprParser.expr());
continue;
}
if (lexer.token() == Token.UNION) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
accept(Token.LPAREN);
SQLTableSource tableSrc = this.createSQLSelectParser().parseTableSource();
stmt.getTableOptions().put("UNION", tableSrc);
accept(Token.RPAREN);
continue;
}
if (lexer.token() == Token.TABLESPACE) {
lexer.nextToken();
TableSpaceOption option = new TableSpaceOption();
option.setName(this.exprParser.name());
if (identifierEquals("STORAGE")) {
lexer.nextToken();
option.setStorage(this.exprParser.name());
}
stmt.getTableOptions().put("TABLESPACE", option);
continue;
}
if (identifierEquals("TABLEGROUP")) {
lexer.nextToken();
SQLName tableGroup = this.exprParser.name();
stmt.setTableGroup(tableGroup);
continue;
}
if (identifierEquals("TYPE")) {
lexer.nextToken();
accept(Token.EQ);
stmt.getTableOptions().put("TYPE", this.exprParser.expr());
lexer.nextToken();
continue;
}
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
accept(Token.BY);
SQLPartitionBy partitionClause;
boolean linera = false;
if (identifierEquals("LINEAR")) {
lexer.nextToken();
linera = true;
}
if (lexer.token() == Token.KEY) {
MySqlPartitionByKey clause = new MySqlPartitionByKey();
lexer.nextToken();
if (linera) {
clause.setLinear(true);
}
accept(Token.LPAREN);
if (lexer.token() != Token.RPAREN) {
for (; ; ) {
clause.addColumn(this.exprParser.name());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
}
accept(Token.RPAREN);
partitionClause = clause;
partitionClauseRest(clause);
} else if (identifierEquals("HASH")) {
lexer.nextToken();
SQLPartitionByHash clause = new SQLPartitionByHash();
if (linera) {
clause.setLinear(true);
}
if (lexer.token() == Token.KEY) {
lexer.nextToken();
clause.setKey(true);
}
accept(Token.LPAREN);
clause.setExpr(this.exprParser.expr());
accept(Token.RPAREN);
partitionClause = clause;
partitionClauseRest(clause);
} else if (identifierEquals("RANGE")) {
SQLPartitionByRange clause = partitionByRange();
partitionClause = clause;
partitionClauseRest(clause);
} else if (identifierEquals("LIST")) {
lexer.nextToken();
SQLPartitionByList clause = new SQLPartitionByList();
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
clause.setExpr(this.exprParser.expr());
accept(Token.RPAREN);
} else {
acceptIdentifier("COLUMNS");
accept(Token.LPAREN);
for (; ; ) {
clause.addColumn(this.exprParser.name());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
}
partitionClause = clause;
partitionClauseRest(clause);
} else {
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (; ; ) {
SQLPartition partitionDef = this.getExprParser().parsePartition();
partitionClause.addPartition(partitionDef);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
accept(Token.RPAREN);
}
stmt.setPartitioning(partitionClause);
continue;
}
break;
}
if (lexer.token() == (Token.ON)) {
throw new ParserException("TODO");
}
if (lexer.token() == (Token.AS)) {
lexer.nextToken();
}
if (lexer.token() == (Token.SELECT)) {
SQLSelect query = new MySqlSelectParser(this.exprParser).select();
stmt.setSelect(query);
}
while (lexer.token() == (Token.HINT)) {
this.exprParser.parseHints(stmt.getOptionHints());
}
return stmt;
}
use of com.alibaba.druid.sql.ast.SQLPartition in project druid by alibaba.
the class MySqlExprParser method parsePartition.
public SQLPartition parsePartition() {
accept(Token.PARTITION);
SQLPartition partitionDef = new SQLPartition();
partitionDef.setName(this.name());
SQLPartitionValue values = this.parsePartitionValues();
if (values != null) {
partitionDef.setValues(values);
}
for (; ; ) {
boolean storage = false;
if (identifierEquals("DATA")) {
lexer.nextToken();
acceptIdentifier("DIRECTORY");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
partitionDef.setDataDirectory(this.expr());
} else if (lexer.token() == Token.TABLESPACE) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLName tableSpace = this.name();
partitionDef.setTableSpace(tableSpace);
} else if (lexer.token() == Token.INDEX) {
lexer.nextToken();
acceptIdentifier("DIRECTORY");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
partitionDef.setIndexDirectory(this.expr());
} else if (identifierEquals("MAX_ROWS")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr maxRows = this.primary();
partitionDef.setMaxRows(maxRows);
} else if (identifierEquals("MIN_ROWS")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr minRows = this.primary();
partitionDef.setMaxRows(minRows);
} else if (//
identifierEquals("ENGINE") || (storage = (lexer.token() == Token.STORAGE || identifierEquals("STORAGE")))) {
if (storage) {
lexer.nextToken();
}
acceptIdentifier("ENGINE");
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLName engine = this.name();
partitionDef.setEngine(engine);
} else if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr comment = this.primary();
partitionDef.setComment(comment);
} else {
break;
}
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (; ; ) {
acceptIdentifier("SUBPARTITION");
SQLName subPartitionName = this.name();
SQLSubPartition subPartition = new SQLSubPartition();
subPartition.setName(subPartitionName);
partitionDef.addSubPartition(subPartition);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
}
return partitionDef;
}
use of com.alibaba.druid.sql.ast.SQLPartition in project druid by alibaba.
the class OracleCreateTableParser method parsePartitionByRest.
protected void parsePartitionByRest(SQLPartitionBy clause) {
if (lexer.token() == Token.STORE) {
lexer.nextToken();
accept(Token.IN);
accept(Token.LPAREN);
for (; ; ) {
SQLName tablespace = this.exprParser.name();
clause.getStoreIn().add(tablespace);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
}
if (identifierEquals("SUBPARTITION")) {
SQLSubPartitionBy subPartitionBy = subPartitionBy();
clause.setSubPartitionBy(subPartitionBy);
}
accept(Token.LPAREN);
for (; ; ) {
SQLPartition partition = parsePartition();
clause.addPartition(partition);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
}
use of com.alibaba.druid.sql.ast.SQLPartition in project druid by alibaba.
the class OracleCreateTableParser method parsePartition.
protected SQLPartition parsePartition() {
acceptIdentifier("PARTITION");
SQLPartition partition = new SQLPartition();
partition.setName(this.exprParser.name());
SQLPartitionValue values = this.exprParser.parsePartitionValues();
if (values != null) {
partition.setValues(values);
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (; ; ) {
SQLSubPartition subPartition = parseSubPartition();
partition.addSubPartition(subPartition);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
} else if (identifierEquals("SUBPARTITIONS")) {
lexer.nextToken();
SQLExpr subPartitionsCount = this.exprParser.primary();
partition.setSubPartitionsCount(subPartitionsCount);
}
return partition;
}
Aggregations