com.kukababy.plus.dao.BasePlus Maven / Gradle / Ivy
The newest version!
/**
*
*/
package com.kukababy.plus.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.script.ScriptEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.kukababy.plus.exception.PlusRuntimeException;
import com.kukababy.plus.pojo.SqlPo;
import com.kukababy.plus.pojo.SqlCfg;
import com.kukababy.plus.pojo.SqlVal;
import com.kukababy.plus.utils.Constant;
import com.kukababy.plus.utils.SqlUtil;
/**
*
* 描述:
*
*
* @author [email protected]
* @date 2019年3月5日 下午10:51:13
*/
public class BasePlus {
private static final Logger log = LoggerFactory.getLogger(BasePlus.class);
protected SqlCfg sqlCfg;
protected ScriptEngine engine;
public BasePlus(SqlCfg sqlCfg, ScriptEngine engine) {
this.sqlCfg = sqlCfg;
this.engine = engine;
this.sqlCfg.setDbType(this.sqlCfg.getDbType().toLowerCase());
}
protected void debugLog(String execSql,Object execVals[]){
if (log.isDebugEnabled()) {
Map map = new HashMap();
map.put("execSql", execSql);
map.put("execVals", execVals);
log.debug(Constant.LogPre + JSON.toJSONString(map, true));
}
}
protected String getInsertSql(SqlPo sqlCols) {
final StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("insert into " + sqlCols.getTableName() + " (");
List allCols = new ArrayList();
if (sqlCols.getStrategy().equals(Constant.AUTO)) {// 前端提供主键,增加插入主键字段
allCols.add(sqlCols.getKey());
}
for (String col : sqlCols.getCols()) {
allCols.add(col);
}
for (int i = 0; i < allCols.size(); i++) {
if (i == allCols.size() - 1) {
sqlBuilder.append(allCols.get(i) + ") values(");
} else {
sqlBuilder.append(allCols.get(i) + ",");
}
}
for (int i = 0; i < allCols.size(); i++) {
if (i == allCols.size() - 1) {
sqlBuilder.append("?)");
} else {
sqlBuilder.append("?,");
}
}
String sql = sqlBuilder.toString();
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + sql);
}
return sql;
}
protected String getUpdateSql(SqlPo sqlPo) {
final StringBuilder sqlBuilder = new StringBuilder();
List updateCols = new ArrayList();
for (String col : sqlPo.getCols()) {
updateCols.add(col);
}
String sql = getUpdateSql(sqlPo.getTableName(), sqlPo.getKey(), updateCols);
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + sql);
}
return sql;
}
protected Map getUpdateSqlAndVals(SqlPo sqlPo, Map plusMap) {
List updateCols = new ArrayList();// 实际需要更新的列
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy