mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for mysql parser.
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user