Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.javaclub.cdl.client.group.SGroupPreparedStatement Maven / Gradle / Ivy
package com.github.javaclub.cdl.client.group;
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.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.github.javaclub.cdl.client.common.DataSourceWrapper;
import com.github.javaclub.cdl.client.common.ExceptionSorter;
import com.github.javaclub.cdl.client.common.SQLParser;
import com.github.javaclub.cdl.client.common.SqlType;
import com.github.javaclub.cdl.client.common.ThreadLocalDataSourceIndex;
import com.github.javaclub.cdl.client.group.DBSelector.DataSourceTryer;
import com.github.javaclub.cdl.client.parameter.ParameterContext;
import com.github.javaclub.cdl.client.parameter.ParameterMethod;
import com.github.javaclub.cdl.client.parameter.Parameters;
import com.github.javaclub.cdl.client.util.ExceptionUtils;
import com.github.javaclub.cdl.client.util.GroupHintParser;
import com.github.javaclub.cdl.client.util.SQLMonitors;
public class SGroupPreparedStatement extends SGroupStatement implements PreparedStatement {
private String sql;
private int autoGeneratedKeys = -1;
private int[] columnIndexes;
private String[] columnNames;
private List> pstArgs;
protected Map parameterSettings = new HashMap();
public SGroupPreparedStatement(SGroupDataSource sGroupDataSource, SGroupConnection sGroupConnection, String sql) {
super(sGroupDataSource, sGroupConnection);
this.sql = sql;
}
/*---------------------- jdbc规范 ---------------------*/
@Override
public ResultSet executeQuery() throws SQLException {
boolean result = true;
try{
SQLMonitors.entry(sql);
if(sGroupDataSource.getDbConfigManager().isEnableTrace()){
//CDLBraveTrace.beginTrace(sql,sGroupDataSource.getGroupName());
}
checkClosed();
ensureResultSetIsEmpty();
boolean isSelect = SqlType.SELECT.equals(SQLParser.getSqlType(sql));
boolean gotoRead = isSelect && sGroupConnection.getAutoCommit();
synchronized (sGroupConnection) { // 同一个连接上面执行多次时,要顺序执行,否则有并发问题
Connection conn = sGroupConnection.getBaseConnection(sql, gotoRead);
if (conn != null) {
sql = GroupHintParser.removeCdlGroupHint(sql);
return executeQueryOnConnection(conn, sql);
} else {
Integer dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeCdlGroupHint(sql);
if (dataSourceIndex < 0) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
if(!gotoRead && isSelect){ // 事务里面的查询先强制走主库
try{
return sGroupDataSource.getDBSelector(false).tryExecute(executeQueryTryer, retryingTimes, sql, dataSourceIndex);
}catch(Exception e){
}
// 事务里面的查询前面没有找到主库,重试一次从库
return this.sGroupDataSource.getDBSelector(true).tryExecute(executeQueryTryer, retryingTimes, sql, dataSourceIndex);
}
return this.sGroupDataSource.getDBSelector(gotoRead).tryExecute(executeQueryTryer, retryingTimes, sql, dataSourceIndex);
}
}
} catch(SQLException e){
result = false;
throw e;
} finally{
SQLMonitors.exit(sql, result);
if(sGroupDataSource.getDbConfigManager().isEnableTrace()){
//CDLBraveTrace.endTrace(null);
}
}
}
@Override
public int executeUpdate() throws SQLException {
boolean result = true;
try{
SQLMonitors.entry(sql);
if(sGroupDataSource.getDbConfigManager().isEnableTrace()){
//CDLBraveTrace.beginTrace(sql,sGroupDataSource.getGroupName());
}
checkClosed();
ensureResultSetIsEmpty();
synchronized (sGroupConnection) { // 同一个连接上面执行多次时,要顺序执行,否则有并发问题
Connection conn = sGroupConnection.getBaseConnection(sql, false);
if (conn != null) {
sql = GroupHintParser.removeCdlGroupHint(sql);
int updateCount = executeUpdateOnConnection(conn);
super.updateCount = updateCount;
return updateCount;
} else {
Integer dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeCdlGroupHint(sql);
if (dataSourceIndex < 0) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
int updateCount = sGroupDataSource.getDBSelector(false).tryExecute(null, executeUpdateTryer, retryingTimes, sql,
dataSourceIndex);
super.updateCount = updateCount;
return updateCount;
}
}
} catch(SQLException e){
result = false;
throw e;
} finally{
if(sGroupDataSource.getDbConfigManager().isEnableTrace()){
//CDLBraveTrace.endTrace(null);
}
SQLMonitors.exit(sql, result);
}
}
@Override
public boolean execute() throws SQLException {
//boolean result = true;
try{
//SQLMonitors.entry(sql);
SqlType sqlType = SQLParser.getSqlType(sql);
if (sqlType == SqlType.SELECT || sqlType == SqlType.SELECT_FOR_UPDATE || sqlType == SqlType.SHOW) {
executeQuery();
return true;
} else if (sqlType == SqlType.INSERT || sqlType == SqlType.UPDATE || sqlType == SqlType.DELETE || sqlType == SqlType.REPLACE
|| sqlType == SqlType.TRUNCATE || sqlType == SqlType.CREATE || sqlType == SqlType.DROP || sqlType == SqlType.LOAD
|| sqlType == SqlType.MERGE) {
super.updateCount = executeUpdate();
return false;
} else {
throw new SQLException("only select, insert, update, delete,truncate,create,drop,load,merge sql is supported");
}
} catch(SQLException e){
//result = false;
throw e;
} finally{
//SQLMonitors.exit(sql, result);
}
}
@Override
public void addBatch() throws SQLException {
if (pstArgs == null) {
pstArgs = new LinkedList>();
}
Map newArg = new HashMap(parameterSettings.size());
newArg.putAll(parameterSettings);
parameterSettings.clear();
pstArgs.add(newArg);
}
@Override
public int[] executeBatch() throws SQLException {
boolean result = true;
try {
SQLMonitors.entry(sql);
if(sGroupDataSource.getDbConfigManager().isEnableTrace()){
//CDLBraveTrace.beginTrace(sql,sGroupDataSource.getGroupName());
}
checkClosed();
ensureResultSetIsEmpty();
if (pstArgs == null || pstArgs.isEmpty()) {
return new int[0];
}
synchronized (sGroupConnection) { // 同一个连接上面执行多次时,要顺序执行,否则有并发问题
Connection conn = sGroupConnection.getBaseConnection(sql, false);
if (conn != null) {
sql = GroupHintParser.removeCdlGroupHint(sql);
return executeBatchOnConnection(conn);
} else {
Integer dataSourceIndex = GroupHintParser.convertHint2Index(sql);
sql = GroupHintParser.removeCdlGroupHint(sql);
if (dataSourceIndex < 0) {
dataSourceIndex = ThreadLocalDataSourceIndex.getIndex();
}
return sGroupDataSource.getDBSelector(false).tryExecute(null, executeBatchTryer, retryingTimes, dataSourceIndex);
}
}
} catch(SQLException e){
result = false;
throw e;
} finally {
if(sGroupDataSource.getDbConfigManager().isEnableTrace()){
//CDLBraveTrace.endTrace(null);
}
SQLMonitors.exit(sql, result);
if (pstArgs != null)
pstArgs.clear();
}
}
@Override
public void setNull(int parameterIndex, int sqlType) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setNull1, new Object[] { parameterIndex, sqlType }));
}
@Override
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setBoolean, new Object[] { parameterIndex, x }));
}
@Override
public void setByte(int parameterIndex, byte x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setByte, new Object[] { parameterIndex, x }));
}
@Override
public void setShort(int parameterIndex, short x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setShort, new Object[] { parameterIndex, x }));
}
@Override
public void setInt(int parameterIndex, int x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setInt, new Object[] { parameterIndex, x }));
}
@Override
public void setLong(int parameterIndex, long x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setLong, new Object[] { parameterIndex, x }));
}
@Override
public void setFloat(int parameterIndex, float x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setFloat, new Object[] { parameterIndex, x }));
}
@Override
public void setDouble(int parameterIndex, double x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setDouble, new Object[] { parameterIndex, x }));
}
@Override
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setBigDecimal, new Object[] { parameterIndex, x }));
}
@Override
public void setString(int parameterIndex, String x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setString, new Object[] { parameterIndex, x }));
}
@Override
public void setBytes(int parameterIndex, byte[] x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setBytes, new Object[] { parameterIndex, x }));
}
@Override
public void setDate(int parameterIndex, Date x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setDate1, new Object[] { parameterIndex, x }));
}
@Override
public void setTime(int parameterIndex, Time x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setTime1, new Object[] { parameterIndex, x }));
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setTimestamp1, new Object[] { parameterIndex, x }));
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setAsciiStream, new Object[] { parameterIndex, x,
length }));
}
@Override
public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setUnicodeStream, new Object[] { parameterIndex, x,
length }));
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setBinaryStream, new Object[] { parameterIndex, x,
length }));
}
@Override
public void clearParameters() throws SQLException {
parameterSettings.clear();
}
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setObject2, new Object[] { parameterIndex, x,
targetSqlType }));
}
@Override
public void setObject(int parameterIndex, Object x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setObject1, new Object[] { parameterIndex, x }));
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setCharacterStream, new Object[] { parameterIndex, reader, length }));
}
@Override
public void setRef(int i, Ref x) throws SQLException {
parameterSettings.put(i, new ParameterContext(ParameterMethod.setRef, new Object[] { i, x }));
}
@Override
public void setBlob(int parameterIndex, Blob x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setBlob, new Object[] { parameterIndex, x }));
}
@Override
public void setClob(int parameterIndex, Clob x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setClob, new Object[] { parameterIndex, x }));
}
@Override
public void setArray(int i, Array x) throws SQLException {
parameterSettings.put(i, new ParameterContext(ParameterMethod.setArray, new Object[] { i, x }));
}
@Override
public ResultSetMetaData getMetaData() throws SQLException {
throw new UnsupportedOperationException("getMetaData");
}
@Override
public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setDate2, new Object[] { parameterIndex, x, cal }));
}
@Override
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setTime2, new Object[] { parameterIndex, x, cal }));
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setTimestamp2, new Object[] { parameterIndex, x, cal }));
}
@Override
public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException {
parameterSettings.put(paramIndex, new ParameterContext(ParameterMethod.setNull2, new Object[] { paramIndex, sqlType, typeName }));
}
@Override
public void setURL(int parameterIndex, URL x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setURL, new Object[] { parameterIndex, x }));
}
@Override
public ParameterMetaData getParameterMetaData() throws SQLException {
throw new UnsupportedOperationException("getParameterMetaData");
}
@Override
public void setRowId(int parameterIndex, RowId x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setRowId, new Object[] { parameterIndex, x }));
}
@Override
public void setNString(int parameterIndex, String value) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setNString, new Object[] { parameterIndex, value }));
}
@Override
public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setNCharacterStream, new Object[] { parameterIndex, value,length }));
}
@Override
public void setNClob(int parameterIndex, NClob value) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setNClob, new Object[] { parameterIndex, value }));
}
@Override
public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setClob2, new Object[] { parameterIndex, reader,length }));
}
@Override
public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setBlob2, new Object[] { parameterIndex, inputStream,length }));
}
@Override
public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setNClob2, new Object[] { parameterIndex, reader,length }));
}
@Override
public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
throw new SQLException("not support exception");
}
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setObject3, new Object[] { parameterIndex, x,
targetSqlType, scale }));
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setAsciiStream2, new Object[] { parameterIndex, x,
length }));
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setBinaryStream2, new Object[] { parameterIndex, x,
length }));
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setCharacterStream2, new Object[] { parameterIndex, reader,
length }));
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setAsciiStream3, new Object[] { parameterIndex, x }));
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setBinaryStream3, new Object[] { parameterIndex, x }));
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setCharacterStream3, new Object[] { parameterIndex, reader }));
}
@Override
public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setNCharacterStream2, new Object[] { parameterIndex, value }));
}
@Override
public void setClob(int parameterIndex, Reader reader) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setClob3, new Object[] { parameterIndex, reader }));
}
@Override
public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setBlob3, new Object[] { parameterIndex, inputStream }));
}
@Override
public void setNClob(int parameterIndex, Reader reader) throws SQLException {
parameterSettings.put(parameterIndex, new ParameterContext(ParameterMethod.setNClob3, new Object[] { parameterIndex, reader }));
}
/*---------------------- jdbc规范 ---------------------*/
@Override
protected ResultSet executeQueryOnConnection(Connection conn, String sql) throws SQLException {
PreparedStatement ps = createPreparedStatementInternal(conn, sql);
Parameters.setParameters(ps, parameterSettings);
this.currentResultSet = ps.executeQuery();
return this.currentResultSet;
}
private int[] executeBatchOnConnection(Connection conn) throws SQLException {
PreparedStatement ps = createPreparedStatementInternal(conn, sql);
for (Map parameterSettings : pstArgs) {
setBatchParameters(ps, parameterSettings.values());
ps.addBatch();
}
return ps.executeBatch();
}
private static void setBatchParameters(PreparedStatement ps, Collection batchedParameters) throws SQLException {
for (ParameterContext context : batchedParameters) {
Parameters.parameterHandlers.get(context.getParameterMethod()).setParameter(ps, context.getArgs());
}
}
private int executeUpdateOnConnection(Connection conn) throws SQLException {
PreparedStatement ps = createPreparedStatementInternal(conn, sql);
Parameters.setParameters(ps, parameterSettings);
return ps.executeUpdate();
}
private DataSourceTryer executeUpdateTryer = new DataSourceTryer() {
@SuppressWarnings("unchecked")
@Override
public Integer onSQLException(List exceptions, ExceptionSorter exceptionSorter, Object... args) throws SQLException {
ExceptionUtils.throwSQLException(exceptions, null, Collections.EMPTY_LIST);
return null;
}
@Override
public Integer tryOnDataSource(DataSourceWrapper dsw, Object... args) throws SQLException {
Connection conn = SGroupPreparedStatement.this.sGroupConnection.createNewConnection(dsw, false);
return executeUpdateOnConnection(conn);
}
};
private DataSourceTryer executeBatchTryer = new DataSourceTryer() {
@SuppressWarnings("unchecked")
@Override
public int[] onSQLException(List exceptions, ExceptionSorter exceptionSorter, Object... args) throws SQLException {
ExceptionUtils.throwSQLException(exceptions, null, Collections.EMPTY_LIST);
return null;
}
@Override
public int[] tryOnDataSource(DataSourceWrapper dsw, Object... args) throws SQLException {
Connection conn = sGroupConnection.createNewConnection(dsw, false);
return executeBatchOnConnection(conn);
}
};
private PreparedStatement createPreparedStatementInternal(Connection conn, String sql) throws SQLException {
PreparedStatement ps;
if (autoGeneratedKeys != -1) {
ps = conn.prepareStatement(sql, autoGeneratedKeys);
} else if (columnIndexes != null) {
ps = conn.prepareStatement(sql, columnIndexes);
} else if (columnNames != null) {
ps = conn.prepareStatement(sql, columnNames);
} else {
int resultSetHoldability = this.resultSetHoldability;
if (resultSetHoldability == -1)
resultSetHoldability = conn.getHoldability();
ps = conn.prepareStatement(sql, this.resultSetType, this.resultSetConcurrency, resultSetHoldability);
}
setBaseStatement(ps);
ps.setQueryTimeout(queryTimeout);
ps.setFetchSize(fetchSize);
ps.setMaxRows(maxRows);
return ps;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public int getAutoGeneratedKeys() {
return autoGeneratedKeys;
}
public void setAutoGeneratedKeys(int autoGeneratedKeys) {
this.autoGeneratedKeys = autoGeneratedKeys;
}
public int[] getColumnIndexes() {
return columnIndexes;
}
public void setColumnIndexes(int[] columnIndexes) {
this.columnIndexes = columnIndexes;
}
public String[] getColumnNames() {
return columnNames;
}
public void setColumnNames(String[] columnNames) {
this.columnNames = columnNames;
}
public List> getPstArgs() {
return pstArgs;
}
public void setPstArgs(List> pstArgs) {
this.pstArgs = pstArgs;
}
public Map getParameterSettings() {
return parameterSettings;
}
public void setParameterSettings(Map parameterSettings) {
this.parameterSettings = parameterSettings;
}
public DataSourceTryer getExecuteUpdateTryer() {
return executeUpdateTryer;
}
public void setExecuteUpdateTryer(DataSourceTryer executeUpdateTryer) {
this.executeUpdateTryer = executeUpdateTryer;
}
}