Loading sharding-jdbc/src/main/java/io/shardingsphere/core/executor/JDBCExecutor.javadeleted 100644 → 0 +0 −39 Original line number Diff line number Diff line /* * Copyright 2016-2018 shardingsphere.io. * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * </p> */ package io.shardingsphere.core.executor; import java.sql.SQLException; /** * JDBC execute callback interface. * * @author zhangliang * * @param <T> class type of return value */ public interface JDBCExecutor<T> { /** * execute JDBC. * * @param baseStatementUnit base statement unit * @return execute result * @throws SQLException SQL exception */ T execute(BaseStatementUnit baseStatementUnit) throws SQLException; } sharding-jdbc/src/main/java/io/shardingsphere/core/executor/SQLExecuteCallback.java +5 −5 Original line number Diff line number Diff line Loading @@ -40,7 +40,7 @@ import java.util.Map; * @param <T> class type of return value */ @RequiredArgsConstructor public class SQLExecuteCallback<T> implements ShardingExecuteCallback<BaseStatementUnit, T> { public abstract class SQLExecuteCallback<T> implements ShardingExecuteCallback<BaseStatementUnit, T> { private final SQLType sqlType; Loading @@ -48,12 +48,10 @@ public class SQLExecuteCallback<T> implements ShardingExecuteCallback<BaseStatem private final Map<String, Object> dataMap; private final JDBCExecutor<T> jdbcCallback; private final EventBus shardingEventBus = ShardingEventBusInstance.getInstance(); @Override public T execute(final BaseStatementUnit input) throws Exception { public final T execute(final BaseStatementUnit input) throws Exception { ExecutorExceptionHandler.setExceptionThrown(isExceptionThrown); ExecutorDataMap.setDataMap(dataMap); List<SQLExecutionEvent> events = new LinkedList<>(); Loading @@ -63,7 +61,7 @@ public class SQLExecuteCallback<T> implements ShardingExecuteCallback<BaseStatem shardingEventBus.post(event); } try { T result = jdbcCallback.execute(input); T result = executeSQL(input); for (SQLExecutionEvent each : events) { each.setExecuteSuccess(); shardingEventBus.post(each); Loading @@ -78,4 +76,6 @@ public class SQLExecuteCallback<T> implements ShardingExecuteCallback<BaseStatem return null; } } protected abstract T executeSQL(BaseStatementUnit baseStatementUnit) throws SQLException; } sharding-jdbc/src/main/java/io/shardingsphere/core/executor/type/batch/BatchPreparedStatementExecutor.java +5 −6 Original line number Diff line number Diff line Loading @@ -22,7 +22,6 @@ import io.shardingsphere.core.constant.SQLType; import io.shardingsphere.core.executor.BaseStatementUnit; import io.shardingsphere.core.executor.SQLExecuteCallback; import io.shardingsphere.core.executor.SQLExecutorEngine; import io.shardingsphere.core.executor.JDBCExecutor; import io.shardingsphere.core.executor.threadlocal.ExecutorDataMap; import io.shardingsphere.core.executor.threadlocal.ExecutorExceptionHandler; import lombok.RequiredArgsConstructor; Loading Loading @@ -60,14 +59,14 @@ public final class BatchPreparedStatementExecutor { public int[] executeBatch() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<int[]> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<int[]>() { SQLExecuteCallback<int[]> callback = new SQLExecuteCallback<int[]>(sqlType, isExceptionThrown, dataMap) { @Override public int[] execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected int[] executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return baseStatementUnit.getStatement().executeBatch(); } }); return accumulate(executorEngine.execute(batchPreparedStatementUnits, executeCallback)); }; return accumulate(executorEngine.execute(batchPreparedStatementUnits, callback)); } private int[] accumulate(final List<int[]> results) { Loading sharding-jdbc/src/main/java/io/shardingsphere/core/executor/type/prepared/PreparedStatementExecutor.java +11 −12 Original line number Diff line number Diff line Loading @@ -21,7 +21,6 @@ import io.shardingsphere.core.constant.SQLType; import io.shardingsphere.core.executor.BaseStatementUnit; import io.shardingsphere.core.executor.SQLExecuteCallback; import io.shardingsphere.core.executor.SQLExecutorEngine; import io.shardingsphere.core.executor.JDBCExecutor; import io.shardingsphere.core.executor.threadlocal.ExecutorDataMap; import io.shardingsphere.core.executor.threadlocal.ExecutorExceptionHandler; import lombok.RequiredArgsConstructor; Loading Loading @@ -58,13 +57,13 @@ public final class PreparedStatementExecutor { public List<ResultSet> executeQuery() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<ResultSet> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<ResultSet>() { SQLExecuteCallback<ResultSet> executeCallback = new SQLExecuteCallback<ResultSet>(sqlType, isExceptionThrown, dataMap) { @Override public ResultSet execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected ResultSet executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return ((PreparedStatement) baseStatementUnit.getStatement()).executeQuery(); } }); }; return executorEngine.execute(preparedStatementUnits, executeCallback); } Loading @@ -77,13 +76,13 @@ public final class PreparedStatementExecutor { public int executeUpdate() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<Integer>() { SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<Integer>(sqlType, isExceptionThrown, dataMap) { @Override public Integer execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected Integer executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return ((PreparedStatement) baseStatementUnit.getStatement()).executeUpdate(); } }); }; List<Integer> results = executorEngine.execute(preparedStatementUnits, executeCallback); return accumulate(results); } Loading @@ -105,13 +104,13 @@ public final class PreparedStatementExecutor { public boolean execute() throws SQLException { boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<Boolean> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<Boolean>() { SQLExecuteCallback<Boolean> executeCallback = new SQLExecuteCallback<Boolean>(sqlType, isExceptionThrown, dataMap) { @Override public Boolean execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected Boolean executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return ((PreparedStatement) baseStatementUnit.getStatement()).execute(); } }); }; List<Boolean> result = executorEngine.execute(preparedStatementUnits, executeCallback); if (null == result || result.isEmpty() || null == result.get(0)) { return false; Loading sharding-jdbc/src/main/java/io/shardingsphere/core/executor/type/statement/StatementExecutor.java +12 −13 Original line number Diff line number Diff line Loading @@ -21,7 +21,6 @@ import io.shardingsphere.core.constant.SQLType; import io.shardingsphere.core.executor.BaseStatementUnit; import io.shardingsphere.core.executor.SQLExecuteCallback; import io.shardingsphere.core.executor.SQLExecutorEngine; import io.shardingsphere.core.executor.JDBCExecutor; import io.shardingsphere.core.executor.threadlocal.ExecutorDataMap; import io.shardingsphere.core.executor.threadlocal.ExecutorExceptionHandler; import lombok.RequiredArgsConstructor; Loading Loading @@ -59,13 +58,13 @@ public final class StatementExecutor { public List<ResultSet> executeQuery() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<ResultSet> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<ResultSet>() { SQLExecuteCallback<ResultSet> executeCallback = new SQLExecuteCallback<ResultSet>(sqlType, isExceptionThrown, dataMap) { @Override public ResultSet execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected ResultSet executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return baseStatementUnit.getStatement().executeQuery(baseStatementUnit.getSqlExecutionUnit().getSqlUnit().getSql()); } }); }; return executorEngine.execute(statementUnits, executeCallback); } Loading Loading @@ -139,13 +138,13 @@ public final class StatementExecutor { private int executeUpdate(final Updater updater) throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<Integer>() { SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<Integer>(sqlType, isExceptionThrown, dataMap) { @Override public Integer execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected Integer executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return updater.executeUpdate(baseStatementUnit.getStatement(), baseStatementUnit.getSqlExecutionUnit().getSqlUnit().getSql()); } }); }; List<Integer> results = executorEngine.execute(statementUnits, executeCallback); return accumulate(results); } Loading Loading @@ -228,13 +227,13 @@ public final class StatementExecutor { private boolean execute(final Executor executor) throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<Boolean> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<Boolean>() { SQLExecuteCallback<Boolean> executeCallback = new SQLExecuteCallback<Boolean>(sqlType, isExceptionThrown, dataMap) { @Override public Boolean execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected Boolean executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return executor.execute(baseStatementUnit.getStatement(), baseStatementUnit.getSqlExecutionUnit().getSqlUnit().getSql()); } }); }; List<Boolean> result = executorEngine.execute(statementUnits, executeCallback); if (null == result || result.isEmpty() || null == result.get(0)) { return false; Loading Loading
sharding-jdbc/src/main/java/io/shardingsphere/core/executor/JDBCExecutor.javadeleted 100644 → 0 +0 −39 Original line number Diff line number Diff line /* * Copyright 2016-2018 shardingsphere.io. * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * </p> */ package io.shardingsphere.core.executor; import java.sql.SQLException; /** * JDBC execute callback interface. * * @author zhangliang * * @param <T> class type of return value */ public interface JDBCExecutor<T> { /** * execute JDBC. * * @param baseStatementUnit base statement unit * @return execute result * @throws SQLException SQL exception */ T execute(BaseStatementUnit baseStatementUnit) throws SQLException; }
sharding-jdbc/src/main/java/io/shardingsphere/core/executor/SQLExecuteCallback.java +5 −5 Original line number Diff line number Diff line Loading @@ -40,7 +40,7 @@ import java.util.Map; * @param <T> class type of return value */ @RequiredArgsConstructor public class SQLExecuteCallback<T> implements ShardingExecuteCallback<BaseStatementUnit, T> { public abstract class SQLExecuteCallback<T> implements ShardingExecuteCallback<BaseStatementUnit, T> { private final SQLType sqlType; Loading @@ -48,12 +48,10 @@ public class SQLExecuteCallback<T> implements ShardingExecuteCallback<BaseStatem private final Map<String, Object> dataMap; private final JDBCExecutor<T> jdbcCallback; private final EventBus shardingEventBus = ShardingEventBusInstance.getInstance(); @Override public T execute(final BaseStatementUnit input) throws Exception { public final T execute(final BaseStatementUnit input) throws Exception { ExecutorExceptionHandler.setExceptionThrown(isExceptionThrown); ExecutorDataMap.setDataMap(dataMap); List<SQLExecutionEvent> events = new LinkedList<>(); Loading @@ -63,7 +61,7 @@ public class SQLExecuteCallback<T> implements ShardingExecuteCallback<BaseStatem shardingEventBus.post(event); } try { T result = jdbcCallback.execute(input); T result = executeSQL(input); for (SQLExecutionEvent each : events) { each.setExecuteSuccess(); shardingEventBus.post(each); Loading @@ -78,4 +76,6 @@ public class SQLExecuteCallback<T> implements ShardingExecuteCallback<BaseStatem return null; } } protected abstract T executeSQL(BaseStatementUnit baseStatementUnit) throws SQLException; }
sharding-jdbc/src/main/java/io/shardingsphere/core/executor/type/batch/BatchPreparedStatementExecutor.java +5 −6 Original line number Diff line number Diff line Loading @@ -22,7 +22,6 @@ import io.shardingsphere.core.constant.SQLType; import io.shardingsphere.core.executor.BaseStatementUnit; import io.shardingsphere.core.executor.SQLExecuteCallback; import io.shardingsphere.core.executor.SQLExecutorEngine; import io.shardingsphere.core.executor.JDBCExecutor; import io.shardingsphere.core.executor.threadlocal.ExecutorDataMap; import io.shardingsphere.core.executor.threadlocal.ExecutorExceptionHandler; import lombok.RequiredArgsConstructor; Loading Loading @@ -60,14 +59,14 @@ public final class BatchPreparedStatementExecutor { public int[] executeBatch() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<int[]> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<int[]>() { SQLExecuteCallback<int[]> callback = new SQLExecuteCallback<int[]>(sqlType, isExceptionThrown, dataMap) { @Override public int[] execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected int[] executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return baseStatementUnit.getStatement().executeBatch(); } }); return accumulate(executorEngine.execute(batchPreparedStatementUnits, executeCallback)); }; return accumulate(executorEngine.execute(batchPreparedStatementUnits, callback)); } private int[] accumulate(final List<int[]> results) { Loading
sharding-jdbc/src/main/java/io/shardingsphere/core/executor/type/prepared/PreparedStatementExecutor.java +11 −12 Original line number Diff line number Diff line Loading @@ -21,7 +21,6 @@ import io.shardingsphere.core.constant.SQLType; import io.shardingsphere.core.executor.BaseStatementUnit; import io.shardingsphere.core.executor.SQLExecuteCallback; import io.shardingsphere.core.executor.SQLExecutorEngine; import io.shardingsphere.core.executor.JDBCExecutor; import io.shardingsphere.core.executor.threadlocal.ExecutorDataMap; import io.shardingsphere.core.executor.threadlocal.ExecutorExceptionHandler; import lombok.RequiredArgsConstructor; Loading Loading @@ -58,13 +57,13 @@ public final class PreparedStatementExecutor { public List<ResultSet> executeQuery() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<ResultSet> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<ResultSet>() { SQLExecuteCallback<ResultSet> executeCallback = new SQLExecuteCallback<ResultSet>(sqlType, isExceptionThrown, dataMap) { @Override public ResultSet execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected ResultSet executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return ((PreparedStatement) baseStatementUnit.getStatement()).executeQuery(); } }); }; return executorEngine.execute(preparedStatementUnits, executeCallback); } Loading @@ -77,13 +76,13 @@ public final class PreparedStatementExecutor { public int executeUpdate() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<Integer>() { SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<Integer>(sqlType, isExceptionThrown, dataMap) { @Override public Integer execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected Integer executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return ((PreparedStatement) baseStatementUnit.getStatement()).executeUpdate(); } }); }; List<Integer> results = executorEngine.execute(preparedStatementUnits, executeCallback); return accumulate(results); } Loading @@ -105,13 +104,13 @@ public final class PreparedStatementExecutor { public boolean execute() throws SQLException { boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<Boolean> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<Boolean>() { SQLExecuteCallback<Boolean> executeCallback = new SQLExecuteCallback<Boolean>(sqlType, isExceptionThrown, dataMap) { @Override public Boolean execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected Boolean executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return ((PreparedStatement) baseStatementUnit.getStatement()).execute(); } }); }; List<Boolean> result = executorEngine.execute(preparedStatementUnits, executeCallback); if (null == result || result.isEmpty() || null == result.get(0)) { return false; Loading
sharding-jdbc/src/main/java/io/shardingsphere/core/executor/type/statement/StatementExecutor.java +12 −13 Original line number Diff line number Diff line Loading @@ -21,7 +21,6 @@ import io.shardingsphere.core.constant.SQLType; import io.shardingsphere.core.executor.BaseStatementUnit; import io.shardingsphere.core.executor.SQLExecuteCallback; import io.shardingsphere.core.executor.SQLExecutorEngine; import io.shardingsphere.core.executor.JDBCExecutor; import io.shardingsphere.core.executor.threadlocal.ExecutorDataMap; import io.shardingsphere.core.executor.threadlocal.ExecutorExceptionHandler; import lombok.RequiredArgsConstructor; Loading Loading @@ -59,13 +58,13 @@ public final class StatementExecutor { public List<ResultSet> executeQuery() throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<ResultSet> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<ResultSet>() { SQLExecuteCallback<ResultSet> executeCallback = new SQLExecuteCallback<ResultSet>(sqlType, isExceptionThrown, dataMap) { @Override public ResultSet execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected ResultSet executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return baseStatementUnit.getStatement().executeQuery(baseStatementUnit.getSqlExecutionUnit().getSqlUnit().getSql()); } }); }; return executorEngine.execute(statementUnits, executeCallback); } Loading Loading @@ -139,13 +138,13 @@ public final class StatementExecutor { private int executeUpdate(final Updater updater) throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<Integer>() { SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<Integer>(sqlType, isExceptionThrown, dataMap) { @Override public Integer execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected Integer executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return updater.executeUpdate(baseStatementUnit.getStatement(), baseStatementUnit.getSqlExecutionUnit().getSqlUnit().getSql()); } }); }; List<Integer> results = executorEngine.execute(statementUnits, executeCallback); return accumulate(results); } Loading Loading @@ -228,13 +227,13 @@ public final class StatementExecutor { private boolean execute(final Executor executor) throws SQLException { final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); final Map<String, Object> dataMap = ExecutorDataMap.getDataMap(); SQLExecuteCallback<Boolean> executeCallback = new SQLExecuteCallback<>(sqlType, isExceptionThrown, dataMap, new JDBCExecutor<Boolean>() { SQLExecuteCallback<Boolean> executeCallback = new SQLExecuteCallback<Boolean>(sqlType, isExceptionThrown, dataMap) { @Override public Boolean execute(final BaseStatementUnit baseStatementUnit) throws SQLException { protected Boolean executeSQL(final BaseStatementUnit baseStatementUnit) throws SQLException { return executor.execute(baseStatementUnit.getStatement(), baseStatementUnit.getSqlExecutionUnit().getSqlUnit().getSql()); } }); }; List<Boolean> result = executorEngine.execute(statementUnits, executeCallback); if (null == result || result.isEmpty() || null == result.get(0)) { return false; Loading