mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-24 13:03:43 +08:00
Update core
This commit is contained in:
@@ -40,6 +40,7 @@ namespace SqlSugar
|
||||
internal bool OldClearParameters { get; set; }
|
||||
public IDataParameterCollection DataReaderParameters { get; set; }
|
||||
public TimeSpan SqlExecutionTime { get { return AfterTime - BeforeTime; } }
|
||||
public StackTraceInfo SqlStackTrace { get { return UtilMethods.GetStackTrace(); } }
|
||||
public bool IsDisableMasterSlaveSeparation { get; set; }
|
||||
internal DateTime BeforeTime = DateTime.MinValue;
|
||||
internal DateTime AfterTime = DateTime.MinValue;
|
||||
@@ -68,6 +69,7 @@ namespace SqlSugar
|
||||
public virtual Action<DiffLogModel> DiffLogEvent => this.Context.CurrentConnectionConfig.AopEvents?.OnDiffLogEvent;
|
||||
public virtual List<IDbConnection> SlaveConnections { get; set; }
|
||||
public virtual IDbConnection MasterConnection { get; set; }
|
||||
public virtual CancellationToken? CancellationToken { get; set; }
|
||||
#endregion
|
||||
|
||||
#region Connection
|
||||
@@ -311,7 +313,7 @@ namespace SqlSugar
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
SetConnectionEnd(sql);
|
||||
//sqlCommand.Dispose();
|
||||
sqlCommand.Dispose();
|
||||
return sqlDataReader;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -404,7 +406,11 @@ namespace SqlSugar
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
ExecuteBefore(sql, parameters);
|
||||
var sqlCommand = GetCommand(sql, parameters);
|
||||
int count = await sqlCommand.ExecuteNonQueryAsync();
|
||||
int count;
|
||||
if (this.CancellationToken == null)
|
||||
count=await sqlCommand.ExecuteNonQueryAsync();
|
||||
else
|
||||
count=await sqlCommand.ExecuteNonQueryAsync(this.CancellationToken.Value);
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
@@ -438,14 +444,18 @@ namespace SqlSugar
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
ExecuteBefore(sql, parameters);
|
||||
var sqlCommand = GetCommand(sql, parameters);
|
||||
var sqlDataReader = await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default);
|
||||
DbDataReader sqlDataReader;
|
||||
if(this.CancellationToken==null)
|
||||
sqlDataReader=await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default);
|
||||
else
|
||||
sqlDataReader=await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default,this.CancellationToken.Value);
|
||||
if (isSp)
|
||||
DataReaderParameters = sqlCommand.Parameters;
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
SetConnectionEnd(sql);
|
||||
//sqlCommand.Dispose();
|
||||
sqlCommand.Dispose();
|
||||
return sqlDataReader;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -469,7 +479,11 @@ namespace SqlSugar
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
ExecuteBefore(sql, parameters);
|
||||
var sqlCommand = GetCommand(sql, parameters);
|
||||
var scalar = await sqlCommand.ExecuteScalarAsync();
|
||||
object scalar;
|
||||
if(CancellationToken==null)
|
||||
scalar=await sqlCommand.ExecuteScalarAsync();
|
||||
else
|
||||
scalar = await sqlCommand.ExecuteScalarAsync(this.CancellationToken.Value);
|
||||
//scalar = (scalar == null ? 0 : scalar);
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
@@ -882,7 +896,7 @@ namespace SqlSugar
|
||||
}
|
||||
public async Task<Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>>> SqlQueryAsync<T, T2, T3, T4, T5, T6>(string sql, object parameters = null)
|
||||
{
|
||||
var result = await SqlQueryAsync<T, T2, T3, T4, T5, T6, object>(sql, parameters);
|
||||
var result =await SqlQueryAsync<T, T2, T3, T4, T5, T6, object>(sql, parameters);
|
||||
return new Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>>(result.Item1, result.Item2, result.Item3, result.Item4, result.Item5, result.Item6);
|
||||
}
|
||||
public async Task<Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>, List<T7>>> SqlQueryAsync<T, T2, T3, T4, T5, T6, T7>(string sql, object parameters = null)
|
||||
@@ -899,7 +913,7 @@ namespace SqlSugar
|
||||
List<T> result = new List<T>();
|
||||
if (DbReader.HasRows)
|
||||
{
|
||||
result = await GetDataAsync<T>(typeof(T), dataReader);
|
||||
result =await GetDataAsync<T>(typeof(T), dataReader);
|
||||
}
|
||||
List<T2> result2 = null;
|
||||
if (NextResult(dataReader))
|
||||
@@ -1163,6 +1177,10 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Helper
|
||||
public virtual void RemoveCancellationToken()
|
||||
{
|
||||
this.CancellationToken = null;
|
||||
}
|
||||
private void Async()
|
||||
{
|
||||
if (this.Context.Root != null & this.Context.Root.AsyncId == null)
|
||||
@@ -1341,7 +1359,7 @@ namespace SqlSugar
|
||||
if (item.Value != null)
|
||||
{
|
||||
var type = item.Value.GetType();
|
||||
if ((type != UtilConstants.ByteArrayType && type.IsArray && item.IsArray == false) || type.FullName.IsCollectionsList())
|
||||
if ((type != UtilConstants.ByteArrayType && type.IsArray&&item.IsArray==false) || type.FullName.IsCollectionsList())
|
||||
{
|
||||
var newValues = new List<string>();
|
||||
foreach (var inValute in item.Value as IEnumerable)
|
||||
@@ -1356,7 +1374,14 @@ namespace SqlSugar
|
||||
{
|
||||
sql = sql.Replace("@" + item.ParameterName.Substring(1), newValues.ToArray().ToJoinSqlInVals());
|
||||
}
|
||||
if (item.ParameterName.Substring(0, 1) != this.SqlParameterKeyWord)
|
||||
{
|
||||
sql = sql.Replace(this.SqlParameterKeyWord+item.ParameterName, newValues.ToArray().ToJoinSqlInVals());
|
||||
}
|
||||
else
|
||||
{
|
||||
sql = sql.Replace(item.ParameterName, newValues.ToArray().ToJoinSqlInVals());
|
||||
}
|
||||
item.Value = DBNull.Value;
|
||||
}
|
||||
}
|
||||
@@ -1389,7 +1414,7 @@ namespace SqlSugar
|
||||
List<TResult> result;
|
||||
if (entityType == UtilConstants.DynamicType)
|
||||
{
|
||||
result = await this.Context.Utilities.DataReaderToExpandoObjectListAsyncNoUsing(dataReader) as List<TResult>;
|
||||
result =await this.Context.Utilities.DataReaderToExpandoObjectListAsyncNoUsing(dataReader) as List<TResult>;
|
||||
}
|
||||
else if (entityType == UtilConstants.ObjType)
|
||||
{
|
||||
@@ -1398,11 +1423,11 @@ namespace SqlSugar
|
||||
}
|
||||
else if (entityType.IsAnonymousType())
|
||||
{
|
||||
result = await this.Context.Utilities.DataReaderToListAsyncNoUsing<TResult>(dataReader);
|
||||
result =await this.Context.Utilities.DataReaderToListAsyncNoUsing<TResult>(dataReader);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await this.Context.Ado.DbBind.DataReaderToListNoUsingAsync<TResult>(entityType, dataReader);
|
||||
result =await this.Context.Ado.DbBind.DataReaderToListNoUsingAsync<TResult>(entityType, dataReader);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@@ -124,6 +124,10 @@ namespace SqlSugar
|
||||
{
|
||||
BindField(generator, result, columnInfo, ReaderKeys.First(it => it.Equals(fileName, StringComparison.CurrentCultureIgnoreCase)));
|
||||
}
|
||||
else if (this.ReaderKeys.Any(it => it.Equals(columnInfo.PropertyName, StringComparison.CurrentCultureIgnoreCase)))
|
||||
{
|
||||
BindField(generator, result, columnInfo, ReaderKeys.First(it => it.Equals(columnInfo.PropertyName, StringComparison.CurrentCultureIgnoreCase)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -153,6 +153,18 @@ namespace SqlSugar
|
||||
{
|
||||
return UtilMethods.To<T>(dr.GetConvertDouble(i));
|
||||
}
|
||||
if (dr.GetFieldType(i) == UtilConstants.GuidType)
|
||||
{
|
||||
var data = dr.GetString(i);
|
||||
if (data.ToString() == "")
|
||||
{
|
||||
return UtilMethods.To<T>(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
return UtilMethods.To<T>(Guid.Parse(data.ToString()));
|
||||
}
|
||||
}
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
}
|
||||
|
@@ -115,6 +115,7 @@ namespace SqlSugar
|
||||
}
|
||||
public ISugarQueryable<T> Mapper<TObject>(Expression<Func<T, TObject>> mapperObject, Expression<Func<T, object>> mainField, Expression<Func<T, object>> childField)
|
||||
{
|
||||
Check.Exception(mapperObject.ReturnType.Name == "IList`1", "Mapper no support IList , Use List<T>");
|
||||
return _Mapper<TObject>(mapperObject, mainField, childField);
|
||||
}
|
||||
public ISugarQueryable<T> Mapper<TObject>(Expression<Func<T, List<TObject>>> mapperObject, Expression<Func<T, object>> mainField, Expression<Func<T, object>> childField)
|
||||
|
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class StackTraceInfo
|
||||
{
|
||||
public string FirstFileName { get { return this.MyStackTraceList.First().FileName; } }
|
||||
public string FirstMethodName { get { return this.MyStackTraceList.First().MethodName; } }
|
||||
public int FirstLine { get { return this.MyStackTraceList.First().Line; } }
|
||||
|
||||
public List<StackTraceInfoItem> MyStackTraceList { get; set; }
|
||||
public List<StackTraceInfoItem> SugarStackTraceList { get; set; }
|
||||
}
|
||||
public class StackTraceInfoItem
|
||||
{
|
||||
public string FileName { get; set; }
|
||||
public string MethodName { get; set; }
|
||||
public int Line { get; set; }
|
||||
}
|
||||
}
|
@@ -252,6 +252,12 @@ namespace SqlSugar
|
||||
return string.Format(" CAST({0} AS DATETIME)", parameter.MemberName);
|
||||
}
|
||||
|
||||
public virtual string ToDateShort(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" CAST({0} AS DATE)", parameter.MemberName);
|
||||
}
|
||||
|
||||
public virtual string ToTime(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
|
@@ -42,6 +42,7 @@ namespace SqlSugar
|
||||
string CaseWhen(List<KeyValuePair<string,string>> sqls);
|
||||
string Substring(MethodCallExpressionModel model);
|
||||
string ToDate(MethodCallExpressionModel model);
|
||||
string ToDateShort(MethodCallExpressionModel model);
|
||||
string ToTime(MethodCallExpressionModel model);
|
||||
string ToDecimal(MethodCallExpressionModel model);
|
||||
string Length(MethodCallExpressionModel model);
|
||||
|
@@ -98,6 +98,7 @@ namespace SqlSugar
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime ToDate(object value) { return value.ObjToDate(); }
|
||||
public static DateTime ToDateShort(object value) { return value.ObjToDate(); }
|
||||
/// <summary>
|
||||
///HH:mm:ss
|
||||
/// </summary>
|
||||
|
@@ -438,26 +438,15 @@ namespace SqlSugar
|
||||
}
|
||||
if (property.PropertyType.IsClass())
|
||||
{
|
||||
|
||||
var comumnInfo=property.GetCustomAttribute<SugarColumn>();
|
||||
if (comumnInfo != null && comumnInfo.IsJson)
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
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(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));
|
||||
}
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -496,6 +485,29 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
private string GetAsName(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(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 &&
|
||||
|
@@ -178,12 +178,7 @@ namespace SqlSugar
|
||||
var value = GetNewExpressionValue(expression.Expression);
|
||||
if (expression.Member.Name == "Date")
|
||||
{
|
||||
AppendMember(parameter, isLeft, GetToDate(this.Context.DbMehtods.MergeString(
|
||||
this.GetDateValue(value, DateType.Year),
|
||||
"'-'",
|
||||
this.GetDateValue(value, DateType.Month),
|
||||
"'-'",
|
||||
this.GetDateValue(value, DateType.Day))));
|
||||
AppendMember(parameter, isLeft, GetToDateShort(value));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -292,12 +287,7 @@ namespace SqlSugar
|
||||
new MethodCallExpressionArgs() { MemberName=DateType.Year, MemberValue=DateType.Year}
|
||||
}
|
||||
};
|
||||
AppendMember(parameter, isLeft, GetToDate(this.Context.DbMehtods.MergeString(
|
||||
this.GetDateValue(parameter.CommonTempData, DateType.Year),
|
||||
"'-'",
|
||||
this.GetDateValue(parameter.CommonTempData, DateType.Month),
|
||||
"'-'",
|
||||
this.GetDateValue(parameter.CommonTempData, DateType.Day))));
|
||||
AppendMember(parameter, isLeft, GetToDateShort(parameter.CommonTempData.ObjToString()));
|
||||
}
|
||||
parameter.CommonTempData = oldCommonTempDate;
|
||||
}
|
||||
@@ -456,7 +446,7 @@ namespace SqlSugar
|
||||
return this.Context.DbMehtods.DateValue(pars);
|
||||
}
|
||||
|
||||
private string GetToDate(string value)
|
||||
private string GetToDateShort(string value)
|
||||
{
|
||||
var pars = new MethodCallExpressionModel()
|
||||
{
|
||||
@@ -464,7 +454,7 @@ namespace SqlSugar
|
||||
new MethodCallExpressionArgs() { MemberName=value, MemberValue=value },
|
||||
}
|
||||
};
|
||||
return this.Context.DbMehtods.ToDate(pars);
|
||||
return this.Context.DbMehtods.ToDateShort(pars);
|
||||
}
|
||||
|
||||
private void SettingParameters(ExpressionParameter parameter, out ExpressionParameter baseParameter, out MemberExpression expression, out bool? isLeft, out bool isSetTempData, out bool isValue, out bool isValueBool, out bool isLength, out bool isDateValue, out bool isHasValue, out bool isDateDate, out bool isMemberValue, out bool isSingle, out bool fieldIsBool, out bool isSelectField, out bool isField)
|
||||
|
@@ -668,6 +668,8 @@ namespace SqlSugar
|
||||
return this.Context.DbMehtods.BitwiseAnd(model);
|
||||
case "BitwiseInclusiveOR":
|
||||
return this.Context.DbMehtods.BitwiseInclusiveOR(model);
|
||||
case "ToDateShort":
|
||||
return this.Context.DbMehtods.ToDateShort(model);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@@ -372,9 +372,21 @@ namespace SqlSugar
|
||||
var name = prop.Name;
|
||||
var typeName = type.Name;
|
||||
if (prop.PropertyType.IsClass())
|
||||
{
|
||||
var suagrColumn=prop.GetCustomAttribute<SugarColumn>();
|
||||
if (suagrColumn != null && suagrColumn.IsJson)
|
||||
{
|
||||
var key = (typeName + "." + name).ToLower();
|
||||
if (readerValues.ContainsKey(key)&& readerValues[key]!=null)
|
||||
{
|
||||
result.Add(name,this.DeserializeObject<List<Dictionary<string,object>>>(readerValues[key]+""));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(name, DataReaderToDynamicList_Part(readerValues, prop, reval));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var key = typeName + "." + name;
|
||||
|
@@ -6,11 +6,12 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
|
||||
public class InstanceFactory
|
||||
{
|
||||
static Assembly assembly = Assembly.Load(UtilConstants.AssemblyName);
|
||||
static Dictionary<string, Type> typeCache = new Dictionary<string, Type>();
|
||||
|
||||
public static bool NoCache = false;
|
||||
|
||||
public static void RemoveCache()
|
||||
{
|
||||
@@ -342,6 +343,26 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
private static Restult CreateInstance<Restult>(string className, params Type[] types)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (NoCache)
|
||||
{
|
||||
return NoCacheGetCacheInstance<Restult>(className, types);
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetCacheInstance<Restult>(className, types);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
NoCache = true;
|
||||
return NoCacheGetCacheInstance<Restult>(className, types);
|
||||
}
|
||||
}
|
||||
|
||||
private static Restult GetCacheInstance<Restult>(string className, Type[] types)
|
||||
{
|
||||
var cacheKey = className + string.Join(",", types.Select(it => it.FullName));
|
||||
Type type;
|
||||
@@ -364,7 +385,34 @@ namespace SqlSugar
|
||||
var result = (Restult)Activator.CreateInstance(type, true);
|
||||
return result;
|
||||
}
|
||||
private static Restult NoCacheGetCacheInstance<Restult>(string className, Type[] types)
|
||||
{
|
||||
|
||||
Type type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types);
|
||||
var result = (Restult)Activator.CreateInstance(type, true);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static T CreateInstance<T>(string className)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (NoCache)
|
||||
{
|
||||
return NoCacheGetCacheInstance<T>(className);
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetCacheInstance<T>(className);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return NoCacheGetCacheInstance<T>(className);
|
||||
}
|
||||
}
|
||||
|
||||
private static T GetCacheInstance<T>(string className)
|
||||
{
|
||||
Type type;
|
||||
if (typeCache.ContainsKey(className))
|
||||
@@ -386,6 +434,12 @@ namespace SqlSugar
|
||||
var result = (T)Activator.CreateInstance(type, true);
|
||||
return result;
|
||||
}
|
||||
private static T NoCacheGetCacheInstance<T>(string className)
|
||||
{
|
||||
Type type = assembly.GetType(className);
|
||||
var result = (T)Activator.CreateInstance(type, true);
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
@@ -21,10 +22,10 @@ namespace SqlSugar
|
||||
void ExecuteBefore(string sql, SugarParameter[] pars);
|
||||
void ExecuteAfter(string sql, SugarParameter[] pars);
|
||||
bool IsEnableLogEvent{get;set;}
|
||||
|
||||
StackTraceInfo SqlStackTrace { get; }
|
||||
IDataParameterCollection DataReaderParameters { get; set; }
|
||||
CommandType CommandType { get; set; }
|
||||
|
||||
CancellationToken? CancellationToken { get; set; }
|
||||
bool IsDisableMasterSlaveSeparation { get; set; }
|
||||
bool IsClearParameters { get; set; }
|
||||
int CommandTimeOut { get; set; }
|
||||
@@ -157,6 +158,7 @@ namespace SqlSugar
|
||||
Task<T> SqlQuerySingleAsync<T>(string sql, params SugarParameter[] parameters);
|
||||
Task<T> SqlQuerySingleAsync<T>(string sql, List<SugarParameter> parameters);
|
||||
|
||||
void RemoveCancellationToken();
|
||||
|
||||
void Dispose();
|
||||
void Close();
|
||||
|
@@ -174,7 +174,12 @@ namespace SqlSugar
|
||||
public override string ToDate(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" to_date({0},'yyyy-mm-dd hh24:mi:ss')", parameter.MemberName);
|
||||
return string.Format(" cast({0} as TIMESTAMP)", parameter.MemberName);
|
||||
}
|
||||
public override string ToDateShort(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" TRUNC({0},'dd') ", parameter.MemberName);
|
||||
}
|
||||
public override string Contains(MethodCallExpressionModel model)
|
||||
{
|
||||
|
@@ -16,17 +16,60 @@ namespace SqlSugar
|
||||
internal InsertBuilder InsertBuilder { get; set; }
|
||||
public int ExecuteBlueCopy()
|
||||
{
|
||||
if (DbColumnInfoList==null||DbColumnInfoList.Count == 0) return 0;
|
||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||
|
||||
var dt= this.Context.Ado.GetDataTable("select top 0 * from " + InsertBuilder.GetTableNameString);
|
||||
DataTable dt;
|
||||
SqlBulkCopy bulkCopy;
|
||||
SetCopyData(out dt, out bulkCopy);
|
||||
try
|
||||
{
|
||||
bulkCopy.WriteToServer(dt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
throw ex;
|
||||
}
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
}
|
||||
return DbColumnInfoList.Count;
|
||||
}
|
||||
public async Task<int> ExecuteBlueCopyAsync()
|
||||
{
|
||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||
|
||||
DataTable dt;
|
||||
SqlBulkCopy bulkCopy;
|
||||
SetCopyData(out dt, out bulkCopy);
|
||||
try
|
||||
{
|
||||
await bulkCopy.WriteToServerAsync(dt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
throw ex;
|
||||
}
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
}
|
||||
return DbColumnInfoList.Count;
|
||||
}
|
||||
|
||||
private void SetCopyData(out DataTable dt, out SqlBulkCopy bulkCopy)
|
||||
{
|
||||
dt = this.Context.Ado.GetDataTable("select top 0 * from " + InsertBuilder.GetTableNameString);
|
||||
foreach (var rowInfos in DbColumnInfoList)
|
||||
{
|
||||
var dr = dt.NewRow();
|
||||
foreach (DataColumn item in dt.Columns)
|
||||
{
|
||||
var rows= rowInfos.ToList();
|
||||
var rows = rowInfos.ToList();
|
||||
var value = rows.FirstOrDefault(it =>
|
||||
it.DbColumnName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase)||
|
||||
it.DbColumnName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase) ||
|
||||
it.PropertyName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase)
|
||||
);
|
||||
if (value != null)
|
||||
@@ -43,7 +86,16 @@ namespace SqlSugar
|
||||
}
|
||||
dt.Rows.Add(dr);
|
||||
}
|
||||
SqlBulkCopy bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection);
|
||||
bulkCopy = null;
|
||||
if (this.Context.Ado.Transaction != null)
|
||||
{
|
||||
var sqlTran = this.Context.Ado.Transaction as SqlTransaction;
|
||||
bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection, SqlBulkCopyOptions.CheckConstraints, sqlTran);
|
||||
}
|
||||
else
|
||||
{
|
||||
bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection);
|
||||
}
|
||||
//获取目标表的名称
|
||||
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
|
||||
//写入DataReader对象
|
||||
@@ -51,20 +103,6 @@ namespace SqlSugar
|
||||
{
|
||||
this.Context.Ado.Connection.Open();
|
||||
}
|
||||
try
|
||||
{
|
||||
bulkCopy.WriteToServer(dt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
throw ex;
|
||||
}
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
}
|
||||
return DbColumnInfoList.Count;
|
||||
}
|
||||
|
||||
private object AddParameter(int i,string dbColumnName, object value)
|
||||
|
@@ -97,6 +97,12 @@ namespace SqlSugar
|
||||
return string.Format(" DATETIME({0})", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string ToDateShort(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" strftime('%Y-%m-%d', {0})", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string DateAddDay(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -44,6 +45,38 @@ namespace SqlSugar
|
||||
return value;
|
||||
}
|
||||
|
||||
public static StackTraceInfo GetStackTrace()
|
||||
{
|
||||
|
||||
StackTrace st = new StackTrace(true);
|
||||
StackTraceInfo info = new StackTraceInfo();
|
||||
info.MyStackTraceList = new List<StackTraceInfoItem>();
|
||||
info.SugarStackTraceList = new List<StackTraceInfoItem>();
|
||||
for (int i = 0; i < st.FrameCount; i++)
|
||||
{
|
||||
var frame = st.GetFrame(i);
|
||||
if (frame.GetMethod().Module.Name.ToLower() != "sqlsugar.dll"&& frame.GetMethod().Name.First()!='<')
|
||||
{
|
||||
info.MyStackTraceList.Add(new StackTraceInfoItem()
|
||||
{
|
||||
FileName = frame.GetFileName(),
|
||||
MethodName = frame.GetMethod().Name,
|
||||
Line = frame.GetFileLineNumber()
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
info.SugarStackTraceList.Add(new StackTraceInfoItem()
|
||||
{
|
||||
FileName = frame.GetFileName(),
|
||||
MethodName = frame.GetMethod().Name,
|
||||
Line = frame.GetFileLineNumber()
|
||||
});
|
||||
}
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
internal static T To<T>(object value)
|
||||
{
|
||||
return (T)To(value, typeof(T));
|
||||
@@ -59,7 +92,7 @@ namespace SqlSugar
|
||||
itemSql = Regex.Replace(itemSql, string.Format(@"{0}\)", "\\" + itemParameter.ParameterName), newName + ")", RegexOptions.IgnoreCase);
|
||||
itemSql = Regex.Replace(itemSql, string.Format(@"{0}\,", "\\" + itemParameter.ParameterName), newName + ",", RegexOptions.IgnoreCase);
|
||||
itemSql = Regex.Replace(itemSql, string.Format(@"{0}$", "\\" + itemParameter.ParameterName), newName, RegexOptions.IgnoreCase);
|
||||
itemSql = Regex.Replace(itemSql, string.Format(@"\+{0}\+", "\\" + itemParameter.ParameterName), "+"+newName+"+", RegexOptions.IgnoreCase);
|
||||
itemSql = Regex.Replace(itemSql, string.Format(@"\+{0}\+", "\\" + itemParameter.ParameterName), "+" + newName + "+", RegexOptions.IgnoreCase);
|
||||
itemSql = Regex.Replace(itemSql, string.Format(@"\|\|{0}\|\|", "\\" + itemParameter.ParameterName), "+" + newName + "+", RegexOptions.IgnoreCase);
|
||||
return itemSql;
|
||||
}
|
||||
@@ -119,7 +152,7 @@ namespace SqlSugar
|
||||
return (T)Convert.ChangeType(obj, typeof(T));
|
||||
}
|
||||
|
||||
internal static void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex,string append=null)
|
||||
internal static void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex, string append = null)
|
||||
{
|
||||
if (appendSql.HasValue() && parameters.HasValue())
|
||||
{
|
||||
@@ -127,7 +160,7 @@ namespace SqlSugar
|
||||
{
|
||||
//Compatible with.NET CORE parameters case
|
||||
var name = parameter.ParameterName;
|
||||
string newName = name +append+ addIndex;
|
||||
string newName = name + append + addIndex;
|
||||
appendSql = ReplaceSqlParameter(appendSql, parameter, newName);
|
||||
parameter.ParameterName = newName;
|
||||
}
|
||||
|
Reference in New Issue
Block a user