1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.proxy.backend.connector.jdbc.executor;
19
20 import lombok.RequiredArgsConstructor;
21 import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
22 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
23 import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
24 import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
25 import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
26 import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
27 import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
28 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
29 import org.apache.shardingsphere.infra.session.query.QueryContext;
30 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
31 import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector;
32 import org.apache.shardingsphere.proxy.backend.connector.jdbc.executor.callback.ProxyJDBCExecutorCallbackFactory;
33 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
34 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
35
36 import java.sql.SQLException;
37 import java.util.List;
38
39
40
41
42 @RequiredArgsConstructor
43 public final class ProxyJDBCExecutor {
44
45 private final String type;
46
47 private final ConnectionSession connectionSession;
48
49 private final DatabaseConnector databaseConnector;
50
51 private final JDBCExecutor jdbcExecutor;
52
53 private final ProcessEngine processEngine = new ProcessEngine();
54
55
56
57
58
59
60
61
62
63
64
65 public List<ExecuteResult> execute(final QueryContext queryContext, final ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext,
66 final boolean isReturnGeneratedKeys, final boolean isExceptionThrown) throws SQLException {
67 try {
68 MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
69 ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(connectionSession.getDatabaseName());
70 DatabaseType protocolType = database.getProtocolType();
71 processEngine.executeSQL(executionGroupContext, queryContext);
72 SQLStatementContext context = queryContext.getSqlStatementContext();
73 return jdbcExecutor.execute(executionGroupContext,
74 ProxyJDBCExecutorCallbackFactory.newInstance(type, protocolType, database.getResourceMetaData(), context.getSqlStatement(), databaseConnector, isReturnGeneratedKeys,
75 isExceptionThrown,
76 true),
77 ProxyJDBCExecutorCallbackFactory.newInstance(type, protocolType, database.getResourceMetaData(), context.getSqlStatement(), databaseConnector, isReturnGeneratedKeys,
78 isExceptionThrown,
79 false));
80 } finally {
81 processEngine.completeSQLExecution(executionGroupContext.getReportContext().getProcessId());
82 }
83 }
84 }