Skip to content

Commit

Permalink
split AbstractUnsupportedOperationPreparedStatement and AbstractUnsup…
Browse files Browse the repository at this point in the history
…portedOperationStatement
  • Loading branch information
terrymanu committed Aug 30, 2017
1 parent 45ee7bf commit 07f5b77
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException;
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.invocation.SetParameterMethodInvocation;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.connection.ShardingConnection;
import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationPreparedStatement;
import lombok.Getter;

Expand All @@ -32,11 +31,13 @@
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 @@ -48,15 +49,175 @@
*/
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<>();

protected AbstractPreparedStatementAdapter(final ShardingConnection shardingConnection, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
super(shardingConnection, resultSetType, resultSetConcurrency, resultSetHoldability);
@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,7 +18,6 @@
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 @@ -29,21 +28,18 @@
/**
* Adapter for {@code Statement}.
*
* @author zhangliang
* @author gaohongtao
*/
@RequiredArgsConstructor
public abstract class AbstractStatementAdapter extends AbstractUnsupportedOperationStatement {

private final Class<? extends Statement> recordTargetClass;

private boolean closed;

private boolean poolable;

private int fetchSize;

@Override
@SuppressWarnings("unchecked")
public final void close() throws SQLException {
closed = true;
getRoutedStatements().clear();
Expand Down Expand Up @@ -72,7 +68,7 @@ public final boolean isPoolable() throws SQLException {
public final void setPoolable(final boolean poolable) throws SQLException {
this.poolable = poolable;
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setPoolable", new Class[] {boolean.class}, new Object[] {poolable});
recordMethodInvocation(Statement.class, "setPoolable", new Class[] {boolean.class}, new Object[] {poolable});
return;
}
for (Statement each : getRoutedStatements()) {
Expand All @@ -89,7 +85,7 @@ public final int getFetchSize() throws SQLException {
public final void setFetchSize(final int rows) throws SQLException {
this.fetchSize = rows;
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setFetchSize", new Class[] {int.class}, new Object[] {rows});
recordMethodInvocation(Statement.class, "setFetchSize", new Class[] {int.class}, new Object[] {rows});
return;
}
for (Statement each : getRoutedStatements()) {
Expand All @@ -100,7 +96,7 @@ public final void setFetchSize(final int rows) throws SQLException {
@Override
public final void setEscapeProcessing(final boolean enable) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setEscapeProcessing", new Class[] {boolean.class}, new Object[] {enable});
recordMethodInvocation(Statement.class, "setEscapeProcessing", new Class[] {boolean.class}, new Object[] {enable});
return;
}
for (Statement each : getRoutedStatements()) {
Expand All @@ -115,17 +111,6 @@ public final void cancel() throws SQLException {
}
}

@Override
public final void setCursorName(final String name) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setCursorName", new Class[] {String.class}, new Object[] {name});
return;
}
for (Statement each : getRoutedStatements()) {
each.setCursorName(name);
}
}

@Override
public final int getUpdateCount() throws SQLException {
long result = 0;
Expand All @@ -151,9 +136,6 @@ public SQLWarning getWarnings() throws SQLException {
public void clearWarnings() throws SQLException {
}

/*
* Only store procedures will support multiple ResetSets, so don't support here.
*/
@Override
public final boolean getMoreResults() throws SQLException {
return false;
Expand All @@ -172,7 +154,7 @@ public final int getMaxFieldSize() throws SQLException {
@Override
public final void setMaxFieldSize(final int max) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setMaxFieldSize", new Class[] {int.class}, new Object[] {max});
recordMethodInvocation(Statement.class, "setMaxFieldSize", new Class[] {int.class}, new Object[] {max});
return;
}
for (Statement each : getRoutedStatements()) {
Expand All @@ -189,7 +171,7 @@ public final int getMaxRows() throws SQLException {
@Override
public final void setMaxRows(final int max) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setMaxRows", new Class[] {int.class}, new Object[] {max});
recordMethodInvocation(Statement.class, "setMaxRows", new Class[] {int.class}, new Object[] {max});
return;
}
for (Statement each : getRoutedStatements()) {
Expand All @@ -205,13 +187,13 @@ public final int getQueryTimeout() throws SQLException {
@Override
public final void setQueryTimeout(final int seconds) throws SQLException {
if (getRoutedStatements().isEmpty()) {
recordMethodInvocation(recordTargetClass, "setQueryTimeout", new Class[] {int.class}, new Object[] {seconds});
recordMethodInvocation(Statement.class, "setQueryTimeout", new Class[] {int.class}, new Object[] {seconds});
return;
}
for (Statement each : getRoutedStatements()) {
each.setQueryTimeout(seconds);
}
}

protected abstract Collection<? extends Statement> getRoutedStatements();
protected abstract Collection<Statement> getRoutedStatements();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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 @@ -35,6 +36,7 @@
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public final class MasterSlaveStatement extends AbstractStatementAdapter {

Expand All @@ -57,14 +59,6 @@ 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 Expand Up @@ -155,7 +149,8 @@ public ResultSet getResultSet() throws SQLException {
return routedStatement.getResultSet();
}

protected Collection<? extends Statement> getRoutedStatements() {
@Override
protected Collection<Statement> getRoutedStatements() {
return Collections.singletonList(routedStatement);
}
}
Loading

0 comments on commit 07f5b77

Please sign in to comment.