/* * 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 io.shardingjdbc.core.parsing.parser.sql; import io.shardingjdbc.core.api.config.ShardingRuleConfiguration; import io.shardingjdbc.core.api.config.TableRuleConfiguration; import io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration; import io.shardingjdbc.core.rule.ShardingRule; import io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue; import io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm; import io.shardingjdbc.core.constant.DatabaseType; import io.shardingjdbc.core.constant.ShardingOperator; import io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator; import io.shardingjdbc.core.parsing.SQLParsingEngine; import io.shardingjdbc.core.parsing.parser.context.condition.Column; import io.shardingjdbc.core.parsing.parser.context.condition.Condition; import io.shardingjdbc.core.parsing.parser.exception.SQLParsingUnsupportedException; import io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement; import org.junit.Test; import javax.sql.DataSource; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.SQLException; import java.util.Collections; import java.util.HashMap; import java.util.Map; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public final class InsertStatementParserTest extends AbstractStatementParserTest { @Test public void assertParseWithoutParameter() throws SQLException { ShardingRule shardingRule = createShardingRule(); SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO `TABLE_XXX` (`field1`, `field2`) VALUES (10, 1)", shardingRule); InsertStatement insertStatement = (InsertStatement) statementParser.parse(); assertInsertStatementWithoutParameter(insertStatement); } @Test public void assertParseWithParameter() throws SQLException { ShardingRule shardingRule = createShardingRule(); SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO TABLE_XXX (field1, field2) VALUES (?, ?)", shardingRule); InsertStatement insertStatement = (InsertStatement) statementParser.parse(); assertInsertStatementWithParameter(insertStatement); } @Test public void assertParseWithGenerateKeyColumnsWithoutParameter() throws SQLException { ShardingRule shardingRule = createShardingRuleWithGenerateKeyColumns(); SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO `TABLE_XXX` (`field1`) VALUES (10)", shardingRule); InsertStatement insertStatement = (InsertStatement) statementParser.parse(); assertInsertStatementWithoutParameter(insertStatement); } @SuppressWarnings("unchecked") private void assertInsertStatementWithoutParameter(final InsertStatement insertStatement) { assertThat(insertStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX")); Condition condition = insertStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get(); assertThat(condition.getOperator(), is(ShardingOperator.EQUAL)); assertThat(((ListShardingValue extends Comparable>) condition.getShardingValue(Collections.emptyList())).getValues().iterator().next(), is((Comparable) 10)); } @Test public void assertParseWithGenerateKeyColumnsWithParameter() throws SQLException { ShardingRule shardingRule = createShardingRuleWithGenerateKeyColumns(); SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO `TABLE_XXX` (`field1`) VALUES (?)", shardingRule); InsertStatement insertStatement = (InsertStatement) statementParser.parse(); assertInsertStatementWithParameter(insertStatement); } private void assertInsertStatementWithParameter(final InsertStatement insertStatement) { assertThat(insertStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX")); Condition condition = insertStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get(); assertThat(condition.getOperator(), is(ShardingOperator.EQUAL)); assertThat(((ListShardingValue extends Comparable>) condition.getShardingValue(Collections.