mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved postgresql parser. for issue #2129
This commit is contained in:
@@ -65,6 +65,8 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
protected final List<SQLSelectOrderByItem> sortedBy = new ArrayList<SQLSelectOrderByItem>();
|
||||
protected int buckets;
|
||||
|
||||
protected Map<String, SQLObject> tableOptions = new LinkedHashMap<String, SQLObject>();
|
||||
|
||||
public SQLCreateTableStatement(){
|
||||
|
||||
}
|
||||
@@ -231,6 +233,10 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
this.partitioning = partitioning;
|
||||
}
|
||||
|
||||
public Map<String, SQLObject> getTableOptions() {
|
||||
return tableOptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
|
||||
-4
@@ -66,10 +66,6 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
|
||||
this.tableOptions = tableOptions;
|
||||
}
|
||||
|
||||
public Map<String, SQLObject> getTableOptions() {
|
||||
return tableOptions;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public SQLSelect getQuery() {
|
||||
return select;
|
||||
|
||||
@@ -46,6 +46,7 @@ import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor, OracleASTVisitor {
|
||||
@@ -2275,6 +2276,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
println();
|
||||
x.getSelect().accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.alibaba.druid.sql.parser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
@@ -130,7 +131,7 @@ public class SQLCreateTableParser extends SQLDDLParser {
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
if (lexer.identifierEquals("INHERITS")) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.INHERITS)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.LPAREN);
|
||||
SQLName inherits = this.exprParser.name();
|
||||
@@ -145,6 +146,29 @@ public class SQLCreateTableParser extends SQLDDLParser {
|
||||
createTable.setSelect(select);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.WITH && JdbcConstants.POSTGRESQL.equals(dbType)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.LPAREN);
|
||||
|
||||
for (;;) {
|
||||
String name = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
accept(Token.EQ);
|
||||
SQLExpr value = this.exprParser.expr();
|
||||
value.setParent(createTable);
|
||||
|
||||
createTable.getTableOptions().put(name, value);
|
||||
|
||||
if (lexer.token == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
return createTable;
|
||||
}
|
||||
|
||||
|
||||
@@ -2431,10 +2431,14 @@ public class SQLExprParser extends SQLParser {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.COLLATE)) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token != Token.IDENTIFIER) {
|
||||
if (lexer.token == Token.LITERAL_ALIAS) {
|
||||
charType.setCollate(lexer.stringVal());
|
||||
} else if (lexer.token == Token.IDENTIFIER) {
|
||||
charType.setCollate(lexer.stringVal());
|
||||
} else {
|
||||
throw new ParserException();
|
||||
}
|
||||
charType.setCollate(lexer.stringVal());
|
||||
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
|
||||
@@ -2530,6 +2530,26 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
public boolean visit(SQLCreateTableStatement x) {
|
||||
printCreateTable(x, true);
|
||||
|
||||
Map<String, SQLObject> options = x.getTableOptions();
|
||||
if (options.size() > 0) {
|
||||
println();
|
||||
print0(ucase ? "WITH (" : "with (");
|
||||
int i = 0;
|
||||
for (Map.Entry<String, SQLObject> option : x.getTableOptions().entrySet()) {
|
||||
if (i > 0) {
|
||||
print0(", ");
|
||||
}
|
||||
String key = option.getKey();
|
||||
print0(key);
|
||||
|
||||
print0(" = ");
|
||||
|
||||
option.getValue().accept(this);
|
||||
++i;
|
||||
}
|
||||
print(')');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -620,5 +620,6 @@ public final class FnvHash {
|
||||
long SAVEPOINT = fnv1a_64_lower("SAVEPOINT");
|
||||
long RELEASE = fnv1a_64_lower("RELEASE");
|
||||
long MERGE = fnv1a_64_lower("MERGE");
|
||||
long INHERITS = fnv1a_64_lower("INHERITS");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user