improve postgres parser support, fix #3858

This commit is contained in:
高铁
2020-10-05 18:42:53 +08:00
parent 7e066d1b72
commit 81c0917b6d
4 changed files with 158 additions and 0 deletions
@@ -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;"));
}
}
@@ -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<SQLStatement> 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());
}
}
@@ -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<SQLStatement> 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());
}
}