improved postgresql parser. bug fixed for issue #2060

This commit is contained in:
wenshao
2017-10-23 19:38:40 +08:00
parent 42efb320ef
commit 9256b904db
8 changed files with 329 additions and 3 deletions
@@ -22,6 +22,7 @@ import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLCaseExpr extends SQLExprImpl implements SQLReplaceable, Serializable {
@@ -187,6 +188,10 @@ public class SQLCaseExpr extends SQLExprImpl implements SQLReplaceable, Serializ
return x;
}
public void output(StringBuffer buf) {
new SQLASTOutputVisitor(buf).visit(this);
}
@Override
public boolean replace(SQLExpr expr, SQLExpr target) {
if (valueExpr == expr) {
@@ -19,7 +19,9 @@ import java.util.ArrayList;
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.SQLInsertStatement;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -28,9 +30,15 @@ import com.alibaba.druid.util.JdbcConstants;
public class PGInsertStatement extends SQLInsertStatement implements PGSQLStatement {
private List<ValuesClause> valuesList = new ArrayList<ValuesClause>();
private SQLExpr returning;
private boolean defaultValues = false;
private List<ValuesClause> valuesList = new ArrayList<ValuesClause>();
private SQLExpr returning;
private boolean defaultValues = false;
private List<SQLExpr> onConflictTarget;
private SQLName onConflictConstraint;
private SQLExpr onConflictWhere;
private boolean onConflictDoNothing;
private List<SQLUpdateSetItem> onConflictUpdateSetItems;
public PGInsertStatement() {
dbType = JdbcConstants.POSTGRESQL;
@@ -113,4 +121,55 @@ public class PGInsertStatement extends SQLInsertStatement implements PGSQLStatem
cloneTo(x);
return x;
}
public List<SQLExpr> getOnConflictTarget() {
return onConflictTarget;
}
public void setOnConflictTarget(List<SQLExpr> onConflictTarget) {
this.onConflictTarget = onConflictTarget;
}
public boolean isOnConflictDoNothing() {
return onConflictDoNothing;
}
public void setOnConflictDoNothing(boolean onConflictDoNothing) {
this.onConflictDoNothing = onConflictDoNothing;
}
public List<SQLUpdateSetItem> getOnConflictUpdateSetItems() {
return onConflictUpdateSetItems;
}
public void addConflicUpdateItem(SQLUpdateSetItem item) {
if (onConflictUpdateSetItems == null) {
onConflictUpdateSetItems = new ArrayList<SQLUpdateSetItem>();
}
item.setParent(this);
onConflictUpdateSetItems.add(item);
}
public SQLName getOnConflictConstraint() {
return onConflictConstraint;
}
public void setOnConflictConstraint(SQLName x) {
if (x != null) {
x.setParent(this);
}
this.onConflictConstraint = x;
}
public SQLExpr getOnConflictWhere() {
return onConflictWhere;
}
public void setOnConflictWhere(SQLExpr x) {
if (x != null) {
x.setParent(this);
}
this.onConflictWhere = x;
}
}
@@ -150,6 +150,57 @@ public class PGSQLStatementParser extends SQLStatementParser {
stmt.setQuery(queryExpr.getSubQuery());
}
if (lexer.token() == Token.ON) {
lexer.nextToken();
if (lexer.identifierEquals(FnvHash.Constants.CONFLICT)) {
lexer.nextToken();
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
List<SQLExpr> onConflictTarget = new ArrayList<SQLExpr>();
this.exprParser.exprList(onConflictTarget, stmt);
stmt.setOnConflictTarget(onConflictTarget);
accept(Token.RPAREN);
}
if (lexer.token() == Token.ON) {
lexer.nextToken();
accept(Token.CONSTRAINT);
SQLName constraintName = this.exprParser.name();
stmt.setOnConflictConstraint(constraintName);
}
if (lexer.token() == Token.WHERE) {
lexer.nextToken();
SQLExpr where = this.exprParser.expr();
stmt.setOnConflictWhere(where);
}
if (lexer.token() == Token.DO) {
lexer.nextToken();
if (lexer.identifierEquals(FnvHash.Constants.NOTHING)) {
lexer.nextToken();
stmt.setOnConflictDoNothing(true);
} else {
accept(Token.UPDATE);
accept(Token.SET);
for (;;) {
SQLUpdateSetItem item = this.exprParser.parseUpdateSetItem();
stmt.addConflicUpdateItem(item);
if (lexer.token() != Token.COMMA) {
break;
}
lexer.nextToken();
}
}
}
}
}
if (lexer.token() == Token.RETURNING) {
lexer.nextToken();
SQLExpr returning = this.exprParser.expr();
@@ -305,6 +305,42 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
}
}
List<SQLExpr> onConflictTarget = x.getOnConflictTarget();
List<SQLUpdateSetItem> onConflictUpdateSetItems = x.getOnConflictUpdateSetItems();
boolean onConflictDoNothing = x.isOnConflictDoNothing();
if (onConflictDoNothing
|| (onConflictTarget != null && onConflictTarget.size() > 0)
|| (onConflictUpdateSetItems != null && onConflictUpdateSetItems.size() > 0)) {
println();
print0(ucase ? "ON CONFLICT" : "on conflict");
if ((onConflictTarget != null && onConflictTarget.size() > 0)) {
print0(" (");
printAndAccept(onConflictTarget, ", ");
print(')');
}
SQLName onConflictConstraint = x.getOnConflictConstraint();
if (onConflictConstraint != null) {
print0(ucase ? " ON CONSTRAINT " : " on constraint ");
printExpr(onConflictConstraint);
}
SQLExpr onConflictWhere = x.getOnConflictWhere();
if (onConflictWhere != null) {
print0(ucase ? " WHERE " : " where ");
printExpr(onConflictWhere);
}
if (onConflictDoNothing) {
print0(ucase ? " DO NOTHING" : " do nothing");
} else if ((onConflictUpdateSetItems != null && onConflictUpdateSetItems.size() > 0)) {
print0(ucase ? " UPDATE SET " : " update set ");
printAndAccept(onConflictUpdateSetItems, ", ");
}
}
if (x.getReturning() != null) {
println();
print0(ucase ? "RETURNING " : "returning ");
@@ -568,6 +568,8 @@ public final class FnvHash {
long ENDS = fnv1a_64_lower("ENDS");
long BINARY = fnv1a_64_lower("BINARY");
long ISOPEN = fnv1a_64_lower("ISOPEN");
long CONFLICT = fnv1a_64_lower("CONFLICT");
long NOTHING = fnv1a_64_lower("NOTHING");
long RS = fnv1a_64_lower("RS");
long RR = fnv1a_64_lower("RR");
@@ -0,0 +1,57 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.postgresql;
import com.alibaba.druid.sql.PGTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import org.junit.Assert;
import java.util.List;
public class PGInsertTest7 extends PGTest {
public void test_0() throws Exception {
String sql = "insert into test(k,v) values (1,2) ON CONFLICT (k) DO UPDATE SET v = EXCLUDED.v; ";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
// print(statementList);
Assert.assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("test")));
Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("test", "k")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("test", "v")));
assertEquals("INSERT INTO test (k, v)\n" +
"VALUES (1, 2)\n" +
"ON CONFLICT (k) UPDATE SET v = EXCLUDED.v;", stmt.toString());
}
}
@@ -0,0 +1,58 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.postgresql;
import com.alibaba.druid.sql.PGTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import org.junit.Assert;
import java.util.List;
public class PGInsertTest8 extends PGTest {
public void test_0() throws Exception {
String sql = "INSERT INTO distributors (did, dname) VALUES (9, 'Antwerp Design')\n" +
" ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING;";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
// print(statementList);
Assert.assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("distributors")));
Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("distributors", "did")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("distributors", "dname")));
assertEquals("INSERT INTO distributors (did, dname)\n" +
"VALUES (9, 'Antwerp Design')\n" +
"ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING;", stmt.toString());
}
}
@@ -0,0 +1,58 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.postgresql;
import com.alibaba.druid.sql.PGTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import org.junit.Assert;
import java.util.List;
public class PGInsertTest9 extends PGTest {
public void test_0() throws Exception {
String sql = "INSERT INTO distributors (did, dname) VALUES (10, 'Conrad International')\n" +
" ON CONFLICT (did) WHERE is_active DO NOTHING;";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
// print(statementList);
Assert.assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("distributors")));
Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("distributors", "did")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("distributors", "dname")));
assertEquals("INSERT INTO distributors (did, dname)\n" +
"VALUES (10, 'Conrad International')\n" +
"ON CONFLICT (did) WHERE is_active DO NOTHING;", stmt.toString());
}
}