All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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 updateVals = new ArrayList(); Object primaryKeyVal = null;// plusMap更新对象是否有主键 for (Map.Entry entry : plusMap.entrySet()) { String key = entry.getKey();// map里的列名,可能是字段名 String colName = SqlUtil.getColNameByVar(key, sqlPo.getCol2VarMap()); if (colName.equals(sqlPo.getKey())) {// 主键的列名 primaryKeyVal = entry.getValue();// 主键的值 continue; } for (String col : sqlPo.getCols()) { if (col.equals(colName)) {// 属于表的列才有效 updateCols.add(col); updateVals.add(entry.getValue()); } } } if (primaryKeyVal == null) { throw new PlusRuntimeException("更新对象没有主键或主键值为null"); } updateVals.add(primaryKeyVal); String sql = getUpdateSql(sqlPo.getTableName(), sqlPo.getKey(), updateCols); if (log.isDebugEnabled()) { Map _map = new HashMap(); _map.put("execSql", sql); _map.put("execVals", updateVals); log.debug(Constant.LogPre + JSON.toJSONString(_map, true)); } Map resMap = new HashMap(); resMap.put("updateSql", sql); resMap.put("updateVals", updateVals); return resMap; } private String getUpdateSql(String tableName, String primaryKey, List cols) { final StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("update " + tableName + " set "); for (int i = 0; i < cols.size(); i++) { if (i == cols.size() - 1) { sqlBuilder.append(cols.get(i) + " = ?"); } else { sqlBuilder.append(cols.get(i) + " = ?" + ","); } } sqlBuilder.append(" where " + primaryKey + " = ?"); return sqlBuilder.toString(); } protected Object[] getUpdateVals(final SqlPo sqlPo, final SqlVal sqlVal) { List allVals = new ArrayList(); for (Object val : sqlVal.getVals()) { allVals.add(val); } allVals.add(sqlVal.getKeyVal()); Object vals[] = allVals.toArray(); if (log.isDebugEnabled()) { log.debug(Constant.LogPre + JSON.toJSONString(allVals, true)); } return vals; } protected Object[] getInsertVals(final SqlPo sqlPo, final SqlVal sqlVal) { List allVals = new ArrayList(); if (sqlPo.getStrategy().equals(Constant.AUTO)) {// 前端提供主键,增加插入主键字段 allVals.add(sqlVal.getKeyVal()); } for (Object val : sqlVal.getVals()) { allVals.add(val); } return allVals.toArray(); } protected SqlPo getSqlPo(Class entityClass, boolean camel) { SqlPo sqlPo = SqlUtil.getSqlPo(entityClass, camel); if (log.isDebugEnabled()) { log.debug(Constant.LogPre + JSON.toJSONString(sqlPo, true)); } return sqlPo; } protected SqlVal getSqlVal(SqlPo sqlPo, Object entity) { SqlVal sqlVal = SqlUtil.getSqlVal(sqlPo, entity); if (log.isDebugEnabled()) { log.debug(Constant.LogPre + JSON.toJSONString(sqlVal, true)); } return sqlVal; } }