Search in sources :

Example 1 with InsertStatement

use of io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement in project sharding-jdbc by shardingjdbc.

the class ParsingSQLRouter method parse.

@Override
public SQLStatement parse(final String logicSQL) {
    SQLParsingEngine parsingEngine = new SQLParsingEngine(databaseType, logicSQL, shardingRule);
    SQLStatement result = parsingEngine.parse();
    if (result instanceof InsertStatement) {
        ((InsertStatement) result).appendGenerateKeyToken(shardingRule);
    }
    return result;
}
Also used : SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)

Example 2 with InsertStatement

use of io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement in project sharding-jdbc by shardingjdbc.

the class InsertStatementParserTest method parseWithSpecialSyntax.

@SuppressWarnings("unchecked")
private void parseWithSpecialSyntax(final DatabaseType dbType, final String actualSQL) {
    ShardingRule shardingRule = createShardingRule();
    InsertStatement insertStatement = (InsertStatement) new SQLParsingEngine(dbType, actualSQL, shardingRule).parse();
    assertThat(insertStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
    assertFalse(insertStatement.getTables().find("TABLE_XXX").get().getAlias().isPresent());
    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) 1));
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)

Example 3 with InsertStatement

use of io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement in project sharding-jdbc by shardingjdbc.

the class InsertStatementParserTest method assertParseWithoutParameter.

@Test
public void assertParseWithoutParameter() {
    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);
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Test(org.junit.Test)

Example 4 with InsertStatement

use of io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement in project sharding-jdbc by shardingjdbc.

the class InsertStatementParserTest method assertParseWithParameter.

@Test
public void assertParseWithParameter() {
    ShardingRule shardingRule = createShardingRule();
    SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO TABLE_XXX (field1, field2) VALUES (?, ?)", shardingRule);
    InsertStatement insertStatement = (InsertStatement) statementParser.parse();
    assertInsertStatementWithParameter(insertStatement);
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Test(org.junit.Test)

Example 5 with InsertStatement

use of io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement in project sharding-jdbc by shardingjdbc.

the class InsertStatementParserTest method assertParseWithGenerateKeyColumnsWithParameter.

@Test
public void assertParseWithGenerateKeyColumnsWithParameter() {
    ShardingRule shardingRule = createShardingRuleWithGenerateKeyColumns();
    SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO `TABLE_XXX` (`field1`) VALUES (?)", shardingRule);
    InsertStatement insertStatement = (InsertStatement) statementParser.parse();
    assertInsertStatementWithParameter(insertStatement);
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Test(org.junit.Test)

Aggregations

InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)9 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)6 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)5 Test (org.junit.Test)5 SQLStatement (io.shardingjdbc.core.parsing.parser.sql.SQLStatement)3 Column (io.shardingjdbc.core.parsing.parser.context.condition.Column)1 Condition (io.shardingjdbc.core.parsing.parser.context.condition.Condition)1 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)1 SQLBuilder (io.shardingjdbc.core.rewrite.SQLBuilder)1 SQLRewriteEngine (io.shardingjdbc.core.rewrite.SQLRewriteEngine)1 SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)1 SQLRouteResult (io.shardingjdbc.core.routing.SQLRouteResult)1 RoutingResult (io.shardingjdbc.core.routing.type.RoutingResult)1 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)1 CartesianDataSource (io.shardingjdbc.core.routing.type.complex.CartesianDataSource)1 CartesianRoutingResult (io.shardingjdbc.core.routing.type.complex.CartesianRoutingResult)1 CartesianTableReference (io.shardingjdbc.core.routing.type.complex.CartesianTableReference)1 DatabaseProtocolPacket (io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket)1 ErrPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket)1 OKPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket)1