improved sql parser.

This commit is contained in:
wenshao
2017-09-10 03:42:21 +08:00
parent 3330643479
commit 7d54eea716
37 changed files with 848 additions and 243 deletions
@@ -49,14 +49,22 @@ public class MySqlParameterizedBenchmark extends TestCase {
"\tOR batch.batch_name = '')\n" +
"ORDER BY batch_gmt_create DESC\n" +
"LIMIT 0, 50";
String sql8 = "select @@session.tx_read_only";
String sql9 = "insert into seller_item_sku_1967 ( item_id, gmt_modified, gmt_create, sku_id, seller_id, outer_id, status, sync_version, sku_feature) values ( 558323210106, '2017-09-08 10:55:39', '2017-09-08 10:55:39', 3634115182525, 3403773871, '', 1, 0, '{\"sku_order_num\":\"2\"}')";
String sql10 = "update seller_spu_site_1976 set gmt_modified = NOW(), spu_id = 0, site_id = null, site_en = null, features = null, outer_id = null, auction_status = -2 where ((sellerid = 2027808696) AND (item_id = 552891941272))";
String sql11 = "select seller_item_sku.item_id,seller_item_sku.gmt_modified,seller_item_sku.gmt_create,seller_item_sku.sku_id,seller_item_sku.seller_id,seller_item_sku.outer_id,seller_item_sku.status,seller_item_sku.sync_version,seller_item_sku.sku_feature from seller_item_sku_2047 seller_item_sku where ((seller_item_sku.item_id = 558182960563) AND (seller_item_sku.seller_id = 673619967) AND (seller_item_sku.sku_id = 3634115286014))";
String sql12 = "/* 0ba9776a15048393743937000d1936/0//7d21f1f2/ */select `auction_relation`.`id`,`auction_relation`.`item_id`,`auction_relation`.`sku_id`,`auction_relation`.`user_id`,`auction_relation`.`target_id`,`auction_relation`.`extra_id`,`auction_relation`.`type`,`auction_relation`.`target_type`,`auction_relation`.`type_attr`,`auction_relation`.`status`,`auction_relation`.`target_user_id`,`auction_relation`.`options`,`auction_relation`.`features`,`auction_relation`.`version`,`auction_relation`.`sub_type`,`auction_relation`.`gmt_create`,`auction_relation`.`gmt_modified` from `auction_relation_1080` `auction_relation` where ((`auction_relation`.`item_id` = 556887226360) AND (`auction_relation`.`target_Type` IN (1,2,3,4)) AND (`auction_relation`.`status` = 0) AND (`auction_relation`.`type` IN (16,17,6,7,8,9,10,11,12,13,14,15)))";
for (int i = 0; i < 5; ++i) {
// perf(sql); // 6740 6201 4752 4514 4391 4218 4127 4124
perf(sql); // 6740 6201 4752 4514 4391 4218 4127 4124
// perf(sql2); // 2948 2928 2869 2780 2502
// perf(sql3); // 15093 10392 10416 10154 10007 9126 8907
// perf(sql4); // 4429 4190 4023 3747
// perf(sql5); // 1917
// perf(sql5); // 1917 1882
// perf(sql6); // 4193
// perf(sql8); // 601 585 314
// perf(sql9); // 2403 2392 2188 2169
// perf(sql10); // 3163 2866
// perf_parse(sql); // 4643 4377 4345 3801 3627 3228 2961 2959
// perf_parse(sql2); // 1918 1779 1666 1646
@@ -65,6 +73,10 @@ public class MySqlParameterizedBenchmark extends TestCase {
// perf_parse(sql5); // 1339
// perf_parse(sql6);
// perf_parse(sql7); // 9831 8581 8552
// perf_parse(sql8); //
// perf_parse(sql10); // 2291 2025 2024 2010
// perf_parse(sql11); // 3209
// perf_parse(sql12); // 4873 4767
// perf_lexer(sql4); // 2051 1802
// perf_lexer(sql5); // 1125 1054
@@ -72,7 +84,7 @@ public class MySqlParameterizedBenchmark extends TestCase {
// perfParameterized(sql6); // 4224 4083
perfFormat(sql7); // 14865 14132 13812 13714 12917
// perfFormat(sql7); // 14865 14132 13812 13714 12917
// perf_hashCode64(sql5); // 181
@@ -2,11 +2,17 @@ package com.alibaba.druid.benckmark.sql;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.parser.LayoutCharacters;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLParserFeature;
import com.alibaba.druid.sql.parser.Token;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
@@ -56,9 +62,22 @@ public class SqlHolder {
if (parsed) {
return;
}
if (text.equals("select @@session.tx_read_only")) {
SQLSelect select = new SQLSelect();
MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock();
queryBlock.addSelectItem(new SQLPropertyExpr(new SQLVariantRefExpr("@@session"), "tx_read_only"));
select.setQuery(queryBlock);
ast = new SQLSelectStatement(select);
parsed = true;
return;
}
// ast = SQLUtils.parseStatements(text, dialect).get(0);
SQLParserFeature[] features = {SQLParserFeature.EnableSQLBinaryOpExprGroup, SQLParserFeature.OptimizedForParameterized};
try {
ast = new MySqlStatementParser(text, SQLParserFeature.EnableSQLBinaryOpExprGroup).parseStatement();
ast = new MySqlStatementParser(text, features).parseStatement();
} catch (ParserException e) {
throw new UnsupportedOperationException(e);
}
@@ -98,6 +117,11 @@ public class SqlHolder {
public String parameterize(Set<String> physicalNames, List<Object> params) {
ensureParsed();
if (text.equals("select @@session.tx_read_only")) {
return text;
}
return Templates.parameterize(ast, physicalNames, params);
}
@@ -41,7 +41,7 @@ public class MySqlParameterizedOutputVisitorTest_41 extends TestCase {
String psql = out.toString();
assertEquals("SELECT *\n" +
"FROM db_00.t\n" +
"FROM db.t\n" +
"WHERE id > ?", psql);
String params_json = JSONArray.toJSONString(parameters, SerializerFeature.WriteClassName);
System.out.println(params_json);
@@ -51,7 +51,7 @@ public class MySqlParameterizedOutputVisitorTest_41 extends TestCase {
String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
assertEquals("SELECT *\n" +
"FROM db_00.t\n" +
"FROM db.t\n" +
"WHERE id > 1", rsql);
}
}
@@ -44,77 +44,77 @@ public class MySqlParameterizedOutputVisitorTest_46 extends TestCase {
assertEquals("SELECT COUNT(*) AS count, `ipv_uv_1d_001` AS col\n" +
"FROM (\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
"\tUNION ALL\n" +
"\t(SELECT ipv_uv_1d_001\n" +
"\tFROM `cbu_da_dihu_16`.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tFROM cbu_da_dihu.ads_tb_sycm_eff_slr_itm_1d_s015_p\n" +
"\tWHERE `auto_seq_id` > ?\n" +
"\tLIMIT ?)\n" +
") ads_tb_sycm_eff_slr_itm_1d_s015_p033\n" +
@@ -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_51_schema extends TestCase {
public void test_for_parameterize() throws Exception {
final String dbType = JdbcConstants.MYSQL;
String sql = "UPDATE `galicdb_0102`.auction_auctions_0201 SET `starts` = ?, `pict_url` = ?, `category` = ?, `minimum_bid` = ?, `reserve_price` = ?, `city` = ?, `prov` = ?, `ends` = ?, `current_bid` = NULL, `quantity` = ?, `zoo` = ?, `secure_trade_ordinary_post_fee` = ?, `secure_trade_fast_post_fee` = ?, `old_quantity` = ?, `options` = ?, `secure_trade_ems_post_fee` = ?, `property` = ?, `last_modified` = ?, `desc_path` = ?, `postage_id` = ?, `shop_categories_id_lists` = ?, `spu_id` = ?, `sync_version` = ?, `auction_status` = ?, `features` = ?, `feature_cc` = ?, `main_color` = ?, `outer_id` = ?, `auction_sub_status` = ?, `commodity_id` = ? WHERE `auction_id` = ?";
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("UPDATE galicdb.auction_auctions\n" +
"SET `starts` = ?, `pict_url` = ?, `category` = ?, `minimum_bid` = ?, `reserve_price` = ?, `city` = ?, `prov` = ?, `ends` = ?, `current_bid` = NULL, `quantity` = ?, `zoo` = ?, `secure_trade_ordinary_post_fee` = ?, `secure_trade_fast_post_fee` = ?, `old_quantity` = ?, `options` = ?, `secure_trade_ems_post_fee` = ?, `property` = ?, `last_modified` = ?, `desc_path` = ?, `postage_id` = ?, `shop_categories_id_lists` = ?, `spu_id` = ?, `sync_version` = ?, `auction_status` = ?, `features` = ?, `feature_cc` = ?, `main_color` = ?, `outer_id` = ?, `auction_sub_status` = ?, `commodity_id` = ?\n" +
"WHERE `auction_id` = ?", psql);
}
}
@@ -0,0 +1,100 @@
/*
* 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_58_alias_dot extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT m.*, m.icon AS micon, md.uid as md.uid, md.lastmsg,md.postnum,md.rvrc,md.money,md.credit,md.currency,md.lastvisit,md.thisvisit,md.onlinetime,md.lastpost,md.todaypost, md.monthpost,md.onlineip,md.uploadtime,md.uploadnum,md.starttime,md.pwdctime,md.monoltime,md.digests,md.f_num,md.creditpop, md.jobnum,md.lastgrab,md.follows,md.fans,md.newfans,md.newreferto,md.newcomment,md.postcheck,md.punch, mi.customdata " +
"FROM pw_members m LEFT JOIN pw_memberdata md ON m.uid=md.uid LEFT JOIN pw_memberinfo mi ON mi.uid=m.uid WHERE m.uid IN (?)";
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 m.*, m.icon AS micon, md.uid AS md.uid, md.lastmsg, md.postnum\n" +
"\t, md.rvrc, md.money, md.credit, md.currency, md.lastvisit\n" +
"\t, md.thisvisit, md.onlinetime, md.lastpost, md.todaypost, md.monthpost\n" +
"\t, md.onlineip, md.uploadtime, md.uploadnum, md.starttime, md.pwdctime\n" +
"\t, md.monoltime, md.digests, md.f_num, md.creditpop, md.jobnum\n" +
"\t, md.lastgrab, md.follows, md.fans, md.newfans, md.newreferto\n" +
"\t, md.newcomment, md.postcheck, md.punch, mi.customdata\n" +
"FROM pw_members m\n" +
"\tLEFT JOIN pw_memberdata md ON m.uid = md.uid\n" +
"\tLEFT JOIN pw_memberinfo mi ON mi.uid = m.uid\n" +
"WHERE m.uid IN (?)", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select m.*, m.icon as micon, md.uid as md.uid, md.lastmsg, md.postnum\n" +
"\t, md.rvrc, md.money, md.credit, md.currency, md.lastvisit\n" +
"\t, md.thisvisit, md.onlinetime, md.lastpost, md.todaypost, md.monthpost\n" +
"\t, md.onlineip, md.uploadtime, md.uploadnum, md.starttime, md.pwdctime\n" +
"\t, md.monoltime, md.digests, md.f_num, md.creditpop, md.jobnum\n" +
"\t, md.lastgrab, md.follows, md.fans, md.newfans, md.newreferto\n" +
"\t, md.newcomment, md.postcheck, md.punch, mi.customdata\n" +
"from pw_members m\n" +
"\tleft join pw_memberdata md on m.uid = md.uid\n" +
"\tleft join pw_memberinfo mi on mi.uid = m.uid\n" +
"where m.uid in (?)", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
assertEquals("SELECT m.*, m.icon AS micon, md.uid AS md.uid, md.lastmsg, md.postnum\n" +
"\t, md.rvrc, md.money, md.credit, md.currency, md.lastvisit\n" +
"\t, md.thisvisit, md.onlinetime, md.lastpost, md.todaypost, md.monthpost\n" +
"\t, md.onlineip, md.uploadtime, md.uploadnum, md.starttime, md.pwdctime\n" +
"\t, md.monoltime, md.digests, md.f_num, md.creditpop, md.jobnum\n" +
"\t, md.lastgrab, md.follows, md.fans, md.newfans, md.newreferto\n" +
"\t, md.newcomment, md.postcheck, md.punch, mi.customdata\n" +
"FROM pw_members m\n" +
"\tLEFT JOIN pw_memberdata md ON m.uid = md.uid\n" +
"\tLEFT JOIN pw_memberinfo mi ON mi.uid = m.uid\n" +
"WHERE m.uid IN (?)", //
output);
}
}
}
@@ -0,0 +1,75 @@
/*
* 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_59_neq extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT * from t where t ! = 1";
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 *\n" +
"FROM t\n" +
"WHERE t != 1", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select *\n" +
"from t\n" +
"where t != 1", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
assertEquals("SELECT *\n" +
"FROM t\n" +
"WHERE t != ?", //
output);
}
}
}
@@ -70,7 +70,7 @@ import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleMultiInsertStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleMultiInsertStatement.ConditionalInsertClause;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleMultiInsertStatement.ConditionalInsertClauseItem;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleMultiInsertStatement.InsertIntoClause;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OraclePLSQLCommitStatement;
import com.alibaba.druid.sql.ast.statement.SQLScriptCommitStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OraclePrimaryKey;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectPivot;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock;
@@ -89,7 +89,7 @@ public class OracleASTVisitorAdapterTest extends TestCase {
public void test_adapter() throws Exception {
OracleASTVisitorAdapter adapter = new OracleASTVisitorAdapter();
new OraclePLSQLCommitStatement().accept(adapter);
new SQLScriptCommitStatement().accept(adapter);
new OracleAnalytic().accept(adapter);
new OracleAnalyticWindowing().accept(adapter);
new SQLDateExpr().accept(adapter);
@@ -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.sqlserver;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import org.junit.Assert;
import java.util.List;
public class SQLServerSelectTest26 extends TestCase {
public void test_simple() throws Exception {
String sql = //
"-- ----------------------------\n" +
"-- Table structure for Adode\n" +
"-- ----------------------------\n" +
"DROP TABLE [dbo].[Adode]\n" +
"GO\n" +
"CREATE TABLE [dbo].[Adode] (\n" +
"[Ad_Work_Start_Time] varchar(255) NULL,\n" +
"[Ad_Work_Stop_Time] varchar(255) NULL,\n" +
"[Ad_Wait_Start_Time] varchar(255) NULL,\n" +
"[Ad_Wait_Stop_Time] varchar(255) NULL,\n" +
"[Order_ID] varchar(255) NOT NULL\n" +
")"; //
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.SQL_SERVER);
{
String text = SQLUtils.toSQLString(stmtList, JdbcConstants.SQL_SERVER);
Assert.assertEquals("DROP TABLE [dbo].[Adode]\n" +
"GO\n" +
"CREATE TABLE [dbo].[Adode] (\n" +
"\t[Ad_Work_Start_Time] varchar(255) NULL,\n" +
"\t[Ad_Work_Stop_Time] varchar(255) NULL,\n" +
"\t[Ad_Wait_Start_Time] varchar(255) NULL,\n" +
"\t[Ad_Wait_Stop_Time] varchar(255) NULL,\n" +
"\t[Order_ID] varchar(255) NOT NULL\n" +
")", text);
}
{
String text = SQLUtils.toSQLString(stmtList, JdbcConstants.SQL_SERVER, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("drop table [dbo].[Adode]\n" +
"go\n" +
"create table [dbo].[Adode] (\n" +
"\t[Ad_Work_Start_Time] varchar(255) null,\n" +
"\t[Ad_Work_Stop_Time] varchar(255) null,\n" +
"\t[Ad_Wait_Start_Time] varchar(255) null,\n" +
"\t[Ad_Wait_Stop_Time] varchar(255) null,\n" +
"\t[Order_ID] varchar(255) not null\n" +
")", text);
}
}
}