improved db2 parser support. for issue #2489

This commit is contained in:
wenshao
2018-04-16 00:03:21 +08:00
parent 3ea9c0c8ba
commit 0acd0c76df
16 changed files with 440 additions and 79 deletions
@@ -75,11 +75,7 @@ public class DB2CreateTableParser extends SQLCreateTableParser {
for (; ; ) {
Token token = lexer.token();
if (token == Token.IDENTIFIER
&& lexer.stringVal().equalsIgnoreCase("SUPPLEMENTAL")
&& JdbcConstants.ORACLE.equals(dbType)) {
this.parseCreateTableSupplementalLogingProps(createTable);
} else if (token == Token.IDENTIFIER //
if (token == Token.IDENTIFIER //
|| token == Token.LITERAL_ALIAS) {
SQLColumnDefinition column = this.exprParser.parseColumn();
createTable.getTableElementList().add(column);
@@ -18,10 +18,8 @@ package com.alibaba.druid.sql.dialect.db2.parser;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.alibaba.druid.sql.parser.SQLParserFeature;
import com.alibaba.druid.sql.parser.Token;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.parser.*;
import com.alibaba.druid.util.FnvHash;
import com.alibaba.druid.util.JdbcConstants;
@@ -184,4 +182,64 @@ public class DB2ExprParser extends SQLExprParser {
return super.dotRest(expr);
}
public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) {
column = super.parseColumnRest(column);
if (lexer.identifierEquals(FnvHash.Constants.GENERATED)) {
lexer.nextToken();
if (lexer.identifierEquals(FnvHash.Constants.ALWAYS)) {
lexer.nextToken();
} else {
throw new ParserException("TODO " + lexer.info());
}
accept(Token.AS);
if (lexer.token() == Token.IDENTITY) {
SQLColumnDefinition.Identity identity = parseIdentity();
column.setIdentity(identity);
} else {
SQLExpr expr = this.expr();
column.setGeneratedAlawsAs(expr);
}
}
return column;
}
private SQLColumnDefinition.Identity parseIdentity() {
SQLColumnDefinition.Identity identity = new SQLColumnDefinition.Identity();
accept(Token.IDENTITY);
accept(Token.LPAREN);
if (lexer.identifierEquals(FnvHash.Constants.START)) {
lexer.nextToken();
accept(Token.WITH);
if (lexer.token() == Token.LITERAL_INT) {
identity.setSeed((Integer) lexer.integerValue());
lexer.nextToken();
} else {
throw new ParserException("TODO " + lexer.info());
}
accept(Token.COMMA);
}
if (lexer.identifierEquals(FnvHash.Constants.INCREMENT)) {
lexer.nextToken();
accept(Token.BY);
if (lexer.token() == Token.LITERAL_INT) {
identity.setIncrement((Integer) lexer.integerValue());
lexer.nextToken();
} else {
throw new ParserException("TODO " + lexer.info());
}
}
accept(Token.RPAREN);
return identity;
}
}
@@ -21,6 +21,7 @@ import com.alibaba.druid.sql.ast.SQLPartitionBy;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLIntervalExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntervalUnit;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2CreateTableStatement;
@@ -167,4 +168,21 @@ public class DB2OutputVisitor extends SQLASTOutputVisitor implements DB2ASTVisit
}
return false;
}
public boolean visit(SQLColumnDefinition.Identity x) {
print0(ucase ? "GENERATED ALWAYS AS IDENTITY (" : "generated always as identity (");
final Integer seed = x.getSeed();
if (seed != null) {
print0(ucase ? "START WITH " : "start with ");
print(seed);
print0(", ");
}
print0(ucase ? "INCREMENT BY " : "increment by ");
print(x.getIncrement());
print(')');
return false;
}
}
@@ -138,22 +138,7 @@ public class PGSelectParser extends SQLSelectParser {
parseGroupBy(queryBlock);
if (lexer.token() == Token.WINDOW) {
lexer.nextToken();
PGSelectQueryBlock.WindowClause window = new PGSelectQueryBlock.WindowClause();
window.setName(this.expr());
accept(Token.AS);
for (;;) {
SQLExpr expr = this.createExprParser().expr();
window.getDefinition().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
queryBlock.setWindow(window);
this.parseWindow(queryBlock);
}
queryBlock.setOrderBy(this.createExprParser().parseOrderBy());