添加postgresql的begin,commit,set命令解析

添加PGStatement的测试代码,修复PGStatement解析bug;修复pg无法识别table别名问题

将PG的关键字移入TOKEN中

start transaction的visit优化
This commit is contained in:
Terry Zhu
2017-05-17 16:39:44 +08:00
parent ed57e419c4
commit b531cb8c24
19 changed files with 603 additions and 63 deletions
@@ -0,0 +1,32 @@
/*
* 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.dialect.postgresql.ast.stmt.PGBeginStatement;
public class PGBeginTest extends PGTest {
public void testBegin() throws Exception {
String sql = "begin;";
String expected = "BEGIN";
testParseSql(sql, expected, expected, PGBeginStatement.class);
sql = "start transaction;";
expected = "START TRANSACTION";
testParseSql(sql, expected, expected, PGBeginStatement.class);
}
}
@@ -0,0 +1,27 @@
/*
* 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.dialect.postgresql.ast.stmt.PGCommitStatement;
public class PGCommitTest extends PGTest {
public void testCommit() throws Exception {
String sql = "commit;";
String expected = "COMMIT";
testParseSql(sql, expected, expected, PGCommitStatement.class);
}
}
@@ -0,0 +1,29 @@
/*
* 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.dialect.postgresql.ast.stmt.PGSelectStatement;
public class PGFromAsTest extends PGTest {
public void testFromAs() throws Exception {
String sql = "SELECT Count(*) FROM tb_abc AS t1 WHERE ((t1.a_id = 'global_a_id') AND (t1.owner = 'global_bc'));";
String expectedSql = "SELECT COUNT(*)\nFROM tb_abc t1\nWHERE t1.a_id = 'global_a_id'\n\tAND t1.owner = 'global_bc'";
String expectedPattern = "SELECT COUNT(*)\nFROM tb_abc t1\nWHERE t1.a_id = ?\n\tAND t1.owner = ?";
testParseSql(sql, expectedSql, expectedPattern, PGSelectStatement.class);
}
}
@@ -0,0 +1,50 @@
/*
* 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.dialect.postgresql.ast.stmt.PGSetStatement;
public class PGSetTest3 extends PGTest {
public void testSet() throws Exception {
Class<?> type = PGSetStatement.class;
String sql = "SET TIME ZONE 'Europe/Rome';";
String expectedSql = "SET TIME ZONE 'Europe/Rome'";
String expectedPattern = "SET TIME ZONE ?";
testParseSql(sql, expectedSql, expectedPattern, type);
sql = "SET configuration_parameter TO DEFAULT;";
expectedSql = "SET configuration_parameter TO DEFAULT";
testParseSql(sql, expectedSql, expectedSql, type);
sql = "SET search_path TO my_schema, public;";
expectedSql = "SET search_path TO my_schema, public";
testParseSql(sql, expectedSql, expectedSql, type);
sql = "SET search_path = my_schema, public;";
expectedSql = "SET search_path TO my_schema, public";
testParseSql(sql, expectedSql, expectedSql, type);
sql = "SET a=1";
expectedSql = "SET a TO 1";
testParseSql(sql, expectedSql, expectedSql, type);
sql = "SET a=1,2";
expectedSql = "SET a TO 1, 2";
testParseSql(sql, expectedSql, expectedSql, type);
}
}
@@ -0,0 +1,31 @@
/*
* 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.dialect.postgresql.ast.stmt.PGShowStatement;
public class PGShowTest1 extends PGTest {
public void testShowAll() throws Exception {
String sql = "show all;";
String expected = "SHOW ALL";
testParseSql(sql, expected, expected, PGShowStatement.class);
sql = "show transaction_read_only;";
expected = "SHOW transaction_read_only";
testParseSql(sql, expected, expected, PGShowStatement.class);
}
}
@@ -33,13 +33,13 @@ public class PGSelectTest42 extends PGTest {
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
Assert.assertEquals("UPDATE sys_account\n" +
Assert.assertEquals("UPDATE sys_account a\n" +
"SET online = 2\n" +
"FROM auto_handler_online o\n" +
"WHERE a.id = o.account_id\n" +
"\tAND a.online != 2", SQLUtils.toPGString(stmt));
Assert.assertEquals("update sys_account\n" +
Assert.assertEquals("update sys_account a\n" +
"set online = 2\n" +
"from auto_handler_online o\n" +
"where a.id = o.account_id\n" +
@@ -17,10 +17,12 @@ package com.alibaba.druid.sql;
import java.util.List;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import junit.framework.TestCase;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor;
import org.junit.Assert;
public class PGTest extends TestCase {
protected String output(List<SQLStatement> stmtList) {
@@ -42,4 +44,18 @@ public class PGTest extends TestCase {
}
System.out.println(text);
}
protected void testParseSql(String sql, String expectedSql, String expectedPattern, Class<?> type) {
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statement = statementList.get(0);
Assert.assertEquals(1, statementList.size());
Assert.assertTrue(type.isAssignableFrom(statement.getClass()));
StringBuilder sb = new StringBuilder();
PGOutputVisitor visitor = new PGOutputVisitor(sb);
statement.accept(visitor);
Assert.assertEquals(expectedSql, sb.toString());
}
}