From 6fc065e25ab698e04aaba14393921ecd2130cdd2 Mon Sep 17 00:00:00 2001 From: gaohongtao Date: Thu, 10 Nov 2016 10:35:11 +0800 Subject: [PATCH] fix #171 sharding-jdbc-core is adjusted for the feature of id-generator --- .gitignore | 2 + .../rdb/sharding/api/rule/ShardingRule.java | 70 +++++++++++++-- .../rdb/sharding/api/rule/TableRule.java | 85 ++++++++++++++++++- .../rdb/sharding/jdbc/util/ParameterList.java | 7 ++ .../rdb/sharding/parser/SQLParseEngine.java | 6 +- .../rdb/sharding/parser/SQLParserFactory.java | 8 +- .../parser/result/router/RouteContext.java | 9 +- .../sharding/parser/visitor/ParseContext.java | 32 ++++++- .../basic/mysql/AbstractMySQLVisitor.java | 2 +- .../basic/mysql/MySQLInsertVisitor.java | 48 ++++++++++- .../sharding/parser/visitor/or/OrVisitor.java | 2 +- .../sharding/router/PreparedSQLRouter.java | 10 +++ .../rdb/sharding/router/SQLRouteEngine.java | 4 +- .../router/single/SingleTableRouter.java | 2 +- .../com/dangdang/ddframe/rdb/AllTests.java | 4 +- ...stractShardingDataBasesOnlyDBUnitTest.java | 3 +- ...DataBasesOnlyForPStatementWithDMLTest.java | 22 ++++- ...gDataBasesOnlyForStatementWithDMLTest.java | 28 ++++-- .../api/fixture/ShardingRuleMockBuilder.java | 74 ++++++++++++++++ .../sharding/api/rule/ShardingRuleTest.java | 8 +- .../rdb/sharding/api/rule/TableRuleTest.java | 27 +++++- .../id/generator/AllIdGeneratorTest.java | 29 +++++++ .../generator/UnsupportedIdGeneratorTest.java | 55 ++++++++++++ .../jdbc/ShardingPreparedStatementTest.java | 48 ++++++++++- .../sharding/parser/UnsupportedParseTest.java | 11 +-- ...LPreparedStatementForOneParameterTest.java | 21 ++--- ...PreparedStatementForTowParametersTest.java | 20 +++-- .../parser/mysql/MySQLStatementTest.java | 21 ++--- .../sharding/parser/mysql/OrParseTest.java | 22 ++--- .../parser/result/SQLParsedResultTest.java | 2 +- .../prepared_statement/one_param/insert.xml | 34 ++++++++ .../parser/mysql/statement/insert.xml | 50 +++++++++++ 32 files changed, 671 insertions(+), 95 deletions(-) create mode 100644 sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/fixture/ShardingRuleMockBuilder.java create mode 100644 sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/AllIdGeneratorTest.java create mode 100644 sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/UnsupportedIdGeneratorTest.java diff --git a/.gitignore b/.gitignore index fa29380ae0cad..25a868961727c 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,5 @@ logs/ # system ignore .DS_Store Thumbs.db + +*.class diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/rule/ShardingRule.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/rule/ShardingRule.java index a1d9f0962f99c..14db90c2d80d9 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/rule/ShardingRule.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/rule/ShardingRule.java @@ -21,16 +21,19 @@ import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy; +import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException; +import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.collect.Sets; import lombok.Getter; import lombok.RequiredArgsConstructor; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; import java.util.Set; +import java.util.TreeSet; /** * 分库分表规则配置对象. @@ -84,12 +87,12 @@ public static ShardingRuleBuilder builder() { } /** - * 根据逻辑表名称查找分片规则. + * 试着根据逻辑表名称查找分片规则. * * @param logicTableName 逻辑表名称 * @return 该逻辑表的分片规则 */ - public Optional findTableRule(final String logicTableName) { + public Optional tryFindTableRule(final String logicTableName) { for (TableRule each : tableRules) { if (each.getLogicTable().equals(logicTableName)) { return Optional.of(each); @@ -98,6 +101,20 @@ public Optional findTableRule(final String logicTableName) { return Optional.absent(); } + /** + * 根据逻辑表名找到指定分片规则. + * + * @param logicTableName 逻辑表名称 + * @return 该逻辑表的分片规则 + */ + public TableRule findTableRule(final String logicTableName) { + Optional tableRuleOptional = tryFindTableRule(logicTableName); + if (tableRuleOptional.isPresent()) { + return tableRuleOptional.get(); + } + throw new ShardingJdbcException(String.format("%s does not exist in ShardingRule", logicTableName)); + } + /** * 获取数据库分片策略. * @@ -194,14 +211,17 @@ public Optional findBindingTableRule(final String logicTable) /** * 获取所有的分片列名. * + * @param tableName 表名 * @return 分片列名集合 */ - // TODO 目前使用分片列名称, 为了进一步提升解析性能,应考虑使用表名 + 列名 - public Collection getAllShardingColumns() { - Set result = new HashSet<>(); + public Collection getAllShardingColumns(final String tableName) { + Set result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); result.addAll(databaseShardingStrategy.getShardingColumns()); result.addAll(tableShardingStrategy.getShardingColumns()); for (TableRule each : tableRules) { + if (!each.getLogicTable().equalsIgnoreCase(tableName)) { + continue; + } if (null != each.getDatabaseShardingStrategy()) { result.addAll(each.getDatabaseShardingStrategy().getShardingColumns()); } @@ -212,6 +232,22 @@ public Collection getAllShardingColumns() { return result; } + /** + * 获取所有需要自增的列名. + * + * @param tableName 表名 + * @return 自增列 + */ + public Collection getAutoIncrementColumns(final String tableName) { + for (TableRule each : tableRules) { + if (!each.getLogicTable().equalsIgnoreCase(tableName)) { + continue; + } + return Sets.newLinkedHashSet(each.getAutoIncrementColumnMap().keySet()); + } + return Collections.emptySet(); + } + /** * 分片规则配置对象构建器. */ @@ -228,6 +264,8 @@ public static class ShardingRuleBuilder { private TableShardingStrategy tableShardingStrategy; + private Class idGeneratorClass; + /** * 构建数据源配置规则. * @@ -282,6 +320,17 @@ public ShardingRuleBuilder tableShardingStrategy(final TableShardingStrategy tab this.tableShardingStrategy = tableShardingStrategy; return this; } + + /** + * 构建默认id生成器. + * + * @param idGeneratorClass 默认的Id生成器 + * @return 分片规则配置对象构建器 + */ + public ShardingRuleBuilder idGenerator(final Class idGeneratorClass) { + this.idGeneratorClass = idGeneratorClass; + return this; + } /** * 构建分片规则配置对象. @@ -289,7 +338,14 @@ public ShardingRuleBuilder tableShardingStrategy(final TableShardingStrategy tab * @return 分片规则配置对象 */ public ShardingRule build() { - return new ShardingRule(dataSourceRule, tableRules, bindingTableRules, databaseShardingStrategy, tableShardingStrategy); + ShardingRule result = new ShardingRule(dataSourceRule, tableRules, bindingTableRules, databaseShardingStrategy, tableShardingStrategy); + if (null == idGeneratorClass) { + return result; + } + for (TableRule each : tableRules) { + each.fillIdGenerator(idGeneratorClass); + } + return result; } } } diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/rule/TableRule.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/rule/TableRule.java index e7d9de460adac..8460c9653b72a 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/rule/TableRule.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/rule/TableRule.java @@ -19,7 +19,9 @@ import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy; +import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator; import com.google.common.base.Preconditions; +import lombok.AccessLevel; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.ToString; @@ -27,8 +29,10 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; /** * 表规则配置对象. @@ -49,6 +53,9 @@ public final class TableRule { private final TableShardingStrategy tableShardingStrategy; + @Getter(AccessLevel.PACKAGE) + private final Map autoIncrementColumnMap = new LinkedHashMap<>(); + /** * 全属性构造器. * @@ -193,6 +200,27 @@ int findActualTableIndex(final String dataSourceName, final String actualTableNa return -1; } + void fillIdGenerator(final Class idGeneratorClass) { + for (Map.Entry each : autoIncrementColumnMap.entrySet()) { + if (null == each.getValue()) { + each.setValue(TableRuleBuilder.instanceIdGenerator(idGeneratorClass)); + } + } + } + + /** + * 生成Id. + * + * @param columnName 列名称 + * @return 生成的id + */ + public Object generateId(final String columnName) { + Object result = autoIncrementColumnMap.get(columnName).generateId(); + Preconditions.checkNotNull(result); + Preconditions.checkState(result instanceof Number || result instanceof String, "id %s(%s) should be Number or String", result.toString(), result.getClass().getName()); + return result; + } + /** * 表规则配置对象构建器. */ @@ -212,7 +240,21 @@ public static class TableRuleBuilder { private DatabaseShardingStrategy databaseShardingStrategy; private TableShardingStrategy tableShardingStrategy; + + private final Map autoIncrementColumnMap = new LinkedHashMap<>(); + + private Class tableIdGeneratorClass; + + static IdGenerator instanceIdGenerator(final Class idGeneratorClass) { + Preconditions.checkNotNull(idGeneratorClass); + try { + return idGeneratorClass.newInstance(); + } catch (final InstantiationException | IllegalAccessException e) { + throw new IllegalArgumentException(String.format("Class %s should have public privilege and no argument constructor", idGeneratorClass.getName())); + } + } + /** * 构建是否为动态表. * @@ -278,6 +320,40 @@ public TableRuleBuilder tableShardingStrategy(final TableShardingStrategy tableS this.tableShardingStrategy = tableShardingStrategy; return this; } + + /** + * 自增列. + * + * @param autoIncrementColumn 自增列名称 + * @return 规则配置对象构建器 + */ + public TableRuleBuilder autoIncrementColumns(final String autoIncrementColumn) { + this.autoIncrementColumnMap.put(autoIncrementColumn, null); + return this; + } + + /** + * 自增列. + * + * @param autoIncrementColumn 自增列名称 + * @param columnIdGeneratorClass 列Id生成器的类 + * @return 规则配置对象构建器 + */ + public TableRuleBuilder autoIncrementColumns(final String autoIncrementColumn, final Class columnIdGeneratorClass) { + this.autoIncrementColumnMap.put(autoIncrementColumn, instanceIdGenerator(columnIdGeneratorClass)); + return this; + } + + /** + * 整个表的Id生成器. + * + * @param tableIdGeneratorClass Id生成器 + * @return 规则配置对象构建器 + */ + public TableRuleBuilder tableIdGenerator(final Class tableIdGeneratorClass) { + this.tableIdGeneratorClass = tableIdGeneratorClass; + return this; + } /** * 构建表规则配置对象. @@ -285,7 +361,14 @@ public TableRuleBuilder tableShardingStrategy(final TableShardingStrategy tableS * @return 表规则配置对象 */ public TableRule build() { - return new TableRule(logicTable, dynamic, actualTables, dataSourceRule, dataSourceNames, databaseShardingStrategy, tableShardingStrategy); + TableRule result = new TableRule(logicTable, dynamic, actualTables, dataSourceRule, dataSourceNames, databaseShardingStrategy, tableShardingStrategy); + result.autoIncrementColumnMap.putAll(autoIncrementColumnMap); + if (null == tableIdGeneratorClass) { + return result; + } + result.fillIdGenerator(tableIdGeneratorClass); + return result; } + } } diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/util/ParameterList.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/util/ParameterList.java index cd58efec25964..8b6c1e0701bb8 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/util/ParameterList.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/util/ParameterList.java @@ -70,6 +70,13 @@ public final void replayMethodsInvocation(final Object target) { } } + @Override + public boolean add(final Object o) { + int index = jdbcMethodInvocations.size() + 1; + recordMethodInvocation(index, "setObject", new Class[]{int.class, Object.class}, new Object[]{index, o}); + return true; + } + /** * 根据索引设置列表中的值. * diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/SQLParseEngine.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/SQLParseEngine.java index a9fa9e3a7e1a3..054748c0f8c75 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/SQLParseEngine.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/SQLParseEngine.java @@ -23,6 +23,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; +import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; import com.dangdang.ddframe.rdb.sharding.exception.SQLParserException; import com.dangdang.ddframe.rdb.sharding.parser.result.SQLParsedResult; import com.dangdang.ddframe.rdb.sharding.parser.result.router.SQLStatementType; @@ -32,7 +33,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import java.util.Collection; import java.util.List; /** @@ -51,7 +51,7 @@ public final class SQLParseEngine { private final SQLASTOutputVisitor visitor; - private final Collection shardingColumns; + private final ShardingRule shardingRule; /** *  解析SQL. @@ -62,7 +62,7 @@ public SQLParsedResult parse() { Preconditions.checkArgument(visitor instanceof SQLVisitor); SQLVisitor sqlVisitor = (SQLVisitor) visitor; visitor.setParameters(parameters); - sqlVisitor.getParseContext().setShardingColumns(shardingColumns); + sqlVisitor.getParseContext().setShardingRule(shardingRule); sqlStatement.accept(visitor); SQLParsedResult result = sqlVisitor.getParseContext().getParsedResult(); if (sqlVisitor.getParseContext().isHasOrCondition()) { diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/SQLParserFactory.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/SQLParserFactory.java index 14ce3707c30a3..dec41c5124e2c 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/SQLParserFactory.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/SQLParserFactory.java @@ -28,6 +28,7 @@ import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser; import com.alibaba.druid.sql.parser.SQLStatementParser; import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; +import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; import com.dangdang.ddframe.rdb.sharding.constants.DatabaseType; import com.dangdang.ddframe.rdb.sharding.exception.SQLParserException; import com.dangdang.ddframe.rdb.sharding.parser.visitor.VisitorLogProxy; @@ -35,7 +36,6 @@ import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; -import java.util.Collection; import java.util.List; /** @@ -54,15 +54,15 @@ public final class SQLParserFactory { * @param databaseType 数据库类型 * @param sql SQL语句 * @param parameters SQL中参数的值 - * @param shardingColumns 分片列名称集合 + * @param shardingRule 分片规则 * @return 解析器引擎对象 * @throws SQLParserException SQL解析异常 */ - public static SQLParseEngine create(final DatabaseType databaseType, final String sql, final List parameters, final Collection shardingColumns) throws SQLParserException { + public static SQLParseEngine create(final DatabaseType databaseType, final String sql, final List parameters, final ShardingRule shardingRule) throws SQLParserException { log.debug("Logic SQL: {}, {}", sql, parameters); SQLStatement sqlStatement = getSQLStatementParser(databaseType, sql).parseStatement(); log.trace("Get {} SQL Statement", sqlStatement.getClass().getName()); - return new SQLParseEngine(sqlStatement, parameters, getSQLVisitor(databaseType, sqlStatement), shardingColumns); + return new SQLParseEngine(sqlStatement, parameters, getSQLVisitor(databaseType, sqlStatement), shardingRule); } private static SQLStatementParser getSQLStatementParser(final DatabaseType databaseType, final String sql) { diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/result/router/RouteContext.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/result/router/RouteContext.java index a9d733a84dcc2..f8ae3b351ea46 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/result/router/RouteContext.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/result/router/RouteContext.java @@ -17,14 +17,15 @@ package com.dangdang.ddframe.rdb.sharding.parser.result.router; -import java.util.Collection; -import java.util.LinkedHashSet; - import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; import lombok.ToString; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.LinkedList; + /** * SQL路由上下文. * @@ -41,4 +42,6 @@ public final class RouteContext { private SQLStatementType sqlStatementType; private SQLBuilder sqlBuilder; + + private Collection autoIncrementColumns = new LinkedList<>(); } diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/ParseContext.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/ParseContext.java index 2105ba3f404c5..991ec8e7bab2b 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/ParseContext.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/ParseContext.java @@ -27,6 +27,7 @@ import com.alibaba.druid.sql.visitor.SQLEvalVisitor; import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils; import com.alibaba.druid.util.JdbcUtils; +import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; import com.dangdang.ddframe.rdb.sharding.constants.DatabaseType; import com.dangdang.ddframe.rdb.sharding.parser.result.SQLParsedResult; import com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn; @@ -42,6 +43,9 @@ import com.dangdang.ddframe.rdb.sharding.parser.visitor.basic.mysql.MySQLEvalVisitor; import com.dangdang.ddframe.rdb.sharding.util.SQLUtil; import com.google.common.base.Optional; +import com.google.common.base.Supplier; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; @@ -52,6 +56,9 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; /** * 解析过程的上下文对象. @@ -72,7 +79,7 @@ public final class ParseContext { private final int parseContextIndex; @Setter - private Collection shardingColumns; + private ShardingRule shardingRule; @Setter private boolean hasOrCondition; @@ -94,6 +101,13 @@ public final class ParseContext { private int itemIndex; + private final Multimap tableShardingColumnsMap = Multimaps.newSetMultimap(new TreeMap>(String.CASE_INSENSITIVE_ORDER), new Supplier>() { + @Override + public Set get() { + return new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + } + }); + public ParseContext(final int parseContextIndex) { this.parseContextIndex = parseContextIndex; autoGenTokenKey = String.format(AUTO_GEN_TOKE_KEY_TEMPLATE, parseContextIndex); @@ -140,7 +154,10 @@ public Table addTable(final SQLExprTableSource x) { */ public void addCondition(final SQLExpr expr, final BinaryOperator operator, final List valueExprList, final DatabaseType databaseType, final List parameters) { Optional column = getColumn(expr); - if (!column.isPresent() || !shardingColumns.contains(column.get().getColumnName())) { + if (!column.isPresent()) { + return; + } + if (notShardingColumns(column.get())) { return; } List values = new ArrayList<>(valueExprList.size()); @@ -168,8 +185,8 @@ public void addCondition(final SQLExpr expr, final BinaryOperator operator, fina */ public void addCondition(final String columnName, final String tableName, final BinaryOperator operator, final SQLExpr valueExpr, final DatabaseType databaseType, final List parameters) { Column column = createColumn(columnName, tableName); - if (!shardingColumns.contains(column.getColumnName())) { - return; + if (notShardingColumns(column)) { + return; } ValuePair value = evalExpression(databaseType, valueExpr, parameters); if (null != value) { @@ -195,6 +212,13 @@ private void addCondition(final Column column, final BinaryOperator operator, fi } } + private boolean notShardingColumns(final Column column) { + if (!tableShardingColumnsMap.containsKey(column.getTableName())) { + tableShardingColumnsMap.putAll(column.getTableName(), shardingRule.getAllShardingColumns(column.getTableName())); + } + return !tableShardingColumnsMap.containsEntry(column.getTableName(), column.getColumnName()); + } + private ValuePair evalExpression(final DatabaseType databaseType, final SQLObject sqlObject, final List parameters) { if (sqlObject instanceof SQLMethodInvokeExpr) { // TODO 解析函数中的sharingValue不支持 diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/basic/mysql/AbstractMySQLVisitor.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/basic/mysql/AbstractMySQLVisitor.java index dc977d5d68128..db8714b199d7a 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/basic/mysql/AbstractMySQLVisitor.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/basic/mysql/AbstractMySQLVisitor.java @@ -71,7 +71,7 @@ final void stepInQuery() { return; } ParseContext parseContext = new ParseContext(parseContextIndex++); - parseContext.setShardingColumns(this.parseContext.getShardingColumns()); + parseContext.setShardingRule(this.parseContext.getShardingRule()); parseContext.setParentParseContext(this.parseContext); this.parseContext.getSubParseContext().add(parseContext); this.parseContext = parseContext; diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/basic/mysql/MySQLInsertVisitor.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/basic/mysql/MySQLInsertVisitor.java index 4b6b044253bc5..f243f6c0c9928 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/basic/mysql/MySQLInsertVisitor.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/basic/mysql/MySQLInsertVisitor.java @@ -17,10 +17,20 @@ package com.dangdang.ddframe.rdb.sharding.parser.visitor.basic.mysql; +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; +import com.alibaba.druid.sql.ast.expr.SQLNumberExpr; +import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; +import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule; import com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition.BinaryOperator; +import com.dangdang.ddframe.rdb.sharding.util.SQLUtil; import com.google.common.base.Optional; +import java.util.Collection; +import java.util.List; + /** * MySQL的INSERT语句访问器. * @@ -31,13 +41,47 @@ public class MySQLInsertVisitor extends AbstractMySQLVisitor { @Override public boolean visit(final MySqlInsertStatement x) { - getParseContext().setCurrentTable(x.getTableName().toString(), Optional.fromNullable(x.getAlias())); + final String tableName = SQLUtil.getExactlyValue(x.getTableName().toString()); + getParseContext().setCurrentTable(tableName, Optional.fromNullable(x.getAlias())); if (null == x.getValues()) { return super.visit(x); } + Collection autoIncrementColumns = getParseContext().getShardingRule().getAutoIncrementColumns(tableName); + List columns = x.getColumns(); + List values = x.getValues().getValues(); for (int i = 0; i < x.getColumns().size(); i++) { - getParseContext().addCondition(x.getColumns().get(i).toString(), x.getTableName().toString(), BinaryOperator.EQUAL, x.getValues().getValues().get(i), getDatabaseType(), getParameters()); + String columnName = SQLUtil.getExactlyValue(columns.get(i).toString()); + getParseContext().addCondition(columnName, tableName, BinaryOperator.EQUAL, values.get(i), getDatabaseType(), getParameters()); + if (autoIncrementColumns.contains(columnName)) { + autoIncrementColumns.remove(columnName); + } + } + if (autoIncrementColumns.isEmpty()) { + return super.visit(x); } + supplyAutoIncrementColumn(autoIncrementColumns, tableName, columns, values); return super.visit(x); } + + private void supplyAutoIncrementColumn(final Collection autoIncrementColumns, final String tableName, final List columns, final List values) { + boolean isPreparedStatement = !getParameters().isEmpty(); + if (isPreparedStatement) { + getParseContext().getParsedResult().getRouteContext().getAutoIncrementColumns().addAll(autoIncrementColumns); + } + TableRule tableRule = getParseContext().getShardingRule().findTableRule(tableName); + for (String each : autoIncrementColumns) { + SQLExpr sqlExpr; + Object id = tableRule.generateId(each); + if (isPreparedStatement) { + sqlExpr = new SQLVariantRefExpr("?"); + getParameters().add(id); + ((SQLVariantRefExpr) sqlExpr).setIndex(getParametersSize() - 1); + } else { + sqlExpr = (id instanceof Number) ? new SQLNumberExpr((Number) id) : new SQLCharExpr((String) id); + } + getParseContext().addCondition(each, tableName, BinaryOperator.EQUAL, sqlExpr, getDatabaseType(), getParameters()); + columns.add(new SQLIdentifierExpr(each)); + values.add(sqlExpr); + } + } } diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/or/OrVisitor.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/or/OrVisitor.java index ada9d558af07b..dd58c2b96a56a 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/or/OrVisitor.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parser/visitor/or/OrVisitor.java @@ -45,7 +45,7 @@ public OrVisitor(final SQLASTOutputVisitor dependencyVisitor) { getParseContext().setCurrentTable(visitor.getParseContext().getCurrentTable().getName(), Optional.absent()); } getParseContext().getParsedResult().getRouteContext().getTables().addAll(visitor.getParseContext().getParsedResult().getRouteContext().getTables()); - getParseContext().setShardingColumns(visitor.getParseContext().getShardingColumns()); + getParseContext().setShardingRule(visitor.getParseContext().getShardingRule()); } /** diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/PreparedSQLRouter.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/PreparedSQLRouter.java index b73a1f9c7514a..f864d7a326dba 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/PreparedSQLRouter.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/PreparedSQLRouter.java @@ -17,6 +17,8 @@ package com.dangdang.ddframe.rdb.sharding.router; +import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; +import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule; import com.dangdang.ddframe.rdb.sharding.parser.result.SQLParsedResult; import com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext; import lombok.RequiredArgsConstructor; @@ -35,8 +37,12 @@ public class PreparedSQLRouter { private final SQLRouteEngine engine; + private final ShardingRule shardingRule; + private SQLParsedResult sqlParsedResult; + private TableRule tableRule; + /** * 使用参数进行SQL路由. * 当第一次路由时进行SQL解析,之后的路由复用第一次的解析结果. @@ -47,7 +53,11 @@ public class PreparedSQLRouter { public SQLRouteResult route(final List parameters) { if (null == sqlParsedResult) { sqlParsedResult = engine.parseSQL(logicSql, parameters); + tableRule = shardingRule.findTableRule(sqlParsedResult.getRouteContext().getTables().iterator().next().getName()); } else { + for (String each : sqlParsedResult.getRouteContext().getAutoIncrementColumns()) { + parameters.add(tableRule.generateId(each)); + } engine.setParameters(parameters); for (ConditionContext each : sqlParsedResult.getConditionContexts()) { each.setNewConditionValue(parameters); diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/SQLRouteEngine.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/SQLRouteEngine.java index 61687bb344aee..669d8defb60ed 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/SQLRouteEngine.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/SQLRouteEngine.java @@ -80,13 +80,13 @@ SQLRouteResult route(final String logicSql, final List parameters) throw * @return 预解析SQL路由器 */ public PreparedSQLRouter prepareSQL(final String logicSql) { - return new PreparedSQLRouter(logicSql, this); + return new PreparedSQLRouter(logicSql, this, shardingRule); } SQLParsedResult parseSQL(final String logicSql, final List parameters) { this.parameters = parameters; Context context = MetricsContext.start("Parse SQL"); - SQLParsedResult result = SQLParserFactory.create(databaseType, logicSql, parameters, shardingRule.getAllShardingColumns()).parse(); + SQLParsedResult result = SQLParserFactory.create(databaseType, logicSql, parameters, shardingRule).parse(); MetricsContext.stop(context); return result; } diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/single/SingleTableRouter.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/single/SingleTableRouter.java index 940ef6227b898..3696f9f430c2a 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/single/SingleTableRouter.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/router/single/SingleTableRouter.java @@ -67,7 +67,7 @@ public SingleTableRouter(final ShardingRule shardingRule, final String logicTabl this.logicTable = logicTable; this.conditionContext = conditionContext; this.sqlStatementType = sqlStatementType; - Optional tableRuleOptional = shardingRule.findTableRule(logicTable); + Optional tableRuleOptional = shardingRule.tryFindTableRule(logicTable); if (tableRuleOptional.isPresent()) { tableRule = tableRuleOptional.get(); } else if (shardingRule.getDataSourceRule().getDefaultDataSource().isPresent()) { diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/AllTests.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/AllTests.java index c2c833ed4633b..3f63f0691f03c 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/AllTests.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/AllTests.java @@ -23,6 +23,7 @@ import com.dangdang.ddframe.rdb.sharding.constants.AllConstantsTests; import com.dangdang.ddframe.rdb.sharding.executor.AllExecutorTests; import com.dangdang.ddframe.rdb.sharding.hint.AllHintTests; +import com.dangdang.ddframe.rdb.sharding.id.generator.AllIdGeneratorTest; import com.dangdang.ddframe.rdb.sharding.jdbc.AllJDBCTests; import com.dangdang.ddframe.rdb.sharding.merger.AllMergerTests; import com.dangdang.ddframe.rdb.sharding.metrics.AllMetricsTests; @@ -46,7 +47,8 @@ AllHintTests.class, AllUtilTests.class, AllMetricsTests.class, - AllIntegrateTests.class + AllIntegrateTests.class, + AllIdGeneratorTest.class, }) public class AllTests { } diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/AbstractShardingDataBasesOnlyDBUnitTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/AbstractShardingDataBasesOnlyDBUnitTest.java index da52c34eaab41..678bd5db481a0 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/AbstractShardingDataBasesOnlyDBUnitTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/AbstractShardingDataBasesOnlyDBUnitTest.java @@ -26,6 +26,7 @@ import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy; +import com.dangdang.ddframe.rdb.sharding.id.generator.fixture.IncrementIdGenerator; import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource; import org.junit.AfterClass; @@ -75,7 +76,7 @@ protected final ShardingDataSource getShardingDataSource() { } isShutdown = false; DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap("dataSource_%s")); - TableRule orderTableRule = TableRule.builder("t_order").dataSourceRule(dataSourceRule).build(); + TableRule orderTableRule = TableRule.builder("t_order").dataSourceRule(dataSourceRule).autoIncrementColumns("user_id").tableIdGenerator(IncrementIdGenerator.class).build(); TableRule orderItemTableRule = TableRule.builder("t_order_item").dataSourceRule(dataSourceRule).build(); ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule)) .bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule)))) diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/pstatement/ShardingDataBasesOnlyForPStatementWithDMLTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/pstatement/ShardingDataBasesOnlyForPStatementWithDMLTest.java index 720ce1ea485c6..17bc9dcf7edc8 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/pstatement/ShardingDataBasesOnlyForPStatementWithDMLTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/pstatement/ShardingDataBasesOnlyForPStatementWithDMLTest.java @@ -17,10 +17,6 @@ package com.dangdang.ddframe.rdb.integrate.db.pstatement; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; - import com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest; import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource; import com.dangdang.ddframe.rdb.sharding.parser.result.router.SQLStatementType; @@ -28,6 +24,10 @@ import org.junit.Before; import org.junit.Test; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; @@ -55,6 +55,20 @@ public void assertInsertWithAllPlaceholders() throws SQLException, DatabaseUnitE assertDataSet("insert", "insert"); } + @Test + public void assertInsertWithAutoIncrementColumn() throws SQLException, DatabaseUnitException { + String sql = "INSERT INTO `t_order` (`order_id`, `status`) VALUES (?, ?)"; + try (Connection connection = shardingDataSource.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + for (int i = 1; i <= 10; i++) { + preparedStatement.setInt(1, i); + preparedStatement.setString(2, "insert"); + preparedStatement.executeUpdate(); + } + } + assertDataSet("insert", "insert"); + } + @Test public void assertInsertWithoutPlaceholder() throws SQLException, DatabaseUnitException { String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (%s, %s, 'insert')"; diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/statement/ShardingDataBasesOnlyForStatementWithDMLTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/statement/ShardingDataBasesOnlyForStatementWithDMLTest.java index 23e74fcce9329..426a62549865b 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/statement/ShardingDataBasesOnlyForStatementWithDMLTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/db/statement/ShardingDataBasesOnlyForStatementWithDMLTest.java @@ -17,20 +17,19 @@ package com.dangdang.ddframe.rdb.integrate.db.statement; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; - import com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest; +import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource; import com.dangdang.ddframe.rdb.sharding.parser.result.router.SQLStatementType; import org.dbunit.DatabaseUnitException; import org.junit.Before; import org.junit.Test; -import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; public class ShardingDataBasesOnlyForStatementWithDMLTest extends AbstractShardingDataBasesOnlyDBUnitTest { @@ -54,6 +53,19 @@ public void assertInsert() throws SQLException, DatabaseUnitException { assertDataSet("insert", "insert"); } + @Test + public void assertInsertWithAutoIncrementColumn() throws SQLException, DatabaseUnitException { + for (int i = 1; i <= 10; i++) { + try (Connection connection = shardingDataSource.getConnection()) { + connection.setAutoCommit(false); + connection.createStatement().executeUpdate(String.format("INSERT INTO `t_order` (`order_id`, `status`) VALUES (%s, '%s')", i, "insert")); + connection.commit(); + connection.close(); + } + } + assertDataSet("insert", "insert"); + } + @Test public void assertUpdate() throws SQLException, DatabaseUnitException { for (int i = 10; i < 30; i++) { diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/fixture/ShardingRuleMockBuilder.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/fixture/ShardingRuleMockBuilder.java new file mode 100644 index 0000000000000..8507322a523e9 --- /dev/null +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/fixture/ShardingRuleMockBuilder.java @@ -0,0 +1,74 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * 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.dangdang.ddframe.rdb.sharding.api.fixture; + +import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule; +import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; +import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule; +import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy; +import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm; +import com.dangdang.ddframe.rdb.sharding.id.generator.fixture.IncrementIdGenerator; +import com.google.common.base.Function; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterators; +import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.Lists; +import com.google.common.collect.Multimap; +import org.mockito.Mockito; + +import javax.sql.DataSource; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +public class ShardingRuleMockBuilder { + + private final List shardingColumns = new ArrayList<>(); + + private final Multimap autoIncrementColumnMap = LinkedHashMultimap.create(); + + public ShardingRuleMockBuilder addShardingColumns(final String shardingColumnName) { + this.shardingColumns.add(shardingColumnName); + return this; + } + + public ShardingRuleMockBuilder addAutoIncrementColumn(final String tableName, final String columnName) { + autoIncrementColumnMap.put(tableName, columnName); + return this; + } + + public ShardingRule build() { + final DataSourceRule dataSourceRule = new DataSourceRule(ImmutableMap.of("db0", Mockito.mock(DataSource.class), "db1", Mockito.mock(DataSource.class))); + Collection tableRules = Lists.newArrayList(Iterators.transform(autoIncrementColumnMap.keySet().iterator(), new Function() { + @Override + public TableRule apply(final String input) { + TableRule.TableRuleBuilder builder = TableRule.builder(input).actualTables(Collections.singletonList(input)).dataSourceRule(dataSourceRule); + for (String each : autoIncrementColumnMap.get(input)) { + builder.autoIncrementColumns(each); + } + return builder.build(); + } + })); + if (tableRules.isEmpty()) { + tableRules.add(new TableRule.TableRuleBuilder("mock").actualTables(Collections.singletonList("mock")).dataSourceRule(dataSourceRule).build()); + } + return new ShardingRule.ShardingRuleBuilder().dataSourceRule(dataSourceRule).idGenerator(IncrementIdGenerator.class) + .tableRules(tableRules).databaseShardingStrategy(new DatabaseShardingStrategy(shardingColumns, new NoneDatabaseShardingAlgorithm())).build(); + } +} diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/rule/ShardingRuleTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/rule/ShardingRuleTest.java index 4a5c36312f040..39342aab59897 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/rule/ShardingRuleTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/rule/ShardingRuleTest.java @@ -85,8 +85,8 @@ public void assertFindTableRule() { .tableRules(Collections.singletonList(createTableRule())) .databaseShardingStrategy(createDatabaseShardingStrategy()) .tableShardingStrategy(createTableShardingStrategy()).build(); - assertTrue(actual.findTableRule("logicTable").isPresent()); - assertFalse(actual.findTableRule("null").isPresent()); + assertTrue(actual.tryFindTableRule("logicTable").isPresent()); + assertFalse(actual.tryFindTableRule("null").isPresent()); } @Test @@ -194,12 +194,12 @@ public void assertIsAllBindingTable() { @Test public void assertGetAllShardingColumnsWithoutStrategy() { - assertThat(createShardingRule().getAllShardingColumns(), is((Collection) Collections.emptySet())); + assertThat(createShardingRule().getAllShardingColumns("logicTable"), is((Collection) Collections.emptySet())); } @Test public void assertGetAllShardingColumnsWithStrategy() { - assertThat(createShardingRuleWithStrategy().getAllShardingColumns(), is((Collection) Sets.newHashSet("column"))); + assertThat(createShardingRuleWithStrategy().getAllShardingColumns("logicTable"), is((Collection) Sets.newHashSet("column"))); } diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/rule/TableRuleTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/rule/TableRuleTest.java index 8a9fe319838ed..146f7d2bbe0ea 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/rule/TableRuleTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/api/rule/TableRuleTest.java @@ -21,6 +21,8 @@ import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy; +import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator; +import com.dangdang.ddframe.rdb.sharding.id.generator.fixture.IncrementIdGenerator; import com.google.common.collect.Sets; import org.junit.Test; @@ -32,6 +34,7 @@ import java.util.Map; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.core.IsInstanceOf.instanceOf; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; @@ -194,7 +197,29 @@ public void assertToString() { + "DataNode(dataSourceName=ds1, tableName=table_0), " + "DataNode(dataSourceName=ds1, tableName=table_1), " + "DataNode(dataSourceName=ds1, tableName=table_2)], " - + "databaseShardingStrategy=null, tableShardingStrategy=null)")); + + "databaseShardingStrategy=null, tableShardingStrategy=null, " + + "autoIncrementColumnMap={})")); + } + + @Test + public void assertAutoIncrementColumn() { + TableRule actual = TableRule.builder("logicTable").dataSourceRule(createDataSourceRule()).autoIncrementColumns("col_1", IncrementIdGenerator.class) + .autoIncrementColumns("col_2").tableIdGenerator(MockIdGenerator.class).build(); + assertThat(actual.getAutoIncrementColumnMap().get("col_1"), instanceOf(IncrementIdGenerator.class)); + assertThat(actual.getAutoIncrementColumnMap().get("col_2"), instanceOf(MockIdGenerator.class)); + } + + public static class MockIdGenerator implements IdGenerator { + + @Override + public void initContext(final String tableName, final String columnName) { + + } + + @Override + public Object generateId() { + return null; + } } private DataSourceRule createDataSourceRule() { diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/AllIdGeneratorTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/AllIdGeneratorTest.java new file mode 100644 index 0000000000000..97c7762baf4c4 --- /dev/null +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/AllIdGeneratorTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * 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.dangdang.ddframe.rdb.sharding.id.generator; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses( + UnsupportedIdGeneratorTest.class +) +public class AllIdGeneratorTest { + +} diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/UnsupportedIdGeneratorTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/UnsupportedIdGeneratorTest.java new file mode 100644 index 0000000000000..7ff4ebb03ad77 --- /dev/null +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/UnsupportedIdGeneratorTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * 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.dangdang.ddframe.rdb.sharding.id.generator; + +import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule; +import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule; +import org.junit.Test; + +import javax.sql.DataSource; +import java.util.HashMap; +import java.util.Map; + +public class UnsupportedIdGeneratorTest { + + @Test(expected = IllegalArgumentException.class) + public void testIllegalClass() { + TableRule.builder("test").dataSourceRule(createDataSourceRule()) + .autoIncrementColumns("col_1", IllegalIdGenerator.class).build(); + } + + private DataSourceRule createDataSourceRule() { + Map result = new HashMap<>(2); + result.put("ds0", null); + result.put("ds1", null); + return new DataSourceRule(result); + } + + private static class IllegalIdGenerator implements IdGenerator { + + @Override + public void initContext(final String tableName, final String columnName) { + + } + + @Override + public Object generateId() { + return null; + } + } +} diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingPreparedStatementTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingPreparedStatementTest.java index 53d081bc821be..5464a16508383 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingPreparedStatementTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingPreparedStatementTest.java @@ -18,7 +18,6 @@ package com.dangdang.ddframe.rdb.sharding.jdbc; import com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest; -import com.mysql.jdbc.Statement; import org.junit.Before; import org.junit.Test; @@ -26,6 +25,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertFalse; @@ -247,6 +247,52 @@ public void assertAddBatch() throws SQLException { } } + @Test + public void assertAddBatchWithAutoIncrementColumn() throws SQLException { + String sql = "INSERT INTO `t_order`(`order_id`, `status`) VALUES (?,?)"; + try ( + Connection connection = shardingDataSource.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql); + java.sql.Statement queryStatement = connection.createStatement()) { + preparedStatement.setInt(1, 11); + preparedStatement.setString(2, "BATCH"); + preparedStatement.addBatch(); + preparedStatement.setInt(1, 12); + preparedStatement.setString(2, "BATCH"); + preparedStatement.addBatch(); + preparedStatement.setInt(1, 21); + preparedStatement.setString(2, "BATCH"); + preparedStatement.addBatch(); + preparedStatement.setInt(1, 22); + preparedStatement.setString(2, "BATCH"); + preparedStatement.addBatch(); + int[] result = preparedStatement.executeBatch(); + for (int each : result) { + assertThat(each, is(1)); + } + try(ResultSet rs = queryStatement.executeQuery("SELECT `order_id` from `t_order` where `user_id` = 1")) { + assertThat(rs.next(), is(true)); + assertThat(rs.getInt(1), is(11)); + assertThat(rs.next(), is(false)); + } + try(ResultSet rs = queryStatement.executeQuery("SELECT `order_id` from `t_order` where `user_id` = 2")) { + assertThat(rs.next(), is(true)); + assertThat(rs.getInt(1), is(12)); + assertThat(rs.next(), is(false)); + } + try(ResultSet rs = queryStatement.executeQuery("SELECT `order_id` from `t_order` where `user_id` = 3")) { + assertThat(rs.next(), is(true)); + assertThat(rs.getInt(1), is(21)); + assertThat(rs.next(), is(false)); + } + try(ResultSet rs = queryStatement.executeQuery("SELECT `order_id` from `t_order` where `user_id` = 4")) { + assertThat(rs.next(), is(true)); + assertThat(rs.getInt(1), is(22)); + assertThat(rs.next(), is(false)); + } + } + } + @Test public void assertClearBatch() throws SQLException { String sql = "INSERT INTO `t_order`(`order_id`, `user_id`, `status`) VALUES (?,?,?)"; diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/UnsupportedParseTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/UnsupportedParseTest.java index d054d0b7c9547..6dc67fb1a1a83 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/UnsupportedParseTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/UnsupportedParseTest.java @@ -17,6 +17,7 @@ package com.dangdang.ddframe.rdb.sharding.parser; +import com.dangdang.ddframe.rdb.sharding.api.fixture.ShardingRuleMockBuilder; import com.dangdang.ddframe.rdb.sharding.constants.DatabaseType; import com.dangdang.ddframe.rdb.sharding.exception.SQLParserException; import org.junit.Test; @@ -27,27 +28,27 @@ public final class UnsupportedParseTest { @Test(expected = SQLParserException.class) public void assertCreate() throws SQLParserException { - SQLParserFactory.create(DatabaseType.MySQL, "CREATE TABLE `order` (id BIGINT(10))", Collections.emptyList(), Collections.emptyList()); + SQLParserFactory.create(DatabaseType.MySQL, "CREATE TABLE `order` (id BIGINT(10))", Collections.emptyList(), new ShardingRuleMockBuilder().build()); } @Test(expected = SQLParserException.class) public void assertDrop() throws SQLParserException { - SQLParserFactory.create(DatabaseType.MySQL, "DROP TABLE `order`", Collections.emptyList(), Collections.emptyList()); + SQLParserFactory.create(DatabaseType.MySQL, "DROP TABLE `order`", Collections.emptyList(), new ShardingRuleMockBuilder().build()); } @Test(expected = SQLParserException.class) public void assertTruncate() throws SQLParserException { - SQLParserFactory.create(DatabaseType.MySQL, "TRUNCATE `order`", Collections.emptyList(), Collections.emptyList()); + SQLParserFactory.create(DatabaseType.MySQL, "TRUNCATE `order`", Collections.emptyList(), new ShardingRuleMockBuilder().build()); } @Test(expected = SQLParserException.class) public void assertAlter() throws SQLParserException { - SQLParserFactory.create(DatabaseType.MySQL, "ALTER TABLE `order` ADD COLUMN `other` VARCHAR(45)", Collections.emptyList(), Collections.emptyList()); + SQLParserFactory.create(DatabaseType.MySQL, "ALTER TABLE `order` ADD COLUMN `other` VARCHAR(45)", Collections.emptyList(), new ShardingRuleMockBuilder().build()); } @Test(expected = SQLParserException.class) public void assertNegativeLimitRowCount() throws SQLParserException { - SQLParserFactory.create(DatabaseType.MySQL, "select * from order limit -2,-1", Collections.emptyList(), Collections.emptyList()).parse(); + SQLParserFactory.create(DatabaseType.MySQL, "select * from order limit -2,-1", Collections.emptyList(), new ShardingRuleMockBuilder().build()).parse(); } } diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLPreparedStatementForOneParameterTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLPreparedStatementForOneParameterTest.java index 32bdecadc4873..8e165c9f4edfc 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLPreparedStatementForOneParameterTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLPreparedStatementForOneParameterTest.java @@ -17,21 +17,20 @@ package com.dangdang.ddframe.rdb.sharding.parser.mysql; -import java.util.Arrays; -import java.util.Collection; - -import com.google.common.collect.Lists; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - +import com.dangdang.ddframe.rdb.sharding.api.fixture.ShardingRuleMockBuilder; import com.dangdang.ddframe.rdb.sharding.constants.DatabaseType; import com.dangdang.ddframe.rdb.sharding.parser.AbstractBaseParseTest; import com.dangdang.ddframe.rdb.sharding.parser.SQLParserFactory; import com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext; import com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext; import com.dangdang.ddframe.rdb.sharding.parser.result.router.Table; +import com.google.common.collect.Lists; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.util.Collection; @RunWith(Parameterized.class) public final class MySQLPreparedStatementForOneParameterTest extends AbstractBaseParseTest { @@ -48,6 +47,8 @@ public static Collection dataParameters() { @Test public void assertParse() { - assertSQLParsedResult(SQLParserFactory.create(DatabaseType.MySQL, getSql(), Lists.newArrayList(1), Arrays.asList("user_id", "order_id", "state")).parse()); + assertSQLParsedResult(SQLParserFactory.create(DatabaseType.MySQL, getSql(), Lists.newArrayList(1), + new ShardingRuleMockBuilder().addShardingColumns("user_id").addShardingColumns("order_id").addShardingColumns("state").addAutoIncrementColumn("order", "order_id") + .addAutoIncrementColumn("payment", "order_id").addAutoIncrementColumn("payment", "pay_no").build()).parse()); } } diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLPreparedStatementForTowParametersTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLPreparedStatementForTowParametersTest.java index 72577216624a3..899f32370ba5c 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLPreparedStatementForTowParametersTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLPreparedStatementForTowParametersTest.java @@ -17,20 +17,20 @@ package com.dangdang.ddframe.rdb.sharding.parser.mysql; -import java.util.Arrays; -import java.util.Collection; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - +import com.dangdang.ddframe.rdb.sharding.api.fixture.ShardingRuleMockBuilder; import com.dangdang.ddframe.rdb.sharding.constants.DatabaseType; import com.dangdang.ddframe.rdb.sharding.parser.AbstractBaseParseTest; import com.dangdang.ddframe.rdb.sharding.parser.SQLParserFactory; import com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext; import com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext; import com.dangdang.ddframe.rdb.sharding.parser.result.router.Table; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.util.Arrays; +import java.util.Collection; @RunWith(Parameterized.class) public final class MySQLPreparedStatementForTowParametersTest extends AbstractBaseParseTest { @@ -47,6 +47,8 @@ public static Collection dataParameters() { @Test public void assertParse() { - assertSQLParsedResult(SQLParserFactory.create(DatabaseType.MySQL, getSql(), Arrays.asList(1, 2), Arrays.asList("user_id", "order_id", "state")).parse()); + assertSQLParsedResult(SQLParserFactory.create(DatabaseType.MySQL, getSql(), Arrays.asList(1, 2), + new ShardingRuleMockBuilder().addShardingColumns("user_id").addShardingColumns("order_id").addShardingColumns("state") + .addAutoIncrementColumn("order", "order_id").build()).parse()); } } diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLStatementTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLStatementTest.java index 694bade10a675..eac460bfc387b 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLStatementTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/MySQLStatementTest.java @@ -17,21 +17,20 @@ package com.dangdang.ddframe.rdb.sharding.parser.mysql; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - +import com.dangdang.ddframe.rdb.sharding.api.fixture.ShardingRuleMockBuilder; import com.dangdang.ddframe.rdb.sharding.constants.DatabaseType; import com.dangdang.ddframe.rdb.sharding.parser.AbstractBaseParseTest; import com.dangdang.ddframe.rdb.sharding.parser.SQLParserFactory; import com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext; import com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext; import com.dangdang.ddframe.rdb.sharding.parser.result.router.Table; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.util.Collection; +import java.util.Collections; @RunWith(Parameterized.class) public final class MySQLStatementTest extends AbstractBaseParseTest { @@ -48,6 +47,8 @@ public static Collection dataParameters() { @Test public void assertParse() { - assertSQLParsedResult(SQLParserFactory.create(DatabaseType.MySQL, getSql(), Collections.emptyList(), Arrays.asList("user_id", "order_id", "state")).parse()); + assertSQLParsedResult(SQLParserFactory.create(DatabaseType.MySQL, getSql(), Collections.emptyList(), + new ShardingRuleMockBuilder().addShardingColumns("user_id").addShardingColumns("order_id").addShardingColumns("state") + .addAutoIncrementColumn("order", "order_id").addAutoIncrementColumn("payment", "id").addAutoIncrementColumn("payment", "order_id").build()).parse()); } } diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/OrParseTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/OrParseTest.java index 77f75a652e5ad..257a391c9055f 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/OrParseTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/mysql/OrParseTest.java @@ -17,21 +17,20 @@ package com.dangdang.ddframe.rdb.sharding.parser.mysql; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - +import com.dangdang.ddframe.rdb.sharding.api.fixture.ShardingRuleMockBuilder; import com.dangdang.ddframe.rdb.sharding.constants.DatabaseType; import com.dangdang.ddframe.rdb.sharding.parser.AbstractBaseParseTest; import com.dangdang.ddframe.rdb.sharding.parser.SQLParserFactory; import com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext; import com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext; import com.dangdang.ddframe.rdb.sharding.parser.result.router.Table; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.util.Collection; +import java.util.Collections; @RunWith(Parameterized.class) public final class OrParseTest extends AbstractBaseParseTest { @@ -49,7 +48,8 @@ public static Collection dataParameters() { // TODO 归并字段,整合进mySQL测试 @Test public void assertParse() { - assertSQLParsedResult(SQLParserFactory.create(DatabaseType.MySQL, getSql(), Collections.emptyList(), - Arrays.asList("id", "user_id", "name", "age", "days", "fee", "travel_date", "long")).parse()); + assertSQLParsedResult(SQLParserFactory.create(DatabaseType.MySQL, getSql(), Collections.emptyList(), new ShardingRuleMockBuilder() + .addShardingColumns("id").addShardingColumns("user_id").addShardingColumns("name").addShardingColumns("age") + .addShardingColumns("days").addShardingColumns("fee").addShardingColumns("travel_date").addShardingColumns("long").build()).parse()); } } diff --git a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/result/SQLParsedResultTest.java b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/result/SQLParsedResultTest.java index c91195ea5e075..96749dd669c7a 100644 --- a/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/result/SQLParsedResultTest.java +++ b/sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/parser/result/SQLParsedResultTest.java @@ -52,7 +52,7 @@ public void assertToString() throws IOException { + "routeContext=RouteContext(" + "tables=[Table(name=order, alias=Optional.of(o)), Table(name=order_item, alias=Optional.absent())], " + "sqlStatementType=null, " - + "sqlBuilder=SELECT * FROM [Token(order)]), " + + "sqlBuilder=SELECT * FROM [Token(order)], autoIncrementColumns=[]), " + "conditionContexts=[ConditionContext(conditions={Condition.Column(columnName=id, tableName=order)=Condition(column=Condition.Column(columnName=id, tableName=order), " + "operator=IN, values=[1, 2, 3], valueIndices=[])})], " + "mergeContext=MergeContext(" diff --git a/sharding-jdbc-core/src/test/resources/com/dangdang/ddframe/rdb/sharding/parser/mysql/prepared_statement/one_param/insert.xml b/sharding-jdbc-core/src/test/resources/com/dangdang/ddframe/rdb/sharding/parser/mysql/prepared_statement/one_param/insert.xml index ffca74b669ef4..2265e852d57c8 100644 --- a/sharding-jdbc-core/src/test/resources/com/dangdang/ddframe/rdb/sharding/parser/mysql/prepared_statement/one_param/insert.xml +++ b/sharding-jdbc-core/src/test/resources/com/dangdang/ddframe/rdb/sharding/parser/mysql/prepared_statement/one_param/insert.xml @@ -16,4 +16,38 @@ + + + + + + + + + 0 + + + + 1 + + + + + + +
+ + + + + + 0 + + + + 1 + + + + diff --git a/sharding-jdbc-core/src/test/resources/com/dangdang/ddframe/rdb/sharding/parser/mysql/statement/insert.xml b/sharding-jdbc-core/src/test/resources/com/dangdang/ddframe/rdb/sharding/parser/mysql/statement/insert.xml index d7ff12783e96b..43952eafe5791 100644 --- a/sharding-jdbc-core/src/test/resources/com/dangdang/ddframe/rdb/sharding/parser/mysql/statement/insert.xml +++ b/sharding-jdbc-core/src/test/resources/com/dangdang/ddframe/rdb/sharding/parser/mysql/statement/insert.xml @@ -15,6 +15,54 @@ + + + +
+ + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + + + + + + + + + + @@ -56,4 +104,6 @@ + +