📄 engineimpl.java
字号:
package org.speedframework.engine.impl;
import org.apache.log4j.Logger;
import org.speedframework.engine.Engine;
import java.sql.Connection;
import java.io.Serializable;
import java.lang.reflect.Array;
import org.speedframework.engine.Query;
import java.sql.SQLException;
import org.speedframework.exception.SpeedException;
import org.speedframework.util.SQLHelper;
import java.util.List;
import java.util.Map;
import org.speedframework.util.TableUtils;
import org.speedframework.engine.Execute;
public class EngineImpl implements Engine {
private Query query;
private Connection con;
private Execute execute;
private boolean useCache = true;
private String dbtype;
private static final Logger log = Logger.getLogger(EngineImpl.class);
public EngineImpl(Connection con) throws SpeedException {
this.con = con;
this.dbtype = SQLHelper.getDataBaseType(con);
execute = new ExecuteImpl(con,this.dbtype);
log.debug("This database is " + dbtype);
}
/**
* delete
*
* @param con
* Connection
* @param pojo
* Object
*/
public void delete(Object pojo) throws SpeedException {
try {
String sql = SQLHelper.getDeleteSQL(con, dbtype, pojo.getClass());
execute.delete(sql, pojo, useCache);
} catch (Exception e) {
throw new SpeedException(e);
}
}
/**
* load
*
* @param con
* Connection
* @param pojo_class
* Class
* @param id
* Serializable
*/
public Object load(Class pojo_class, Serializable id) throws SpeedException {
Object object = null;
try {
String sql = SQLHelper.getSelectSQLByID(con, dbtype, pojo_class);
String[] param = this.getSerializableID(id);
// String[] param = new String[] { id.toString() };
List list = execute.select(sql, param, pojo_class, useCache);
if (list.size() > 0) {
object = list.get(0);
}
} catch (Exception e) {
throw new SpeedException(e);
}
return object;
}
/**
* save
*
* @param con
* Connection
* @param pojo
* Object
*/
public Object save(Object pojo) throws SpeedException {
Object r_obj = null;
try {
String sql = SQLHelper.getInsertSQL(con, dbtype, pojo);
List keys = execute.insert(sql, pojo, useCache);
r_obj = TableUtils.getReturnData(con, dbtype, pojo, keys);
} catch (Exception e) {
throw new SpeedException(e);
}
return r_obj;
}
/**
* update
*
* @param con
* Connection
* @param pojo
* Object
*/
public Object update(Object pojo) throws SpeedException {
try {
String sql = SQLHelper.getUpdateSQL(con, dbtype, pojo.getClass());
execute.update(sql, pojo, useCache);
} catch (Exception e) {
throw new SpeedException(e);
}
return pojo;
}
/**
* getConnection
*
* @param commit
* boolean
* @return Connection
*/
public Connection getConnection(boolean commit) throws SpeedException {
try {
if (con.getAutoCommit() != commit) {
con.setAutoCommit(commit);
}
} catch (SQLException e) {
throw new SpeedException(e);
}
return con;
}
/**
* commit
*/
public void commit() throws SpeedException {
try {
con.commit();
} catch (SQLException e) {
throw new SpeedException(e);
}
}
/**
* rollback
*/
public void rollback() throws SpeedException {
try {
con.rollback();
} catch (SQLException e) {
throw new SpeedException(e);
}
}
/**
* closeEngine
*/
public void closeEngine() throws SpeedException {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
throw new SpeedException(e);
}
}
/**
* getQuery
*/
public Query getQuery() {
query = new QueryImpl(con,this.dbtype);
return query;
}
/**
* setUserCache
*
* @param use
* boolean
*/
public void setUseCacheStatus(boolean status) {
this.useCache = status;
}
/**
*
* @return boolean
*/
public boolean getUseCacheStatus() {
return this.useCache;
}
/**
*
* @param SQL
* String
* @param params
* Object[][]
* @throws Exception
*/
public void executeBatch(String SQL, Object[][] params)
throws SpeedException {
try {
execute.executeBatch(SQL, params, useCache);
} catch (Exception e) {
throw new SpeedException(e);
}
}
/**
* executeSpecialSQL
*
* @param SQL
* String
* @param parmas
* Object[]
*/
public void executeSpecialSQL(String SQL, Object[] params)
throws SpeedException {
try {
SQL = SQL.toLowerCase();
if (SQL.indexOf("insert") != -1) {
execute.insert(SQL, params, useCache);
}
if (SQL.indexOf("update") != -1) {
execute.update(SQL, params, useCache);
}
if (SQL.indexOf("delete") != -1) {
execute.delete(SQL, params, useCache);
}
} catch (Exception ex) {
throw new SpeedException(ex);
}
}
/**
* executeCallableSQL
*
* @param SQL
* String
* @param params
* Object[]
* @throws SpeedException
* @return List
*/
public List executeProcedureCall(String SQL, Object[] params, Class voclass)
throws SpeedException {
List relist = null;
try {
relist = execute.executeCall(SQL, params, voclass, useCache);
} catch (Exception ex) {
throw new SpeedException(ex);
}
return relist;
}
/**
* executeCallSQL
*
* @param SQL
* String
* @param params
* Object[]
*/
public void executeProcedureCall(String SQL, Object[] params)
throws SpeedException {
try {
execute.executeCall(SQL, params, null, useCache);
} catch (Exception ex) {
throw new SpeedException(ex);
}
}
public String getSequences(String SeqName) throws SpeedException {
String seq = null;
try {
String dbtype = SQLHelper.getDataBaseType(con);
String sql = SQLHelper.getSequencesSQL(dbtype,SeqName);
String[] param = new String[] {};
List list = execute.select(sql, param, null, useCache);
if (list.size() > 0) {
Map map = (Map) list.get(0);
seq = map.get("nextval").toString();
}
log.debug(SeqName + " is " + seq);
} catch (SpeedException ex) {
throw new SpeedException(ex);
}
return seq;
}
private String[] getSerializableID(Serializable id) {
// log.debug(""+id.getClass().isArray());
String[] param = null;
if (id.getClass().isArray()) {
int length = Array.getLength(id);
// log.debug("" + length);
param=new String[length];
for(int i=0;i<length;i++){
param[i]=(String)Array.get(id,i);
}
} else if (!id.getClass().isArray()) {
param = new String[] { id.toString() };
}
// String[] param = new String[] { id.toString() };
return param;
}
public Object executeFunctionCall(String SQL, Object[] params,int returnType) throws SpeedException {
try {
return execute.executeFunction(SQL, params,returnType, null, useCache);
} catch (Exception ex) {
throw new SpeedException(ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -