public without sharing class DataBasePlus {
|
|
public DataBasePlus() {
|
|
}
|
|
public static String Mysql;
|
public static String MysqlWhere;
|
|
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
|
// static SObject getOne() {
|
// QueryWrapper queryWrapper = new QueryWrapper(Test01__mdt.getSObjectType());
|
// return getOne( queryWrapper, true);
|
|
// }
|
// 根据 Wrapper,查询一条记录
|
public static SObject getOne(QueryWrapper queryWrapper, boolean throwEx) {
|
|
List<SObject> sObjectList = getRes(queryWrapper.getType(), queryWrapper.getSObjectFieldList(),queryWrapper.build(),'');
|
if(sObjectList != null && sObjectList.size() > 1){
|
//异常
|
}else{
|
return sObjectList == null || sObjectList.size() == 0 ? null : sObjectList.get(0);
|
}
|
return null;
|
}
|
|
// 查询列表
|
public static List<SObject> listPlus(QueryWrapper queryWrapper) {
|
return getRes(queryWrapper.getType(), queryWrapper.getSObjectFieldList(),queryWrapper.build(),'');
|
}
|
|
// 条件分页查询
|
public static PagePlus page(PagePlus page, QueryWrapper queryWrapper) {
|
page.setRecords(getRes(queryWrapper.getType(), queryWrapper.getSObjectFieldList(),queryWrapper.build(),page.getPage()));
|
page.setTotal(count(queryWrapper));
|
return page;
|
}
|
// 条数
|
public static Integer count(QueryWrapper queryWrapper) {
|
return getCount(queryWrapper.getType(), queryWrapper.build());
|
}
|
|
|
// 获取查询数据
|
private static List<SObject> getRes(Schema.SObjectType type, List<SObjectField> sObjectFieldList, String bulid, String limits){
|
//获取传过来对象的列的值
|
List<SObject> sobjList = null;
|
String sql = '';
|
// Make the describe call
|
try {
|
// Make the describe call
|
Schema.DescribeSObjectResult results = type.getDescribe();
|
|
//获取表中所有列的字段
|
Map<String, Schema.SObjectField> fieldMap = results.fields.getMap();
|
//字段拼接成字符串
|
for(String keyset : fieldMap.keySet()) {
|
Schema.DescribeFieldResult checksObjectField=fieldMap.get(keyset).getDescribe();
|
if(checksObjectField.isAccessible()==true){
|
if(checksObjectField.isAccessible()){
|
if(sql == ''){
|
sql = keyset;
|
}else{
|
sql = sql + ',' + keyset;
|
}
|
}
|
}
|
}
|
if(sObjectFieldList != null && sObjectFieldList.size() > 0){
|
|
for(Schema.SObjectField sObjectField : sObjectFieldList) {
|
Schema.DescribeFieldResult dfr =sObjectField.getDescribe();
|
Integer num=dfr.getRelationshipOrder(); //0
|
if(num !=null){
|
List<Schema.sObjectType> list1 = dfr.getReferenceTo(); //表名
|
String a=dfr.getRelationshipName(); //字段名
|
for (Schema.sObjectType st : list1) {
|
// Make the describe call
|
Schema.DescribeSObjectResult res = st.getDescribe();
|
|
//获取表中所有列的字段
|
Map<String, Object> fieldMaps = res.fields.getMap();
|
//字段拼接成字符串
|
for(String keyset : fieldMaps.keySet()) {
|
if(sql == ''){
|
sql = a + '.' + keyset;
|
}else{
|
sql = sql + ',' + a + '.' + keyset;
|
}
|
}
|
|
}
|
}
|
}
|
|
}
|
//拼接查询语句
|
if(!''.equals(sql)){
|
sql = 'select ' + sql +' from ' + results.Name;
|
}
|
//条件
|
if(!''.equals(bulid)){
|
sql += bulid;
|
}
|
//分页
|
if(!''.equals(limits)){
|
sql += limits;
|
}
|
System.debug(sql);
|
sobjList = Database.query(sql);
|
MysqlWhere = bulid;
|
Mysql = sql;
|
|
} catch(Exception e) {
|
// 捕捉 DML 相关的异常
|
|
}
|
|
return sobjList;
|
}
|
|
// 获取查询数据条数
|
public static Integer getCount(Schema.SObjectType type, String bulid){
|
//获取传过来对象的列的值
|
Integer cnt = 0;
|
String sql = '';
|
// Make the describe call
|
try {
|
// Make the describe call
|
Schema.DescribeSObjectResult results = type.getDescribe();
|
|
//拼接查询语句
|
sql = 'select count() from ' + results.Name;
|
//条件
|
if(!''.equals(bulid)){
|
sql += bulid;
|
}
|
//sql
|
System.debug(sql);
|
cnt = Database.countQuery(sql);
|
} catch(Exception e) {
|
// 捕捉 DML 相关的异常
|
|
}
|
|
return cnt;
|
}
|
|
|
}
|