bug fixed for mysql parser.

This commit is contained in:
wenshao
2018-01-17 21:26:39 +08:00
parent c39f787c10
commit f50c14d9ea
15 changed files with 277 additions and 26 deletions
@@ -821,7 +821,13 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
accept(Token.LPAREN);
for (;;) {
SQLExpr expr = this.exprParser.expr();
SQLExpr expr;
if (lexer.token() == Token.LITERAL_ALIAS) {
expr = this.exprParser.name();
expr = this.exprParser.primaryRest(expr);
} else {
expr = this.exprParser.expr();
}
if (lexer.token() == Token.ASC) {
lexer.nextToken();
expr = new MySqlOrderingExpr(expr, SQLOrderingSpecification.ASC);
@@ -705,7 +705,13 @@ public class MySqlExprParser extends SQLExprParser {
accept(Token.LPAREN);
for (;;) {
primaryKey.addColumn(this.expr());
SQLExpr expr;
if (lexer.token() == Token.LITERAL_ALIAS) {
expr = this.name();
} else {
expr = this.expr();
}
primaryKey.addColumn(expr);
if (!(lexer.token() == (Token.COMMA))) {
break;
} else {
@@ -965,4 +971,9 @@ public class MySqlExprParser extends SQLExprParser {
}
return partitionDef;
}
protected SQLExpr parseAliasExpr(String alias) {
String chars = alias.substring(1, alias.length() - 1);
return new SQLCharExpr(chars);
}
}
@@ -3651,7 +3651,7 @@ public class MySqlStatementParser extends SQLStatementParser {
}
} else if (lexer.identifierEquals("RENAME")) {
lexer.nextToken();
if (lexer.token() == Token.INDEX) {
lexer.nextToken();
SQLName name = this.exprParser.name();
@@ -31,10 +31,10 @@ public class Bug_for_ruiyi extends TestCase {
+ "\\\"title\\\":\\\"Please contact your card issuer to double confirm.\\\","
+ "\\\"type\\\":\\\"END\\\"," + "\\\"currentLevel\\\":3}]}]}\",607)";
String expected = "INSERT INTO icshall_guide (id, gmt_create, gmt_modified, subject, content"
+ "\n\t, cat_id)"
+ "\nVALUES (8, now(), now(), \"Why my payment is deducted incorrectly?/ Why my payment is deducted twice?\", '{\"id\":1,\"title\":\"Have you contacted your card issuer to double check instead of only checking online?\",\"type\":\"START\",\"currentLevel\":1,\"name\":\"name1\",\"values\":[{\"id\":2,\"title\":\"Yes\",\"type\":\"MIDWAY\",\"currentLevel\":2,\"value\":1,\"childs\":[{\"id\":3,\"title\":\"If it is deducted twice, please contact the online service with the official bank statement.\",\"type\":\"END\",\"currentLevel\":3}]},{\"id\":4,\"title\":\"No\",\"type\":\"MIDWAY\",\"currentLevel\":2,\"value\":1,\"childs\":[{\"id\":5,\"title\":\"Please contact your card issuer to double confirm.\",\"type\":\"END\",\"currentLevel\":3}]}]}'"
+ "\n\t, 607);\n";
String expected = "INSERT INTO icshall_guide (id, gmt_create, gmt_modified, subject, content\n" +
"\t, cat_id)\n" +
"VALUES (8, now(), now(), 'Why my payment is deducted incorrectly?/ Why my payment is deducted twice?', '{\"id\":1,\"title\":\"Have you contacted your card issuer to double check instead of only checking online?\",\"type\":\"START\",\"currentLevel\":1,\"name\":\"name1\",\"values\":[{\"id\":2,\"title\":\"Yes\",\"type\":\"MIDWAY\",\"currentLevel\":2,\"value\":1,\"childs\":[{\"id\":3,\"title\":\"If it is deducted twice, please contact the online service with the official bank statement.\",\"type\":\"END\",\"currentLevel\":3}]},{\"id\":4,\"title\":\"No\",\"type\":\"MIDWAY\",\"currentLevel\":2,\"value\":1,\"childs\":[{\"id\":5,\"title\":\"Please contact your card issuer to double confirm.\",\"type\":\"END\",\"currentLevel\":3}]}]}'\n" +
"\t, 607);\n";
StringBuilder out = new StringBuilder();
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
@@ -46,8 +46,8 @@ public class TenantInsertTest extends TestCase {
public void testMySql3() throws Exception {
String insert_sql = "INSERT INTO orders (ID, NAME) VALUES (1, \"KIKI\")";
String expect_sql = "INSERT INTO orders (ID, NAME, tenant)" + //
"\nVALUES (1, \"KIKI\", 123)";
String expect_sql = "INSERT INTO orders (ID, NAME, tenant)\n" +
"VALUES (1, 'KIKI', 123)";
{
MySqlWallProvider provider = new MySqlWallProvider(config_callback);
WallCheckResult checkResult = provider.check(insert_sql);
@@ -71,9 +71,9 @@ public class TenantInsertTest extends TestCase {
public void testMySql4() throws Exception {
String insert_sql = "INSERT INTO orders (ID, NAME) VALUES (1, \"KIKI\"), (1, \"CICI\")";
String expect_sql = "INSERT INTO orders (ID, NAME, tenant)" + //
"\nVALUES (1, \"KIKI\", 123)," + //
"\n\t(1, \"CICI\", 123)";
String expect_sql = "INSERT INTO orders (ID, NAME, tenant)\n" +
"VALUES (1, 'KIKI', 123),\n" +
"\t(1, 'CICI', 123)";
{
MySqlWallProvider provider = new MySqlWallProvider(config_callback);
@@ -46,8 +46,27 @@ public class MergeStatFilterTest_tddl extends TestCase {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xx");
dataSource.setFilters("mergeStat");
dataSource.setDbType("mysql");
// {
// Connection conn = dataSource.getConnection();
// {
// Statement stmt = conn.createStatement();
// stmt.execute("select 1");
// }
// conn.close();
// System.out.println(conn.getClass().getName());
// }
dataSource.setFilters("mergeStat");
Connection conn = dataSource.getConnection();
{
Statement stmt = conn.createStatement();
stmt.execute("select 1");
System.out.println(stmt.getClass().getName());
}
conn.close();
System.out.println(conn.getClass().getName());
}
protected void tearDown() throws Exception {
@@ -69,7 +88,7 @@ public class MergeStatFilterTest_tddl extends TestCase {
conn.close();
}
Assert.assertEquals(1, dataSource.getDataSourceStat().getSqlStatMap().size());
Assert.assertEquals(2, dataSource.getDataSourceStat().getSqlStatMap().size());
}
@@ -43,7 +43,7 @@ public class MySqlCreateTriggerTest3 extends MysqlTest {
"\tFOR EACH ROW\n" +
"BEGIN\n" +
"\tINSERT INTO log_table\n" +
"\tVALUES (\"delete row from test.t1\");\n" +
"\tVALUES ('delete row from test.t1');\n" +
"\tINSERT INTO t4\n" +
"\tVALUES (old.col1, old.col1 + 5, old.col1 + 7);\n" +
"END;", stmt.toString());
@@ -39,7 +39,7 @@ public class MySqlCreateTriggerTest4 extends MysqlTest {
"\tON test.t2\n" +
"\tFOR EACH ROW\n" +
"INSERT INTO log_table\n" +
"VALUES (concat(\"inserted into table test.t2 values: (1c, _) = (\", CAST(NEW.col1 AS char(100)), \", \", convert(new.`_`, char(100)), \")\"));", stmt.toString());
"VALUES (concat('inserted into table test.t2 values: (1c, _) = (', CAST(NEW.col1 AS char(100)), ', ', convert(new.`_`, char(100)), ')'));", stmt.toString());
}
@@ -55,7 +55,7 @@ public class MySqlCreateViewTest1 extends MysqlTest {
"\t\t\tFROM actvty_audit\n" +
"\t\t\tWHERE enroll_id = a.enroll_id\n" +
"\t\t\t\tAND rankjurisdiction = 1\n" +
"\t\t) > 0 THEN \"县站已审核\"\n" +
"\t\t) > 0 THEN '县站已审核'\n" +
"\t\tELSE NULL\n" +
"\tEND AS 'countyAudit'\n" +
"\t, CASE \n" +
@@ -64,7 +64,7 @@ public class MySqlCreateViewTest1 extends MysqlTest {
"\t\t\tFROM actvty_audit\n" +
"\t\t\tWHERE enroll_id = a.enroll_id\n" +
"\t\t\t\tAND rankjurisdiction = 2\n" +
"\t\t) > 0 THEN \"市馆已审核\"\n" +
"\t\t) > 0 THEN '市馆已审核'\n" +
"\t\tELSE NULL\n" +
"\tEND AS 'cityAudit'\n" +
"\t, CASE \n" +
@@ -73,7 +73,7 @@ public class MySqlCreateViewTest1 extends MysqlTest {
"\t\t\tFROM actvty_audit\n" +
"\t\t\tWHERE enroll_id = a.enroll_id\n" +
"\t\t\t\tAND rankjurisdiction = 3\n" +
"\t\t) > 0 THEN \"省馆已审核\"\n" +
"\t\t) > 0 THEN '省馆已审核'\n" +
"\t\tELSE NULL\n" +
"\tEND AS 'provinceAudit'\n" +
"FROM actvty_audit a\n" +
@@ -92,7 +92,7 @@ public class MySqlCreateViewTest1 extends MysqlTest {
"\t\t\tfrom actvty_audit\n" +
"\t\t\twhere enroll_id = a.enroll_id\n" +
"\t\t\t\tand rankjurisdiction = 1\n" +
"\t\t) > 0 then \"县站已审核\"\n" +
"\t\t) > 0 then '县站已审核'\n" +
"\t\telse null\n" +
"\tend as 'countyAudit'\n" +
"\t, case \n" +
@@ -101,7 +101,7 @@ public class MySqlCreateViewTest1 extends MysqlTest {
"\t\t\tfrom actvty_audit\n" +
"\t\t\twhere enroll_id = a.enroll_id\n" +
"\t\t\t\tand rankjurisdiction = 2\n" +
"\t\t) > 0 then \"市馆已审核\"\n" +
"\t\t) > 0 then '市馆已审核'\n" +
"\t\telse null\n" +
"\tend as 'cityAudit'\n" +
"\t, case \n" +
@@ -110,7 +110,7 @@ public class MySqlCreateViewTest1 extends MysqlTest {
"\t\t\tfrom actvty_audit\n" +
"\t\t\twhere enroll_id = a.enroll_id\n" +
"\t\t\t\tand rankjurisdiction = 3\n" +
"\t\t) > 0 then \"省馆已审核\"\n" +
"\t\t) > 0 then '省馆已审核'\n" +
"\t\telse null\n" +
"\tend as 'provinceAudit'\n" +
"from actvty_audit a\n" +
@@ -123,7 +123,7 @@ public class MySqlCreateViewTest1 extends MysqlTest {
// System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(6, visitor.getColumns().size());
Assert.assertEquals(3, visitor.getColumns().size());
Assert.assertEquals(2, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("actvty_audit")));
@@ -0,0 +1,44 @@
/*
* 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.insert;
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.ast.statement.MySqlInsertStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import org.junit.Assert;
import java.util.List;
public class MySqlInsertTest_18_alias extends MysqlTest {
public void test_insert_rollback_on_fail() throws Exception {
String sql = "INSERT INTO t1 (fval) values (\"abc\")";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
// System.out.println(SQLUtils.toMySqlString(insertStmt));
String formatSql = "INSERT INTO t1 (fval)\n" +
"VALUES ('abc')";
Assert.assertEquals(formatSql, SQLUtils.toMySqlString(insertStmt));
}
}
@@ -53,7 +53,7 @@ public class MySqlSelectTest_63_alias extends MysqlTest {
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("SELECT totalNumber\n" +
"\t, concat(\"\", ?, round(memberNumber, 0), \"\") AS totalDisplay\n" +
"\t, concat('', ?, round(memberNumber, 0), '') AS totalDisplay\n" +
"FROM (\n" +
"\tSELECT COUNT(1) AS totalNumber, SUM(memberNumber) AS memberNumber\n" +
"\tFROM (\n" +
@@ -76,7 +76,7 @@ public class MySqlSelectTest_63_alias extends MysqlTest {
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select totalNumber\n" +
"\t, concat(\"\", ?, round(memberNumber, 0), \"\") as totalDisplay\n" +
"\t, concat('', ?, round(memberNumber, 0), '') as totalDisplay\n" +
"from (\n" +
"\tselect count(1) as totalNumber, sum(memberNumber) as memberNumber\n" +
"\tfrom (\n" +
@@ -100,7 +100,7 @@ public class MySqlSelectTest_63_alias extends MysqlTest {
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
assertEquals("SELECT totalNumber\n" +
"\t, concat(\"\", ?, round(memberNumber, ?), \"\") AS totalDisplay\n" +
"\t, concat(?, ?, round(memberNumber, ?), ?) AS totalDisplay\n" +
"FROM (\n" +
"\tSELECT COUNT(1) AS totalNumber, SUM(memberNumber) AS memberNumber\n" +
"\tFROM (\n" +
@@ -46,4 +46,6 @@ public class MySqlSelectTest_92 extends MysqlTest {
"\tAND cfgdatasou0_.module_name = ?\n" +
"\tAND cfgdatasou0_.node_type = ?", stmt.toString());
}
}
@@ -0,0 +1,74 @@
/*
* 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.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import java.util.List;
public class MySqlSelectTest_93 extends MysqlTest {
public void test_0() throws Exception {
String sql = "select\n" +
" \n" +
" \n" +
"\n" +
" id,whiteList_type, origin_address,gmt_create,gmt_modified, updateby, createby, (whitelist->'$.latitude') AS latitude,\n" +
" (whitelist->'$.longitude') AS longitude, whiteList_status, remark\n" +
"\n" +
" \n" +
" \n" +
" from geo_whitelist\n" +
" where 1=1\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" and whiteList_status = ?";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
assertEquals(1, statementList.size());
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
assertEquals("SELECT id, whiteList_type, origin_address, gmt_create, gmt_modified\n" +
"\t, updateby, createby, whitelist -> '$.latitude' AS latitude\n" +
"\t, whitelist -> '$.longitude' AS longitude, whiteList_status, remark\n" +
"FROM geo_whitelist\n" +
"WHERE 1 = 1\n" +
"\tAND whiteList_status = ?", stmt.toString());
}
public void test_1() throws Exception {
String sql = "SELECT * FROM user WHERE lastlogininfo ->'$.time' > '2015-10-02';";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
assertEquals(1, statementList.size());
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
assertEquals("SELECT *\n" +
"FROM user\n" +
"WHERE lastlogininfo -> '$.time' > '2015-10-02';", stmt.toString());
}
}
@@ -0,0 +1,40 @@
/*
* 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.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.ParserException;
import java.util.List;
public class MySqlSelectTest_95_error_orderBy extends MysqlTest {
public void test_0() throws Exception {
String sql = "select * from xx ordor by name desc";
Exception error = null;
try {
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
} catch (ParserException ex) {
error = ex;
ex.printStackTrace();
}
assertNotNull(error);
}
}
@@ -0,0 +1,55 @@
/*
* 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.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;
import com.alibaba.druid.stat.TableStat;
import java.util.List;
public class PGCreateTableTest_13 extends PGTest {
public void test_0() throws Exception {
String sql = "CREATE index fact_pageview_sum_p_day_id_idx ON fact_pageview_sum USING btree (p_day_id);";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
assertEquals("CREATE INDEX fact_pageview_sum_p_day_id_idx USING btree ON fact_pageview_sum (p_day_id);", SQLUtils.toPGString(stmt));
assertEquals("create index fact_pageview_sum_p_day_id_idx using btree on fact_pageview_sum (p_day_id);", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
assertTrue(visitor.getTables().containsKey(new TableStat.Name("fact_pageview_sum")));
// assertTrue(visitor.getTables().get(new TableStat.Name("fact_pageview_sum")).getCreateCount() == 1);
assertEquals(1, visitor.getColumns().size() );
}
}