-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1269 from tristaZero/dev
Add AbstractStatementExecutor
- Loading branch information
Showing
8 changed files
with
187 additions
and
271 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
sharding-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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 io.shardingsphere.core.constant.DatabaseType; | ||
import io.shardingsphere.core.constant.SQLType; | ||
import io.shardingsphere.core.executor.sql.execute.SQLExecuteCallback; | ||
import io.shardingsphere.core.executor.sql.execute.SQLExecuteTemplate; | ||
import io.shardingsphere.core.executor.sql.prepare.SQLExecutePrepareTemplate; | ||
import io.shardingsphere.core.jdbc.core.connection.ShardingConnection; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
/** | ||
* Abstract statement executor. | ||
* | ||
* @author panjuan | ||
*/ | ||
@Getter(AccessLevel.PROTECTED) | ||
public class AbstractStatementExecutor { | ||
|
||
private final DatabaseType databaseType; | ||
|
||
protected SQLType sqlType; | ||
|
||
@Getter | ||
private final int resultSetType; | ||
|
||
@Getter | ||
private final int resultSetConcurrency; | ||
|
||
@Getter | ||
private final int resultSetHoldability; | ||
|
||
private final ShardingConnection connection; | ||
|
||
private final SQLExecutePrepareTemplate sqlExecutePrepareTemplate; | ||
|
||
private final SQLExecuteTemplate sqlExecuteTemplate; | ||
|
||
private final Collection<Connection> connections = new LinkedList<>(); | ||
|
||
@Getter | ||
private final List<List<Object>> parameterSets = new LinkedList<>(); | ||
|
||
@Getter | ||
private final List<Statement> statements = new LinkedList<>(); | ||
|
||
@Getter | ||
private final List<ResultSet> resultSets = new CopyOnWriteArrayList<>(); | ||
|
||
private final Collection<ShardingExecuteGroup<StatementExecuteUnit>> executeGroups = new LinkedList<>(); | ||
|
||
public AbstractStatementExecutor(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability, final ShardingConnection shardingConnection) { | ||
this.databaseType = shardingConnection.getShardingDataSource().getShardingContext().getDatabaseType(); | ||
this.resultSetType = resultSetType; | ||
this.resultSetConcurrency = resultSetConcurrency; | ||
this.resultSetHoldability = resultSetHoldability; | ||
this.connection = shardingConnection; | ||
sqlExecuteTemplate = new SQLExecuteTemplate(connection.getShardingDataSource().getShardingContext().getExecuteEngine()); | ||
sqlExecutePrepareTemplate = new SQLExecutePrepareTemplate(connection.getShardingDataSource().getShardingContext().getMaxConnectionsSizePerQuery()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected <T> List<T> executeCallback(final SQLExecuteCallback<T> executeCallback) throws SQLException { | ||
return sqlExecuteTemplate.executeGroup((Collection) executeGroups, executeCallback); | ||
} | ||
|
||
/** | ||
* Clear data. | ||
* | ||
* @throws SQLException sql exception | ||
*/ | ||
public void clear() throws SQLException { | ||
clearStatements(); | ||
clearConnections(); | ||
statements.clear(); | ||
parameterSets.clear(); | ||
connections.clear(); | ||
resultSets.clear(); | ||
executeGroups.clear(); | ||
} | ||
|
||
private void clearStatements() throws SQLException { | ||
for (Statement each : getStatements()) { | ||
each.close(); | ||
} | ||
} | ||
|
||
private void clearConnections() { | ||
for (Connection each : connections) { | ||
connection.release(each); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.