improved sql parser.

This commit is contained in:
wenshao
2017-06-28 15:34:43 +08:00
parent c64c40e81d
commit 2f6e406e33
19 changed files with 971 additions and 14 deletions
@@ -0,0 +1,54 @@
/*
* 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.mysql.createTable;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat.Column;
public class MySqlCreateTableTest78_json extends MysqlTest {
public void test_one() throws Exception {
String sql = "CREATE TABLE t1 (jdoc JSON);";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseCreateTable();
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
Column column = visitor.getColumn("t1", "jdoc");
assertNotNull(column);
assertEquals("JSON", column.getDataType());
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("CREATE TABLE t1 (\n" +
"\tjdoc JSON\n" +
")", output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("create table t1 (\n" +
"\tjdoc JSON\n" +
")", output);
}
}
}
@@ -0,0 +1,105 @@
/*
* 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.mysql.select;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import org.junit.Assert;
import java.util.List;
public class MySqlSelectTest_42_with_cte extends MysqlTest {
public void test_0() throws Exception {
String sql = "WITH\n" +
" cte1 AS (SELECT a, b FROM table1),\n" +
" cte2 AS (SELECT c, d FROM table2)\n" +
"SELECT b, d FROM cte1 JOIN cte2\n" +
"WHERE cte1.a = cte2.c;";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, true);
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
// Assert.assertEquals(1, visitor.getTables().size());
// Assert.assertEquals(1, visitor.getColumns().size());
// Assert.assertEquals(0, visitor.getConditions().size());
// Assert.assertEquals(0, visitor.getOrderByColumns().size());
{
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("WITH cte1 AS (\n" +
"\t\tSELECT a, b\n" +
"\t\tFROM table1\n" +
"\t), \n" +
"\tcte2 AS (\n" +
"\t\tSELECT c, d\n" +
"\t\tFROM table2\n" +
"\t)\n" +
"SELECT b, d\n" +
"FROM cte1\n" +
"\tJOIN cte2\n" +
"WHERE cte1.a = cte2.c;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("with cte1 as (\n" +
"\t\tselect a, b\n" +
"\t\tfrom table1\n" +
"\t), \n" +
"\tcte2 as (\n" +
"\t\tselect c, d\n" +
"\t\tfrom table2\n" +
"\t)\n" +
"select b, d\n" +
"from cte1\n" +
"\tjoin cte2\n" +
"where cte1.a = cte2.c;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
Assert.assertEquals("WITH cte1 AS (\n" +
"\t\tSELECT a, b\n" +
"\t\tFROM table1\n" +
"\t), \n" +
"\tcte2 AS (\n" +
"\t\tSELECT c, d\n" +
"\t\tFROM table2\n" +
"\t)\n" +
"SELECT b, d\n" +
"FROM cte1\n" +
"\tJOIN cte2\n" +
"WHERE cte1.a = cte2.c;", //
output);
}
}
}
@@ -0,0 +1,105 @@
/*
* 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.mysql.select;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import org.junit.Assert;
import java.util.List;
public class MySqlSelectTest_43_with_cte extends MysqlTest {
public void test_0() throws Exception {
String sql = "WITH\n" +
" cte1 AS (SELECT a, b FROM table1),\n" +
" cte2 AS (SELECT c, d FROM table2)\n" +
"SELECT b, d FROM cte1 JOIN cte2\n" +
"WHERE cte1.a = cte2.c;";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, true);
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
// Assert.assertEquals(1, visitor.getTables().size());
// Assert.assertEquals(1, visitor.getColumns().size());
// Assert.assertEquals(0, visitor.getConditions().size());
// Assert.assertEquals(0, visitor.getOrderByColumns().size());
{
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("WITH cte1 AS (\n" +
"\t\tSELECT a, b\n" +
"\t\tFROM table1\n" +
"\t), \n" +
"\tcte2 AS (\n" +
"\t\tSELECT c, d\n" +
"\t\tFROM table2\n" +
"\t)\n" +
"SELECT b, d\n" +
"FROM cte1\n" +
"\tJOIN cte2\n" +
"WHERE cte1.a = cte2.c;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("with cte1 as (\n" +
"\t\tselect a, b\n" +
"\t\tfrom table1\n" +
"\t), \n" +
"\tcte2 as (\n" +
"\t\tselect c, d\n" +
"\t\tfrom table2\n" +
"\t)\n" +
"select b, d\n" +
"from cte1\n" +
"\tjoin cte2\n" +
"where cte1.a = cte2.c;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
Assert.assertEquals("WITH cte1 AS (\n" +
"\t\tSELECT a, b\n" +
"\t\tFROM table1\n" +
"\t), \n" +
"\tcte2 AS (\n" +
"\t\tSELECT c, d\n" +
"\t\tFROM table2\n" +
"\t)\n" +
"SELECT b, d\n" +
"FROM cte1\n" +
"\tJOIN cte2\n" +
"WHERE cte1.a = cte2.c;", //
output);
}
}
}
@@ -0,0 +1,98 @@
/*
* 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.mysql.select;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import org.junit.Assert;
import java.util.List;
public class MySqlSelectTest_44_with_cte extends MysqlTest {
public void test_0() throws Exception {
String sql = "WITH RECURSIVE cte AS\n" +
"(\n" +
" SELECT 1 AS n, 1 AS p, -1 AS q\n" +
" UNION ALL\n" +
" SELECT n + 1, q * 2, p * 2 FROM cte WHERE n < 5\n" +
")\n" +
"SELECT * FROM cte;";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, true);
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
// Assert.assertEquals(1, visitor.getTables().size());
// Assert.assertEquals(1, visitor.getColumns().size());
// Assert.assertEquals(0, visitor.getConditions().size());
// Assert.assertEquals(0, visitor.getOrderByColumns().size());
{
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("WITH RECURSIVE cte AS (\n" +
"\t\tSELECT 1 AS n, 1 AS p, -1 AS q\n" +
"\t\tUNION ALL\n" +
"\t\tSELECT n + 1, q * 2, p * 2\n" +
"\t\tFROM cte\n" +
"\t\tWHERE n < 5\n" +
"\t)\n" +
"SELECT *\n" +
"FROM cte;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("with recursive cte as (\n" +
"\t\tselect 1 as n, 1 as p, -1 as q\n" +
"\t\tunion all\n" +
"\t\tselect n + 1, q * 2, p * 2\n" +
"\t\tfrom cte\n" +
"\t\twhere n < 5\n" +
"\t)\n" +
"select *\n" +
"from cte;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
Assert.assertEquals("WITH RECURSIVE cte AS (\n" +
"\t\tSELECT ? AS n, ? AS p, ? AS q\n" +
"\t\tUNION ALL\n" +
"\t\tSELECT n + ?, q * ?, p * ?\n" +
"\t\tFROM cte\n" +
"\t\tWHERE n < ?\n" +
"\t)\n" +
"SELECT *\n" +
"FROM cte;", //
output);
}
}
}
@@ -0,0 +1,80 @@
/*
* 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.mysql.select;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import org.junit.Assert;
import java.util.List;
public class MySqlSelectTest_45_subquery_scalar_operand extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT (SELECT s1 FROM t2) FROM t1;";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, true);
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
// Assert.assertEquals(1, visitor.getTables().size());
// Assert.assertEquals(1, visitor.getColumns().size());
// Assert.assertEquals(0, visitor.getConditions().size());
// Assert.assertEquals(0, visitor.getOrderByColumns().size());
{
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("SELECT (\n" +
"\t\tSELECT s1\n" +
"\t\tFROM t2\n" +
"\t)\n" +
"FROM t1;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select (\n" +
"\t\tselect s1\n" +
"\t\tfrom t2\n" +
"\t)\n" +
"from t1;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
Assert.assertEquals("SELECT (\n" +
"\t\tSELECT s1\n" +
"\t\tFROM t2\n" +
"\t)\n" +
"FROM t1;", //
output);
}
}
}