-
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.
fix #1205, add SQLExecutePrepareTemplate & SQLExecutePrepareCallback
- Loading branch information
Showing
5 changed files
with
193 additions
and
76 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
.../src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareCallback.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,52 @@ | ||
/* | ||
* 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.sql.prepare; | ||
|
||
import io.shardingsphere.core.executor.sql.StatementExecuteUnit; | ||
import io.shardingsphere.core.routing.SQLExecutionUnit; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* SQL execute prepare callback. | ||
* | ||
* @author zhangliang | ||
*/ | ||
public interface SQLExecutePrepareCallback { | ||
|
||
/** | ||
* Get connection. | ||
* | ||
* @param dataSourceName data source name | ||
* @return connection | ||
* @throws SQLException SQL exception | ||
*/ | ||
Connection getConnection(String dataSourceName) throws SQLException; | ||
|
||
/** | ||
* Create statement execute unit. | ||
* | ||
* @param connection connection | ||
* @param isReturnGeneratedKeys is return generated keys | ||
* @param sqlExecutionUnit SQL execution unit | ||
* @return statement execute unit | ||
* @throws SQLException SQL exception | ||
*/ | ||
StatementExecuteUnit createStatementExecuteUnit(Connection connection, boolean isReturnGeneratedKeys, SQLExecutionUnit sqlExecutionUnit) throws SQLException; | ||
} |
82 changes: 82 additions & 0 deletions
82
.../src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.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,82 @@ | ||
/* | ||
* 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.sql.prepare; | ||
|
||
import com.google.common.collect.Lists; | ||
import io.shardingsphere.core.executor.sql.StatementExecuteUnit; | ||
import io.shardingsphere.core.routing.SQLExecutionUnit; | ||
import io.shardingsphere.core.routing.SQLUnit; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
/** | ||
* SQL execute prepare template. | ||
* | ||
* @author zhaojun | ||
* @author zhangliang | ||
*/ | ||
@RequiredArgsConstructor | ||
public final class SQLExecutePrepareTemplate { | ||
|
||
private final int maxConnectionsSizePerQuery; | ||
|
||
/** | ||
* Get statement execute units. | ||
* | ||
* @param sqlUnitGroups SQL unit groups | ||
* @param isReturnGeneratedKeys is return generated keys | ||
* @param callback SQL execute prepare callback | ||
* @return key is data source name, value is statement execute unit groups | ||
* @throws SQLException SQL exception | ||
*/ | ||
public Map<String, List<List<StatementExecuteUnit>>> getStatementExecuteUnits( | ||
final Map<String, List<SQLUnit>> sqlUnitGroups, final boolean isReturnGeneratedKeys, final SQLExecutePrepareCallback callback) throws SQLException { | ||
Map<String, List<List<StatementExecuteUnit>>> result = new HashMap<>(sqlUnitGroups.size(), 1); | ||
for (Entry<String, List<SQLUnit>> entry : sqlUnitGroups.entrySet()) { | ||
result.put(entry.getKey(), partitionSQLUnits(entry.getKey(), entry.getValue(), isReturnGeneratedKeys, callback)); | ||
} | ||
return result; | ||
} | ||
|
||
private List<List<StatementExecuteUnit>> partitionSQLUnits( | ||
final String dataSourceName, final List<SQLUnit> sqlUnits, final boolean isReturnGeneratedKeys, final SQLExecutePrepareCallback callback) throws SQLException { | ||
List<List<StatementExecuteUnit>> result = new LinkedList<>(); | ||
int desiredPartitionSize = Math.max(sqlUnits.size() / maxConnectionsSizePerQuery, 1); | ||
for (List<SQLUnit> each : Lists.partition(sqlUnits, desiredPartitionSize)) { | ||
// TODO get connection sync to prevent dead lock | ||
result.add(getStatementExecuteUnitGroup(callback.getConnection(dataSourceName), dataSourceName, isReturnGeneratedKeys, each, callback)); | ||
} | ||
return result; | ||
} | ||
|
||
private List<StatementExecuteUnit> getStatementExecuteUnitGroup(final Connection connection, final String dataSourceName, final boolean isReturnGeneratedKeys, | ||
final List<SQLUnit> sqlUnitGroup, final SQLExecutePrepareCallback callback) throws SQLException { | ||
List<StatementExecuteUnit> result = new LinkedList<>(); | ||
for (SQLUnit each : sqlUnitGroup) { | ||
result.add(callback.createStatementExecuteUnit(connection, isReturnGeneratedKeys, new SQLExecutionUnit(dataSourceName, each))); | ||
} | ||
return result; | ||
} | ||
} |
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
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.