io.github.wycst.wast.jdbc.executer.EntityExecuter Maven / Gradle / Ivy
package io.github.wycst.wast.jdbc.executer;
import io.github.wycst.wast.common.beans.GregorianDate;
import io.github.wycst.wast.common.utils.ObjectUtils;
import io.github.wycst.wast.common.utils.StringUtils;
import io.github.wycst.wast.jdbc.exception.EntityException;
import io.github.wycst.wast.jdbc.exception.OqlParematerException;
import io.github.wycst.wast.jdbc.exception.SqlExecuteException;
import io.github.wycst.wast.jdbc.query.page.Page;
import io.github.wycst.wast.jdbc.query.sql.Sql;
import io.github.wycst.wast.jdbc.util.StreamCursor;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.*;
/**
* 实体类执行器
*
* @Author wangyunchao
* @Date 2022/12/3 12:24
*/
public final class EntityExecuter implements OqlExecuter {
private final DefaultSqlExecuter sqlExecuter;
EntityExecuter(DefaultSqlExecuter sqlExecuter) {
this.sqlExecuter = sqlExecuter;
}
@Override
public DefaultSqlExecuter getSqlExecuter() {
return sqlExecuter;
}
@Override
public E get(Class entityCls, Serializable id) {
return get(entityCls, id, false);
}
@Override
public E get(Class entityCls, Serializable id, boolean fetch) {
id.getClass();
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
E result = entitySqlMapping.getEntityHandler().getById(sqlExecuter, entityCls, id); // getById(entitySqlMapping, entityCls, id);
if (fetch && result != null) {
this.handleFetch(entitySqlMapping, result);
}
return result;
}
@Override
public List queryAll(Class cls) {
return queryBy(cls, (Map) null);
}
@Override
public E queryOne(Class entityCls, Map params) {
return executeQueryOne(entityCls, params);
}
@Override
public E queryOne(Class entityCls, E params) {
return executeQueryOne(entityCls, params);
}
@Override
public long queryCount(Class entityCls) {
return queryCount(entityCls, (Map) null);
}
@Override
public long queryCount(Class entityCls, Map params) {
return executeQueryCount(entityCls, params);
}
@Override
public long queryCount(Class entityCls, E params) {
return executeQueryCount(entityCls, params);
}
public long queryCount(Class cls, OqlQuery query, Object params) {
checkEntityClass(cls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(cls);
Sql countSqlObject = entitySqlMapping.getCountSqlObject(query, params);
return sqlExecuter.queryValue(countSqlObject.getFormalSql(), long.class, countSqlObject.getParamValues());
}
@Override
public E queryUnique(Class entityCls, Map params) {
return executeQueryUnique(entityCls, params);
}
@Override
public E queryUnique(Class entityCls, E params) {
return executeQueryUnique(entityCls, params);
}
@Override
public List queryList(Class entityCls, OqlQuery query, Object params) {
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
Sql sqlObject = entitySqlMapping.getSelectSqlObject(query, params);
return sqlExecuter.queryList(sqlObject.getFormalSql(), entityCls, sqlObject.getParamValues());
}
@Override
public List queryList(Class cls, OqlQuery query) {
return queryList(cls, query, new HashMap());
}
@Override
public StreamCursor queryStreamBy(Class entityCls, Map params) {
return executeQueryStreamBy(entityCls, params);
}
@Override
public StreamCursor queryStreamBy(Class entityCls, E params) {
return executeQueryStreamBy(entityCls, params);
}
@Override
public StreamCursor queryStream(Class entityCls, OqlQuery query, Object params) {
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
Sql sqlObject = entitySqlMapping.getSelectSqlObject(query, params);
return sqlExecuter.queryStream(sqlObject.getFormalSql(), entityCls, sqlObject.getParamValues());
}
@Override
public List queryBy(Class entityCls, Map params) {
return executeQueryBy(entityCls, params);
}
@Override
public List queryBy(Class entityCls, E params) {
return executeQueryBy(entityCls, params);
}
@Override
public List queryByIds(Class entityCls, List extends Serializable> ids) {
if (ids == null || ids.isEmpty()) {
return new ArrayList();
}
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
// select * from t where id in(?, ?, ?)
String selectInSql = entitySqlMapping.getSelectSqlByIds(ids);
return sqlExecuter.queryList(selectInSql, entityCls, ids.toArray());
}
@Override
public List queryByIds(Class entityCls, Serializable... ids) {
if (ids.length == 0) {
return new ArrayList();
}
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
// select * from t where id in(?, ?, ?)
Collection list = Arrays.asList(ids);
String selectInSql = entitySqlMapping.getSelectSqlByIds(list);
return sqlExecuter.queryList(selectInSql, entityCls, ids);
}
@Override
public Page queryPage(Page page, OqlQuery query, Object params) {
Class entityCls = page.actualType();
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
Sql sqlObject = entitySqlMapping.getSelectSqlObject(query, params, true);
this.executeQueryPage(page, entityCls, sqlObject);
return page;
}
@Override
public Page queryPage(Page page) {
return queryPage(page, (Map) null);
}
@Override
public Page queryPage(Page page, Map params) {
return executeQueryPage(page, params);
}
@Override
public Page queryPage(Page page, E params) {
return executeQueryPage(page, params);
}
@Override
public Serializable insert(E entity) {
Class entityCls = entity.getClass();
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
Sql sqlObject = entitySqlMapping.getInsertSqlObject(entity);
return sqlExecuter.insert(sqlObject.getFormalSql(), true, sqlObject.getParamValues());
}
@Override
public void insertList(List entityList) {
if (entityList.size() == 0) {
return;
}
Class entityCls = entityList.get(0).getClass();
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
if(entitySqlMapping.isUsePlaceholderOnInsert()) {
if(isSupportBatchInsert()) {
Sql batchInsertSqlObject = entitySqlMapping.getBatchInsertSqlObject(entityList);
sqlExecuter.update(batchInsertSqlObject.getFormalSql(), batchInsertSqlObject.getParamValues());
} else {
for (E e : entityList) {
Sql sqlObject = entitySqlMapping.getInsertSqlObject(e);
sqlExecuter.insert(sqlObject.getFormalSql(), true, sqlObject.getParamValues());
}
}
} else {
Sql sqlObject = entitySqlMapping.getInsertSqlObjectList(entityList);
sqlExecuter.updateCollection(sqlObject.getFormalSql(), sqlObject.getParamValuesList());
}
}
@Override
public int mysqlBatchInsert(List entityList) {
if (!isSupportBatchInsert()) {
throw new SqlExecuteException("当前数据库不支持批量插入");
}
if (entityList.size() == 0) {
return 0;
}
Class entityCls = entityList.get(0).getClass();
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
Sql batchInsertSqlObject = entitySqlMapping.getBatchInsertSqlObject(entityList);
return sqlExecuter.update(batchInsertSqlObject.getFormalSql(), batchInsertSqlObject.getParamValues());
}
@Override
public int update(E entity) {
Class entityCls = entity.getClass();
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
return entitySqlMapping.getEntityHandler().updateEntity(sqlExecuter, entity);
}
@Override
public int updateBy(Class entityCls, OqlQuery query, E params, String... fields) {
return handleUpdateByParams(entityCls, query, params, fields);
}
@Override
public int updateBy(Class entityCls, OqlQuery query, Map params, String... fields) {
return handleUpdateByParams(entityCls, query, params, fields);
}
int handleUpdateByParams(Class entityCls, OqlQuery query, Object params, String... fields) {
if(fields.length == 0) return 0;
if(params == null) {
throw new SqlExecuteException("params is null");
}
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
String sqlStringFormat = getSqlStringFormat(SqlFunctionType.UPDATE_BY_PARAMS);
Sql sqlObject = entitySqlMapping.getUpdateSqlObject(sqlStringFormat, query, params, fields);
return sqlExecuter.update(sqlObject.getFormalSql(), sqlObject.getParamValues());
}
@Override
public int updateFields(E e, String... fields) {
return updateFields(e, Arrays.asList(fields), false);
}
@Override
public int updateFields(E e, boolean isExclude, String... fields) {
return updateFields(e, Arrays.asList(fields), isExclude);
}
@Override
public int updateFields(E entity, List fields) {
return updateFields(entity, fields, false);
}
@Override
public int updateFields(E entity, List fields, boolean isExclude) {
Class entityCls = entity.getClass();
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
String sqlStringFormat = getSqlStringFormat(SqlFunctionType.UPDATE_BY_ID);
Sql sqlObject = entitySqlMapping.getUpdateSqlObject(sqlStringFormat, entity, fields, isExclude);
if (sqlObject == null) {
throw new OqlParematerException("配置错误:" + entityCls + "可能没有定义@Id,请检查配置");
}
try {
return sqlExecuter.update(sqlObject.getFormalSql(), sqlObject.getParamValues());
} finally {
entitySqlMapping.getEntityHandler().afterUpdate(sqlExecuter, entity);
}
}
@Override
public int deleteAll(Class entityCls) {
return deleteBy(entityCls, (Map) null);
}
@Override
public int delete(Class entityCls, Serializable id) {
return delete(entityCls, id, false);
}
@Override
public int delete(Class entityCls, Serializable id, boolean cascade) {
id.getClass();
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
if (cascade) {
// handle cascadeDelete
this.executeCascadeDelete(entitySqlMapping, id);
}
return entitySqlMapping.getEntityHandler().deleteById(sqlExecuter, entityCls, id);
}
@Override
public int deleteList(List entityList) {
if (entityList == null) {
return 0;
}
int influenceRows = 0;
Class> entityCls = entityList.get(0).getClass();
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
String sqlStringFormat = getSqlStringFormat(SqlFunctionType.DELETE_BY_ID);
String deleteSql = entitySqlMapping.getDeleteSql(sqlStringFormat);
if (deleteSql == null) {
throw new OqlParematerException("配置错误:" + entityCls + "可能没有定义@Id,请检查配置");
}
for (Object entity : entityList) {
Serializable id = entitySqlMapping.getId(entity);
influenceRows += sqlExecuter.update(deleteSql, id);
}
if (influenceRows > 0) {
entitySqlMapping.getEntityHandler().afterBatchDelete();
}
return influenceRows;
}
@Override
public int deleteByIds(Class entityCls, List extends Serializable> ids) {
if (ids == null || ids.size() == 0) {
return 0;
}
checkEntityClass(entityCls);
EntitySqlMapping entitySqlMapping = getEntitySqlMapping(entityCls);
String sqlStringFormat = getSqlStringFormat(SqlFunctionType.DELETE_BY_ID);
String deleteSql = entitySqlMapping.getDeleteSql(sqlStringFormat);
if (deleteSql == null) {
throw new OqlParematerException("配置错误:" + entityCls + "可能没有定义@Id,请检查配置");
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy