mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Update Core
This commit is contained in:
parent
716a63b816
commit
5664311f83
@ -5,6 +5,7 @@ using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
@ -55,6 +56,8 @@ namespace SqlSugar
|
||||
public virtual Action<string, SugarParameter[]> LogEventCompleted { get; set; }
|
||||
public virtual Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get; set; }
|
||||
public virtual Action<Exception> ErrorEvent { get; set; }
|
||||
public virtual List<IDbConnection> SlaveConnections { get; set; }
|
||||
public virtual IDbConnection MasterConnection { get; set; }
|
||||
#endregion
|
||||
|
||||
#region Connection
|
||||
@ -72,6 +75,16 @@ namespace SqlSugar
|
||||
{
|
||||
this.Connection.Close();
|
||||
}
|
||||
if (this.IsMasterSlaveSeparation)
|
||||
{
|
||||
foreach (var slaveConnection in this.SlaveConnections)
|
||||
{
|
||||
if (slaveConnection != null && slaveConnection.State == ConnectionState.Open)
|
||||
{
|
||||
slaveConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public virtual void Dispose()
|
||||
{
|
||||
@ -89,6 +102,17 @@ namespace SqlSugar
|
||||
this.Connection.Dispose();
|
||||
}
|
||||
this.Connection = null;
|
||||
|
||||
if (this.IsMasterSlaveSeparation)
|
||||
{
|
||||
foreach (var slaveConnection in this.SlaveConnections)
|
||||
{
|
||||
if (slaveConnection != null && slaveConnection.State == ConnectionState.Open)
|
||||
{
|
||||
slaveConnection.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public virtual void CheckConnection()
|
||||
{
|
||||
@ -230,6 +254,7 @@ namespace SqlSugar
|
||||
{
|
||||
try
|
||||
{
|
||||
SetConnectionStart(sql);
|
||||
if (this.ProcessingEventStartingSQL != null)
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
ExecuteBefore(sql, parameters);
|
||||
@ -238,6 +263,7 @@ namespace SqlSugar
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
SetConnectionEnd();
|
||||
return count;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -255,6 +281,7 @@ namespace SqlSugar
|
||||
{
|
||||
try
|
||||
{
|
||||
SetConnectionStart(sql);
|
||||
var isSp = this.CommandType == CommandType.StoredProcedure;
|
||||
if (this.ProcessingEventStartingSQL != null)
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
@ -266,6 +293,7 @@ namespace SqlSugar
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
SetConnectionEnd();
|
||||
return sqlDataReader;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -279,6 +307,7 @@ namespace SqlSugar
|
||||
{
|
||||
try
|
||||
{
|
||||
SetConnectionStart(sql);
|
||||
if (this.ProcessingEventStartingSQL != null)
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
ExecuteBefore(sql, parameters);
|
||||
@ -290,6 +319,7 @@ namespace SqlSugar
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
SetConnectionEnd();
|
||||
return ds;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -307,6 +337,7 @@ namespace SqlSugar
|
||||
{
|
||||
try
|
||||
{
|
||||
SetConnectionStart(sql);
|
||||
if (this.ProcessingEventStartingSQL != null)
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
ExecuteBefore(sql, parameters);
|
||||
@ -316,6 +347,7 @@ namespace SqlSugar
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
SetConnectionEnd();
|
||||
return scalar;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -650,6 +682,58 @@ namespace SqlSugar
|
||||
{
|
||||
return this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null;
|
||||
}
|
||||
private bool IsMasterSlaveSeparation
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Context.CurrentConnectionConfig.SlaveConnectionConfigs.HasValue();
|
||||
}
|
||||
}
|
||||
private void SetConnectionStart(string sql)
|
||||
{
|
||||
if (this.Transaction==null&&this.IsMasterSlaveSeparation && IsRead(sql))
|
||||
{
|
||||
if (this.MasterConnection == null)
|
||||
{
|
||||
this.MasterConnection = this.Connection;
|
||||
}
|
||||
var saves = this.Context.CurrentConnectionConfig.SlaveConnectionConfigs.Where(it => it.HitRate > 0).ToList();
|
||||
var currentIndex = UtilRandom.GetRandomIndex(saves.ToDictionary(it => saves.ToList().IndexOf(it), it => it.HitRate));
|
||||
var currentSaveConnection = saves[currentIndex];
|
||||
this.Connection = null;
|
||||
this.Context.CurrentConnectionConfig.ConnectionString = currentSaveConnection.ConnectionString;
|
||||
this.Connection = this.Connection;
|
||||
if (this.SlaveConnections.IsNullOrEmpty() || !this.SlaveConnections.Any(it => EqualsConnectionString(it.ConnectionString, this.Connection.ConnectionString)))
|
||||
{
|
||||
if (this.SlaveConnections == null) this.SlaveConnections = new List<IDbConnection>();
|
||||
this.SlaveConnections.Add(this.Connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool EqualsConnectionString(string connectionString1, string connectionString2)
|
||||
{
|
||||
var connectionString1Array = connectionString1.Split(';');
|
||||
var connectionString2Array = connectionString2.Split(';');
|
||||
var result = connectionString1Array.Except(connectionString2Array);
|
||||
return result.Count() == 0;
|
||||
}
|
||||
|
||||
private void SetConnectionEnd()
|
||||
{
|
||||
if (this.IsMasterSlaveSeparation)
|
||||
{
|
||||
this.Connection = this.MasterConnection;
|
||||
this.Context.CurrentConnectionConfig.ConnectionString = this.MasterConnection.ConnectionString;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsRead(string sql)
|
||||
{
|
||||
var sqlLower = sql.ToLower();
|
||||
var result = Regex.IsMatch(sqlLower, "[ ]*select[ ]") && !Regex.IsMatch(sqlLower, "[ ]*insert[ ]|[ ]*update[ ]|[ ]*delete[ ]");
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ namespace SqlSugar
|
||||
protected List<T> GetEntityList<T>(SqlSugarClient context, IDataReader dataReader, string fields)
|
||||
{
|
||||
Type type = typeof(T);
|
||||
string key = "DataReaderToList." + fields + context.CurrentConnectionConfig.DbType + type.FullName;
|
||||
string key = "DataReaderToList." + fields+ dataReader.FieldCount+ context.CurrentConnectionConfig.DbType + type.FullName;
|
||||
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(key, () =>
|
||||
{
|
||||
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader).CreateBuilder(type);
|
||||
|
@ -278,7 +278,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
var mappInfo = this.Context.MappingColumns.FirstOrDefault(it =>it.EntityName==EntityInfo.EntityName&&it.PropertyName.Equals(columns, StringComparison.CurrentCultureIgnoreCase));
|
||||
var mappInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName == EntityInfo.EntityName && it.PropertyName.Equals(columns, StringComparison.CurrentCultureIgnoreCase));
|
||||
return mappInfo == null ? columns : mappInfo.DbColumnName;
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,11 @@ namespace SqlSugar
|
||||
/// </summary>
|
||||
public ConfigureExternalServices ConfigureExternalServices = _DefaultServices;
|
||||
private static ConfigureExternalServices _DefaultServices = new ConfigureExternalServices();
|
||||
/// <summary>
|
||||
/// If SlaveConnectionStrings has value,ConnectionString is write operation, SlaveConnectionStrings is read operation.
|
||||
/// All operations within a transaction is ConnectionString
|
||||
/// </summary>
|
||||
public List<SlaveConnectionConfig> SlaveConnectionConfigs { get; set; }
|
||||
}
|
||||
|
||||
public class ConfigureExternalServices
|
||||
|
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SlaveConnectionConfig
|
||||
{
|
||||
/// <summary>
|
||||
///Default value is 1
|
||||
///If value is 0 means permanent non execution
|
||||
/// </summary>
|
||||
public int HitRate = 1;
|
||||
public string ConnectionString { get; set; }
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ namespace SqlSugar
|
||||
var isDateDate = memberName == "Date" && expression.Expression.Type == UtilConstants.DateType;
|
||||
var isMemberValue = expression.Expression != null && expression.Expression.NodeType != ExpressionType.Parameter && !isValueBool;
|
||||
var isSingle = parameter.Context.ResolveType == ResolveExpressType.WhereSingle;
|
||||
var fieldIsBool = isBool && isLogicOperator;
|
||||
var fieldIsBool = isBool && isLogicOperator&&(parameter.BaseParameter==null||!(parameter.BaseParameter.CurrentExpression is MemberInitExpression|| parameter.BaseParameter.CurrentExpression is NewExpression));
|
||||
baseParameter.ChildExpression = expression;
|
||||
if (isLength)
|
||||
{
|
||||
|
@ -74,19 +74,19 @@ namespace SqlSugar
|
||||
InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8>();
|
||||
InitMppingInfo<T9>();
|
||||
}
|
||||
protected void InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8,T9,T10>()
|
||||
protected void InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>()
|
||||
{
|
||||
InitMppingInfo<T, T2, T3, T4, T5, T6, T7,T8,T9>();
|
||||
InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9>();
|
||||
InitMppingInfo<T10>();
|
||||
}
|
||||
protected void InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10,T11>()
|
||||
protected void InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>()
|
||||
{
|
||||
InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9,T10>();
|
||||
InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>();
|
||||
InitMppingInfo<T11>();
|
||||
}
|
||||
protected void InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,T12>()
|
||||
protected void InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>()
|
||||
{
|
||||
InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10,T11>();
|
||||
InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>();
|
||||
InitMppingInfo<T12>();
|
||||
}
|
||||
#endregion
|
||||
@ -206,10 +206,11 @@ namespace SqlSugar
|
||||
{
|
||||
this.CreateQueryable<T>(queryable);
|
||||
string shortName = string.Empty;
|
||||
List<SugarParameter> paramters =new List<SugarParameter>();
|
||||
queryable.SqlBuilder.QueryBuilder.JoinQueryInfos = this.GetJoinInfos(queryable.SqlBuilder,joinExpression, ref paramters, ref shortName, types);
|
||||
List<SugarParameter> paramters = new List<SugarParameter>();
|
||||
queryable.SqlBuilder.QueryBuilder.JoinQueryInfos = this.GetJoinInfos(queryable.SqlBuilder, joinExpression, ref paramters, ref shortName, types);
|
||||
queryable.SqlBuilder.QueryBuilder.TableShortName = shortName;
|
||||
if (paramters != null) {
|
||||
if (paramters != null)
|
||||
{
|
||||
queryable.SqlBuilder.QueryBuilder.Parameters.AddRange(paramters);
|
||||
}
|
||||
}
|
||||
@ -217,13 +218,13 @@ namespace SqlSugar
|
||||
{
|
||||
this.CreateQueryable<T>(queryable);
|
||||
string shortName = string.Empty;
|
||||
queryable.SqlBuilder.QueryBuilder.EasyJoinInfos = this.GetEasyJoinInfo(joinExpression, ref shortName,queryable.SqlBuilder,types);
|
||||
queryable.SqlBuilder.QueryBuilder.EasyJoinInfos = this.GetEasyJoinInfo(joinExpression, ref shortName, queryable.SqlBuilder, types);
|
||||
queryable.SqlBuilder.QueryBuilder.TableShortName = shortName;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Private methods
|
||||
protected List<JoinQueryInfo> GetJoinInfos(ISqlBuilder sqlBuilder,Expression joinExpression,ref List<SugarParameter> parameters, ref string shortName, params Type[] entityTypeArray)
|
||||
protected List<JoinQueryInfo> GetJoinInfos(ISqlBuilder sqlBuilder, Expression joinExpression, ref List<SugarParameter> parameters, ref string shortName, params Type[] entityTypeArray)
|
||||
{
|
||||
List<JoinQueryInfo> result = new List<JoinQueryInfo>();
|
||||
var lambdaParameters = ((LambdaExpression)joinExpression).Parameters.ToList();
|
||||
@ -232,7 +233,7 @@ namespace SqlSugar
|
||||
expressionContext.MappingTables = this.Context.MappingTables;
|
||||
expressionContext.Resolve(joinExpression, ResolveExpressType.Join);
|
||||
int i = 0;
|
||||
var joinArray = expressionContext.Result.GetResultArray();
|
||||
var joinArray = MergeJoinArray(expressionContext.Result.GetResultArray());
|
||||
parameters = expressionContext.Parameters;
|
||||
foreach (var entityType in entityTypeArray)
|
||||
{
|
||||
@ -265,7 +266,38 @@ namespace SqlSugar
|
||||
expressionContext.Clear();
|
||||
return result;
|
||||
}
|
||||
protected Dictionary<string,string> GetEasyJoinInfo(Expression joinExpression, ref string shortName, ISqlBuilder builder, params Type[] entityTypeArray)
|
||||
|
||||
private string[] MergeJoinArray(string[] joinArray)
|
||||
{
|
||||
List<string> result = new List<string>();
|
||||
string joinValue = null;
|
||||
int i = 0;
|
||||
foreach (var item in joinArray)
|
||||
{
|
||||
++i;
|
||||
var isLast = joinArray.Length == i;
|
||||
var isJoinType = item.IsIn(JoinType.Inner.ToString(), JoinType.Left.ToString(), JoinType.Right.ToString());
|
||||
if (isJoinType)
|
||||
{
|
||||
if (joinValue != null)
|
||||
result.Add(joinValue);
|
||||
joinValue = null;
|
||||
result.Add(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
isJoinType = false;
|
||||
joinValue += joinValue==null?item:(","+item);
|
||||
}
|
||||
if (isLast)
|
||||
{
|
||||
result.Add(joinValue);
|
||||
}
|
||||
}
|
||||
return result.ToArray(); ;
|
||||
}
|
||||
|
||||
protected Dictionary<string, string> GetEasyJoinInfo(Expression joinExpression, ref string shortName, ISqlBuilder builder, params Type[] entityTypeArray)
|
||||
{
|
||||
Dictionary<string, string> result = new Dictionary<string, string>();
|
||||
var lambdaParameters = ((LambdaExpression)joinExpression).Parameters.ToList();
|
||||
@ -273,7 +305,7 @@ namespace SqlSugar
|
||||
var index = 1;
|
||||
foreach (var item in entityTypeArray)
|
||||
{
|
||||
result.Add(UtilConstants.Space +lambdaParameters[index].Name, item.Name);
|
||||
result.Add(UtilConstants.Space + lambdaParameters[index].Name, item.Name);
|
||||
++index;
|
||||
}
|
||||
return result;
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>4.5.9.2</Version>
|
||||
<Version>4.5.9.4</Version>
|
||||
<Copyright>sun_kai_xuan</Copyright>
|
||||
<PackageProjectUrl>https://github.com/sunkaixuan/SqlSugar</PackageProjectUrl>
|
||||
<PackageLicenseUrl></PackageLicenseUrl>
|
||||
|
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class UtilRandom
|
||||
{
|
||||
public static Random Random = new Random();
|
||||
public static int GetRandomIndex(Dictionary<int, int> pars)
|
||||
{
|
||||
int maxValue = 0;
|
||||
foreach (var item in pars)
|
||||
{
|
||||
maxValue += item.Value;
|
||||
}
|
||||
var num = Random.Next(1, maxValue);
|
||||
var result = 0;
|
||||
var endValue = 0;
|
||||
foreach (var item in pars)
|
||||
{
|
||||
var index = pars.ToList().IndexOf(item);
|
||||
var beginValue = index == 0 ? 0 : pars[index - 1];
|
||||
endValue += item.Value;
|
||||
result = item.Key;
|
||||
if (num >= beginValue && num <= endValue)
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user