Search in sources :

Example 6 with InsertStatement

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

the class MergeEngineFactoryTest method assertNewInstanceWithOtherStatement.

@Test(expected = UnsupportedOperationException.class)
public void assertNewInstanceWithOtherStatement() throws SQLException {
    SQLStatement insertStatement = new InsertStatement();
    MergeEngineFactory.newInstance(null, queryResults, insertStatement);
}
Also used : SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Test(org.junit.Test)

Example 7 with InsertStatement

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

the class ParsingSQLRouter method route.

@Override
public SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) {
    SQLRouteResult result = new SQLRouteResult(sqlStatement);
    if (sqlStatement instanceof InsertStatement && null != ((InsertStatement) sqlStatement).getGeneratedKey()) {
        processGeneratedKey(parameters, (InsertStatement) sqlStatement, result);
    }
    RoutingResult routingResult = route(parameters, sqlStatement);
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, logicSQL, databaseType, sqlStatement);
    boolean isSingleRouting = routingResult.isSingleRouting();
    if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getLimit()) {
        processLimit(parameters, (SelectStatement) sqlStatement, isSingleRouting);
    }
    SQLBuilder sqlBuilder = rewriteEngine.rewrite(!isSingleRouting);
    if (routingResult instanceof CartesianRoutingResult) {
        for (CartesianDataSource cartesianDataSource : ((CartesianRoutingResult) routingResult).getRoutingDataSources()) {
            for (CartesianTableReference cartesianTableReference : cartesianDataSource.getRoutingTableReferences()) {
                result.getExecutionUnits().add(new SQLExecutionUnit(cartesianDataSource.getDataSource(), rewriteEngine.generateSQL(cartesianTableReference, sqlBuilder)));
            }
        }
    } else {
        for (TableUnit each : routingResult.getTableUnits().getTableUnits()) {
            result.getExecutionUnits().add(new SQLExecutionUnit(each.getDataSourceName(), rewriteEngine.generateSQL(each, sqlBuilder)));
        }
    }
    if (showSQL) {
        SQLLogger.logSQL(logicSQL, sqlStatement, result.getExecutionUnits(), parameters);
    }
    return result;
}
Also used : RoutingResult(io.shardingjdbc.core.routing.type.RoutingResult) CartesianRoutingResult(io.shardingjdbc.core.routing.type.complex.CartesianRoutingResult) SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) SQLBuilder(io.shardingjdbc.core.rewrite.SQLBuilder) SQLRouteResult(io.shardingjdbc.core.routing.SQLRouteResult) CartesianRoutingResult(io.shardingjdbc.core.routing.type.complex.CartesianRoutingResult) CartesianDataSource(io.shardingjdbc.core.routing.type.complex.CartesianDataSource) CartesianTableReference(io.shardingjdbc.core.routing.type.complex.CartesianTableReference) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) SQLRewriteEngine(io.shardingjdbc.core.rewrite.SQLRewriteEngine) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) TableUnit(io.shardingjdbc.core.routing.type.TableUnit)

Example 8 with InsertStatement

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

the class InsertStatementParserTest method assertParseWithGenerateKeyColumnsWithoutParameter.

@Test
public void assertParseWithGenerateKeyColumnsWithoutParameter() {
    ShardingRule shardingRule = createShardingRuleWithGenerateKeyColumns();
    SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO `TABLE_XXX` (`field1`) VALUES (10)", 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 9 with InsertStatement

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

the class SQLExecuteBackendHandler method executeUpdate.

private List<DatabaseProtocolPacket> executeUpdate(final DataSource dataSource, final String sql, final SQLStatement sqlStatement) {
    try (Connection conn = dataSource.getConnection();
        Statement statement = conn.createStatement()) {
        int affectedRows;
        long lastInsertId = 0;
        if (sqlStatement instanceof InsertStatement) {
            affectedRows = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
            lastInsertId = getGeneratedKey(statement);
        } else {
            affectedRows = statement.executeUpdate(sql);
        }
        return Collections.<DatabaseProtocolPacket>singletonList(new OKPacket(1, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
    } catch (final SQLException ex) {
        return Collections.<DatabaseProtocolPacket>singletonList(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage()));
    }
}
Also used : SQLException(java.sql.SQLException) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ErrPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) OKPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)

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