|
|
@@ -2,25 +2,10 @@
|
|
|
//
|
|
|
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
|
|
|
|
|
|
-namespace Admin.NET.Core.Service;
|
|
|
-
|
|
|
using AspectCore.Extensions.Reflection;
|
|
|
-using NewLife.Data;
|
|
|
-using Newtonsoft.Json.Linq;
|
|
|
-using Newtonsoft.Json.Schema;
|
|
|
-using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
|
|
|
-using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
|
|
|
-using Org.BouncyCastle.Bcpg.OpenPgp;
|
|
|
-using SqlSugar;
|
|
|
-using StackExchange.Redis;
|
|
|
-using System;
|
|
|
-using System.Collections;
|
|
|
-using System.Collections.Generic;
|
|
|
using System.Dynamic;
|
|
|
-using System.Linq;
|
|
|
-using System.Text.RegularExpressions;
|
|
|
-using static OfficeOpenXml.ExcelErrorValue;
|
|
|
-using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinExpressIntracityUpdateStoreRequest.Types;
|
|
|
+
|
|
|
+namespace Admin.NET.Core.Service;
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
@@ -31,31 +16,25 @@ public class SelectTable : ISingleton
|
|
|
private readonly TableMapper _tableMapper;
|
|
|
private readonly ISqlSugarClient _db;
|
|
|
|
|
|
- /// <summary>
|
|
|
- ///
|
|
|
- /// </summary>
|
|
|
- /// <param name="identityService"></param>
|
|
|
- /// <param name="tableMapper"></param>
|
|
|
- /// <param name="dbClient"></param>
|
|
|
public SelectTable(IdentityService identityService, TableMapper tableMapper, ISqlSugarClient dbClient)
|
|
|
{
|
|
|
_identitySvc = identityService;
|
|
|
_tableMapper = tableMapper;
|
|
|
_db = dbClient;
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
- /// 判断表名是否正确,如果不正确则抛异常
|
|
|
+ /// 判断表名是否正确,若不正确则抛异常
|
|
|
/// </summary>
|
|
|
/// <param name="table"></param>
|
|
|
/// <returns></returns>
|
|
|
public virtual bool IsTable(string table)
|
|
|
{
|
|
|
- if (_db.DbMaintenance.GetTableInfoList().Any(it => it.Name.Equals(table, StringComparison.CurrentCultureIgnoreCase)))
|
|
|
- return true;
|
|
|
- else
|
|
|
- throw new Exception($"表名【{table}】不正确!");
|
|
|
-
|
|
|
+ return _db.DbMaintenance.GetTableInfoList().Any(it => it.Name.Equals(table, StringComparison.CurrentCultureIgnoreCase))
|
|
|
+ ? true
|
|
|
+ : throw new Exception($"表名【{table}】不正确!");
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 判断表的列名是否正确,如果不正确则抛异常,更早地暴露给调用方
|
|
|
/// </summary>
|
|
|
@@ -64,10 +43,9 @@ public class SelectTable : ISingleton
|
|
|
/// <returns></returns>
|
|
|
public virtual bool IsCol(string table, string col)
|
|
|
{
|
|
|
- if (_db.DbMaintenance.GetColumnInfosByTableName(table).Any(it => it.DbColumnName.Equals(col, StringComparison.CurrentCultureIgnoreCase)))
|
|
|
- return true;
|
|
|
- else
|
|
|
- throw new Exception($"表【{table}】不存在列【{col}】!请检查输入参数");
|
|
|
+ return _db.DbMaintenance.GetColumnInfosByTableName(table).Any(it => it.DbColumnName.Equals(col, StringComparison.CurrentCultureIgnoreCase))
|
|
|
+ ? true
|
|
|
+ : throw new Exception($"表【{table}】不存在列【{col}】!请检查输入参数");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -81,46 +59,41 @@ public class SelectTable : ISingleton
|
|
|
/// <returns></returns>
|
|
|
public virtual Tuple<dynamic, int> GetTableData(string subtable, int page, int count, int query, string json, JObject dd)
|
|
|
{
|
|
|
-
|
|
|
var role = _identitySvc.GetSelectRole(subtable);
|
|
|
- if (!role.Item1)//没有权限返回异常
|
|
|
- {
|
|
|
+ if (!role.Item1)
|
|
|
throw new Exception(role.Item2);
|
|
|
- }
|
|
|
- string selectrole = role.Item2;
|
|
|
+
|
|
|
+ var selectrole = role.Item2;
|
|
|
subtable = _tableMapper.GetTableName(subtable);
|
|
|
|
|
|
- JObject values = JObject.Parse(json);
|
|
|
+ var values = JObject.Parse(json);
|
|
|
page = values["page"] == null ? page : int.Parse(values["page"].ToString());
|
|
|
count = values["count"] == null ? count : int.Parse(values["count"].ToString());
|
|
|
query = values["query"] == null ? query : int.Parse(values["query"].ToString());
|
|
|
values.Remove("page");
|
|
|
values.Remove("count");
|
|
|
- //构造查询过程
|
|
|
+ // 构造查询过程
|
|
|
var tb = SugarQueryable(subtable, selectrole, values, dd);
|
|
|
|
|
|
- //实际会在这里执行
|
|
|
- if (query == 1)//1-总数
|
|
|
+ // 实际会在这里执行
|
|
|
+ if (query == 1) // 1-总数
|
|
|
+ {
|
|
|
return new Tuple<dynamic, int>(null, tb.MergeTable().Count());
|
|
|
+ }
|
|
|
else
|
|
|
{
|
|
|
- if (page > 0)//分页
|
|
|
+ if (page > 0) // 分页
|
|
|
{
|
|
|
int total = 0;
|
|
|
- if (query == 0)//0-对象
|
|
|
- return new Tuple<dynamic, int>(tb.ToPageList(page, count), total);
|
|
|
+ if (query == 0)
|
|
|
+ return new Tuple<dynamic, int>(tb.ToPageList(page, count), total); // 0-对象
|
|
|
else
|
|
|
- //2-以上全部
|
|
|
- return new Tuple<dynamic, int>(tb.ToPageList(page, count, ref total), total);
|
|
|
-
|
|
|
+ return new Tuple<dynamic, int>(tb.ToPageList(page, count, ref total), total); // 2-以上全部
|
|
|
}
|
|
|
- else//列表
|
|
|
+ else // 列表
|
|
|
{
|
|
|
IList l = tb.ToList();
|
|
|
- if (query == 0)
|
|
|
- return new Tuple<dynamic, int>(l, 0);
|
|
|
- else
|
|
|
- return new Tuple<dynamic, int>(l, l.Count);
|
|
|
+ return query == 0 ? new Tuple<dynamic, int>(l, 0) : new Tuple<dynamic, int>(l, l.Count);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -128,15 +101,12 @@ public class SelectTable : ISingleton
|
|
|
/// <summary>
|
|
|
/// 解析并查询
|
|
|
/// </summary>
|
|
|
- /// <param name="query"></param>
|
|
|
+ /// <param name="queryJson"></param>
|
|
|
/// <returns></returns>
|
|
|
public virtual JObject Query(string queryJson)
|
|
|
{
|
|
|
- JObject resultObj = new JObject();
|
|
|
-
|
|
|
- JObject queryJobj = JObject.Parse(queryJson);
|
|
|
- resultObj = Query(queryJobj);
|
|
|
- return resultObj;
|
|
|
+ var queryJobj = JObject.Parse(queryJson);
|
|
|
+ return Query(queryJobj);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -147,13 +117,12 @@ public class SelectTable : ISingleton
|
|
|
/// <returns></returns>
|
|
|
public virtual JObject QuerySingle(JObject queryObj, string nodeName = "infos")
|
|
|
{
|
|
|
- JObject resultObj = new JObject();
|
|
|
+ var resultObj = new JObject();
|
|
|
|
|
|
- int total = 0;
|
|
|
+ var total = 0;
|
|
|
foreach (var item in queryObj)
|
|
|
{
|
|
|
- string key = item.Key.Trim();
|
|
|
-
|
|
|
+ var key = item.Key.Trim();
|
|
|
if (key.EndsWith("[]"))
|
|
|
{
|
|
|
total = QuerySingleList(resultObj, item, nodeName);
|
|
|
@@ -167,7 +136,6 @@ public class SelectTable : ISingleton
|
|
|
resultObj.Add("total", total);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return resultObj;
|
|
|
}
|
|
|
|
|
|
@@ -180,12 +148,8 @@ public class SelectTable : ISingleton
|
|
|
{
|
|
|
foreach (var item in queryObj)
|
|
|
{
|
|
|
- string key = item.Key.Trim();
|
|
|
-
|
|
|
- if (key.EndsWith("[]"))
|
|
|
- {
|
|
|
+ if (item.Key.Trim().EndsWith("[]"))
|
|
|
return ToSql(item);
|
|
|
- }
|
|
|
}
|
|
|
return string.Empty;
|
|
|
}
|
|
|
@@ -193,21 +157,20 @@ public class SelectTable : ISingleton
|
|
|
/// <summary>
|
|
|
/// 解析并查询
|
|
|
/// </summary>
|
|
|
- /// <param name="query"></param>
|
|
|
+ /// <param name="queryObj"></param>
|
|
|
/// <returns></returns>
|
|
|
public virtual JObject Query(JObject queryObj)
|
|
|
{
|
|
|
- JObject resultObj = new JObject();
|
|
|
+ var resultObj = new JObject();
|
|
|
|
|
|
int total;
|
|
|
foreach (var item in queryObj)
|
|
|
{
|
|
|
- string key = item.Key.Trim();
|
|
|
-
|
|
|
- if (key.Equals("[]"))//列表
|
|
|
+ var key = item.Key.Trim();
|
|
|
+ if (key.Equals("[]")) // 列表
|
|
|
{
|
|
|
total = QueryMoreList(resultObj, item);
|
|
|
- resultObj.Add("total", total);//只要是列表查询都自动返回总数
|
|
|
+ resultObj.Add("total", total); // 只要是列表查询都自动返回总数
|
|
|
}
|
|
|
else if (key.EndsWith("[]"))
|
|
|
{
|
|
|
@@ -219,38 +182,32 @@ public class SelectTable : ISingleton
|
|
|
}
|
|
|
else if (key.Equals("total@") || key.Equals("total"))
|
|
|
{
|
|
|
- //resultObj.Add("total", total);
|
|
|
+ // resultObj.Add("total", total);
|
|
|
continue;
|
|
|
}
|
|
|
- else//单条
|
|
|
+ else // 单条
|
|
|
{
|
|
|
var template = GetFirstData(key, item.Value.ToString(), resultObj);
|
|
|
if (template != null)
|
|
|
- {
|
|
|
resultObj.Add(key, JToken.FromObject(template));
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return resultObj;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- //动态调用方法
|
|
|
+ // 动态调用方法
|
|
|
private object ExecFunc(string funcname, object[] param, Type[] types)
|
|
|
{
|
|
|
var method = typeof(FuncList).GetMethod(funcname);
|
|
|
-
|
|
|
var reflector = method.GetReflector();
|
|
|
var result = reflector.Invoke(new FuncList(), param);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- //生成sql
|
|
|
+ // 生成sql
|
|
|
private string ToSql(string subtable, int page, int count, int query, string json)
|
|
|
{
|
|
|
- JObject values = JObject.Parse(json);
|
|
|
+ var values = JObject.Parse(json);
|
|
|
page = values["page"] == null ? page : int.Parse(values["page"].ToString());
|
|
|
count = values["count"] == null ? count : int.Parse(values["count"].ToString());
|
|
|
query = values["query"] == null ? query : int.Parse(values["query"].ToString());
|
|
|
@@ -272,25 +229,24 @@ public class SelectTable : ISingleton
|
|
|
/// <exception cref="Exception"></exception>
|
|
|
private dynamic GetFirstData(string subtable, string json, JObject job)
|
|
|
{
|
|
|
-
|
|
|
var role = _identitySvc.GetSelectRole(subtable);
|
|
|
- if (!role.Item1)//没有权限返回异常
|
|
|
- {
|
|
|
+ if (!role.Item1)
|
|
|
throw new Exception(role.Item2);
|
|
|
- }
|
|
|
- string selectrole = role.Item2;
|
|
|
+
|
|
|
+ var selectrole = role.Item2;
|
|
|
subtable = _tableMapper.GetTableName(subtable);
|
|
|
- JObject values = JObject.Parse(json);
|
|
|
+
|
|
|
+ var values = JObject.Parse(json);
|
|
|
values.Remove("page");
|
|
|
values.Remove("count");
|
|
|
var tb = SugarQueryable(subtable, selectrole, values, job).First();
|
|
|
var dic = (IDictionary<string, object>)tb;
|
|
|
foreach (var item in values.Properties().Where(it => it.Name.EndsWith("()")))
|
|
|
{
|
|
|
- if (item.Value.HasValue())
|
|
|
+ if (item.Value.IsNullOrEmpty())
|
|
|
{
|
|
|
- string func = item.Value.ToString().Substring(0, item.Value.ToString().IndexOf("("));
|
|
|
- string param = item.Value.ToString().Substring(item.Value.ToString().IndexOf("(") + 1).TrimEnd(')');
|
|
|
+ var func = item.Value.ToString().Substring(0, item.Value.ToString().IndexOf("("));
|
|
|
+ var param = item.Value.ToString().Substring(item.Value.ToString().IndexOf("(") + 1).TrimEnd(')');
|
|
|
var types = new List<Type>();
|
|
|
var paramss = new List<object>();
|
|
|
foreach (var va in param.Split(','))
|
|
|
@@ -301,19 +257,17 @@ public class SelectTable : ISingleton
|
|
|
dic[item.Name] = ExecFunc(func, paramss.ToArray(), types.ToArray());
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return tb;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- //单表查询,返回的数据在指定的NodeName节点
|
|
|
+ // 单表查询,返回的数据在指定的NodeName节点
|
|
|
private int QuerySingleList(JObject resultObj, KeyValuePair<string, JToken> item, string nodeName)
|
|
|
{
|
|
|
- string key = item.Key.Trim();
|
|
|
+ var key = item.Key.Trim();
|
|
|
var jb = JObject.Parse(item.Value.ToString());
|
|
|
int page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
|
|
|
int count = jb["count"] == null ? 10 : int.Parse(jb["count"].ToString());
|
|
|
- int query = jb["query"] == null ? 2 : int.Parse(jb["query"].ToString());//默认输出数据和数量
|
|
|
+ int query = jb["query"] == null ? 2 : int.Parse(jb["query"].ToString()); // 默认输出数据和数量
|
|
|
int total = 0;
|
|
|
|
|
|
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
|
|
|
@@ -323,9 +277,8 @@ public class SelectTable : ISingleton
|
|
|
{
|
|
|
var datas = GetTableData(t.Key, page, count, query, t.Value.ToString(), null);
|
|
|
if (query > 0)
|
|
|
- {
|
|
|
total = datas.Item2;
|
|
|
- }
|
|
|
+
|
|
|
foreach (var data in datas.Item1)
|
|
|
{
|
|
|
htt.Add(JToken.FromObject(data));
|
|
|
@@ -333,37 +286,33 @@ public class SelectTable : ISingleton
|
|
|
}
|
|
|
|
|
|
if (!string.IsNullOrEmpty(nodeName))
|
|
|
- {
|
|
|
resultObj.Add(nodeName, htt);
|
|
|
- }
|
|
|
else
|
|
|
resultObj.Add(key, htt);
|
|
|
+
|
|
|
return total;
|
|
|
}
|
|
|
|
|
|
- //生成sql
|
|
|
+ // 生成sql
|
|
|
private string ToSql(KeyValuePair<string, JToken> item)
|
|
|
{
|
|
|
- string key = item.Key.Trim();
|
|
|
var jb = JObject.Parse(item.Value.ToString());
|
|
|
int page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
|
|
|
int count = jb["count"] == null ? 10 : int.Parse(jb["count"].ToString());
|
|
|
- int query = jb["query"] == null ? 2 : int.Parse(jb["query"].ToString());//默认输出数据和数量
|
|
|
+ int query = jb["query"] == null ? 2 : int.Parse(jb["query"].ToString()); // 默认输出数据和数量
|
|
|
|
|
|
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
|
|
|
- var htt = new JArray();
|
|
|
foreach (var t in jb)
|
|
|
{
|
|
|
return ToSql(t.Key, page, count, query, t.Value.ToString());
|
|
|
}
|
|
|
-
|
|
|
return string.Empty;
|
|
|
}
|
|
|
|
|
|
- //单表查询
|
|
|
+ // 单表查询
|
|
|
private int QuerySingleList(JObject resultObj, KeyValuePair<string, JToken> item)
|
|
|
{
|
|
|
- string key = item.Key.TrimEnd("[]");
|
|
|
+ var key = item.Key.TrimEnd("[]");
|
|
|
return QuerySingleList(resultObj, item, key);
|
|
|
}
|
|
|
|
|
|
@@ -380,7 +329,7 @@ public class SelectTable : ISingleton
|
|
|
var jb = JObject.Parse(item.Value.ToString());
|
|
|
var page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
|
|
|
var count = jb["count"] == null ? 10 : int.Parse(jb["count"].ToString());
|
|
|
- var query = jb["query"] == null ? 2 : int.Parse(jb["query"].ToString());//默认输出数据和数量
|
|
|
+ var query = jb["query"] == null ? 2 : int.Parse(jb["query"].ToString()); // 默认输出数据和数量
|
|
|
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
|
|
|
var htt = new JArray();
|
|
|
List<string> tables = new List<string>(), where = new List<string>();
|
|
|
@@ -393,18 +342,19 @@ public class SelectTable : ISingleton
|
|
|
string table = tables[0].TrimEnd("[]");
|
|
|
var temp = GetTableData(table, page, count, query, where[0], null);
|
|
|
if (query > 0)
|
|
|
- {
|
|
|
total = temp.Item2;
|
|
|
- }
|
|
|
- //关联查询,先查子表数据,再根据外键循环查询主表
|
|
|
+
|
|
|
+ // 关联查询,先查子表数据,再根据外键循环查询主表
|
|
|
foreach (var dd in temp.Item1)
|
|
|
{
|
|
|
- var zht = new JObject();
|
|
|
- zht.Add(table, JToken.FromObject(dd));
|
|
|
- for (int i = 1; i < tables.Count; i++)//从第二个表开始循环
|
|
|
+ var zht = new JObject
|
|
|
+ {
|
|
|
+ { table, JToken.FromObject(dd) }
|
|
|
+ };
|
|
|
+ for (int i = 1; i < tables.Count; i++) // 从第二个表开始循环
|
|
|
{
|
|
|
string subtable = tables[i];
|
|
|
- //有bug,暂不支持[]分支
|
|
|
+ // 有bug,暂不支持[]分支
|
|
|
//if (subtable.EndsWith("[]"))
|
|
|
//{
|
|
|
// string tableName = subtable.TrimEnd("[]".ToCharArray());
|
|
|
@@ -421,24 +371,17 @@ public class SelectTable : ISingleton
|
|
|
//}
|
|
|
//else
|
|
|
//{
|
|
|
-
|
|
|
var ddf = GetFirstData(subtable, where[i].ToString(), zht);
|
|
|
if (ddf != null)
|
|
|
- {
|
|
|
zht.Add(subtable, JToken.FromObject(ddf));
|
|
|
-
|
|
|
- }
|
|
|
- //}
|
|
|
}
|
|
|
htt.Add(zht);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
if (query != 1)
|
|
|
- {
|
|
|
resultObj.Add("[]", htt);
|
|
|
- }
|
|
|
- //分页自动添加当前页数和数量
|
|
|
+
|
|
|
+ // 分页自动添加当前页数和数量
|
|
|
if (page > 0 && count > 0)
|
|
|
{
|
|
|
resultObj.Add("page", page);
|
|
|
@@ -449,11 +392,10 @@ public class SelectTable : ISingleton
|
|
|
return total;
|
|
|
}
|
|
|
|
|
|
- //执行方法
|
|
|
+ // 执行方法
|
|
|
private void ExecFunc(JObject resultObj, KeyValuePair<string, JToken> item)
|
|
|
{
|
|
|
- JObject jb = JObject.Parse(item.Value.ToString());
|
|
|
- Type type = typeof(FuncList);
|
|
|
+ var jb = JObject.Parse(item.Value.ToString());
|
|
|
|
|
|
var dataJObj = new JObject();
|
|
|
foreach (var f in jb)
|
|
|
@@ -484,8 +426,8 @@ public class SelectTable : ISingleton
|
|
|
|
|
|
var tb = _db.Queryable(subtable, "tb");
|
|
|
|
|
|
- //select
|
|
|
- if (values["@column"].HasValue())
|
|
|
+ // select
|
|
|
+ if (values["@column"].IsNullOrEmpty())
|
|
|
{
|
|
|
ProcessColumn(subtable, selectrole, values, tb);
|
|
|
}
|
|
|
@@ -494,63 +436,59 @@ public class SelectTable : ISingleton
|
|
|
tb.Select(selectrole);
|
|
|
}
|
|
|
|
|
|
- //前几行
|
|
|
+ // 前几行
|
|
|
ProcessLimit(values, tb);
|
|
|
|
|
|
- //where
|
|
|
+ // where
|
|
|
ProcessWhere(subtable, values, tb, dd);
|
|
|
|
|
|
- //排序
|
|
|
+ // 排序
|
|
|
ProcessOrder(subtable, values, tb);
|
|
|
|
|
|
- //分组
|
|
|
+ // 分组
|
|
|
PrccessGroup(subtable, values, tb);
|
|
|
|
|
|
- //Having
|
|
|
+ // Having
|
|
|
ProcessHaving(values, tb);
|
|
|
+
|
|
|
return tb;
|
|
|
}
|
|
|
|
|
|
- //处理字段重命名 "@column":"toId:parentId",对应SQL是toId AS parentId,将查询的字段toId变为parentId返回
|
|
|
+ // 处理字段重命名 "@column":"toId:parentId",对应SQL是toId AS parentId,将查询的字段toId变为parentId返回
|
|
|
private void ProcessColumn(string subtable, string selectrole, JObject values, ISugarQueryable<ExpandoObject> tb)
|
|
|
{
|
|
|
var str = new System.Text.StringBuilder(100);
|
|
|
foreach (var item in values["@column"].ToString().Split(','))
|
|
|
{
|
|
|
- string[] ziduan = item.Split(':');
|
|
|
- string colName = ziduan[0];
|
|
|
+ var ziduan = item.Split(':');
|
|
|
+ var colName = ziduan[0];
|
|
|
var ma = new Regex(@"\((\w+)\)").Match(colName);
|
|
|
- //处理max,min这样的函数
|
|
|
+ // 处理max、min这样的函数
|
|
|
if (ma.Success && ma.Groups.Count > 1)
|
|
|
- {
|
|
|
colName = ma.Groups[1].Value;
|
|
|
- }
|
|
|
|
|
|
- //判断列表是否有权限 sum(1),sum(*),Count(1)这样的值直接有效
|
|
|
+ // 判断列表是否有权限 sum(1)、sum(*)、Count(1)这样的值直接有效
|
|
|
if (colName == "*" || int.TryParse(colName, out int colNumber) || (IsCol(subtable, colName) && _identitySvc.ColIsRole(colName, selectrole.Split(','))))
|
|
|
{
|
|
|
if (ziduan.Length > 1)
|
|
|
{
|
|
|
if (ziduan[1].Length > 20)
|
|
|
- {
|
|
|
throw new Exception("别名不能超过20个字符");
|
|
|
- }
|
|
|
+
|
|
|
str.Append(ziduan[0] + " as `" + ReplaceSQLChar(ziduan[1]) + "`,");
|
|
|
}
|
|
|
- //不对函数加``,解决sum(*),Count(1)等不能使用的问题
|
|
|
+ // 不对函数加``,解决sum(*)、Count(1)等不能使用的问题
|
|
|
else if (ziduan[0].Contains('('))
|
|
|
{
|
|
|
str.Append(ziduan[0] + ",");
|
|
|
}
|
|
|
else
|
|
|
str.Append("`" + ziduan[0] + "`" + ",");
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
if (string.IsNullOrEmpty(str.ToString()))
|
|
|
- {
|
|
|
throw new Exception($"表名{subtable}没有可查询的字段!");
|
|
|
- }
|
|
|
+
|
|
|
tb.Select(str.ToString().TrimEnd(','));
|
|
|
}
|
|
|
|
|
|
@@ -563,11 +501,10 @@ public class SelectTable : ISingleton
|
|
|
/// <param name="dd"></param>
|
|
|
private void ProcessWhere(string subtable, JObject values, ISugarQueryable<ExpandoObject> tb, JObject dd)
|
|
|
{
|
|
|
- List<IConditionalModel> conModels = new List<IConditionalModel>();
|
|
|
- if (values["identity"].HasValue())
|
|
|
- {
|
|
|
+ var conModels = new List<IConditionalModel>();
|
|
|
+ if (values["identity"].IsNullOrEmpty())
|
|
|
conModels.Add(new ConditionalModel() { FieldName = values["identity"].ToString(), ConditionalType = ConditionalType.Equal, FieldValue = _identitySvc.GetUserIdentity() });
|
|
|
- }
|
|
|
+
|
|
|
foreach (var va in values)
|
|
|
{
|
|
|
string key = va.Key.Trim();
|
|
|
@@ -576,59 +513,52 @@ public class SelectTable : ISingleton
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
- if (key.EndsWith("$"))//模糊查询
|
|
|
+ if (key.EndsWith("$")) // 模糊查询
|
|
|
{
|
|
|
FuzzyQuery(subtable, conModels, va);
|
|
|
}
|
|
|
- else if (key.EndsWith("{}"))//逻辑运算
|
|
|
+ else if (key.EndsWith("{}")) // 逻辑运算
|
|
|
{
|
|
|
ConditionQuery(subtable, conModels, va);
|
|
|
}
|
|
|
- else if (key.EndsWith("%"))//bwtween查询
|
|
|
+ else if (key.EndsWith("%")) // bwtween查询
|
|
|
{
|
|
|
ConditionBetween(subtable, conModels, va, tb);
|
|
|
-
|
|
|
}
|
|
|
else if (key.EndsWith("@")) // 关联上一个table
|
|
|
{
|
|
|
if (dd == null)
|
|
|
- {
|
|
|
continue;
|
|
|
- }
|
|
|
- string[] str = fieldValue.Split('/');
|
|
|
+
|
|
|
+ var str = fieldValue.Split('/');
|
|
|
var lastTableRecord = ((JObject)dd[str[^2]]);
|
|
|
if (!lastTableRecord.ContainsKey(str[^1]))
|
|
|
- {
|
|
|
throw new Exception($"找不到关联列:{str},请在{str[^2]}@column中设置");
|
|
|
- }
|
|
|
- string value = lastTableRecord[str[^1]].ToString();
|
|
|
|
|
|
+ var value = lastTableRecord[str[^1]].ToString();
|
|
|
conModels.Add(new ConditionalModel() { FieldName = key.TrimEnd('@'), ConditionalType = ConditionalType.Equal, FieldValue = value });
|
|
|
-
|
|
|
}
|
|
|
- else if (key.EndsWith("~"))//不等于 这里应该是正则匹配
|
|
|
+ else if (key.EndsWith("~")) // 不等于(应该是正则匹配)
|
|
|
{
|
|
|
//conModels.Add(new ConditionalModel() { FieldName = key.TrimEnd('~'), ConditionalType = ConditionalType.NoEqual, FieldValue = fieldValue });
|
|
|
}
|
|
|
- else if (IsCol(subtable, key.TrimEnd('!'))) //其他where条件
|
|
|
+ else if (IsCol(subtable, key.TrimEnd('!'))) // 其他where条件
|
|
|
{
|
|
|
ConditionEqual(subtable, conModels, va);
|
|
|
}
|
|
|
}
|
|
|
if (conModels.Any())
|
|
|
- {
|
|
|
tb.Where(conModels);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
// "@having":"function0(...)?value0;function1(...)?value1;function2(...)?value2...",
|
|
|
// SQL函数条件,一般和 @group一起用,函数一般在 @column里声明
|
|
|
private void ProcessHaving(JObject values, ISugarQueryable<ExpandoObject> tb)
|
|
|
{
|
|
|
- if (values["@having"].HasValue())
|
|
|
+ if (values["@having"].IsNullOrEmpty())
|
|
|
{
|
|
|
- List<IConditionalModel> hw = new List<IConditionalModel>();
|
|
|
- List<string> havingItems = new List<string>();
|
|
|
+ var hw = new List<IConditionalModel>();
|
|
|
+ var havingItems = new List<string>();
|
|
|
if (values["@having"].HasValues)
|
|
|
{
|
|
|
havingItems = values["@having"].Select(p => p.ToString()).ToList();
|
|
|
@@ -639,7 +569,7 @@ public class SelectTable : ISingleton
|
|
|
}
|
|
|
foreach (var item in havingItems)
|
|
|
{
|
|
|
- string and = item.ToString();
|
|
|
+ var and = item.ToString();
|
|
|
var model = new ConditionalModel();
|
|
|
if (and.Contains(">="))
|
|
|
{
|
|
|
@@ -649,7 +579,6 @@ public class SelectTable : ISingleton
|
|
|
}
|
|
|
else if (and.Contains("<="))
|
|
|
{
|
|
|
-
|
|
|
model.FieldName = and.Split(new string[] { "<=" }, StringSplitOptions.RemoveEmptyEntries)[0];
|
|
|
model.ConditionalType = ConditionalType.LessThanOrEqual;
|
|
|
model.FieldValue = and.Split(new string[] { "<=" }, StringSplitOptions.RemoveEmptyEntries)[1];
|
|
|
@@ -680,45 +609,42 @@ public class SelectTable : ISingleton
|
|
|
}
|
|
|
hw.Add(model);
|
|
|
}
|
|
|
-
|
|
|
//var d = db.Context.Utilities.ConditionalModelToSql(hw);
|
|
|
//tb.Having(d.Key, d.Value);
|
|
|
tb.Having(string.Join(",", havingItems));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //"@group":"column0,column1...",分组方式。如果 @column里声明了Table的id,则id也必须在 @group中声明;其它情况下必须满足至少一个条件:
|
|
|
- //1.分组的key在 @column里声明
|
|
|
- //2.Table主键在 @group中声明
|
|
|
+ // "@group":"column0,column1...",分组方式。如果 @column里声明了Table的id,则id也必须在 @group中声明;其它情况下必须满足至少一个条件:
|
|
|
+ // 1.分组的key在 @column里声明
|
|
|
+ // 2.Table主键在 @group中声明
|
|
|
private void PrccessGroup(string subtable, JObject values, ISugarQueryable<ExpandoObject> tb)
|
|
|
{
|
|
|
- if (values["@group"].HasValue())
|
|
|
+ if (values["@group"].IsNullOrEmpty())
|
|
|
{
|
|
|
- List<GroupByModel> groupList = new();//多库兼容写法
|
|
|
+ var groupList = new List<GroupByModel>(); // 多库兼容写法
|
|
|
foreach (var col in values["@group"].ToString().Split(','))
|
|
|
{
|
|
|
if (IsCol(subtable, col))
|
|
|
{
|
|
|
- //str.Append(and + ",");
|
|
|
+ // str.Append(and + ",");
|
|
|
groupList.Add(new GroupByModel() { FieldName = col });
|
|
|
}
|
|
|
}
|
|
|
if (groupList.Any())
|
|
|
- {
|
|
|
tb.GroupBy(groupList);
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //处理排序 "@order":"name-,id"查询按 name降序、id默认顺序 排序的User数组
|
|
|
+ // 处理排序 "@order":"name-,id"查询按 name降序、id默认顺序 排序的User数组
|
|
|
private void ProcessOrder(string subtable, JObject values, ISugarQueryable<ExpandoObject> tb)
|
|
|
{
|
|
|
- if (values["@order"].HasValue())
|
|
|
+ if (values["@order"].IsNullOrEmpty())
|
|
|
{
|
|
|
- List<OrderByModel> orderList = new(); //多库兼容写法
|
|
|
+ var orderList = new List<OrderByModel>(); // 多库兼容写法
|
|
|
foreach (var item in values["@order"].ToString().Split(','))
|
|
|
{
|
|
|
- string col = item.Replace("-", "").Replace("+", "").Replace(" desc", "").Replace(" asc", "");//增加对原生排序的支持
|
|
|
+ string col = item.Replace("-", "").Replace("+", "").Replace(" desc", "").Replace(" asc", ""); // 增加对原生排序的支持
|
|
|
if (IsCol(subtable, col))
|
|
|
{
|
|
|
orderList.Add(new OrderByModel()
|
|
|
@@ -730,9 +656,7 @@ public class SelectTable : ISingleton
|
|
|
}
|
|
|
|
|
|
if (orderList.Any())
|
|
|
- {
|
|
|
tb.OrderBy(orderList);
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -743,31 +667,33 @@ public class SelectTable : ISingleton
|
|
|
/// <param name="tb"></param>
|
|
|
private void ProcessLimit(JObject values, ISugarQueryable<ExpandoObject> tb)
|
|
|
{
|
|
|
- if (values["@count"].HasValue())
|
|
|
+ if (values["@count"].IsNullOrEmpty())
|
|
|
{
|
|
|
int c = values["@count"].ToObject<int>();
|
|
|
tb.Take(c);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- //条件查询 "key{}":"条件0,条件1...",条件为任意SQL比较表达式字符串,非Number类型必须用''包含条件的值,如'a'
|
|
|
- //&, |, ! 逻辑运算符,对应数据库 SQL 中的 AND, OR, NOT。
|
|
|
- // 横或纵与:同一字段的值内条件默认 | 或连接,不同字段的条件默认 & 与连接。
|
|
|
- // ① & 可用于"key&{}":"条件"等
|
|
|
- // ② | 可用于"key|{}":"条件", "key|{}":[] 等,一般可省略
|
|
|
- // ③ ! 可单独使用,如"key!":Object,也可像&,|一样配合其他功能符使用
|
|
|
+ // 条件查询 "key{}":"条件0,条件1...",条件为任意SQL比较表达式字符串,非Number类型必须用''包含条件的值,如'a'
|
|
|
+ // &, |, ! 逻辑运算符,对应数据库 SQL 中的 AND, OR, NOT。
|
|
|
+ // 横或纵与:同一字段的值内条件默认 | 或连接,不同字段的条件默认 & 与连接。
|
|
|
+ // ① & 可用于"key&{}":"条件"等
|
|
|
+ // ② | 可用于"key|{}":"条件", "key|{}":[] 等,一般可省略
|
|
|
+ // ③ ! 可单独使用,如"key!":Object,也可像&,|一样配合其他功能符使用
|
|
|
private void ConditionQuery(string subtable, List<IConditionalModel> conModels, KeyValuePair<string, JToken> va)
|
|
|
{
|
|
|
- string vakey = va.Key.Trim();
|
|
|
- string field = vakey.TrimEnd("{}".ToCharArray());
|
|
|
- string columnName = field.TrimEnd(new char[] { '&', '|' });
|
|
|
+ var vakey = va.Key.Trim();
|
|
|
+ var field = vakey.TrimEnd("{}".ToCharArray());
|
|
|
+ var columnName = field.TrimEnd(new char[] { '&', '|' });
|
|
|
IsCol(subtable, columnName);
|
|
|
var ddt = new List<KeyValuePair<WhereType, ConditionalModel>>();
|
|
|
foreach (var and in va.Value.ToString().Split(','))
|
|
|
{
|
|
|
- var model = new ConditionalModel();
|
|
|
- model.FieldName = columnName;
|
|
|
+ var model = new ConditionalModel
|
|
|
+ {
|
|
|
+ FieldName = columnName
|
|
|
+ };
|
|
|
+
|
|
|
if (and.StartsWith(">="))
|
|
|
{
|
|
|
model.ConditionalType = ConditionalType.GreaterThanOrEqual;
|
|
|
@@ -775,7 +701,6 @@ public class SelectTable : ISingleton
|
|
|
}
|
|
|
else if (and.StartsWith("<="))
|
|
|
{
|
|
|
-
|
|
|
model.ConditionalType = ConditionalType.LessThanOrEqual;
|
|
|
model.FieldValue = and.TrimStart("<=".ToCharArray());
|
|
|
}
|
|
|
@@ -793,10 +718,8 @@ public class SelectTable : ISingleton
|
|
|
ddt.Add(new KeyValuePair<WhereType, ConditionalModel>(field.EndsWith("!") ? WhereType.Or : WhereType.And, model));
|
|
|
}
|
|
|
conModels.Add(new ConditionalCollections() { ConditionalList = ddt });
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// "key%":"start,end" => "key%":["start,end"],其中 start 和 end 都只能为 Boolean, Number, String 中的一种,如 "2017-01-01,2019-01-01" ,["1,90000", "82001,100000"] ,可用于连续范围内的筛选
|
|
|
/// 目前不支持数组形式
|
|
|
@@ -807,9 +730,9 @@ public class SelectTable : ISingleton
|
|
|
/// <param name="tb"></param>
|
|
|
private void ConditionBetween(string subtable, List<IConditionalModel> conModels, KeyValuePair<string, JToken> va, ISugarQueryable<ExpandoObject> tb)
|
|
|
{
|
|
|
- string vakey = va.Key.Trim();
|
|
|
- string field = vakey.TrimEnd("%".ToCharArray());
|
|
|
- List<string> inValues = new List<string>();
|
|
|
+ var vakey = va.Key.Trim();
|
|
|
+ var field = vakey.TrimEnd("%".ToCharArray());
|
|
|
+ var inValues = new List<string>();
|
|
|
if (va.Value.HasValues)
|
|
|
{
|
|
|
foreach (var cm in va.Value)
|
|
|
@@ -821,12 +744,13 @@ public class SelectTable : ISingleton
|
|
|
{
|
|
|
inValues.Add(va.Value.ToString());
|
|
|
}
|
|
|
+
|
|
|
for (var i = 0; i < inValues.Count; i++)
|
|
|
{
|
|
|
var fileds = inValues[i].Split(',');
|
|
|
if (fileds.Length == 2)
|
|
|
{
|
|
|
- string type = FuncList.GetValueCSharpType(fileds[0]);
|
|
|
+ var type = FuncList.GetValueCSharpType(fileds[0]);
|
|
|
ObjectFuncModel f = ObjectFuncModel.Create("between", field, $"{{{type}}}:{fileds[0]}", $"{{{type}}}:{fileds[1]}");
|
|
|
tb.Where(f);
|
|
|
}
|
|
|
@@ -842,8 +766,8 @@ public class SelectTable : ISingleton
|
|
|
/// <param name="key"></param>
|
|
|
private void ConditionEqual(string subtable, List<IConditionalModel> conModels, KeyValuePair<string, JToken> va)
|
|
|
{
|
|
|
- string key = va.Key;
|
|
|
- string fieldValue = va.Value.ToString();
|
|
|
+ var key = va.Key;
|
|
|
+ var fieldValue = va.Value.ToString();
|
|
|
// in / not in
|
|
|
if (va.Value is JArray)
|
|
|
{
|
|
|
@@ -856,7 +780,6 @@ public class SelectTable : ISingleton
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
-
|
|
|
if (string.IsNullOrEmpty(fieldValue))
|
|
|
{
|
|
|
// is not null or ''
|
|
|
@@ -870,7 +793,6 @@ public class SelectTable : ISingleton
|
|
|
{
|
|
|
conModels.Add(new ConditionalModel() { FieldName = key.TrimEnd('!'), FieldValue = null });
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
// = / !=
|
|
|
else
|
|
|
@@ -885,15 +807,15 @@ public class SelectTable : ISingleton
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //模糊搜索 "key$":"SQL搜索表达式" => "key$":["SQL搜索表达式"],任意SQL搜索表达式字符串,如 %key%(包含key), key%(以key开始), %k%e%y%(包含字母k,e,y) 等,%表示任意字符
|
|
|
+ // 模糊搜索 "key$":"SQL搜索表达式" => "key$":["SQL搜索表达式"],任意SQL搜索表达式字符串,如 %key%(包含key), key%(以key开始), %k%e%y%(包含字母k,e,y) 等,%表示任意字符
|
|
|
private void FuzzyQuery(string subtable, List<IConditionalModel> conModels, KeyValuePair<string, JToken> va)
|
|
|
{
|
|
|
- string vakey = va.Key.Trim();
|
|
|
- string fieldValue = va.Value.ToString();
|
|
|
+ var vakey = va.Key.Trim();
|
|
|
+ var fieldValue = va.Value.ToString();
|
|
|
var conditionalType = ConditionalType.Like;
|
|
|
if (IsCol(subtable, vakey.TrimEnd('$')))
|
|
|
{
|
|
|
- //支持三种like查询
|
|
|
+ // 支持三种like查询
|
|
|
if (fieldValue.StartsWith("%") && fieldValue.EndsWith("%"))
|
|
|
{
|
|
|
conditionalType = ConditionalType.Like;
|
|
|
@@ -910,16 +832,12 @@ public class SelectTable : ISingleton
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- //处理sql注入
|
|
|
+ // 处理sql注入
|
|
|
private string ReplaceSQLChar(string str)
|
|
|
{
|
|
|
- if (str == String.Empty)
|
|
|
- return String.Empty;
|
|
|
+ if (string.IsNullOrWhiteSpace(str))
|
|
|
+ return string.Empty;
|
|
|
+
|
|
|
str = str.Replace("'", "");
|
|
|
str = str.Replace(";", "");
|
|
|
str = str.Replace(",", "");
|
|
|
@@ -938,7 +856,7 @@ public class SelectTable : ISingleton
|
|
|
str = str.Replace("$", "");
|
|
|
str = str.Replace("\"", "");
|
|
|
|
|
|
- //删除与数据库相关的词
|
|
|
+ // 删除与数据库相关的词
|
|
|
str = Regex.Replace(str, "delete from", "", RegexOptions.IgnoreCase);
|
|
|
str = Regex.Replace(str, "drop table", "", RegexOptions.IgnoreCase);
|
|
|
str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
|
|
|
@@ -951,8 +869,6 @@ public class SelectTable : ISingleton
|
|
|
return str;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 单条插入
|
|
|
/// </summary>
|
|
|
@@ -965,14 +881,14 @@ public class SelectTable : ISingleton
|
|
|
role ??= _identitySvc.GetRole();
|
|
|
var dt = new Dictionary<string, object>();
|
|
|
|
|
|
- foreach (var f in cols)//遍历字段
|
|
|
+ foreach (var f in cols) // 遍历字段
|
|
|
{
|
|
|
if (//f.Key.ToLower() != "id" && //是否一定要传id
|
|
|
IsCol(tableName, f.Key) &&
|
|
|
(role.Insert.Column.Contains("*") || role.Insert.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
|
|
|
dt.Add(f.Key, FuncList.TransJObjectToSugarPara(f.Value));
|
|
|
}
|
|
|
- //如果外部没传id,就后端生成或使用数据库默认值,如果都没有会出错
|
|
|
+ // 如果外部没传Id,就后端生成或使用数据库默认值,如果都没有会出错
|
|
|
object id;
|
|
|
if (!dt.ContainsKey("id"))
|
|
|
{
|
|
|
@@ -987,6 +903,7 @@ public class SelectTable : ISingleton
|
|
|
|
|
|
return id;
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 为每天记录创建udpate sql
|
|
|
/// </summary>
|
|
|
@@ -998,9 +915,8 @@ public class SelectTable : ISingleton
|
|
|
{
|
|
|
role ??= _identitySvc.GetRole();
|
|
|
if (!record.ContainsKey("id"))
|
|
|
- {
|
|
|
throw Oops.Bah("未传主键id");
|
|
|
- }
|
|
|
+
|
|
|
var dt = new Dictionary<string, object>();
|
|
|
var sb = new StringBuilder(100);
|
|
|
object id = null;
|
|
|
@@ -1008,13 +924,12 @@ public class SelectTable : ISingleton
|
|
|
{
|
|
|
if (f.Key.Equals("id", StringComparison.OrdinalIgnoreCase))
|
|
|
{
|
|
|
- if (f.Value is JArray)//id数组
|
|
|
+ if (f.Value is JArray)
|
|
|
{
|
|
|
sb.Append($"{f.Key} in (@{f.Key})");
|
|
|
id = FuncList.TransJArrayToSugarPara(f.Value);
|
|
|
-
|
|
|
}
|
|
|
- else//单个id
|
|
|
+ else
|
|
|
{
|
|
|
sb.Append($"{f.Key}=@{f.Key}");
|
|
|
id = FuncList.TransJObjectToSugarPara(f.Value);
|
|
|
@@ -1041,18 +956,17 @@ public class SelectTable : ISingleton
|
|
|
{
|
|
|
role ??= _identitySvc.GetRole();
|
|
|
int count = 0;
|
|
|
- if (records is JArray)//遍历每行记录
|
|
|
+ if (records is JArray)
|
|
|
{
|
|
|
foreach (var record in records.ToObject<JObject[]>())
|
|
|
{
|
|
|
count += UpdateSingleRecord(tableName, record, role);
|
|
|
}
|
|
|
}
|
|
|
- else//单条记录
|
|
|
+ else
|
|
|
{
|
|
|
count = UpdateSingleRecord(tableName, records.ToObject<JObject>(), role);
|
|
|
}
|
|
|
-
|
|
|
return count;
|
|
|
}
|
|
|
}
|