Skip to content

Commit

Permalink
refactor AbstractUnsupportedOperationPreparedStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Aug 30, 2017
1 parent 07f5b77 commit 1c68d93
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
Expand All @@ -49,175 +47,11 @@
*/
public abstract class AbstractPreparedStatementAdapter extends AbstractUnsupportedOperationPreparedStatement {

private boolean closed;

private boolean poolable;

private int fetchSize;

private final List<SetParameterMethodInvocation> setParameterMethodInvocations = new LinkedList<>();

@Getter
private final List<Object> parameters = new ArrayList<>();

@Override
public final void close() throws SQLException {
closed = true;
getRoutedPreparedStatements().clear();
Collection<SQLException> exceptions = new LinkedList<>();
for (PreparedStatement each : getRoutedPreparedStatements()) {
try {
each.close();
} catch (final SQLException ex) {
exceptions.add(ex);
}
}
throwSQLExceptionIfNecessary(exceptions);
}

@Override
public final boolean isClosed() throws SQLException {
return closed;
}

@Override
public final boolean isPoolable() throws SQLException {
return poolable;
}

@Override
public final void setPoolable(final boolean poolable) throws SQLException {
this.poolable = poolable;
if (getRoutedPreparedStatements().isEmpty()) {
recordMethodInvocation(PreparedStatement.class, "setPoolable", new Class[] {boolean.class}, new Object[] {poolable});
return;
}
for (PreparedStatement each : getRoutedPreparedStatements()) {
each.setPoolable(poolable);
}
}

@Override
public final int getFetchSize() throws SQLException {
return fetchSize;
}

@Override
public final void setFetchSize(final int rows) throws SQLException {
this.fetchSize = rows;
if (getRoutedPreparedStatements().isEmpty()) {
recordMethodInvocation(PreparedStatement.class, "setFetchSize", new Class[] {int.class}, new Object[] {rows});
return;
}
for (PreparedStatement each : getRoutedPreparedStatements()) {
each.setFetchSize(rows);
}
}

@Override
public final void setEscapeProcessing(final boolean enable) throws SQLException {
if (getRoutedPreparedStatements().isEmpty()) {
recordMethodInvocation(PreparedStatement.class, "setEscapeProcessing", new Class[] {boolean.class}, new Object[] {enable});
return;
}
for (PreparedStatement each : getRoutedPreparedStatements()) {
each.setEscapeProcessing(enable);
}
}

@Override
public final void cancel() throws SQLException {
for (PreparedStatement each : getRoutedPreparedStatements()) {
each.cancel();
}
}

@Override
public final int getUpdateCount() throws SQLException {
long result = 0;
boolean hasResult = false;
for (PreparedStatement each : getRoutedPreparedStatements()) {
if (each.getUpdateCount() > -1) {
hasResult = true;
}
result += each.getUpdateCount();
}
if (result > Integer.MAX_VALUE) {
result = Integer.MAX_VALUE;
}
return hasResult ? Long.valueOf(result).intValue() : -1;
}

@Override
public SQLWarning getWarnings() throws SQLException {
return null;
}

@Override
public void clearWarnings() throws SQLException {
}

@Override
public final boolean getMoreResults() throws SQLException {
return false;
}

@Override
public final boolean getMoreResults(final int current) throws SQLException {
return false;
}

@Override
public final int getMaxFieldSize() throws SQLException {
return getRoutedPreparedStatements().isEmpty() ? 0 : getRoutedPreparedStatements().iterator().next().getMaxFieldSize();
}

@Override
public final void setMaxFieldSize(final int max) throws SQLException {
if (getRoutedPreparedStatements().isEmpty()) {
recordMethodInvocation(PreparedStatement.class, "setMaxFieldSize", new Class[] {int.class}, new Object[] {max});
return;
}
for (PreparedStatement each : getRoutedPreparedStatements()) {
each.setMaxFieldSize(max);
}
}

// TODO Confirm MaxRows for multiple databases is need special handle. eg: 10 statements maybe MaxRows / 10
@Override
public final int getMaxRows() throws SQLException {
return getRoutedPreparedStatements().isEmpty() ? -1 : getRoutedPreparedStatements().iterator().next().getMaxRows();
}

@Override
public final void setMaxRows(final int max) throws SQLException {
if (getRoutedPreparedStatements().isEmpty()) {
recordMethodInvocation(PreparedStatement.class, "setMaxRows", new Class[] {int.class}, new Object[] {max});
return;
}
for (PreparedStatement each : getRoutedPreparedStatements()) {
each.setMaxRows(max);
}
}

@Override
public final int getQueryTimeout() throws SQLException {
return getRoutedPreparedStatements().isEmpty() ? 0 : getRoutedPreparedStatements().iterator().next().getQueryTimeout();
}

@Override
public final void setQueryTimeout(final int seconds) throws SQLException {
if (getRoutedPreparedStatements().isEmpty()) {
recordMethodInvocation(PreparedStatement.class, "setQueryTimeout", new Class[] {int.class}, new Object[] {seconds});
return;
}
for (PreparedStatement each : getRoutedPreparedStatements()) {
each.setQueryTimeout(seconds);
}
}

protected abstract Collection<PreparedStatement> getRoutedPreparedStatements();

@Override
public final void setNull(final int parameterIndex, final int sqlType) throws SQLException {
setParameter(parameterIndex, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.dangdang.ddframe.rdb.sharding.jdbc.adapter;

import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationStatement;
import lombok.RequiredArgsConstructor;

import java.sql.SQLException;
import java.sql.SQLWarning;
Expand All @@ -31,8 +32,11 @@
* @author zhangliang
* @author gaohongtao
*/
@RequiredArgsConstructor
public abstract class AbstractStatementAdapter extends AbstractUnsupportedOperationStatement {

private final Class<? extends Statement> targetClass;

private boolean closed;

private boolean poolable;
Expand Down Expand Up @@ -68,7 +72,7 @@ public final boolean isPoolable() throws SQLException {
public final void setPoolable(final boolean poolable) throws SQLException {
this.poolable = poolable;
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(Statement.class, "setPoolable", new Class[] {boolean.class}, new Object[] {poolable});
recordMethodInvocation(targetClass, "setPoolable", new Class[] {boolean.class}, new Object[] {poolable});
return;
}
for (Statement each : getRoutedStatements()) {
Expand All @@ -85,7 +89,7 @@ public final int getFetchSize() throws SQLException {
public final void setFetchSize(final int rows) throws SQLException {
this.fetchSize = rows;
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(Statement.class, "setFetchSize", new Class[] {int.class}, new Object[] {rows});
recordMethodInvocation(targetClass, "setFetchSize", new Class[] {int.class}, new Object[] {rows});
return;
}
for (Statement each : getRoutedStatements()) {
Expand All @@ -96,7 +100,7 @@ public final void setFetchSize(final int rows) throws SQLException {
@Override
public final void setEscapeProcessing(final boolean enable) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(Statement.class, "setEscapeProcessing", new Class[] {boolean.class}, new Object[] {enable});
recordMethodInvocation(targetClass, "setEscapeProcessing", new Class[] {boolean.class}, new Object[] {enable});
return;
}
for (Statement each : getRoutedStatements()) {
Expand Down Expand Up @@ -154,7 +158,7 @@ public final int getMaxFieldSize() throws SQLException {
@Override
public final void setMaxFieldSize(final int max) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(Statement.class, "setMaxFieldSize", new Class[] {int.class}, new Object[] {max});
recordMethodInvocation(targetClass, "setMaxFieldSize", new Class[] {int.class}, new Object[] {max});
return;
}
for (Statement each : getRoutedStatements()) {
Expand All @@ -171,7 +175,7 @@ public final int getMaxRows() throws SQLException {
@Override
public final void setMaxRows(final int max) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(Statement.class, "setMaxRows", new Class[] {int.class}, new Object[] {max});
recordMethodInvocation(targetClass, "setMaxRows", new Class[] {int.class}, new Object[] {max});
return;
}
for (Statement each : getRoutedStatements()) {
Expand All @@ -187,13 +191,13 @@ public final int getQueryTimeout() throws SQLException {
@Override
public final void setQueryTimeout(final int seconds) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(Statement.class, "setQueryTimeout", new Class[] {int.class}, new Object[] {seconds});
recordMethodInvocation(targetClass, "setQueryTimeout", new Class[] {int.class}, new Object[] {seconds});
return;
}
for (Statement each : getRoutedStatements()) {
each.setQueryTimeout(seconds);
}
}

protected abstract Collection<Statement> getRoutedStatements();
protected abstract Collection<? extends Statement> getRoutedStatements();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.sql.Connection;
import java.sql.ResultSet;
Expand All @@ -36,7 +35,6 @@
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public final class MasterSlaveStatement extends AbstractStatementAdapter {

Expand All @@ -59,6 +57,14 @@ public MasterSlaveStatement(final MasterSlaveConnection connection, final int re
this(connection, resultSetType, resultSetConcurrency, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}

public MasterSlaveStatement(final MasterSlaveConnection connection, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
super(Statement.class);
this.connection = connection;
this.resultSetType = resultSetType;
this.resultSetConcurrency = resultSetConcurrency;
this.resultSetHoldability = resultSetHoldability;
}

@Override
public ResultSet executeQuery(final String sql) throws SQLException {
Collection<Connection> connections = connection.getConnection(sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public final class ShardingPreparedStatement extends AbstractPreparedStatementAd

private final List<List<Object>> parameterSets = new LinkedList<>();

private final Collection<PreparedStatement> routedPreparedStatements = new LinkedList<>();
private final Collection<PreparedStatement> routedStatements = new LinkedList<>();

@Getter(AccessLevel.NONE)
private boolean returnGeneratedKeys;
Expand Down Expand Up @@ -155,7 +155,7 @@ private Collection<PreparedStatementUnit> route() throws SQLException {
} else {
preparedStatements = Collections.singletonList(generatePreparedStatement(each));
}
routedPreparedStatements.addAll(preparedStatements);
routedStatements.addAll(preparedStatements);
for (PreparedStatement preparedStatement : preparedStatements) {
replaySetParameter(preparedStatement);
result.add(new PreparedStatementUnit(each, preparedStatement));
Expand Down Expand Up @@ -227,8 +227,8 @@ public ResultSet getGeneratedKeys() throws SQLException {
if (returnGeneratedKeys && generatedKey.isPresent()) {
return new GeneratedKeysResultSet(routeResult.getGeneratedKeys().iterator(), generatedKey.get().getColumn(), this);
}
if (1 == routedPreparedStatements.size()) {
return routedPreparedStatements.iterator().next().getGeneratedKeys();
if (1 == routedStatements.size()) {
return routedStatements.iterator().next().getGeneratedKeys();
}
return new GeneratedKeysResultSet();
}
Expand Down Expand Up @@ -265,20 +265,15 @@ public ResultSet getResultSet() throws SQLException {
if (null != currentResultSet) {
return currentResultSet;
}
if (1 == routedPreparedStatements.size()) {
currentResultSet = routedPreparedStatements.iterator().next().getResultSet();
if (1 == routedStatements.size()) {
currentResultSet = routedStatements.iterator().next().getResultSet();
return currentResultSet;
}
List<ResultSet> resultSets = new ArrayList<>(routedPreparedStatements.size());
for (PreparedStatement each : routedPreparedStatements) {
List<ResultSet> resultSets = new ArrayList<>(routedStatements.size());
for (PreparedStatement each : routedStatements) {
resultSets.add(each.getResultSet());
}
currentResultSet = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge());
return currentResultSet;
}

@Override
protected Collection<PreparedStatement> getRoutedPreparedStatements() {
return routedPreparedStatements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public ShardingStatement(final ShardingConnection connection, final int resultSe
}

public ShardingStatement(final ShardingConnection connection, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
super(Statement.class);
this.connection = connection;
this.resultSetType = resultSetType;
this.resultSetConcurrency = resultSetConcurrency;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.dangdang.ddframe.rdb.sharding.jdbc.unsupported;

import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractStatementAdapter;

import java.io.Reader;
import java.sql.Array;
import java.sql.NClob;
Expand All @@ -34,7 +36,11 @@
*
* @author zhangliang
*/
public abstract class AbstractUnsupportedOperationPreparedStatement extends AbstractUnsupportedOperationStatement implements PreparedStatement {
public abstract class AbstractUnsupportedOperationPreparedStatement extends AbstractStatementAdapter implements PreparedStatement {

public AbstractUnsupportedOperationPreparedStatement() {
super(PreparedStatement.class);
}

@Override
public final ResultSetMetaData getMetaData() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void assertOverMaxUpdateRow() throws SQLException {
when(statement1.getUpdateCount()).thenReturn(Integer.MAX_VALUE);
final Statement statement2 = Mockito.mock(Statement.class);
when(statement2.getUpdateCount()).thenReturn(Integer.MAX_VALUE);
AbstractStatementAdapter statement = new AbstractStatementAdapter() {
AbstractStatementAdapter statement = new AbstractStatementAdapter(Statement.class) {

@Override
protected Collection<Statement> getRoutedStatements() {
Expand Down

0 comments on commit 1c68d93

Please sign in to comment.