io.github.jinghui70.rainbow.dbaccess.object.ObjectSql Maven / Gradle / Ivy
The newest version!
package io.github.jinghui70.rainbow.dbaccess.object;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.CaseInsensitiveMap;
import io.github.jinghui70.rainbow.dbaccess.*;
import io.github.jinghui70.rainbow.dbaccess.fieldmapper.FieldValue;
import io.github.jinghui70.rainbow.utils.tree.ITreeNode;
import org.springframework.jdbc.core.RowMapper;
import java.util.*;
import static io.github.jinghui70.rainbow.dbaccess.DbaUtil.INSERT_INTO;
@SuppressWarnings("unused")
public class ObjectSql extends GeneralSql> {
private final Class queryClass;
private final LinkedHashMap propMap;
private final BeanMapper mapper;
public ObjectSql(Dba dba, Class queryClass) {
super(dba);
this.queryClass = queryClass;
this.propMap = PropInfoCache.get(queryClass);
this.mapper = new BeanMapper<>(queryClass, propMap);
}
public T queryForObject() {
return queryForObject(mapper);
}
public Optional queryForObjectOptional() {
return queryForObjectOptional(mapper);
}
public List queryForList() {
return queryForList(mapper);
}
public PageData pageQuery(int pageNo, int pageSize) {
return pageQuery(mapper, pageNo, pageSize);
}
@SuppressWarnings({"unchecked", "rawtypes"})
public List queryForTree() {
Assert.isAssignable(ITreeNode.class, queryClass);
return (List) queryForTree((RowMapper extends ITreeNode>) mapper);
}
public Map queryToMap(ResultSetFunction keyFunc) {
return queryToMap(keyFunc, mapper);
}
public Map> queryToGroup(ResultSetFunction keyFunc) {
return queryToGroup(keyFunc, mapper);
}
/**
* 拼接 SQL 插入语句的插入字段部分
*
* @return this
*/
public ObjectSql insertInto() {
return append(INSERT_INTO)
.append(DbaUtil.tableName(queryClass))
.append("(").join(propMap.keySet()).append(")");
}
/**
* 拼接 SQL 选择字段部分
*
* @param replaceMap 字段替换为具体值的 Map,key 是字段名,Value 是要替换的值
* @return this
*/
public ObjectSql selectFields(Map replaceMap) {
replaceMap = new CaseInsensitiveMap<>(replaceMap);
append("SELECT ");
for (String key : propMap.keySet()) {
if (replaceMap.containsKey(key))
append("?").addParam(replaceMap.get(key)).append(" AS ").append(key);
else
append(key);
appendTempComma();
}
return clearTemp().from(DbaUtil.tableName(queryClass));
}
@Override
public ObjectSql set(String field, Object value) {
if (value != null) {
PropInfo propInfo = this.propMap.get(field);
if (propInfo != null && propInfo.getMapper() != null) {
value = new FieldValue(value, propInfo.getMapper());
}
}
return super.set(field, value);
}
}