improved sql parser for postgresql, support create table inherits

This commit is contained in:
wenshao
2014-11-30 01:35:00 +08:00
parent 1fd34a9294
commit cadce912f6
6 changed files with 102 additions and 21 deletions
@@ -30,12 +30,15 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
protected List<SQLTableElement> tableElementList = new ArrayList<SQLTableElement>();
// for postgresql
private SQLExprTableSource inherits;
public SQLCreateTableStatement(){
}
public SQLCreateTableStatement(String dbType){
super (dbType);
super(dbType);
}
public SQLName getName() {
@@ -76,7 +79,7 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
public List<SQLTableElement> getTableElementList() {
return tableElementList;
}
public boolean isIfNotExiists() {
return ifNotExiists;
}
@@ -85,26 +88,15 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
this.ifNotExiists = ifNotExiists;
}
@Override
public void output(StringBuffer buf) {
buf.append("CREATE TABLE ");
if (Type.GLOBAL_TEMPORARY.equals(this.type)) {
buf.append("GLOBAL TEMPORARY ");
} else if (Type.LOCAL_TEMPORARY.equals(this.type)) {
buf.append("LOCAL TEMPORARY ");
}
public SQLExprTableSource getInherits() {
return inherits;
}
this.tableSource.output(buf);
buf.append(" ");
buf.append("(");
for (int i = 0, size = tableElementList.size(); i < size; ++i) {
if (i != 0) {
buf.append(", ");
}
tableElementList.get(i).output(buf);
public void setInherits(SQLExprTableSource inherits) {
if (inherits != null) {
inherits.setParent(this);
}
buf.append(")");
this.inherits = inherits;
}
@Override
@@ -112,6 +104,7 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
if (visitor.visit(this)) {
this.acceptChild(visitor, tableSource);
this.acceptChild(visitor, tableElementList);
this.acceptChild(visitor, inherits);
}
visitor.endVisit(this);
}
@@ -0,0 +1,14 @@
package com.alibaba.druid.sql.dialect.postgresql.ast.stmt;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.util.JdbcConstants;
public class PGCreateTableStatement extends SQLCreateTableStatement {
private SQLExprTableSource inherits;
public PGCreateTableStatement(){
super(JdbcConstants.POSTGRESQL);
}
}
@@ -15,9 +15,11 @@
*/
package com.alibaba.druid.sql.parser;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLConstraint;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
public class SQLCreateTableParser extends SQLDDLParser {
@@ -109,6 +111,13 @@ public class SQLCreateTableParser extends SQLDDLParser {
accept(Token.RPAREN);
if (identifierEquals("INHERITS")) {
lexer.nextToken();
accept(Token.LPAREN);
SQLName inherits = this.exprParser.name();
createTable.setInherits(new SQLExprTableSource(inherits));
accept(Token.RPAREN);
}
}
return createTable;
@@ -1058,6 +1058,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
println();
print(")");
}
if (x.getInherits() != null) {
print(" INHERITS (");
x.getInherits().accept(this);
print(")");
}
return false;
}
@@ -1107,6 +1107,10 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
accept(x.getTableElementList());
restoreCurrentTable(x);
if (x.getInherits() != null) {
x.getInherits().accept(this);
}
return false;
}