From c0308e1a30653bbaefada7802f88c74cb071ac1e Mon Sep 17 00:00:00 2001 From: wenshao Date: Mon, 26 Nov 2012 19:34:30 +0800 Subject: [PATCH] remove sharding --- .../druid/sharding/ShardingConnection.java | 698 --------------- .../druid/sharding/ShardingDataSource.java | 162 ---- .../sharding/ShardingPreparedStatement.java | 716 --------------- .../sharding/ShardingRuntimeException.java | 40 - .../druid/sharding/ShardingStatement.java | 813 ------------------ .../druid/sharding/config/LogicTable.java | 49 -- .../druid/sharding/config/MappingRule.java | 29 - .../sharding/config/MappingRuleAdapter.java | 71 -- .../sharding/config/MappingRuleEntry.java | 23 - .../sharding/config/MappingRuleListEntry.java | 64 -- .../sharding/config/MappingRuleLtEntry.java | 45 - .../druid/sharding/config/RouteConfig.java | 85 -- .../druid/sharding/config/TablePartition.java | 62 -- .../sharding/sql/MySqlShardingVisitor.java | 404 --------- .../druid/sharding/sql/ShardingVisitor.java | 32 - .../bvt/sharding/MySqlShardingDeleteTest.java | 81 -- .../bvt/sharding/MySqlShardingSelectTest.java | 83 -- .../bvt/sharding/MySqlShardingUpdateTest.java | 83 -- .../bvt/sharding/MysqlShardingInsertTest.java | 76 -- 19 files changed, 3616 deletions(-) delete mode 100644 src/main/java/com/alibaba/druid/sharding/ShardingConnection.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/ShardingDataSource.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/ShardingPreparedStatement.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/ShardingRuntimeException.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/ShardingStatement.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/config/LogicTable.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/config/MappingRule.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/config/MappingRuleAdapter.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/config/MappingRuleEntry.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/config/MappingRuleListEntry.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/config/MappingRuleLtEntry.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/config/RouteConfig.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/config/TablePartition.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/sql/MySqlShardingVisitor.java delete mode 100644 src/main/java/com/alibaba/druid/sharding/sql/ShardingVisitor.java delete mode 100644 src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingDeleteTest.java delete mode 100644 src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingSelectTest.java delete mode 100644 src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingUpdateTest.java delete mode 100644 src/test/java/com/alibaba/druid/bvt/sharding/MysqlShardingInsertTest.java diff --git a/src/main/java/com/alibaba/druid/sharding/ShardingConnection.java b/src/main/java/com/alibaba/druid/sharding/ShardingConnection.java deleted file mode 100644 index 4ae97aaaa4..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/ShardingConnection.java +++ /dev/null @@ -1,698 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding; - -import java.sql.Array; -import java.sql.Blob; -import java.sql.CallableStatement; -import java.sql.Clob; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.NClob; -import java.sql.PreparedStatement; -import java.sql.SQLClientInfoException; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.sql.SQLWarning; -import java.sql.SQLXML; -import java.sql.Savepoint; -import java.sql.Statement; -import java.sql.Struct; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.Executor; - -import com.alibaba.druid.pool.WrapperAdapter; -import com.alibaba.druid.pool.ha.DataSourceHolder; -import com.alibaba.druid.proxy.jdbc.CallableStatementProxyImpl; -import com.alibaba.druid.proxy.jdbc.ConnectionProxy; -import com.alibaba.druid.proxy.jdbc.DataSourceProxy; -import com.alibaba.druid.proxy.jdbc.TransactionInfo; -import com.alibaba.druid.util.JdbcUtils; - -public class ShardingConnection extends WrapperAdapter implements Connection, ConnectionProxy { - - private final ShardingDataSource dataSource; - - private Connection conn; - private DataSourceHolder dataSourceHolder; - - private final long id; - - private Boolean autoCommit = null; - private Boolean readOnly = null; - private String catalog = null; - private Integer transactionLeval = null; - private Map> typeMap = null; - private Integer holdability = null; - - private Properties clientInfo = null; - - private Map attributes = null; - - private Date connectedTime = null; - - private int closeCount = 0; - - private TransactionInfo transcationInfo; - - private String database; - - public ShardingConnection(ShardingDataSource dataSource, long id){ - this.dataSource = dataSource; - this.id = id; - } - - public String getDatabase() { - return database; - } - - public void setDatabase(String database) { - this.database = database; - } - - public DataSourceHolder getDataSourceHolder() { - return dataSourceHolder; - } - - public void checkConnection(String sql) throws SQLException { - if (conn == null) { - conn = dataSource.getConnectionBySql(sql); - } - - initConnection(conn); - - connectedTime = new Date(); - } - - private void initConnection(Connection conn) throws SQLException, SQLClientInfoException { - if (autoCommit != null) { - conn.setAutoCommit(autoCommit); - } - - if (readOnly != null) { - conn.setReadOnly(readOnly); - } - - if (catalog != null) { - conn.setCatalog(catalog); - } - - if (transactionLeval != null) { - conn.setTransactionIsolation(transactionLeval); - } - - if (typeMap != null) { - conn.setTypeMap(typeMap); - } - - if (holdability != null) { - conn.setHoldability(holdability); - } - - if (clientInfo != null) { - conn.setClientInfo(clientInfo); - } - } - - public ShardingDataSource getDataSource() { - return dataSource; - } - - @Override - public String nativeSQL(String sql) throws SQLException { - checkConnection(sql); - - return conn.nativeSQL(sql); - } - - public Connection createRealConnectionByDb(String db) throws SQLException { - if (conn != null) { - JdbcUtils.close(conn); - conn = null; - } - - conn = dataSource.getConnectionByDb(db); - initConnection(conn); - return conn; - } - - public Connection getRealConnection() { - return conn; - } - - public void closeRealConnection() { - if (conn != null) { - JdbcUtils.close(conn); - conn = null; - } - } - - @Override - public void setAutoCommit(boolean autoCommit) throws SQLException { - if (conn != null) { - conn.setAutoCommit(autoCommit); - } else { - this.autoCommit = Boolean.valueOf(autoCommit); - } - - if (!autoCommit) { - if (transcationInfo == null) { - long transactionId = this.getDirectDataSource().createTransactionId(); - transcationInfo = new TransactionInfo(transactionId); - } - } else { - transcationInfo = null; - } - } - - @Override - public boolean getAutoCommit() throws SQLException { - if (conn != null) { - return conn.getAutoCommit(); - } - - if (this.autoCommit != null) { - return this.autoCommit.booleanValue(); - } - - return false; - } - - @Override - public void commit() throws SQLException { - if (conn != null) { - conn.commit(); - } - - if (transcationInfo != null) { - transcationInfo.setEndTimeMillis(); - } - } - - @Override - public void rollback() throws SQLException { - if (conn != null) { - conn.rollback(); - } - - if (transcationInfo != null) { - transcationInfo.setEndTimeMillis(); - } - } - - @Override - public void close() throws SQLException { - if (closeCount > 0) { - closeCount++; - return; - } - - if (conn != null) { - conn.close(); - } - this.closeCount++; - - dataSource.afterConnectionClosed(this); - } - - @Override - public boolean isClosed() throws SQLException { - return closeCount > 0; - } - - @Override - public DatabaseMetaData getMetaData() throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - return conn.getMetaData(); - } - - @Override - public void setReadOnly(boolean readOnly) throws SQLException { - if (conn != null) { - conn.setReadOnly(readOnly); - } else { - this.readOnly = Boolean.valueOf(readOnly); - } - } - - @Override - public boolean isReadOnly() throws SQLException { - if (conn != null) { - return conn.isReadOnly(); - } - - if (this.readOnly != null) { - return this.readOnly.booleanValue(); - } - - return false; - } - - @Override - public void setCatalog(String catalog) throws SQLException { - if (conn != null) { - conn.setCatalog(catalog); - } else { - this.catalog = catalog; - } - } - - @Override - public String getCatalog() throws SQLException { - if (conn != null) { - return conn.getCatalog(); - } - - return this.catalog; - } - - @Override - public void setTransactionIsolation(int level) throws SQLException { - if (conn != null) { - conn.setTransactionIsolation(level); - } else { - this.transactionLeval = level; - } - } - - @Override - public int getTransactionIsolation() throws SQLException { - if (conn != null) { - return conn.getTransactionIsolation(); - } - - if (this.transactionLeval != null) { - return this.transactionLeval; - } - - return TRANSACTION_NONE; - } - - @Override - public SQLWarning getWarnings() throws SQLException { - if (conn != null) { - return conn.getWarnings(); - } - - return null; - } - - @Override - public void clearWarnings() throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - if (conn != null) { - conn.clearWarnings(); - } - } - - @Override - public Map> getTypeMap() throws SQLException { - if (conn != null) { - return conn.getTypeMap(); - } - - return typeMap; - } - - @Override - public void setTypeMap(Map> map) throws SQLException { - if (conn != null) { - conn.setTypeMap(map); - } else { - this.typeMap = map; - } - } - - @Override - public void setHoldability(int holdability) throws SQLException { - if (conn != null) { - conn.setHoldability(holdability); - } else { - this.holdability = holdability; - } - } - - @Override - public int getHoldability() throws SQLException { - if (conn != null) { - return conn.getHoldability(); - } - - if (this.holdability != null) { - return this.holdability; - } - - return 0; - } - - @Override - public Savepoint setSavepoint() throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - return conn.setSavepoint(); - } - - @Override - public Savepoint setSavepoint(String name) throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - return conn.setSavepoint(name); - } - - @Override - public void rollback(Savepoint savepoint) throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - conn.rollback(savepoint); - - if (transcationInfo != null) { - transcationInfo.setEndTimeMillis(); - } - } - - @Override - public void releaseSavepoint(Savepoint savepoint) throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - conn.releaseSavepoint(savepoint); - } - - @Override - public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { - long stmtId = dataSource.createStatementId(); - ShardingStatement stmt = new ShardingStatement(this, stmtId, resultSetType, resultSetConcurrency); - return stmt; - } - - @Override - public Statement createStatement() throws SQLException { - long stmtId = dataSource.createStatementId(); - ShardingStatement stmt = new ShardingStatement(this, stmtId); - return stmt; - } - - @Override - public PreparedStatement prepareStatement(String sql) throws SQLException { - long stmtId = dataSource.createStatementId(); - return new ShardingPreparedStatement(this, sql, stmtId); - } - - @Override - public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) - throws SQLException { - long stmtId = dataSource.createStatementId(); - - return new ShardingPreparedStatement(this, sql, stmtId); - } - - @Override - public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { - // TODO - throw new UnsupportedOperationException(); - } - - @Override - public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { - // TODO - throw new UnsupportedOperationException(); - } - - @Override - public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { - // TODO - throw new UnsupportedOperationException(); - } - - @Override - public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, - int resultSetHoldability) throws SQLException { - - long stmtId = dataSource.createStatementId(); - - return new ShardingPreparedStatement(this, sql, stmtId, resultSetType, resultSetConcurrency, - resultSetHoldability); - } - - @Override - public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { - throw new UnsupportedOperationException(); - } - - @Override - public CallableStatement prepareCall(String sql) throws SQLException { - checkConnection(sql); - - CallableStatement stmt = conn.prepareCall(sql); - long stmtId = dataSource.createStatementId(); - - return new CallableStatementProxyImpl(this, stmt, sql, stmtId); - } - - @Override - public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) - throws SQLException { - long stmtId = dataSource.createStatementId(); - ShardingStatement stmt = new ShardingStatement(this, stmtId, resultSetType, resultSetConcurrency, - resultSetHoldability); - return stmt; - } - - @Override - public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, - int resultSetHoldability) throws SQLException { - checkConnection(sql); - - CallableStatement stmt = conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); - long stmtId = dataSource.createStatementId(); - - return new CallableStatementProxyImpl(this, stmt, sql, stmtId); - } - - @Override - public Clob createClob() throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - return conn.createClob(); - } - - @Override - public Blob createBlob() throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - return conn.createBlob(); - } - - @Override - public NClob createNClob() throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - return conn.createNClob(); - } - - @Override - public SQLXML createSQLXML() throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - return conn.createSQLXML(); - } - - @Override - public boolean isValid(int timeout) throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - return conn.isValid(timeout); - } - - @Override - public void setClientInfo(String name, String value) throws SQLClientInfoException { - if (conn != null) { - conn.setClientInfo(name, value); - return; - } - - if (clientInfo == null) { - clientInfo = new Properties(); - } - clientInfo.setProperty(name, value); - } - - @Override - public void setClientInfo(Properties properties) throws SQLClientInfoException { - if (conn != null) { - conn.setClientInfo(properties); - return; - } - - this.clientInfo = properties; - } - - @Override - public String getClientInfo(String name) throws SQLException { - if (conn != null) { - return conn.getClientInfo(name); - } - - if (clientInfo == null) { - return null; - } - - return clientInfo.getProperty(name); - } - - @Override - public Properties getClientInfo() throws SQLException { - if (conn != null) { - return conn.getClientInfo(); - } - - return clientInfo; - } - - @Override - public Array createArrayOf(String typeName, Object[] elements) throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - return conn.createArrayOf(typeName, elements); - } - - @Override - public Struct createStruct(String typeName, Object[] attributes) throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - return conn.createStruct(typeName, attributes); - } - - // ///// - - @Override - public long getId() { - return id; - } - - @Override - public Connection getRawObject() { - return conn; - } - - @Override - public Map getAttributes() { - if (attributes == null) { - attributes = new HashMap(); - } - return attributes; - } - - @Override - public Properties getProperties() { - return dataSource.getProperties(); - } - - @Override - public DataSourceProxy getDirectDataSource() { - return dataSource; - } - - @Override - public Date getConnectedTime() { - return connectedTime; - } - - public boolean isWrapperFor(Class iface) throws SQLException { - boolean result = super.isWrapperFor(iface); - - if ((!result) && conn != null) { - result = conn.isWrapperFor(iface); - } - - return result; - } - - public T unwrap(Class iface) throws SQLException { - T object = super.unwrap(iface); - - if (object == null && conn != null) { - object = conn.unwrap(iface); - } - - return object; - } - - public void setSchema(String schema) throws SQLException { - throw new SQLFeatureNotSupportedException(); - } - - public String getSchema() throws SQLException { - throw new SQLFeatureNotSupportedException(); - } - - public void abort(Executor executor) throws SQLException { - if (conn == null) { - throw new SQLException("connection not init"); - } - - throw new SQLFeatureNotSupportedException(); - } - - public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { - throw new SQLFeatureNotSupportedException(); - } - - public int getNetworkTimeout() throws SQLException { - throw new SQLFeatureNotSupportedException(); - } - - @Override - public TransactionInfo getTransactionInfo() { - return null; - } - - @Override - public int getCloseCount() { - return closeCount; - } - -} diff --git a/src/main/java/com/alibaba/druid/sharding/ShardingDataSource.java b/src/main/java/com/alibaba/druid/sharding/ShardingDataSource.java deleted file mode 100644 index f5846b49a1..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/ShardingDataSource.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding; - -import java.sql.Connection; -import java.sql.Driver; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicLong; - -import javax.sql.DataSource; - -import com.alibaba.druid.filter.Filter; -import com.alibaba.druid.pool.DataSourceAdapter; -import com.alibaba.druid.pool.DruidPooledConnection; -import com.alibaba.druid.proxy.jdbc.DataSourceProxy; -import com.alibaba.druid.sharding.config.RouteConfig; -import com.alibaba.druid.sharding.sql.MySqlShardingVisitor; -import com.alibaba.druid.sharding.sql.ShardingVisitor; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.parser.SQLParserUtils; -import com.alibaba.druid.sql.parser.SQLStatementParser; -import com.alibaba.druid.stat.JdbcDataSourceStat; - -public class ShardingDataSource extends DataSourceAdapter implements DataSource, DataSourceProxy { - - private final AtomicLong connectionIdSeed = new AtomicLong(); - private final AtomicLong statementIdSeed = new AtomicLong(); - private final AtomicLong resultSetIdSeed = new AtomicLong(); - private final AtomicLong transactionIdSeed = new AtomicLong(); - private final Properties properties = new Properties(); - private String dbType; - private List filters = new CopyOnWriteArrayList(); - - private RouteConfig routeConfig = new RouteConfig(); - - public Connection getConnectionByPartition(String partition) { - throw new UnsupportedOperationException(); - } - - public Connection getConnectionByDb(String partition) { - throw new UnsupportedOperationException(); - } - - public DruidPooledConnection getConnectionBySql(String partition) { - throw new UnsupportedOperationException(); - } - - protected void afterConnectionClosed(ShardingConnection conn) { - - } - - public ShardingVisitor createShardingVisitor() { - return createShardingVisitor(new ArrayList()); - } - - public SQLStatement parseStatement(String sql) throws SQLException { - SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType); - List stmtList = parser.parseStatementList(); - if (stmtList.size() > 0) { - throw new SQLException("not support multi-statement"); - } - - return stmtList.get(0); - } - - public ShardingVisitor createShardingVisitor(List parameters) { - return new MySqlShardingVisitor(routeConfig, parameters); - } - - public RouteConfig getRouteConfig() { - return routeConfig; - } - - public void setRouteConfig(RouteConfig routeConfig) { - this.routeConfig = routeConfig; - } - - public void setDbType(String dbType) { - this.dbType = dbType; - } - - public long createStatementId() { - return statementIdSeed.incrementAndGet(); - } - - public long createConnectionId() { - return connectionIdSeed.getAndIncrement(); - } - - public Properties getProperties() { - return properties; - } - - @Override - public JdbcDataSourceStat getDataSourceStat() { - return null; - } - - @Override - public String getName() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getDbType() { - return dbType; - } - - @Override - public Driver getRawDriver() { - return null; - } - - @Override - public String getUrl() { - return null; - } - - @Override - public String getRawJdbcUrl() { - return null; - } - - @Override - public List getProxyFilters() { - return filters; - } - - @Override - public long createResultSetId() { - return resultSetIdSeed.incrementAndGet(); - } - - @Override - public long createTransactionId() { - return transactionIdSeed.incrementAndGet(); - } - - @Override - public Properties getConnectProperties() { - return properties; - } - -} diff --git a/src/main/java/com/alibaba/druid/sharding/ShardingPreparedStatement.java b/src/main/java/com/alibaba/druid/sharding/ShardingPreparedStatement.java deleted file mode 100644 index 441161ef46..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/ShardingPreparedStatement.java +++ /dev/null @@ -1,716 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding; - -import java.io.InputStream; -import java.io.Reader; -import java.math.BigDecimal; -import java.net.URL; -import java.sql.Array; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.Connection; -import java.sql.Date; -import java.sql.NClob; -import java.sql.ParameterMetaData; -import java.sql.PreparedStatement; -import java.sql.Ref; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.RowId; -import java.sql.SQLException; -import java.sql.SQLXML; -import java.sql.Time; -import java.sql.Timestamp; -import java.sql.Types; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -import org.h2.util.StringUtils; - -import com.alibaba.druid.proxy.jdbc.JdbcParameter; -import com.alibaba.druid.proxy.jdbc.JdbcParameter.TYPE; -import com.alibaba.druid.sharding.sql.ShardingVisitor; -import com.alibaba.druid.sql.SQLUtils; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; -import com.alibaba.druid.util.JdbcUtils; - -public class ShardingPreparedStatement extends ShardingStatement implements PreparedStatement { - - private final String sql; - private PreparedStatement pstmt; - - private List tables; - protected final Map parameters = new TreeMap(); - - public ShardingPreparedStatement(ShardingConnection conn, String sql, long id){ - super(conn, id); - this.sql = sql; - } - - public ShardingPreparedStatement(ShardingConnection conn, String sql, long id, int resultSetType, - int resultSetConcurrency, int resultSetHoldability){ - super(conn, id, resultSetType, resultSetConcurrency, resultSetHoldability); - this.sql = sql; - } - - public ShardingPreparedStatement(ShardingConnection conn, String sql, long id, int resultSetType, - int resultSetConcurrency){ - super(conn, id, resultSetType, resultSetConcurrency); - this.sql = sql; - } - - @Override - public void setArray(int parameterIndex, Array x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.ARRAY, x)); - } - - @Override - public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(JdbcParameter.TYPE.AsciiInputStream, x)); - } - - @Override - public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(JdbcParameter.TYPE.AsciiInputStream, x, length)); - } - - @Override - public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(JdbcParameter.TYPE.AsciiInputStream, x, length)); - } - - @Override - public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.DECIMAL, x)); - } - - @Override - public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(JdbcParameter.TYPE.BinaryInputStream, x)); - - } - - @Override - public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(JdbcParameter.TYPE.BinaryInputStream, x, length)); - - } - - @Override - public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(JdbcParameter.TYPE.BinaryInputStream, x, length)); - - } - - @Override - public void setBlob(int parameterIndex, Blob x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.BLOB, x)); - - } - - @Override - public void setBlob(int parameterIndex, InputStream x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.BLOB, x)); - - } - - @Override - public void setBlob(int parameterIndex, InputStream x, long length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.BLOB, x, length)); - } - - @Override - public void setBoolean(int parameterIndex, boolean x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.BOOLEAN, x)); - } - - @Override - public void setByte(int parameterIndex, byte x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.TINYINT, x)); - - } - - @Override - public void setBytes(int parameterIndex, byte[] x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(TYPE.BYTES, x)); - - } - - @Override - public void setCharacterStream(int parameterIndex, Reader x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(TYPE.CharacterInputStream, x)); - - } - - @Override - public void setCharacterStream(int parameterIndex, Reader x, int length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(TYPE.CharacterInputStream, x, length)); - - } - - @Override - public void setCharacterStream(int parameterIndex, Reader x, long length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(TYPE.CharacterInputStream, x, length)); - - } - - @Override - public void setClob(int parameterIndex, Clob x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.CLOB, x)); - - } - - @Override - public void setClob(int parameterIndex, Reader x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.CLOB, x)); - - } - - @Override - public void setClob(int parameterIndex, Reader x, long length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.CLOB, x, length)); - - } - - @Override - public void setDate(int parameterIndex, Date x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.DATE, x)); - - } - - @Override - public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.DATE, x, cal)); - - } - - @Override - public void setDouble(int parameterIndex, double x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.DOUBLE, x)); - - } - - @Override - public void setFloat(int parameterIndex, float x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.FLOAT, x)); - - } - - @Override - public void setInt(int parameterIndex, int x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.INTEGER, x)); - - } - - @Override - public void setLong(int parameterIndex, long x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.BIGINT, x)); - - } - - @Override - public void setNCharacterStream(int parameterIndex, Reader x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(TYPE.NCharacterInputStream, x)); - - } - - @Override - public void setNCharacterStream(int parameterIndex, Reader x, long length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(TYPE.NCharacterInputStream, x, length)); - - } - - @Override - public void setNClob(int parameterIndex, NClob x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.NCLOB, x)); - - } - - @Override - public void setNClob(int parameterIndex, Reader x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.NCLOB, x)); - - } - - @Override - public void setNClob(int parameterIndex, Reader x, long length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.NCLOB, x, length)); - - } - - @Override - public void setNString(int parameterIndex, String x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.NVARCHAR, x)); - - } - - @Override - public void setNull(int parameterIndex, int sqlType) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(sqlType, null)); - - } - - @Override - public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(sqlType, null)); - - } - - @Override - public void setObject(int parameterIndex, Object x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.OTHER, null)); - - } - - @Override - public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(targetSqlType, x)); - - } - - @Override - public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(targetSqlType, x, -1, null, scaleOrLength)); - } - - @Override - public void setRef(int parameterIndex, Ref x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.REF, x)); - - } - - @Override - public void setRowId(int parameterIndex, RowId x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.ROWID, x)); - - } - - @Override - public void setSQLXML(int parameterIndex, SQLXML x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.SQLXML, x)); - - } - - @Override - public void setShort(int parameterIndex, short x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.SMALLINT, x)); - - } - - @Override - public void setString(int parameterIndex, String x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.VARCHAR, x)); - - } - - @Override - public void setTime(int parameterIndex, Time x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.TIME, x)); - - } - - @Override - public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.TIME, x, cal)); - - } - - @Override - public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.TIMESTAMP, x)); - - } - - @Override - public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(Types.TIMESTAMP, x)); - - } - - @Override - public void setURL(int parameterIndex, URL x) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(TYPE.URL, x)); - - } - - @Override - public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { - setParameter(parameterIndex, new JdbcParameter(TYPE.UnicodeStream, x, length)); - - } - - public void setParameter(int parameterIndex, JdbcParameter parameter) { - this.getParameters().put(parameterIndex, parameter); - } - - public Map getParameters() { - return parameters; - } - - @Override - public void clearParameters() throws SQLException { - this.parameters.clear(); - } - - @Override - public ResultSetMetaData getMetaData() throws SQLException { - if (pstmt != null) { - return pstmt.getMetaData(); - } - return null; - } - - @Override - public ParameterMetaData getParameterMetaData() throws SQLException { - if (pstmt != null) { - return pstmt.getParameterMetaData(); - } - return null; - } - - @Override - public ResultSet executeQuery() throws SQLException { - List values = getParameterValues(); - - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(values); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - checkParameterValues(values, resultSqlStmtList); - - if (resultSqlStmtList.size() > 1) { - throw new SQLException("executeQuery not support multi-statement"); - } - - SQLStatement resultSqlStmt = resultSqlStmtList.get(0); - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - checkPreparedStatement(resultSqlStmt, resultSql, database); - - return pstmt.executeQuery(); - } - - @SuppressWarnings("unchecked") - private void checkPreparedStatement(SQLStatement resultSqlStmt, String resultSql, String database) - throws SQLException { - if (conn.getRealConnection() != null // - && StringUtils.equals(database, conn.getDatabase()) // - ) { - if (pstmt == null) { - pstmt = createRealPreparedStatement(conn.getRealConnection(), resultSql); - tables = (List) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_TABLES); - } else if (tables != null // - && tables.equals(resultSqlStmt.getAttribute(ShardingVisitor.ATTR_TABLES))) { - if (pstmt != null) { - JdbcUtils.close(pstmt); - pstmt = null; - tables = null; - } - pstmt = createRealPreparedStatement(conn.getRealConnection(), resultSql); - tables = (List) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_TABLES); - } - } else { - conn.closeRealConnection(); - if (pstmt != null) { - JdbcUtils.close(pstmt); - pstmt = null; - tables = null; - } - conn.createRealConnectionByDb(database); - pstmt = createRealPreparedStatement(conn.getRealConnection(), resultSql); - } - } - - @Override - public int executeUpdate() throws SQLException { - List values = getParameterValues(); - - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(values); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - checkParameterValues(values, resultSqlStmtList); - - int updateCount = 0; - - for (SQLStatement resultSqlStmt : resultSqlStmtList) { - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - checkPreparedStatement(resultSqlStmt, resultSql, database); - updateCount += pstmt.executeUpdate(); - } - - return updateCount; - } - - private void checkParameterValues(List values, List resultSqlStmtList) throws SQLException { - if (resultSqlStmtList.size() > 1) { - for (Object value : values) { - if (value instanceof InputStream // - || value instanceof Reader // - || value instanceof Blob // - || value instanceof Clob // - || value instanceof NClob // - ) { - throw new SQLException(value.getClass().getName() + " not support multi-statement"); - } - } - } - } - - private List getParameterValues() { - List values = new ArrayList(); - for (Map.Entry entry : this.parameters.entrySet()) { - values.add(entry.getValue().getValue()); - } - return values; - } - - @Override - public boolean execute() throws SQLException { - List values = getParameterValues(); - - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(values); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - checkParameterValues(values, resultSqlStmtList); - - boolean isSelelct = sqlStmt instanceof SQLSelectStatement; - - if (resultSqlStmtList.size() > 1 && isSelelct) { - throw new SQLException("select not support multi-statement"); - } - - for (SQLStatement resultSqlStmt : resultSqlStmtList) { - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - checkPreparedStatement(resultSqlStmt, resultSql, database); - pstmt.execute(); - - if (!isSelelct) { - if (updateCount == null) { - updateCount = pstmt.getUpdateCount(); - } else { - updateCount = updateCount.intValue() + pstmt.getUpdateCount(); - } - } - } - - return isSelelct; - } - - @Override - public void addBatch() throws SQLException { - throw new UnsupportedOperationException(); - } - - @SuppressWarnings("deprecation") - private PreparedStatement createRealPreparedStatement(Connection conn, String sql) throws SQLException { - PreparedStatement stmt; - if (resultSetType != null && resultSetConcurrency != null && resultSetHoldability != null) { - stmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); - } else if (resultSetType != null && resultSetConcurrency != null) { - stmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency); - } else { - stmt = conn.prepareStatement(sql); - } - - initStatement(stmt); - - for (Map.Entry entry : this.parameters.entrySet()) { - int index = entry.getKey(); - JdbcParameter param = entry.getValue(); - Object value = param.getValue(); - - if (param.getScaleOrLength() != -1) { - stmt.setObject(index, value, param.getSqlType(), param.getScaleOrLength()); - } else if (value == null) { - stmt.setNull(index, param.getSqlType()); - } else { - switch (param.getSqlType()) { - case Types.ARRAY: - stmt.setArray(index, (Array) value); - break; - case TYPE.AsciiInputStream: - if (param.getLength() >= 0) { - stmt.setAsciiStream(index, (InputStream) value, param.getLength()); - } else { - stmt.setAsciiStream(index, (InputStream) value); - } - break; - case Types.DECIMAL: - stmt.setBigDecimal(index, (BigDecimal) value); - break; - case TYPE.BinaryInputStream: - if (param.getLength() >= 0) { - stmt.setBinaryStream(index, (InputStream) value, param.getLength()); - } else { - stmt.setBinaryStream(index, (InputStream) value); - } - break; - case Types.BLOB: - if (value instanceof Blob) { - stmt.setBlob(index, (Blob) value); - } else { - if (param.getLength() >= 0) { - stmt.setBlob(index, (InputStream) value, param.getLength()); - } else { - stmt.setBlob(index, (InputStream) value); - } - } - break; - case Types.BOOLEAN: - stmt.setBoolean(index, (Boolean) value); - break; - case Types.TINYINT: - stmt.setByte(index, (Byte) value); - break; - case TYPE.BYTES: - stmt.setBytes(index, (byte[]) value); - break; - case TYPE.CharacterInputStream: - if (param.getLength() >= 0) { - stmt.setCharacterStream(index, (Reader) value, param.getLength()); - } else { - stmt.setCharacterStream(index, (Reader) value); - } - break; - case Types.CLOB: - if (value instanceof Clob) { - stmt.setClob(index, (Clob) value); - } else { - if (param.getLength() >= 0) { - stmt.setClob(index, (Reader) value, param.getLength()); - } else { - stmt.setClob(index, (Reader) value); - } - } - break; - case Types.DATE: - if (param.getCalendar() != null) { - stmt.setDate(index, (Date) value, param.getCalendar()); - } else { - stmt.setDate(index, (Date) value); - } - break; - case Types.DOUBLE: - stmt.setDouble(index, (Double) value); - break; - case Types.FLOAT: - stmt.setFloat(index, (Float) value); - break; - case Types.INTEGER: - stmt.setInt(index, (Integer) value); - break; - case Types.BIGINT: - stmt.setLong(index, (Long) value); - break; - case TYPE.NCharacterInputStream: - if (param.getLength() >= 0) { - stmt.setNCharacterStream(index, (Reader) value, param.getLength()); - } else { - stmt.setNCharacterStream(index, (Reader) value); - } - break; - case Types.NCLOB: - if (value instanceof Clob) { - stmt.setNClob(index, (NClob) value); - } else { - if (param.getLength() >= 0) { - stmt.setNClob(index, (Reader) value, param.getLength()); - } else { - stmt.setNClob(index, (Reader) value); - } - } - break; - case Types.NCHAR: - case Types.NVARCHAR: - stmt.setNString(index, (String) value); - break; - case Types.OTHER: - stmt.setObject(index, value); - break; - case Types.REF: - stmt.setRef(index, (Ref) value); - break; - case Types.ROWID: - stmt.setRowId(index, (RowId) value); - break; - case Types.SQLXML: - stmt.setSQLXML(index, (SQLXML) value); - break; - case Types.SMALLINT: - stmt.setShort(index, (Short) value); - break; - case Types.VARCHAR: - case Types.CHAR: - stmt.setString(index, (String) value); - break; - case Types.TIME: - if (param.getCalendar() != null) { - stmt.setTime(index, (Time) value, param.getCalendar()); - } else { - stmt.setTime(index, (Time) value); - } - break; - case Types.TIMESTAMP: - if (param.getCalendar() != null) { - stmt.setTimestamp(index, (Timestamp) value, param.getCalendar()); - } else { - stmt.setTimestamp(index, (Timestamp) value); - } - break; - case TYPE.URL: - stmt.setURL(index, (URL) value); - break; - case TYPE.UnicodeStream: - stmt.setUnicodeStream(index, (InputStream) value, (int) param.getLength()); - break; - default: - stmt.setObject(index, value); - break; - } - } - } - - return stmt; - } - -} diff --git a/src/main/java/com/alibaba/druid/sharding/ShardingRuntimeException.java b/src/main/java/com/alibaba/druid/sharding/ShardingRuntimeException.java deleted file mode 100644 index eeb3998549..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/ShardingRuntimeException.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding; - -import com.alibaba.druid.DruidRuntimeException; - - -public class ShardingRuntimeException extends DruidRuntimeException { - - private static final long serialVersionUID = 1L; - - public ShardingRuntimeException(){ - super(); - } - - public ShardingRuntimeException(String message, Throwable cause){ - super(message, cause); - } - - public ShardingRuntimeException(String message){ - super(message); - } - - public ShardingRuntimeException(Throwable cause){ - super(cause); - } -} diff --git a/src/main/java/com/alibaba/druid/sharding/ShardingStatement.java b/src/main/java/com/alibaba/druid/sharding/ShardingStatement.java deleted file mode 100644 index 1bdc96e809..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/ShardingStatement.java +++ /dev/null @@ -1,813 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding; - -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.sql.SQLWarning; -import java.sql.Statement; -import java.util.List; - -import org.h2.util.StringUtils; - -import com.alibaba.druid.pool.WrapperAdapter; -import com.alibaba.druid.sharding.sql.ShardingVisitor; -import com.alibaba.druid.sql.SQLUtils; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; -import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; -import com.alibaba.druid.util.JdbcUtils; - -public class ShardingStatement extends WrapperAdapter implements Statement { - - protected final ShardingConnection conn; - private final long id; - protected Integer resultSetType; - protected Integer resultSetConcurrency; - protected Integer resultSetHoldability; - - private Statement stmt; - protected Integer updateCount; - - private Integer maxFieldSize; - private Integer maxRows; - private Boolean escapeProcessing; - private Integer queryTimeout; - private String cursorName; - private Integer fetchDirection; - private Integer fetchSize; - - protected void initStatement(Statement stmt) throws SQLException { - if (maxFieldSize != null) { - stmt.setMaxFieldSize(maxFieldSize); - } - if (maxRows != null) { - stmt.setMaxRows(maxRows); - } - if (escapeProcessing != null) { - stmt.setEscapeProcessing(escapeProcessing); - } - if (queryTimeout != null) { - stmt.setQueryTimeout(queryTimeout); - } - if (cursorName != null) { - stmt.setCursorName(cursorName); - } - if (fetchDirection != null) { - stmt.setFetchDirection(fetchDirection); - } - if (fetchSize != null) { - stmt.setFetchSize(fetchSize); - } - } - - private Statement createRealStatement(Connection conn) throws SQLException { - Statement stmt; - if (resultSetType != null && resultSetConcurrency != null && resultSetHoldability != null) { - stmt = conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); - } else if (resultSetType != null && resultSetConcurrency != null) { - stmt = conn.createStatement(resultSetType, resultSetConcurrency); - } else { - stmt = conn.createStatement(); - } - - initStatement(stmt); - - return stmt; - } - - public ShardingStatement(ShardingConnection conn, long id){ - this.conn = conn; - this.id = id; - } - - public ShardingStatement(ShardingConnection conn, long id, int resultSetType, int resultSetConcurrency){ - this.conn = conn; - this.id = id; - this.resultSetType = resultSetType; - this.resultSetConcurrency = resultSetConcurrency; - } - - public ShardingStatement(ShardingConnection conn, long id, int resultSetType, int resultSetConcurrency, - int resultSetHoldability){ - this.conn = conn; - this.id = id; - this.resultSetType = resultSetType; - this.resultSetConcurrency = resultSetConcurrency; - this.resultSetHoldability = resultSetHoldability; - } - - public long getId() { - return id; - } - - @Override - public ResultSet executeQuery(String sql) throws SQLException { - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - if (resultSqlStmtList.size() > 1) { - throw new SQLException("executeQuery not support multi-statement"); - } - - SQLStatement resultSqlStmt = resultSqlStmtList.get(0); - - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - if (conn.getRealConnection() != null && StringUtils.equals(database, conn.getDatabase())) { - if (stmt == null) { - stmt = createRealStatement(conn.getRealConnection()); - } - } else { - conn.closeRealConnection(); - if (stmt != null) { - JdbcUtils.close(stmt); - stmt = null; - } - conn.createRealConnectionByDb(database); - stmt = createRealStatement(conn.getRealConnection()); - } - return stmt.executeQuery(resultSql); - } - - @Override - public void close() throws SQLException { - if (stmt != null) { - stmt.close(); - } - } - - @Override - public int getMaxFieldSize() throws SQLException { - if (stmt != null) { - return stmt.getMaxFieldSize(); - } - - if (maxFieldSize == null) { - return 0; - } - - return maxFieldSize; - } - - @Override - public void setMaxFieldSize(int max) throws SQLException { - if (stmt != null) { - stmt.setMaxFieldSize(max); - return; - } - - this.maxFieldSize = max; - } - - @Override - public int getMaxRows() throws SQLException { - if (stmt != null) { - return stmt.getMaxRows(); - } - - if (maxRows == null) { - return 0; - } - - return maxRows; - } - - @Override - public void setMaxRows(int max) throws SQLException { - if (stmt != null) { - stmt.setMaxRows(max); - return; - } - - this.maxRows = max; - } - - @Override - public void setEscapeProcessing(boolean enable) throws SQLException { - if (stmt != null) { - stmt.setEscapeProcessing(enable); - return; - } - - this.escapeProcessing = enable; - } - - @Override - public int getQueryTimeout() throws SQLException { - if (stmt != null) { - return stmt.getQueryTimeout(); - } - - if (queryTimeout == null) { - return 0; - } - - return queryTimeout; - } - - @Override - public void setQueryTimeout(int seconds) throws SQLException { - if (stmt != null) { - stmt.setQueryTimeout(seconds); - return; - } - - this.queryTimeout = seconds; - } - - @Override - public void cancel() throws SQLException { - if (stmt != null) { - stmt.cancel(); - } - } - - @Override - public SQLWarning getWarnings() throws SQLException { - if (stmt != null) { - return stmt.getWarnings(); - } - - return null; - } - - @Override - public void clearWarnings() throws SQLException { - if (stmt != null) { - stmt.clearWarnings(); - } - } - - @Override - public void setCursorName(String name) throws SQLException { - if (stmt != null) { - stmt.setCursorName(name); - return; - } - - this.cursorName = name; - } - - @Override - public boolean execute(String sql) throws SQLException { - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - boolean isSelelct = sqlStmt instanceof SQLSelectStatement; - - if (resultSqlStmtList.size() > 1 && isSelelct) { - throw new SQLException("select not support multi-statement"); - } - - for (SQLStatement resultSqlStmt : resultSqlStmtList) { - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - if (conn.getRealConnection() != null && StringUtils.equals(database, conn.getDatabase())) { - if (stmt == null) { - stmt = createRealStatement(conn.getRealConnection()); - } - } else { - conn.closeRealConnection(); - if (stmt != null) { - JdbcUtils.close(stmt); - stmt = null; - } - conn.createRealConnectionByDb(database); - stmt = createRealStatement(conn.getRealConnection()); - } - stmt.execute(resultSql); - - if (!isSelelct) { - if (updateCount == null) { - updateCount = stmt.getUpdateCount(); - } else { - updateCount = updateCount.intValue() + stmt.getUpdateCount(); - } - } - } - - return isSelelct; - } - - @Override - public ResultSet getResultSet() throws SQLException { - if (stmt != null) { - return stmt.getResultSet(); - } - - return null; - } - - @Override - public int getUpdateCount() throws SQLException { - if (updateCount != null) { - return updateCount.intValue(); - } - - return 0; - } - - @Override - public boolean getMoreResults() throws SQLException { - if (stmt != null) { - return stmt.getMoreResults(); - } - - return false; - } - - @Override - public void setFetchDirection(int direction) throws SQLException { - if (stmt != null) { - stmt.setFetchDirection(direction); - return; - } - - this.fetchDirection = direction; - } - - @Override - public int getFetchDirection() throws SQLException { - if (stmt != null) { - return stmt.getFetchDirection(); - } - - return this.fetchDirection; - } - - @Override - public void setFetchSize(int rows) throws SQLException { - if (stmt != null) { - stmt.setFetchSize(rows); - return; - } - - this.fetchSize = rows; - } - - @Override - public int getFetchSize() throws SQLException { - if (stmt != null) { - return stmt.getFetchSize(); - } - - return this.fetchSize; - } - - @Override - public int getResultSetConcurrency() throws SQLException { - if (stmt != null) { - return stmt.getResultSetConcurrency(); - } - - if (resultSetConcurrency != null) { - return resultSetConcurrency; - } - - return 0; - } - - @Override - public int getResultSetType() throws SQLException { - if (stmt != null) { - return stmt.getResultSetType(); - } - - return resultSetType; - } - - @Override - public void addBatch(String sql) throws SQLException { - throw new UnsupportedOperationException(); - } - - @Override - public void clearBatch() throws SQLException { - if (stmt != null) { - stmt.clearBatch(); - } - } - - @Override - public int[] executeBatch() throws SQLException { - if (stmt != null) { - return stmt.executeBatch(); - } - return new int[0]; - } - - @Override - public ShardingConnection getConnection() throws SQLException { - return conn; - } - - @Override - public boolean getMoreResults(int current) throws SQLException { - if (stmt != null) { - return stmt.getMoreResults(current); - } - - return false; - } - - @Override - public ResultSet getGeneratedKeys() throws SQLException { - if (stmt != null) { - return stmt.getGeneratedKeys(); - } - - return null; - } - - @Override - public int executeUpdate(String sql) throws SQLException { - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - int updateCount = 0; - - for (SQLStatement resultSqlStmt : resultSqlStmtList) { - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - if (conn.getRealConnection() != null && StringUtils.equals(database, conn.getDatabase())) { - if (stmt == null) { - stmt = createRealStatement(conn.getRealConnection()); - } - } else { - conn.closeRealConnection(); - if (stmt != null) { - JdbcUtils.close(stmt); - stmt = null; - } - conn.createRealConnectionByDb(database); - stmt = createRealStatement(conn.getRealConnection()); - } - updateCount += stmt.executeUpdate(resultSql); - } - - return updateCount; - } - - @Override - public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - if (autoGeneratedKeys == Statement.RETURN_GENERATED_KEYS && resultSqlStmtList.size() > 1 - && sqlStmt instanceof SQLInsertStatement) { - throw new SQLException("RETURN_GENERATED_KEYS not support multi-statement"); - } - - int updateCount = 0; - - for (SQLStatement resultSqlStmt : resultSqlStmtList) { - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - if (conn.getRealConnection() != null && StringUtils.equals(database, conn.getDatabase())) { - if (stmt == null) { - stmt = createRealStatement(conn.getRealConnection()); - } - } else { - conn.closeRealConnection(); - if (stmt != null) { - JdbcUtils.close(stmt); - stmt = null; - } - conn.createRealConnectionByDb(database); - stmt = createRealStatement(conn.getRealConnection()); - } - updateCount += stmt.executeUpdate(resultSql, autoGeneratedKeys); - } - - return updateCount; - } - - @Override - public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - int updateCount = 0; - - for (SQLStatement resultSqlStmt : resultSqlStmtList) { - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - if (conn.getRealConnection() != null && StringUtils.equals(database, conn.getDatabase())) { - if (stmt == null) { - stmt = createRealStatement(conn.getRealConnection()); - } - } else { - conn.closeRealConnection(); - if (stmt != null) { - JdbcUtils.close(stmt); - stmt = null; - } - conn.createRealConnectionByDb(database); - stmt = createRealStatement(conn.getRealConnection()); - } - updateCount += stmt.executeUpdate(resultSql, columnIndexes); - } - - return updateCount; - } - - @Override - public int executeUpdate(String sql, String[] columnNames) throws SQLException { - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - int updateCount = 0; - - for (SQLStatement resultSqlStmt : resultSqlStmtList) { - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - if (conn.getRealConnection() != null && StringUtils.equals(database, conn.getDatabase())) { - if (stmt == null) { - stmt = createRealStatement(conn.getRealConnection()); - } - } else { - conn.closeRealConnection(); - if (stmt != null) { - JdbcUtils.close(stmt); - stmt = null; - } - conn.createRealConnectionByDb(database); - stmt = createRealStatement(conn.getRealConnection()); - } - updateCount += stmt.executeUpdate(resultSql, columnNames); - } - - return updateCount; - } - - @Override - public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - boolean isSelelct = sqlStmt instanceof SQLSelectStatement; - - if (resultSqlStmtList.size() > 1) { - if (autoGeneratedKeys == Statement.RETURN_GENERATED_KEYS && sqlStmt instanceof SQLInsertStatement) { - throw new SQLException("RETURN_GENERATED_KEYS not support multi-statement"); - } else if (isSelelct) { - throw new SQLException("select not support multi-statement"); - } - } - - for (SQLStatement resultSqlStmt : resultSqlStmtList) { - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - if (conn.getRealConnection() != null && StringUtils.equals(database, conn.getDatabase())) { - if (stmt == null) { - stmt = createRealStatement(conn.getRealConnection()); - } - } else { - conn.closeRealConnection(); - if (stmt != null) { - JdbcUtils.close(stmt); - stmt = null; - } - conn.createRealConnectionByDb(database); - stmt = createRealStatement(conn.getRealConnection()); - } - stmt.execute(resultSql, autoGeneratedKeys); - - if (!isSelelct) { - if (updateCount == null) { - updateCount = stmt.getUpdateCount(); - } else { - updateCount = updateCount.intValue() + stmt.getUpdateCount(); - } - } - } - - return isSelelct; - } - - @Override - public boolean execute(String sql, int[] columnIndexes) throws SQLException { - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - boolean isSelelct = sqlStmt instanceof SQLSelectStatement; - - if (resultSqlStmtList.size() > 1 && isSelelct) { - throw new SQLException("select not support multi-statement"); - } - - for (SQLStatement resultSqlStmt : resultSqlStmtList) { - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - if (conn.getRealConnection() != null && StringUtils.equals(database, conn.getDatabase())) { - if (stmt == null) { - stmt = createRealStatement(conn.getRealConnection()); - } - } else { - conn.closeRealConnection(); - if (stmt != null) { - JdbcUtils.close(stmt); - stmt = null; - } - conn.createRealConnectionByDb(database); - stmt = createRealStatement(conn.getRealConnection()); - } - stmt.execute(resultSql, columnIndexes); - - if (!isSelelct) { - if (updateCount == null) { - updateCount = stmt.getUpdateCount(); - } else { - updateCount = updateCount.intValue() + stmt.getUpdateCount(); - } - } - } - - return isSelelct; - } - - @Override - public boolean execute(String sql, String[] columnNames) throws SQLException { - ShardingDataSource dataSource = conn.getDataSource(); - ShardingVisitor visitor = dataSource.createShardingVisitor(); - SQLStatement sqlStmt = dataSource.parseStatement(sql); - - sqlStmt.accept(visitor); - - List resultSqlStmtList = visitor.getResult(); - - boolean isSelelct = sqlStmt instanceof SQLSelectStatement; - - if (resultSqlStmtList.size() > 1 && isSelelct) { - throw new SQLException("select not support multi-statement"); - } - - for (SQLStatement resultSqlStmt : resultSqlStmtList) { - String resultSql = SQLUtils.toSQLString(resultSqlStmt, dataSource.getDbType()); - - String database = (String) resultSqlStmt.getAttribute(ShardingVisitor.ATTR_DB); - - if (conn.getRealConnection() != null && StringUtils.equals(database, conn.getDatabase())) { - if (stmt == null) { - stmt = createRealStatement(conn.getRealConnection()); - } - } else { - conn.closeRealConnection(); - if (stmt != null) { - JdbcUtils.close(stmt); - stmt = null; - } - conn.createRealConnectionByDb(database); - stmt = createRealStatement(conn.getRealConnection()); - } - stmt.execute(resultSql, columnNames); - - if (!isSelelct) { - if (updateCount == null) { - updateCount = stmt.getUpdateCount(); - } else { - updateCount = updateCount.intValue() + stmt.getUpdateCount(); - } - } - } - - return isSelelct; - } - - @Override - public int getResultSetHoldability() throws SQLException { - if (stmt != null) { - return stmt.getResultSetHoldability(); - } - - if (resultSetHoldability != null) { - return resultSetHoldability; - } - - return 0; - } - - @Override - public boolean isClosed() throws SQLException { - if (stmt != null) { - return stmt.isClosed(); - } - return false; - } - - @Override - public void setPoolable(boolean poolable) throws SQLException { - throw new SQLException("not supported"); - } - - @Override - public boolean isPoolable() throws SQLException { - return false; - } - - @SuppressWarnings("unchecked") - @Override - public T unwrap(Class iface) throws SQLException { - if (iface == null) { - return null; - } - - if (iface.isInstance(this)) { - return (T) this; - } - - if (stmt != null) { - return stmt.unwrap(iface); - } - - return null; - } - - public Boolean isEscapeProcessing() { - return escapeProcessing; - } - - public String getCursorName() { - return cursorName; - } - - public void closeOnCompletion() throws SQLException { - throw new SQLFeatureNotSupportedException(); - } - - public boolean isCloseOnCompletion() throws SQLException { - throw new SQLFeatureNotSupportedException(); - } -} diff --git a/src/main/java/com/alibaba/druid/sharding/config/LogicTable.java b/src/main/java/com/alibaba/druid/sharding/config/LogicTable.java deleted file mode 100644 index 3b56d26ce7..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/config/LogicTable.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding.config; - -import java.util.HashMap; -import java.util.Map; - -public class LogicTable { - - private String logicTable; - private Map partitions = new HashMap(); - - public String getLogicTable() { - return logicTable; - } - - public void setLogicTable(String logicTable) { - this.logicTable = logicTable; - } - - public Map getPartitions() { - return partitions; - } - - public void setPartitions(Map partitions) { - this.partitions = partitions; - } - - public void addPartition(String partitionName, String physicalTableName) { - this.partitions.put(partitionName, new TablePartition(partitionName, physicalTableName)); - } - - public void addPartition(String partitionName, String database, String physicalTableName) { - this.partitions.put(partitionName, new TablePartition(partitionName, database, physicalTableName)); - } -} diff --git a/src/main/java/com/alibaba/druid/sharding/config/MappingRule.java b/src/main/java/com/alibaba/druid/sharding/config/MappingRule.java deleted file mode 100644 index 4feeac4030..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/config/MappingRule.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding.config; - -import java.util.List; - -public interface MappingRule { - - String getTable(); - - String getColumn(); - - List getEntries(); - - String getPartition(Object value); -} diff --git a/src/main/java/com/alibaba/druid/sharding/config/MappingRuleAdapter.java b/src/main/java/com/alibaba/druid/sharding/config/MappingRuleAdapter.java deleted file mode 100644 index 9e17ce24f3..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/config/MappingRuleAdapter.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding.config; - -import java.util.ArrayList; -import java.util.List; - -public class MappingRuleAdapter implements MappingRule { - - private String defaultPartition; - - private String table; - private String column; - - private List entries = new ArrayList(); - - public List getEntries() { - return entries; - } - - public void setEntries(List entries) { - this.entries = entries; - } - - public String getPartition(Object value) { - for (MappingRuleEntry entry : this.entries) { - if (entry.match(value)) { - return entry.getPartition(); - } - } - - return this.getDefaultPartition(); - } - - public String getTable() { - return table; - } - - public void setTable(String table) { - this.table = table; - } - - public String getColumn() { - return column; - } - - public void setColumn(String column) { - this.column = column; - } - - public String getDefaultPartition() { - return defaultPartition; - } - - public void setDefaultPartition(String defaultPartition) { - this.defaultPartition = defaultPartition; - } -} diff --git a/src/main/java/com/alibaba/druid/sharding/config/MappingRuleEntry.java b/src/main/java/com/alibaba/druid/sharding/config/MappingRuleEntry.java deleted file mode 100644 index e2d530bc50..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/config/MappingRuleEntry.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding.config; - -public interface MappingRuleEntry { - - boolean match(Object value); - - String getPartition(); -} diff --git a/src/main/java/com/alibaba/druid/sharding/config/MappingRuleListEntry.java b/src/main/java/com/alibaba/druid/sharding/config/MappingRuleListEntry.java deleted file mode 100644 index 780be48d3a..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/config/MappingRuleListEntry.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding.config; - -import java.util.ArrayList; -import java.util.List; - -import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils; - - -public class MappingRuleListEntry implements MappingRuleEntry { - private List values = new ArrayList(); - private String partition; - - public MappingRuleListEntry() { - - } - - public MappingRuleListEntry(String partition, Object...values) { - this.partition = partition; - for (Object value : values) { - this.values.add(value); - } - } - - public List getValues() { - return values; - } - - public void setValues(List values) { - this.values = values; - } - - public String getPartition() { - return partition; - } - - public void setPartition(String partition) { - this.partition = partition; - } - - @Override - public boolean match(Object value) { - for (Object item : this.values) { - if (SQLEvalVisitorUtils.eq(item, value)) { - return true; - } - } - return false; - } -} diff --git a/src/main/java/com/alibaba/druid/sharding/config/MappingRuleLtEntry.java b/src/main/java/com/alibaba/druid/sharding/config/MappingRuleLtEntry.java deleted file mode 100644 index 4f9dd4f8ca..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/config/MappingRuleLtEntry.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding.config; - -import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils; - -public class MappingRuleLtEntry implements MappingRuleEntry { - - private Object value; - private String partition; - - public Object getValue() { - return value; - } - - public void setValue(Object value) { - this.value = value; - } - - public String getPartition() { - return partition; - } - - public void setPartition(String partition) { - this.partition = partition; - } - - @Override - public boolean match(Object value) { - return SQLEvalVisitorUtils.eq(this.value, value); - } -} diff --git a/src/main/java/com/alibaba/druid/sharding/config/RouteConfig.java b/src/main/java/com/alibaba/druid/sharding/config/RouteConfig.java deleted file mode 100644 index 380e7f747e..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/config/RouteConfig.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding.config; - -import java.util.HashMap; -import java.util.Map; - -public class RouteConfig { - - private String defaultDatabase; - private Map logicTables = new HashMap(); - private Map mappingRules = new HashMap(); - - public String getDefaultDatabase() { - return defaultDatabase; - } - - public void setDefaultDatabase(String defaultDatabase) { - this.defaultDatabase = defaultDatabase; - } - - public Map getLogicTables() { - return logicTables; - } - - public void setLogicTables(Map logicTables) { - this.logicTables = logicTables; - } - - public Map getMappingRules() { - return mappingRules; - } - - public void setMappingRules(Map mappingRules) { - this.mappingRules = mappingRules; - } - - public MappingRule getMappingRule(String table) { - if (table == null) { - return null; - } - - String lowerTable = table.toLowerCase(); - - return mappingRules.get(lowerTable); - } - - public TablePartition getPartition(String table, String partitionName) { - if (table == null) { - throw new IllegalArgumentException("table is null"); - } - if (partitionName == null) { - throw new IllegalArgumentException("partitionName is null"); - } - - String lowerTable = table.toLowerCase(); - LogicTable config = logicTables.get(lowerTable); - if (config == null) { - throw new IllegalArgumentException("table's partition config not exists, tableName : " + table); - } - - String lowerPartitionName = partitionName.toLowerCase(); - TablePartition tablePartition = config.getPartitions().get(lowerPartitionName); - - if (tablePartition == null) { - throw new IllegalArgumentException("table's partition config not exists, tableName : " + table - + ", partition : " + lowerPartitionName); - } - - return tablePartition; - } -} diff --git a/src/main/java/com/alibaba/druid/sharding/config/TablePartition.java b/src/main/java/com/alibaba/druid/sharding/config/TablePartition.java deleted file mode 100644 index 2a64f0110d..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/config/TablePartition.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding.config; - -public class TablePartition { - - private String name; - private String database; - private String table; - - public TablePartition(){ - } - - public TablePartition(String name, String table){ - this.name = name; - this.table = table; - } - - public TablePartition(String name, String database, String table){ - this.name = name; - this.database = database; - this.table = table; - } - - public String getDatabase() { - return database; - } - - public void setDatabase(String database) { - this.database = database; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getTable() { - return table; - } - - public void setTable(String table) { - this.table = table; - } - -} diff --git a/src/main/java/com/alibaba/druid/sharding/sql/MySqlShardingVisitor.java b/src/main/java/com/alibaba/druid/sharding/sql/MySqlShardingVisitor.java deleted file mode 100644 index b657f08487..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/sql/MySqlShardingVisitor.java +++ /dev/null @@ -1,404 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding.sql; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import com.alibaba.druid.sharding.ShardingRuntimeException; -import com.alibaba.druid.sharding.config.MappingRule; -import com.alibaba.druid.sharding.config.RouteConfig; -import com.alibaba.druid.sharding.config.TablePartition; -import com.alibaba.druid.sql.ast.SQLExpr; -import com.alibaba.druid.sql.ast.SQLObject; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; -import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; -import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr; -import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; -import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr; -import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; -import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; -import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; -import com.alibaba.druid.sql.ast.statement.SQLTableSource; -import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement; -import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter; -import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils; -import com.alibaba.druid.util.JdbcConstants; - -public class MySqlShardingVisitor extends MySqlASTVisitorAdapter implements ShardingVisitor { - - private final RouteConfig routeConfig; - - private final List parameters; - - private List result = new ArrayList(2); - - private SQLStatement input = null; - - public MySqlShardingVisitor(RouteConfig routeConfig, List parameters){ - this.routeConfig = routeConfig; - this.parameters = parameters; - } - - public MySqlShardingVisitor(RouteConfig routeConfig, Object... parameters){ - this(routeConfig, Arrays.asList(parameters)); - } - - public List getResult() { - return result; - } - - public RouteConfig getRouteConfig() { - return routeConfig; - } - - public List getParameters() { - return parameters; - } - - public boolean visit(SQLSelectStatement x) { - input = x; - return true; - } - - public boolean visit(MySqlDeleteStatement x) { - input = x; - - if (x.getFrom() != null) { - x.getFrom().setParent(x); - x.getFrom().accept(this); - } - - if (x.getTableSource() != null) { - x.getTableSource().setParent(x); - x.getTableSource().accept(this); - } - - if (x.getWhere() != null) { - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - result.add(x); - - return false; - } - - @Override - public boolean visit(MySqlUpdateStatement x) { - input = x; - - if (x.getTableSource() != null) { - x.getTableSource().setParent(x); - x.getTableSource().accept(this); - } - - if (x.getWhere() != null) { - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - result.add(x); - return false; - } - - public boolean visit(MySqlInsertStatement x) { - input = x; - String table = x.getTableName().getSimleName(); - - MappingRule mappingRule = routeConfig.getMappingRule(table); - - if (mappingRule == null) { - result.add(x); - return false; - } - - if (x.getValues() == null) { - throw new ShardingRuntimeException("sharding rule violation, insert's values clause is null"); - } - - String column = mappingRule.getColumn(); - - int columnIndex = -1; - for (int i = 0; i < x.getColumns().size(); ++i) { - SQLExpr columnExpr = x.getColumns().get(i); - if (columnExpr instanceof SQLIdentifierExpr) { - String columnName = ((SQLIdentifierExpr) columnExpr).getName(); - if (column.equalsIgnoreCase(columnName)) { - columnIndex = i; - break; - } - } - } - - if (columnIndex == -1) { - throw new ShardingRuntimeException("sharding rule violation, columns not set : " + column); - } - - SQLExpr valueExpr = x.getValues().getValues().get(columnIndex); - Object value = SQLEvalVisitorUtils.eval(null, valueExpr, parameters); - - String partition = mappingRule.getPartition(value); - - if (partition == null) { - throw new ShardingRuntimeException("sharding rule violation, partition not match, value : " + value); - } - - TablePartition tablePartition = routeConfig.getPartition(table, partition); - - x.setTableName(new SQLIdentifierExpr(tablePartition.getTable())); - - if (tablePartition.getDatabase() != null) { - x.putAttribute(ATTR_DB, tablePartition.getDatabase()); - } - - result.add(x); - - return false; - } - - @Override - public boolean visit(MySqlSelectQueryBlock x) { - if (x.getFrom() != null) { - x.getFrom().setParent(x); - x.getFrom().accept(this); - } - - if (x.getWhere() != null) { - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public boolean visit(SQLJoinTableSource x) { - x.getLeft().setParent(x); - x.getRight().setParent(x); - - if (x.getCondition() != null) { - x.getCondition().setParent(x); - } - - return true; - } - - @Override - public boolean visit(SQLExprTableSource x) { - Map aliasMap = getAliasMap(x); - - if (aliasMap != null) { - if (x.getAlias() != null) { - aliasMap.put(x.getAlias(), x); - } - - if (x.getExpr() instanceof SQLIdentifierExpr) { - String tableName = ((SQLIdentifierExpr) x.getExpr()).getName(); - aliasMap.put(tableName, x); - } - } - - return false; - } - - @Override - public boolean visit(SQLBinaryOpExpr x) { - x.getLeft().setParent(x); - x.getRight().setParent(x); - - x.getLeft().accept(this); - x.getRight().accept(this); - - String column = null; - if ((column = getColumn(x.getLeft())) != null && isValue(x.getRight())) { - boolean isMappingColumn = false; - SQLTableSource tableSource = getBinaryOpExprLeftOrRightTableSource(x.getLeft()); - MappingRule mappingRule = getMappingRule(tableSource); - if (mappingRule != null) { - if (mappingRule.getColumn().equalsIgnoreCase(column)) { - isMappingColumn = true; - } - } - if (isMappingColumn) { - Object value = SQLEvalVisitorUtils.eval(JdbcConstants.MYSQL, x.getRight(), parameters); - String partitionName = null; - switch (x.getOperator()) { - case Equality: - partitionName = mappingRule.getPartition(value); - break; - default: - throw new ShardingRuntimeException("not support operator " + x.getOperator()); - } - - if (partitionName == null) { - throw new ShardingRuntimeException("sharding rule violation, partition not match, value : " + value); - } - - TablePartition tablePartition = routeConfig.getPartition(mappingRule.getTable(), partitionName); - - if (tableSource.getAttribute(ATTR_PARTITION) == null) { - ((SQLExprTableSource) tableSource).setExpr(new SQLIdentifierExpr(tablePartition.getTable())); - - tableSource.putAttribute(ATTR_PARTITION, tablePartition); - - if (tablePartition.getDatabase() != null && input != null) { - input.putAttribute(ATTR_DB, tablePartition.getDatabase()); - } - } else if (tableSource.getAttribute(ATTR_PARTITION) != tablePartition) { - throw new ShardingRuntimeException("sharding rule violation, multi-partition matched, value : " - + value); - } - } - } - - return false; - } - - MappingRule getMappingRule(SQLTableSource tableSource) { - if (tableSource instanceof SQLExprTableSource) { - SQLExpr expr = ((SQLExprTableSource) tableSource).getExpr(); - if (expr instanceof SQLIdentifierExpr) { - String table = ((SQLIdentifierExpr) expr).getName(); - return routeConfig.getMappingRule(table); - } - } - - return null; - } - - static boolean isValue(SQLExpr x) { - return x instanceof SQLLiteralExpr || x instanceof SQLVariantRefExpr; - } - - static String getColumn(SQLExpr x) { - if (x instanceof SQLPropertyExpr) { - return ((SQLPropertyExpr) x).getName(); - } - if (x instanceof SQLIdentifierExpr) { - return ((SQLIdentifierExpr) x).getName(); - } - return null; - } - - static SQLTableSource getBinaryOpExprLeftOrRightTableSource(SQLExpr x) { - SQLTableSource tableSource = (SQLTableSource) x.getAttribute(ATTR_TABLE_SOURCE); - if (tableSource != null) { - return tableSource; - } - - SQLTableSource defaltTableSource = getDefaultTableSource(x.getParent()); - if (defaltTableSource instanceof SQLExprTableSource) { - SQLExpr expr = ((SQLExprTableSource) defaltTableSource).getExpr(); - if (expr instanceof SQLIdentifierExpr) { - x.putAttribute(ATTR_TABLE_SOURCE, defaltTableSource); - return defaltTableSource; - } - } - - return null; - } - - @Override - public boolean visit(SQLPropertyExpr x) { - x.getOwner().setParent(x); - - x.getOwner().accept(this); - - return false; - } - - @Override - public boolean visit(SQLIdentifierExpr x) { - SQLTableSource tableSource = getTableSource(x.getName(), x.getParent()); - if (tableSource != null) { - x.putAttribute(ATTR_TABLE_SOURCE, tableSource); - } - - return false; - } - - public static SQLTableSource getTableSource(String name, SQLObject parent) { - Map aliasMap = getAliasMap(parent); - - if (aliasMap == null) { - return null; - } - - SQLTableSource tableSource = aliasMap.get(name); - - if (tableSource != null) { - return tableSource; - } - - for (Map.Entry entry : aliasMap.entrySet()) { - if (name.equalsIgnoreCase(entry.getKey())) { - return tableSource; - } - } - - return null; - } - - @SuppressWarnings("unchecked") - public static Map getAliasMap(SQLObject x) { - if (x == null) { - return null; - } - - if (x instanceof SQLSelectQueryBlock || x instanceof SQLDeleteStatement) { - Map map = (Map) x.getAttribute(ATTR_ALIAS); - if (map == null) { - map = new HashMap(); - x.putAttribute(ATTR_ALIAS, map); - } - return map; - } - - return getAliasMap(x.getParent()); - } - - public static SQLTableSource getDefaultTableSource(SQLObject x) { - if (x == null) { - return null; - } - - if (x instanceof SQLSelectQueryBlock) { - return ((SQLSelectQueryBlock) x).getFrom(); - } - - if (x instanceof SQLDeleteStatement) { - return ((SQLDeleteStatement) x).getTableSource(); - } - - if (x instanceof SQLUpdateStatement) { - return ((SQLUpdateStatement) x).getTableSource(); - } - - return getDefaultTableSource(x.getParent()); - } - -} diff --git a/src/main/java/com/alibaba/druid/sharding/sql/ShardingVisitor.java b/src/main/java/com/alibaba/druid/sharding/sql/ShardingVisitor.java deleted file mode 100644 index 78ba7b2be8..0000000000 --- a/src/main/java/com/alibaba/druid/sharding/sql/ShardingVisitor.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.sharding.sql; - -import java.util.List; - -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.visitor.SQLASTVisitor; - -public interface ShardingVisitor extends SQLASTVisitor { - - public static final String ATTR_DB = "sharding.db"; - public static final String ATTR_PARTITION = "sharding.partition"; - public static final String ATTR_TABLE_SOURCE = "sharding.tableSource"; - public static final String ATTR_ALIAS = "sharding.alias"; - public static final String ATTR_TABLES = "sharding.tables"; - - List getResult(); -} diff --git a/src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingDeleteTest.java b/src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingDeleteTest.java deleted file mode 100644 index 8bb4e02754..0000000000 --- a/src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingDeleteTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.alibaba.druid.bvt.sharding; - -import java.util.Arrays; -import java.util.List; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import com.alibaba.druid.sharding.config.LogicTable; -import com.alibaba.druid.sharding.config.MappingRuleAdapter; -import com.alibaba.druid.sharding.config.MappingRuleListEntry; -import com.alibaba.druid.sharding.config.RouteConfig; -import com.alibaba.druid.sharding.sql.MySqlShardingVisitor; -import com.alibaba.druid.sharding.sql.ShardingVisitor; -import com.alibaba.druid.sql.SQLUtils; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; -import com.alibaba.druid.sql.parser.SQLStatementParser; - -public class MySqlShardingDeleteTest extends TestCase { - - private RouteConfig routeConfig = new RouteConfig(); - - protected void setUp() throws Exception { - MappingRuleAdapter rule = new MappingRuleAdapter(); - - rule.setColumn("gender"); - rule.setTable("person"); - rule.getEntries().add(new MappingRuleListEntry("person_m", "M")); - rule.getEntries().add(new MappingRuleListEntry("person_f", "F")); - - routeConfig.getMappingRules().put("person", rule); - - LogicTable logicTable = new LogicTable(); - logicTable.setLogicTable("person"); - - logicTable.addPartition("person_m", "db0", "person_001"); - logicTable.addPartition("person_f", "db1", "person_002"); - - routeConfig.getLogicTables().put("person", logicTable); - } - - public void test_delete_m() throws Exception { - String sql = "delete from person where name = 'xx' AND gender = 'M'"; - - SQLStatementParser parser = new MySqlStatementParser(sql); - List stmtList = parser.parseStatementList(); - Assert.assertEquals(1, stmtList.size()); - - SQLStatement stmt = stmtList.get(0); - MySqlShardingVisitor visitor = new MySqlShardingVisitor(routeConfig); - stmt.accept(visitor); - - String result = SQLUtils.toMySqlString(stmt); - - Assert.assertEquals("DELETE FROM person_001" + // - "\nWHERE name = 'xx'" + // - "\n\tAND gender = 'M'", result); - Assert.assertEquals("db0", stmt.getAttribute(ShardingVisitor.ATTR_DB)); - } - - public void test_delete_f_params() throws Exception { - String sql = "delete from person where name = 'xx' AND gender = ?"; - - SQLStatementParser parser = new MySqlStatementParser(sql); - List stmtList = parser.parseStatementList(); - Assert.assertEquals(1, stmtList.size()); - - SQLStatement stmt = stmtList.get(0); - MySqlShardingVisitor visitor = new MySqlShardingVisitor(routeConfig, "F"); // 参数化 - - stmt.accept(visitor); - - String result = SQLUtils.toMySqlString(stmt); - - Assert.assertEquals("DELETE FROM person_002" + // - "\nWHERE name = 'xx'" + // - "\n\tAND gender = ?", result); - Assert.assertEquals("db1", stmt.getAttribute(ShardingVisitor.ATTR_DB)); - } -} diff --git a/src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingSelectTest.java b/src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingSelectTest.java deleted file mode 100644 index bc00b3ef7d..0000000000 --- a/src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingSelectTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.alibaba.druid.bvt.sharding; - -import java.util.Arrays; -import java.util.List; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import com.alibaba.druid.sharding.config.LogicTable; -import com.alibaba.druid.sharding.config.MappingRuleAdapter; -import com.alibaba.druid.sharding.config.MappingRuleListEntry; -import com.alibaba.druid.sharding.config.RouteConfig; -import com.alibaba.druid.sharding.sql.MySqlShardingVisitor; -import com.alibaba.druid.sharding.sql.ShardingVisitor; -import com.alibaba.druid.sql.SQLUtils; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; -import com.alibaba.druid.sql.parser.SQLStatementParser; - -public class MySqlShardingSelectTest extends TestCase { - - private RouteConfig routeConfig = new RouteConfig(); - - protected void setUp() throws Exception { - MappingRuleAdapter rule = new MappingRuleAdapter(); - - rule.setColumn("gender"); - rule.setTable("person"); - rule.getEntries().add(new MappingRuleListEntry("person_m", "M")); - rule.getEntries().add(new MappingRuleListEntry("person_f", "F")); - - routeConfig.getMappingRules().put("person", rule); - - LogicTable logicTable = new LogicTable(); - logicTable.setLogicTable("person"); - - logicTable.addPartition("person_m", "db0", "person_001"); - logicTable.addPartition("person_f", "db1", "person_002"); - - routeConfig.getLogicTables().put("person", logicTable); - } - - public void test_insert_m() throws Exception { - String sql = "select * from person where name = 'xx' AND gender = 'M'"; - - SQLStatementParser parser = new MySqlStatementParser(sql); - List stmtList = parser.parseStatementList(); - Assert.assertEquals(1, stmtList.size()); - - SQLStatement stmt = stmtList.get(0); - MySqlShardingVisitor visitor = new MySqlShardingVisitor(routeConfig); - stmt.accept(visitor); - - String result = SQLUtils.toMySqlString(stmt); - - Assert.assertEquals("SELECT *" + // - "\nFROM person_001" + // - "\nWHERE name = 'xx'" + // - "\n\tAND gender = 'M'", result); - Assert.assertEquals("db0", stmt.getAttribute(ShardingVisitor.ATTR_DB)); - } - - public void test_insert_f_params() throws Exception { - String sql = "select * from person where name = 'xx' AND gender = ?"; - - SQLStatementParser parser = new MySqlStatementParser(sql); - List stmtList = parser.parseStatementList(); - Assert.assertEquals(1, stmtList.size()); - - SQLStatement stmt = stmtList.get(0); - MySqlShardingVisitor visitor = new MySqlShardingVisitor(routeConfig, "F"); // 参数化 - - stmt.accept(visitor); - - String result = SQLUtils.toMySqlString(stmt); - - Assert.assertEquals("SELECT *" + // - "\nFROM person_002" + // - "\nWHERE name = 'xx'" + // - "\n\tAND gender = ?", result); - Assert.assertEquals("db1", stmt.getAttribute(ShardingVisitor.ATTR_DB)); - } -} diff --git a/src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingUpdateTest.java b/src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingUpdateTest.java deleted file mode 100644 index c536628cc8..0000000000 --- a/src/test/java/com/alibaba/druid/bvt/sharding/MySqlShardingUpdateTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.alibaba.druid.bvt.sharding; - -import java.util.Arrays; -import java.util.List; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import com.alibaba.druid.sharding.config.LogicTable; -import com.alibaba.druid.sharding.config.MappingRuleAdapter; -import com.alibaba.druid.sharding.config.MappingRuleListEntry; -import com.alibaba.druid.sharding.config.RouteConfig; -import com.alibaba.druid.sharding.sql.MySqlShardingVisitor; -import com.alibaba.druid.sharding.sql.ShardingVisitor; -import com.alibaba.druid.sql.SQLUtils; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; -import com.alibaba.druid.sql.parser.SQLStatementParser; - -public class MySqlShardingUpdateTest extends TestCase { - - private RouteConfig routeConfig = new RouteConfig(); - - protected void setUp() throws Exception { - MappingRuleAdapter rule = new MappingRuleAdapter(); - - rule.setColumn("gender"); - rule.setTable("person"); - rule.getEntries().add(new MappingRuleListEntry("person_m", "M")); - rule.getEntries().add(new MappingRuleListEntry("person_f", "F")); - - routeConfig.getMappingRules().put("person", rule); - - LogicTable logicTable = new LogicTable(); - logicTable.setLogicTable("person"); - - logicTable.addPartition("person_m", "db0", "person_001"); - logicTable.addPartition("person_f", "db1", "person_002"); - - routeConfig.getLogicTables().put("person", logicTable); - } - - public void test_update_m() throws Exception { - String sql = "update person set X = null where name = 'xx' AND gender = 'M'"; - - SQLStatementParser parser = new MySqlStatementParser(sql); - List stmtList = parser.parseStatementList(); - Assert.assertEquals(1, stmtList.size()); - - SQLStatement stmt = stmtList.get(0); - MySqlShardingVisitor visitor = new MySqlShardingVisitor(routeConfig); - stmt.accept(visitor); - - String result = SQLUtils.toMySqlString(stmt); - - Assert.assertEquals("UPDATE person_001" + // - "\nSET X = NULL" + // - "\nWHERE name = 'xx'" + // - "\n\tAND gender = 'M'", result); - Assert.assertEquals("db0", stmt.getAttribute(ShardingVisitor.ATTR_DB)); - } - - public void test_delete_f_params() throws Exception { - String sql = "update person set X = null where name = 'xx' AND gender = ?"; - - SQLStatementParser parser = new MySqlStatementParser(sql); - List stmtList = parser.parseStatementList(); - Assert.assertEquals(1, stmtList.size()); - - SQLStatement stmt = stmtList.get(0); - MySqlShardingVisitor visitor = new MySqlShardingVisitor(routeConfig, "F"); // 参数化 - - stmt.accept(visitor); - - String result = SQLUtils.toMySqlString(stmt); - - Assert.assertEquals("UPDATE person_002" + // - "\nSET X = NULL" + // - "\nWHERE name = 'xx'" + // - "\n\tAND gender = ?", result); - Assert.assertEquals("db1", stmt.getAttribute(ShardingVisitor.ATTR_DB)); - } -} diff --git a/src/test/java/com/alibaba/druid/bvt/sharding/MysqlShardingInsertTest.java b/src/test/java/com/alibaba/druid/bvt/sharding/MysqlShardingInsertTest.java deleted file mode 100644 index 7b2f295dd9..0000000000 --- a/src/test/java/com/alibaba/druid/bvt/sharding/MysqlShardingInsertTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.alibaba.druid.bvt.sharding; - -import java.util.List; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import com.alibaba.druid.sharding.config.LogicTable; -import com.alibaba.druid.sharding.config.MappingRule; -import com.alibaba.druid.sharding.config.MappingRuleAdapter; -import com.alibaba.druid.sharding.config.MappingRuleListEntry; -import com.alibaba.druid.sharding.config.RouteConfig; -import com.alibaba.druid.sharding.sql.MySqlShardingVisitor; -import com.alibaba.druid.sharding.sql.ShardingVisitor; -import com.alibaba.druid.sql.SQLUtils; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; -import com.alibaba.druid.sql.parser.SQLStatementParser; - -public class MysqlShardingInsertTest extends TestCase { - - private RouteConfig routeConfig = new RouteConfig(); - - protected void setUp() throws Exception { - MappingRuleAdapter rule = new MappingRuleAdapter(); - - rule.setColumn("gender"); - rule.getEntries().add(new MappingRuleListEntry("person_m", "M")); - rule.getEntries().add(new MappingRuleListEntry("person_f", "F")); - - routeConfig.getMappingRules().put("person", rule); - - LogicTable logicTable = new LogicTable(); - logicTable.setLogicTable("person"); - - logicTable.addPartition("person_m", "db0", "person_001"); - logicTable.addPartition("person_f", "db1", "person_002"); - - routeConfig.getLogicTables().put("person", logicTable); - } - - public void test_insert_m() throws Exception { - String sql = "insert into person (name, gender) values ('a', 'M')"; - - SQLStatementParser parser = new MySqlStatementParser(sql); - List stmtList = parser.parseStatementList(); - Assert.assertEquals(1, stmtList.size()); - - SQLStatement stmt = stmtList.get(0); - MySqlShardingVisitor visitor = new MySqlShardingVisitor(routeConfig); - stmt.accept(visitor); - - String result = SQLUtils.toMySqlString(stmt); - - Assert.assertEquals("INSERT INTO person_001 (name, gender)" + // - "\nVALUES ('a', 'M')", result); - Assert.assertEquals("db0", stmt.getAttribute(ShardingVisitor.ATTR_DB)); - } - - public void test_insert_f() throws Exception { - String sql = "insert into person (name, gender) values ('a', 'F')"; - - SQLStatementParser parser = new MySqlStatementParser(sql); - List stmtList = parser.parseStatementList(); - Assert.assertEquals(1, stmtList.size()); - - SQLStatement stmt = stmtList.get(0); - MySqlShardingVisitor visitor = new MySqlShardingVisitor(routeConfig); - stmt.accept(visitor); - - String result = SQLUtils.toMySqlString(stmt); - Assert.assertEquals("INSERT INTO person_002 (name, gender)" + // - "\nVALUES ('a', 'F')", result); - Assert.assertEquals("db1", stmt.getAttribute(ShardingVisitor.ATTR_DB)); - } -}