Skip to content

Commit

Permalink
继续优化括号逻辑,
Browse files Browse the repository at this point in the history
继续优化括号逻辑,还剩44个单测未验证和调整
  • Loading branch information
lizongbo committed May 1, 2024
1 parent a703f00 commit 371ffa1
Show file tree
Hide file tree
Showing 20 changed files with 172 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -535,38 +535,8 @@ public boolean visit(SQLBetweenExpr x) {
if (x.isParenthesized()) {
print('(');
}

boolean quote = false;
if (testExpr instanceof SQLBinaryOpExpr) {
SQLBinaryOperator operator = ((SQLBinaryOpExpr) testExpr).getOperator();
switch (operator) {
case BooleanAnd:
case BooleanOr:
case BooleanXor:
case Assignment:
quote = true;
break;
default:
quote = false;
break;
}
} else if (testExpr instanceof SQLInListExpr
|| testExpr instanceof SQLBetweenExpr
|| testExpr instanceof SQLNotExpr
|| testExpr instanceof SQLUnaryExpr
|| testExpr instanceof SQLCaseExpr
|| testExpr instanceof SQLBinaryOpExprGroup) {
quote = true;
}

if (testExpr != null) {
if (quote) {
print('(');
printExpr(testExpr, parameterized);
print(')');
} else {
printExpr(testExpr, parameterized);
}
printExpr(testExpr, parameterized);
}

if (x.isNot()) {
Expand Down Expand Up @@ -620,11 +590,7 @@ public boolean visit(SQLBetweenExpr x) {
printExpr(endExpr, parameterized);
}
decrementIndent();
} else if (endExpr instanceof SQLInListExpr
|| endExpr instanceof SQLBetweenExpr
|| endExpr instanceof SQLNotExpr
|| endExpr instanceof SQLUnaryExpr
|| endExpr instanceof SQLCaseExpr
} else if (endExpr instanceof SQLNotExpr
|| endExpr instanceof SQLBinaryOpExprGroup) {
print('(');
printExpr(endExpr, parameterized);
Expand Down Expand Up @@ -1019,7 +985,7 @@ private void visitorBinaryRight(SQLBinaryOpExpr x) {
indentCount--;
}
} else if (SQLBinaryOperator.Equality.priority >= op.priority
&& (right instanceof SQLInListExpr || right instanceof SQLNotExpr)) {
&& (right instanceof SQLNotExpr)) {
indentCount++;
print('(');
printExpr(right, parameterized);
Expand Down Expand Up @@ -1101,6 +1067,9 @@ private void visitBinaryLeft(SQLExpr left, SQLBinaryOperator op) {
} else {
quote = op.priority < SQLBinaryOperator.Equality.priority;
}
if (inListExpr.isParenthesized()) {
quote = false;
}
if (quote) {
print('(');
}
Expand Down Expand Up @@ -1367,6 +1336,10 @@ public boolean visit(SQLCharExpr x) {
}

public boolean visit(SQLCharExpr x, boolean parameterized) {
if (x.isParenthesized()) {
print('(');
print("TTTQQQQ");
}
if (parameterized) {
print('?');
incrementReplaceCunt();
Expand All @@ -1377,7 +1350,10 @@ public boolean visit(SQLCharExpr x, boolean parameterized) {
}

printChars(x.getText());

if (x.isParenthesized()) {
print(')');
print("TTTQQQQ");
}
return false;
}

Expand Down Expand Up @@ -1478,7 +1454,13 @@ public boolean visit(SQLExistsExpr x) {
}

public boolean visit(SQLIdentifierExpr x) {
if (x.isParenthesized()) {
print('(');
}
printName0(x.getName());
if (x.isParenthesized()) {
print(')');
}
return false;
}

Expand Down Expand Up @@ -1571,6 +1553,9 @@ private boolean printName(SQLName x, String name, boolean shardingSupport) {
}

public boolean visit(SQLInListExpr x) {
if (x.isParenthesized()) {
print('(');
}
final SQLExpr expr = x.getExpr();

boolean quote = false;
Expand Down Expand Up @@ -1696,18 +1681,20 @@ public boolean visit(SQLInListExpr x) {
if (x.getHint() != null) {
x.getHint().accept(this);
}

if (x.isParenthesized()) {
print(')');
}
return false;
}
}

if (quote) {
print('(');
}
//if (quote) {
//print('(');
//}
printExpr(expr, parameterized);
if (quote) {
print(')');
}
//if (quote) {
//print(')');
//}

if (x.isNot()) {
print0(ucase ? " NOT IN (" : " not in (");
Expand Down Expand Up @@ -1762,6 +1749,9 @@ public boolean visit(SQLInListExpr x) {
if (x.getHint() != null) {
x.getHint().accept(this);
}
if (x.isParenthesized()) {
print(')');
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void test_7() throws Exception {

String text = output(stmtList);

Assert.assertEquals("SELECT (!1) + 1;", text);
Assert.assertEquals("SELECT !1 + 1;", text);
}

public void test_8() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ public void test_insert_0() throws Exception {
"\tFROM sales_order t1\n" +
"\t\tJOIN (\n" +
"\t\t\tSELECT t2_1.warehouse_id, t2_1.external_batch_code\n" +
"\t\t\t\t, CASE \n" +
"\t\t\t\t, (CASE \n" +
"\t\t\t\t\tWHEN t2_2.operation_type = 2\n" +
"\t\t\t\t\t\tOR t2_2.operation_type IS NULL\n" +
"\t\t\t\t\tTHEN '2'\n" +
"\t\t\t\t\tWHEN t2_2.operation_type = 3 THEN '800'\n" +
"\t\t\t\t\tWHEN t2_2.operation_type = 1 THEN '900'\n" +
"\t\t\t\tEND AS businessType\n" +
"\t\t\t\tEND) AS businessType\n" +
"\t\t\tFROM batch_order t2_1\n" +
"\t\t\t\tJOIN wave_order t2_2\n" +
"\t\t\t\tON t2_1.wave_id = t2_2.id\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@ public void test_for_parameterize() throws Exception {
" ,`a1`.`airline`,`a1`.`params_stat_id`,`a1`.`total_num`,`a1`.`finish_num`,`a1`.`type_idx_key`" +
" ,`a1`.`seqno`,`a1`.`task_flag`,`a1`.`tariff` " +
"from `xx_abcde_ta_0018` `a1` " +
"where ((`a1`.`push_date` = '2017-01-19 00:00:00') AND (`a1`.`schedule_no` <= '201701181201') AND (`a1`.`type` IN (1,4,2,3,7,8,11,12,13,14,15,16)) AND (`a1`.`retry_count` < 3) AND (`a1`.`status` IN (3,6)) AND (`a1`.`gmt_modified` <= DATE_ADD(NOW(),INTERVAL -(5) MINUTE))) limit 0,2000";
"where ((`a1`.`push_date` = '2017-01-19 00:00:00') "
+ "AND (`a1`.`schedule_no` <= '201701181201') "
+ "AND (`a1`.`type` IN (1,4,2,3,7,8,11,12,13,14,15,16))"
+ " AND (`a1`.`retry_count` < 3)"
+ " AND (`a1`.`status` IN (3,6)) "
+ "AND (`a1`.`gmt_modified` <= DATE_ADD(NOW(),INTERVAL -(5) MINUTE))) limit 0,2000";

List<SQLStatement> stmtList111 = SQLUtils.parseStatements(sql, dbType);
SQLStatement stmt111 = stmtList111.get(0);
System.out.println(stmt111.toString());
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);

assertEquals("SELECT `a1`.`id`, `a1`.`gmt_create`, `a1`.`gmt_modified`, `a1`.`push_date`, `a1`.`parent_task_id`\n" +
"\t, `a1`.`parent_task_type`, `a1`.`action_type`, `a1`.`schedule_no`, `a1`.`type`, `a1`.`md5`\n" +
"\t, `a1`.`message_content`, `a1`.`retry_count`, `a1`.`level`, `a1`.`extra`, `a1`.`status`\n" +
Expand All @@ -39,9 +48,9 @@ public void test_for_parameterize() throws Exception {
"FROM xx_abcde_ta `a1`\n" +
"WHERE (`a1`.`push_date` = ?)\n" +
"\tAND (`a1`.`schedule_no` <= ?)\n" +
"\tAND `a1`.`type` IN (?)\n" +
"\tAND (`a1`.`type` IN (?))\n" +
"\tAND (`a1`.`retry_count` < ?)\n" +
"\tAND `a1`.`status` IN (?)\n" +
"\tAND (`a1`.`status` IN (?))\n" +
"\tAND (`a1`.`gmt_modified` <= DATE_ADD(NOW(), INTERVAL -? MINUTE))\n" +
"LIMIT ?, ?", psql);

Expand Down Expand Up @@ -82,9 +91,9 @@ public void test_for_parameterize() throws Exception {
"FROM xx_abcde_ta_0018 `a1`\n" +
"WHERE (`a1`.`push_date` = '2017-01-19 00:00:00')\n" +
"\tAND (`a1`.`schedule_no` <= '201701181201')\n" +
"\tAND `a1`.`type` IN (1, 4, 2, 3, 7, 8, 11, 12, 13, 14, 15, 16)\n" +
"\tAND (`a1`.`type` IN (1, 4, 2, 3, 7, 8, 11, 12, 13, 14, 15, 16))\n" +
"\tAND (`a1`.`retry_count` < 3)\n" +
"\tAND `a1`.`status` IN (3, 6)\n" +
"\tAND (`a1`.`status` IN (3, 6))\n" +
"\tAND (`a1`.`gmt_modified` <= DATE_ADD(NOW(), INTERVAL -5 MINUTE))\n" +
"LIMIT 0, 2000", buf.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,38 @@ public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;

String sql = "/* 0a67bca314863468702364451e/0.3// */select `udata`.`id` as `id`,`udata`.`gmt_create` as `gmtCreate`,`udata`.`gmt_modified` as `gmtModified`,`udata`.`uid` as `userId`,`udata`.`user_nick` as `userNick`,`udata`.`user_type` as `userType`,`udata`.`aps` as `acPeSe`,`udata`.`rn` as `rn`,`udata`.`start_period_time` as `startPeriodTime`,`udata`.`ept` as `adTm`,`udata`.`status` as `status`,`udata`.`charging_period` as `chargingPeriod`,`udata`.`sn` as `sn`,`udata`.`cpd` as `chargingPeriodDesc`,`udata`.`task_total_num` as `taskTotalNum`,`udata`.`tcn` as `taCoNu`,`udata`.`task_type` as `taskType`,`udata`.`ilbu` as `isLaBiUs`" +
" from `udata_0888` `udata` where ((`udata`.`id` IN ((select MAX(`udata`.`id`) from `udata_0888` `udata` where ((`udata`.`uid` = 1039100792) AND (`udata`.`user_type` = 2) AND (`udata`.`start_period_time` <= '2017-01-01 00:00:00') AND (`udata`.`status` = 10) AND (`udata`.`charging_period` = 1) AND (`udata`.`task_type` = 1) AND (`udata`.`task_total_num` <= `udata`.`tcn`)) group by `udata`.`charging_period`,`udata`.`start_period_time`,`udata`.`ept`))) AND ((`udata`.`uid` = '1039100792') AND (`udata`.`user_type` = 2))) order by `udata`.`start_period_time` desc limit 0,6";
" from `udata_0888` `udata` "
+ "where ((`udata`.`id` IN ((select MAX(`udata`.`id`) from `udata_0888` `udata` "
+ "where ((`udata`.`uid` = 1039100792) "
+ "AND (`udata`.`user_type` = 2) "
+ "AND (`udata`.`start_period_time` <= '2017-01-01 00:00:00') "
+ "AND (`udata`.`status` = 10) "
+ "AND (`udata`.`charging_period` = 1) "
+ "AND (`udata`.`task_type` = 1) "
+ "AND (`udata`.`task_total_num` <= `udata`.`tcn`)) group by `udata`.`charging_period`,`udata`.`start_period_time`,`udata`.`ept`))) "
+ "AND ((`udata`.`uid` = '1039100792') AND (`udata`.`user_type` = 2))) "
+ "order by `udata`.`start_period_time` desc limit 0,6";

String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
assertEquals("SELECT `udata`.`id` AS `id`, `udata`.`gmt_create` AS `gmtCreate`, `udata`.`gmt_modified` AS `gmtModified`, `udata`.`uid` AS `userId`, `udata`.`user_nick` AS `userNick`\n" +
"\t, `udata`.`user_type` AS `userType`, `udata`.`aps` AS `acPeSe`, `udata`.`rn` AS `rn`, `udata`.`start_period_time` AS `startPeriodTime`, `udata`.`ept` AS `adTm`\n" +
"\t, `udata`.`status` AS `status`, `udata`.`charging_period` AS `chargingPeriod`, `udata`.`sn` AS `sn`, `udata`.`cpd` AS `chargingPeriodDesc`, `udata`.`task_total_num` AS `taskTotalNum`\n" +
"\t, `udata`.`tcn` AS `taCoNu`, `udata`.`task_type` AS `taskType`, `udata`.`ilbu` AS `isLaBiUs`\n" +
"FROM udata `udata`\n" +
"WHERE `udata`.`id` IN (\n" +
"WHERE (`udata`.`id` IN (\n" +
"\t\tSELECT MAX(`udata`.`id`)\n" +
"\t\tFROM udata `udata`\n" +
"\t\tWHERE `udata`.`uid` = ?\n" +
"\t\t\tAND `udata`.`user_type` = ?\n" +
"\t\t\tAND `udata`.`start_period_time` <= ?\n" +
"\t\t\tAND `udata`.`status` = ?\n" +
"\t\t\tAND `udata`.`charging_period` = ?\n" +
"\t\t\tAND `udata`.`task_type` = ?\n" +
"\t\t\tAND `udata`.`task_total_num` <= `udata`.`tcn`\n" +
"\t\tWHERE (`udata`.`uid` = ?)\n" +
"\t\t\tAND (`udata`.`user_type` = ?)\n" +
"\t\t\tAND (`udata`.`start_period_time` <= ?)\n" +
"\t\t\tAND (`udata`.`status` = ?)\n" +
"\t\t\tAND (`udata`.`charging_period` = ?)\n" +
"\t\t\tAND (`udata`.`task_type` = ?)\n" +
"\t\t\tAND (`udata`.`task_total_num` <= `udata`.`tcn`)\n" +
"\t\tGROUP BY `udata`.`charging_period`, `udata`.`start_period_time`, `udata`.`ept`\n" +
"\t)\n" +
"\tAND (`udata`.`uid` = ?\n" +
"\t\tAND `udata`.`user_type` = ?)\n" +
"\tAND ((`udata`.`uid` = ?)\n" +
"\t\tAND (`udata`.`user_type` = ?)))\n" +
"ORDER BY `udata`.`start_period_time` DESC\n" +
"LIMIT ?, ?", psql);

Expand Down Expand Up @@ -79,20 +89,20 @@ public void test_for_parameterize() throws Exception {
"\t, `udata`.`status` AS `status`, `udata`.`charging_period` AS `chargingPeriod`, `udata`.`sn` AS `sn`, `udata`.`cpd` AS `chargingPeriodDesc`, `udata`.`task_total_num` AS `taskTotalNum`\n" +
"\t, `udata`.`tcn` AS `taCoNu`, `udata`.`task_type` AS `taskType`, `udata`.`ilbu` AS `isLaBiUs`\n" +
"FROM udata_0888 `udata`\n" +
"WHERE `udata`.`id` IN (\n" +
"WHERE (`udata`.`id` IN (\n" +
"\t\tSELECT MAX(`udata`.`id`)\n" +
"\t\tFROM udata_0888 `udata`\n" +
"\t\tWHERE `udata`.`uid` = 1039100792\n" +
"\t\t\tAND `udata`.`user_type` = 2\n" +
"\t\t\tAND `udata`.`start_period_time` <= '2017-01-01 00:00:00'\n" +
"\t\t\tAND `udata`.`status` = 10\n" +
"\t\t\tAND `udata`.`charging_period` = 1\n" +
"\t\t\tAND `udata`.`task_type` = 1\n" +
"\t\t\tAND `udata`.`task_total_num` <= `udata`.`tcn`\n" +
"\t\tWHERE (`udata`.`uid` = 1039100792)\n" +
"\t\t\tAND (`udata`.`user_type` = 2)\n" +
"\t\t\tAND (`udata`.`start_period_time` <= '2017-01-01 00:00:00')\n" +
"\t\t\tAND (`udata`.`status` = 10)\n" +
"\t\t\tAND (`udata`.`charging_period` = 1)\n" +
"\t\t\tAND (`udata`.`task_type` = 1)\n" +
"\t\t\tAND (`udata`.`task_total_num` <= `udata`.`tcn`)\n" +
"\t\tGROUP BY `udata`.`charging_period`, `udata`.`start_period_time`, `udata`.`ept`\n" +
"\t)\n" +
"\tAND (`udata`.`uid` = '1039100792'\n" +
"\t\tAND `udata`.`user_type` = 2)\n" +
"\tAND ((`udata`.`uid` = '1039100792')\n" +
"\t\tAND (`udata`.`user_type` = 2)))\n" +
"ORDER BY `udata`.`start_period_time` DESC\n" +
"LIMIT 0, 6", buf.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,25 @@
public class MySqlParameterizedOutputVisitorTest_56 extends TestCase {
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "/* 0b802ab415058233338893940e1882/0.1.2.23//abd5b625/ */select `ktv_resource`.`RESOURCE_ID`,`ktv_resource`.`RESOURCE_PROVIDER`,`ktv_resource`.`KTV_ID`,`ktv_resource`.`RESOURCE_TYPE`,`ktv_resource`.`SUB_RESOURCE_TYPE`,`ktv_resource`.`STATUS`,`ktv_resource`.`START_TIME`,`ktv_resource`.`END_TIME`,`ktv_resource`.`FEATURE`,`ktv_resource`.`GMT_CREATED`,`ktv_resource`.`GMT_MODIFIED`,`ktv_resource`.`source`,`ktv_resource`.`seller_id`,`ktv_resource`.`original_Resource_Id`,`ktv_resource`.`business_unit`,`ktv_resource`.`resource_code`,`ktv_resource`.`OPTIONS`,`ktv_resource`.`AVAILABLE_COUNT`,`ktv_resource`.`TOTAL_COUNT`,`ktv_resource`.`OUT_INSTANCE_ID`,`ktv_resource`.`CONSUME_ID`,`ktv_resource`.`GROUP_ID`,`ktv_resource`.`BUSINESS_ID`,`ktv_resource`.`rule`,`ktv_resource`.`market_place`,`ktv_resource`.`VERSION` from `ktv_resource_0062` `ktv_resource` where ((`ktv_resource`.`KTV_ID` = 880693310) AND (`ktv_resource`.`STATUS` = 1) AND (`ktv_resource`.`START_TIME` <= '2017-09-19 20:15:34.199') AND (`ktv_resource`.`END_TIME` >= '2017-09-19 20:15:34.199') AND (`ktv_resource`.`seller_id` IN (2680068332)) AND (`ktv_resource`.`AVAILABLE_COUNT` IS NULL OR (`ktv_resource`.`AVAILABLE_COUNT` > 0) OR (`ktv_resource`.`AVAILABLE_COUNT` = -1))) limit 0,30";
String sql = "/* 0b802ab415058233338893940e1882/0.1.2.23//abd5b625/ */"
+ "select `ktv_resource`.`RESOURCE_ID`,`ktv_resource`.`RESOURCE_PROVIDER`,"
+ "`ktv_resource`.`KTV_ID`,`ktv_resource`.`RESOURCE_TYPE`,`ktv_resource`.`SUB_RESOURCE_TYPE`,"
+ "`ktv_resource`.`STATUS`,`ktv_resource`.`START_TIME`,`ktv_resource`.`END_TIME`,"
+ "`ktv_resource`.`FEATURE`,`ktv_resource`.`GMT_CREATED`,`ktv_resource`.`GMT_MODIFIED`,"
+ "`ktv_resource`.`source`,`ktv_resource`.`seller_id`,`ktv_resource`.`original_Resource_Id`,"
+ "`ktv_resource`.`business_unit`,`ktv_resource`.`resource_code`,`ktv_resource`.`OPTIONS`,"
+ "`ktv_resource`.`AVAILABLE_COUNT`,`ktv_resource`.`TOTAL_COUNT`,`ktv_resource`.`OUT_INSTANCE_ID`,"
+ "`ktv_resource`.`CONSUME_ID`,`ktv_resource`.`GROUP_ID`,`ktv_resource`.`BUSINESS_ID`,"
+ "`ktv_resource`.`rule`,`ktv_resource`.`market_place`,`ktv_resource`.`VERSION` "
+ "from `ktv_resource_0062` `ktv_resource` where "
+ "((`ktv_resource`.`KTV_ID` = 880693310) "
+ "AND (`ktv_resource`.`STATUS` = 1) "
+ "AND (`ktv_resource`.`START_TIME` <= '2017-09-19 20:15:34.199') "
+ "AND (`ktv_resource`.`END_TIME` >= '2017-09-19 20:15:34.199') "
+ "AND (`ktv_resource`.`seller_id` IN (2680068332)) "
+ "AND (`ktv_resource`.`AVAILABLE_COUNT` IS NULL "
+ "OR (`ktv_resource`.`AVAILABLE_COUNT` > 0) "
+ "OR (`ktv_resource`.`AVAILABLE_COUNT` = -1))) limit 0,30";

SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
Expand Down Expand Up @@ -55,7 +73,7 @@ public void test_for_parameterize() throws Exception {
+ "\tAND (`ktv_resource`.`STATUS` = ?)\n"
+ "\tAND (`ktv_resource`.`START_TIME` <= ?)\n"
+ "\tAND (`ktv_resource`.`END_TIME` >= ?)\n"
+ "\tAND `ktv_resource`.`seller_id` IN (?)\n"
+ "\tAND (`ktv_resource`.`seller_id` IN (?))\n"
+ "\tAND (`ktv_resource`.`AVAILABLE_COUNT` IS NULL\n"
+ "\t\tOR (`ktv_resource`.`AVAILABLE_COUNT` > ?)\n"
+ "\t\tOR (`ktv_resource`.`AVAILABLE_COUNT` = ?)))\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.fastjson2.JSON;
Expand All @@ -15,6 +16,10 @@ public class MySqlParameterizedOutputVisitorTest_69 extends TestCase {
public void test_in() throws Exception {
String sql = "select ((0='x6') & 31) ^ (ROW(76, 4) NOT IN (ROW(1, 2 ),ROW(3, 4)) );";

List<SQLStatement> stmtList111 = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
SQLStatement stmt111 = stmtList111.get(0);
System.out.println(stmt111.toString());

List<Object> params = new ArrayList<Object>();
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, JdbcConstants.MYSQL, params, VisitorFeature.OutputParameterizedUnMergeShardingTable);
assertEquals("SELECT ((? = ?) & ?) ^ (ROW(?, ?) NOT IN (ROW(?, ?), ROW(?, ?)));", psql);
Expand Down
Loading

0 comments on commit 371ffa1

Please sign in to comment.