mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved parser performance.
This commit is contained in:
@@ -15,20 +15,23 @@ import java.util.Set;
|
||||
public class MySqlParameterizedBenchmark extends TestCase {
|
||||
|
||||
public void test_perf() throws Exception {
|
||||
String sql = "select id as id, gmt_create as gmtCreate, gmt_modified as gmtModified, name as name, owner as owner, type as type, statement as statement, datasource as datasource, meta as meta, param_file as paramFile, sharable as sharable, data_type as dataType, status as status, config as config, project_id as projectId, plugins as plugins, field_compare as fieldCompare, field_ext as fieldExt, open as open from quark_s_dataset where id = 12569434";
|
||||
String sql = "select id as id, gmt_create as gmtCreate, gmt_modified as gmtModified, name as name, owner as owner, type as type, statement as statement, datasource as datasource, meta as meta, param_file as paramFile, sharable as sharable, data_type as dataType, status as status, config as config, project_id as projectId, plugins as plugins, field_compare as fieldCompare, field_ext as fieldExt, openx as openx from quark_s_dataset where id = 12569434";
|
||||
String sql2 = "/* ///6ea6f232/ */select count(*) FROM (SELECT * FROM cluster_ins_mapping WHERE `engine` = 'MySQL' and `status`='Activation' GROUP BY ip,port ) a;";
|
||||
String sql3 = "/* 0be5256b15035048614234260e129b/0.1//722b9d1a/ */ select count(*) from (SELECT message_id,employee_name,employee_id FROM tunning_overview where instance_info IS NOT NULL and instance_info = '11.179.218.9:3306' and gmt_modified >= '2017-08-23 00:00:00' and gmt_modified <= '2017-08-24 00:00:00' ) o join (SELECT message_id,index_advice_count,index_advice,gmt_created FROM tunning_task_detail where gmt_modified >= '2017-08-23 00:00:00' and gmt_modified <= '2017-08-24 00:00:00' and index_advice_count >= 0 and error_code='0000' ) t on o.message_id=t.message_id where o.employee_name!='system' or ( o.employee_name='system' and t.index_advice_count>0);";
|
||||
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
// perf(sql); // 6740 6201
|
||||
perf_parse(sql); // 4643 4377
|
||||
// perf(sql); // 6740 6201 4752 4514
|
||||
// perf(sql2); // 2948 2928 2869 2780 2502
|
||||
perf(sql3); // 15093 10392 10416 10154 10007 9126 8907
|
||||
// perf_parse(sql); // 4643 4377 4345 3801 3627 3228
|
||||
// perf_parse(sql2); // 1918 1779 1666 1646
|
||||
// perf_parse(sql3); // 9174 5875 5805 5536
|
||||
}
|
||||
}
|
||||
public void perf(String sql) {
|
||||
long startMillis = System.currentTimeMillis();
|
||||
for (int i = 0; i < 1000 * 1000; ++i) {
|
||||
|
||||
|
||||
String psql = SqlHolder.of(sql).parameterize();
|
||||
// stmt.toString();
|
||||
}
|
||||
long millis = System.currentTimeMillis() - startMillis;
|
||||
System.out.println("millis : " + millis);
|
||||
@@ -37,12 +40,10 @@ public class MySqlParameterizedBenchmark extends TestCase {
|
||||
public void perf_parse(String sql) {
|
||||
long startMillis = System.currentTimeMillis();
|
||||
for (int i = 0; i < 1000 * 1000; ++i) {
|
||||
|
||||
|
||||
SqlHolder.of(sql).ensureParsed();
|
||||
// stmt.toString();
|
||||
}
|
||||
long millis = System.currentTimeMillis() - startMillis;
|
||||
System.out.println("millis : " + millis);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLParserFeature;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
@@ -55,7 +56,7 @@ public class SqlHolder {
|
||||
}
|
||||
// ast = SQLUtils.parseStatements(text, dialect).get(0);
|
||||
try {
|
||||
ast = new MySqlStatementParser(text).parseStatement();
|
||||
ast = new MySqlStatementParser(text, SQLParserFeature.EnableSQLBinaryOpExprGroup).parseStatement();
|
||||
} catch (ParserException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public class Bug_for_ruiyi extends TestCase {
|
||||
|
||||
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}]}]}'"
|
||||
+ "\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";
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
|
||||
@@ -47,7 +47,7 @@ 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)";
|
||||
"\nVALUES (1, \"KIKI\", 123)";
|
||||
{
|
||||
MySqlWallProvider provider = new MySqlWallProvider(config_callback);
|
||||
WallCheckResult checkResult = provider.check(insert_sql);
|
||||
@@ -72,8 +72,8 @@ 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)";
|
||||
"\nVALUES (1, \"KIKI\", 123)," + //
|
||||
"\n\t(1, \"CICI\", 123)";
|
||||
|
||||
{
|
||||
MySqlWallProvider provider = new MySqlWallProvider(config_callback);
|
||||
|
||||
@@ -31,7 +31,7 @@ import com.alibaba.druid.wall.WallUtils;
|
||||
public class MySqlWallTest11 extends TestCase {
|
||||
|
||||
public void test_true() throws Exception {
|
||||
Assert.assertTrue(WallUtils.isValidateMySql(//
|
||||
assertTrue(WallUtils.isValidateMySql(//
|
||||
"SELECT *, MATCH(question_content_fulltext) " + //
|
||||
"AGAINST('2015431867 2636826089 3807520117 2796321160 2615920174' IN BOOLEAN MODE) AS score " + //
|
||||
"FROM aws_question " + //
|
||||
|
||||
@@ -191,7 +191,7 @@ public class DALParserTest extends TestCase {
|
||||
SQLStatement set = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(set);
|
||||
Assert.assertEquals("SET NAMES 'utf8' COLLATE latin1_danish_ci", output);
|
||||
Assert.assertEquals("SET NAMES 'utf8' COLLATE \"latin1_danish_ci\"", output);
|
||||
}
|
||||
|
||||
public void test_setNames_2() throws Exception {
|
||||
|
||||
@@ -39,6 +39,6 @@ public class MySqlError_test_3 extends MysqlTest {
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
// error.printStackTrace();
|
||||
Assert.assertEquals("syntax error, error in :'us = LIMIT ?, ?',expect QUES, actual QUES pos 248, line 1, column 247, token QUES", error.getMessage());
|
||||
Assert.assertEquals("syntax error, error in :'us = LIMIT ?, ?', expect QUES, actual QUES pos 248, line 1, column 247, token QUES", error.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,20 @@ public class SQLUtilsTest extends TestCase {
|
||||
"\nFROM t" + //
|
||||
"\nWHERE id = 'abc'", formattedSql);
|
||||
}
|
||||
|
||||
public void test_format_0() throws Exception {
|
||||
String sql = "select \"%\"'温'\"%\" FROM dual;";
|
||||
String formattedSql = SQLUtils.formatMySql(sql);
|
||||
Assert.assertEquals("SELECT '%温%'\n" +
|
||||
"FROM dual;", formattedSql);
|
||||
}
|
||||
|
||||
public void test_format_1() throws Exception {
|
||||
String sql = "select * from t where tname LIKE \"%\"'温'\"%\"";
|
||||
String formattedSql = SQLUtils.formatMySql(sql);
|
||||
Assert.assertEquals("SELECT *\nFROM t\nWHERE tname LIKE CONCAT('%', '温', '%')", formattedSql);
|
||||
Assert.assertEquals("SELECT *\n" +
|
||||
"FROM t\n" +
|
||||
"WHERE tname LIKE '%温%'", formattedSql);
|
||||
}
|
||||
|
||||
public void test_format_2() throws Exception {
|
||||
|
||||
@@ -526,7 +526,7 @@ public class StringFunctionsTest extends TestCase {
|
||||
|
||||
String text = output(stmtList);
|
||||
|
||||
assertEquals("SELECT SUBSTRING('Sakila', -4, 2)", text);
|
||||
assertEquals("SELECT SUBSTRING('Sakila' FROM -4 FOR 2)", text);
|
||||
}
|
||||
|
||||
public void test_45() throws Exception {
|
||||
@@ -537,7 +537,7 @@ public class StringFunctionsTest extends TestCase {
|
||||
|
||||
String text = output(stmtList);
|
||||
|
||||
assertEquals("SELECT SUBSTRING('foobarbar', 4)", text);
|
||||
assertEquals("SELECT SUBSTRING('foobarbar' FROM 4)", text);
|
||||
}
|
||||
|
||||
public void test_46() throws Exception {
|
||||
|
||||
@@ -53,8 +53,8 @@ public class MySqlCeateTableTest34 extends TestCase {
|
||||
"\t\"type\" tinyint(4) UNSIGNED NOT NULL DEFAULT '0' COMMENT '扩展属性类型,0:虚拟商品',\n" +
|
||||
"\t\"attr_key\" varchar(50) NOT NULL COMMENT '扩展属性key',\n" +
|
||||
"\t\"attr_value\" varchar(500) NOT NULL COMMENT '扩展属性value',\n" +
|
||||
"\tPRIMARY KEY ('id'),\n" +
|
||||
"\tKEY \"idx_item_type_kv\" ('item_id', 'type', 'attr_key', 'attr_value'(191))\n" +
|
||||
"\tPRIMARY KEY (\"id\"),\n" +
|
||||
"\tKEY \"idx_item_type_kv\" (\"item_id\", \"type\", \"attr_key\", \"attr_value\"(191))\n" +
|
||||
")", output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(3, visitor.getColumns().size());
|
||||
Assert.assertEquals(6, visitor.getColumns().size());
|
||||
Assert.assertEquals(2, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("actvty_audit")));
|
||||
|
||||
+4
-4
@@ -51,8 +51,8 @@ public class MySqlCreateTableTest72 extends MysqlTest {
|
||||
Assert.assertEquals("CREATE TABLE \"MessageInstance\" ("
|
||||
+ "\n\t\"id\" int(11) NOT NULL AUTO_INCREMENT,"
|
||||
+ "\n\t\"messageId\" int(11) NOT NULL,"
|
||||
+ "\n\tPRIMARY KEY ('id'),"
|
||||
+ "\n\tKEY \"ix_messageId\" ('messageId')"
|
||||
+ "\n\tPRIMARY KEY (\"id\"),"
|
||||
+ "\n\tKEY \"ix_messageId\" (\"messageId\")"
|
||||
+ "\n)", output);
|
||||
}
|
||||
|
||||
@@ -61,8 +61,8 @@ public class MySqlCreateTableTest72 extends MysqlTest {
|
||||
Assert.assertEquals("create table \"MessageInstance\" ("
|
||||
+ "\n\t\"id\" int(11) not null auto_increment,"
|
||||
+ "\n\t\"messageId\" int(11) not null,"
|
||||
+ "\n\tprimary key ('id'),"
|
||||
+ "\n\tkey \"ix_messageId\" ('messageId')"
|
||||
+ "\n\tprimary key (\"id\"),"
|
||||
+ "\n\tkey \"ix_messageId\" (\"messageId\")"
|
||||
+ "\n)", output);
|
||||
}
|
||||
}
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
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.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/8/23.
|
||||
*/
|
||||
public class MySqlParameterizedOutputVisitorTest_48_aggregate extends TestCase {
|
||||
public void test_for_parameterize() throws Exception {
|
||||
final String dbType = JdbcConstants.MYSQL;
|
||||
String sql = "select count(1) from t";
|
||||
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
SQLStatement statement = stmtList.get(0);
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
|
||||
List<Object> parameters = new ArrayList<Object>();
|
||||
visitor.setParameterized(true);
|
||||
visitor.setParameterizedMergeInList(true);
|
||||
visitor.setParameters(parameters);
|
||||
/*visitor.setPrettyFormat(false);*/
|
||||
statement.accept(visitor);
|
||||
/* JSONArray array = new JSONArray();
|
||||
for(String table : visitor.getTables()){
|
||||
array.add(table.replaceAll("`",""));
|
||||
}*/
|
||||
|
||||
String psql = out.toString();
|
||||
|
||||
System.out.println(psql);
|
||||
|
||||
|
||||
assertEquals("SELECT COUNT(1)\n" +
|
||||
"FROM t", psql);
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
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.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/8/23.
|
||||
*/
|
||||
public class MySqlParameterizedOutputVisitorTest_49_createTable extends TestCase {
|
||||
public void test_for_parameterize() throws Exception {
|
||||
final String dbType = JdbcConstants.MYSQL;
|
||||
String sql = "CREATE TABLE projects ("//
|
||||
+ "long_name int(3) NOT NULL default 1 + 2" +
|
||||
") ";
|
||||
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
SQLStatement statement = stmtList.get(0);
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
|
||||
List<Object> parameters = new ArrayList<Object>();
|
||||
visitor.setParameterized(true);
|
||||
visitor.setParameterizedMergeInList(true);
|
||||
visitor.setParameters(parameters);
|
||||
/*visitor.setPrettyFormat(false);*/
|
||||
statement.accept(visitor);
|
||||
/* JSONArray array = new JSONArray();
|
||||
for(String table : visitor.getTables()){
|
||||
array.add(table.replaceAll("`",""));
|
||||
}*/
|
||||
|
||||
String psql = out.toString();
|
||||
|
||||
System.out.println(psql);
|
||||
|
||||
|
||||
assertEquals("CREATE TABLE projects (\n" +
|
||||
"\tlong_name int(3) NOT NULL DEFAULT 1 + 2\n" +
|
||||
")", psql);
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
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.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/8/23.
|
||||
*/
|
||||
public class MySqlParameterizedOutputVisitorTest_50_orderby extends TestCase {
|
||||
public void test_for_parameterize() throws Exception {
|
||||
final String dbType = JdbcConstants.MYSQL;
|
||||
String sql = "select * from t order by 1";
|
||||
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
SQLStatement statement = stmtList.get(0);
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
|
||||
List<Object> parameters = new ArrayList<Object>();
|
||||
visitor.setParameterized(true);
|
||||
visitor.setParameterizedMergeInList(true);
|
||||
visitor.setParameters(parameters);
|
||||
/*visitor.setPrettyFormat(false);*/
|
||||
statement.accept(visitor);
|
||||
/* JSONArray array = new JSONArray();
|
||||
for(String table : visitor.getTables()){
|
||||
array.add(table.replaceAll("`",""));
|
||||
}*/
|
||||
|
||||
String psql = out.toString();
|
||||
|
||||
System.out.println(psql);
|
||||
|
||||
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM t\n" +
|
||||
"ORDER BY 1", psql);
|
||||
}
|
||||
}
|
||||
@@ -33,20 +33,20 @@ public class MySqlParserResourceTest extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
// exec_test("bvt/parser/mysql-0.txt");
|
||||
exec_test("bvt/parser/mysql-1.txt");
|
||||
exec_test("bvt/parser/mysql-2.txt");
|
||||
exec_test("bvt/parser/mysql-3.txt");
|
||||
exec_test("bvt/parser/mysql-4.txt");
|
||||
exec_test("bvt/parser/mysql-5.txt");
|
||||
exec_test("bvt/parser/mysql-6.txt");
|
||||
exec_test("bvt/parser/mysql-7.txt");
|
||||
exec_test("bvt/parser/mysql-8.txt");
|
||||
// exec_test("bvt/parser/mysql-1.txt");
|
||||
// exec_test("bvt/parser/mysql-2.txt");
|
||||
// exec_test("bvt/parser/mysql-3.txt");
|
||||
// exec_test("bvt/parser/mysql-4.txt");
|
||||
// exec_test("bvt/parser/mysql-5.txt");
|
||||
// exec_test("bvt/parser/mysql-6.txt");
|
||||
// exec_test("bvt/parser/mysql-7.txt");
|
||||
// exec_test("bvt/parser/mysql-8.txt");
|
||||
exec_test("bvt/parser/mysql-9.txt");
|
||||
exec_test("bvt/parser/mysql-10.txt");
|
||||
exec_test("bvt/parser/mysql-11.txt");
|
||||
exec_test("bvt/parser/mysql-12.txt");
|
||||
exec_test("bvt/parser/mysql-13.txt");
|
||||
exec_test("bvt/parser/mysql-15.txt");
|
||||
// exec_test("bvt/parser/mysql-10.txt");
|
||||
// exec_test("bvt/parser/mysql-11.txt");
|
||||
// exec_test("bvt/parser/mysql-12.txt");
|
||||
// exec_test("bvt/parser/mysql-13.txt");
|
||||
// exec_test("bvt/parser/mysql-15.txt");
|
||||
}
|
||||
|
||||
public void exec_test(String resource) throws Exception {
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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_53_match extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT *, MATCH(question_content_fulltext) " + //
|
||||
"AGAINST('2015431867 2636826089 3807520117 2796321160 2615920174' IN BOOLEAN MODE) AS score " + //
|
||||
"FROM aws_question " + //
|
||||
"WHERE MATCH(question_content_fulltext) " + //
|
||||
" AGAINST('2015431867 2636826089 3807520117 2796321160 2615920174' IN BOOLEAN MODE) " + //
|
||||
"ORDER BY score DESC, agree_count DESC LIMIT 10";
|
||||
|
||||
System.out.println(sql);
|
||||
|
||||
|
||||
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 *, MATCH (question_content_fulltext) AGAINST ('2015431867 2636826089 3807520117 2796321160 2615920174' IN BOOLEAN MODE) AS score\n" +
|
||||
"FROM aws_question\n" +
|
||||
"WHERE MATCH (question_content_fulltext) AGAINST ('2015431867 2636826089 3807520117 2796321160 2615920174' IN BOOLEAN MODE)\n" +
|
||||
"ORDER BY score DESC, agree_count DESC\n" +
|
||||
"LIMIT 10", //
|
||||
output);
|
||||
}
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
assertEquals("select *, match (question_content_fulltext) against ('2015431867 2636826089 3807520117 2796321160 2615920174' in boolean mode) as score\n" +
|
||||
"from aws_question\n" +
|
||||
"where match (question_content_fulltext) against ('2015431867 2636826089 3807520117 2796321160 2615920174' in boolean mode)\n" +
|
||||
"order by score desc, agree_count desc\n" +
|
||||
"limit 10", //
|
||||
output);
|
||||
}
|
||||
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
|
||||
assertEquals("SELECT *, MATCH (question_content_fulltext) AGAINST (? IN BOOLEAN MODE) AS score\n" +
|
||||
"FROM aws_question\n" +
|
||||
"WHERE MATCH (question_content_fulltext) AGAINST (? IN BOOLEAN MODE)\n" +
|
||||
"ORDER BY score DESC, agree_count DESC\n" +
|
||||
"LIMIT ?", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.oracle;
|
||||
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExprGroup;
|
||||
import com.alibaba.druid.sql.parser.SQLParserFeature;
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -33,11 +35,11 @@ public class LargeOrTest extends TestCase {
|
||||
buf.append(" OR ID = ?");
|
||||
}
|
||||
String sql = buf.toString();
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
OracleStatementParser parser = new OracleStatementParser(sql, SQLParserFeature.EnableSQLBinaryOpExprGroup);
|
||||
SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatementList().get(0);
|
||||
SQLSelectQueryBlock select = (SQLSelectQueryBlock) stmt.getSelect().getQuery();
|
||||
SQLBinaryOpExpr where = (SQLBinaryOpExpr) select.getWhere();
|
||||
SQLBinaryOpExpr last = (SQLBinaryOpExpr) where.getRight();
|
||||
SQLBinaryOpExprGroup where = (SQLBinaryOpExprGroup) select.getWhere();
|
||||
SQLBinaryOpExpr last = (SQLBinaryOpExpr) where.getItems().get(0);
|
||||
Assert.assertEquals(SQLBinaryOperator.Equality, last.getOperator());
|
||||
}
|
||||
|
||||
@@ -48,11 +50,11 @@ public class LargeOrTest extends TestCase {
|
||||
buf.append(" AND ID = ?");
|
||||
}
|
||||
String sql = buf.toString();
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
OracleStatementParser parser = new OracleStatementParser(sql, SQLParserFeature.EnableSQLBinaryOpExprGroup);
|
||||
SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatementList().get(0);
|
||||
SQLSelectQueryBlock select = (SQLSelectQueryBlock) stmt.getSelect().getQuery();
|
||||
SQLBinaryOpExpr where = (SQLBinaryOpExpr) select.getWhere();
|
||||
SQLBinaryOpExpr last = (SQLBinaryOpExpr) where.getRight();
|
||||
SQLBinaryOpExprGroup where = (SQLBinaryOpExprGroup) select.getWhere();
|
||||
SQLBinaryOpExpr last = (SQLBinaryOpExpr) where.getItems().get(0);
|
||||
Assert.assertEquals(SQLBinaryOperator.Equality, last.getOperator());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,13 +54,13 @@ public class OracleCreateDbLinkTest1 extends OracleTest {
|
||||
{
|
||||
String text = SQLUtils.toOracleString(stmt);
|
||||
|
||||
Assert.assertEquals("CREATE DATABASE LINK db_link CONNECT TO \"xxx\" IDENTIFIED BY xxx USING '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))'", text);
|
||||
Assert.assertEquals("CREATE DATABASE LINK db_link CONNECT TO \"xxx\" IDENTIFIED BY \"xxx\" USING '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))'", text);
|
||||
}
|
||||
|
||||
{
|
||||
String text = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
|
||||
Assert.assertEquals("create database link db_link connect to \"xxx\" identified by xxx using '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))'", text);
|
||||
Assert.assertEquals("create database link db_link connect to \"xxx\" identified by \"xxx\" using '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))'", text);
|
||||
}
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
|
||||
|
||||
|
||||
@@ -52,12 +52,12 @@ public class OracleSelectTest57 extends OracleTest {
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
|
||||
Assert.assertEquals(4, visitor.getColumns().size());
|
||||
Assert.assertEquals(3, visitor.getColumns().size());
|
||||
|
||||
{
|
||||
String text = SQLUtils.toOracleString(stmt);
|
||||
|
||||
Assert.assertEquals("SELECT TRIM(EUCD) AS \"value\", NTLANG1 AS \"text\""
|
||||
Assert.assertEquals("SELECT TRIM(BOTH FROM EUCD) AS \"value\", NTLANG1 AS \"text\""
|
||||
+ "\nFROM T_HT_WREM_ENUMLANG_D"
|
||||
+ "\nWHERE TYPE = ?"
|
||||
+ "\nORDER BY \"value\" ASC", text);
|
||||
@@ -66,7 +66,7 @@ public class OracleSelectTest57 extends OracleTest {
|
||||
{
|
||||
String text = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
|
||||
Assert.assertEquals("select TRIM(EUCD) as \"value\", NTLANG1 as \"text\""
|
||||
Assert.assertEquals("select TRIM(BOTH from EUCD) as \"value\", NTLANG1 as \"text\""
|
||||
+ "\nfrom T_HT_WREM_ENUMLANG_D"
|
||||
+ "\nwhere TYPE = ?"
|
||||
+ "\norder by \"value\" asc", text);
|
||||
|
||||
@@ -46,6 +46,6 @@ public class OracleSelectTest_param extends OracleTest {
|
||||
assertEquals("SELECT c1\n" +
|
||||
"FROM t1 t\n" +
|
||||
"WHERE t.c2 = ?\n" +
|
||||
"\tAND t.c3 = ?", SQLUtils.toSQLString(stmt, dbType, new SQLUtils.FormatOption(true, true, true)));
|
||||
"\tAND (t.c3 = ?)", SQLUtils.toSQLString(stmt, dbType, new SQLUtils.FormatOption(true, true, true)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user