From 2659474225bc3f73efa7ae59c703d72a94b6a2e5 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 10 Dec 2016 20:16:44 +0800 Subject: [PATCH] sql parser parameterized insert_stmt_null output changed. --- .../mysql/visitor/MySqlOutputVisitor.java | 59 ----------- .../sql/visitor/SQLASTOutputVisitor.java | 100 ++++++++++++++---- ...ySqlParameterizedOutputVisitorTest_15.java | 32 ++++++ 3 files changed, 109 insertions(+), 82 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/sql/mysql/param/MySqlParameterizedOutputVisitorTest_15.java diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java index 59b8f5b58c..7a1e78704e 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java @@ -194,65 +194,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV this.shardingSupport = shardingSupport; } - public boolean visit(SQLIdentifierExpr x) { - if (!parameterized) { - return super.visit(x); - } - - final String name = x.getName(); - if (shardingSupport) { - SQLObject parent = x.getParent(); - shardingSupport = parent instanceof SQLExprTableSource || parent instanceof SQLPropertyExpr; - } - - if (shardingSupport) { - int pos = name.lastIndexOf('_'); - if (pos != -1 && pos != name.length() - 1) { - boolean quote = name.charAt(0) == '`' && name.charAt(name.length() - 1) == '`'; - boolean isNumber = true; - - int end = name.length(); - if (quote) { - end--; - } - for (int i = pos + 1; i < end; ++i) { - char ch = name.charAt(i); - if (ch < '0' || ch > '9') { - isNumber = false; - break; - } - } - if (isNumber) { - int start = quote ? 1 : 0; - String realName = name.substring(start, pos); - print0(realName); - incrementReplaceCunt(); - return false; - } - } - - int numberCount = 0; - for (int i = name.length() - 1; i >= 0; --i) { - char ch = name.charAt(i); - if (ch < '0' || ch > '9') { - break; - } else { - numberCount++; - } - } - - if (numberCount > 1) { - int numPos = name.length() - numberCount; - String realName = name.substring(0, numPos); - print0(realName); - incrementReplaceCunt(); - return false; - } - } - print0(name); - return false; - } - @Override public boolean visit(SQLSelectQueryBlock select) { if (select instanceof MySqlSelectQueryBlock) { diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java index fe12957991..5798b65234 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java @@ -522,7 +522,67 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet } public boolean visit(SQLIdentifierExpr x) { - print0(x.getName()); + final String name = x.getName(); + if (!parameterized) { + print0(x.getName()); + return false; + } + + return printName(x, name); + } + + private boolean printName(SQLName x, String name) { + boolean shardingSupport = this.shardingSupport; + if (shardingSupport) { + SQLObject parent = x.getParent(); + shardingSupport = parent instanceof SQLExprTableSource || parent instanceof SQLPropertyExpr; + } + + if (shardingSupport) { + int pos = name.lastIndexOf('_'); + if (pos != -1 && pos != name.length() - 1) { + boolean quote = name.charAt(0) == '`' && name.charAt(name.length() - 1) == '`'; + boolean isNumber = true; + + int end = name.length(); + if (quote) { + end--; + } + for (int i = pos + 1; i < end; ++i) { + char ch = name.charAt(i); + if (ch < '0' || ch > '9') { + isNumber = false; + break; + } + } + if (isNumber) { + int start = quote ? 1 : 0; + String realName = name.substring(start, pos); + print0(realName); + incrementReplaceCunt(); + return false; + } + } + + int numberCount = 0; + for (int i = name.length() - 1; i >= 0; --i) { + char ch = name.charAt(i); + if (ch < '0' || ch > '9') { + break; + } else { + numberCount++; + } + } + + if (numberCount > 1) { + int numPos = name.length() - numberCount; + String realName = name.substring(0, numPos); + print0(realName); + incrementReplaceCunt(); + return false; + } + } + print0(name); return false; } @@ -735,26 +795,15 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet } public boolean visit(SQLNullExpr x) { -// if (this.parameterized -// && ParameterizedOutputVisitorUtils.checkParameterize(x)) { -// SQLObject parent = x.getParent(); -// if (parent instanceof SQLBinaryOpExpr) { -// SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) parent; -// if (binaryOpExpr.getOperator() == SQLBinaryOperator.IsNot -// || binaryOpExpr.getOperator() == SQLBinaryOperator.Is) { -// print("NULL"); -// return false; -// } -// } -// -// print('?'); -// incrementReplaceCunt(); -// -// if(this instanceof ExportParameterVisitor || this.parameters != null){ -// ExportParameterVisitorUtils.exportParameter(this.parameters, x); -// } -// return false; -// } + if (this.parameterized + && ParameterizedOutputVisitorUtils.checkParameterize(x)) { + SQLObject parent = x.getParent(); + if (parent instanceof ValuesClause) { + print('?'); + incrementReplaceCunt(); + return false; + } + } print0(ucase ? "NULL" : "null"); return false; @@ -776,9 +825,14 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet } public boolean visit(SQLPropertyExpr x) { - x.getOwner().accept(this); + SQLExpr owner = x.getOwner(); + + owner.accept(this); print('.'); - print0(x.getName()); + + String name = x.getName(); + printName(x, name); + return false; } diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/param/MySqlParameterizedOutputVisitorTest_15.java b/src/test/java/com/alibaba/druid/bvt/sql/mysql/param/MySqlParameterizedOutputVisitorTest_15.java new file mode 100644 index 0000000000..5ddb4b30f7 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/mysql/param/MySqlParameterizedOutputVisitorTest_15.java @@ -0,0 +1,32 @@ +package com.alibaba.druid.bvt.sql.mysql.param; + +import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils; +import com.alibaba.druid.util.JdbcConstants; +import junit.framework.TestCase; + +/** + * Created by wenshao on 16/8/23. + */ +public class MySqlParameterizedOutputVisitorTest_15 extends TestCase { + public void test_for_parameterize() throws Exception { + final String dbType = JdbcConstants.MYSQL; + + String sql = "INSERT INTO mycart.`member_cart_0172` (`cart_id`, `sku_id`, `item_id`, `quantity`, `user_id`\n" + + ", `status`, `type`, `sub_type`, `gmt_create`, `gmt_modified`\n" + + ", `attribute`, `attribute_cc`, `sync_version`, `ex1`, `ex2`\n" + + ", `seller_id`, `ext_status`)\n" + + "VALUES (?, ?, ?, ?, ?\n" + + ", ?, ?, ?, ?, ?\n" + + ", ?, ?, NULL, NULL, NULL\n" + + ", ?, ?)\n"; + String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType); + assertEquals("INSERT INTO mycart.member_cart (`cart_id`, `sku_id`, `item_id`, `quantity`, `user_id`\n" + + "\t, `status`, `type`, `sub_type`, `gmt_create`, `gmt_modified`\n" + + "\t, `attribute`, `attribute_cc`, `sync_version`, `ex1`, `ex2`\n" + + "\t, `seller_id`, `ext_status`)\n" + + "VALUES (?, ?, ?, ?, ?\n" + + "\t\t, ?, ?, ?, ?, ?\n" + + "\t\t, ?, ?, ?, ?, ?\n" + + "\t\t, ?, ?)", psql); + } +}