1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.infra.executor.sql.execute.engine.raw;
19
20 import lombok.RequiredArgsConstructor;
21 import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
22 import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
23 import org.apache.shardingsphere.infra.executor.sql.execute.engine.SQLExecutorExceptionHandler;
24 import org.apache.shardingsphere.infra.executor.sql.execute.engine.raw.callback.RawSQLExecutorCallback;
25 import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
26 import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
27 import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
28 import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
29 import org.apache.shardingsphere.infra.session.query.QueryContext;
30
31 import java.sql.SQLException;
32 import java.util.Collections;
33 import java.util.List;
34
35
36
37
38 @RequiredArgsConstructor
39 public final class RawExecutor {
40
41 private final ExecutorEngine executorEngine;
42
43 private final ConnectionContext connectionContext;
44
45 private final ProcessEngine processEngine = new ProcessEngine();
46
47
48
49
50
51
52
53
54
55
56 public List<ExecuteResult> execute(final ExecutionGroupContext<RawSQLExecutionUnit> executionGroupContext,
57 final QueryContext queryContext, final RawSQLExecutorCallback callback) throws SQLException {
58 try {
59 processEngine.executeSQL(executionGroupContext, queryContext);
60
61 List<ExecuteResult> results = execute(executionGroupContext, (RawSQLExecutorCallback) null, callback);
62 return results.isEmpty() || null == results.get(0) ? Collections.singletonList(new UpdateResult(0, 0L)) : results;
63 } finally {
64 processEngine.completeSQLExecution(executionGroupContext.getReportContext().getProcessId());
65 }
66 }
67
68 @SuppressWarnings("unchecked")
69 private <T> List<T> execute(final ExecutionGroupContext<RawSQLExecutionUnit> executionGroupContext,
70 final RawSQLExecutorCallback firstCallback, final RawSQLExecutorCallback callback) throws SQLException {
71 try {
72 return (List<T>) executorEngine.execute(executionGroupContext, firstCallback, callback, connectionContext.getTransactionContext().isInTransaction());
73 } catch (final SQLException ex) {
74 SQLExecutorExceptionHandler.handleException(ex);
75 return Collections.emptyList();
76 }
77 }
78 }