mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-18 17:48:11 +08:00
Update core
This commit is contained in:
@@ -101,7 +101,7 @@ namespace SqlSugar
|
||||
{
|
||||
if (this.Transaction != null)
|
||||
{
|
||||
this.Transaction.Commit();
|
||||
this.Transaction.Rollback();
|
||||
this.Transaction = null;
|
||||
}
|
||||
if (this.Connection != null && this.Connection.State != ConnectionState.Open)
|
||||
@@ -214,9 +214,29 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<DbResult<bool>> UseTranAsync(Action action, Action<Exception> errorCallBack = null)
|
||||
public async Task<DbResult<bool>> UseTranAsync(Func<Task> action, Action<Exception> errorCallBack = null)
|
||||
{
|
||||
return Task.FromResult(UseTran(action, errorCallBack));
|
||||
var result = new DbResult<bool>();
|
||||
try
|
||||
{
|
||||
this.BeginTran();
|
||||
if (action != null)
|
||||
await action();
|
||||
this.CommitTran();
|
||||
result.Data = result.IsSuccess = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.ErrorException = ex;
|
||||
result.ErrorMessage = ex.Message;
|
||||
result.IsSuccess = false;
|
||||
this.RollbackTran();
|
||||
if (errorCallBack != null)
|
||||
{
|
||||
errorCallBack(ex);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public DbResult<T> UseTran<T>(Func<T> action, Action<Exception> errorCallBack = null)
|
||||
@@ -244,9 +264,29 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<DbResult<T>> UseTranAsync<T>(Func<T> action, Action<Exception> errorCallBack = null)
|
||||
public async Task<DbResult<T>> UseTranAsync<T>(Func<Task<T>> action, Action<Exception> errorCallBack = null)
|
||||
{
|
||||
return Task.FromResult(UseTran(action, errorCallBack));
|
||||
var result = new DbResult<T>();
|
||||
try
|
||||
{
|
||||
this.BeginTran();
|
||||
if (action != null)
|
||||
result.Data = await action();
|
||||
this.CommitTran();
|
||||
result.IsSuccess = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.ErrorException = ex;
|
||||
result.ErrorMessage = ex.Message;
|
||||
result.IsSuccess = false;
|
||||
this.RollbackTran();
|
||||
if (errorCallBack != null)
|
||||
{
|
||||
errorCallBack(ex);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public IAdo UseStoredProcedure()
|
||||
|
@@ -175,6 +175,17 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sugarColumn.IsJson && String.IsNullOrEmpty(sugarColumn.ColumnDataType))
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.PostgreSQL)
|
||||
{
|
||||
column.DataType = "json";
|
||||
}
|
||||
else
|
||||
{
|
||||
column.DataType = "varchar(4000)";
|
||||
}
|
||||
}
|
||||
if (sugarColumn.IsIgnore == false)
|
||||
{
|
||||
column.DbColumnName = sugarColumn.ColumnName.IsNullOrEmpty() ? property.Name : sugarColumn.ColumnName;
|
||||
|
@@ -792,7 +792,7 @@ namespace SqlSugar
|
||||
Check.Exception(this.MapperAction != null || this.MapperActionWithCache != null, "'Mapper’ needs to be written after ‘MergeTable’ ");
|
||||
Check.Exception(this.QueryBuilder.SelectValue.IsNullOrEmpty(), "MergeTable need to use Queryable.Select Method .");
|
||||
Check.Exception(this.QueryBuilder.Skip > 0 || this.QueryBuilder.Take > 0 || this.QueryBuilder.OrderByValue.HasValue(), "MergeTable Queryable cannot Take Skip OrderBy PageToList ");
|
||||
var sqlobj = this.ToSql();
|
||||
var sqlobj = this._ToSql();
|
||||
var index = QueryBuilder.WhereIndex + 1;
|
||||
var result = this.Context.Queryable<T>().AS(SqlBuilder.GetPackTable(sqlobj.Key, "MergeTable")).AddParameters(sqlobj.Value).Select("*").With(SqlWith.Null);
|
||||
result.QueryBuilder.WhereIndex = index;
|
||||
@@ -1448,7 +1448,7 @@ namespace SqlSugar
|
||||
{
|
||||
QueryBuilder.ResultType = typeof(SugarCacheDataTable);
|
||||
InitMapping();
|
||||
var sqlObj = this.ToSql();
|
||||
var sqlObj = this._ToSql();
|
||||
RestoreMapping();
|
||||
DataTable result = null;
|
||||
if (IsCache)
|
||||
@@ -1821,7 +1821,7 @@ namespace SqlSugar
|
||||
protected async Task<List<TResult>> _ToListAsync<TResult>()
|
||||
{
|
||||
List<TResult> result = null;
|
||||
var sqlObj = this.ToSql();
|
||||
var sqlObj = this._ToSql();
|
||||
if (IsCache)
|
||||
{
|
||||
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
|
||||
|
@@ -474,7 +474,7 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual bool IsComplexModel(string sql)
|
||||
{
|
||||
return Regex.IsMatch(sql, @"AS \[\w+\.\w+\]");
|
||||
return Regex.IsMatch(sql, @"AS \[\w+\.\w+\]")|| Regex.IsMatch(sql, @"AS \[\w+\.\w+\.\w+\]");
|
||||
}
|
||||
public string GetSqlQuerySql(string result)
|
||||
{
|
||||
|
@@ -243,7 +243,7 @@ namespace SqlSugar
|
||||
parameters.Add(new SugarParameter(itemParameterName, "%" + val + "%"));
|
||||
i++;
|
||||
}
|
||||
builder.Append($" ({string.Join(" OR ", sqls)}) ");
|
||||
builder.Append($" {type} ({string.Join(" OR ", sqls)}) ");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
@@ -32,6 +32,7 @@ namespace SqlSugar
|
||||
public List<string> PrimaryKeys { get; set; }
|
||||
public bool IsOffIdentity { get; set; }
|
||||
public bool IsWhereColumns { get; set; }
|
||||
public bool? IsListUpdate { get; set; }
|
||||
|
||||
public virtual string SqlTemplate
|
||||
{
|
||||
@@ -112,6 +113,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType, bool isMapping = true)
|
||||
{
|
||||
ILambdaExpressions resolveExpress = this.LambdaExpressions;
|
||||
@@ -156,7 +158,7 @@ namespace SqlSugar
|
||||
}
|
||||
var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
|
||||
var isSingle = groupList.Count() == 1;
|
||||
if (isSingle)
|
||||
if (isSingle&&this.IsListUpdate==null)
|
||||
{
|
||||
return ToSingleSqlString(groupList);
|
||||
}
|
||||
|
@@ -684,7 +684,9 @@ namespace SqlSugar
|
||||
{
|
||||
UpdateObjs = new List<T>();
|
||||
}
|
||||
return Updateable(UpdateObjs.ToArray());
|
||||
var result= (UpdateableProvider<T>)Updateable(UpdateObjs.ToArray());
|
||||
result.UpdateBuilder.IsListUpdate = true;
|
||||
return result;
|
||||
}
|
||||
public virtual IUpdateable<T> Updateable<T>(T UpdateObj) where T : class, new()
|
||||
{
|
||||
|
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlSguarTransaction:IDisposable
|
||||
{
|
||||
private readonly SqlSugarClient context;
|
||||
|
||||
public SqlSguarTransaction(SqlSugarClient client)
|
||||
{
|
||||
context = client;
|
||||
context.BeginTran();
|
||||
}
|
||||
public void CommitTran()
|
||||
{
|
||||
context.CommitTran();
|
||||
}
|
||||
public void RollbackTran()
|
||||
{
|
||||
context.RollbackTran();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
context.RollbackTran();
|
||||
}
|
||||
}
|
||||
}
|
@@ -126,8 +126,7 @@ namespace SqlSugar
|
||||
}
|
||||
else if (type == UtilConstants.TimeSpanType)
|
||||
{
|
||||
if (this.Value != null)
|
||||
this.Value = this.Value.ToString();
|
||||
this.DbType = System.Data.DbType.Time;
|
||||
}
|
||||
else if (type!=null&&type.IsEnum())
|
||||
{
|
||||
|
@@ -477,5 +477,18 @@ namespace SqlSugar
|
||||
var array = model.Args.Skip(1).Select(it => it.IsMember?it.MemberName:it.MemberValue).ToArray();
|
||||
return string.Format("'"+str+ "'", array);
|
||||
}
|
||||
|
||||
public string Abs(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" ABS({0}) ", parameter.MemberName);
|
||||
}
|
||||
|
||||
public string Round(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var parameter2= model.Args[1];
|
||||
return string.Format(" ROUND({0},{1}) ", parameter.MemberName, parameter2.MemberName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -73,5 +73,7 @@ namespace SqlSugar
|
||||
string Oracle_ToChar(MethodCallExpressionModel model);
|
||||
string SqlServer_DateDiff(MethodCallExpressionModel model);
|
||||
string Format(MethodCallExpressionModel model);
|
||||
string Abs(MethodCallExpressionModel model);
|
||||
string Round(MethodCallExpressionModel model);
|
||||
}
|
||||
}
|
||||
|
@@ -130,6 +130,11 @@ namespace SqlSugar
|
||||
public static TResult GetSelfAndAutoFill<TResult>(TResult value) { throw new NotSupportedException("Can only be used in expressions"); }
|
||||
public static DateTime GetDate() { throw new NotSupportedException("Can only be used in expressions"); }
|
||||
public static string GetRandom() { throw new NotSupportedException("Can only be used in expressions"); }
|
||||
|
||||
|
||||
public static T Abs<T>( T value) { throw new NotSupportedException("Can only be used in expressions"); }
|
||||
public static T Round<T>(T value,int precision) { throw new NotSupportedException("Can only be used in expressions"); }
|
||||
|
||||
/// <summary>
|
||||
/// Subquery
|
||||
/// </summary>
|
||||
|
@@ -443,6 +443,9 @@ namespace SqlSugar
|
||||
}
|
||||
else if (item.Type.IsClass())
|
||||
{
|
||||
var mappingKeys = GetMappingColumns(parameter.CurrentExpression);
|
||||
var isSameType = mappingKeys.Keys.Count>0;
|
||||
CallContextThread<Dictionary<string,string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
var shortName = parameter.CommonTempData;
|
||||
@@ -462,7 +465,11 @@ namespace SqlSugar
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (isSameType)
|
||||
{
|
||||
asName = GetAsNameAndShortName(item, shortName, property);
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
@@ -503,6 +510,61 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, string> GetMappingColumns(Expression currentExpression)
|
||||
{
|
||||
Dictionary<string, string> result = new Dictionary<string, string>();
|
||||
if (currentExpression == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
List<Type> types = new List<Type>();
|
||||
int i = 0;
|
||||
if (currentExpression is NewExpression)
|
||||
{
|
||||
i = (currentExpression as NewExpression).Arguments.Count;
|
||||
foreach (var item in (currentExpression as NewExpression).Arguments)
|
||||
{
|
||||
if (item.Type.IsClass())
|
||||
{
|
||||
types.Add(item.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (currentExpression is MemberInitExpression)
|
||||
{
|
||||
i = (currentExpression as MemberInitExpression).Bindings.Count;
|
||||
foreach (var item in (currentExpression as MemberInitExpression).Bindings)
|
||||
{
|
||||
MemberAssignment memberAssignment = (MemberAssignment)item;
|
||||
if (memberAssignment.Expression.Type.IsClass())
|
||||
{
|
||||
types.Add(memberAssignment.Expression.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (types.Count == i)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
var array = currentExpression.ToString().Split(',');
|
||||
foreach (var item in array)
|
||||
{
|
||||
var itemArray = item.Split('=').ToArray();
|
||||
var last = itemArray.Last().Trim().Split('.').First().TrimEnd(')').TrimEnd('}');
|
||||
var first = itemArray.First().Trim();
|
||||
if (first.Contains("{"))
|
||||
{
|
||||
first = first.Split('{').Last().Trim();
|
||||
}
|
||||
if (first.Contains("("))
|
||||
{
|
||||
first = first.Split('(').Last().Trim();
|
||||
}
|
||||
result.Add(first,last);
|
||||
}
|
||||
return result; ;
|
||||
}
|
||||
|
||||
private string GetAsName(Expression item, object shortName, PropertyInfo property)
|
||||
{
|
||||
string asName;
|
||||
@@ -525,7 +587,28 @@ namespace SqlSugar
|
||||
|
||||
return asName;
|
||||
}
|
||||
private string GetAsNameAndShortName(Expression item, object shortName, PropertyInfo property)
|
||||
{
|
||||
string asName;
|
||||
var propertyName = property.Name;
|
||||
var dbColumnName = propertyName;
|
||||
var mappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName == item.Type.Name && it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (mappingInfo.HasValue())
|
||||
{
|
||||
dbColumnName = mappingInfo.DbColumnName;
|
||||
}
|
||||
asName = this.Context.GetTranslationText(shortName+"."+item.Type.Name + "." + propertyName);
|
||||
if (Context.IsJoin)
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName, shortName.ObjToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName));
|
||||
}
|
||||
|
||||
return asName;
|
||||
}
|
||||
private static bool IsBoolValue(Expression item)
|
||||
{
|
||||
return item.Type == UtilConstants.BoolType &&
|
||||
|
@@ -789,6 +789,10 @@ namespace SqlSugar
|
||||
var result = this.Context.DbMehtods.Format(model);
|
||||
this.Context.Parameters.RemoveAll(it => model.Args.Select(x=>x.MemberName.ObjToString()).Contains(it.ParameterName) );
|
||||
return result;
|
||||
case "Abs":
|
||||
return this.Context.DbMehtods.Abs(model);
|
||||
case "Round":
|
||||
return this.Context.DbMehtods.Round(model);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@@ -271,6 +271,7 @@ namespace SqlSugar
|
||||
private Dictionary<string, object> DataReaderToList<T>(IDataReader reader, Type tType, List<PropertyInfo> classProperties, List<T> reval)
|
||||
{
|
||||
var readerValues = DataReaderToDictionary(reader, tType);
|
||||
var mappingKeys = CallContextThread<Dictionary<string, string>>.GetData("Exp_Select_Mapping_Key");
|
||||
var result = new Dictionary<string, object>();
|
||||
foreach (var item in classProperties)
|
||||
{
|
||||
@@ -296,7 +297,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(name, DataReaderToDynamicList_Part(readerValues, item, reval));
|
||||
result.Add(name, DataReaderToDynamicList_Part(readerValues, item, reval, mappingKeys));
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -370,7 +371,7 @@ namespace SqlSugar
|
||||
Regex.IsMatch(readerValues[item.Name.ToLower()].ToString(), @"^\[{.+\}]$");
|
||||
}
|
||||
|
||||
private Dictionary<string, object> DataReaderToDynamicList_Part<T>(Dictionary<string, object> readerValues, PropertyInfo item, List<T> reval)
|
||||
private Dictionary<string, object> DataReaderToDynamicList_Part<T>(Dictionary<string, object> readerValues, PropertyInfo item, List<T> reval, Dictionary<string, string> mappingKeys=null)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
var type = item.PropertyType;
|
||||
@@ -407,6 +408,11 @@ namespace SqlSugar
|
||||
{
|
||||
var key = typeName + "." + name;
|
||||
var info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower());
|
||||
if (mappingKeys.ContainsKey(item.Name))
|
||||
{
|
||||
key = mappingKeys[item.Name]+"."+typeName + "." + name;
|
||||
info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower());
|
||||
}
|
||||
if (info != null)
|
||||
{
|
||||
var addItem = readerValues[info];
|
||||
|
@@ -174,8 +174,8 @@ namespace SqlSugar
|
||||
|
||||
DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack = null);
|
||||
DbResult<T> UseTran<T>(Func<T> action, Action<Exception> errorCallBack = null);
|
||||
Task<DbResult<bool>> UseTranAsync(Action action, Action<Exception> errorCallBack = null);
|
||||
Task<DbResult<T>> UseTranAsync<T>(Func<T> action, Action<Exception> errorCallBack = null);
|
||||
Task<DbResult<bool>> UseTranAsync(Func<Task> action, Action<Exception> errorCallBack = null);
|
||||
Task<DbResult<T>> UseTranAsync<T>(Func<Task<T>> action, Action<Exception> errorCallBack = null);
|
||||
IAdo UseStoredProcedure();
|
||||
|
||||
|
||||
|
@@ -13,6 +13,7 @@ namespace SqlSugar
|
||||
void RollbackTran();
|
||||
void ChangeDatabase(dynamic configId);
|
||||
void ChangeDatabase(Func<ConnectionConfig, bool> changeExpression);
|
||||
SqlSguarTransaction UseTran();
|
||||
DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack = null);
|
||||
Task<DbResult<bool>> UseTranAsync(Func<Task> action, Action<Exception> errorCallBack = null);
|
||||
DbResult<T> UseTran<T>(Func<T> action, Action<Exception> errorCallBack = null);
|
||||
|
@@ -8,7 +8,7 @@ namespace SqlSugar
|
||||
{
|
||||
public override bool IsComplexModel(string sql)
|
||||
{
|
||||
return Regex.IsMatch(sql, @"AS ""\w+\.\w+""");
|
||||
return Regex.IsMatch(sql, @"AS ""\w+\.\w+""")|| Regex.IsMatch(sql, @"AS ""\w+\.\w+\.\w+""");
|
||||
}
|
||||
public override string SqlTemplate
|
||||
{
|
||||
|
@@ -27,7 +27,12 @@ namespace SqlSugar
|
||||
i++;
|
||||
return string.Format("{0} {1} WHERE {2};", updateTable, setValues, string.Join("AND", whereList));
|
||||
}).ToArray()));
|
||||
return sb.ToString();
|
||||
var result= sb.ToString();
|
||||
if (result == "\r\n")
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private string GetOracleUpdateColums(int i, DbColumnInfo m)
|
||||
|
@@ -31,7 +31,7 @@ namespace SqlSugar
|
||||
#region Common Methods
|
||||
public override bool IsComplexModel(string sql)
|
||||
{
|
||||
return Regex.IsMatch(sql, @"AS \`\w+\.\w+\`");
|
||||
return Regex.IsMatch(sql, @"AS \`\w+\.\w+\`")|| Regex.IsMatch(sql, @"AS \`\w+\.\w+\.\w+\`");
|
||||
}
|
||||
public override string ToSqlString()
|
||||
{
|
||||
|
@@ -11,7 +11,7 @@ namespace SqlSugar
|
||||
{
|
||||
public override bool IsComplexModel(string sql)
|
||||
{
|
||||
return Regex.IsMatch(sql, @"AS ""\w+\.\w+""");
|
||||
return Regex.IsMatch(sql, @"AS ""\w+\.\w+""")|| Regex.IsMatch(sql, @"AS ""\w+\.\w+\.\w+""");
|
||||
}
|
||||
public override string SqlTemplate
|
||||
{
|
||||
|
@@ -37,6 +37,11 @@ namespace SqlSugar
|
||||
ColumnDescription = item.ColumnDescription,
|
||||
Length = item.Length
|
||||
};
|
||||
if (propertyType == UtilConstants.DecType)
|
||||
{
|
||||
result.Scale = item.DecimalDigits;
|
||||
result.DecimalDigits = item.DecimalDigits;
|
||||
}
|
||||
GetDbType(item, propertyType, result);
|
||||
if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0)
|
||||
{
|
||||
|
@@ -115,7 +115,7 @@ namespace SqlSugar
|
||||
new KeyValuePair<string, CSharpDataType>("bit",CSharpDataType.byteArray),
|
||||
new KeyValuePair<string, CSharpDataType>("bit varying",CSharpDataType.byteArray),
|
||||
new KeyValuePair<string, CSharpDataType>("varbit",CSharpDataType.@byte),
|
||||
|
||||
new KeyValuePair<string, CSharpDataType>("time",CSharpDataType.TimeSpan),
|
||||
};
|
||||
public override List<string> StringThrow
|
||||
{
|
||||
|
@@ -360,6 +360,10 @@ namespace SqlSugar
|
||||
dataType = "varchar";
|
||||
}
|
||||
string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
|
||||
if (item.DecimalDigits > 0&&item.Length>0 && dataType == "numeric")
|
||||
{
|
||||
dataSize = $"({item.Length},{item.DecimalDigits})";
|
||||
}
|
||||
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
|
||||
string primaryKey = null;
|
||||
string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName.ToLower()), dataType, dataSize, nullType, primaryKey, "");
|
||||
|
@@ -13,7 +13,7 @@ namespace SqlSugar
|
||||
{
|
||||
InsertBuilder.IsReturnIdentity = true;
|
||||
PreToSql();
|
||||
string sql = InsertBuilder.ToSqlString().Replace("$PrimaryKey", GetIdentityKeys().FirstOrDefault());
|
||||
string sql = InsertBuilder.ToSqlString().Replace("$PrimaryKey",this.SqlBuilder.GetTranslationColumnName(GetIdentityKeys().FirstOrDefault()));
|
||||
RestoreMapping();
|
||||
var result = Ado.GetScalar(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()).ObjToInt();
|
||||
return result;
|
||||
@@ -22,7 +22,7 @@ namespace SqlSugar
|
||||
{
|
||||
InsertBuilder.IsReturnIdentity = true;
|
||||
PreToSql();
|
||||
string sql = InsertBuilder.ToSqlString().Replace("$PrimaryKey", GetIdentityKeys().FirstOrDefault());
|
||||
string sql = InsertBuilder.ToSqlString().Replace("$PrimaryKey", this.SqlBuilder.GetTranslationColumnName(GetIdentityKeys().FirstOrDefault()));
|
||||
RestoreMapping();
|
||||
var obj = await Ado.GetScalarAsync(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
|
||||
var result = obj.ObjToInt();
|
||||
@@ -38,7 +38,7 @@ namespace SqlSugar
|
||||
{
|
||||
InsertBuilder.IsReturnIdentity = true;
|
||||
PreToSql();
|
||||
string sql = InsertBuilder.ToSqlString().Replace("$PrimaryKey", GetIdentityKeys().FirstOrDefault());
|
||||
string sql = InsertBuilder.ToSqlString().Replace("$PrimaryKey", this.SqlBuilder.GetTranslationColumnName(GetIdentityKeys().FirstOrDefault()));
|
||||
RestoreMapping();
|
||||
var result = Convert.ToInt64(Ado.GetScalar(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()) ?? "0");
|
||||
return result;
|
||||
@@ -47,7 +47,7 @@ namespace SqlSugar
|
||||
{
|
||||
InsertBuilder.IsReturnIdentity = true;
|
||||
PreToSql();
|
||||
string sql = InsertBuilder.ToSqlString().Replace("$PrimaryKey", GetIdentityKeys().FirstOrDefault());
|
||||
string sql = InsertBuilder.ToSqlString().Replace("$PrimaryKey", this.SqlBuilder.GetTranslationColumnName(GetIdentityKeys().FirstOrDefault()));
|
||||
RestoreMapping();
|
||||
var result = Convert.ToInt64(await Ado.GetScalarAsync(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()) ?? "0");
|
||||
return result;
|
||||
|
@@ -31,7 +31,7 @@ namespace SqlSugar
|
||||
#region Common Methods
|
||||
public override bool IsComplexModel(string sql)
|
||||
{
|
||||
return Regex.IsMatch(sql, @"AS ""\w+\.\w+""");
|
||||
return Regex.IsMatch(sql, @"AS ""\w+\.\w+""")|| Regex.IsMatch(sql, @"AS ""\w+\.\w+\.\w+""");
|
||||
}
|
||||
public override string ToSqlString()
|
||||
{
|
||||
|
@@ -16,7 +16,7 @@ namespace SqlSugar
|
||||
internal InsertBuilder InsertBuilder { get; set; }
|
||||
internal object[] Inserts { get; set; }
|
||||
|
||||
public int ExecuteBlukCopy()
|
||||
public int ExecuteBulkCopy()
|
||||
{
|
||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace SqlSugar
|
||||
return DbColumnInfoList.Count;
|
||||
}
|
||||
|
||||
public async Task<int> ExecuteBlukCopyAsync()
|
||||
public async Task<int> ExecuteBulkCopyAsync()
|
||||
{
|
||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||
|
||||
|
@@ -130,6 +130,12 @@ namespace SqlSugar
|
||||
sqlParameter.Size = parameter.Size;
|
||||
sqlParameter.Value = parameter.Value;
|
||||
sqlParameter.DbType = parameter.DbType;
|
||||
var isTime = parameter.DbType == System.Data.DbType.Time;
|
||||
if (isTime)
|
||||
{
|
||||
sqlParameter.SqlDbType = SqlDbType.Time;
|
||||
sqlParameter.Value=DateTime.Parse(parameter.Value?.ToString()).TimeOfDay;
|
||||
}
|
||||
if (sqlParameter.Value!=null&& sqlParameter.Value != DBNull.Value && sqlParameter.DbType == System.Data.DbType.DateTime)
|
||||
{
|
||||
var date = Convert.ToDateTime(sqlParameter.Value);
|
||||
@@ -138,7 +144,7 @@ namespace SqlSugar
|
||||
sqlParameter.Value = Convert.ToDateTime("1753/01/01");
|
||||
}
|
||||
}
|
||||
if (parameter.Direction == 0)
|
||||
if (parameter.Direction == 0)
|
||||
{
|
||||
parameter.Direction = ParameterDirection.Input;
|
||||
}
|
||||
|
@@ -31,7 +31,7 @@ namespace SqlSugar
|
||||
#region Common Methods
|
||||
public override bool IsComplexModel(string sql)
|
||||
{
|
||||
return Regex.IsMatch(sql, @"AS \`\w+\.\w+\`");
|
||||
return Regex.IsMatch(sql, @"AS \`\w+\.\w+\`")|| Regex.IsMatch(sql, @"AS \`\w+\.\w+\.\w+\`");
|
||||
}
|
||||
public override string ToSqlString()
|
||||
{
|
||||
|
@@ -86,6 +86,7 @@ namespace SqlSugar
|
||||
|
||||
public IInsertable<T> Insertable<T>(T insertObj) where T : class, new()
|
||||
{
|
||||
Check.Exception(typeof(T).FullName.Contains("System.Collections.Generic.List`"), " need where T: class, new() ");
|
||||
return this.Context.Insertable<T>(insertObj);
|
||||
}
|
||||
|
||||
@@ -575,6 +576,10 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region TenantManager
|
||||
public SqlSguarTransaction UseTran()
|
||||
{
|
||||
return new SqlSguarTransaction(this);
|
||||
}
|
||||
public void AddConnection(ConnectionConfig connection)
|
||||
{
|
||||
Check.ArgumentNullException(connection, "AddConnection.connection can't be null");
|
||||
@@ -660,7 +665,19 @@ namespace SqlSugar
|
||||
public void CommitTran()
|
||||
{
|
||||
this.Context.Ado.CommitTran();
|
||||
AllClientEach(it => it.Ado.CommitTran());
|
||||
AllClientEach(it =>
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
it.Ado.CommitTran();
|
||||
}
|
||||
catch
|
||||
{
|
||||
SugarRetry.Execute(() => it.Ado.CommitTran(), new TimeSpan(0, 0, 5), 3);
|
||||
}
|
||||
|
||||
});
|
||||
_IsAllTran = false;
|
||||
}
|
||||
public DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack = null)
|
||||
@@ -748,6 +765,7 @@ namespace SqlSugar
|
||||
if (action != null)
|
||||
data = await action();
|
||||
this.CommitTran();
|
||||
result.IsSuccess = true;
|
||||
result.Data = data;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -767,7 +785,19 @@ namespace SqlSugar
|
||||
public void RollbackTran()
|
||||
{
|
||||
this.Context.Ado.RollbackTran();
|
||||
AllClientEach(it => it.Ado.RollbackTran());
|
||||
AllClientEach(it =>
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
it.Ado.RollbackTran();
|
||||
}
|
||||
catch
|
||||
{
|
||||
SugarRetry.Execute(() => it.Ado.RollbackTran(), new TimeSpan(0, 0, 5), 3);
|
||||
}
|
||||
|
||||
});
|
||||
_IsAllTran = false;
|
||||
}
|
||||
public void Close()
|
||||
|
@@ -34,62 +34,7 @@ namespace SqlSugar
|
||||
_configs = configs;
|
||||
this._configAction = configAction;
|
||||
}
|
||||
//public ScopedClient(SqlSugarClient context,Action<SqlSugarClient> configAction)
|
||||
//{
|
||||
// this.db = context;
|
||||
// this.configAction = configAction;
|
||||
//}
|
||||
public SqlSugarClient ScopedContext
|
||||
{
|
||||
get
|
||||
{
|
||||
SqlSugarClient result = null;
|
||||
var key = _configs.GetHashCode().ToString();
|
||||
StackTrace st = new StackTrace(true);
|
||||
var methods = st.GetFrames();
|
||||
var isAsync = UtilMethods.IsAnyAsyncMethod(methods);
|
||||
if (isAsync)
|
||||
{
|
||||
result=GetAsyncContext(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = GetThreadContext(key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private SqlSugarClient GetAsyncContext(string key)
|
||||
{
|
||||
SqlSugarClient result = CallContextAsync<SqlSugarClient>.GetData(key);
|
||||
if (result == null)
|
||||
{
|
||||
CallContextAsync<SqlSugarClient>.SetData(key, new SqlSugarClient(_configs));
|
||||
result = CallContextAsync<SqlSugarClient>.GetData(key);
|
||||
if (this._configAction != null)
|
||||
{
|
||||
this._configAction(result);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private SqlSugarClient GetThreadContext(string key)
|
||||
{
|
||||
SqlSugarClient result = CallContextThread<SqlSugarClient>.GetData(key);
|
||||
if (result == null)
|
||||
{
|
||||
CallContextThread<SqlSugarClient>.SetData(key, new SqlSugarClient(_configs));
|
||||
result = CallContextThread<SqlSugarClient>.GetData(key);
|
||||
if (this._configAction != null)
|
||||
{
|
||||
this._configAction(result);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public SqlSugarClient ScopedContext{ get{ return GetContext();}}
|
||||
|
||||
public MappingTableList MappingTables { get => ScopedContext.MappingTables; set => ScopedContext.MappingTables = value; }
|
||||
public MappingColumnList MappingColumns { get => ScopedContext.MappingColumns; set => ScopedContext.MappingColumns=value; }
|
||||
@@ -172,7 +117,7 @@ namespace SqlSugar
|
||||
|
||||
public IDeleteable<T> Deleteable<T>(dynamic primaryKeyValue) where T : class, new()
|
||||
{
|
||||
return ScopedContext.Deleteable(primaryKeyValue);
|
||||
return ScopedContext.Deleteable<T>(primaryKeyValue);
|
||||
}
|
||||
|
||||
public IDeleteable<T> Deleteable<T>(dynamic[] primaryKeyValues) where T : class, new()
|
||||
@@ -651,7 +596,10 @@ namespace SqlSugar
|
||||
{
|
||||
return ScopedContext.Updateable(UpdateObjs);
|
||||
}
|
||||
|
||||
public SqlSguarTransaction UseTran()
|
||||
{
|
||||
return ScopedContext.UseTran();
|
||||
}
|
||||
public DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack = null)
|
||||
{
|
||||
return ScopedContext.UseTran(action,errorCallBack);
|
||||
@@ -676,5 +624,54 @@ namespace SqlSugar
|
||||
{
|
||||
return ScopedContext.IsAnyConnection(configId);
|
||||
}
|
||||
|
||||
private SqlSugarClient GetContext()
|
||||
{
|
||||
SqlSugarClient result = null;
|
||||
var key = _configs.GetHashCode().ToString();
|
||||
StackTrace st = new StackTrace(true);
|
||||
var methods = st.GetFrames();
|
||||
var isAsync = UtilMethods.IsAnyAsyncMethod(methods);
|
||||
if (isAsync)
|
||||
{
|
||||
result = GetAsyncContext(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = GetThreadContext(key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private SqlSugarClient GetAsyncContext(string key)
|
||||
{
|
||||
SqlSugarClient result = CallContextAsync<SqlSugarClient>.GetData(key);
|
||||
if (result == null)
|
||||
{
|
||||
CallContextAsync<SqlSugarClient>.SetData(key, new SqlSugarClient(_configs));
|
||||
result = CallContextAsync<SqlSugarClient>.GetData(key);
|
||||
if (this._configAction != null)
|
||||
{
|
||||
this._configAction(result);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private SqlSugarClient GetThreadContext(string key)
|
||||
{
|
||||
SqlSugarClient result = CallContextThread<SqlSugarClient>.GetData(key);
|
||||
if (result == null)
|
||||
{
|
||||
CallContextThread<SqlSugarClient>.SetData(key, new SqlSugarClient(_configs));
|
||||
result = CallContextThread<SqlSugarClient>.GetData(key);
|
||||
if (this._configAction != null)
|
||||
{
|
||||
this._configAction(result);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
166
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Utilities/SugarRetry.cs
Normal file
166
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Utilities/SugarRetry.cs
Normal file
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public static class SugarRetry
|
||||
{
|
||||
|
||||
public static void Execute(Action action, TimeSpan retryInterval, int retryCount = 3)
|
||||
{
|
||||
Execute<object>(() =>
|
||||
{
|
||||
action();
|
||||
return null;
|
||||
}, retryInterval, retryCount);
|
||||
}
|
||||
|
||||
|
||||
public static void Execute<T1>(Action<T1> action, T1 arg1, TimeSpan retryInterval, int retryCount = 3)
|
||||
{
|
||||
Execute<T1, object>((x1) =>
|
||||
{
|
||||
action(arg1);
|
||||
return null;
|
||||
}, arg1, retryInterval, retryCount);
|
||||
}
|
||||
|
||||
|
||||
public static void Execute<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2, TimeSpan retryInterval, int retryCount = 3)
|
||||
{
|
||||
Execute<T1, T2, object>((x1, x2) =>
|
||||
{
|
||||
action(arg1, arg2);
|
||||
return null;
|
||||
}, arg1, arg2, retryInterval, retryCount);
|
||||
}
|
||||
|
||||
|
||||
public static void Execute<T1, T2, T3>(Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3, TimeSpan retryInterval, int retryCount = 3)
|
||||
{
|
||||
Execute<T1, T2, T3, object>((x1, x2, x3) =>
|
||||
{
|
||||
action(arg1, arg2, arg3);
|
||||
return null;
|
||||
}, arg1, arg2, arg3, retryInterval, retryCount);
|
||||
}
|
||||
|
||||
|
||||
public static void Execute<T1, T2, T3, T4>(Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, TimeSpan retryInterval, int retryCount = 3)
|
||||
{
|
||||
Execute<T1, T2, T3, T4, object>((x1, x2, x3, x4) =>
|
||||
{
|
||||
action(arg1, arg2, arg3, arg4);
|
||||
return null;
|
||||
}, arg1, arg2, arg3, arg4, retryInterval, retryCount);
|
||||
}
|
||||
|
||||
|
||||
public static T Execute<T>(Func<T> func, TimeSpan retryInterval, int retryCount = 3)
|
||||
{
|
||||
var exceptions = new List<Exception>();
|
||||
|
||||
for (int retry = 0; retry < retryCount; retry++)
|
||||
{
|
||||
try
|
||||
{
|
||||
return func();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exceptions.Add(ex);
|
||||
Thread.Sleep(retryInterval);
|
||||
}
|
||||
}
|
||||
|
||||
throw new AggregateException(exceptions);
|
||||
}
|
||||
|
||||
|
||||
public static T Execute<T1, T>(Func<T1, T> func, T1 arg1, TimeSpan retryInterval, int retryCount = 3)
|
||||
{
|
||||
var exceptions = new List<Exception>();
|
||||
|
||||
for (int retry = 0; retry < retryCount; retry++)
|
||||
{
|
||||
try
|
||||
{
|
||||
return func(arg1);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exceptions.Add(ex);
|
||||
Thread.Sleep(retryInterval);
|
||||
}
|
||||
}
|
||||
|
||||
throw new AggregateException(exceptions);
|
||||
}
|
||||
|
||||
public static T Execute<T1, T2, T>(Func<T1, T2, T> func, T1 arg1, T2 arg2, TimeSpan retryInterval, int retryCount = 3)
|
||||
{
|
||||
var exceptions = new List<Exception>();
|
||||
|
||||
for (int retry = 0; retry < retryCount; retry++)
|
||||
{
|
||||
try
|
||||
{
|
||||
return func(arg1, arg2);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exceptions.Add(ex);
|
||||
Thread.Sleep(retryInterval);
|
||||
}
|
||||
}
|
||||
|
||||
throw new AggregateException(exceptions);
|
||||
}
|
||||
|
||||
|
||||
public static T Execute<T1, T2, T3, T>(Func<T1, T2, T3, T> func, T1 arg1, T2 arg2, T3 arg3, TimeSpan retryInterval, int retryCount = 3)
|
||||
{
|
||||
var exceptions = new List<Exception>();
|
||||
|
||||
for (int retry = 0; retry < retryCount; retry++)
|
||||
{
|
||||
try
|
||||
{
|
||||
return func(arg1, arg2, arg3);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exceptions.Add(ex);
|
||||
Thread.Sleep(retryInterval);
|
||||
}
|
||||
}
|
||||
|
||||
throw new AggregateException(exceptions);
|
||||
}
|
||||
|
||||
|
||||
public static T Execute<T1, T2, T3, T4, T>(Func<T1, T2, T3, T4, T> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, TimeSpan retryInterval, int retryCount = 3)
|
||||
{
|
||||
var exceptions = new List<Exception>();
|
||||
|
||||
for (int retry = 0; retry < retryCount; retry++)
|
||||
{
|
||||
try
|
||||
{
|
||||
return func(arg1, arg2, arg3, arg4);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exceptions.Add(ex);
|
||||
Thread.Sleep(retryInterval);
|
||||
}
|
||||
}
|
||||
|
||||
throw new AggregateException(exceptions);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user