ecology_maven/aiyh/utils/sqlUtil/builderSql/impl/BuilderSqlImpl.java

451 lines
15 KiB
Java
Raw Normal View History

2021-11-14 15:29:16 +08:00
package aiyh.utils.sqlUtil.builderSql.impl;
import aiyh.utils.Util;
import aiyh.utils.mapUtil.UtilHashMap;
import aiyh.utils.mapUtil.UtilLinkedHashMap;
import aiyh.utils.sqlUtil.builderSql.BuilderSql;
import aiyh.utils.sqlUtil.sqlResult.impl.PrepSqlResultImpl;
import aiyh.utils.sqlUtil.sqlResult.impl.BatchSqlResultImpl;
import aiyh.utils.sqlUtil.whereUtil.Where;
import aiyh.utils.sqlUtil.whereUtil.impl.WhereImpl;
import weaver.conn.RecordSet;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* @author EBU7-dev1-ayh
* @date 2021/8/23 0023 14:45
*/
public class BuilderSqlImpl implements BuilderSql {
private String DB_TYPE;
{
// 获取当前数据库的类型
this.DB_TYPE = (new RecordSet()).getDBType();
}
/**
*
* @param tableName
* @param mapConfig
* @return SQL
*/
@Override
public PrepSqlResultImpl insertSql(String tableName, Map<String, Object> mapConfig) {
UtilHashMap<String, Object> map = this.verifyMap(mapConfig);
List<Object> args = new ArrayList<>();
StringBuilder sqlBuilder = new StringBuilder("insert into ");
sqlBuilder.append(tableName);
StringBuilder fieldBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder();
map.forEach((field, value) -> {
fieldBuilder.append(field);
fieldBuilder.append(", ");
valueBuilder.append(" ?, ");
args.add(value);
});
sqlBuilder.append(" ( ");
sqlBuilder.append(Util.removeSeparator(fieldBuilder));
sqlBuilder.append(" ) values ( ");
sqlBuilder.append(Util.removeSeparator(valueBuilder));
sqlBuilder.append(" )");
return new PrepSqlResultImpl(sqlBuilder.toString(), args);
}
public <T> PrepSqlResultImpl insertSqlByEntity(String tableName, T t){
List<Object> args = new ArrayList<>();
StringBuilder sqlBuilder = new StringBuilder("insert into ");
sqlBuilder.append(tableName);
StringBuilder fieldBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder();
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(t.getClass(), Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor proper : propertyDescriptors) {
String name = proper.getName();
Method readMethod = proper.getReadMethod();
Object invoke = readMethod.invoke(t);
// System.out.println(name);
// System.out.println(invoke);
if(invoke == null){
continue;
}
fieldBuilder.append(name);
fieldBuilder.append(", ");
valueBuilder.append(" ?, ");
args.add(invoke);
}
} catch (Exception e) {
e.printStackTrace();
}
sqlBuilder.append(" ( ");
sqlBuilder.append(Util.removeSeparator(fieldBuilder));
sqlBuilder.append(" ) values ( ");
sqlBuilder.append(Util.removeSeparator(valueBuilder));
sqlBuilder.append(" )");
return new PrepSqlResultImpl(sqlBuilder.toString(), args);
}
/**
* SQL
* @param tableName
* @param mapListConfig list
* @return SQL
*/
@Override
public BatchSqlResultImpl insertBatchSql(String tableName, List<LinkedHashMap<String, Object>> mapListConfig) {
List<UtilLinkedHashMap<String, Object>> mapList = this.verifyMapList(mapListConfig);
StringBuilder sqlBuilder = new StringBuilder("insert into ");
sqlBuilder.append(tableName);
StringBuilder fieldBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder();
List<List> args = new ArrayList<>();
AtomicInteger i = new AtomicInteger();
mapList.forEach(item -> {
List<Object> arg = new ArrayList<>();
item.forEach((field, value) -> {
if (i.get() == 0) {
fieldBuilder.append(field);
fieldBuilder.append(", ");
valueBuilder.append(" ?, ");
}
arg.add(value);
});
args.add(arg);
i.getAndIncrement();
});
sqlBuilder.append(" ( ");
sqlBuilder.append(Util.removeSeparator(fieldBuilder));
sqlBuilder.append(" ) values ( ");
sqlBuilder.append(Util.removeSeparator(valueBuilder));
sqlBuilder.append(" )");
return new BatchSqlResultImpl(sqlBuilder.toString(), args);
}
public <T> BatchSqlResultImpl insertBatchSqlByEntity(String tableName, List<T> list) {
StringBuilder sqlBuilder = new StringBuilder("insert into ");
sqlBuilder.append(tableName);
StringBuilder fieldBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder();
List<List> args = new ArrayList<>();
AtomicInteger i = new AtomicInteger();
list.forEach(item -> {
List<Object> arg = new ArrayList<>();
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(item.getClass(), Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor proper : propertyDescriptors) {
String name = proper.getName();
Method readMethod = proper.getReadMethod();
Object invoke = readMethod.invoke(item);
if(invoke == null){
continue;
}
if (i.get() == 0) {
fieldBuilder.append(name);
fieldBuilder.append(", ");
valueBuilder.append(" ?, ");
}
arg.add(invoke);
}
} catch (Exception e) {
e.printStackTrace();
}
args.add(arg);
i.getAndIncrement();
});
sqlBuilder.append(" ( ");
sqlBuilder.append(Util.removeSeparator(fieldBuilder));
sqlBuilder.append(" ) values ( ");
sqlBuilder.append(Util.removeSeparator(valueBuilder));
sqlBuilder.append(" )");
return new BatchSqlResultImpl(sqlBuilder.toString(), args);
}
/**
*
* @param tableName
* @param mapConfig
* @param where
* @return SQL
*/
@Override
public PrepSqlResultImpl updateSql(String tableName, Map<String, Object> mapConfig, Where where) {
UtilHashMap<String, Object> map = this.verifyMap(mapConfig);
this.verifyWhere(where);
StringBuilder sqlBuilder = new StringBuilder("update ");
StringBuilder fieldValue = new StringBuilder();
List<Object> args = new ArrayList<>();
sqlBuilder.append(tableName);
sqlBuilder.append(" set ");
map.forEach((field, value) -> {
fieldValue.append(field);
fieldValue.append(" = ?, ");
args.add(value);
});
sqlBuilder.append(Util.removeSeparator(fieldValue));
sqlBuilder.append(" ");
sqlBuilder.append(where.getSql());
if (where.getArgs() != null && where.getArgs().size() > 0) {
args.addAll(where.getArgs());
}
return new PrepSqlResultImpl(sqlBuilder.toString(), args);
}
public <T> PrepSqlResultImpl updateSqlByEntity(String tableName, T t, Where where){
this.verifyWhere(where);
StringBuilder sqlBuilder = new StringBuilder("update ");
StringBuilder fieldValue = new StringBuilder();
List<Object> args = new ArrayList<>();
sqlBuilder.append(tableName);
sqlBuilder.append(" set ");
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(t.getClass(), Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor proper : propertyDescriptors) {
String name = proper.getName();
Method readMethod = proper.getReadMethod();
Object invoke = readMethod.invoke(t);
if(invoke == null){
continue;
}
fieldValue.append(name);
fieldValue.append(" = ?, ");
args.add(invoke);
}
} catch (Exception e) {
e.printStackTrace();
}
sqlBuilder.append(Util.removeSeparator(fieldValue));
sqlBuilder.append(" ");
sqlBuilder.append(where.getSql());
if (where.getArgs() != null && where.getArgs().size() > 0) {
args.addAll(where.getArgs());
}
return new PrepSqlResultImpl(sqlBuilder.toString(), args);
}
/**
* @param tableName
* @param mapListConfig
* @param whereList
* @return id= ?,id
*/
@Override
public BatchSqlResultImpl updateBatchSql(String tableName, List<LinkedHashMap<String, Object>> mapListConfig, List<Where> whereList) {
List<UtilLinkedHashMap<String, Object>> mapList = this.verifyMapList(mapListConfig);
this.verifyWhereList(whereList);
if (mapList.size() != whereList.size()) {
throw new RuntimeException("mapList与whereList长度不一致mapList size and whereList size Not the same!");
}
StringBuilder sqlBuilder = new StringBuilder("update ");
StringBuilder fieldValue = new StringBuilder();
sqlBuilder.append(tableName);
sqlBuilder.append(" set ");
List<List> args = new ArrayList<>();
AtomicInteger i = new AtomicInteger();
mapList.forEach(item -> {
List<Object> arg = new ArrayList<>();
item.forEach((field, value) -> {
if (i.get() == 0) {
fieldValue.append(field);
fieldValue.append(" = ?, ");
}
arg.add(value);
});
if (whereList.get(i.get()).getArgs() != null && whereList.get(i.get()).getArgs().size() > 0) {
arg.addAll(whereList.get(i.get()).getArgs());
}
args.add(arg);
i.getAndIncrement();
});
sqlBuilder.append(Util.removeSeparator(fieldValue));
sqlBuilder.append(" ");
sqlBuilder.append(whereList.get(0).getSql());
return new BatchSqlResultImpl(sqlBuilder.toString(), args);
}
public <T> BatchSqlResultImpl updateBatchSqlByEntity(String tableName, List<T> list, List<Where> whereList) {
this.verifyWhereList(whereList);
if (list.size() != whereList.size()) {
throw new RuntimeException("List与whereList长度不一致mapList size and whereList size Not the same!");
}
StringBuilder sqlBuilder = new StringBuilder("update ");
StringBuilder fieldValue = new StringBuilder();
sqlBuilder.append(tableName);
sqlBuilder.append(" set ");
List<List> args = new ArrayList<>();
AtomicInteger i = new AtomicInteger();
list.forEach(item -> {
List<Object> arg = new ArrayList<>();
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(item.getClass(), Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor proper : propertyDescriptors) {
String name = proper.getName();
Method readMethod = proper.getReadMethod();
Object invoke = readMethod.invoke(item);
if(invoke == null){
continue;
}
if (i.get() == 0) {
fieldValue.append(name);
fieldValue.append(" = ?, ");
}
arg.add(invoke);
}
} catch (Exception e) {
e.printStackTrace();
}
if (whereList.get(i.get()).getArgs() != null && whereList.get(i.get()).getArgs().size() > 0) {
arg.addAll(whereList.get(i.get()).getArgs());
}
args.add(arg);
i.getAndIncrement();
});
sqlBuilder.append(Util.removeSeparator(fieldValue));
sqlBuilder.append(" ");
sqlBuilder.append(whereList.get(0).getSql());
return new BatchSqlResultImpl(sqlBuilder.toString(), args);
}
/**
* SQL
* @param tableName
* @param mapConfig
* @return SQL
*/
@Override
public String insertSqlForString(String tableName, Map<String, Object> mapConfig) {
UtilHashMap<String, Object> map = this.verifyMap(mapConfig);
StringBuilder sqlBuilder = new StringBuilder("insert into ");
sqlBuilder.append(tableName);
StringBuilder fieldBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder();
map.forEach((field, value) -> {
fieldBuilder.append(field);
fieldBuilder.append(", ");
valueBuilder.append("'");
valueBuilder.append(value);
valueBuilder.append("'");
valueBuilder.append(", ");
});
sqlBuilder.append(" ( ");
sqlBuilder.append(Util.removeSeparator(fieldBuilder));
sqlBuilder.append(" ) values ( ");
sqlBuilder.append(Util.removeSeparator(valueBuilder));
sqlBuilder.append(" )");
return sqlBuilder.toString();
}
/**
* SQL
* @param tableName
* @param mapConfig
* @return SQL
*/
@Override
public String updateSqlForString(String tableName, Map<String, Object> mapConfig, WhereImpl where) {
UtilHashMap<String, Object> map = this.verifyMap(mapConfig);
this.verifyWhere(where);
StringBuilder sqlBuilder = new StringBuilder("update ");
StringBuilder fieldValue = new StringBuilder();
sqlBuilder.append(tableName);
sqlBuilder.append(" set ");
map.forEach((field, value) -> {
fieldValue.append(field);
fieldValue.append(" = ");
fieldValue.append("'");
fieldValue.append(value);
fieldValue.append("'");
fieldValue.append(", ");
});
sqlBuilder.append(Util.removeSeparator(fieldValue));
sqlBuilder.append(" ");
sqlBuilder.append(where.getSql());
return sqlBuilder.toString();
}
/**
* SQLnull
* @param map SQLmap
* @return UtilHashMap
*/
public UtilHashMap<String, Object> verifyMap(Map<String, Object> map) {
UtilHashMap<String, Object> filterMap = Util.createUtilHashMap()
.uPutAll(map)
.filter((key, value) -> !Objects.isNull(key) && !Objects.isNull(value));
if (Util.mapIsNullOrEmpty(filterMap)) {
throw new RuntimeException("map为空或没有数据 map is null or empty!");
}
return filterMap;
}
/**
* SQL
* @param mapList SQL
* @return
*/
public List<UtilLinkedHashMap<String, Object>> verifyMapList(List<LinkedHashMap<String, Object>> mapList) {
if (Objects.isNull(mapList)) {
throw new RuntimeException("mapList为nullmapList is null!");
}
List<UtilLinkedHashMap<String, Object>> collect = mapList.stream().map(item ->
Util.createUtilLinkedHashMap()
.uPutAll(item)
.filter((key, value) -> !Objects.isNull(key) && !Objects.isNull(value))
).collect(Collectors.toList());
if (mapList.size() == 0) {
throw new RuntimeException("mapList没有数据mapList is empty!");
}
return collect;
}
/**
* where
* @param where where
*/
private void verifyWhere(Where where) {
if (where == null) {
throw new RuntimeException("where为null where is null!");
}
}
/**
* where
* @param whereList where
*/
private void verifyWhereList(List<Where> whereList) {
if (whereList == null) {
throw new RuntimeException("whereList为null whereList is null!");
}
whereList.forEach(item->{
if(item == null){
throw new RuntimeException("whereList中数据为null whereDate is null in whereList!");
}
});
}
}