mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for sql parser for postgresql, for issue 1204, https://github.com/alibaba/druid/issues/1204
This commit is contained in:
@@ -65,6 +65,10 @@ public class PGExprParser extends SQLExprParser {
|
||||
}
|
||||
return super.parseDataType();
|
||||
}
|
||||
|
||||
public PGSelectParser createSelectParser() {
|
||||
return new PGSelectParser(this);
|
||||
}
|
||||
|
||||
public SQLExpr primary() {
|
||||
if (lexer.token() == Token.ARRAY) {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 1999-2101 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 java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.OracleTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlParameterizedOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.util.Utils;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class PGResourceTest extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
// 13
|
||||
// exec_test("bvt/parser/oracle-56.txt");
|
||||
for (int i = 0; i < 1; ++i) {
|
||||
exec_test("bvt/parser/pg-" + i + ".txt");
|
||||
}
|
||||
}
|
||||
|
||||
public void exec_test(String resource) throws Exception {
|
||||
System.out.println(resource);
|
||||
InputStream is = null;
|
||||
|
||||
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
|
||||
Reader reader = new InputStreamReader(is, "UTF-8");
|
||||
String input = Utils.read(reader);
|
||||
JdbcUtils.close(reader);
|
||||
String[] items = input.split("---------------------------");
|
||||
String sql = items[0].trim();
|
||||
String expect = items[1].trim();
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
|
||||
// Assert.assertEquals(1, statementList.size());
|
||||
|
||||
System.out.println(sql);
|
||||
|
||||
print(statementList);
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
|
||||
for (int i = 0, size = statementList.size(); i < size; ++i) {
|
||||
SQLStatement statement = statementList.get(i);
|
||||
statement.accept(visitor);
|
||||
}
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
void mergValidate(String sql, String expect) {
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
MySqlParameterizedOutputVisitor visitor = new MySqlParameterizedOutputVisitor(out);
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println(out.toString());
|
||||
|
||||
Assert.assertEquals(expect, out.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 1999-2101 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 java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.PGTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
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;
|
||||
|
||||
public class PGSelectTest25 extends PGTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "select COALESCE((SELECT project_deduct_mandays from mytable_01 limit 1)) from t";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals("SELECT COALESCE(("
|
||||
+ "\n\t\tSELECT project_deduct_mandays"
|
||||
+ "\n\t\tFROM mytable_01"
|
||||
+ "\n\t\tLIMIT 1"
|
||||
+ "\n\t\t))"
|
||||
+ "\nFROM t", SQLUtils.toPGString(stmt));
|
||||
|
||||
Assert.assertEquals("select COALESCE(("
|
||||
+ "\n\t\tselect project_deduct_mandays"
|
||||
+ "\n\t\tfrom mytable_01"
|
||||
+ "\n\t\tlimit 1"
|
||||
+ "\n\t\t))"
|
||||
+ "\nfrom t", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
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.assertEquals(1, visitor.getColumns().size());
|
||||
Assert.assertEquals(2, visitor.getTables().size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user