mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -31,9 +31,14 @@ public class MySqlPerfMain_select {
|
||||
System.out.println(arguments);
|
||||
|
||||
String sql = "SELECT ID, NAME, AGE FROM USER WHERE ID = ?";
|
||||
// String sql = "SELECT student_name, " //
|
||||
// + "GROUP_CONCAT(DISTINCT test_score " //
|
||||
// + " ORDER BY test_score DESC SEPARATOR ' ') " //
|
||||
// + "FROM student " //
|
||||
// + "GROUP BY student_name";
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
perfMySql(sql);
|
||||
perfMySql(sql); // 831 810
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -72,9 +72,10 @@ public class DeleteSyntaxTest extends TestCase {
|
||||
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
Assert.assertEquals("DELETE FROM a1, a2 USING (t1) AS a1" + //
|
||||
"\n\tINNER JOIN t2 a2\nWHERE a1.id = a2.id", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("delete from a1, a2 using (t1) as a1" + //
|
||||
Assert.assertEquals("DELETE FROM (a1, a2 USING (t1) AS a1)\n" +
|
||||
"\tINNER JOIN t2 a2\n" +
|
||||
"WHERE a1.id = a2.id", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("delete from (a1, a2 using (t1) as a1)" + //
|
||||
"\n\tinner join t2 a2\nwhere a1.id = a2.id", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
}
|
||||
|
||||
|
||||
@@ -36,10 +36,10 @@ public class MySqlDeleteTest_1 extends MysqlTest {
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals("DELETE FROM a1, a2 USING (t1) AS a1"
|
||||
Assert.assertEquals("DELETE FROM (a1, a2 USING (t1) AS a1)"
|
||||
+ "\n\tINNER JOIN t2 a2"
|
||||
+ "\nWHERE a1.id = a2.id;", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("delete from a1, a2 using (t1) as a1"
|
||||
Assert.assertEquals("delete from (a1, a2 using (t1) as a1)"
|
||||
+ "\n\tinner join t2 a2"
|
||||
+ "\nwhere a1.id = a2.id;", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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 java.util.List;
|
||||
|
||||
public class MySqlSelectTest_52_comment extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT comment.id,user.id AS user_id,user.username,user.avatar,comment.created_at,\n" +
|
||||
" getDistance('31.860968', '117.252579', user.latitude, user.longitude) AS distance,\n" +
|
||||
" 1 AS type\n" +
|
||||
" FROM user,comment\n" +
|
||||
" WHERE user.id=comment.user_id\n" +
|
||||
" AND getDistance('31.860968', '117.252579', user.latitude, user.longitude)>=0\n" +
|
||||
" AND getDistance('31.860968', '117.252579', user.latitude, user.longitude)<=10000\n" +
|
||||
" UNION\n" +
|
||||
" SELECT user_book_list.id,user.id AS user_id,user.username,user.avatar,user_book_list.created_at,\n" +
|
||||
" getDistance('31.860968', '117.252579', user.latitude, user.longitude) AS distance,\n" +
|
||||
" 2 AS type\n" +
|
||||
" FROM user_book_list,user\n" +
|
||||
" WHERE user.id=user_book_list.user_id\n" +
|
||||
" AND getDistance('31.860968', '117.252579', user.latitude, user.longitude)>=0\n" +
|
||||
" AND getDistance('31.860968', '117.252579', user.latitude, user.longitude)<=10000\n" +
|
||||
" ORDER BY created_at DESC\n" +
|
||||
" LIMIT 10, 10";
|
||||
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, true);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
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());
|
||||
|
||||
// assertEquals(1, visitor.getTables().size());
|
||||
// assertEquals(1, visitor.getColumns().size());
|
||||
// assertEquals(0, visitor.getConditions().size());
|
||||
// assertEquals(0, visitor.getOrderByColumns().size());
|
||||
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
assertEquals("SELECT comment.id, user.id AS user_id, user.username, user.avatar, comment.created_at\n" +
|
||||
"\t, getDistance('31.860968', '117.252579', user.latitude, user.longitude) AS distance\n" +
|
||||
"\t, 1 AS type\n" +
|
||||
"FROM user, comment\n" +
|
||||
"WHERE user.id = comment.user_id\n" +
|
||||
"\tAND getDistance('31.860968', '117.252579', user.latitude, user.longitude) >= 0\n" +
|
||||
"\tAND getDistance('31.860968', '117.252579', user.latitude, user.longitude) <= 10000\n" +
|
||||
"UNION\n" +
|
||||
"SELECT user_book_list.id, user.id AS user_id, user.username, user.avatar, user_book_list.created_at\n" +
|
||||
"\t, getDistance('31.860968', '117.252579', user.latitude, user.longitude) AS distance\n" +
|
||||
"\t, 2 AS type\n" +
|
||||
"FROM user_book_list, user\n" +
|
||||
"WHERE user.id = user_book_list.user_id\n" +
|
||||
"\tAND getDistance('31.860968', '117.252579', user.latitude, user.longitude) >= 0\n" +
|
||||
"\tAND getDistance('31.860968', '117.252579', user.latitude, user.longitude) <= 10000\n" +
|
||||
"ORDER BY created_at DESC\n" +
|
||||
"LIMIT 10, 10", //
|
||||
output);
|
||||
}
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
assertEquals("select comment.id, user.id as user_id, user.username, user.avatar, comment.created_at\n" +
|
||||
"\t, getDistance('31.860968', '117.252579', user.latitude, user.longitude) as distance\n" +
|
||||
"\t, 1 as type\n" +
|
||||
"from user, comment\n" +
|
||||
"where user.id = comment.user_id\n" +
|
||||
"\tand getDistance('31.860968', '117.252579', user.latitude, user.longitude) >= 0\n" +
|
||||
"\tand getDistance('31.860968', '117.252579', user.latitude, user.longitude) <= 10000\n" +
|
||||
"union\n" +
|
||||
"select user_book_list.id, user.id as user_id, user.username, user.avatar, user_book_list.created_at\n" +
|
||||
"\t, getDistance('31.860968', '117.252579', user.latitude, user.longitude) as distance\n" +
|
||||
"\t, 2 as type\n" +
|
||||
"from user_book_list, user\n" +
|
||||
"where user.id = user_book_list.user_id\n" +
|
||||
"\tand getDistance('31.860968', '117.252579', user.latitude, user.longitude) >= 0\n" +
|
||||
"\tand getDistance('31.860968', '117.252579', user.latitude, user.longitude) <= 10000\n" +
|
||||
"order by created_at desc\n" +
|
||||
"limit 10, 10", //
|
||||
output);
|
||||
}
|
||||
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
|
||||
assertEquals("SELECT comment.id, user.id AS user_id, user.username, user.avatar, comment.created_at\n" +
|
||||
"\t, getDistance(?, ?, user.latitude, user.longitude) AS distance\n" +
|
||||
"\t, ? AS type\n" +
|
||||
"FROM user, comment\n" +
|
||||
"WHERE user.id = comment.user_id\n" +
|
||||
"\tAND getDistance(?, ?, user.latitude, user.longitude) >= ?\n" +
|
||||
"\tAND getDistance(?, ?, user.latitude, user.longitude) <= ?\n" +
|
||||
"UNION\n" +
|
||||
"SELECT user_book_list.id, user.id AS user_id, user.username, user.avatar, user_book_list.created_at\n" +
|
||||
"\t, getDistance(?, ?, user.latitude, user.longitude) AS distance\n" +
|
||||
"\t, ? AS type\n" +
|
||||
"FROM user_book_list, user\n" +
|
||||
"WHERE user.id = user_book_list.user_id\n" +
|
||||
"\tAND getDistance(?, ?, user.latitude, user.longitude) >= ?\n" +
|
||||
"\tAND getDistance(?, ?, user.latitude, user.longitude) <= ?\n" +
|
||||
"ORDER BY created_at DESC\n" +
|
||||
"LIMIT ?, ?", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.alibaba.druid.bvt.sql.mysql.transform;
|
||||
|
||||
import com.alibaba.druid.sql.SQLTransformUtils;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class DecodeTest_0 extends TestCase {
|
||||
public void test_transform() throws Exception {
|
||||
SQLMethodInvokeExpr decodeExpr = (SQLMethodInvokeExpr) SQLUtils.toSQLExpr("decode(a, b, c, d)", JdbcConstants.ORACLE);
|
||||
SQLExpr expr = SQLTransformUtils.transformDecode(decodeExpr);
|
||||
String targetSql = SQLUtils.toSQLString(expr, JdbcConstants.MYSQL);
|
||||
assertEquals("if(a = b, c, d)", targetSql);
|
||||
}
|
||||
|
||||
public void test_transform_null() throws Exception {
|
||||
SQLMethodInvokeExpr decodeExpr = (SQLMethodInvokeExpr) SQLUtils.toSQLExpr("decode(a, null, c, d)", JdbcConstants.ORACLE);
|
||||
SQLExpr expr = SQLTransformUtils.transformDecode(decodeExpr);
|
||||
String targetSql = SQLUtils.toSQLString(expr, JdbcConstants.MYSQL);
|
||||
assertEquals("if(a IS NULL, c, d)", targetSql);
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.oracle.select;
|
||||
|
||||
import com.alibaba.druid.sql.OracleTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleSelectTest74_translate extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"SELECT TRANSLATE(SUBSTR(TRIM(E.BZ)||\n" +
|
||||
" TRIM(I.BZ)||\n" +
|
||||
" TRIM(H.MC)||\n" +
|
||||
" TRIM(A.MLPH)||\n" +
|
||||
" TRIM(A.MLXZ),1,35)\n" +
|
||||
" USING CHAR_CS) FROM T"; //
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("relationships : " + visitor.getRelationships());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals(1, visitor.getTables().size());
|
||||
|
||||
assertEquals(5, visitor.getColumns().size());
|
||||
|
||||
{
|
||||
String text = SQLUtils.toOracleString(stmt);
|
||||
|
||||
assertEquals("SELECT TRANSLATE(SUBSTR(TRIM(E.BZ) || TRIM(I.BZ) || TRIM(H.MC) || TRIM(A.MLPH) || TRIM(A.MLXZ), 1, 35) USING CHAR_CS)\n" +
|
||||
"FROM T", text);
|
||||
}
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
|
||||
|
||||
// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user