📄 executorimpl.java
字号:
package org.speedframework.db.executor;
import org.apache.log4j.Logger;
import org.speedframework.exception.SpeedFrameworkException;
import org.speedframework.sql.*;
import java.sql.*;
import java.util.List;
import java.util.Map;
/**
* Class ExecutorImpl
*
* @author <a href="mailto:santafeng@gmail.com"> lizf </a>
* @version $Revision:1.0.0, $Date: 2007-10-30 上午08:11:35 $
*/
public class ExecutorImpl implements IExecutor {
private static final Logger log = Logger.getLogger(ExecutorImpl.class);
private Connection connection;
private ISQLBuilderFactory factory;
private DBExecutor psHelper;
public void setFactory(ISQLBuilderFactory factory) {
this.factory = factory;
}
public String getDateBaseVersion() throws SpeedFrameworkException {
String name = null;
try {
DatabaseMetaData md = connection.getMetaData();
name = md.getDatabaseProductName().toLowerCase();
} catch (Exception ex) {
throw new SpeedFrameworkException("getDateBaseVersion error ", ex);
}
return name;
}
public void setConnection(Connection connection)
throws SpeedFrameworkException {
this.connection = connection;
}
public void closeConnection() throws SpeedFrameworkException {
try {
if (this.connection != null) {
this.connection.close();
}
} catch (Exception e) {
throw new SpeedFrameworkException(
"Connection closeConnection error ", e);
}
}
public void commitConnection() throws SpeedFrameworkException {
try {
if (this.connection != null) {
this.connection.commit();
}
} catch (Exception e) {
throw new SpeedFrameworkException(
"Connection commitConnection error ", e);
}
}
public void rollbackConnection() throws SpeedFrameworkException {
try {
if (this.connection != null) {
log.debug("It is rollback...");
this.connection.rollback();
}
} catch (Exception e) {
throw new SpeedFrameworkException(
"Connection rollbackConnection error ", e);
}
}
public void delete(String SQL, String[][] sourceType, Object[] param)
throws SpeedFrameworkException {
PreparedStatement ps = null;
try {
log.debug(SQL);
psHelper.execute(ps, SQL, sourceType, param, false);
} catch (Exception e) {
throw new SpeedFrameworkException(e);
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException ex) {
throw new SpeedFrameworkException(ex);
}
}
}
}
public void delete(String SQL, String[][] columnType, Object entity)
throws SpeedFrameworkException {
IDeleteSQL deleteSQL = this.factory.getDelSql();
deleteSQL.setTableObject(entity);
List list = deleteSQL.getParam(SQL);
Object[] param = deleteSQL.getParamValue(list, entity);
this.delete(SQL, columnType, param);
}
public void executeBatch(String SQL, Object[][] params)
throws SpeedFrameworkException {
try {
this.psHelper.executeBatch(SQL, params);
} catch (Exception e) {
throw new SpeedFrameworkException(e);
}
}
public void executeBatch(String SQL, List list)
throws SpeedFrameworkException {
}
public List executeCall(String SQL, Object[] params, Class voclass)
throws SpeedFrameworkException {
List relist = null;
CallableStatement proc = null;
ResultSet rs = null;
SQL = SQL.toLowerCase();
try {
log.debug(SQL);
psHelper.setExecuteCall(proc, SQL, params);
if (voclass != null) {
rs = (ResultSet) proc.getObject(1);
relist = RowsUtils.copyRows(rs, voclass);
}
} catch (Exception ex) {
throw new SpeedFrameworkException(ex);
} finally {
try {
if (rs != null) {
rs.close();
}
if (proc != null) {
proc.close();
}
} catch (SQLException ex) {
throw new SpeedFrameworkException(ex);
}
}
return relist;
}
public Object executeFunction(String SQL, Object[] params, int returnType,
Class voclass) throws SpeedFrameworkException {
CallableStatement cstmt = null;
try {
log.debug(SQL);
return psHelper.setFunctionCall(cstmt, SQL, params, returnType);
} catch (Exception ex) {
throw new SpeedFrameworkException(ex);
} finally {
try {
if (cstmt != null) {
cstmt.close();
}
} catch (SQLException e) {
throw new SpeedFrameworkException(e);
}
}
}
public int getDataCount(String SQL, Object[] params)
throws SpeedFrameworkException {
List rlist = this.select(SQL, params, null);
Map map = (Map) rlist.get(0);
return Integer.parseInt(map.get("count_").toString());
}
public int insert(String SQL, String[][] sourceType, Object[] param)
throws SpeedFrameworkException {
PreparedStatement ps = null;
int type_ = 0;
try {
log.debug(SQL);
type_ = psHelper.execute(ps, SQL, sourceType, param, false);
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new SpeedFrameworkException(e);
}
return type_;
}
public int insert(String SQL, String[][] columnType, Object entity)
throws SpeedFrameworkException {
IInsertSQL asql = factory.getInsertSQL();
asql.setTableObject(entity);
List list = asql.getParam(SQL);
Object[] param = asql.getParamValue(list, entity);
return this.insert(SQL, columnType, param);
}
public List select(String SQL, Object[] parmvalue, Class voclass)
throws SpeedFrameworkException {
PreparedStatement ps = null;
ResultSet rs = null;
List relist = null;
try {
log.debug(SQL);
if (relist == null) {
ps = connection.prepareStatement(SQL);
rs = this.psHelper.setExecuteQuery(ps, parmvalue);
relist = RowsUtils.copyRows(rs, voclass);
}
// } catch (SpeedFrameworkException e) {
// throw e;
} catch (SQLException e) {
throw new SpeedFrameworkException(e);
} catch (Exception e) {
throw new SpeedFrameworkException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
} catch (SQLException ex) {
throw new SpeedFrameworkException(ex);
}
}
return relist;
}
public List select(String SQL, Object entity, Class voclass)
throws SpeedFrameworkException {
ISelectSQL isql = factory.getSelectSQL();
isql.setTableObject(entity);
List list = isql.getParam(SQL);
Object[] param = isql.getParamValue(list, entity);
return this.select(SQL, param, voclass);
}
public List select(String SQL, Class voclass)
throws SpeedFrameworkException {
return this.select(SQL, new Object[] {}, voclass);
}
public void update(String SQL, String[][] columnType, Object[] param)
throws SpeedFrameworkException {
PreparedStatement ps = null;
try {
log.debug(SQL);
if ((SQL == null) || (param.length == 0)) {
throw new Exception();
}
psHelper.execute(ps, SQL, columnType, param, true);
} catch (Exception e) {
throw new SpeedFrameworkException(e);
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException ex) {
throw new SpeedFrameworkException(ex);
}
}
}
}
public void update(String SQL, String[][] columnType, Object entity)
throws SpeedFrameworkException {
IUpdateSQL usql = factory.getUpdateSQL();
usql.setTableObject(entity);
List list = usql.getParam(SQL);
Object[] param = usql.getParamValue(list, entity);
this.update(SQL, columnType, param);
}
public void setPsHelper(DBExecutor psHelper) {
this.psHelper = psHelper;
this.psHelper.setCon(connection);
}
public Connection getConnection() throws SpeedFrameworkException {
return this.connection;
}
public void executeSpecial(String sql, Object[] param)
throws SpeedFrameworkException {
PreparedStatement ps = null;
try {
log.debug(sql);
// if (sql == null || param.length == 0) {
// throw new Exception();
// }
psHelper.executeSpecial(sql,param);
} catch (Exception e) {
throw new SpeedFrameworkException(e);
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException ex) {
throw new SpeedFrameworkException(ex);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -