Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.javaoffers.brief.modelhelper.parse.RealtimeSmartModelParse Maven / Gradle / Ivy
package com.javaoffers.brief.modelhelper.parse;
import com.javaoffers.brief.modelhelper.core.ConvertDelegate;
import com.javaoffers.brief.modelhelper.core.ConvertProxy;
import com.javaoffers.brief.modelhelper.core.ConvertRegisterSelectorDelegate;
import com.javaoffers.brief.modelhelper.exception.BaseException;
import com.javaoffers.brief.modelhelper.exception.ParseModelException;
import com.javaoffers.brief.modelhelper.jdbc.ResultSetExecutor;
import com.javaoffers.brief.modelhelper.util.HelperUtils;
import com.javaoffers.brief.modelhelper.util.Model;
import com.javaoffers.brief.modelhelper.utils.DBType;
import com.javaoffers.brief.modelhelper.utils.ModelFieldInfo;
import com.javaoffers.brief.modelhelper.utils.ModelFieldInfoPosition;
import com.javaoffers.brief.modelhelper.utils.ModelInfo;
import com.javaoffers.brief.modelhelper.utils.TableHelper;
import com.javaoffers.brief.modelhelper.utils.TableInfo;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
/**
* @Description: model解析器
* @Auther: create by cmj on 2023/07/21 19:59
*/
public class RealtimeSmartModelParse implements RealtimeModelParse {
private final static ConvertRegisterSelectorDelegate convert = ConvertRegisterSelectorDelegate.convert;
private final static ThreadLocal> tl = new ThreadLocal>();
private final static AtomicLong nextKey = new AtomicLong();
private final static String ROOT_KEY = "";
/**
* rs 不需要next(又上层进行next操作), 直接获取数据即可.
*
* @param clazz
* @param rs
* @param
*/
@Override
public List converterResultSet2ModelForJoinSelect(Class clazz, ResultSetExecutor rs) {
try {
return buildModelForJoinSelect(clazz, rs);
} catch (Exception e) {
e.printStackTrace();
throw new ParseModelException(e.getMessage());
}
}
@Override
public List converterResultSet2ModelForNormalSelect(Class clazz, ResultSetExecutor rs) {
return buildModelForNormalSelect(clazz, rs);
}
/**
* 用于原始数据集转换Model数据集
*
* @param clazz Model的Class对象
* @param rs 封装后的Model数据集
* @throws Exception
* @throws SecurityException
*/
private static List buildModelForNormalSelect(Class clazz, ResultSetExecutor rs) {
ArrayList result = new ArrayList<>();
List colNames = rs.getColNames();
ModelInfo modelInfo = TableHelper.getModelInfo(clazz);
List ones = modelInfo.getOnesCol(colNames);
//read next row
int size = ones.size();
while (rs.nextRow()) {
Object o = modelInfo.newC();
result.add((E) o);
buildDataForNormalSelect(rs, ones, o);
}
return result;
}
private static void buildDataForNormalSelect(ResultSetExecutor rs,
List ones,
E model
) {
for (ModelFieldInfoPosition one : ones) {
if (!one.isModelClass()) {
Object o = null;
if ((o = rs.getColValueByColPosition(one.getPosition())) != null) {
ModelFieldInfo mfi = one.getModelFieldInfo();
Object o1 = convert(mfi, o);
mfi.getSetter().setter(model, o1);
}
}
}
}
private static Object convert(ModelFieldInfo one, Object o) {
ConvertProxy convertProxy = one.getConvertProxy();
if (convertProxy == null) {
convertProxy = convert.choseConverter(one.getFieldGenericClass(), o, one.getField());
one.setConvertProxy(convertProxy);
}
Object o1 = convertProxy.convert(o);
return o1;
}
/**
* 用于原始数据集转换Model数据集
*
* @param clazz Model的Class对象
* @param rs 封装后的Model数据集
* @throws Exception
* @throws SecurityException
*/
private static List buildModelForJoinSelect(Class clazz, ResultSetExecutor rs) throws Exception {
Map tmpCache = new HashMap<>();//
ArrayList result = new ArrayList<>();
List colNames = rs.getColNames();
ModelInfo modelInfo = TableHelper.getModelInfo(clazz);
List ones = modelInfo.getOnesColWithOneModel(colNames);
List arrays = modelInfo.getArrays(colNames);
List list = modelInfo.getList(colNames);
List set = modelInfo.getSet(colNames);
List unique = modelInfo.getUniqueCol(colNames);
//read next row
while (rs.nextRow()) {
String keyStr = getUniqueKey(ROOT_KEY, rs, unique);
Object o = tmpCache.get(keyStr);
if (o == null) {
o = modelInfo.newC();
tmpCache.put(keyStr, o);
result.add((E) o);
buildData(keyStr,rs, tmpCache, ones, arrays, list, set, o, true);
} else {
buildData(keyStr, rs, tmpCache, ones, arrays, list, set, o, false);
}
}
return result;
}
private static String getUniqueKey(String parentKey, ResultSetExecutor rs, List unique) {
if(unique.size()==0){
return String.valueOf(nextKey.getAndIncrement());
}
StringBuilder key = new StringBuilder(parentKey);
unique.forEach(modelFieldInfoPosition -> {
int position = modelFieldInfoPosition.getPosition();
Object o = rs.getColValueByColPosition(position);
key.append(modelFieldInfoPosition.getModelFieldInfo().getAliasName());
key.append(":");
key.append(Objects.hashCode(o));
});
return key.toString();
}
@SuppressWarnings("unchecked")
private static void buildData(
String parentKey,
ResultSetExecutor rs,
Map tmpCache,
List ones,
List arrays,
List list_,
List set_,
E model,
boolean processNoneModelField
) throws Exception {
for (ModelFieldInfoPosition mif : ones) {
ModelFieldInfo one = mif.getModelFieldInfo();
if (one.isModelClass()) {
Class modelClassOfField = one.getModelClassOfField();
ModelInfo modelInfo = TableHelper.getModelInfo(modelClassOfField);
List colNames = rs.getColNames();
String uniqueKey = getUniqueKey(parentKey, rs, modelInfo.getUniqueCol(colNames));
Object o = tmpCache.get(uniqueKey);
if (o == null) {
o = modelInfo.newC();
tmpCache.put(uniqueKey, o);
one.getSetter().setter(model, o);
buildData(uniqueKey,rs, tmpCache, modelInfo.getOnesColWithOneModel(colNames), modelInfo.getArrays(colNames),
modelInfo.getList(colNames), modelInfo.getSet(colNames), o, true);
} else {
buildData(uniqueKey, rs, tmpCache, modelInfo.getOnesColWithOneModel(colNames), modelInfo.getArrays(colNames),
modelInfo.getList(colNames), modelInfo.getSet(colNames), o, false);
}
} else if (processNoneModelField) {
Object o = null;
if ((o = rs.getColValueByColPosition(mif.getPosition())) != null) {
o = convert(one, o);
one.getSetter().setter(model, o);
}
}
}
for (ModelFieldInfo arrayField : arrays) {
Class modelClassOfField = arrayField.getModelClassOfField();
ModelInfo modelInfo = TableHelper.getModelInfo(modelClassOfField);
List colNames = rs.getColNames();
String uniqueKey = getUniqueKey(parentKey, rs, modelInfo.getUniqueCol(colNames));
Object o = tmpCache.get(uniqueKey);
if (o == null) {
o = modelInfo.newC();
tmpCache.put(uniqueKey, o);
Object arrayObj = arrayField.getGetter().getter(model);
if (arrayObj == null) {
arrayObj = Array.newInstance(modelClassOfField, 1);
Array.set(arrayObj, 0, o);
arrayField.getSetter().setter(model, arrayObj);
} else {
int len = Array.getLength(arrayObj);
Object newArrayObj = Array.newInstance(modelClassOfField, len + 1);
System.arraycopy(arrayObj, 0, newArrayObj, 0, len);
Array.set(newArrayObj, len, o);
arrayField.getSetter().setter(model, newArrayObj);
}
buildData(uniqueKey, rs, tmpCache, modelInfo.getOnesColWithOneModel(colNames), modelInfo.getArrays(colNames),
modelInfo.getList(colNames), modelInfo.getSet(colNames), o, true);
} else {
buildData(uniqueKey, rs, tmpCache, modelInfo.getOnesColWithOneModel(colNames), modelInfo.getArrays(colNames),
modelInfo.getList(colNames), modelInfo.getSet(colNames), o, false);
}
}
for (ModelFieldInfo listField : list_) {
List listFieldValue = (List) listField.getGetter().getter(model);
if (listFieldValue == null) {
listFieldValue = (List) listField.getNewc();
listField.getSetter().setter(model, listFieldValue);
}
Class modelClassOfField = listField.getModelClassOfField();
ModelInfo modelInfo = TableHelper.getModelInfo(modelClassOfField);
List colNames = rs.getColNames();
String uniqueKey = getUniqueKey(parentKey, rs, modelInfo.getUniqueCol(colNames));
Object o = tmpCache.get(uniqueKey);
if (o == null) {
o = modelInfo.newC();
tmpCache.put(uniqueKey, o);
buildData(uniqueKey, rs, tmpCache, modelInfo.getOnesColWithOneModel(colNames), modelInfo.getArrays(colNames),
modelInfo.getList(colNames), modelInfo.getSet(colNames), o, true);
listFieldValue.add(o);
} else {
buildData(uniqueKey, rs, tmpCache, modelInfo.getOnesColWithOneModel(colNames), modelInfo.getArrays(colNames),
modelInfo.getList(colNames), modelInfo.getSet(colNames), o, false);
}
}
for (ModelFieldInfo setField : set_) {
Set setFieldValue = (Set) setField.getGetter().getter(model);
if (setFieldValue == null) {
setFieldValue = (Set) setField.getNewc();
setField.getSetter().setter(model, setFieldValue);
}
Class modelClassOfField = setField.getModelClassOfField();
ModelInfo modelInfo = TableHelper.getModelInfo(modelClassOfField);
List colNames = rs.getColNames();
String uniqueKey = getUniqueKey(parentKey, rs, modelInfo.getUniqueCol(colNames));
Object o = tmpCache.get(uniqueKey);
if (o == null) {
o = modelInfo.newC();
tmpCache.put(uniqueKey, o);
buildData(uniqueKey, rs, tmpCache, modelInfo.getOnesColWithOneModel(colNames), modelInfo.getArrays(colNames),
modelInfo.getList(colNames), modelInfo.getSet(colNames), o, true);
} else {
buildData(uniqueKey, rs, tmpCache, modelInfo.getOnesColWithOneModel(colNames), modelInfo.getArrays(colNames),
modelInfo.getList(colNames), modelInfo.getSet(colNames), o, false);
}
}
}
}