From 81c0917b6d83047c5e6e3a4648e8be600824cc4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=93=81?= Date: Mon, 5 Oct 2020 18:42:53 +0800 Subject: [PATCH] improve postgres parser support, fix #3858 --- .../postgresql/parser/PGExprParser.java | 5 ++ .../druid/bvt/filter/wall/pg/PGWallTest2.java | 49 +++++++++++++++++ .../sql/postgresql/select/PGSelectTest84.java | 53 +++++++++++++++++++ .../sql/postgresql/select/PGSelectTest85.java | 51 ++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/wall/pg/PGWallTest2.java create mode 100644 src/test/java/com/alibaba/druid/bvt/sql/postgresql/select/PGSelectTest84.java create mode 100644 src/test/java/com/alibaba/druid/bvt/sql/postgresql/select/PGSelectTest85.java diff --git a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/parser/PGExprParser.java b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/parser/PGExprParser.java index 5f026e9b70..8a0e259e7f 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/parser/PGExprParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/parser/PGExprParser.java @@ -128,6 +128,11 @@ public class PGExprParser extends SQLExprParser { break; } return values; + } else if (lexer.token() == Token.WITH) { + SQLQueryExpr queryExpr = new SQLQueryExpr( + createSelectParser() + .select()); + return queryExpr; } return super.primary(); diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/pg/PGWallTest2.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/pg/PGWallTest2.java new file mode 100644 index 0000000000..4ffc6a09c4 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/pg/PGWallTest2.java @@ -0,0 +1,49 @@ +/* + * Copyright 1999-2018 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.filter.wall.pg; + +import com.alibaba.druid.wall.WallUtils; +import junit.framework.TestCase; +import org.junit.Assert; + +/** + * SQLServerWallTest + * + * @author RaymondXiu + * @version 1.0, 2012-3-18 + * @see + */ +public class PGWallTest2 extends TestCase { + public void test_wall() throws Exception { + assertTrue(WallUtils.isValidatePostgres(// + "select u.id, (\n" + + "\tWITH RECURSIVE users AS (\n" + + "\t\tselect id from t_user limit 1\n" + + "\t) select id from users\n" + + ") from t_user u \n" + + "limit 1;")); + } + + public void test_wall_2() throws Exception { + assertTrue(WallUtils.isValidatePostgres(// + "select 1 from t_user u\n" + + "join (\n" + + "\tWITH RECURSIVE users AS (\n" + + "\t\tselect id from t_user limit 1\n" + + "\t) select id from users\n" + + ") t on u.id = t.id;")); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/postgresql/select/PGSelectTest84.java b/src/test/java/com/alibaba/druid/bvt/sql/postgresql/select/PGSelectTest84.java new file mode 100644 index 0000000000..74a910bea5 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/postgresql/select/PGSelectTest84.java @@ -0,0 +1,53 @@ +package com.alibaba.druid.bvt.sql.postgresql.select; + +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.util.JdbcConstants; +import junit.framework.TestCase; + +import java.util.List; + +public class PGSelectTest84 + extends TestCase { + public void test_0() throws Exception { + String sql = "select u.id, (\n" + + "\tWITH RECURSIVE users AS (\n" + + "\t\tselect id from t_user limit 1\n" + + "\t) select id from users\n" + + ") from t_user u \n" + + "limit 1;"; + + final List statements = SQLUtils.parseStatements(sql, JdbcConstants.POSTGRESQL); + assertEquals(1, statements.size()); + final SQLStatement stmt = statements.get(0); + + System.out.println(stmt); + + assertEquals("SELECT u.id\n" + + "\t, (\n" + + "\t\tWITH RECURSIVE users AS (\n" + + "\t\t\t\tSELECT id\n" + + "\t\t\t\tFROM t_user\n" + + "\t\t\t\tLIMIT 1\n" + + "\t\t\t)\n" + + "\t\tSELECT id\n" + + "\t\tFROM users\n" + + "\t)\n" + + "FROM t_user u\n" + + "LIMIT 1;" + , stmt.toString()); + + assertEquals("select u.id\n" + + "\t, (\n" + + "\t\twith recursive users as (\n" + + "\t\t\t\tselect id\n" + + "\t\t\t\tfrom t_user\n" + + "\t\t\t\tlimit 1\n" + + "\t\t\t)\n" + + "\t\tselect id\n" + + "\t\tfrom users\n" + + "\t)\n" + + "from t_user u\n" + + "limit 1;", stmt.toLowerCaseString()); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/postgresql/select/PGSelectTest85.java b/src/test/java/com/alibaba/druid/bvt/sql/postgresql/select/PGSelectTest85.java new file mode 100644 index 0000000000..9d382708db --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/postgresql/select/PGSelectTest85.java @@ -0,0 +1,51 @@ +package com.alibaba.druid.bvt.sql.postgresql.select; + +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.util.JdbcConstants; +import junit.framework.TestCase; + +import java.util.List; + +public class PGSelectTest85 + extends TestCase { + public void test_0() throws Exception { + String sql = "select 1 from t_user u\n" + + "join (\n" + + "\tWITH RECURSIVE users AS (\n" + + "\t\tselect id from t_user limit 1\n" + + "\t) select id from users\n" + + ") t on u.id = t.id;"; + + final List statements = SQLUtils.parseStatements(sql, JdbcConstants.POSTGRESQL); + assertEquals(1, statements.size()); + final SQLStatement stmt = statements.get(0); + + System.out.println(stmt); + + assertEquals("SELECT 1\n" + + "FROM t_user u\n" + + "\tJOIN (\n" + + "\t\tWITH RECURSIVE users AS (\n" + + "\t\t\t\tSELECT id\n" + + "\t\t\t\tFROM t_user\n" + + "\t\t\t\tLIMIT 1\n" + + "\t\t\t)\n" + + "\t\tSELECT id\n" + + "\t\tFROM users\n" + + "\t) t ON u.id = t.id;" + , stmt.toString()); + + assertEquals("select 1\n" + + "from t_user u\n" + + "\tjoin (\n" + + "\t\twith recursive users as (\n" + + "\t\t\t\tselect id\n" + + "\t\t\t\tfrom t_user\n" + + "\t\t\t\tlimit 1\n" + + "\t\t\t)\n" + + "\t\tselect id\n" + + "\t\tfrom users\n" + + "\t) t on u.id = t.id;", stmt.toLowerCaseString()); + } +}