Skip to content

Commit

Permalink
fix #59 can't set mysql "text" column
Browse files Browse the repository at this point in the history
  • Loading branch information
hanahmily authored and gaoht committed Oct 18, 2016
1 parent 5f7d663 commit 6b58890
Show file tree
Hide file tree
Showing 39 changed files with 641 additions and 326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
*
* @author gaohongtao
*/
public interface MergeUnit<I, O> {
interface MergeUnit<I, O> {

/**
* 合并执行结果.
*
* @param params 合并前数据
* @return 合并后结果
*/
O merge(final List<I> params);
O merge(List<I> params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,32 @@ public final class ShardingPreparedStatement extends AbstractPreparedStatementAd

private String[] columnNames;

public ShardingPreparedStatement(final ShardingConnection shardingConnection, final String sql) throws SQLException {
ShardingPreparedStatement(final ShardingConnection shardingConnection, final String sql) {
this(shardingConnection, sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}

public ShardingPreparedStatement(final ShardingConnection shardingConnection,
final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
ShardingPreparedStatement(final ShardingConnection shardingConnection,
final String sql, final int resultSetType, final int resultSetConcurrency) {
this(shardingConnection, sql, resultSetType, resultSetConcurrency, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}

public ShardingPreparedStatement(final ShardingConnection shardingConnection,
ShardingPreparedStatement(final ShardingConnection shardingConnection,
final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
super(shardingConnection, resultSetType, resultSetConcurrency, resultSetHoldability);
preparedSQLRouter = shardingConnection.getShardingContext().getSqlRouteEngine().prepareSQL(sql);
}

public ShardingPreparedStatement(final ShardingConnection shardingConnection, final String sql, final int autoGeneratedKeys) throws SQLException {
ShardingPreparedStatement(final ShardingConnection shardingConnection, final String sql, final int autoGeneratedKeys) {
this(shardingConnection, sql);
this.autoGeneratedKeys = autoGeneratedKeys;
}

public ShardingPreparedStatement(final ShardingConnection shardingConnection, final String sql, final int[] columnIndexes) throws SQLException {
ShardingPreparedStatement(final ShardingConnection shardingConnection, final String sql, final int[] columnIndexes) {
this(shardingConnection, sql);
this.columnIndexes = columnIndexes;
}

public ShardingPreparedStatement(final ShardingConnection shardingConnection, final String sql, final String[] columnNames) throws SQLException {
ShardingPreparedStatement(final ShardingConnection shardingConnection, final String sql, final String[] columnNames) {
this(shardingConnection, sql);
this.columnNames = columnNames;
}
Expand Down Expand Up @@ -159,13 +159,13 @@ public int[] executeBatch() throws SQLException {
private List<PreparedStatementExecutorWrapper> routeSQL() throws SQLException {
List<Object> parameters = getParameters();
List<PreparedStatementExecutorWrapper> result = new ArrayList<>();
SQLRouteResult sqlRouteResult = preparedSQLRouter.route(parameters);
SQLRouteResult sqlRouteResult = preparedSQLRouter.route(getParameters());
MergeContext mergeContext = sqlRouteResult.getMergeContext();
setMergeContext(mergeContext);
for (SQLExecutionUnit each : sqlRouteResult.getExecutionUnits()) {
PreparedStatement preparedStatement = (PreparedStatement) getStatement(getShardingConnection().getConnection(each.getDataSource(), sqlRouteResult.getSqlStatementType()), each.getSql());
replayMethodsInvocation(preparedStatement);
setParameters(preparedStatement, parameters);
getParameters().replayMethodsInvocation(preparedStatement);
result.add(new PreparedStatementExecutorWrapper(preparedStatement, parameters, each));
}
return result;
Expand All @@ -186,11 +186,4 @@ protected PreparedStatement generateStatement(final Connection conn, final Strin
}
return conn.prepareStatement(shardingSql, getResultSetType(), getResultSetConcurrency());
}

private void setParameters(final PreparedStatement preparedStatement, final List<Object> parameters) throws SQLException {
int i = 1;
for (Object each : parameters) {
preparedStatement.setObject(i++, each);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingConnection;
import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationPreparedStatement;
import com.dangdang.ddframe.rdb.sharding.jdbc.util.ParameterList;
import lombok.AccessLevel;
import lombok.Getter;

import java.io.InputStream;
Expand All @@ -28,14 +30,13 @@
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
* 预编译语句对象的适配类.
Expand All @@ -48,225 +49,219 @@
*/
public abstract class AbstractPreparedStatementAdapter extends AbstractUnsupportedOperationPreparedStatement {

@Getter
private final List<Object> parameters = new ArrayList<>();
@Getter(AccessLevel.PROTECTED)
private ParameterList parameters = new ParameterList(PreparedStatement.class);

protected AbstractPreparedStatementAdapter(final ShardingConnection shardingConnection, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
super(shardingConnection, resultSetType, resultSetConcurrency, resultSetHoldability);
}

@Override
public final void setNull(final int parameterIndex, final int sqlType) throws SQLException {
setParameter(parameterIndex, null);
setParameter(parameterIndex, "setNull", new Class[]{int.class, int.class}, parameterIndex, sqlType);
}

@Override
public final void setNull(final int parameterIndex, final int sqlType, final String typeName) throws SQLException {
setParameter(parameterIndex, null);
setParameter(parameterIndex, "setNull", new Class[]{int.class, int.class, String.class}, parameterIndex, sqlType, typeName);
}

@Override
public final void setBoolean(final int parameterIndex, final boolean x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setBoolean", new Class[]{int.class, boolean.class}, parameterIndex, x);
}

@Override
public final void setByte(final int parameterIndex, final byte x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setByte", new Class[]{int.class, byte.class}, parameterIndex, x);
}

@Override
public final void setShort(final int parameterIndex, final short x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setShort", new Class[]{int.class, short.class}, parameterIndex, x);
}

@Override
public final void setInt(final int parameterIndex, final int x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setInt", new Class[]{int.class, int.class}, parameterIndex, x);
}

@Override
public final void setLong(final int parameterIndex, final long x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setLong", new Class[]{int.class, long.class}, parameterIndex, x);
}

@Override
public final void setFloat(final int parameterIndex, final float x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setFloat", new Class[]{int.class, float.class}, parameterIndex, x);
}

@Override
public final void setDouble(final int parameterIndex, final double x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setDouble", new Class[]{int.class, double.class}, parameterIndex, x);
}

@Override
public final void setString(final int parameterIndex, final String x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setString", new Class[]{int.class, String.class}, parameterIndex, x);
}

@Override
public final void setBigDecimal(final int parameterIndex, final BigDecimal x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setBigDecimal", new Class[]{int.class, BigDecimal.class}, parameterIndex, x);
}

@Override
public final void setDate(final int parameterIndex, final Date x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setDate", new Class[]{int.class, Date.class}, parameterIndex, x);
}

@Override
public final void setDate(final int parameterIndex, final Date x, final Calendar cal) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setDate", new Class[]{int.class, Date.class, Calendar.class}, parameterIndex, x, cal);
}

@Override
public final void setTime(final int parameterIndex, final Time x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setTime", new Class[]{int.class, Time.class}, parameterIndex, x);
}

@Override
public final void setTime(final int parameterIndex, final Time x, final Calendar cal) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setTime", new Class[]{int.class, Time.class, Calendar.class}, parameterIndex, x, cal);
}

@Override
public final void setTimestamp(final int parameterIndex, final Timestamp x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setTimestamp", new Class[]{int.class, Timestamp.class}, parameterIndex, x);
}

@Override
public final void setTimestamp(final int parameterIndex, final Timestamp x, final Calendar cal) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setTimestamp", new Class[]{int.class, Timestamp.class, Calendar.class}, parameterIndex, x, cal);
}

@Override
public final void setBytes(final int parameterIndex, final byte[] x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setBytes", new Class[]{int.class, byte[].class}, parameterIndex, x);
}

@Override
public final void setBlob(final int parameterIndex, final Blob x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setBlob", new Class[]{int.class, Blob.class}, parameterIndex, x);
}

@Override
public final void setBlob(final int parameterIndex, final InputStream x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setBlob", new Class[]{int.class, InputStream.class}, parameterIndex, x);
}

@Override
public final void setBlob(final int parameterIndex, final InputStream x, final long length) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setBlob", new Class[]{int.class, InputStream.class, long.class}, parameterIndex, x, length);
}

@Override
public final void setClob(final int parameterIndex, final Clob x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setClob", new Class[]{int.class, Clob.class}, parameterIndex, x);
}

@Override
public final void setClob(final int parameterIndex, final Reader x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setClob", new Class[]{int.class, Reader.class}, parameterIndex, x);
}

@Override
public final void setClob(final int parameterIndex, final Reader x, final long length) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setClob", new Class[]{int.class, Reader.class, long.class}, parameterIndex, x, length);
}

@Override
public final void setAsciiStream(final int parameterIndex, final InputStream x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setAsciiStream", new Class[]{int.class, InputStream.class}, parameterIndex, x);
}

@Override
public final void setAsciiStream(final int parameterIndex, final InputStream x, final int length) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setAsciiStream", new Class[]{int.class, InputStream.class, int.class}, parameterIndex, x, length);
}

@Override
public final void setAsciiStream(final int parameterIndex, final InputStream x, final long length) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setAsciiStream", new Class[]{int.class, InputStream.class, long.class}, parameterIndex, x, length);
}

@Override
public final void setUnicodeStream(final int parameterIndex, final InputStream x, final int length) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setUnicodeStream", new Class[]{int.class, InputStream.class, int.class}, parameterIndex, x, length);
}

@Override
public final void setBinaryStream(final int parameterIndex, final InputStream x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setBinaryStream", new Class[]{int.class, InputStream.class}, parameterIndex, x);
}

@Override
public final void setBinaryStream(final int parameterIndex, final InputStream x, final int length) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setBinaryStream", new Class[]{int.class, InputStream.class, int.class}, parameterIndex, x, length);
}

@Override
public final void setBinaryStream(final int parameterIndex, final InputStream x, final long length) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setBinaryStream", new Class[]{int.class, InputStream.class, long.class}, parameterIndex, x, length);
}

@Override
public final void setCharacterStream(final int parameterIndex, final Reader x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setCharacterStream", new Class[]{int.class, Reader.class}, parameterIndex, x);
}

@Override
public final void setCharacterStream(final int parameterIndex, final Reader x, final int length) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setCharacterStream", new Class[]{int.class, Reader.class, int.class}, parameterIndex, x, length);
}

@Override
public final void setCharacterStream(final int parameterIndex, final Reader x, final long length) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setCharacterStream", new Class[]{int.class, Reader.class, long.class}, parameterIndex, x, length);
}

@Override
public final void setURL(final int parameterIndex, final URL x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setURL", new Class[]{int.class, URL.class}, parameterIndex, x);
}

@Override
public final void setSQLXML(final int parameterIndex, final SQLXML x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setSQLXML", new Class[]{int.class, SQLXML.class}, parameterIndex, x);
}

@Override
public final void setRef(final int parameterIndex, final Ref x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setRef", new Class[]{int.class, Ref.class}, parameterIndex, x);
}

@Override
public final void setObject(final int parameterIndex, final Object x) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setObject", new Class[]{int.class, Object.class}, parameterIndex, x);
}

@Override
public final void setObject(final int parameterIndex, final Object x, final int targetSqlType) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setObject", new Class[]{int.class, Object.class, int.class}, parameterIndex, x, targetSqlType);
}

@Override
public final void setObject(final int parameterIndex, final Object x, final int targetSqlType, final int scaleOrLength) throws SQLException {
setParameter(parameterIndex, x);
setParameter(parameterIndex, "setObject", new Class[]{int.class, Object.class, int.class, int.class}, parameterIndex, x, targetSqlType, scaleOrLength);
}

@Override
public final void clearParameters() throws SQLException {
parameters.clear();
}

private void setParameter(final int index, final Object x) {
int extendedSize = index - parameters.size();
if (extendedSize > 1) {
while (--extendedSize > 0) {
parameters.add(null);
}
}
parameters.add(index - 1, x);
private void setParameter(final int parameterIndex, final String methodName, final Class[] parameterTypes, final Object... arguments) {
parameters.recordMethodInvocation(parameterIndex, methodName, parameterTypes, arguments);
}
}
Loading

0 comments on commit 6b58890

Please sign in to comment.