improved sql parser.

This commit is contained in:
wenshao
2017-06-29 15:19:57 +08:00
parent 2f6e406e33
commit bdbc39ec3c
39 changed files with 963 additions and 185 deletions
@@ -62,17 +62,16 @@ public class MySqlCreateTableTest42 extends MysqlTest {
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("rc")));
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE TABLE rc (" + //
"\n\ta INT NOT NULL," + //
"\n\tb INT NOT NULL" + //
"\n)"
+ "\nPARTITION BY RANGE COLUMNS (a, b)"
+ "\n(" + //
"\n\tPARTITION p0 VALUES LESS THAN (10, 5)," + //
"\n\tPARTITION p1 VALUES LESS THAN (20, 10)," + //
"\n\tPARTITION p2 VALUES LESS THAN (MAXVALUE, 15)," + //
"\n\tPARTITION p3 VALUES LESS THAN (MAXVALUE, MAXVALUE)" + //
"\n);", output);
Assert.assertEquals("CREATE TABLE rc (\n" +
"\ta INT NOT NULL,\n" +
"\tb INT NOT NULL\n" +
")\n" +
"PARTITION BY RANGE COLUMNS (a, b) (\n" +
"\tPARTITION p0 VALUES LESS THAN (10, 5),\n" +
"\tPARTITION p1 VALUES LESS THAN (20, 10),\n" +
"\tPARTITION p2 VALUES LESS THAN (MAXVALUE, 15),\n" +
"\tPARTITION p3 VALUES LESS THAN (MAXVALUE, MAXVALUE)\n" +
");", output);
}
}
@@ -47,8 +47,7 @@ public class MySqlCreateTableTest67 extends MysqlTest {
"\ta INT NOT NULL,\n" +
"\tPRIMARY KEY (a)\n" +
") ENGINE = InnoDB TABLESPACE ts1\n" +
"PARTITION BY RANGE (a) PARTITIONS 3\n" +
"(\n" +
"PARTITION BY RANGE (a) PARTITIONS 3 (\n" +
"\tPARTITION P1 VALUES LESS THAN (2),\n" +
"\tPARTITION P2 VALUES LESS THAN (4)\n" +
"\t\tTABLESPACE ts2,\n" +
@@ -62,8 +61,7 @@ public class MySqlCreateTableTest67 extends MysqlTest {
"\ta INT not null,\n" +
"\tprimary key (a)\n" +
") engine = InnoDB tablespace ts1\n" +
"partition by range (a) partitions 3\n" +
"(\n" +
"partition by range (a) partitions 3 (\n" +
"\tpartition P1 values less than (2),\n" +
"\tpartition P2 values less than (4)\n" +
"\t\ttablespace ts2,\n" +
@@ -53,8 +53,7 @@ public class MySqlCreateTableTest68 extends MysqlTest {
+ "\n\tyear_col INT,"
+ "\n\tsome_data INT"
+ "\n)"
+ "\nPARTITION BY RANGE (year_col)"
+ "\n("
+ "\nPARTITION BY RANGE (year_col) ("
+ "\n\tPARTITION p0 VALUES LESS THAN (1991),"
+ "\n\tPARTITION p1 VALUES LESS THAN (1995),"
+ "\n\tPARTITION p2 VALUES LESS THAN (1999),"
@@ -70,8 +69,7 @@ public class MySqlCreateTableTest68 extends MysqlTest {
+ "\n\tyear_col INT,"
+ "\n\tsome_data INT"
+ "\n)"
+ "\npartition by range (year_col)"
+ "\n("
+ "\npartition by range (year_col) ("
+ "\n\tpartition p0 values less than (1991),"
+ "\n\tpartition p1 values less than (1995),"
+ "\n\tpartition p2 values less than (1999),"
@@ -0,0 +1,69 @@
package com.alibaba.druid.bvt.sql.mysql.param;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import junit.framework.TestCase;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by wenshao on 16/8/23.
*/
public class MySqlParameterizedOutputVisitorTest_43 extends TestCase {
public void test_for_parameterize() throws Exception {
final String dbType = JdbcConstants.MYSQL;
String sql = "UPDATE `feel_07`.feed_item_receive SET `attributes` = ?, `gmt_modified` = ?, `lock_version` = ? WHERE `feed_id` = ?";
String params = "[\"enableTime:1498682416713,src:top,importFrom:0\",\"2017-06-29 04:40:20\",1,313825887478L]";
String table = "[\"`feel_07`.`feed_item_receive_0502`\"]";
String restoredSql = restore(sql,table,params);
assertEquals("UPDATE `feel_07`.`feed_item_receive_0502`\n" +
"SET `attributes` = 'enableTime:1498682416713,src:top,importFrom:0', `gmt_modified` = '2017-06-29 04:40:20', `lock_version` = 1\n" +
"WHERE `feed_id` = 313825887478", restoredSql);
}
public static String restore(String sql, String table, String params/*JSONArray paramsArray, JSONArray destArray*/) {
JSONArray destArray = JSON.parseArray(table.replaceAll("''", "'"));
params = StringUtils.replace(params.replaceAll("''", "'"), "\\\"","\"");
JSONArray paramsArray = JSON.parseArray(params);
String dbType = JdbcConstants.MYSQL;
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
SQLStatement stmt = stmtList.get(0);
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, dbType);
List<Object> paramsList = Lists.newArrayList(paramsArray);
visitor.setParameters(paramsList);
SchemaStatVisitor schemaStatVisitor = new MySqlSchemaStatVisitor();
stmt.accept(schemaStatVisitor);
JSONArray srcArray = new JSONArray();
for (Map.Entry<TableStat.Name, TableStat> entry : schemaStatVisitor.getTables().entrySet()) {
srcArray.add(entry.getKey().getName());
}
for (int i = 0; i < srcArray.size(); i++) {
visitor.addTableMapping(srcArray.getString(i), destArray.getString(i));
}
stmt.accept(visitor);
return out.toString();
}
}
@@ -0,0 +1,77 @@
/*
* 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_46_group_concat extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT student_name,\n" +
" GROUP_CONCAT(test_score)\n" +
" FROM student\n" +
" GROUP BY student_name;";
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 student_name, GROUP_CONCAT(test_score)\n" +
"FROM student\n" +
"GROUP BY student_name;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select student_name, group_concat(test_score)\n" +
"from student\n" +
"group by student_name;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
Assert.assertEquals("SELECT student_name, GROUP_CONCAT(test_score)\n" +
"FROM student\n" +
"GROUP BY student_name;", //
output);
}
}
}
@@ -0,0 +1,78 @@
/*
* 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_47_group_concat extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT student_name,\n" +
" GROUP_CONCAT(DISTINCT test_score\n" +
" ORDER BY test_score DESC SEPARATOR ' ')\n" +
" FROM student\n" +
" GROUP BY student_name;";
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 student_name, GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ')\n" +
"FROM student\n" +
"GROUP BY student_name;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select student_name, group_concat(DISTINCT test_score order by test_score desc separator ' ')\n" +
"from student\n" +
"group by student_name;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
Assert.assertEquals("SELECT student_name, GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ')\n" +
"FROM student\n" +
"GROUP BY student_name;", //
output);
}
}
}