Search in sources :

Example 1 with PreparedStatementUnit

use of io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit in project sharding-jdbc by shardingjdbc.

the class ShardingPreparedStatement method executeQuery.

@Override
public ResultSet executeQuery() throws SQLException {
    ResultSet result;
    try {
        Collection<PreparedStatementUnit> preparedStatementUnits = route();
        List<ResultSet> resultSets = new PreparedStatementExecutor(getConnection().getShardingContext().getExecutorEngine(), routeResult.getSqlStatement().getType(), preparedStatementUnits, getParameters()).executeQuery();
        List<QueryResult> queryResults = new ArrayList<>(resultSets.size());
        for (ResultSet each : resultSets) {
            queryResults.add(new JDBCQueryResult(each));
        }
        MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getShardingContext().getShardingRule(), queryResults, routeResult.getSqlStatement());
        result = new ShardingResultSet(resultSets, mergeEngine.merge(), this);
    } finally {
        clearBatch();
    }
    currentResultSet = result;
    return result;
}
Also used : BatchPreparedStatementExecutor(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor) PreparedStatementExecutor(io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor) QueryResult(io.shardingjdbc.core.merger.QueryResult) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult) MergeEngine(io.shardingjdbc.core.merger.MergeEngine) ShardingResultSet(io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet) ResultSet(java.sql.ResultSet) GeneratedKeysResultSet(io.shardingjdbc.core.jdbc.core.resultset.GeneratedKeysResultSet) ShardingResultSet(io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet) ArrayList(java.util.ArrayList) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult) PreparedStatementUnit(io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit) BatchPreparedStatementUnit(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)

Example 2 with PreparedStatementUnit

use of io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit in project sharding-jdbc by shardingjdbc.

the class ShardingPreparedStatement method route.

private Collection<PreparedStatementUnit> route() throws SQLException {
    Collection<PreparedStatementUnit> result = new LinkedList<>();
    routeResult = routingEngine.route(getParameters());
    for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
        SQLType sqlType = routeResult.getSqlStatement().getType();
        Collection<PreparedStatement> preparedStatements;
        if (SQLType.DDL == sqlType) {
            preparedStatements = generatePreparedStatementForDDL(each);
        } else {
            preparedStatements = Collections.singletonList(generatePreparedStatement(each));
        }
        routedStatements.addAll(preparedStatements);
        for (PreparedStatement preparedStatement : preparedStatements) {
            replaySetParameter(preparedStatement);
            result.add(new PreparedStatementUnit(each, preparedStatement));
        }
    }
    return result;
}
Also used : SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) PreparedStatement(java.sql.PreparedStatement) PreparedStatementUnit(io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit) BatchPreparedStatementUnit(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit) SQLType(io.shardingjdbc.core.constant.SQLType) LinkedList(java.util.LinkedList)

Example 3 with PreparedStatementUnit

use of io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit in project sharding-jdbc by shardingjdbc.

the class PreparedStatementExecutorTest method createPreparedStatementUnits.

private Collection<PreparedStatementUnit> createPreparedStatementUnits(final String sql, final PreparedStatement preparedStatement, final String dataSource) {
    Collection<PreparedStatementUnit> result = new LinkedList<>();
    SQLBuilder sqlBuilder = new SQLBuilder();
    sqlBuilder.appendLiterals(sql);
    result.add(new PreparedStatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(Collections.<String, String>emptyMap(), null)), preparedStatement));
    return result;
}
Also used : SQLBuilder(io.shardingjdbc.core.rewrite.SQLBuilder) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) PreparedStatementUnit(io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit) LinkedList(java.util.LinkedList)

Aggregations

PreparedStatementUnit (io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit)3 BatchPreparedStatementUnit (io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)2 SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)2 LinkedList (java.util.LinkedList)2 SQLType (io.shardingjdbc.core.constant.SQLType)1 BatchPreparedStatementExecutor (io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor)1 PreparedStatementExecutor (io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor)1 GeneratedKeysResultSet (io.shardingjdbc.core.jdbc.core.resultset.GeneratedKeysResultSet)1 ShardingResultSet (io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet)1 JDBCQueryResult (io.shardingjdbc.core.merger.JDBCQueryResult)1 MergeEngine (io.shardingjdbc.core.merger.MergeEngine)1 QueryResult (io.shardingjdbc.core.merger.QueryResult)1 SQLBuilder (io.shardingjdbc.core.rewrite.SQLBuilder)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1