提交 f63b74e3 编写于 作者: G gaohongtao

fix #13

上级 b78ca054
...@@ -124,7 +124,9 @@ public final class ShardingPreparedStatement extends AbstractPreparedStatementAd ...@@ -124,7 +124,9 @@ public final class ShardingPreparedStatement extends AbstractPreparedStatementAd
int[] result = new int[batchParameters.size()]; int[] result = new int[batchParameters.size()];
int i = 0; int i = 0;
for (List<Object> each : batchParameters) { for (List<Object> each : batchParameters) {
result[i++] = new PreparedStatementExecutor(getShardingConnection().getContext().getExecutorEngine(), routeSQL(each)).executeUpdate(); List<PreparedStatement> routePreparedStatements = routeSQL(each);
cachedRoutedPreparedStatements.addAll(routePreparedStatements);
result[i++] = new PreparedStatementExecutor(getShardingConnection().getContext().getExecutorEngine(), routePreparedStatements).executeUpdate();
} }
return result; return result;
} }
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
package com.dangdang.ddframe.rdb.sharding.parser.visitor.basic.mysql; package com.dangdang.ddframe.rdb.sharding.parser.visitor.basic.mysql;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
import com.dangdang.ddframe.rdb.sharding.exception.SQLParserException;
import com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition.BinaryOperator; import com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition.BinaryOperator;
import com.google.common.base.Optional; import com.google.common.base.Optional;
...@@ -34,6 +35,15 @@ public class MySQLInsertVisitor extends AbstractMySQLVisitor { ...@@ -34,6 +35,15 @@ public class MySQLInsertVisitor extends AbstractMySQLVisitor {
for (int i = 0; i < x.getColumns().size(); i++) { 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()); getParseContext().addCondition(x.getColumns().get(i).toString(), x.getTableName().toString(), BinaryOperator.EQUAL, x.getValues().getValues().get(i), getDatabaseType(), getParameters());
} }
if (getParseContext().getCurrentConditionContext().isEmpty()) {
String errMsg;
if (x.getColumns().size() < 1) {
errMsg = "Insert statement DOES NOT contains column name.The syntax is : INSERT INTO tbl_name (col_name,...) VALUES (expr,...)";
} else {
errMsg = "Sharding columns DO NOT exist in insert column names";
}
throw new SQLParserException(errMsg);
}
return super.visit(x); return super.visit(x);
} }
} }
...@@ -17,15 +17,21 @@ ...@@ -17,15 +17,21 @@
package com.dangdang.ddframe.rdb.sharding.parser; package com.dangdang.ddframe.rdb.sharding.parser;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import org.junit.Test;
import com.dangdang.ddframe.rdb.sharding.api.DatabaseType; import com.dangdang.ddframe.rdb.sharding.api.DatabaseType;
import com.dangdang.ddframe.rdb.sharding.exception.SQLParserException; import com.dangdang.ddframe.rdb.sharding.exception.SQLParserException;
import com.google.common.collect.Lists;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public final class UnsupportedParseTest { public final class UnsupportedParseTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test(expected = SQLParserException.class) @Test(expected = SQLParserException.class)
public void assertCreate() throws SQLParserException { public void assertCreate() throws SQLParserException {
SQLParserFactory.create(DatabaseType.MySQL, "CREATE TABLE `order` (id BIGINT(10))", Collections.emptyList(), Collections.<String>emptyList()); SQLParserFactory.create(DatabaseType.MySQL, "CREATE TABLE `order` (id BIGINT(10))", Collections.emptyList(), Collections.<String>emptyList());
...@@ -45,4 +51,18 @@ public final class UnsupportedParseTest { ...@@ -45,4 +51,18 @@ public final class UnsupportedParseTest {
public void assertAlter() throws SQLParserException { public void assertAlter() throws SQLParserException {
SQLParserFactory.create(DatabaseType.MySQL, "ALTER TABLE `order` ADD COLUMN `other` VARCHAR(45)", Collections.emptyList(), Collections.<String>emptyList()); SQLParserFactory.create(DatabaseType.MySQL, "ALTER TABLE `order` ADD COLUMN `other` VARCHAR(45)", Collections.emptyList(), Collections.<String>emptyList());
} }
@Test
public void testWithoutColumnNames() {
expectedException.expect(SQLParserException.class);
expectedException.expectMessage("Insert statement DOES NOT contains column name.The syntax is : INSERT INTO tbl_name (col_name,...) VALUES (expr,...)");
SQLParserFactory.create(DatabaseType.MySQL, "insert into `t_order` values(1,2,'INSERT')", Lists.newArrayList(), Arrays.asList("order_id", "user_id")).parse();
}
@Test
public void testWithoutMissShardingKey() {
expectedException.expect(SQLParserException.class);
expectedException.expectMessage("Sharding columns DO NOT exist in insert column names");
SQLParserFactory.create(DatabaseType.MySQL, "insert into `t_order`(ORDER_ID, USER_ID) values(1,2,'INSERT')", Lists.newArrayList(), Arrays.asList("order_id", "user_id")).parse();
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册