mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Update .net core project
This commit is contained in:
parent
06a669a1da
commit
e5cc8d0a75
@ -320,6 +320,7 @@ namespace SqlSugar
|
||||
}
|
||||
public IInsertable<T> IgnoreColumns(Expression<Func<T, object>> columns)
|
||||
{
|
||||
if (columns == null) return this;
|
||||
var ignoreColumns = InsertBuilder.GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => this.SqlBuilder.GetNoTranslationColumnName(it)).ToList();
|
||||
this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it => !ignoreColumns.Any(ig => ig.Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase))).ToList();
|
||||
this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it => !ignoreColumns.Any(ig => ig.Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase))).ToList();
|
||||
@ -336,6 +337,7 @@ namespace SqlSugar
|
||||
|
||||
public IInsertable<T> InsertColumns(Expression<Func<T, object>> columns)
|
||||
{
|
||||
if (columns == null) return this;
|
||||
var ignoreColumns = InsertBuilder.GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => this.SqlBuilder.GetNoTranslationColumnName(it)).ToList();
|
||||
this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it => ignoreColumns.Any(ig => ig.Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || ignoreColumns.Any(ig => ig.Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase))).ToList();
|
||||
return this;
|
||||
@ -343,6 +345,7 @@ namespace SqlSugar
|
||||
|
||||
public IInsertable<T> InsertColumns(string[] columns)
|
||||
{
|
||||
if (columns == null) return this;
|
||||
this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it => columns.Any(ig => ig.Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase))|| columns.Any(ig => ig.Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase))).ToList();
|
||||
return this;
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class JsonDeleteResult
|
||||
{
|
||||
public int UpdateRows { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class JsonInsertResult
|
||||
{
|
||||
public int IdentityValue { get; set; }
|
||||
public int InsertCount { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class JsonQueryResult
|
||||
{
|
||||
public object Data { get; set; }
|
||||
public Dictionary<string, string> TableInfo{get;set;}
|
||||
public int ToTalRows { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
|
||||
public class JsonTableConfig
|
||||
{
|
||||
public string TableName { get; set; }
|
||||
public string TableDescription { get; set; }
|
||||
public List<IConditionalModel> Conditionals { get; set; }
|
||||
public bool? AllowQuery { get; set; }
|
||||
public bool? AllowUpdate { get; set; }
|
||||
public bool? AllowDelete { get; set; }
|
||||
public bool? AllowInsert { get; set; }
|
||||
public List<JsonColumnConfig> Columns { get; set; }
|
||||
}
|
||||
public class JsonColumnConfig
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string ValidateMessage { get; set; }
|
||||
public object Validate { get; set; }
|
||||
bool? AllowEdit { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class JsonUpdateResult
|
||||
{
|
||||
public int UpdateRows { get; set; }
|
||||
}
|
||||
}
|
16
Src/Asp.NetCore2/SqlSugar/Json2Sql/Enums/Json2SqlType.cs
Normal file
16
Src/Asp.NetCore2/SqlSugar/Json2Sql/Enums/Json2SqlType.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public enum JsonProviderType
|
||||
{
|
||||
Queryable,
|
||||
QueryableCount,
|
||||
Insertable,
|
||||
InsertableIdentity,
|
||||
Updateable,
|
||||
Deleteable
|
||||
}
|
||||
}
|
10
Src/Asp.NetCore2/SqlSugar/Json2Sql/Interface/IFuncModel.cs
Normal file
10
Src/Asp.NetCore2/SqlSugar/Json2Sql/Interface/IFuncModel.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface IFuncModel
|
||||
{
|
||||
}
|
||||
}
|
15
Src/Asp.NetCore2/SqlSugar/Json2Sql/Interface/IJsonClient.cs
Normal file
15
Src/Asp.NetCore2/SqlSugar/Json2Sql/Interface/IJsonClient.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface IJsonClient
|
||||
{
|
||||
ISqlSugarClient Context { get; set; }
|
||||
|
||||
IJsonProvider<JsonDeleteResult> Deleteable(string json);
|
||||
List<string> GetTableNameList(string json);
|
||||
IJsonProvider<JsonInsertResult> Insertable(string json);
|
||||
IJsonQueryableProvider<JsonQueryResult> Queryable(string json);
|
||||
IJsonProvider<JsonUpdateResult> Updateable(string json);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface IJsonDeleteableProvider<T> : IJsonProvider<T>
|
||||
{
|
||||
// IJsonQueryableProvider<T> UpdateColumns(string tableName, string[] columns);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface IJsonInsertableProvider<T> : IJsonProvider<T>
|
||||
{
|
||||
// IJsonQueryableProvider<T> UpdateColumns(string tableName, string[] columns);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
|
||||
public interface IJsonProvider<T>
|
||||
{
|
||||
List<SqlObjectResult> ToSqlList();
|
||||
SqlObjectResult ToSql();
|
||||
List<string> ToSqlString();
|
||||
T ToResult();
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface IJsonQueryableProvider<JsonQueryResult> : IJsonProvider<JsonQueryResult>
|
||||
{
|
||||
IJsonQueryableProvider<JsonQueryResult> ShowDesciption();
|
||||
IJsonQueryableProvider<JsonQueryResult> UseAuthentication(JsonTableConfig config);
|
||||
IJsonQueryableProvider<JsonQueryResult> UseAuthentication(List<JsonTableConfig> config);
|
||||
}
|
||||
}
|
19
Src/Asp.NetCore2/SqlSugar/Json2Sql/Interface/IJsonToModel.cs
Normal file
19
Src/Asp.NetCore2/SqlSugar/Json2Sql/Interface/IJsonToModel.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// Json to model
|
||||
/// </summary>
|
||||
public partial interface IContextMethods
|
||||
{
|
||||
List<OrderByModel> JsonToOrderByModels(string json);
|
||||
List<GroupByModel> JsonToGroupByModels(string json);
|
||||
List<Dictionary<string, object>> JsonToColumnsModels(string json);
|
||||
List<SelectModel> JsonToSelectModels(string json);
|
||||
IFuncModel JsonToSqlFuncModels(string json);
|
||||
JoinModel JsonToJoinModels(string json);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface IJsonUpdateableProvider<T> : IJsonProvider<T>
|
||||
{
|
||||
// IJsonQueryableProvider<T> UpdateColumns(string tableName, string[] columns);
|
||||
}
|
||||
}
|
18
Src/Asp.NetCore2/SqlSugar/Json2Sql/Interface/IModelToSql.cs
Normal file
18
Src/Asp.NetCore2/SqlSugar/Json2Sql/Interface/IModelToSql.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// Json Model to sql
|
||||
/// </summary>
|
||||
public partial interface ISqlBuilder
|
||||
{
|
||||
KeyValuePair<string, SugarParameter[]> OrderByModelToSql(List<OrderByModel> models);
|
||||
KeyValuePair<string, SugarParameter[]> GroupByModelToSql(List<GroupByModel> models);
|
||||
KeyValuePair<string, SugarParameter[]> SelectModelToSql(List<SelectModel> models);
|
||||
KeyValuePair<string, SugarParameter[]> FuncModelToSql(IFuncModel model);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial interface ISugarQueryable<T>
|
||||
{
|
||||
ISugarQueryable<T> Having(IFuncModel model);
|
||||
ISugarQueryable<T> OrderBy(List<OrderByModel> models);
|
||||
ISugarQueryable<T> GroupBy(List<GroupByModel> models);
|
||||
ISugarQueryable<T> Select(List<SelectModel> models);
|
||||
ISugarQueryable<T> AS(string tableName, string shortName);
|
||||
ISugarQueryable<T> AddJoinInfo(string tableName, string shortName, IFuncModel models, JoinType type = JoinType.Left);
|
||||
}
|
||||
}
|
17
Src/Asp.NetCore2/SqlSugar/Json2Sql/JsonModels/FuncModel.cs
Normal file
17
Src/Asp.NetCore2/SqlSugar/Json2Sql/JsonModels/FuncModel.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ObjectFuncModel: IFuncModel
|
||||
{
|
||||
public string FuncName { get; set; }
|
||||
public List<object> Parameters { get; set; }
|
||||
}
|
||||
public class ArrayFuncModel: IFuncModel
|
||||
{
|
||||
public List<object> Objects { get; set; }
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class GroupByModel
|
||||
{
|
||||
public string FieldName { get; set; }
|
||||
}
|
||||
}
|
15
Src/Asp.NetCore2/SqlSugar/Json2Sql/JsonModels/JoinModel.cs
Normal file
15
Src/Asp.NetCore2/SqlSugar/Json2Sql/JsonModels/JoinModel.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
|
||||
public class JoinModel
|
||||
{
|
||||
public string TableName { get; set; }
|
||||
public string ShortName { get; set; }
|
||||
public ObjectFuncModel OnWhereList { get; set; }
|
||||
public JoinType JoinType { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class JsonQueryParameter
|
||||
{
|
||||
public bool IsSelect { get; set; }
|
||||
public bool IsJoin { get; set; }
|
||||
public int? PageIndex { get; set; }
|
||||
public int? PageSize { get; set; } = 20;
|
||||
public bool JoinNoSelect { get { return IsJoin && !IsSelect; } }
|
||||
|
||||
public bool IsPage { get { return PageIndex != null; } }
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlObjectResult
|
||||
{
|
||||
|
||||
|
||||
public SqlObjectResult(KeyValuePair<string, List<SugarParameter>> keyValuePair, JsonProviderType jsonSqlType)
|
||||
{
|
||||
this.Sql = keyValuePair.Key;
|
||||
this.Parameters = keyValuePair.Value;
|
||||
this.JsonSqlType = jsonSqlType;
|
||||
}
|
||||
|
||||
public JsonProviderType JsonSqlType { get; set; }
|
||||
public string Sql { get; set; }
|
||||
public List<SugarParameter> Parameters { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class JsonTableNameInfo
|
||||
{
|
||||
public string TableName { get; set; }
|
||||
public string ShortName { get; set; }
|
||||
public string Scheme { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class OrderByModel
|
||||
{
|
||||
public string FieldName { get; set; }
|
||||
public OrderByType OrderByType { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SelectModel
|
||||
{
|
||||
public object FiledName { get; set; }
|
||||
public string AsName { get; set; }
|
||||
}
|
||||
}
|
46
Src/Asp.NetCore2/SqlSugar/Json2Sql/JsonToModel/Helper.cs
Normal file
46
Src/Asp.NetCore2/SqlSugar/Json2Sql/JsonToModel/Helper.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// SqlFunc to model
|
||||
/// </summary>
|
||||
public partial class ContextMethods : IContextMethods
|
||||
{
|
||||
private bool IsObjct(JToken sqlfunc)
|
||||
{
|
||||
return sqlfunc.Type == JTokenType.Object;
|
||||
}
|
||||
private bool IsArray(string sqlfunc)
|
||||
{
|
||||
return sqlfunc.StartsWith("[");
|
||||
}
|
||||
public static bool IsSqlFunc(JToken item, string fileName)
|
||||
{
|
||||
return item.Type == JTokenType.Object || fileName.ToLower().Contains("SqlFunc_");
|
||||
}
|
||||
private bool IsObject(JToken parameters)
|
||||
{
|
||||
return parameters.Type == JTokenType.Object;
|
||||
}
|
||||
private bool IsArray(JToken parameters)
|
||||
{
|
||||
return parameters.Type == JTokenType.Array;
|
||||
}
|
||||
private bool IsString(JToken parameters)
|
||||
{
|
||||
return parameters.Type == JTokenType.String;
|
||||
}
|
||||
private bool IsFieldName(JToken item)
|
||||
{
|
||||
return item.ObjToString().ToLower().Contains("fieldname");
|
||||
}
|
||||
private bool IsArraySingleItem(JToken item)
|
||||
{
|
||||
return IsArray(item) && item.Count() == 1;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// SqlFunc to model
|
||||
/// </summary>
|
||||
public partial class ContextMethods : IContextMethods
|
||||
{
|
||||
#region Root
|
||||
public ObjectFuncModel JsonToSqlFuncModels(JToken sqlfunc)
|
||||
{
|
||||
var key = sqlfunc.First();
|
||||
if (IsObjct(sqlfunc))
|
||||
{
|
||||
return GetFuncModelByObject(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetFuncModelByArray(sqlfunc);
|
||||
}
|
||||
}
|
||||
public IFuncModel JsonToSqlFuncModels(string sqlfunc)
|
||||
{
|
||||
if (IsArray(sqlfunc))
|
||||
{
|
||||
return GetFuncModelByArray(sqlfunc);
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetFuncModelByObject(sqlfunc);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Level 1
|
||||
private ObjectFuncModel GetFuncModelByArray(JToken sqlfunc)
|
||||
{
|
||||
ObjectFuncModel result = new ObjectFuncModel();
|
||||
result.Parameters = new List<object>();
|
||||
result.FuncName = "Sqlfunc_Format";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach (var item in sqlfunc)
|
||||
{
|
||||
result.Parameters.Add(GetParameter(item));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private ObjectFuncModel GetFuncModelByObject(JToken key)
|
||||
{
|
||||
ObjectFuncModel result = new ObjectFuncModel();
|
||||
JProperty jProperty = key.ToObject<JProperty>();
|
||||
result.FuncName = jProperty.Name;
|
||||
var parameters = jProperty.Value;
|
||||
result.Parameters = GetParameter(parameters);
|
||||
return result;
|
||||
}
|
||||
private IFuncModel GetFuncModelByObject(string sqlfunc)
|
||||
{
|
||||
var jObject = this.Context.Utilities.DeserializeObject<JObject>(sqlfunc);
|
||||
return JsonToSqlFuncModels(jObject);
|
||||
}
|
||||
private IFuncModel GetFuncModelByArray(string sqlfunc)
|
||||
{
|
||||
ObjectFuncModel result = new ObjectFuncModel();
|
||||
result.Parameters = new List<object>();
|
||||
result.FuncName = "Sqlfunc_Format";
|
||||
var jArrary = this.Context.Utilities.DeserializeObject<JArray>(sqlfunc);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach (var item in jArrary)
|
||||
{
|
||||
result.Parameters.Add(GetParameter(item));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class ContextMethods : IContextMethods
|
||||
{
|
||||
#region Root
|
||||
public List<object> GetParameter(JToken parameters)
|
||||
{
|
||||
List<object> result = new List<object>();
|
||||
if (IsString(parameters))
|
||||
{
|
||||
result.Add(GetStringParameters(parameters));
|
||||
}
|
||||
else if (IsArray(parameters))
|
||||
{
|
||||
result.AddRange(GetArrayParameters(parameters));
|
||||
}
|
||||
else if (IsObject(parameters))
|
||||
{
|
||||
result.Add(GetObjectParameters(parameters));
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(GetObjectErrorParameters(parameters));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Level1
|
||||
private static List<object> GetObjectErrorParameters(JToken parameters)
|
||||
{
|
||||
Check.Exception(true, ErrorMessage.GetThrowMessage($" {parameters.ToString()} format is error ", $" {parameters.ToString()} 格式错误"));
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<object> GetArrayParameters(JToken parameters)
|
||||
{
|
||||
List<object> result = new List<object>();
|
||||
foreach (var item in parameters)
|
||||
{
|
||||
result.Add(GetParameter(item));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public object GetObjectParameters(JToken parameters)
|
||||
{
|
||||
return JsonToSqlFuncModels(parameters);
|
||||
}
|
||||
|
||||
public object GetStringParameters(JToken parameters)
|
||||
{
|
||||
return parameters.ObjToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class ContextMethods : IContextMethods
|
||||
{
|
||||
public List<Dictionary<string, object>> JsonToColumnsModels(string json)
|
||||
{
|
||||
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
|
||||
List<SelectModel> conditionalModels = new List<SelectModel>();
|
||||
if (IsArray(json))
|
||||
{
|
||||
return GetColumnsByArray(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetColumnsByObject(json);
|
||||
}
|
||||
}
|
||||
private List<Dictionary<string, object>> GetColumnsByObject(string json)
|
||||
{
|
||||
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
|
||||
var dic = this.Context.Utilities.DeserializeObject<Dictionary<string,object>>(json);
|
||||
result.Add( GetColumns(dic));
|
||||
return result;
|
||||
}
|
||||
private List<Dictionary<string, object>> GetColumnsByArray(string json)
|
||||
{
|
||||
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
|
||||
var jarray = this.Context.Utilities.DeserializeObject<List<Dictionary<string,object>>>(json);
|
||||
foreach (var item in jarray)
|
||||
{
|
||||
result.Add(GetColumns(item));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Dictionary<string, object> GetColumns(Dictionary<string, object> dictionary)
|
||||
{
|
||||
Dictionary<string, object> result= new Dictionary<string, object>();
|
||||
foreach (var item in dictionary)
|
||||
{
|
||||
var value = GetValue(item);
|
||||
result.Add(item.Key, value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static object GetValue(KeyValuePair<string, object> item)
|
||||
{
|
||||
if (item.Value == null)
|
||||
return null;
|
||||
var valueString = item.Value.ToString();
|
||||
var vallue = Json2SqlHelper.GetValue(valueString);
|
||||
var type = Json2SqlHelper.GetType(valueString);
|
||||
return UtilMethods.ConvertDataByTypeName(type,vallue);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class ContextMethods : IContextMethods
|
||||
{
|
||||
public List<GroupByModel> JsonToGroupByModels(string json)
|
||||
{
|
||||
List<GroupByModel> conditionalModels = new List<GroupByModel>();
|
||||
var jarray = this.Context.Utilities.DeserializeObject<JArray>(json);
|
||||
foreach (var item in jarray)
|
||||
{
|
||||
if (item.ObjToString().ToLower().Contains("fieldname"))
|
||||
{
|
||||
var model = item.ToObject<GroupByModel>();
|
||||
conditionalModels.Add(model);
|
||||
}
|
||||
else
|
||||
{
|
||||
conditionalModels.Add(new GroupByModel() { FieldName = item.ObjToString() });
|
||||
}
|
||||
}
|
||||
return conditionalModels;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// Json to model
|
||||
/// </summary>
|
||||
public partial class ContextMethods : IContextMethods
|
||||
{
|
||||
|
||||
public JoinModel JsonToJoinModels(string json)
|
||||
{
|
||||
JoinModel conditionalModels = new JoinModel();
|
||||
var array = JArray.Parse(json);
|
||||
Check.Exception(array.Count != 3, json + " format error");
|
||||
var tableName = array[0];
|
||||
var shortName = array[1];
|
||||
var onWhere = array[2];
|
||||
JoinModel result = new JoinModel();
|
||||
result.TableName = tableName.ObjToString();
|
||||
result.ShortName = shortName.ObjToString();
|
||||
result.OnWhereList = JsonToSqlFuncModels(onWhere);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class ContextMethods : IContextMethods
|
||||
{
|
||||
public List<OrderByModel> JsonToOrderByModels(string json)
|
||||
{
|
||||
List<OrderByModel> conditionalModels = new List<OrderByModel>();
|
||||
var jarray = this.Context.Utilities.DeserializeObject<JArray>(json);
|
||||
foreach (var item in jarray)
|
||||
{
|
||||
if (IsFieldName(item))
|
||||
{
|
||||
var model = item.ToObject<OrderByModel>();
|
||||
conditionalModels.Add(model);
|
||||
}
|
||||
else if (item.Type == JTokenType.String)
|
||||
{
|
||||
conditionalModels.Add(new OrderByModel() { FieldName = item.ObjToString() });
|
||||
}
|
||||
else if (item.Type == JTokenType.Array)
|
||||
{
|
||||
conditionalModels.Add(new OrderByModel()
|
||||
{
|
||||
FieldName = item[0].ObjToString(),
|
||||
OrderByType = item[1].ObjToString().ToLower() == "desc" ? OrderByType.Desc : OrderByType.Asc
|
||||
});
|
||||
}
|
||||
}
|
||||
return conditionalModels;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class ContextMethods : IContextMethods
|
||||
{
|
||||
public List<SelectModel> JsonToSelectModels(string json)
|
||||
{
|
||||
List<SelectModel> conditionalModels = new List<SelectModel>();
|
||||
var jarray = this.Context.Utilities.DeserializeObject<JArray>(json);
|
||||
foreach (var item in jarray)
|
||||
{
|
||||
if (IsFieldName(item))
|
||||
{
|
||||
var model = item.ToObject<SelectModel>();
|
||||
conditionalModels.Add(model);
|
||||
}
|
||||
else if (IsString(item))
|
||||
{
|
||||
conditionalModels.Add(new SelectModel() { FiledName = item.ObjToString(), AsName = item.ObjToString().Replace(".", "_") });
|
||||
}
|
||||
else if (IsArraySingleItem(item))
|
||||
{
|
||||
object fileName = item[0].ObjToString();
|
||||
var asName = item[0].ObjToString().Replace(".", "_");
|
||||
if (IsSqlFunc(item[0], fileName.ObjToString()))
|
||||
{
|
||||
fileName = JsonToSqlFuncModels(item[0]);
|
||||
}
|
||||
conditionalModels.Add(new SelectModel()
|
||||
{
|
||||
FiledName = fileName,
|
||||
AsName = asName
|
||||
});
|
||||
|
||||
}
|
||||
else if (IsArray(item))
|
||||
{
|
||||
object fileName = item[0].ObjToString();
|
||||
var asName = item[1].ObjToString().Replace(".", "_");
|
||||
if (IsSqlFunc(item[0], fileName.ObjToString()))
|
||||
{
|
||||
fileName = JsonToSqlFuncModels(item[0]);
|
||||
}
|
||||
conditionalModels.Add(new SelectModel()
|
||||
{
|
||||
FiledName = fileName,
|
||||
AsName = asName
|
||||
});
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
conditionalModels.Add(new SelectModel()
|
||||
{
|
||||
FiledName = item.ObjToString().Trim(),
|
||||
AsName = item.ObjToString().Trim()
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
return conditionalModels;
|
||||
}
|
||||
}
|
||||
}
|
135
Src/Asp.NetCore2/SqlSugar/Json2Sql/ModelToSql/FuncModelToSql.cs
Normal file
135
Src/Asp.NetCore2/SqlSugar/Json2Sql/ModelToSql/FuncModelToSql.cs
Normal file
@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
///Json model to sql
|
||||
/// </summary>
|
||||
public abstract partial class SqlBuilderProvider : SqlBuilderAccessory, ISqlBuilder
|
||||
{
|
||||
#region Root
|
||||
public KeyValuePair<string, SugarParameter[]> FuncModelToSql(IFuncModel model)
|
||||
{
|
||||
ObjectFuncModel data = model as ObjectFuncModel;
|
||||
var name = data.FuncName;
|
||||
var parameters = data.Parameters;
|
||||
var dbMethods = this.Context.Queryable<object>().QueryBuilder.LambdaExpressions.DbMehtods;
|
||||
var methods = GetAllMethods(dbMethods);
|
||||
var methodName = GetMethodName(name, methods);
|
||||
var methodInfo = GetMethod(dbMethods, methodName);
|
||||
var pars = methodInfo.GetParameters();
|
||||
|
||||
var resSql = "";
|
||||
var resPars = new List<SugarParameter>();
|
||||
resSql = GetSql(parameters, dbMethods, methodName, methodInfo, pars, resPars);
|
||||
return new KeyValuePair<string, SugarParameter[]>(resSql, resPars.ToArray());
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Level2
|
||||
private string GetSql(List<object> parameters, IDbMethods dbMethods, string methodName, System.Reflection.MethodInfo methodInfo, System.Reflection.ParameterInfo[] pars, List<SugarParameter> resPars)
|
||||
{
|
||||
string resSql;
|
||||
if (IsNoParameter(pars))
|
||||
{
|
||||
resSql = GetNoParameterMehtodSql(dbMethods, methodInfo);
|
||||
}
|
||||
else if (IsFormatMethod(methodName))
|
||||
{
|
||||
resSql = GetFormatMethodSql(parameters, resPars);
|
||||
}
|
||||
else if (IsSqlFuncMethod(pars))
|
||||
{
|
||||
resSql = GetSqlFuncSql(parameters, dbMethods, methodName, methodInfo, resPars);
|
||||
}
|
||||
else
|
||||
{
|
||||
resSql = GetNoSupportMethodSql(methodInfo);
|
||||
}
|
||||
|
||||
return resSql;
|
||||
}
|
||||
private static System.Reflection.MethodInfo GetMethod(IDbMethods dbMethods, string methodName)
|
||||
{
|
||||
return dbMethods.GetType().GetMethods()
|
||||
.Where(it => it.Name == methodName)
|
||||
.Where(it => it.Name != "Equals" || it.GetParameters().Length == 1 && it.GetParameters().First().ParameterType == typeof(MethodCallExpressionModel))
|
||||
.FirstOrDefault();
|
||||
}
|
||||
private static string GetMethodName(string name, List<string> methods)
|
||||
{
|
||||
var result = methods.FirstOrDefault(it => name.EqualCase("SqlFunc_" + it) || name.EqualCase(it));
|
||||
Check.Exception(result == null, $" { name } is error ");
|
||||
return result;
|
||||
}
|
||||
private static List<string> GetAllMethods(IDbMethods dbMethods)
|
||||
{
|
||||
return new ReflectionInoCacheService().GetOrCreate("Json2SqlGetFuncSql", () =>
|
||||
dbMethods.GetType()
|
||||
.GetMethods().Where(it => it.Name != "GetHashCode").Select(it => it.Name).ToList());
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Level3
|
||||
private static string GetNoSupportMethodSql(System.Reflection.MethodInfo methodInfo)
|
||||
{
|
||||
throw new Exception(methodInfo.Name);
|
||||
}
|
||||
private string GetSqlFuncSql(List<object> parameters, IDbMethods dbMethods, string methodName, System.Reflection.MethodInfo methodInfo, List<SugarParameter> resPars)
|
||||
{
|
||||
string resSql;
|
||||
var args = new List<MethodCallExpressionArgs>();
|
||||
foreach (var item in parameters)
|
||||
{
|
||||
var value = GetSqlPart(item, resPars);
|
||||
args.Add(new MethodCallExpressionArgs
|
||||
{
|
||||
MemberName = value,
|
||||
MemberValue = value,
|
||||
IsMember = true
|
||||
});
|
||||
}
|
||||
resSql = methodInfo.Invoke(dbMethods, new object[] { new MethodCallExpressionModel() {
|
||||
Name=methodName,
|
||||
Args=args
|
||||
} }).ObjToString();
|
||||
return resSql;
|
||||
}
|
||||
private string GetFormatMethodSql(List<object> parameters, List<SugarParameter> resPars)
|
||||
{
|
||||
string resSql;
|
||||
var objects = new List<string>();
|
||||
foreach (var item in parameters)
|
||||
{
|
||||
var value = GetSqlPart(item, resPars);
|
||||
objects.Add(value.ObjToString());
|
||||
}
|
||||
resSql = string.Join(" ", string.Join(" ", objects));
|
||||
return resSql;
|
||||
}
|
||||
private static string GetNoParameterMehtodSql(IDbMethods dbMethods, System.Reflection.MethodInfo methodInfo)
|
||||
{
|
||||
return methodInfo.Invoke(dbMethods, new object[] { }).ObjToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Helper
|
||||
private static bool IsSqlFuncMethod(System.Reflection.ParameterInfo[] pars)
|
||||
{
|
||||
return pars.First().ParameterType == typeof(MethodCallExpressionModel);
|
||||
}
|
||||
private static bool IsFormatMethod(string methodName)
|
||||
{
|
||||
return methodName.EqualCase("format");
|
||||
}
|
||||
private static bool IsNoParameter(System.Reflection.ParameterInfo[] pars)
|
||||
{
|
||||
return pars.Length == 0;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public abstract partial class SqlBuilderProvider : SqlBuilderAccessory, ISqlBuilder
|
||||
{
|
||||
public KeyValuePair<string, SugarParameter[]> GroupByModelToSql(List<GroupByModel> models)
|
||||
{
|
||||
StringBuilder sql = new StringBuilder("");
|
||||
SugarParameter[] pars = new SugarParameter[] { };
|
||||
foreach (var item in models)
|
||||
{
|
||||
if (item is GroupByModel)
|
||||
{
|
||||
var orderByModel = item as GroupByModel;
|
||||
sql.Append($" {this.GetTranslationColumnName(orderByModel.FieldName.ToSqlFilter())} ,");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return new KeyValuePair<string, SugarParameter[]>(sql.ToString().TrimEnd(','), pars);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public abstract partial class SqlBuilderProvider : SqlBuilderAccessory, ISqlBuilder
|
||||
{
|
||||
public KeyValuePair<string, SugarParameter[]> OrderByModelToSql(List<OrderByModel> models)
|
||||
{
|
||||
StringBuilder sql = new StringBuilder("");
|
||||
SugarParameter[] pars = new SugarParameter[] { };
|
||||
foreach (var item in models)
|
||||
{
|
||||
if (item is OrderByModel)
|
||||
{
|
||||
var orderByModel = item as OrderByModel;
|
||||
sql.Append($" {this.GetTranslationColumnName(orderByModel.FieldName.ToSqlFilter())} {orderByModel.OrderByType.ToString().ToUpper()} ,");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return new KeyValuePair<string, SugarParameter[]>(sql.ToString().TrimEnd(','), pars);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public abstract partial class SqlBuilderProvider : SqlBuilderAccessory, ISqlBuilder
|
||||
{
|
||||
public KeyValuePair<string, SugarParameter[]> SelectModelToSql(List<SelectModel> models)
|
||||
{
|
||||
StringBuilder sql = new StringBuilder("");
|
||||
var pars = new List<SugarParameter> { };
|
||||
foreach (var item in models)
|
||||
{
|
||||
if (item is SelectModel)
|
||||
{
|
||||
var orderByModel = item as SelectModel;
|
||||
orderByModel.AsName=GetAsName(orderByModel);
|
||||
orderByModel.FiledName = GetSqlPart(orderByModel.FiledName, pars).ObjToString();
|
||||
AppendFiledName(sql, orderByModel);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
return new KeyValuePair<string, SugarParameter[]>(sql.ToString().TrimEnd(','), pars.ToArray());
|
||||
}
|
||||
|
||||
private string GetAsName(SelectModel orderByModel)
|
||||
{
|
||||
if (orderByModel.AsName.IsNullOrEmpty())
|
||||
{
|
||||
orderByModel.AsName = orderByModel.FiledName.ObjToString();
|
||||
}
|
||||
return this.GetTranslationColumnName(orderByModel.AsName);
|
||||
}
|
||||
|
||||
private void AppendFiledName(StringBuilder sql, SelectModel orderByModel)
|
||||
{
|
||||
sql.Append($" {orderByModel.FiledName} AS {orderByModel.AsName} ,");
|
||||
}
|
||||
}
|
||||
}
|
146
Src/Asp.NetCore2/SqlSugar/Json2Sql/ModelToSql/SqlPart.cs
Normal file
146
Src/Asp.NetCore2/SqlSugar/Json2Sql/ModelToSql/SqlPart.cs
Normal file
@ -0,0 +1,146 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public abstract partial class SqlBuilderProvider : SqlBuilderAccessory, ISqlBuilder
|
||||
{
|
||||
#region Variable
|
||||
private string[] SqlSplicingOperator = new string[] { ">", ">=", "<", "<=", "(", ")", "=", "||", "&&","&","|" };
|
||||
#endregion
|
||||
|
||||
#region Root
|
||||
private string GetSqlPart(object value, List<SugarParameter> pars)
|
||||
{
|
||||
Check.Exception(value == null, $" FiledName is error ");
|
||||
if (IsSqlSplicingOperator(value))
|
||||
{
|
||||
return GetSqlSplicingOperator(value);
|
||||
}
|
||||
else if (IsString(value))
|
||||
{
|
||||
return GetSqlPartByString(value, pars);
|
||||
}
|
||||
else if (IsListObject(value))
|
||||
{
|
||||
return GetSqlPartByListObject(value, pars);
|
||||
}
|
||||
else if (IsObjectFunc(value))
|
||||
{
|
||||
return GetSqlPartByObjectFuncModel(value, pars);
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetSqlPartError(value);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Level2
|
||||
|
||||
private static string GetSqlSplicingOperator(object value)
|
||||
{
|
||||
var result= value.ObjToString();
|
||||
if (result == "||") return "AND";
|
||||
else if (result == "&&") return "OR";
|
||||
return result;
|
||||
}
|
||||
private static string GetSqlPartError(object value)
|
||||
{
|
||||
Check.Exception(value == null, $" {value} is error ");
|
||||
return null;
|
||||
}
|
||||
private string GetSqlPartByObjectFuncModel(object value, List<SugarParameter> pars)
|
||||
{
|
||||
var data = value as ObjectFuncModel;
|
||||
var obj = FuncModelToSql(data);
|
||||
pars.AddRange(obj.Value);
|
||||
return obj.Key;
|
||||
}
|
||||
private string GetSqlPartByListObject(object value, List<SugarParameter> pars)
|
||||
{
|
||||
var list = (value as List<object>);
|
||||
if (list.Count == 1)
|
||||
{
|
||||
return GetSqlPart(list.First(), pars).ObjToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
Check.Exception(value == null, $" {value} is error ");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private string GetSqlPartByString(object value, List<SugarParameter> pars)
|
||||
{
|
||||
var valueString = value.ObjToString().Trim();
|
||||
if (Json2SqlHelper.IsSqlValue(valueString))
|
||||
{
|
||||
return GetParameterName(pars, valueString);
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.GetTranslationColumnName(value.ObjToString());
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Level3
|
||||
private string GetSplicingOperator(string valueString)
|
||||
{
|
||||
var parvalue = Regex.Match(valueString, @"\@s\:(.+)").Groups[1].Value;
|
||||
if (parvalue == null) parvalue = "";
|
||||
parvalue = parvalue.Trim();
|
||||
if (parvalue.ToLower().IsIn(SqlSplicingOperator))
|
||||
{
|
||||
return parvalue;
|
||||
}
|
||||
else
|
||||
{
|
||||
Check.ExceptionEasy($"{valueString} is error ", $"{valueString} 不是有效的拼接符号,拼接符号有:and、or、>=、<=、>、<、=、(、)");
|
||||
}
|
||||
return parvalue;
|
||||
}
|
||||
private string GetParameterName(List<SugarParameter> pars, string valueString)
|
||||
{
|
||||
object parvalue = Json2SqlHelper.GetValue(valueString);
|
||||
SugarParameter parameter = new SugarParameter("@p" + pars.Count(), parvalue);
|
||||
var type = Json2SqlHelper.GetType(valueString);
|
||||
parvalue = UtilMethods.ConvertDataByTypeName(type, parvalue.ObjToString());
|
||||
var parname = GetParameterName(pars, parvalue);
|
||||
return parname;
|
||||
}
|
||||
|
||||
private static string GetParameterName(List<SugarParameter> pars, object parvalue)
|
||||
{
|
||||
var parname = "@p" + pars.Count();
|
||||
SugarParameter parameter = new SugarParameter(parname, parvalue);
|
||||
pars.Add(parameter);
|
||||
return parname;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Helper
|
||||
|
||||
private static bool IsListObject(object value)
|
||||
{
|
||||
return value.GetType() == typeof(List<object>);
|
||||
}
|
||||
private static bool IsString(object value)
|
||||
{
|
||||
return value.GetType() == typeof(string);
|
||||
}
|
||||
private static bool IsObjectFunc(object value)
|
||||
{
|
||||
return value is ObjectFuncModel;
|
||||
}
|
||||
private bool IsSqlSplicingOperator(object value)
|
||||
{
|
||||
return SqlSplicingOperator.Contains(value.ObjToString());
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class JsonCommonProvider
|
||||
{
|
||||
public JsonCommonProvider(ISqlSugarClient context)
|
||||
{
|
||||
//this.context = context;
|
||||
this.sqlBuilder = InstanceFactory.GetSqlbuilder(context.CurrentConnectionConfig);
|
||||
this.sqlBuilder.Context = (context as SqlSugarClient).Context;
|
||||
}
|
||||
//public ISqlSugarClient context { get; set; }
|
||||
public ISqlBuilder sqlBuilder { get; set; }
|
||||
public JsonTableNameInfo GetTableName(JToken item)
|
||||
{
|
||||
JsonTableNameInfo jsonTableNameInfo = new JsonTableNameInfo();
|
||||
if (item.First().Type == JTokenType.Array && item.First.Count() == 2)
|
||||
{
|
||||
var tableName = item.First()[0].ObjToString();
|
||||
var shortName = item.First()[1].ObjToString();
|
||||
jsonTableNameInfo.ShortName = shortName;
|
||||
jsonTableNameInfo.TableName = tableName;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
var value = item.First().ToString();
|
||||
jsonTableNameInfo.TableName = value;
|
||||
}
|
||||
return jsonTableNameInfo;
|
||||
}
|
||||
public KeyValuePair<string, SugarParameter[]> GetWhere(string item, SqlSugarProvider context)
|
||||
{
|
||||
|
||||
if (!IsConditionalModel(item))
|
||||
{
|
||||
var obj = context.Utilities.JsonToSqlFuncModels(item);
|
||||
var sqlobj = sqlBuilder.FuncModelToSql(obj);
|
||||
return sqlobj;
|
||||
}
|
||||
else
|
||||
{
|
||||
var obj = context.Utilities.JsonToConditionalModels(item);
|
||||
var sqlObj = sqlBuilder.ConditionalModelToSql(obj, 0);
|
||||
return sqlObj;
|
||||
}
|
||||
}
|
||||
public KeyValuePair<string,SugarParameter[]> GetWhere(JToken item,SqlSugarProvider context)
|
||||
{
|
||||
var value = item.First().ToString();
|
||||
Check.ExceptionEasy(item.First().Type != JTokenType.Array, "Where format error " + item, "Where格式错误" + item);
|
||||
if (!IsConditionalModel(value))
|
||||
{
|
||||
var obj = context.Utilities.JsonToSqlFuncModels(value);
|
||||
var sqlobj = sqlBuilder.FuncModelToSql(obj);
|
||||
return sqlobj;
|
||||
}
|
||||
else
|
||||
{
|
||||
var obj = context.Utilities.JsonToConditionalModels(value);
|
||||
var sqlObj = sqlBuilder.ConditionalModelToSql(obj, 0);
|
||||
return sqlObj;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsConditionalModel(string value)
|
||||
{
|
||||
return value.ToLower().Contains("fieldname");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class JsonDeleteableProvider : IJsonDeleteableProvider<JsonDeleteResult>
|
||||
{
|
||||
private ISqlSugarClient context;
|
||||
private JObject jObject;
|
||||
private JsonCommonProvider jsonCommonProvider;
|
||||
private IDeleteable<object> sugarDeleteable=null;
|
||||
public JsonDeleteableProvider(ISqlSugarClient context, JObject jObject)
|
||||
{
|
||||
this.jObject = jObject;
|
||||
this.context = context;
|
||||
this.jsonCommonProvider = new JsonCommonProvider(context);
|
||||
}
|
||||
public SqlObjectResult ToSql()
|
||||
{
|
||||
return this.ToSqlList().First();
|
||||
}
|
||||
public List<SqlObjectResult> ToSqlList()
|
||||
{
|
||||
List<SqlObjectResult> result = new List<SqlObjectResult>();
|
||||
JsonQueryParameter jsonQueryParameter = new JsonQueryParameter();
|
||||
var appendTypeNames = this.jObject.AsJEnumerable().ToList();
|
||||
this.sugarDeleteable = this.context.Deleteable<object>();
|
||||
foreach (JToken item in appendTypeNames)
|
||||
{
|
||||
AppendAll(jsonQueryParameter, item);
|
||||
}
|
||||
result.Add(new SqlObjectResult(this.sugarDeleteable.ToSql(),JsonProviderType.Deleteable));
|
||||
return result;
|
||||
}
|
||||
private void AppendAll(JsonQueryParameter jsonQueryParameter, JToken item)
|
||||
{
|
||||
var name = item.Path.ToLower();
|
||||
if (IsWhere(name))
|
||||
{
|
||||
AppendWhere(item);
|
||||
}
|
||||
else if (IsTable(name))
|
||||
{
|
||||
AppendTable(item);
|
||||
}
|
||||
}
|
||||
private void AppendTable(JToken item)
|
||||
{
|
||||
var tableInfo = jsonCommonProvider.GetTableName(item);
|
||||
var tableName = tableInfo.TableName;
|
||||
if (tableInfo.ShortName.HasValue())
|
||||
{
|
||||
tableName = tableInfo.ShortName + "." + tableInfo.TableName;
|
||||
}
|
||||
this.sugarDeleteable.AS(tableName);
|
||||
}
|
||||
private void AppendWhere(JToken item)
|
||||
{
|
||||
var sqlObj = jsonCommonProvider.GetWhere(item, sugarDeleteable.DeleteBuilder.Context);
|
||||
sugarDeleteable.Where(sqlObj.Key, sqlObj.Value);
|
||||
}
|
||||
private static bool IsTable(string name)
|
||||
{
|
||||
return name == JsonProviderConfig.KeyDeleteable.Get().ToLower();
|
||||
}
|
||||
private static bool IsWhere(string name)
|
||||
{
|
||||
return name == "Where".ToLower();
|
||||
}
|
||||
public string ToSqlString()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public JsonDeleteResult ToResult()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
List<string> IJsonProvider<JsonDeleteResult>.ToSqlString()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonInsertableProvider : IJsonInsertableProvider<JsonInsertResult>
|
||||
{
|
||||
private void AppendIdentity(JToken item)
|
||||
{
|
||||
var tableInfo = jsonCommonProvider.GetTableName(item);
|
||||
this.IdentityId = tableInfo.TableName;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonInsertableProvider : IJsonInsertableProvider<JsonInsertResult>
|
||||
{
|
||||
private void AppendName(JToken item)
|
||||
{
|
||||
var tableInfo = jsonCommonProvider.GetTableName(item);
|
||||
this.TableName = tableInfo.TableName;
|
||||
if (tableInfo.ShortName.HasValue())
|
||||
{
|
||||
this.TableName = tableInfo.ShortName + "." + tableInfo.TableName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonInsertableProvider : IJsonInsertableProvider<JsonInsertResult>
|
||||
{
|
||||
private void AppendRow(JToken item)
|
||||
{
|
||||
var value = item.First().ToString();
|
||||
var dics = context.Utilities.JsonToColumnsModels(value);
|
||||
sugarInsertable =this.context.Insertable(dics).AS(this.TableName);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonInsertableProvider : IJsonInsertableProvider<JsonInsertResult>
|
||||
{
|
||||
private static bool IsColumns(string name)
|
||||
{
|
||||
return name == "columns".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsName(string name)
|
||||
{
|
||||
return name == JsonProviderConfig.KeyInsertable.Get().ToLower();
|
||||
}
|
||||
|
||||
private static bool IsIdentity(string name)
|
||||
{
|
||||
return name == "identity".ToLower();
|
||||
}
|
||||
private List<SqlObjectResult> ToSqlHelper()
|
||||
{
|
||||
List<SqlObjectResult> result = new List<SqlObjectResult>();
|
||||
JsonQueryParameter jsonQueryParameter = new JsonQueryParameter();
|
||||
var appendTypeNames = this.jObject.AsJEnumerable().ToList();
|
||||
foreach (JToken item in appendTypeNames.OrderBy(it => it.Path.EqualCase(JsonProviderConfig.KeyInsertable.Get()) ? 0 : 1))
|
||||
{
|
||||
AppendAll(jsonQueryParameter, item);
|
||||
}
|
||||
var addItem = this.sugarInsertable.ToSql();
|
||||
if (this.IdentityId.HasValue())
|
||||
{
|
||||
result.Add(new SqlObjectResult(addItem, JsonProviderType.InsertableIdentity));
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(new SqlObjectResult(addItem, JsonProviderType.Insertable));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonInsertableProvider : IJsonInsertableProvider<JsonInsertResult>
|
||||
{
|
||||
public SqlObjectResult ToSql()
|
||||
{
|
||||
return this.ToSqlList().First();
|
||||
}
|
||||
public JsonInsertableProvider(ISqlSugarClient context, JObject jObject)
|
||||
{
|
||||
this.jObject = jObject;
|
||||
this.context = context;
|
||||
this.jsonCommonProvider = new JsonCommonProvider(context);
|
||||
}
|
||||
public JsonInsertResult ToResult()
|
||||
{
|
||||
var result=new JsonInsertResult();
|
||||
var sqlInfo = this.ToSqlList();
|
||||
var sqlInfoResult = sqlInfo.First();
|
||||
if (sqlInfoResult.JsonSqlType == JsonProviderType.InsertableIdentity)
|
||||
{
|
||||
result.InsertCount = this.context.Ado.ExecuteCommand(sqlInfoResult.Sql,sqlInfoResult.Parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.InsertCount = this.Count;
|
||||
result.IdentityValue = this.context.Ado.GetInt(sqlInfoResult.Sql, sqlInfoResult.Parameters);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public List<SqlObjectResult> ToSqlList()
|
||||
{
|
||||
return ToSqlHelper();
|
||||
}
|
||||
|
||||
private void AppendAll(JsonQueryParameter jsonQueryParameter, JToken item)
|
||||
{
|
||||
var name = item.Path.ToLower();
|
||||
if (IsName(name))
|
||||
{
|
||||
AppendName(item);
|
||||
}
|
||||
else if (IsIdentity(name))
|
||||
{
|
||||
AppendIdentity(item);
|
||||
}
|
||||
else if (IsColumns(name))
|
||||
{
|
||||
AppendRow(item);
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> ToSqlString()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonInsertableProvider : IJsonInsertableProvider<JsonInsertResult>
|
||||
{
|
||||
private ISqlSugarClient context;
|
||||
private JObject jObject;
|
||||
private JsonCommonProvider jsonCommonProvider;
|
||||
private string TableName { get; set; }
|
||||
private string IdentityId { get; set; }
|
||||
private int Count { get; set; }
|
||||
private IInsertable<Dictionary<string, object>> sugarInsertable;
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// ApendJoinLastAfter
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
private void ApendJoinLastAfter(JToken item)
|
||||
{
|
||||
if (IsAppendSelect())
|
||||
{
|
||||
JArray jArray = new JArray();
|
||||
var tableConfigs = this.jsonTableConfigs.GroupBy(it => it.TableName).Select(it => it.First()).ToList();
|
||||
var isJoinTable = IsAnyJoin(appendTypeNames);
|
||||
foreach (var config in tableConfigs)
|
||||
{
|
||||
|
||||
if (isJoinTable)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (config.Columns.Any())
|
||||
{
|
||||
foreach (var column in config.Columns.Select(it => it.Name).Distinct())
|
||||
{
|
||||
jArray.Add(column);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
this.AppendSelect(jArray);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsAppendSelect()
|
||||
{
|
||||
return !IsAnySelect(appendTypeNames) && this.jsonTableConfigs.Any();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// AppendFrom
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
|
||||
private void AppendFrom(JToken item)
|
||||
{
|
||||
var tableNameInfo=jsonCommonProvider.GetTableName(item);
|
||||
AddMasterTableInfos(tableNameInfo);
|
||||
if (tableNameInfo.ShortName.HasValue())
|
||||
{
|
||||
this.sugarQueryable.AS(tableNameInfo.TableName, tableNameInfo.ShortName);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sugarQueryable.AS(tableNameInfo.TableName, tableNameInfo.ShortName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// AppendGroupBy
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
|
||||
private void AppendGroupBy(JToken item)
|
||||
{
|
||||
var value = item.First().ToString();
|
||||
var obj = context.Utilities.JsonToGroupByModels(value);
|
||||
sugarQueryable.GroupBy(obj);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// AppendHaving
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
private void AppendHaving(JToken item)
|
||||
{
|
||||
var value = item.First().ToString();
|
||||
var obj = context.Utilities.JsonToSqlFuncModels(value);
|
||||
sugarQueryable.Having(obj);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// AppendJoin
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
private bool AppendJoin(JToken item)
|
||||
{
|
||||
BeforeJoin();
|
||||
bool isJoin = true;
|
||||
var value = item.First().ToString();
|
||||
var obj = context.Utilities.JsonToJoinModels(value);
|
||||
sugarQueryable.AddJoinInfo(obj.TableName, obj.ShortName, obj.OnWhereList, obj.JoinType);
|
||||
AddTableInfos(obj.TableName,obj.ShortName);
|
||||
AfterJoin();
|
||||
return isJoin;
|
||||
}
|
||||
|
||||
private void AfterJoin()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void BeforeJoin()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// AppendOrderBy
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
private void AppendOrderBy(JToken item)
|
||||
{
|
||||
var value = item.First().ToString();
|
||||
var obj = context.Utilities.JsonToOrderByModels(value);
|
||||
sugarQueryable.OrderBy(obj);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// AppendPage
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
private int AppendPageSize(JToken item)
|
||||
{
|
||||
return Convert.ToInt32(item.First());
|
||||
}
|
||||
|
||||
private int AppendPageNumber(JToken item)
|
||||
{
|
||||
var result= Convert.ToInt32(item.First());
|
||||
if (result == 0)
|
||||
{
|
||||
result = 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// AppendSelect
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
private bool AppendSelect(JToken item)
|
||||
{
|
||||
bool isSelect = true;
|
||||
if (item.Type == JTokenType.Property)
|
||||
{
|
||||
var value = item.First().ToString();
|
||||
var obj = context.Utilities.JsonToSelectModels(value);
|
||||
obj =FilterSelect(obj);
|
||||
sugarQueryable.Select(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
var obj = context.Utilities.JsonToSelectModels(item.ToString());
|
||||
obj = FilterSelect(obj);
|
||||
sugarQueryable.Select(obj);
|
||||
}
|
||||
return isSelect;
|
||||
}
|
||||
|
||||
private List<SelectModel> FilterSelect(List<SelectModel> obj)
|
||||
{
|
||||
if (!this.jsonTableConfigs.Any())
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
List<SelectModel> result = new List<SelectModel>();
|
||||
foreach (var item in obj)
|
||||
{
|
||||
if (item.FiledName is string)
|
||||
{
|
||||
var tableName = GetTableName(item.FiledName + "");
|
||||
var columnName = GetColumnName(item.FiledName + "");
|
||||
if (IsMyColums(tableName, columnName))
|
||||
{
|
||||
result.Add(item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(item);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private bool IsMyColums(string tableName, string columnName)
|
||||
{
|
||||
return this.jsonTableConfigs.Any(it => it.TableName.EqualCase(tableName)
|
||||
&& it.Columns.Any(z => z.Name.EqualCase(columnName)));
|
||||
}
|
||||
|
||||
private string GetColumnName(string filedName)
|
||||
{
|
||||
return filedName.Split('.').Last();
|
||||
}
|
||||
|
||||
private string GetTableName(string filedName)
|
||||
{
|
||||
if (!filedName.Contains("."))
|
||||
{
|
||||
return TableInfos.First(it => it.IsMaster).Table;
|
||||
}
|
||||
else
|
||||
{
|
||||
var shortName=filedName.Split('.').First();
|
||||
return TableInfos.First(it => it.ShortName==shortName ).Table;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// AppendWhere
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
private void AppendWhere(JToken item)
|
||||
{
|
||||
BeforeWhere();
|
||||
var sqlObj = jsonCommonProvider.GetWhere(item, sugarQueryable.Context);
|
||||
sugarQueryable.Where(sqlObj.Key, sqlObj.Value);
|
||||
AfterWhere();
|
||||
}
|
||||
|
||||
private void AfterWhere()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void BeforeWhere()
|
||||
{
|
||||
if (!IsExecutedBeforeWhereFunc)
|
||||
{
|
||||
BeforeWhereFunc();
|
||||
IsExecutedBeforeWhereFunc = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class JsonQueryableProvider_TableInfo
|
||||
{
|
||||
public string Table { get; set; }
|
||||
public string ShortName { get; set; }
|
||||
public bool IsMaster { get; set; }
|
||||
public bool IsJoin { get; set; }
|
||||
public int Index { get; set; }
|
||||
}
|
||||
}
|
104
Src/Asp.NetCore2/SqlSugar/Json2Sql/Provider/Queryable/Helper.cs
Normal file
104
Src/Asp.NetCore2/SqlSugar/Json2Sql/Provider/Queryable/Helper.cs
Normal file
@ -0,0 +1,104 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
private static bool IsJoin(string name)
|
||||
{
|
||||
return name.StartsWith("LeftJoin".ToLower()) || name.StartsWith("RightJoin".ToLower()) || name.StartsWith("InnertJoin".ToLower());
|
||||
}
|
||||
private static bool IsJoinLastAfter(string name)
|
||||
{
|
||||
return name== "joinlastafter";
|
||||
}
|
||||
|
||||
private static bool IsPageSize(string name)
|
||||
{
|
||||
return name == "PageSize".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsPageNumber(string name)
|
||||
{
|
||||
return name == "PageNumber".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsSelect(string name)
|
||||
{
|
||||
return name == "Select".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsHaving(string name)
|
||||
{
|
||||
return name == "Having".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsGroupBy(string name)
|
||||
{
|
||||
return name == "GroupBy".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsOrderBy(string name)
|
||||
{
|
||||
return name == "OrderBy".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsWhere(string name)
|
||||
{
|
||||
return name == "Where".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsForm(string name)
|
||||
{
|
||||
return name == JsonProviderConfig.KeyQueryable.Get().ToLower();
|
||||
}
|
||||
|
||||
private static bool IsAnySelect(List<JToken> appendTypeNames)
|
||||
{
|
||||
return appendTypeNames.Any(it => IsSelect(it.Path.ToLower()));
|
||||
}
|
||||
private static bool IsAnyJoin(List<JToken> appendTypeNames)
|
||||
{
|
||||
return appendTypeNames.Any(it => IsJoin(it.Path.ToLower()));
|
||||
}
|
||||
private int GetSort(string name)
|
||||
{
|
||||
if (IsForm(name))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (IsJoin(name))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (IsJoinLastAfter(name))
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 100;
|
||||
}
|
||||
}
|
||||
private void AddMasterTableInfos(JsonTableNameInfo tableNameInfo)
|
||||
{
|
||||
AddTableInfos(tableNameInfo.TableName, tableNameInfo.ShortName,true);
|
||||
}
|
||||
private void AddTableInfos(string tableName,string shortName,bool isMaster=false)
|
||||
{
|
||||
UtilMethods.IsNullReturnNew(TableInfos);
|
||||
TableInfos.Add(new JsonQueryableProvider_TableInfo() { Table = tableName, ShortName = shortName, IsMaster = true });
|
||||
}
|
||||
private JsonQueryableProvider_TableInfo GetMasterTable()
|
||||
{
|
||||
return this.TableInfos.First(it => it.IsMaster);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// JsonQueryableProvider
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
|
||||
public JsonQueryableProvider(ISqlSugarClient context, JObject jobject)
|
||||
{
|
||||
this.jobject = jobject;
|
||||
this.context = context;
|
||||
this.jsonCommonProvider = new JsonCommonProvider(context);
|
||||
}
|
||||
public IJsonQueryableProvider<JsonQueryResult> ShowDesciption()
|
||||
{
|
||||
this.IsDescription = true;
|
||||
return this;
|
||||
}
|
||||
public IJsonQueryableProvider<JsonQueryResult> UseAuthentication(JsonTableConfig config)
|
||||
{
|
||||
if (config == null)
|
||||
{
|
||||
jsonTableConfigs =new List<JsonTableConfig>() { config };
|
||||
}
|
||||
else
|
||||
{
|
||||
jsonTableConfigs.Add(config);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public IJsonQueryableProvider<JsonQueryResult> UseAuthentication(List<JsonTableConfig> configs)
|
||||
{
|
||||
foreach (JsonTableConfig config in configs)
|
||||
{
|
||||
UseAuthentication(config);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public SqlObjectResult ToSql()
|
||||
{
|
||||
return this.ToSqlList().First();
|
||||
}
|
||||
public JsonQueryResult ToResult()
|
||||
{
|
||||
return ToResultDefault();
|
||||
}
|
||||
|
||||
public List<SqlObjectResult> ToSqlList()
|
||||
{
|
||||
var result = ToSqlDefault();
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<string> ToSqlString()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private void AppendQueryableAll(JsonQueryParameter jsonQueryParameter, JToken item)
|
||||
{
|
||||
var name = item.Path.ToLower();
|
||||
if (IsForm(name))
|
||||
{
|
||||
AppendFrom(item);
|
||||
}
|
||||
else if (IsWhere(name))
|
||||
{
|
||||
AppendWhere(item);
|
||||
}
|
||||
else if (IsOrderBy(name))
|
||||
{
|
||||
AppendOrderBy(item);
|
||||
}
|
||||
else if (IsJoinLastAfter(name))
|
||||
{
|
||||
ApendJoinLastAfter(item);
|
||||
}
|
||||
else if (IsGroupBy(name))
|
||||
{
|
||||
AppendGroupBy(item);
|
||||
}
|
||||
else if (IsHaving(name))
|
||||
{
|
||||
AppendHaving(item);
|
||||
}
|
||||
else if (IsSelect(name))
|
||||
{
|
||||
jsonQueryParameter.IsSelect = AppendSelect(item);
|
||||
}
|
||||
else if (IsPageNumber(name))
|
||||
{
|
||||
jsonQueryParameter.PageIndex = AppendPageNumber(item);
|
||||
}
|
||||
else if (IsPageSize(name))
|
||||
{
|
||||
jsonQueryParameter.PageSize = AppendPageSize(item);
|
||||
}
|
||||
else if (IsJoin(name))
|
||||
{
|
||||
jsonQueryParameter.IsSelect = AppendJoin(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// Property
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
int appendIndex = 1000;
|
||||
List<JToken> appendTypeNames;
|
||||
JObject jobject;
|
||||
ISqlSugarClient context;
|
||||
ISugarQueryable<object> sugarQueryable;
|
||||
JsonCommonProvider jsonCommonProvider;
|
||||
List<JsonTableConfig> jsonTableConfigs = new List<JsonTableConfig>();
|
||||
bool IsDescription = false;
|
||||
List<JsonQueryableProvider_TableInfo> TableInfos =new List<JsonQueryableProvider_TableInfo> ();
|
||||
bool IsExecutedBeforeWhereFunc = false;
|
||||
Action BeforeWhereFunc { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// RegisterAop
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
private void RegisterAop()
|
||||
{
|
||||
this.BeforeWhereFunc = () =>
|
||||
{
|
||||
var masterTable = GetMasterTable();
|
||||
var masterFilters = this.jsonTableConfigs.Where(it => it.TableName.EqualCase(masterTable.Table)).ToList();
|
||||
if (masterFilters.Any())
|
||||
{
|
||||
foreach (var filter in masterFilters)
|
||||
{
|
||||
var conditions = filter.Conditionals;
|
||||
conditions=GetConvertConditions(conditions);
|
||||
var p = this.sugarQueryable.SqlBuilder.ConditionalModelToSql(conditions);
|
||||
var sql = p.Key;
|
||||
sugarQueryable.SqlBuilder.RepairReplicationParameters(ref sql, p.Value, appendIndex);
|
||||
appendIndex++;
|
||||
sugarQueryable.Where(sql, p.Value);
|
||||
};
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private List<IConditionalModel> GetConvertConditions(List<IConditionalModel> conditions)
|
||||
{
|
||||
return conditions;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// ResultDefault
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
private List<SqlObjectResult> ToSqlDefault()
|
||||
{
|
||||
List<SqlObjectResult> result = new List<SqlObjectResult>();
|
||||
sugarQueryable = context.Queryable<object>();
|
||||
appendTypeNames = GetTypeNames();
|
||||
JsonQueryParameter jsonQueryParameter = new JsonQueryParameter();
|
||||
RegisterAop();
|
||||
foreach (JToken item in appendTypeNames)
|
||||
{
|
||||
AppendQueryableAll(jsonQueryParameter, item);
|
||||
}
|
||||
return ToPageDefault(result, jsonQueryParameter);
|
||||
}
|
||||
|
||||
private List<JToken> GetTypeNames()
|
||||
{
|
||||
var result= this.jobject.AsJEnumerable().ToList();
|
||||
result.Add(JToken.Parse("{JoinLastAfter:null}").First());
|
||||
result = result.OrderBy(it => GetSort(it.Path.ToLower())).ToList();
|
||||
return result;
|
||||
}
|
||||
|
||||
private JsonQueryResult ToResultDefault()
|
||||
{
|
||||
JsonQueryResult result = new JsonQueryResult();
|
||||
var toSqls = this.ToSqlList();
|
||||
var SqlCount = toSqls.FirstOrDefault(it => it.JsonSqlType == JsonProviderType.QueryableCount);
|
||||
var SqlList = toSqls.FirstOrDefault(it => it.JsonSqlType == JsonProviderType.Queryable);
|
||||
AddCount(result, SqlCount);
|
||||
AddList(result, SqlList);
|
||||
AddDescription();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// ResultHelper
|
||||
/// </summary>
|
||||
public partial class JsonQueryableProvider : IJsonQueryableProvider<JsonQueryResult>
|
||||
{
|
||||
#region SqlHelper
|
||||
private List<SqlObjectResult> ToPageDefault(List<SqlObjectResult> result, JsonQueryParameter jsonQueryParameter)
|
||||
{
|
||||
if (jsonQueryParameter.IsPage)
|
||||
{
|
||||
AddPageSql(result, jsonQueryParameter);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddDefaultSql(result);
|
||||
}
|
||||
Check.ExceptionEasy(jsonQueryParameter.JoinNoSelect, "join query need Select", "联表查询需要设置Select");
|
||||
return result;
|
||||
}
|
||||
|
||||
private void AddDefaultSql(List<SqlObjectResult> result)
|
||||
{
|
||||
result.Add(new SqlObjectResult(sugarQueryable.Clone().ToSql(), JsonProviderType.Queryable));
|
||||
}
|
||||
|
||||
private void AddPageSql(List<SqlObjectResult> result, JsonQueryParameter jsonQueryParameter)
|
||||
{
|
||||
var skipValue = (jsonQueryParameter.PageIndex.Value - 1) * jsonQueryParameter.PageSize.Value;
|
||||
var takeValue = jsonQueryParameter.PageSize.Value;
|
||||
result.Add(new SqlObjectResult(sugarQueryable.Clone().Skip(skipValue).Take(takeValue).ToSql(), JsonProviderType.Queryable));
|
||||
result.Add(new SqlObjectResult(sugarQueryable.Select("COUNT(1)").ToSql(), JsonProviderType.QueryableCount));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ObjectHeper
|
||||
private void AddDescription()
|
||||
{
|
||||
if (this.IsDescription)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private void AddList(JsonQueryResult result, SqlObjectResult SqlList)
|
||||
{
|
||||
if (SqlList != null)
|
||||
{
|
||||
result.Data = this.context.Ado.SqlQuery<dynamic>(SqlList.Sql, SqlList.Parameters);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddCount(JsonQueryResult result, SqlObjectResult SqlCount)
|
||||
{
|
||||
if (SqlCount != null)
|
||||
{
|
||||
result.ToTalRows = this.context.Ado.GetInt(SqlCount.Sql, SqlCount.Parameters);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonUpdateableProvider : IJsonUpdateableProvider<JsonUpdateResult>
|
||||
{
|
||||
private void AppendRow(JToken item)
|
||||
{
|
||||
var itemFirst = item.First();
|
||||
var isObject = itemFirst.Type == JTokenType.Object;
|
||||
var value = itemFirst.ToString();
|
||||
var dics = context.Utilities.JsonToColumnsModels(value);
|
||||
if (isObject)
|
||||
sugarUpdateable = this.context.Updateable(dics.First()).AS(this.TableName);
|
||||
else
|
||||
{
|
||||
sugarUpdateable = this.context.Updateable(dics).AS(this.TableName);
|
||||
isList = dics.Take(2).Count() > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonUpdateableProvider : IJsonUpdateableProvider<JsonUpdateResult>
|
||||
{
|
||||
private void AppendTable(JToken item)
|
||||
{
|
||||
var tableInfo = jsonCommonProvider.GetTableName(item);
|
||||
this.TableName = tableInfo.TableName;
|
||||
if (tableInfo.ShortName.HasValue())
|
||||
{
|
||||
this.TableName = tableInfo.ShortName + "." + tableInfo.TableName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonUpdateableProvider : IJsonUpdateableProvider<JsonUpdateResult>
|
||||
{
|
||||
private void AppendWhere(JToken item)
|
||||
{
|
||||
Check.Exception(isList, "Batch updates cannot use Where, only WhereColumns can set columns", "批量更新不能使用Where,只能通过WhereColumns设置列");
|
||||
var sqlObj = jsonCommonProvider.GetWhere(item, sugarUpdateable.UpdateBuilder.Context);
|
||||
sugarUpdateable.Where(sqlObj.Key, sqlObj.Value);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonUpdateableProvider : IJsonUpdateableProvider<JsonUpdateResult>
|
||||
{
|
||||
private void AppendWhereColumns(JToken item)
|
||||
{
|
||||
var columns = item.First().ToObject<string[]>();
|
||||
Check.ExceptionEasy(columns.IsNullOrEmpty(), "need WhereColumns", "WhereColumns 需要设置列名");
|
||||
this.sugarUpdateable.WhereColumns(columns);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonUpdateableProvider : IJsonUpdateableProvider<JsonUpdateResult>
|
||||
{
|
||||
|
||||
private static bool IsColumns(string name)
|
||||
{
|
||||
return name == "Columns".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsWhere(string name)
|
||||
{
|
||||
return name == "Where".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsWhereColumns(string name)
|
||||
{
|
||||
return name == "WhereColumns".ToLower();
|
||||
}
|
||||
|
||||
private static bool IsTable(string name)
|
||||
{
|
||||
return name == JsonProviderConfig.KeyUpdateable.Get().ToLower();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class JsonUpdateableProvider : IJsonUpdateableProvider<JsonUpdateResult>
|
||||
{
|
||||
private ISqlSugarClient context;
|
||||
private JObject jObject;
|
||||
private JsonCommonProvider jsonCommonProvider;
|
||||
private string TableName { get; set; }
|
||||
private bool isList { get; set; }
|
||||
private IUpdateable<Dictionary<string, object>> sugarUpdateable;
|
||||
public JsonUpdateableProvider(ISqlSugarClient context, JObject jObject)
|
||||
{
|
||||
this.jObject = jObject;
|
||||
this.context = context;
|
||||
this.jsonCommonProvider = new JsonCommonProvider(context);
|
||||
}
|
||||
public JsonUpdateResult ToResult()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
public SqlObjectResult ToSql()
|
||||
{
|
||||
return this.ToSqlList().First();
|
||||
}
|
||||
public List<SqlObjectResult> ToSqlList()
|
||||
{
|
||||
List<SqlObjectResult> result = new List<SqlObjectResult>();
|
||||
JsonQueryParameter jsonQueryParameter = new JsonQueryParameter();
|
||||
List<JToken> appendTypeNames = GetAppendTypes();
|
||||
foreach (JToken item in appendTypeNames)
|
||||
{
|
||||
AppendAll(jsonQueryParameter, item);
|
||||
}
|
||||
var addItem = this.sugarUpdateable.ToSql();
|
||||
result.Add(new SqlObjectResult(addItem,JsonProviderType.Updateable));
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<JToken> GetAppendTypes()
|
||||
{
|
||||
var appendTypeNames = this.jObject.AsJEnumerable().ToList();
|
||||
appendTypeNames = appendTypeNames.OrderBy(it =>
|
||||
{
|
||||
if (it.Path.EqualCase(JsonProviderConfig.KeyUpdateable.Get())) return 0;
|
||||
if (it.Path.EqualCase("Columns")) return 1;
|
||||
else return 3;
|
||||
|
||||
} ).ToList();
|
||||
return appendTypeNames;
|
||||
}
|
||||
|
||||
private void AppendAll(JsonQueryParameter jsonQueryParameter, JToken item)
|
||||
{
|
||||
var name = item.Path.ToLower();
|
||||
if (IsTable(name))
|
||||
{
|
||||
AppendTable(item);
|
||||
}
|
||||
else if (IsWhereColumns(name))
|
||||
{
|
||||
AppendWhereColumns(item);
|
||||
}
|
||||
else if (IsWhere(name))
|
||||
{
|
||||
AppendWhere(item);
|
||||
}
|
||||
else if (IsColumns(name))
|
||||
{
|
||||
AppendRow(item);
|
||||
}
|
||||
}
|
||||
public List<string> ToSqlString()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
|
||||
public partial class QueryableProvider<T> : QueryableAccessory, ISugarQueryable<T>
|
||||
{
|
||||
public ISugarQueryable<T> AddJoinInfo(string tableName, string shortName, IFuncModel models, JoinType type = JoinType.Left)
|
||||
{
|
||||
var sqlobj = this.SqlBuilder.FuncModelToSql(models);
|
||||
this.QueryBuilder.Parameters.AddRange(sqlobj.Value);
|
||||
return this.AddJoinInfo(tableName, shortName, sqlobj.Key, type);
|
||||
}
|
||||
public ISugarQueryable<T> AS(string tableName, string shortName)
|
||||
{
|
||||
return this.AS($"{this.SqlBuilder.GetTranslationTableName(tableName)} {shortName}");
|
||||
}
|
||||
public ISugarQueryable<T> OrderBy(List<OrderByModel> models)
|
||||
{
|
||||
var orderObj = this.SqlBuilder.OrderByModelToSql(models);
|
||||
this.OrderBy(orderObj.Key);
|
||||
this.QueryBuilder.Parameters.AddRange(orderObj.Value);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> GroupBy(List<GroupByModel> models)
|
||||
{
|
||||
var orderObj = this.SqlBuilder.GroupByModelToSql(models);
|
||||
this.GroupBy(orderObj.Key);
|
||||
this.QueryBuilder.Parameters.AddRange(orderObj.Value);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Select(List<SelectModel> models)
|
||||
{
|
||||
var orderObj = this.SqlBuilder.SelectModelToSql(models);
|
||||
this.Select(orderObj.Key);
|
||||
this.QueryBuilder.Parameters.AddRange(orderObj.Value);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Having(IFuncModel model)
|
||||
{
|
||||
var orderObj = this.SqlBuilder.FuncModelToSql(model);
|
||||
this.Having(orderObj.Key);
|
||||
this.QueryBuilder.Parameters.AddRange(orderObj.Value);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
35
Src/Asp.NetCore2/SqlSugar/Json2Sql/Utils/Json2SqlConfig.cs
Normal file
35
Src/Asp.NetCore2/SqlSugar/Json2Sql/Utils/Json2SqlConfig.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public static class JsonProviderConfig
|
||||
{
|
||||
public const string KeyInsertable = "Insertable";
|
||||
public const string KeyUpdateable = "Updateable";
|
||||
public const string KeyQueryable = "Queryable";
|
||||
public const string KeyDeleteable = "Deleteable";
|
||||
|
||||
private static Dictionary<string, string> words = new Dictionary<string, string>()
|
||||
{
|
||||
{ KeyInsertable,"Table"},
|
||||
{ KeyUpdateable,"Table"},
|
||||
{ KeyQueryable,"Table"},
|
||||
{ KeyDeleteable,"Table"}
|
||||
};
|
||||
public static string Rename(string key,string name)
|
||||
{
|
||||
return words[key]=name;
|
||||
}
|
||||
internal static string Get(this string value)
|
||||
{
|
||||
return words[value];
|
||||
}
|
||||
internal static string GetWord(string key)
|
||||
{
|
||||
Check.ExceptionEasy(words.ContainsKey(key) == false, $"{key} is error", $"{key} 不存在 ");
|
||||
return words[key];
|
||||
}
|
||||
}
|
||||
}
|
39
Src/Asp.NetCore2/SqlSugar/Json2Sql/Utils/Json2SqlHelper.cs
Normal file
39
Src/Asp.NetCore2/SqlSugar/Json2Sql/Utils/Json2SqlHelper.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class Json2SqlHelper
|
||||
{
|
||||
public static bool IsSqlValue(string valueString)
|
||||
{
|
||||
return Regex.IsMatch(valueString, @"^\{\w{1,10}\}\:");
|
||||
}
|
||||
public static string GetType(string valueString)
|
||||
{
|
||||
return Regex.Match(valueString, @"^\{(\w+)\}\:").Groups[1].Value;
|
||||
}
|
||||
public static string GetValue(string valueString)
|
||||
{
|
||||
return Regex.Replace(valueString, @"^\{\w{1,10}\}\:", "");
|
||||
}
|
||||
|
||||
public static List<string> GetTableNames(string json)
|
||||
{
|
||||
List<string> result = new List<string>();
|
||||
var mainTable = JObject.Parse(json).AsJEnumerable().Where(it =>
|
||||
it.Path.ToLower().IsIn(
|
||||
JsonProviderConfig.KeyInsertable.Get().ToLower(),
|
||||
JsonProviderConfig.KeyUpdateable.Get().ToLower(),
|
||||
JsonProviderConfig.KeyDeleteable.Get().ToLower(),
|
||||
JsonProviderConfig.KeyQueryable.Get().ToLower()
|
||||
)).FirstOrDefault();
|
||||
if (mainTable != null)
|
||||
result.Add(mainTable.First().ToString());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
38
Src/Asp.NetCore2/SqlSugar/JsonClient.cs
Normal file
38
Src/Asp.NetCore2/SqlSugar/JsonClient.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using SqlSugar;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class JsonClient : IJsonClient
|
||||
{
|
||||
public ISqlSugarClient Context { get; set; }
|
||||
|
||||
public IJsonQueryableProvider<JsonQueryResult> Queryable(string json)
|
||||
{
|
||||
var iJsonToSql = new JsonQueryableProvider(Context, JObject.Parse(json));
|
||||
return iJsonToSql;
|
||||
}
|
||||
public IJsonProvider<JsonInsertResult> Insertable(string json)
|
||||
{
|
||||
var iJsonToSql = new JsonInsertableProvider(Context, JObject.Parse(json));
|
||||
return iJsonToSql;
|
||||
}
|
||||
public IJsonProvider<JsonUpdateResult> Updateable(string json)
|
||||
{
|
||||
var iJsonToSql = new JsonUpdateableProvider(Context, JObject.Parse(json));
|
||||
return iJsonToSql;
|
||||
}
|
||||
public IJsonProvider<JsonDeleteResult> Deleteable(string json)
|
||||
{
|
||||
var iJsonToSql = new JsonDeleteableProvider(Context, JObject.Parse(json));
|
||||
return iJsonToSql;
|
||||
}
|
||||
public List<string> GetTableNameList(string json)
|
||||
{
|
||||
List<string> result = Json2SqlHelper.GetTableNames(json);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user