Skip to content

Commit

Permalink
fix apache#13
Browse files Browse the repository at this point in the history
  • Loading branch information
hanahmily committed Feb 26, 2016
1 parent b78ca05 commit f63b74e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ public int[] executeBatch() throws SQLException {
int[] result = new int[batchParameters.size()];
int i = 0;
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.dangdang.ddframe.rdb.sharding.parser.visitor.basic.mysql;

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.google.common.base.Optional;

Expand All @@ -34,6 +35,15 @@ public boolean visit(final MySqlInsertStatement x) {
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());
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@

package com.dangdang.ddframe.rdb.sharding.parser;

import java.util.Arrays;
import java.util.Collections;

import org.junit.Test;

import com.dangdang.ddframe.rdb.sharding.api.DatabaseType;
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 {

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test(expected = SQLParserException.class)
public void assertCreate() throws SQLParserException {
SQLParserFactory.create(DatabaseType.MySQL, "CREATE TABLE `order` (id BIGINT(10))", Collections.emptyList(), Collections.<String>emptyList());
Expand All @@ -45,4 +51,18 @@ public void assertTruncate() throws SQLParserException {
public void assertAlter() throws SQLParserException {
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();
}
}

0 comments on commit f63b74e

Please sign in to comment.