Update Core

This commit is contained in:
sunkaixuna 2021-08-22 08:58:49 +08:00
parent bf511b53a9
commit c276ee5cd5
17 changed files with 164 additions and 22 deletions

View File

@ -125,6 +125,7 @@ namespace SqlSugar
if (isIdEntity)
{
id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity();
this.Entity.Columns.First(it => it.IsIdentity || it.OracleSequenceName.HasValue()).PropertyInfo.SetValue(InsertObject, id);
}
var pk = GetPrimaryKey(this.Entity, InsertObject, id);
AddChildList(this.SubList, InsertObject, pk);

View File

@ -26,6 +26,7 @@ namespace SqlSugar
public List<Action<List<T>>> Mappers { get; set; }
public bool IsCache { get; set; }
public int CacheTime { get; set; }
public string CacheKey { get; set; }
public bool IsAs { get; set; }
public QueryBuilder QueryBuilder
{
@ -434,7 +435,7 @@ namespace SqlSugar
public virtual ISugarQueryable<T> Where(List<IConditionalModel> conditionalModels)
{
if (conditionalModels.IsNullOrEmpty()) return this;
var sqlObj = this.SqlBuilder.ConditionalModelToSql(conditionalModels);
var sqlObj = this.SqlBuilder.ConditionalModelToSql(conditionalModels,0);
return this.Where(sqlObj.Key, sqlObj.Value);
}
@ -817,7 +818,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<int>(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<int>(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context,CacheKey);
}
else
{
@ -904,7 +905,7 @@ namespace SqlSugar
var result = CacheSchemeMain.GetOrCreate<string>(cacheService, this.QueryBuilder, () =>
{
return this.Context.Utilities.SerializeObject(this.ToList(), typeof(T));
}, CacheTime, this.Context);
}, CacheTime, this.Context,CacheKey);
return result;
}
else
@ -1027,7 +1028,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<DataTable>(cacheService, this.QueryBuilder, () => { return this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray()); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<DataTable>(cacheService, this.QueryBuilder, () => { return this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray()); }, CacheTime, this.Context,CacheKey);
}
else
{
@ -1187,6 +1188,15 @@ namespace SqlSugar
return _ToSql();
}
}
public ISugarQueryable<T> WithCache(string cacheKey, int cacheDurationInSeconds = int.MaxValue)
{
cacheDurationInSeconds = SetCacheTime(cacheDurationInSeconds);
Check.ArgumentNullException(this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService, "Use Cache ConnectionConfig.ConfigureExternalServices.DataInfoCacheService is required ");
this.IsCache = true;
this.CacheTime = cacheDurationInSeconds;
this.CacheKey = cacheKey;
return this;
}
public ISugarQueryable<T> WithCache(int cacheDurationInSeconds = int.MaxValue)
{
cacheDurationInSeconds = SetCacheTime(cacheDurationInSeconds);
@ -1322,7 +1332,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<int>(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<int>(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context,CacheKey);
}
else
{
@ -1415,7 +1425,7 @@ namespace SqlSugar
var result = CacheSchemeMain.GetOrCreate<string>(cacheService, this.QueryBuilder, () =>
{
return this.Context.Utilities.SerializeObject(this.ToList(), typeof(T));
}, CacheTime, this.Context);
}, CacheTime, this.Context,CacheKey);
return result;
}
else
@ -1444,7 +1454,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<DataTable>(cacheService, this.QueryBuilder, () => { return this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray()); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<DataTable>(cacheService, this.QueryBuilder, () => { return this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray()); }, CacheTime, this.Context,CacheKey);
}
else
{
@ -1798,7 +1808,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<List<TResult>>(cacheService, this.QueryBuilder, () => { return GetData<TResult>(sqlObj); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<List<TResult>>(cacheService, this.QueryBuilder, () => { return GetData<TResult>(sqlObj); }, CacheTime, this.Context,CacheKey);
}
else
{
@ -1815,7 +1825,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<List<TResult>>(cacheService, this.QueryBuilder, () => { return GetData<TResult>(sqlObj); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<List<TResult>>(cacheService, this.QueryBuilder, () => { return GetData<TResult>(sqlObj); }, CacheTime, this.Context,CacheKey);
}
else
{

View File

@ -203,6 +203,10 @@ namespace SqlSugar
foreach (var item in whereColumns)
{
var value = item.PropertyInfo.GetValue(dataItem.Item, null);
if (value != null&&value.GetType().IsEnum())
{
value = Convert.ToInt64(value);
}
condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(i==0?WhereType.Or :WhereType.And, new ConditionalModel()
{
FieldName = item.DbColumnName,

View File

@ -206,6 +206,10 @@ namespace SqlSugar
{
return "N'" + value.ToString().ToSqlFilter() + "'";
}
else if (type == UtilConstants.FloatType)
{
return "N'" +Convert.ToDouble(value).ToString() + "'";
}
else
{
return "N'" + value.ToString() + "'";

View File

@ -122,19 +122,20 @@ namespace SqlSugar
StringBuilder builder = new StringBuilder();
List<SugarParameter> parameters = new List<SugarParameter>();
var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
var mainIndex = 0;
foreach (var model in models)
{
if (model is ConditionalModel)
{
var item = model as ConditionalModel;
var index = models.IndexOf(item) + beginIndex;
var index = mainIndex + beginIndex;
var type = index == 0 ? "" : "AND";
if (beginIndex > 0)
{
type = null;
}
string temp = " {0} {1} {2} {3} ";
string parameterName = string.Format("{0}Conditional{1}{2}", sqlBuilder.SqlParameterKeyWord, item.FieldName, index);
string parameterName = string.Format("{0}Condit{1}{2}", sqlBuilder.SqlParameterKeyWord, item.FieldName, index);
if (parameterName.Contains("."))
{
parameterName = parameterName.Replace(".", "_");
@ -231,6 +232,19 @@ namespace SqlSugar
parameters.Add(new SugarParameter(parameterName, GetFieldValue(item)));
}
break;
case ConditionalType.InLike:
var array =(item.FieldValue+"").Split(',').ToList();
List<string> sqls = new List<string>();
int i = 0;
foreach (var val in array)
{
var itemParameterName = $"{ parameterName}{index}{i}";
sqls.Add(item.FieldName.ToSqlFilter()+ " LIKE " + itemParameterName);
parameters.Add(new SugarParameter(itemParameterName, "%" + val + "%"));
i++;
}
builder.Append($" ({string.Join(" OR ", sqls)}) ");
break;
default:
break;
}
@ -275,6 +289,7 @@ namespace SqlSugar
}
}
}
mainIndex++;
}
return new KeyValuePair<string, SugarParameter[]>(builder.ToString(), parameters.ToArray());
}

View File

@ -66,7 +66,7 @@ namespace SqlSugar
{
return this.ExecuteCommand() > 0;
}
public async Task<int> ExecuteCommandAsync()
public virtual async Task<int> ExecuteCommandAsync()
{
string sql = _ExecuteCommand();
if (string.IsNullOrEmpty(sql))

View File

@ -7,9 +7,10 @@ namespace SqlSugar
{
internal class CacheSchemeMain
{
public static T GetOrCreate<T>(ICacheService cacheService, QueryBuilder queryBuilder, Func<T> getData, int cacheDurationInSeconds, SqlSugarProvider context)
public static T GetOrCreate<T>(ICacheService cacheService, QueryBuilder queryBuilder, Func<T> getData, int cacheDurationInSeconds, SqlSugarProvider context,string cacheKey)
{
CacheKey key = CacheKeyBuider.GetKey(context, queryBuilder);
key.AppendKey = cacheKey;
string keyString = key.ToString();
var result = cacheService.GetOrCreate(keyString, getData, cacheDurationInSeconds);
return result;

View File

@ -7,12 +7,15 @@ namespace SqlSugar
{
public class CacheKey
{
public string AppendKey { get; set; }
public string Database { get; set; }
public List<string> Tables { get; set; }
public List<string> IdentificationList { get; set; }
public new string ToString()
{
return "SqlSugarDataCache" + UtilConstants.Dot + string.Join(UtilConstants.Dot, this.Tables) +UtilConstants.Dot+ string.Join(UtilConstants.Dot, this.IdentificationList.Where(it=>it.HasValue()));
var result= "SqlSugarDataCache" + UtilConstants.Dot + string.Join(UtilConstants.Dot, this.Tables) +UtilConstants.Dot+ string.Join(UtilConstants.Dot, this.IdentificationList.Where(it=>it.HasValue()));
result = result + AppendKey;
return result;
}
}
}

View File

@ -22,5 +22,6 @@ namespace SqlSugar
IsNot=12,
NoLike = 13,
EqualNull = 14,
InLike=15
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SubAs : ISubOperation
{
public bool HasWhere
{
get; set;
}
public string Name
{
get
{
return "AS";
}
}
public Expression Expression
{
get; set;
}
public int Sort
{
get
{
return 200;
}
}
public ExpressionContext Context
{
get; set;
}
public string GetValue(Expression expression = null)
{
var exp = expression as MethodCallExpression;
var expString= SubTools.GetMethodValue(this.Context, exp.Arguments[0], ResolveExpressType.WhereSingle)?.Trim();
var result = this.Context.Parameters.First(it => it.ParameterName == expString).Value+"";
return "$SubAs:"+result;
}
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
namespace SqlSugar
{
@ -16,6 +17,7 @@ namespace SqlSugar
{
List<MethodCallExpression> allMethods = new List<MethodCallExpression>();
private ExpressionContext context = null;
private string subKey = "$SubAs:";
private bool hasWhere;
public SubResolve(MethodCallExpression expression, ExpressionContext context, Expression oppsiteExpression)
{
@ -85,14 +87,32 @@ namespace SqlSugar
currentExpression = addItem;
}
}
public string GetSql()
{
List<string> subItems = GetSubItems();
var sql = string.Join(UtilConstants.Space, subItems);
var sqlItems = subItems.Where(it => !it.StartsWith(subKey)).ToList();
var asItems = subItems.Where(it => it.StartsWith(subKey)).ToList();
if (asItems.Any())
{
GetSubAs(sqlItems, asItems);
}
var sql = string.Join(UtilConstants.Space, sqlItems);
return this.context.DbMehtods.Pack(sql);
}
private void GetSubAs(List<string> sqlItems, List<string> asItems)
{
for (int i = 0; i < sqlItems.Count; i++)
{
if (sqlItems[i].StartsWith("FROM " + this.context.SqlTranslationLeft))
{
var asName = this.context.GetTranslationTableName(asItems.First().Replace(subKey, ""), false);
var repKey = $"\\{this.context.SqlTranslationLeft}.+\\{this.context.SqlTranslationRight}";
sqlItems[i] = Regex.Replace(sqlItems[i], repKey, asName);
}
}
}
private List<string> GetSubItems()
{
var isubList = this.allMethods.Select(exp =>

View File

@ -31,7 +31,8 @@ namespace SqlSugar
new SubAvg(){ Context=Context },
new SubOrderBy(){ Context=Context },
new SubOrderByDesc(){ Context=Context },
new SubGroupBy(){ Context=Context}
new SubGroupBy(){ Context=Context},
new SubAs(){Context=Context}
};
}

View File

@ -10,6 +10,10 @@ namespace SqlSugar
public class Subqueryable<T> where T : class, new()
{
public Subqueryable<T> AS(string tableName)
{
return this;
}
public Subqueryable<T> InnerJoin<JoinType>(Func<T, JoinType, bool> expression)
{
return this;

View File

@ -165,6 +165,7 @@ namespace SqlSugar
List<T> ToPageList(int pageIndex, int pageSize, ref int totalNumber);
List<T> ToPageList(int pageIndex, int pageSize, ref int totalNumber,ref int totalPage);
Task<List<T>> ToPageListAsync(int pageIndex, int pageSize, RefAsync<int> totalNumber);
ISugarQueryable<T> WithCache(string cacheKey,int cacheDurationInSeconds = int.MaxValue);
ISugarQueryable<T> WithCache(int cacheDurationInSeconds = int.MaxValue);
ISugarQueryable<T> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue);
string ToClassString(string className);

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public interface ISugarRepository
{
ISqlSugarClient Context { get; set; }
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
@ -27,5 +28,21 @@ namespace SqlSugar
return base.UpdateObjs.Count();
}
}
public async override Task<int> ExecuteCommandAsync()
{
if (base.UpdateObjs.Count() == 1)
{
return await base.ExecuteCommandAsync();
}
else if (base.UpdateObjs.Count() == 0)
{
return 0;
}
else
{
await base.ExecuteCommandAsync();
return base.UpdateObjs.Count();
}
}
}
}

View File

@ -319,9 +319,4 @@ namespace SqlSugar
[Obsolete("Use AsSugarClient()")]
public ISqlSugarClient FullClient { get { return this.Context; } }
}
public interface ISugarRepository
{
ISqlSugarClient Context { get; set; }
}
}