Commit 853c69ef by yubg

sharding-jdbc

parents
# Created by .ignore support plugin (hsz.mobi)
### Eagle template
# Ignore list for Eagle, a PCB layout tool
# Backup files
*.s#?
*.b#?
*.l#?
*.b$?
*.s$?
*.l$?
# Eagle project file
# It contains a serial number and references to the file structure
# on your computer.
# comment the following line if you want to have your project file included.
eagle.epf
# Autorouter files
*.pro
*.job
# CAM files
*.$$$
*.cmp
*.ly2
*.l15
*.sol
*.plc
*.stc
*.sts
*.crc
*.crs
*.dri
*.drl
*.gpi
*.pls
*.ger
*.xln
*.drd
*.drd.*
*.s#*
*.b#*
*.info
*.eps
# file locks introduced since 7.x
*.lck
### Example user template template
### Example user template
# IntelliJ project files
.idea
*.iml
out
gen
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere</artifactId>
<version>4.0.0-RC1</version>
</parent>
<artifactId>sharding-jdbc</artifactId>
<name>${project.artifactId}</name>
<packaging>pom</packaging>
<modules>
<module>sharding-jdbc-core</module>
<module>sharding-jdbc-orchestration</module>
</modules>
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Releases Repository Pro</name>
<url>http://nexus.yidianling.com/repository/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshots Repository Pro</name>
<url>http://nexus.yidianling.com/repository/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc</artifactId>
<version>4.0.0-RC1</version>
</parent>
<artifactId>sharding-jdbc-core</artifactId>
<name>${project.artifactId}</name>
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-core-entry</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-transaction-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-sql-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java7</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
</dependencies>
</project>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.api;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.EncryptDataSource;
import javax.sql.DataSource;
/**
* Encrypt data source factory.
*
* @author panjuan
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EncryptDataSourceFactory {
/**
* Create encrypt data source.
*
* @param dataSource data source
* @param encryptRuleConfiguration encrypt rule configuration
* @return encrypt data source
*/
public static DataSource createDataSource(final DataSource dataSource, final EncryptRuleConfiguration encryptRuleConfiguration) {
return new EncryptDataSource(dataSource, encryptRuleConfiguration);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.api;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
/**
* Master-slave data source factory.
*
* @author zhangliang
* @author panjuan
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MasterSlaveDataSourceFactory {
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param masterSlaveRuleConfig master-slave rule configuration
* @param props props
* @return master-slave data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Properties props) throws SQLException {
return new MasterSlaveDataSource(dataSourceMap, masterSlaveRuleConfig, props);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.api;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
/**
* Sharding data source factory.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingDataSourceFactory {
/**
* Create sharding data source.
*
* @param dataSourceMap data source map
* @param shardingRuleConfig rule configuration for databases and tables sharding
* @param props properties for data source
* @return sharding data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(
final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, final Properties props) throws SQLException {
return new ShardingDataSource(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), props);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.api.yaml;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.apache.shardingsphere.core.yaml.config.encrypt.YamlRootEncryptRuleConfiguration;
import org.apache.shardingsphere.core.yaml.engine.YamlEngine;
import org.apache.shardingsphere.core.yaml.swapper.impl.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import javax.sql.DataSource;
import java.io.File;
/**
* Encrypt data source factory for YAML.
*
* @author panjuan
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class YamlEncryptDataSourceFactory {
/**
* Create encrypt data source.
*
* @param yamlFile YAML file for encrypt rule configuration with data sources
* @return encrypt data source
*/
@SneakyThrows
public static DataSource createDataSource(final File yamlFile) {
YamlRootEncryptRuleConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootEncryptRuleConfiguration.class);
return EncryptDataSourceFactory.createDataSource(config.getDataSource(), new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule()));
}
/**
* Create encrypt data source.
*
* @param yamlBytes YAML bytes for encrypt rule configuration with data sources
* @return encrypt data source
*/
@SneakyThrows
public static DataSource createDataSource(final byte[] yamlBytes) {
YamlRootEncryptRuleConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootEncryptRuleConfiguration.class);
return EncryptDataSourceFactory.createDataSource(config.getDataSource(), new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule()));
}
/**
* Create encrypt data source.
*
* @param dataSource data source
* @param yamlFile YAML file for encrypt rule configuration without data sources
* @return encrypt data source
*/
@SneakyThrows
public static DataSource createDataSource(final DataSource dataSource, final File yamlFile) {
YamlRootEncryptRuleConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootEncryptRuleConfiguration.class);
return EncryptDataSourceFactory.createDataSource(dataSource, new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule()));
}
/**
* Create encrypt data source.
*
* @param dataSource data source
* @param yamlBytes YAML bytes for encrypt rule configuration without data sources
* @return encrypt data source
*/
@SneakyThrows
public static DataSource createDataSource(final DataSource dataSource, final byte[] yamlBytes) {
YamlRootEncryptRuleConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootEncryptRuleConfiguration.class);
return EncryptDataSourceFactory.createDataSource(dataSource, new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule()));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.api.yaml;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.core.yaml.config.masterslave.YamlRootMasterSlaveConfiguration;
import org.apache.shardingsphere.core.yaml.engine.YamlEngine;
import org.apache.shardingsphere.core.yaml.swapper.impl.MasterSlaveRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;
/**
* Master-slave data source factory for YAML.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class YamlMasterSlaveDataSourceFactory {
/**
* Create master-slave data source.
*
* @param yamlFile YAML file for master-slave rule configuration with data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final File yamlFile) throws SQLException, IOException {
YamlRootMasterSlaveConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootMasterSlaveConfiguration.class);
return MasterSlaveDataSourceFactory.createDataSource(config.getDataSources(), new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule()), config.getProps());
}
/**
* Create master-slave data source.
*
* @param yamlBytes YAML bytes for master-slave rule configuration with data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final byte[] yamlBytes) throws SQLException, IOException {
YamlRootMasterSlaveConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootMasterSlaveConfiguration.class);
return MasterSlaveDataSourceFactory.createDataSource(config.getDataSources(), new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule()), config.getProps());
}
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param yamlFile YAML file for master-slave rule configuration without data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws SQLException, IOException {
YamlRootMasterSlaveConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootMasterSlaveConfiguration.class);
return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule()), config.getProps());
}
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param yamlBytes YAML bytes for master-slave rule configuration without data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlBytes) throws SQLException, IOException {
YamlRootMasterSlaveConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootMasterSlaveConfiguration.class);
return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule()), config.getProps());
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.api.yaml;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.core.yaml.config.sharding.YamlRootShardingConfiguration;
import org.apache.shardingsphere.core.yaml.engine.YamlEngine;
import org.apache.shardingsphere.core.yaml.swapper.impl.ShardingRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;
/**
* Sharding data source factory for YAML.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class YamlShardingDataSourceFactory {
/**
* Create sharding data source.
*
* @param yamlFile YAML file for rule configuration of databases and tables sharding with data sources
* @return sharding data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final File yamlFile) throws SQLException, IOException {
YamlRootShardingConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootShardingConfiguration.class);
return ShardingDataSourceFactory.createDataSource(config.getDataSources(), new ShardingRuleConfigurationYamlSwapper().swap(config.getShardingRule()), config.getProps());
}
/**
* Create sharding data source.
*
* @param yamlBytes YAML bytes for rule configuration of databases and tables sharding with data sources
* @return sharding data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final byte[] yamlBytes) throws SQLException, IOException {
YamlRootShardingConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootShardingConfiguration.class);
return ShardingDataSourceFactory.createDataSource(config.getDataSources(), new ShardingRuleConfigurationYamlSwapper().swap(config.getShardingRule()), config.getProps());
}
/**
* Create sharding data source.
*
* @param dataSourceMap data source map
* @param yamlFile YAML file for rule configuration of databases and tables sharding without data sources
* @return sharding data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws SQLException, IOException {
YamlRootShardingConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootShardingConfiguration.class);
return ShardingDataSourceFactory.createDataSource(dataSourceMap, new ShardingRuleConfigurationYamlSwapper().swap(config.getShardingRule()), config.getProps());
}
/**
* Create sharding data source.
*
* @param dataSourceMap data source map
* @param yamlBytes YAML bytes for rule configuration of databases and tables sharding without data sources
* @return sharding data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlBytes) throws SQLException, IOException {
YamlRootShardingConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootShardingConfiguration.class);
return ShardingDataSourceFactory.createDataSource(dataSourceMap, new ShardingRuleConfigurationYamlSwapper().swap(config.getShardingRule()), config.getProps());
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.executor;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.constant.properties.ShardingProperties;
import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.apache.shardingsphere.core.execute.ShardingExecuteEngine;
import org.apache.shardingsphere.core.execute.ShardingExecuteGroup;
import org.apache.shardingsphere.core.execute.StatementExecuteUnit;
import org.apache.shardingsphere.core.execute.metadata.TableMetaDataInitializer;
import org.apache.shardingsphere.core.execute.sql.execute.SQLExecuteCallback;
import org.apache.shardingsphere.core.execute.sql.execute.SQLExecuteTemplate;
import org.apache.shardingsphere.core.execute.sql.prepare.SQLExecutePrepareTemplate;
import org.apache.shardingsphere.core.parse.antlr.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.antlr.sql.statement.ddl.AlterTableStatement;
import org.apache.shardingsphere.core.parse.antlr.sql.statement.ddl.CreateTableStatement;
import org.apache.shardingsphere.core.parse.antlr.sql.statement.ddl.DropTableStatement;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.ShardingContext;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingConnection;
import org.apache.shardingsphere.shardingjdbc.jdbc.metadata.JDBCTableMetaDataConnectionManager;
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
* @author maxiaoguang
*/
@Getter(AccessLevel.PROTECTED)
public class AbstractStatementExecutor {
private final DatabaseType databaseType;
@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
@Setter
private SQLStatement sqlStatement;
@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.getShardingContext().getDatabaseType();
this.resultSetType = resultSetType;
this.resultSetConcurrency = resultSetConcurrency;
this.resultSetHoldability = resultSetHoldability;
this.connection = shardingConnection;
int maxConnectionsSizePerQuery = connection.getShardingContext().getShardingProperties().<Integer>getValue(ShardingPropertiesConstant.MAX_CONNECTIONS_SIZE_PER_QUERY);
ShardingExecuteEngine executeEngine = connection.getShardingContext().getExecuteEngine();
sqlExecutePrepareTemplate = new SQLExecutePrepareTemplate(maxConnectionsSizePerQuery);
sqlExecuteTemplate = new SQLExecuteTemplate(executeEngine, connection.isSerialExecute());
}
protected final void cacheStatements() {
for (ShardingExecuteGroup<StatementExecuteUnit> each : executeGroups) {
statements.addAll(Lists.transform(each.getInputs(), new Function<StatementExecuteUnit, Statement>() {
@Override
public Statement apply(final StatementExecuteUnit input) {
return input.getStatement();
}
}));
parameterSets.addAll(Lists.transform(each.getInputs(), new Function<StatementExecuteUnit, List<Object>>() {
@Override
public List<Object> apply(final StatementExecuteUnit input) {
return input.getRouteUnit().getSqlUnit().getParameters();
}
}));
}
}
@SuppressWarnings("unchecked")
protected final <T> List<T> executeCallback(final SQLExecuteCallback<T> executeCallback) throws SQLException {
List<T> result = sqlExecuteTemplate.executeGroup((Collection) executeGroups, executeCallback);
refreshShardingMetaDataIfNeeded(connection.getShardingContext(), sqlStatement);
return result;
}
protected boolean isAccumulate() {
return !connection.getShardingContext().getShardingRule().isAllBroadcastTables(sqlStatement.getTables().getTableNames());
}
/**
* Clear data.
*
* @throws SQLException sql exception
*/
public void clear() throws SQLException {
clearStatements();
statements.clear();
parameterSets.clear();
connections.clear();
resultSets.clear();
executeGroups.clear();
}
private void clearStatements() throws SQLException {
for (Statement each : getStatements()) {
each.close();
}
}
private void refreshShardingMetaDataIfNeeded(final ShardingContext shardingContext, final SQLStatement sqlStatement) {
if (sqlStatement instanceof CreateTableStatement) {
refreshTableMetaData(shardingContext, (CreateTableStatement) sqlStatement);
} else if (sqlStatement instanceof AlterTableStatement) {
refreshTableMetaData(shardingContext, (AlterTableStatement) sqlStatement);
} else if (sqlStatement instanceof DropTableStatement) {
refreshTableMetaData(shardingContext, (DropTableStatement) sqlStatement);
}
}
private void refreshTableMetaData(final ShardingContext shardingContext, final CreateTableStatement createTableStatement) {
String tableName = createTableStatement.getTables().getSingleTableName();
shardingContext.getMetaData().getTable().put(tableName, getTableMetaDataInitializer().load(tableName, shardingContext.getShardingRule()));
}
private void refreshTableMetaData(final ShardingContext shardingContext, final AlterTableStatement alterTableStatement) {
String tableName = alterTableStatement.getTables().getSingleTableName();
shardingContext.getMetaData().getTable().put(tableName, getTableMetaDataInitializer().load(tableName, shardingContext.getShardingRule()));
}
private void refreshTableMetaData(final ShardingContext shardingContext, final DropTableStatement dropTableStatement) {
for (String each : dropTableStatement.getTables().getTableNames()) {
shardingContext.getMetaData().getTable().remove(each);
}
}
private TableMetaDataInitializer getTableMetaDataInitializer() {
ShardingProperties shardingProperties = connection.getShardingContext().getShardingProperties();
return new TableMetaDataInitializer(connection.getShardingContext().getMetaData().getDataSource(),
connection.getShardingContext().getExecuteEngine(), new JDBCTableMetaDataConnectionManager(connection.getDataSourceMap()),
shardingProperties.<Integer>getValue(ShardingPropertiesConstant.MAX_CONNECTIONS_SIZE_PER_QUERY),
shardingProperties.<Boolean>getValue(ShardingPropertiesConstant.CHECK_TABLE_METADATA_ENABLED));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.executor;
import lombok.Getter;
import org.apache.shardingsphere.core.constant.ConnectionMode;
import org.apache.shardingsphere.core.execute.ShardingExecuteGroup;
import org.apache.shardingsphere.core.execute.StatementExecuteUnit;
import org.apache.shardingsphere.core.execute.sql.execute.SQLExecuteCallback;
import org.apache.shardingsphere.core.execute.sql.execute.result.MemoryQueryResult;
import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult;
import org.apache.shardingsphere.core.execute.sql.execute.result.StreamQueryResult;
import org.apache.shardingsphere.core.execute.sql.execute.threadlocal.ExecutorExceptionHandler;
import org.apache.shardingsphere.core.execute.sql.prepare.SQLExecutePrepareCallback;
import org.apache.shardingsphere.core.route.RouteUnit;
import org.apache.shardingsphere.core.route.SQLRouteResult;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.List;
/**
* Prepared statement executor.
*
* @author zhangliang
* @author caohao
* @author maxiaoguang
* @author panjuan
*/
public final class PreparedStatementExecutor extends AbstractStatementExecutor {
@Getter
private final boolean returnGeneratedKeys;
public PreparedStatementExecutor(
final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability, final boolean returnGeneratedKeys, final ShardingConnection shardingConnection) {
super(resultSetType, resultSetConcurrency, resultSetHoldability, shardingConnection);
this.returnGeneratedKeys = returnGeneratedKeys;
}
/**
* Initialize executor.
*
* @param routeResult route result
* @throws SQLException SQL exception
*/
public void init(final SQLRouteResult routeResult) throws SQLException {
setSqlStatement(routeResult.getSqlStatement());
getExecuteGroups().addAll(obtainExecuteGroups(routeResult.getRouteUnits()));
cacheStatements();
}
private Collection<ShardingExecuteGroup<StatementExecuteUnit>> obtainExecuteGroups(final Collection<RouteUnit> routeUnits) throws SQLException {
return getSqlExecutePrepareTemplate().getExecuteUnitGroups(routeUnits, new SQLExecutePrepareCallback() {
@Override
public List<Connection> getConnections(final ConnectionMode connectionMode, final String dataSourceName, final int connectionSize) throws SQLException {
return PreparedStatementExecutor.super.getConnection().getConnections(connectionMode, dataSourceName, connectionSize);
}
@Override
public StatementExecuteUnit createStatementExecuteUnit(final Connection connection, final RouteUnit routeUnit, final ConnectionMode connectionMode) throws SQLException {
return new StatementExecuteUnit(routeUnit, createPreparedStatement(connection, routeUnit.getSqlUnit().getSql()), connectionMode);
}
});
}
@SuppressWarnings("MagicConstant")
private PreparedStatement createPreparedStatement(final Connection connection, final String sql) throws SQLException {
return returnGeneratedKeys ? connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
: connection.prepareStatement(sql, getResultSetType(), getResultSetConcurrency(), getResultSetHoldability());
}
/**
* Execute query.
*
* @return result set list
* @throws SQLException SQL exception
*/
public List<QueryResult> executeQuery() throws SQLException {
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
SQLExecuteCallback<QueryResult> executeCallback = new SQLExecuteCallback<QueryResult>(getDatabaseType(), isExceptionThrown) {
@Override
protected QueryResult executeSQL(final RouteUnit routeUnit, final Statement statement, final ConnectionMode connectionMode) throws SQLException {
return getQueryResult(statement, connectionMode);
}
};
return executeCallback(executeCallback);
}
private QueryResult getQueryResult(final Statement statement, final ConnectionMode connectionMode) throws SQLException {
PreparedStatement preparedStatement = (PreparedStatement) statement;
ResultSet resultSet = preparedStatement.executeQuery();
ShardingRule shardingRule = getConnection().getShardingContext().getShardingRule();
getResultSets().add(resultSet);
return ConnectionMode.MEMORY_STRICTLY == connectionMode ? new StreamQueryResult(resultSet, shardingRule, shardingRule.getShardingEncryptorEngine())
: new MemoryQueryResult(resultSet, shardingRule, shardingRule.getShardingEncryptorEngine());
}
/**
* Execute update.
*
* @return effected records count
* @throws SQLException SQL exception
*/
public int executeUpdate() throws SQLException {
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
SQLExecuteCallback<Integer> executeCallback = SQLExecuteCallbackFactory.getPreparedUpdateSQLExecuteCallback(getDatabaseType(), isExceptionThrown);
List<Integer> results = executeCallback(executeCallback);
if (isAccumulate()) {
return accumulate(results);
} else {
return results.get(0);
}
}
private int accumulate(final List<Integer> results) {
int result = 0;
for (Integer each : results) {
result += null == each ? 0 : each;
}
return result;
}
/**
* Execute SQL.
*
* @return return true if is DQL, false if is DML
* @throws SQLException SQL exception
*/
public boolean execute() throws SQLException {
boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
SQLExecuteCallback<Boolean> executeCallback = SQLExecuteCallbackFactory.getPreparedSQLExecuteCallback(getDatabaseType(), isExceptionThrown);
List<Boolean> result = executeCallback(executeCallback);
if (null == result || result.isEmpty() || null == result.get(0)) {
return false;
}
return result.get(0);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.executor;
import org.apache.shardingsphere.core.constant.ConnectionMode;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.execute.sql.execute.SQLExecuteCallback;
import org.apache.shardingsphere.core.route.RouteUnit;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
* SQL execute callback factory.
*
* @author yangyi
*/
public final class SQLExecuteCallbackFactory {
/**
* Get update callback.
*
* @param databaseType database type
* @param isExceptionThrown is exception thrown
* @return update callback
*/
public static SQLExecuteCallback<Integer> getPreparedUpdateSQLExecuteCallback(final DatabaseType databaseType, final boolean isExceptionThrown) {
return new SQLExecuteCallback<Integer>(databaseType, isExceptionThrown) {
@Override
protected Integer executeSQL(final RouteUnit routeUnit, final Statement statement, final ConnectionMode connectionMode) throws SQLException {
return ((PreparedStatement) statement).executeUpdate();
}
};
}
/**
* Get execute callback.
*
* @param databaseType database type
* @param isExceptionThrown is exception thrown
* @return execute callback
*/
public static SQLExecuteCallback<Boolean> getPreparedSQLExecuteCallback(final DatabaseType databaseType, final boolean isExceptionThrown) {
return new SQLExecuteCallback<Boolean>(databaseType, isExceptionThrown) {
@Override
protected Boolean executeSQL(final RouteUnit routeUnit, final Statement statement, final ConnectionMode connectionMode) throws SQLException {
return ((PreparedStatement) statement).execute();
}
};
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.adapter;
import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;
import java.util.logging.Logger;
/**
* Adapter for {@code Datasource}.
*
* @author zhangliang
* @author panjuan
* @author zhaojun
*/
@Getter
@Setter
public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOperationDataSource implements AutoCloseable {
private final DatabaseType databaseType;
private final Map<String, DataSource> dataSourceMap;
private ShardingTransactionManagerEngine shardingTransactionManagerEngine = new ShardingTransactionManagerEngine();
private PrintWriter logWriter = new PrintWriter(System.out);
public AbstractDataSourceAdapter(final Map<String, DataSource> dataSourceMap) throws SQLException {
databaseType = getDatabaseType(dataSourceMap.values());
shardingTransactionManagerEngine.init(databaseType, dataSourceMap);
this.dataSourceMap = dataSourceMap;
}
protected final DatabaseType getDatabaseType(final Collection<DataSource> dataSources) throws SQLException {
DatabaseType result = null;
for (DataSource each : dataSources) {
DatabaseType databaseType = getDatabaseType(each);
Preconditions.checkState(null == result || result.equals(databaseType), String.format("Database type inconsistent with '%s' and '%s'", result, databaseType));
result = databaseType;
}
return result;
}
private DatabaseType getDatabaseType(final DataSource dataSource) throws SQLException {
if (dataSource instanceof AbstractDataSourceAdapter) {
return ((AbstractDataSourceAdapter) dataSource).databaseType;
}
try (Connection connection = dataSource.getConnection()) {
return DatabaseType.valueFrom(connection.getMetaData().getDatabaseProductName());
}
}
@Override
public final Logger getParentLogger() {
return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
}
@Override
public final Connection getConnection(final String username, final String password) throws SQLException {
return getConnection();
}
@Override
public void close() throws Exception {
for (DataSource each : dataSourceMap.values()) {
try {
Method method = each.getClass().getDeclaredMethod("close");
method.setAccessible(true);
method.invoke(each);
} catch (final ReflectiveOperationException ignored) {
}
}
shardingTransactionManagerEngine.close();
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.adapter;
import com.google.common.base.Preconditions;
import lombok.Getter;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.executor.ForceExecuteCallback;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.executor.ForceExecuteTemplate;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset.ShardingResultSetMetaData;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingPreparedStatement;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingStatement;
import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationResultSet;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.List;
/**
* Adapter for {@code ResultSet}.
*
* @author zhangliang
*/
public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperationResultSet {
@Getter
private final List<ResultSet> resultSets;
@Getter
private final Statement statement;
private boolean closed;
private final ForceExecuteTemplate<ResultSet> forceExecuteTemplate = new ForceExecuteTemplate<>();
public AbstractResultSetAdapter(final List<ResultSet> resultSets, final Statement statement) {
Preconditions.checkArgument(!resultSets.isEmpty());
this.resultSets = resultSets;
this.statement = statement;
}
@Override
public final ResultSetMetaData getMetaData() throws SQLException {
return new ShardingResultSetMetaData(resultSets.get(0).getMetaData(), getShardingRule());
}
private ShardingRule getShardingRule() {
return statement instanceof ShardingPreparedStatement
? ((ShardingPreparedStatement) statement).getConnection().getShardingContext().getShardingRule()
: ((ShardingStatement) statement).getConnection().getShardingContext().getShardingRule();
}
protected final ShardingEncryptorEngine getShardingEncryptorEngine() {
return getShardingRule().getShardingEncryptorEngine();
}
@Override
public final int findColumn(final String columnLabel) throws SQLException {
return resultSets.get(0).findColumn(columnLabel);
}
@Override
public final void close() throws SQLException {
closed = true;
forceExecuteTemplate.execute(resultSets, new ForceExecuteCallback<ResultSet>() {
@Override
public void execute(final ResultSet resultSet) throws SQLException {
resultSet.close();
}
});
}
@Override
public final boolean isClosed() {
return closed;
}
@Override
public final void setFetchDirection(final int direction) throws SQLException {
forceExecuteTemplate.execute(resultSets, new ForceExecuteCallback<ResultSet>() {
@Override
public void execute(final ResultSet resultSet) throws SQLException {
resultSet.setFetchDirection(direction);
}
});
}
@Override
public final int getFetchDirection() throws SQLException {
return resultSets.get(0).getFetchDirection();
}
@Override
public final void setFetchSize(final int rows) throws SQLException {
forceExecuteTemplate.execute(resultSets, new ForceExecuteCallback<ResultSet>() {
@Override
public void execute(final ResultSet resultSet) throws SQLException {
resultSet.setFetchSize(rows);
}
});
}
@Override
public final int getFetchSize() throws SQLException {
return resultSets.get(0).getFetchSize();
}
@Override
public final int getType() throws SQLException {
return resultSets.get(0).getType();
}
@Override
public final int getConcurrency() throws SQLException {
return resultSets.get(0).getConcurrency();
}
@Override
public final SQLWarning getWarnings() throws SQLException {
return resultSets.get(0).getWarnings();
}
@Override
public final void clearWarnings() throws SQLException {
forceExecuteTemplate.execute(resultSets, new ForceExecuteCallback<ResultSet>() {
@Override
public void execute(final ResultSet resultSet) throws SQLException {
resultSet.clearWarnings();
}
});
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.adapter;
import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.invocation.SetParameterMethodInvocation;
import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationPreparedStatement;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
/**
* Sharding adapter for {@code PreparedStatement}.
*
* @author zhangliang
* @author maxiaoguang
*/
public abstract class AbstractShardingPreparedStatementAdapter extends AbstractUnsupportedOperationPreparedStatement {
private final List<SetParameterMethodInvocation> setParameterMethodInvocations = new LinkedList<>();
@Getter
private final List<Object> parameters = new ArrayList<>();
@Override
public final void setNull(final int parameterIndex, final int sqlType) {
setParameter(parameterIndex, null);
}
@Override
public final void setNull(final int parameterIndex, final int sqlType, final String typeName) {
setParameter(parameterIndex, null);
}
@Override
public final void setBoolean(final int parameterIndex, final boolean x) {
setParameter(parameterIndex, x);
}
@Override
public final void setByte(final int parameterIndex, final byte x) {
setParameter(parameterIndex, x);
}
@Override
public final void setShort(final int parameterIndex, final short x) {
setParameter(parameterIndex, x);
}
@Override
public final void setInt(final int parameterIndex, final int x) {
setParameter(parameterIndex, x);
}
@Override
public final void setLong(final int parameterIndex, final long x) {
setParameter(parameterIndex, x);
}
@Override
public final void setFloat(final int parameterIndex, final float x) {
setParameter(parameterIndex, x);
}
@Override
public final void setDouble(final int parameterIndex, final double x) {
setParameter(parameterIndex, x);
}
@Override
public final void setString(final int parameterIndex, final String x) {
setParameter(parameterIndex, x);
}
@Override
public final void setBigDecimal(final int parameterIndex, final BigDecimal x) {
setParameter(parameterIndex, x);
}
@Override
public final void setDate(final int parameterIndex, final Date x) {
setParameter(parameterIndex, x);
}
@Override
public final void setDate(final int parameterIndex, final Date x, final Calendar cal) {
setParameter(parameterIndex, x);
}
@Override
public final void setTime(final int parameterIndex, final Time x) {
setParameter(parameterIndex, x);
}
@Override
public final void setTime(final int parameterIndex, final Time x, final Calendar cal) {
setParameter(parameterIndex, x);
}
@Override
public final void setTimestamp(final int parameterIndex, final Timestamp x) {
setParameter(parameterIndex, x);
}
@Override
public final void setTimestamp(final int parameterIndex, final Timestamp x, final Calendar cal) {
setParameter(parameterIndex, x);
}
@Override
public final void setBytes(final int parameterIndex, final byte[] x) {
setParameter(parameterIndex, x);
}
@Override
public final void setBlob(final int parameterIndex, final Blob x) {
setParameter(parameterIndex, x);
}
@Override
public final void setBlob(final int parameterIndex, final InputStream x) {
setParameter(parameterIndex, x);
}
@Override
public final void setBlob(final int parameterIndex, final InputStream x, final long length) {
setParameter(parameterIndex, x);
}
@Override
public final void setClob(final int parameterIndex, final Clob x) {
setParameter(parameterIndex, x);
}
@Override
public final void setClob(final int parameterIndex, final Reader x) {
setParameter(parameterIndex, x);
}
@Override
public final void setClob(final int parameterIndex, final Reader x, final long length) {
setParameter(parameterIndex, x);
}
@Override
public final void setAsciiStream(final int parameterIndex, final InputStream x) {
setParameter(parameterIndex, x);
}
@Override
public final void setAsciiStream(final int parameterIndex, final InputStream x, final int length) {
setParameter(parameterIndex, x);
}
@Override
public final void setAsciiStream(final int parameterIndex, final InputStream x, final long length) {
setParameter(parameterIndex, x);
}
@SuppressWarnings("deprecation")
@Override
public final void setUnicodeStream(final int parameterIndex, final InputStream x, final int length) {
setParameter(parameterIndex, x);
}
@Override
public final void setBinaryStream(final int parameterIndex, final InputStream x) {
setParameter(parameterIndex, x);
}
@Override
public final void setBinaryStream(final int parameterIndex, final InputStream x, final int length) {
setParameter(parameterIndex, x);
}
@Override
public final void setBinaryStream(final int parameterIndex, final InputStream x, final long length) {
setParameter(parameterIndex, x);
}
@Override
public final void setCharacterStream(final int parameterIndex, final Reader x) {
setParameter(parameterIndex, x);
}
@Override
public final void setCharacterStream(final int parameterIndex, final Reader x, final int length) {
setParameter(parameterIndex, x);
}
@Override
public final void setCharacterStream(final int parameterIndex, final Reader x, final long length) {
setParameter(parameterIndex, x);
}
@Override
public final void setURL(final int parameterIndex, final URL x) {
setParameter(parameterIndex, x);
}
@Override
public final void setSQLXML(final int parameterIndex, final SQLXML x) {
setParameter(parameterIndex, x);
}
@Override
public final void setObject(final int parameterIndex, final Object x) {
setParameter(parameterIndex, x);
}
@Override
public final void setObject(final int parameterIndex, final Object x, final int targetSqlType) {
setParameter(parameterIndex, x);
}
@Override
public final void setObject(final int parameterIndex, final Object x, final int targetSqlType, final int scaleOrLength) {
setParameter(parameterIndex, x);
}
private void setParameter(final int parameterIndex, final Object value) {
if (parameters.size() == parameterIndex - 1) {
parameters.add(value);
return;
}
for (int i = parameters.size(); i <= parameterIndex - 1; i++) {
parameters.add(null);
}
parameters.set(parameterIndex - 1, value);
}
protected final void replaySetParameter(final PreparedStatement preparedStatement, final List<Object> parameters) {
setParameterMethodInvocations.clear();
addParameters(parameters);
for (SetParameterMethodInvocation each : setParameterMethodInvocations) {
each.invoke(preparedStatement);
}
}
private void addParameters(final List<Object> parameters) {
int i = 0;
for (Object each : parameters) {
setParameter(new Class[]{int.class, Object.class}, i++ + 1, each);
}
}
@SneakyThrows
private void setParameter(final Class[] argumentTypes, final Object... arguments) {
setParameterMethodInvocations.add(new SetParameterMethodInvocation(PreparedStatement.class.getMethod("setObject", argumentTypes), arguments, arguments[1]));
}
@Override
public final void clearParameters() {
parameters.clear();
setParameterMethodInvocations.clear();
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.adapter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.executor.ForceExecuteCallback;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.executor.ForceExecuteTemplate;
import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.Collection;
/**
* Adapter for {@code Statement}.
*
* @author zhangliang
* @author gaohongtao
* @author yangyi
*/
@RequiredArgsConstructor
public abstract class AbstractStatementAdapter extends AbstractUnsupportedOperationStatement {
private final Class<? extends Statement> targetClass;
private boolean closed;
private boolean poolable;
private int fetchSize;
private final ForceExecuteTemplate<Statement> forceExecuteTemplate = new ForceExecuteTemplate<>();
@SuppressWarnings("unchecked")
@Override
public final void close() throws SQLException {
closed = true;
try {
forceExecuteTemplate.execute((Collection) getRoutedStatements(), new ForceExecuteCallback<Statement>() {
@Override
public void execute(final Statement statement) throws SQLException {
statement.close();
}
});
} finally {
getRoutedStatements().clear();
}
}
@Override
public final boolean isClosed() {
return closed;
}
@Override
public final boolean isPoolable() {
return poolable;
}
@SuppressWarnings("unchecked")
@Override
public final void setPoolable(final boolean poolable) throws SQLException {
this.poolable = poolable;
recordMethodInvocation(targetClass, "setPoolable", new Class[] {boolean.class}, new Object[] {poolable});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), new ForceExecuteCallback<Statement>() {
@Override
public void execute(final Statement statement) throws SQLException {
statement.setPoolable(poolable);
}
});
}
@Override
public final int getFetchSize() {
return fetchSize;
}
@SuppressWarnings("unchecked")
@Override
public final void setFetchSize(final int rows) throws SQLException {
this.fetchSize = rows;
recordMethodInvocation(targetClass, "setFetchSize", new Class[] {int.class}, new Object[] {rows});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), new ForceExecuteCallback<Statement>() {
@Override
public void execute(final Statement statement) throws SQLException {
statement.setFetchSize(rows);
}
});
}
@SuppressWarnings("unchecked")
@Override
public final void setEscapeProcessing(final boolean enable) throws SQLException {
recordMethodInvocation(targetClass, "setEscapeProcessing", new Class[] {boolean.class}, new Object[] {enable});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), new ForceExecuteCallback<Statement>() {
@Override
public void execute(final Statement statement) throws SQLException {
statement.setEscapeProcessing(enable);
}
});
}
@SuppressWarnings("unchecked")
@Override
public final void cancel() throws SQLException {
forceExecuteTemplate.execute((Collection) getRoutedStatements(), new ForceExecuteCallback<Statement>() {
@Override
public void execute(final Statement statement) throws SQLException {
statement.cancel();
}
});
}
@Override
public final int getUpdateCount() throws SQLException {
if (isAccumulate()) {
return accumulate();
} else {
Collection<? extends Statement> statements = getRoutedStatements();
if (0 == statements.size()) {
return -1;
}
return getRoutedStatements().iterator().next().getUpdateCount();
}
}
private int accumulate() throws SQLException {
long result = 0;
boolean hasResult = false;
for (Statement each : getRoutedStatements()) {
int updateCount = each.getUpdateCount();
if (updateCount > -1) {
hasResult = true;
}
result += updateCount;
}
if (result > Integer.MAX_VALUE) {
result = Integer.MAX_VALUE;
}
return hasResult ? Long.valueOf(result).intValue() : -1;
}
@Override
public final SQLWarning getWarnings() {
return null;
}
@Override
public final void clearWarnings() {
}
@Override
public final boolean getMoreResults() throws SQLException {
boolean result = false;
for (Statement each : getRoutedStatements()) {
result = each.getMoreResults();
}
return result;
}
@Override
public final boolean getMoreResults(final int current) {
return false;
}
@Override
public final int getMaxFieldSize() throws SQLException {
return getRoutedStatements().isEmpty() ? 0 : getRoutedStatements().iterator().next().getMaxFieldSize();
}
@SuppressWarnings("unchecked")
@Override
public final void setMaxFieldSize(final int max) throws SQLException {
recordMethodInvocation(targetClass, "setMaxFieldSize", new Class[] {int.class}, new Object[] {max});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), new ForceExecuteCallback<Statement>() {
@Override
public void execute(final Statement statement) throws SQLException {
statement.setMaxFieldSize(max);
}
});
}
// TODO Confirm MaxRows for multiple databases is need special handle. eg: 10 statements maybe MaxRows / 10
@Override
public final int getMaxRows() throws SQLException {
return getRoutedStatements().isEmpty() ? -1 : getRoutedStatements().iterator().next().getMaxRows();
}
@SuppressWarnings("unchecked")
@Override
public final void setMaxRows(final int max) throws SQLException {
recordMethodInvocation(targetClass, "setMaxRows", new Class[] {int.class}, new Object[] {max});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), new ForceExecuteCallback<Statement>() {
@Override
public void execute(final Statement statement) throws SQLException {
statement.setMaxRows(max);
}
});
}
@Override
public final int getQueryTimeout() throws SQLException {
return getRoutedStatements().isEmpty() ? 0 : getRoutedStatements().iterator().next().getQueryTimeout();
}
@SuppressWarnings("unchecked")
@Override
public final void setQueryTimeout(final int seconds) throws SQLException {
recordMethodInvocation(targetClass, "setQueryTimeout", new Class[] {int.class}, new Object[] {seconds});
forceExecuteTemplate.execute((Collection) getRoutedStatements(), new ForceExecuteCallback<Statement>() {
@Override
public void execute(final Statement statement) throws SQLException {
statement.setQueryTimeout(seconds);
}
});
}
protected abstract boolean isAccumulate();
protected abstract Collection<? extends Statement> getRoutedStatements();
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.adapter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.invocation.JdbcMethodInvocation;
import java.sql.SQLException;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Collection;
/**
* Adapter for {@code java.sql.Wrapper}.
*
* @author zhangliang
*/
public abstract class WrapperAdapter implements Wrapper {
private final Collection<JdbcMethodInvocation> jdbcMethodInvocations = new ArrayList<>();
@SuppressWarnings("unchecked")
@Override
public final <T> T unwrap(final Class<T> iface) throws SQLException {
if (isWrapperFor(iface)) {
return (T) this;
}
throw new SQLException(String.format("[%s] cannot be unwrapped as [%s]", getClass().getName(), iface.getName()));
}
@Override
public final boolean isWrapperFor(final Class<?> iface) {
return iface.isInstance(this);
}
/**
* record method invocation.
*
* @param targetClass target class
* @param methodName method name
* @param argumentTypes argument types
* @param arguments arguments
*/
@SneakyThrows
public final void recordMethodInvocation(final Class<?> targetClass, final String methodName, final Class<?>[] argumentTypes, final Object[] arguments) {
jdbcMethodInvocations.add(new JdbcMethodInvocation(targetClass.getMethod(methodName, argumentTypes), arguments));
}
/**
* Replay methods invocation.
*
* @param target target object
*/
public final void replayMethodsInvocation(final Object target) {
for (JdbcMethodInvocation each : jdbcMethodInvocations) {
each.invoke(target);
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.adapter.executor;
import java.sql.SQLException;
/**
* Force execute callback.
*
* @author zhangliang
*
* @param <T> type of target to be executed
*/
public interface ForceExecuteCallback<T> {
/**
* Execute.
*
* @param target target to be executed
* @throws SQLException SQL exception
*/
void execute(T target) throws SQLException;
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.adapter.executor;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
/**
* Force execute template.
*
* @author zhangliang
*
* @param <T> type of targets to be executed
*/
public final class ForceExecuteTemplate<T> {
/**
* Force execute.
*
* @param targets targets to be executed
* @param callback force execute callback
* @throws SQLException throw SQL exception after all targets are executed
*/
public void execute(final Collection<T> targets, final ForceExecuteCallback<T> callback) throws SQLException {
Collection<SQLException> exceptions = new LinkedList<>();
for (T each : targets) {
try {
callback.execute(each);
} catch (final SQLException ex) {
exceptions.add(ex);
}
}
throwSQLExceptionIfNecessary(exceptions);
}
private void throwSQLExceptionIfNecessary(final Collection<SQLException> exceptions) throws SQLException {
if (exceptions.isEmpty()) {
return;
}
SQLException ex = new SQLException();
for (SQLException each : exceptions) {
ex.setNextException(each);
}
throw ex;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.adapter.invocation;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import java.lang.reflect.Method;
/**
* Invocation that reflected call for JDBC method.
*
* @author gaohongtao
*/
@RequiredArgsConstructor
public class JdbcMethodInvocation {
@Getter
private final Method method;
@Getter
private final Object[] arguments;
/**
* Invoke JDBC method.
*
* @param target target object
*/
@SneakyThrows
public void invoke(final Object target) {
method.invoke(target, arguments);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.adapter.invocation;
import lombok.Getter;
import java.lang.reflect.Method;
/**
* Invocation that reflected call for {@code PreparedStatement.setParameter} method.
*
* @author zhangliang
*/
public final class SetParameterMethodInvocation extends JdbcMethodInvocation {
@Getter
private final int index;
@Getter
private final Object value;
public SetParameterMethodInvocation(final Method method, final Object[] arguments, final Object value) {
super(method, arguments);
this.index = (int) arguments[0];
this.value = value;
}
/**
* Set argument.
*
* @param value argument value
*/
public void changeValueArgument(final Object value) {
getArguments()[1] = value;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core;
import lombok.Getter;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.constant.properties.ShardingProperties;
import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.apache.shardingsphere.core.execute.ShardingExecuteEngine;
import org.apache.shardingsphere.core.execute.metadata.TableMetaDataInitializer;
import org.apache.shardingsphere.core.metadata.ShardingMetaData;
import org.apache.shardingsphere.core.metadata.datasource.ShardingDataSourceMetaData;
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.parse.cache.ParsingResultCache;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.metadata.CachedDatabaseMetaData;
import org.apache.shardingsphere.shardingjdbc.jdbc.metadata.JDBCTableMetaDataConnectionManager;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
/**
* Sharding runtime context.
*
* @author gaohongtao
* @author panjuan
*/
@Getter
public final class ShardingContext implements AutoCloseable {
private final DatabaseMetaData cachedDatabaseMetaData;
private final ShardingRule shardingRule;
private final DatabaseType databaseType;
private final ShardingExecuteEngine executeEngine;
private final ShardingProperties shardingProperties;
private final ShardingMetaData metaData;
private final ParsingResultCache parsingResultCache;
public ShardingContext(final Map<String, DataSource> dataSourceMap, final ShardingRule shardingRule, final DatabaseType databaseType, final Properties props) throws SQLException {
this.shardingRule = shardingRule;
this.cachedDatabaseMetaData = createCachedDatabaseMetaData(dataSourceMap);
this.databaseType = databaseType;
shardingProperties = new ShardingProperties(null == props ? new Properties() : props);
int executorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
executeEngine = new ShardingExecuteEngine(executorSize);
ShardingDataSourceMetaData shardingDataSourceMetaData = new ShardingDataSourceMetaData(getDataSourceURLs(dataSourceMap), shardingRule, databaseType);
ShardingTableMetaData shardingTableMetaData = new ShardingTableMetaData(getTableMetaDataInitializer(dataSourceMap, shardingDataSourceMetaData).load(shardingRule));
metaData = new ShardingMetaData(shardingDataSourceMetaData, shardingTableMetaData);
parsingResultCache = new ParsingResultCache();
}
private DatabaseMetaData createCachedDatabaseMetaData(final Map<String, DataSource> dataSourceMap) throws SQLException {
try (Connection connection = dataSourceMap.values().iterator().next().getConnection()) {
return new CachedDatabaseMetaData(connection.getMetaData(), dataSourceMap, shardingRule);
}
}
private Map<String, String> getDataSourceURLs(final Map<String, DataSource> dataSourceMap) throws SQLException {
Map<String, String> result = new LinkedHashMap<>(dataSourceMap.size(), 1);
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
result.put(entry.getKey(), getDataSourceURL(entry.getValue()));
}
return result;
}
private String getDataSourceURL(final DataSource dataSource) throws SQLException {
try (Connection connection = dataSource.getConnection()) {
return connection.getMetaData().getURL();
}
}
private TableMetaDataInitializer getTableMetaDataInitializer(final Map<String, DataSource> dataSourceMap, final ShardingDataSourceMetaData shardingDataSourceMetaData) {
return new TableMetaDataInitializer(shardingDataSourceMetaData, executeEngine, new JDBCTableMetaDataConnectionManager(dataSourceMap),
shardingProperties.<Integer>getValue(ShardingPropertiesConstant.MAX_CONNECTIONS_SIZE_PER_QUERY),
shardingProperties.<Boolean>getValue(ShardingPropertiesConstant.CHECK_TABLE_METADATA_ENABLED));
}
@Override
public void close() {
executeEngine.close();
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.connection;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parse.EncryptSQLParsingEngine;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.EncryptPreparedStatement;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.EncryptStatement;
import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationConnection;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
/**
* Encrypt connection.
*
* @author panjuan
*/
@Getter
@RequiredArgsConstructor
public final class EncryptConnection extends AbstractUnsupportedOperationConnection {
private final Connection connection;
private final EncryptRule encryptRule;
private final DatabaseType databaseType;
private final EncryptSQLParsingEngine encryptSQLParsingEngine;
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return connection.getMetaData();
}
@Override
public Statement createStatement() {
return new EncryptStatement(this);
}
@Override
public Statement createStatement(final int resultSetType, final int resultSetConcurrency) {
return new EncryptStatement(this, resultSetType, resultSetConcurrency);
}
@Override
public Statement createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
return new EncryptStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(final String sql) {
return new EncryptPreparedStatement(this, sql);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) {
return new EncryptPreparedStatement(this, sql, resultSetType, resultSetConcurrency);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
return new EncryptPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) {
return new EncryptPreparedStatement(this, sql, autoGeneratedKeys);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) {
return new EncryptPreparedStatement(this, sql, columnIndexes);
}
@Override
public PreparedStatement prepareStatement(final String sql, final String[] columnNames) {
return new EncryptPreparedStatement(this, sql, columnNames);
}
@Override
public boolean getAutoCommit() throws SQLException {
return connection.getAutoCommit();
}
@Override
public void setAutoCommit(final boolean autoCommit) throws SQLException {
connection.setAutoCommit(autoCommit);
}
@Override
public void commit() throws SQLException {
connection.commit();
}
@Override
public void rollback() throws SQLException {
connection.rollback();
}
@Override
public void close() throws SQLException {
connection.close();
}
@Override
public boolean isClosed() throws SQLException {
return connection.isClosed();
}
@Override
public boolean isReadOnly() throws SQLException {
return connection.isReadOnly();
}
@Override
public void setReadOnly(final boolean readOnly) throws SQLException {
connection.setReadOnly(readOnly);
}
@Override
public int getTransactionIsolation() throws SQLException {
return connection.getTransactionIsolation();
}
@Override
public void setTransactionIsolation(final int level) throws SQLException {
connection.setTransactionIsolation(level);
}
@Override
public SQLWarning getWarnings() {
return null;
}
@Override
public void clearWarnings() {
}
@Override
public int getHoldability() {
return ResultSet.CLOSE_CURSORS_AT_COMMIT;
}
@Override
public void setHoldability(final int holdability) {
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.connection;
import lombok.Getter;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractConnectionAdapter;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.MasterSlavePreparedStatement;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.MasterSlaveStatement;
import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine;
import org.apache.shardingsphere.transaction.core.TransactionType;
import javax.sql.DataSource;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
/**
* Connection that support master-slave.
*
* @author zhangliang
* @author zhaojun
*/
@Getter
public final class MasterSlaveConnection extends AbstractConnectionAdapter {
private final MasterSlaveDataSource masterSlaveDataSource;
private final Map<String, DataSource> dataSourceMap;
public MasterSlaveConnection(final MasterSlaveDataSource masterSlaveDataSource, final Map<String, DataSource> dataSourceMap,
final ShardingTransactionManagerEngine shardingTransactionManagerEngine, final TransactionType transactionType) {
super(shardingTransactionManagerEngine, transactionType);
this.masterSlaveDataSource = masterSlaveDataSource;
this.dataSourceMap = dataSourceMap;
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return getCachedConnections().isEmpty() ? masterSlaveDataSource.getCachedDatabaseMetaData() : getCachedConnections().values().iterator().next().getMetaData();
}
@Override
public Statement createStatement() {
return new MasterSlaveStatement(this);
}
@Override
public Statement createStatement(final int resultSetType, final int resultSetConcurrency) {
return new MasterSlaveStatement(this, resultSetType, resultSetConcurrency);
}
@Override
public Statement createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
return new MasterSlaveStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(final String sql) throws SQLException {
return new MasterSlavePreparedStatement(this, sql);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
return new MasterSlavePreparedStatement(this, sql, resultSetType, resultSetConcurrency);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
return new MasterSlavePreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException {
return new MasterSlavePreparedStatement(this, sql, autoGeneratedKeys);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws SQLException {
return new MasterSlavePreparedStatement(this, sql, columnIndexes);
}
@Override
public PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws SQLException {
return new MasterSlavePreparedStatement(this, sql, columnNames);
}
@Override
protected boolean isOnlyLocalTransactionValid() {
return true;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.connection;
import lombok.Getter;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractConnectionAdapter;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.ShardingContext;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingPreparedStatement;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingStatement;
import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine;
import org.apache.shardingsphere.transaction.core.TransactionType;
import javax.sql.DataSource;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
/**
* Connection that support sharding.
*
* @author zhangliang
* @author caohao
* @author gaohongtao
* @author zhaojun
*/
@Getter
public final class ShardingConnection extends AbstractConnectionAdapter {
private final Map<String, DataSource> dataSourceMap;
private final ShardingContext shardingContext;
public ShardingConnection(final Map<String, DataSource> dataSourceMap, final ShardingContext shardingContext,
final ShardingTransactionManagerEngine shardingTransactionManagerEngine, final TransactionType transactionType) {
super(shardingTransactionManagerEngine, transactionType);
this.dataSourceMap = dataSourceMap;
this.shardingContext = shardingContext;
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return getCachedConnections().isEmpty() ? shardingContext.getCachedDatabaseMetaData() : getCachedConnections().values().iterator().next().getMetaData();
}
@Override
public PreparedStatement prepareStatement(final String sql) {
return new ShardingPreparedStatement(this, sql);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) {
return new ShardingPreparedStatement(this, sql, resultSetType, resultSetConcurrency);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
return new ShardingPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) {
return new ShardingPreparedStatement(this, sql, autoGeneratedKeys);
}
@Override
public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) {
return new ShardingPreparedStatement(this, sql, Statement.RETURN_GENERATED_KEYS);
}
@Override
public PreparedStatement prepareStatement(final String sql, final String[] columnNames) {
return new ShardingPreparedStatement(this, sql, Statement.RETURN_GENERATED_KEYS);
}
@Override
public Statement createStatement() {
return new ShardingStatement(this);
}
@Override
public Statement createStatement(final int resultSetType, final int resultSetConcurrency) {
return new ShardingStatement(this, resultSetType, resultSetConcurrency);
}
@Override
public Statement createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
return new ShardingStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
protected boolean isOnlyLocalTransactionValid() {
return false;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.metadata.table.ColumnMetaData;
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.metadata.table.TableMetaData;
import org.apache.shardingsphere.core.parse.EncryptSQLParsingEngine;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.EncryptConnection;
import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
/**
* Encrypt data source.
*
* @author panjuan
*/
@Getter
public class EncryptDataSource extends AbstractUnsupportedOperationDataSource implements AutoCloseable {
private final DataSource dataSource;
private final EncryptRule encryptRule;
private final DatabaseType databaseType;
private final EncryptSQLParsingEngine encryptSQLParsingEngine;
@Setter
private PrintWriter logWriter = new PrintWriter(System.out);
@SneakyThrows
public EncryptDataSource(final DataSource dataSource, final EncryptRuleConfiguration encryptRuleConfiguration) {
this.dataSource = dataSource;
encryptRule = new EncryptRule(encryptRuleConfiguration);
databaseType = getDatabaseType();
encryptSQLParsingEngine = new EncryptSQLParsingEngine(databaseType, encryptRule, createEncryptTableMetaData());
}
@SneakyThrows
private ShardingTableMetaData createEncryptTableMetaData() {
Map<String, TableMetaData> tables = new LinkedHashMap<>();
try (Connection connection = dataSource.getConnection()) {
for (String each : encryptRule.getEncryptTableNames()) {
if (isTableExist(connection, each)) {
tables.put(each, new TableMetaData(getColumnMetaDataList(connection, each)));
}
}
}
return new ShardingTableMetaData(tables);
}
private boolean isTableExist(final Connection connection, final String tableName) throws SQLException {
try (ResultSet resultSet = connection.getMetaData().getTables(connection.getCatalog(), null, tableName, null)) {
return resultSet.next();
}
}
private List<ColumnMetaData> getColumnMetaDataList(final Connection connection, final String tableName) throws SQLException {
List<ColumnMetaData> result = new LinkedList<>();
Collection<String> primaryKeys = getPrimaryKeys(connection, tableName);
try (ResultSet resultSet = connection.getMetaData().getColumns(connection.getCatalog(), null, tableName, "%")) {
while (resultSet.next()) {
String columnName = resultSet.getString("COLUMN_NAME");
String columnType = resultSet.getString("TYPE_NAME");
result.add(new ColumnMetaData(columnName, columnType, primaryKeys.contains(columnName)));
}
}
return result;
}
private Collection<String> getPrimaryKeys(final Connection connection, final String tableName) throws SQLException {
Collection<String> result = new HashSet<>();
try (ResultSet resultSet = connection.getMetaData().getPrimaryKeys(connection.getCatalog(), null, tableName)) {
while (resultSet.next()) {
result.add(resultSet.getString("COLUMN_NAME"));
}
}
return result;
}
private DatabaseType getDatabaseType() throws SQLException {
try (Connection connection = dataSource.getConnection()) {
return DatabaseType.valueFrom(connection.getMetaData().getDatabaseProductName());
}
}
@Override
@SneakyThrows
public EncryptConnection getConnection() {
return new EncryptConnection(dataSource.getConnection(), encryptRule, databaseType, encryptSQLParsingEngine);
}
@Override
@SneakyThrows
public Connection getConnection(final String username, final String password) {
return getConnection();
}
@Override
public Logger getParentLogger() {
return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
}
@Override
public void close() {
try {
Method method = dataSource.getClass().getDeclaredMethod("close");
method.setAccessible(true);
method.invoke(dataSource);
} catch (final ReflectiveOperationException ignored) {
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource;
import lombok.Getter;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.core.constant.properties.ShardingProperties;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractDataSourceAdapter;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.MasterSlaveConnection;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.metadata.CachedDatabaseMetaData;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
/**
* Master-slave data source.
*
* @author zhangliang
* @author panjuan
* @author zhaojun
*/
@Getter
public class MasterSlaveDataSource extends AbstractDataSourceAdapter {
private final DatabaseMetaData cachedDatabaseMetaData;
private final MasterSlaveRule masterSlaveRule;
private final ShardingProperties shardingProperties;
public MasterSlaveDataSource(final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Properties props) throws SQLException {
super(dataSourceMap);
cachedDatabaseMetaData = createCachedDatabaseMetaData(dataSourceMap);
this.masterSlaveRule = new MasterSlaveRule(masterSlaveRuleConfig);
shardingProperties = new ShardingProperties(null == props ? new Properties() : props);
}
public MasterSlaveDataSource(final Map<String, DataSource> dataSourceMap, final MasterSlaveRule masterSlaveRule, final Properties props) throws SQLException {
super(dataSourceMap);
cachedDatabaseMetaData = createCachedDatabaseMetaData(dataSourceMap);
this.masterSlaveRule = masterSlaveRule;
shardingProperties = new ShardingProperties(null == props ? new Properties() : props);
}
private DatabaseMetaData createCachedDatabaseMetaData(final Map<String, DataSource> dataSourceMap) throws SQLException {
try (Connection connection = dataSourceMap.values().iterator().next().getConnection()) {
return new CachedDatabaseMetaData(connection.getMetaData(), dataSourceMap, null);
}
}
@Override
public final MasterSlaveConnection getConnection() {
return new MasterSlaveConnection(this, getDataSourceMap(), getShardingTransactionManagerEngine(), TransactionTypeHolder.get());
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource;
import com.google.common.base.Preconditions;
import lombok.Getter;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractDataSourceAdapter;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.ShardingContext;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingConnection;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
/**
* Sharding data source.
*
* @author zhangliang
* @author zhaojun
* @author panjuan
*/
@Getter
public class ShardingDataSource extends AbstractDataSourceAdapter {
private final ShardingContext shardingContext;
public ShardingDataSource(final Map<String, DataSource> dataSourceMap, final ShardingRule shardingRule) throws SQLException {
this(dataSourceMap, shardingRule, new Properties());
}
public ShardingDataSource(final Map<String, DataSource> dataSourceMap, final ShardingRule shardingRule, final Properties props) throws SQLException {
super(dataSourceMap);
checkDataSourceType(dataSourceMap);
shardingContext = new ShardingContext(getDataSourceMap(), shardingRule, getDatabaseType(), props);
}
private void checkDataSourceType(final Map<String, DataSource> dataSourceMap) {
for (DataSource each : dataSourceMap.values()) {
Preconditions.checkArgument(!(each instanceof MasterSlaveDataSource), "Initialized data sources can not be master-slave data sources.");
}
}
@Override
public final ShardingConnection getConnection() {
return new ShardingConnection(getDataSourceMap(), shardingContext, getShardingTransactionManagerEngine(), TransactionTypeHolder.get());
}
@Override
public final void close() throws Exception {
super.close();
shardingContext.close();
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.metadata;
import org.apache.shardingsphere.core.rule.ShardingRule;
import javax.sql.DataSource;
import java.util.Map;
/**
* Adapted database meta data.
*
* @author zhangliang
*/
public abstract class AdaptedDatabaseMetaData extends ResultSetReturnedDatabaseMetaData {
public AdaptedDatabaseMetaData(final Map<String, DataSource> dataSourceMap, final ShardingRule shardingRule) {
super(dataSourceMap, shardingRule);
}
@Override
public final boolean ownInsertsAreVisible(final int type) {
return true;
}
@Override
public final boolean ownUpdatesAreVisible(final int type) {
return true;
}
@Override
public final boolean ownDeletesAreVisible(final int type) {
return true;
}
@Override
public final boolean othersInsertsAreVisible(final int type) {
return true;
}
@Override
public final boolean othersUpdatesAreVisible(final int type) {
return true;
}
@Override
public final boolean othersDeletesAreVisible(final int type) {
return true;
}
@Override
public final boolean insertsAreDetected(final int type) {
return true;
}
@Override
public final boolean updatesAreDetected(final int type) {
return true;
}
@Override
public final boolean deletesAreDetected(final int type) {
return true;
}
@Override
public final boolean supportsResultSetType(final int type) {
return true;
}
@Override
public final boolean supportsResultSetConcurrency(final int type, final int concurrency) {
return true;
}
@Override
public final boolean supportsResultSetHoldability(final int holdability) {
return true;
}
@Override
public final boolean supportsTransactionIsolationLevel(final int level) {
return true;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedGeneratedKeysResultSet;
import java.math.BigDecimal;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Collections;
import java.util.Iterator;
/**
* ResultSet for generated keys.
*
* @author gaohongtao
*/
@RequiredArgsConstructor
public final class GeneratedKeysResultSet extends AbstractUnsupportedGeneratedKeysResultSet {
private final Iterator<Comparable<?>> generatedKeys;
private final String generatedKeyColumn;
private final Statement statement;
private boolean closed;
private Comparable<?> currentGeneratedKey;
public GeneratedKeysResultSet() {
generatedKeys = Collections.<Comparable<?>>emptyList().iterator();
generatedKeyColumn = null;
statement = null;
}
@Override
public boolean isClosed() {
return closed;
}
@Override
public boolean next() {
if (closed || !generatedKeys.hasNext()) {
currentGeneratedKey = null;
return false;
}
currentGeneratedKey = generatedKeys.next();
return true;
}
@Override
public void close() {
closed = true;
}
@Override
public ResultSetMetaData getMetaData() {
checkState();
return new GeneratedKeysResultSetMetaData(generatedKeyColumn);
}
@Override
public boolean wasNull() {
checkState();
return false;
}
@Override
public String getString(final int columnIndex) {
checkStateForGetData();
return currentGeneratedKey.toString();
}
@Override
public String getString(final String columnLabel) {
return getString(1);
}
@Override
public byte getByte(final int columnIndex) {
checkStateForGetData();
return Byte.valueOf(getString(columnIndex));
}
@Override
public byte getByte(final String columnLabel) {
return getByte(1);
}
@Override
public short getShort(final int columnIndex) {
checkStateForGetData();
return Short.valueOf(getString(columnIndex));
}
@Override
public short getShort(final String columnLabel) {
return getShort(1);
}
@Override
public int getInt(final int columnIndex) {
checkStateForGetData();
return Integer.valueOf(getString(columnIndex));
}
@Override
public int getInt(final String columnLabel) {
return getInt(1);
}
@Override
public long getLong(final int columnIndex) {
checkStateForGetData();
return Long.valueOf(getString(columnIndex));
}
@Override
public long getLong(final String columnLabel) {
return getLong(1);
}
@Override
public float getFloat(final int columnIndex) {
checkStateForGetData();
return Float.valueOf(getString(columnIndex));
}
@Override
public float getFloat(final String columnLabel) {
return getFloat(1);
}
@Override
public double getDouble(final int columnIndex) {
checkStateForGetData();
return Double.valueOf(getString(columnIndex));
}
@Override
public double getDouble(final String columnLabel) {
return getDouble(1);
}
@SuppressWarnings("deprecation")
@Override
public BigDecimal getBigDecimal(final int columnIndex, final int scale) {
checkStateForGetData();
return new BigDecimal(getString(columnIndex)).setScale(scale, BigDecimal.ROUND_HALF_UP);
}
@SuppressWarnings("deprecation")
@Override
public BigDecimal getBigDecimal(final String columnLabel, final int scale) {
return getBigDecimal(1, scale);
}
@Override
public BigDecimal getBigDecimal(final int columnIndex) {
checkStateForGetData();
return new BigDecimal(getString(columnIndex));
}
@Override
public BigDecimal getBigDecimal(final String columnLabel) {
return getBigDecimal(1);
}
@Override
public byte[] getBytes(final int columnIndex) {
checkStateForGetData();
return getString(columnIndex).getBytes();
}
@Override
public byte[] getBytes(final String columnLabel) {
return getBytes(1);
}
@Override
public Object getObject(final int columnIndex) {
checkStateForGetData();
return currentGeneratedKey;
}
@Override
public Object getObject(final String columnLabel) {
return getObject(1);
}
@Override
public int findColumn(final String columnLabel) {
checkState();
return 1;
}
@Override
public int getType() {
checkState();
return TYPE_FORWARD_ONLY;
}
@Override
public int getConcurrency() {
checkState();
return CONCUR_READ_ONLY;
}
@Override
public Statement getStatement() {
checkState();
return statement;
}
private void checkState() {
Preconditions.checkState(!closed, "ResultSet has closed.");
}
private void checkStateForGetData() {
checkState();
Preconditions.checkNotNull(currentGeneratedKey, "ResultSet should call next or has no more data.");
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.WrapperAdapter;
import java.sql.ResultSetMetaData;
import java.sql.Types;
/**
* Result set meta data for generated keys.
*
* @author gaohongtao
*/
@RequiredArgsConstructor
public final class GeneratedKeysResultSetMetaData extends WrapperAdapter implements ResultSetMetaData {
private final String generatedKeyColumn;
@Override
public int getColumnCount() {
return 1;
}
@Override
public boolean isAutoIncrement(final int column) {
Preconditions.checkArgument(column == 1);
return true;
}
@Override
public boolean isCaseSensitive(final int column) {
Preconditions.checkArgument(column == 1);
return true;
}
@Override
public boolean isSearchable(final int column) {
Preconditions.checkArgument(column == 1);
return false;
}
@Override
public boolean isCurrency(final int column) {
Preconditions.checkArgument(column == 1);
return false;
}
@Override
public int isNullable(final int column) {
Preconditions.checkArgument(column == 1);
return columnNoNulls;
}
@Override
public boolean isSigned(final int column) {
Preconditions.checkArgument(column == 1);
return true;
}
@Override
public int getColumnDisplaySize(final int column) {
Preconditions.checkArgument(column == 1);
return 0;
}
@Override
public String getColumnLabel(final int column) {
Preconditions.checkArgument(column == 1);
return generatedKeyColumn;
}
@Override
public String getColumnName(final int column) {
Preconditions.checkArgument(column == 1);
return generatedKeyColumn;
}
@Override
public String getSchemaName(final int column) {
Preconditions.checkArgument(column == 1);
return "";
}
@Override
public int getPrecision(final int column) {
Preconditions.checkArgument(column == 1);
return 0;
}
@Override
public int getScale(final int column) {
Preconditions.checkArgument(column == 1);
return 0;
}
@Override
public String getTableName(final int column) {
Preconditions.checkArgument(column == 1);
return "";
}
@Override
public String getCatalogName(final int column) {
Preconditions.checkArgument(column == 1);
return "";
}
@Override
public int getColumnType(final int column) {
Preconditions.checkArgument(column == 1);
return Types.BIGINT;
}
@Override
public String getColumnTypeName(final int column) {
Preconditions.checkArgument(column == 1);
return "";
}
@Override
public boolean isReadOnly(final int column) {
Preconditions.checkArgument(column == 1);
return true;
}
@Override
public boolean isWritable(final int column) {
Preconditions.checkArgument(column == 1);
return false;
}
@Override
public boolean isDefinitelyWritable(final int column) {
Preconditions.checkArgument(column == 1);
return false;
}
@Override
public String getColumnClassName(final int column) {
Preconditions.checkArgument(column == 1);
return Number.class.getName();
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.exception.ShardingException;
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
/**
* ResultSet utility class.
*
* @author gaohongtao
*/
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class ResultSetUtil {
/**
* Convert value via expected class type.
*
* @param value original value
* @param convertType expected class type
* @return converted value
*/
public static Object convertValue(final Object value, final Class<?> convertType) {
if (null == value) {
return convertNullValue(convertType);
}
if (value.getClass() == convertType) {
return value;
}
if (value instanceof Number) {
return convertNumberValue(value, convertType);
}
if (value instanceof Date) {
return convertDateValue(value, convertType);
}
if (String.class.equals(convertType)) {
return value.toString();
} else {
return value;
}
}
private static Object convertNullValue(final Class<?> convertType) {
switch (convertType.getName()) {
case "boolean":
return false;
case "byte":
return (byte) 0;
case "short":
return (short) 0;
case "int":
return 0;
case "long":
return 0L;
case "float":
return 0F;
case "double":
return 0D;
default:
return null;
}
}
private static Object convertNumberValue(final Object value, final Class<?> convertType) {
Number number = (Number) value;
switch (convertType.getName()) {
case "byte":
return number.byteValue();
case "short":
return number.shortValue();
case "int":
return number.intValue();
case "long":
return number.longValue();
case "double":
return number.doubleValue();
case "float":
return number.floatValue();
case "java.math.BigDecimal":
return new BigDecimal(number.toString());
case "java.lang.Object":
return value;
case "java.lang.String":
return value.toString();
default:
throw new ShardingException("Unsupported data type:%s", convertType);
}
}
private static Object convertDateValue(final Object value, final Class<?> convertType) {
Date date = (Date) value;
switch (convertType.getName()) {
case "java.sql.Date":
return new java.sql.Date(date.getTime());
case "java.sql.Time":
return new Time(date.getTime());
case "java.sql.Timestamp":
return new Timestamp(date.getTime());
default:
throw new ShardingException("Unsupported Date type:%s", convertType);
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.constant.ShardingConstant;
import org.apache.shardingsphere.core.parse.old.parser.constant.DerivedColumn;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.WrapperAdapter;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
/**
* Sharding result set meta data.
*
* @author zhangliang
*/
@RequiredArgsConstructor
public final class ShardingResultSetMetaData extends WrapperAdapter implements ResultSetMetaData {
private final ResultSetMetaData resultSetMetaData;
private final ShardingRule shardingRule;
@Override
public int getColumnCount() throws SQLException {
int result = 0;
for (int columnIndex = 1; columnIndex <= resultSetMetaData.getColumnCount(); columnIndex++) {
if (!DerivedColumn.isDerivedColumn(resultSetMetaData.getColumnLabel(columnIndex))) {
result++;
}
}
return result;
}
@Override
public boolean isAutoIncrement(final int column) throws SQLException {
return resultSetMetaData.isAutoIncrement(column);
}
@Override
public boolean isCaseSensitive(final int column) throws SQLException {
return resultSetMetaData.isCaseSensitive(column);
}
@Override
public boolean isSearchable(final int column) throws SQLException {
return resultSetMetaData.isSearchable(column);
}
@Override
public boolean isCurrency(final int column) throws SQLException {
return resultSetMetaData.isCurrency(column);
}
@Override
public int isNullable(final int column) throws SQLException {
return resultSetMetaData.isNullable(column);
}
@Override
public boolean isSigned(final int column) throws SQLException {
return resultSetMetaData.isSigned(column);
}
@Override
public int getColumnDisplaySize(final int column) throws SQLException {
return resultSetMetaData.getColumnDisplaySize(column);
}
@Override
public String getColumnLabel(final int column) throws SQLException {
return resultSetMetaData.getColumnLabel(column);
}
@Override
public String getColumnName(final int column) throws SQLException {
return resultSetMetaData.getColumnName(column);
}
@Override
public String getSchemaName(final int column) {
return ShardingConstant.LOGIC_SCHEMA_NAME;
}
@Override
public int getPrecision(final int column) throws SQLException {
return resultSetMetaData.getPrecision(column);
}
@Override
public int getScale(final int column) throws SQLException {
return resultSetMetaData.getScale(column);
}
@Override
public String getTableName(final int column) throws SQLException {
String actualTableName = resultSetMetaData.getTableName(column);
return shardingRule.getLogicTableNames(actualTableName).isEmpty() ? actualTableName : shardingRule.getLogicTableNames(actualTableName).iterator().next();
}
@Override
public String getCatalogName(final int column) {
return ShardingConstant.LOGIC_SCHEMA_NAME;
}
@Override
public int getColumnType(final int column) throws SQLException {
return resultSetMetaData.getColumnType(column);
}
@Override
public String getColumnTypeName(final int column) throws SQLException {
return resultSetMetaData.getColumnTypeName(column);
}
@Override
public boolean isReadOnly(final int column) throws SQLException {
return resultSetMetaData.isReadOnly(column);
}
@Override
public boolean isWritable(final int column) throws SQLException {
return resultSetMetaData.isWritable(column);
}
@Override
public boolean isDefinitelyWritable(final int column) throws SQLException {
return resultSetMetaData.isDefinitelyWritable(column);
}
@Override
public String getColumnClassName(final int column) throws SQLException {
return resultSetMetaData.getColumnClassName(column);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement;
import lombok.SneakyThrows;
import org.apache.shardingsphere.core.optimize.OptimizeEngineFactory;
import org.apache.shardingsphere.core.optimize.result.OptimizeResult;
import org.apache.shardingsphere.core.parse.antlr.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.rewrite.EncryptSQLRewriteEngine;
import org.apache.shardingsphere.core.rewrite.SQLBuilder;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.EncryptConnection;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset.EncryptResultSet;
import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.LinkedList;
/**
* Encrypt statement.
*
* @author panjuan
*/
public final class EncryptStatement extends AbstractUnsupportedOperationStatement {
private final Statement statement;
private final EncryptConnection connection;
private EncryptResultSet resultSet;
@SneakyThrows
public EncryptStatement(final EncryptConnection connection) {
statement = connection.getConnection().createStatement();
this.connection = connection;
}
@SneakyThrows
public EncryptStatement(final EncryptConnection connection, final int resultSetType, final int resultSetConcurrency) {
statement = connection.getConnection().createStatement(resultSetType, resultSetConcurrency);
this.connection = connection;
}
@SneakyThrows
public EncryptStatement(final EncryptConnection connection, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
statement = connection.getConnection().createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
this.connection = connection;
}
@Override
public ResultSet executeQuery(final String sql) throws SQLException {
ResultSet resultSet = statement.executeQuery(getRewriteSQL(sql));
this.resultSet = new EncryptResultSet(this, resultSet, connection.getEncryptRule());
return this.resultSet;
}
@Override
public ResultSet getResultSet() {
return resultSet;
}
private String getRewriteSQL(final String sql) {
SQLStatement sqlStatement = connection.getEncryptSQLParsingEngine().parse(false, sql);
OptimizeResult optimizeResult = OptimizeEngineFactory.newInstance(connection.getEncryptRule(), sqlStatement, new LinkedList<>()).optimize();
SQLBuilder sqlBuilder = new EncryptSQLRewriteEngine(connection.getEncryptRule(), sql, connection.getDatabaseType(), sqlStatement, new LinkedList<>(), optimizeResult).rewrite();
return sqlBuilder.toSQL().getSql();
}
@Override
public int executeUpdate(final String sql) throws SQLException {
return statement.executeUpdate(getRewriteSQL(sql));
}
@Override
public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
return statement.executeUpdate(getRewriteSQL(sql), autoGeneratedKeys);
}
@Override
public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
return statement.executeUpdate(getRewriteSQL(sql), columnIndexes);
}
@Override
public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
return statement.executeUpdate(getRewriteSQL(sql), columnNames);
}
@Override
public boolean execute(final String sql) throws SQLException {
boolean result = statement.execute(getRewriteSQL(sql));
this.resultSet = createEncryptResultSet(statement);
return result;
}
@Override
public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
boolean result = statement.execute(getRewriteSQL(sql), autoGeneratedKeys);
this.resultSet = createEncryptResultSet(statement);
return result;
}
@Override
public boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
boolean result = statement.execute(getRewriteSQL(sql), columnIndexes);
this.resultSet = createEncryptResultSet(statement);
return result;
}
@Override
public boolean execute(final String sql, final String[] columnNames) throws SQLException {
boolean result = statement.execute(getRewriteSQL(sql), columnNames);
this.resultSet = createEncryptResultSet(statement);
return result;
}
private EncryptResultSet createEncryptResultSet(final Statement statement) throws SQLException {
return null == statement.getResultSet() ? null : new EncryptResultSet(this, statement.getResultSet(), connection.getEncryptRule());
}
@Override
public void close() throws SQLException {
statement.close();
}
@Override
public int getMaxFieldSize() throws SQLException {
return statement.getMaxFieldSize();
}
@Override
public void setMaxFieldSize(final int max) throws SQLException {
statement.setMaxFieldSize(max);
}
@Override
public int getMaxRows() throws SQLException {
return statement.getMaxRows();
}
@Override
public void setMaxRows(final int max) throws SQLException {
statement.setMaxRows(max);
}
@Override
public void setEscapeProcessing(final boolean enable) throws SQLException {
statement.setEscapeProcessing(enable);
}
@Override
public int getQueryTimeout() throws SQLException {
return statement.getQueryTimeout();
}
@Override
public void setQueryTimeout(final int seconds) throws SQLException {
statement.setQueryTimeout(seconds);
}
@Override
public void cancel() throws SQLException {
statement.cancel();
}
@Override
public SQLWarning getWarnings() throws SQLException {
return statement.getWarnings();
}
@Override
public void clearWarnings() throws SQLException {
statement.clearWarnings();
}
@Override
public int getUpdateCount() throws SQLException {
return statement.getUpdateCount();
}
@Override
public boolean getMoreResults() throws SQLException {
return statement.getMoreResults();
}
@Override
public boolean getMoreResults(final int current) throws SQLException {
return statement.getMoreResults(current);
}
@Override
public void setFetchSize(final int rows) throws SQLException {
statement.setFetchSize(rows);
}
@Override
public int getFetchSize() throws SQLException {
return statement.getFetchSize();
}
@Override
public int getResultSetConcurrency() throws SQLException {
return statement.getResultSetConcurrency();
}
@Override
public int getResultSetType() throws SQLException {
return statement.getResultSetType();
}
@Override
public Connection getConnection() {
return connection;
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
return statement.getGeneratedKeys();
}
@Override
public int getResultSetHoldability() throws SQLException {
return statement.getResultSetHoldability();
}
@Override
public boolean isClosed() throws SQLException {
return statement.isClosed();
}
@Override
public void setPoolable(final boolean poolable) throws SQLException {
statement.setPoolable(poolable);
}
@Override
public boolean isPoolable() throws SQLException {
return statement.isPoolable();
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement;
import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.apache.shardingsphere.core.route.router.masterslave.MasterSlaveRouter;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractMasterSlavePreparedStatementAdapter;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.MasterSlaveConnection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
/**
* PreparedStatement that support master-slave.
*
* @author zhangliang
* @author panjuan
*/
@Getter
public final class MasterSlavePreparedStatement extends AbstractMasterSlavePreparedStatementAdapter {
private final MasterSlaveConnection connection;
@Getter(AccessLevel.NONE)
private final MasterSlaveRouter masterSlaveRouter;
private final Collection<PreparedStatement> routedStatements = new LinkedList<>();
public MasterSlavePreparedStatement(final MasterSlaveConnection connection, final String sql) throws SQLException {
this(connection, sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}
public MasterSlavePreparedStatement(final MasterSlaveConnection connection, final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
this(connection, sql, resultSetType, resultSetConcurrency, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}
public MasterSlavePreparedStatement(
final MasterSlaveConnection connection, final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
this.connection = connection;
masterSlaveRouter = new MasterSlaveRouter(connection.getMasterSlaveDataSource().getMasterSlaveRule(),
connection.getMasterSlaveDataSource().getShardingProperties().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
for (String each : masterSlaveRouter.route(sql)) {
PreparedStatement preparedStatement = connection.getConnection(each).prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
routedStatements.add(preparedStatement);
}
}
public MasterSlavePreparedStatement(final MasterSlaveConnection connection, final String sql, final int autoGeneratedKeys) throws SQLException {
this.connection = connection;
masterSlaveRouter = new MasterSlaveRouter(connection.getMasterSlaveDataSource().getMasterSlaveRule(),
connection.getMasterSlaveDataSource().getShardingProperties().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
for (String each : masterSlaveRouter.route(sql)) {
PreparedStatement preparedStatement = connection.getConnection(each).prepareStatement(sql, autoGeneratedKeys);
routedStatements.add(preparedStatement);
}
}
public MasterSlavePreparedStatement(final MasterSlaveConnection connection, final String sql, final int[] columnIndexes) throws SQLException {
this.connection = connection;
masterSlaveRouter = new MasterSlaveRouter(connection.getMasterSlaveDataSource().getMasterSlaveRule(),
connection.getMasterSlaveDataSource().getShardingProperties().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
for (String each : masterSlaveRouter.route(sql)) {
PreparedStatement preparedStatement = connection.getConnection(each).prepareStatement(sql, columnIndexes);
routedStatements.add(preparedStatement);
}
}
public MasterSlavePreparedStatement(final MasterSlaveConnection connection, final String sql, final String[] columnNames) throws SQLException {
this.connection = connection;
masterSlaveRouter = new MasterSlaveRouter(connection.getMasterSlaveDataSource().getMasterSlaveRule(),
connection.getMasterSlaveDataSource().getShardingProperties().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
for (String each : masterSlaveRouter.route(sql)) {
PreparedStatement preparedStatement = connection.getConnection(each).prepareStatement(sql, columnNames);
routedStatements.add(preparedStatement);
}
}
@Override
public ResultSet executeQuery() throws SQLException {
Preconditions.checkArgument(1 == routedStatements.size(), "Cannot support executeQuery for DDL");
return routedStatements.iterator().next().executeQuery();
}
@Override
public int executeUpdate() throws SQLException {
int result = 0;
for (PreparedStatement each : routedStatements) {
result += each.executeUpdate();
}
return result;
}
@Override
public boolean execute() throws SQLException {
boolean result = false;
for (PreparedStatement each : routedStatements) {
result = each.execute();
}
return result;
}
@Override
public void clearBatch() throws SQLException {
Preconditions.checkArgument(1 == routedStatements.size(), "Cannot support clearBatch for DDL");
routedStatements.iterator().next().clearBatch();
}
@Override
public void addBatch() throws SQLException {
Preconditions.checkArgument(1 == routedStatements.size(), "Cannot support addBatch for DDL");
routedStatements.iterator().next().addBatch();
}
@Override
public int[] executeBatch() throws SQLException {
Preconditions.checkArgument(1 == routedStatements.size(), "Cannot support executeBatch for DDL");
return routedStatements.iterator().next().executeBatch();
}
@Override
public ResultSet getResultSet() throws SQLException {
Preconditions.checkArgument(1 == routedStatements.size(), "Cannot support getResultSet for DDL");
return routedStatements.iterator().next().getResultSet();
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
Preconditions.checkArgument(1 == routedStatements.size(), "Cannot support getGeneratedKeys for DDL");
return routedStatements.iterator().next().getGeneratedKeys();
}
@Override
public int getResultSetHoldability() throws SQLException {
return routedStatements.iterator().next().getResultSetHoldability();
}
@Override
public int getResultSetConcurrency() throws SQLException {
return routedStatements.iterator().next().getResultSetConcurrency();
}
@Override
public int getResultSetType() throws SQLException {
return routedStatements.iterator().next().getResultSetType();
}
@Override
public boolean isAccumulate() {
return false;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement;
import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.apache.shardingsphere.core.route.router.masterslave.MasterSlaveRouter;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractStatementAdapter;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.MasterSlaveConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.LinkedList;
/**
* Statement that support master-slave.
*
* @author zhangliang
* @author panjuan
*/
@Getter
public final class MasterSlaveStatement extends AbstractStatementAdapter {
private final MasterSlaveConnection connection;
@Getter(AccessLevel.NONE)
private final MasterSlaveRouter masterSlaveRouter;
private final int resultSetType;
private final int resultSetConcurrency;
private final int resultSetHoldability;
private final Collection<Statement> routedStatements = new LinkedList<>();
public MasterSlaveStatement(final MasterSlaveConnection connection) {
this(connection, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}
public MasterSlaveStatement(final MasterSlaveConnection connection, final int resultSetType, final int resultSetConcurrency) {
this(connection, resultSetType, resultSetConcurrency, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}
public MasterSlaveStatement(final MasterSlaveConnection connection, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
super(Statement.class);
this.connection = connection;
masterSlaveRouter = new MasterSlaveRouter(connection.getMasterSlaveDataSource().getMasterSlaveRule(),
connection.getMasterSlaveDataSource().getShardingProperties().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
this.resultSetType = resultSetType;
this.resultSetConcurrency = resultSetConcurrency;
this.resultSetHoldability = resultSetHoldability;
}
@Override
public ResultSet executeQuery(final String sql) throws SQLException {
clearPrevious();
Collection<String> dataSourceNames = masterSlaveRouter.route(sql);
Preconditions.checkState(1 == dataSourceNames.size(), "Cannot support executeQuery for DML or DDL");
Statement statement = connection.getConnection(dataSourceNames.iterator().next()).createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
routedStatements.add(statement);
return statement.executeQuery(sql);
}
@Override
public int executeUpdate(final String sql) throws SQLException {
clearPrevious();
int result = 0;
for (String each : masterSlaveRouter.route(sql)) {
Statement statement = connection.getConnection(each).createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
routedStatements.add(statement);
result += statement.executeUpdate(sql);
}
return result;
}
@Override
public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
clearPrevious();
int result = 0;
for (String each : masterSlaveRouter.route(sql)) {
Statement statement = connection.getConnection(each).createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
routedStatements.add(statement);
result += statement.executeUpdate(sql, autoGeneratedKeys);
}
return result;
}
@Override
public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
clearPrevious();
int result = 0;
for (String each : masterSlaveRouter.route(sql)) {
Statement statement = connection.getConnection(each).createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
routedStatements.add(statement);
result += statement.executeUpdate(sql, columnIndexes);
}
return result;
}
@Override
public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
clearPrevious();
int result = 0;
for (String each : masterSlaveRouter.route(sql)) {
Statement statement = connection.getConnection(each).createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
routedStatements.add(statement);
result += statement.executeUpdate(sql, columnNames);
}
return result;
}
@Override
public boolean execute(final String sql) throws SQLException {
clearPrevious();
boolean result = false;
for (String each : masterSlaveRouter.route(sql)) {
Statement statement = connection.getConnection(each).createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
routedStatements.add(statement);
result = statement.execute(sql);
}
return result;
}
@Override
public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
clearPrevious();
boolean result = false;
for (String each : masterSlaveRouter.route(sql)) {
Statement statement = connection.getConnection(each).createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
routedStatements.add(statement);
result = statement.execute(sql, autoGeneratedKeys);
}
return result;
}
@Override
public boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
clearPrevious();
boolean result = false;
for (String each : masterSlaveRouter.route(sql)) {
Statement statement = connection.getConnection(each).createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
routedStatements.add(statement);
result = statement.execute(sql, columnIndexes);
}
return result;
}
@Override
public boolean execute(final String sql, final String[] columnNames) throws SQLException {
clearPrevious();
boolean result = false;
for (String each : masterSlaveRouter.route(sql)) {
Statement statement = connection.getConnection(each).createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
routedStatements.add(statement);
result = statement.execute(sql, columnNames);
}
return result;
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
Preconditions.checkState(1 == routedStatements.size());
return routedStatements.iterator().next().getGeneratedKeys();
}
@Override
public ResultSet getResultSet() throws SQLException {
Preconditions.checkState(1 == routedStatements.size());
return routedStatements.iterator().next().getResultSet();
}
private void clearPrevious() throws SQLException {
for (Statement each : routedStatements) {
each.close();
}
routedStatements.clear();
}
@Override
public boolean isAccumulate() {
return false;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.metadata;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.execute.metadata.TableMetaDataConnectionManager;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
/**
* Manager of connection which for table meta data loader of JDBC.
*
* @author zhangliang
*/
@RequiredArgsConstructor
public final class JDBCTableMetaDataConnectionManager implements TableMetaDataConnectionManager {
private final Map<String, DataSource> dataSourceMap;
@Override
public Connection getConnection(final String dataSourceName) throws SQLException {
return dataSourceMap.get(dataSourceName).getConnection();
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.unsupported;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
/**
* Unsupported Database meta data result set.
*
* @author yangyi
*/
public abstract class AbstractUnsupportedDatabaseMetaDataResultSet extends AbstractUnsupportedOperationResultSet {
@Override
public final BigDecimal getBigDecimal(final int columnIndex, final int scale) throws SQLException {
throw new SQLFeatureNotSupportedException("getBigDecimal");
}
@Override
public final BigDecimal getBigDecimal(final String columnLabel, final int scale) throws SQLException {
throw new SQLFeatureNotSupportedException("getBigDecimal");
}
@Override
public final BigDecimal getBigDecimal(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getBigDecimal");
}
@Override
public final BigDecimal getBigDecimal(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getBigDecimal");
}
@Override
public final InputStream getAsciiStream(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getAsciiStream");
}
@Override
public final InputStream getAsciiStream(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getAsciiStream");
}
@Override
public final InputStream getUnicodeStream(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getUnicodeStream");
}
@Override
public final InputStream getUnicodeStream(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getUnicodeStream");
}
@Override
public final InputStream getBinaryStream(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getBinaryStream");
}
@Override
public final InputStream getBinaryStream(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getBinaryStream");
}
@Override
public final SQLWarning getWarnings() throws SQLException {
throw new SQLFeatureNotSupportedException("getWarnings");
}
@Override
public final void clearWarnings() throws SQLException {
throw new SQLFeatureNotSupportedException("clearWarnings");
}
@Override
public final Reader getCharacterStream(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getCharacterStream");
}
@Override
public final Reader getCharacterStream(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getCharacterStream");
}
@Override
public final void setFetchDirection(final int direction) throws SQLException {
throw new SQLFeatureNotSupportedException("setFetchDirection");
}
@Override
public final int getFetchDirection() throws SQLException {
throw new SQLFeatureNotSupportedException("getFetchDirection");
}
@Override
public final void setFetchSize(final int rows) throws SQLException {
throw new SQLFeatureNotSupportedException("setFetchSize");
}
@Override
public final int getFetchSize() throws SQLException {
throw new SQLFeatureNotSupportedException("getFetchSize");
}
@Override
public final Statement getStatement() throws SQLException {
throw new SQLFeatureNotSupportedException("getStatement");
}
@Override
public final Blob getBlob(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getBlob");
}
@Override
public final Blob getBlob(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getBlob");
}
@Override
public final Clob getClob(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getClob");
}
@Override
public final Clob getClob(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getClob");
}
@Override
public final Date getDate(final int columnIndex, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getDate(int columnIndex, Calendar cal)");
}
@Override
public final Date getDate(final String columnLabel, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getDate(String columnLabel, Calendar cal)");
}
@Override
public final Time getTime(final int columnIndex, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getTime(int columnIndex, Calendar cal)");
}
@Override
public final Time getTime(final String columnLabel, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getTime(String columnLabel, Calendar cal)");
}
@Override
public final Timestamp getTimestamp(final int columnIndex, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getTime(int columnIndex, Calendar cal)");
}
@Override
public final Timestamp getTimestamp(final String columnLabel, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getTime(String columnLabel, Calendar cal)");
}
@Override
public final URL getURL(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getURL");
}
@Override
public final URL getURL(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getURL");
}
@Override
public final SQLXML getSQLXML(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getSQLXML");
}
@Override
public final SQLXML getSQLXML(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getSQLXML");
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.jdbc.unsupported;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
/**
* Unsupported {@code ResultSet} methods for generated keys.
*
* @author gaohongtao
*/
public abstract class AbstractUnsupportedGeneratedKeysResultSet extends AbstractUnsupportedOperationResultSet {
@Override
public final boolean getBoolean(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getBoolean");
}
@Override
public final boolean getBoolean(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getBoolean");
}
@Override
public final Date getDate(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getDate");
}
@Override
public final Date getDate(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getDate");
}
@Override
public final Date getDate(final int columnIndex, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getDate");
}
@Override
public final Date getDate(final String columnLabel, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getDate");
}
@Override
public final Time getTime(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getTime");
}
@Override
public final Time getTime(final int columnIndex, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getTime");
}
@Override
public final Time getTime(final String columnLabel, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getTime");
}
@Override
public final Time getTime(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getTime");
}
@Override
public final Timestamp getTimestamp(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getTimestamp");
}
@Override
public final Timestamp getTimestamp(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getTimestamp");
}
@Override
public final Timestamp getTimestamp(final int columnIndex, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getTimestamp");
}
@Override
public final Timestamp getTimestamp(final String columnLabel, final Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("getTimestamp");
}
@Override
public final InputStream getAsciiStream(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getAsciiStream");
}
@Override
public final InputStream getAsciiStream(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getAsciiStream");
}
@SuppressWarnings("deprecation")
@Override
public final InputStream getUnicodeStream(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getUnicodeStream");
}
@SuppressWarnings("deprecation")
@Override
public final InputStream getUnicodeStream(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getUnicodeStream");
}
@Override
public final InputStream getBinaryStream(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getBinaryStream");
}
@Override
public final InputStream getBinaryStream(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getBinaryStream");
}
@Override
public final SQLWarning getWarnings() throws SQLException {
throw new SQLFeatureNotSupportedException("getWarnings");
}
@Override
public final void clearWarnings() throws SQLException {
throw new SQLFeatureNotSupportedException("clearWarnings");
}
@Override
public final Reader getCharacterStream(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getCharacterStream");
}
@Override
public final Reader getCharacterStream(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getCharacterStream");
}
@Override
public final void setFetchDirection(final int direction) throws SQLException {
throw new SQLFeatureNotSupportedException("setFetchDirection");
}
@Override
public final int getFetchDirection() throws SQLException {
throw new SQLFeatureNotSupportedException("getFetchDirection");
}
@Override
public final void setFetchSize(final int rows) throws SQLException {
throw new SQLFeatureNotSupportedException("setFetchSize");
}
@Override
public final int getFetchSize() throws SQLException {
throw new SQLFeatureNotSupportedException("getFetchSize");
}
@Override
public final Blob getBlob(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getBlob");
}
@Override
public final Blob getBlob(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getBlob");
}
@Override
public final Clob getClob(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getClob");
}
@Override
public final Clob getClob(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getClob");
}
@Override
public final URL getURL(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getURL");
}
@Override
public final URL getURL(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getURL");
}
@Override
public final SQLXML getSQLXML(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getSQLXML");
}
@Override
public final SQLXML getSQLXML(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getSQLXML");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment