diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UThread.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UThread.cs index 5972a5360..2c6fb3ae6 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UThread.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UThread.cs @@ -25,13 +25,13 @@ namespace OrmTest } } }); - public static SqlSugarClient ssDb => new SqlSugarClient(new ConnectionConfig() + public static SqlSugarScope ssDb => new SqlSugarScope(new ConnectionConfig() { DbType = DbType.SqlServer, ConnectionString = Config.ConnectionString, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, - IsShardSameThread = true, + //IsShardSameThread = true, AopEvents = new AopEvents { OnLogExecuting = (sql, p) => diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 3290eca6b..52a973b95 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -1274,25 +1274,25 @@ namespace SqlSugar } public virtual void ExecuteBefore(string sql, SugarParameter[] parameters) { - if (this.Context.CurrentConnectionConfig.Debugger != null && this.Context.CurrentConnectionConfig.Debugger.EnableThreadSecurityValidation == true) - { + //if (this.Context.CurrentConnectionConfig.Debugger != null && this.Context.CurrentConnectionConfig.Debugger.EnableThreadSecurityValidation == true) + //{ - var contextId = this.Context.ContextID.ToString(); - var processId = Thread.CurrentThread.ManagedThreadId.ToString(); - var cache = new ReflectionInoCacheService(); - if (!cache.ContainsKey(contextId)) - { - cache.Add(contextId, processId); - } - else - { - var cacheValue = cache.Get(contextId); - if (processId != cacheValue) - { - throw new SqlSugarException(this.Context, ErrorMessage.GetThrowMessage("Detection of SqlSugarClient cross-threading usage,a thread needs a new one", "检测到声名的SqlSugarClient跨线程使用,请检查是否静态、是否单例、或者IOC配置错误引起的,保证一个线程new出一个对象 ,具本Sql:") + sql, parameters); - } - } - } + // var contextId = this.Context.ContextID.ToString(); + // var processId = Thread.CurrentThread.ManagedThreadId.ToString(); + // var cache = new ReflectionInoCacheService(); + // if (!cache.ContainsKey(contextId)) + // { + // cache.Add(contextId, processId); + // } + // else + // { + // var cacheValue = cache.Get(contextId); + // if (processId != cacheValue) + // { + // throw new SqlSugarException(this.Context, ErrorMessage.GetThrowMessage("Detection of SqlSugarClient cross-threading usage,a thread needs a new one", "检测到声名的SqlSugarClient跨线程使用,请检查是否静态、是否单例、或者IOC配置错误引起的,保证一个线程new出一个对象 ,具本Sql:") + sql, parameters); + // } + // } + //} this.BeforeTime = DateTime.Now; if (this.IsEnableLogEvent) { diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index 52ed59297..3696cea80 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -24,6 +24,13 @@ namespace SqlSugar #endregion #region Public methods + public SplitCodeFirstProvider SplitTables() + { + var result = new SplitCodeFirstProvider(); + result.Context = this.Context; + return result; + } + public virtual ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue) { this.IsBackupTable = true; @@ -40,8 +47,8 @@ namespace SqlSugar public virtual void InitTables(Type entityType) { - this.Context.Utilities.RemoveCacheAll(); - this.Context.InitMappingInfo(entityType); + //this.Context.Utilities.RemoveCacheAll(); + this.Context.InitMappingInfoNoCache(entityType); if (!this.Context.DbMaintenance.IsAnySystemTablePermissions()) { Check.Exception(true, "Dbfirst and Codefirst requires system table permissions"); @@ -107,7 +114,7 @@ namespace SqlSugar #region Core Logic protected virtual void Execute(Type entityType) { - var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(entityType); + var entityInfo = this.Context.EntityMaintenance.GetEntityInfoNoCache(entityType); if (this.DefultLength > 0) { foreach (var item in entityInfo.Columns) @@ -132,7 +139,7 @@ namespace SqlSugar this.Context.MappingTables.Add(entityInfo.EntityName,tableName); entityInfo.DbTableName = tableName; entityInfo.Columns.ForEach(it => { it.DbTableName = tableName; }); - var isAny = this.Context.DbMaintenance.IsAnyTable(tableName); + var isAny = this.Context.DbMaintenance.IsAnyTable(tableName,false); if (isAny&&entityInfo.IsDisabledUpdateAll) { return; @@ -168,7 +175,7 @@ namespace SqlSugar //Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Multiple primary keys do not support modifications"); var tableName = GetTableName(entityInfo); - var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName); + var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName,false); ConvertColumns(dbColumns); var entityColumns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList(); var dropColumns = dbColumns diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs new file mode 100644 index 000000000..181d5ba53 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SplitCodeFirstProvider + { + public SqlSugarProvider Context; + public void InitTables() + { + var type = typeof(T); + InitTables(type); + } + + public void InitTables(Type type) + { + //var oldMapping = this.Context.Utilities.TranslateCopy(this.Context.MappingTables); + SplitTableContext helper = new SplitTableContext(Context) + { + EntityInfo = this.Context.EntityMaintenance.GetEntityInfo(type) + }; + helper.CheckPrimaryKey(); + var tables = helper.GetTables(); + //var oldMapingTables = this.Context.MappingTables; + if (tables.Count > 0) + { + foreach (var item in tables) + { + this.Context.MappingTables.Add(helper.EntityInfo.EntityName, item.TableName); + this.Context.CodeFirst.InitTables(type); + } + } + else + { + this.Context.MappingTables.Add(helper.EntityInfo.EntityName, helper.GetDefaultTableName()); + this.Context.CodeFirst.InitTables(type); + } + this.Context.MappingTables.Add(helper.EntityInfo.EntityName, helper.EntityInfo.DbTableName); + } + + public void InitTables(Type [] types) + { + foreach (var type in types) + { + InitTables(type); + } + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs index b2d8ef8e9..c8bf71b2b 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs @@ -258,6 +258,20 @@ namespace SqlSugar this.Where(Regex.Split(sqlable.Key," Where ",RegexOptions.IgnoreCase).Last(), sqlable.Value); return this; } + public SplitTableDeleteProvider SplitTable(Func, IEnumerable> getTableNamesFunc) + { + this.Context.MappingTables.Add(this.EntityInfo.EntityName, this.EntityInfo.DbTableName); + SplitTableDeleteProvider result = new SplitTableDeleteProvider(); + result.Context = this.Context; + SplitTableContext helper = new SplitTableContext((SqlSugarProvider)Context) + { + EntityInfo = this.EntityInfo + }; + var tables = getTableNamesFunc(helper.GetTables()); + result.Tables = tables; + result.deleteobj = this; + return result; + } public IDeleteable RemoveDataCache(string likeString) { this.RemoveCacheFunc = () => diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/SplitTableDeleteProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/SplitTableDeleteProvider.cs new file mode 100644 index 000000000..83cf69d1c --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/SplitTableDeleteProvider.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SplitTableDeleteProvider where T : class, new() + { + public ISqlSugarClient Context; + public DeleteableProvider deleteobj; + + public IEnumerable Tables { get; set; } + public int ExecuteCommand() + { + if (this.Context.Ado.Transaction == null) + { + try + { + this.Context.Ado.BeginTran(); + var result = _ExecuteCommand(); + this.Context.Ado.CommitTran(); + return result; + } + catch (Exception ex) + { + this.Context.Ado.RollbackTran(); + throw ex; + } + } + else + { + return _ExecuteCommand(); + } + } + public async Task ExecuteCommandAsync() + { + if (this.Context.Ado.Transaction == null) + { + try + { + this.Context.Ado.BeginTran(); + var result = await _ExecuteCommandAsync(); + this.Context.Ado.BeginTran(); + return result; + } + catch (Exception ex) + { + this.Context.Ado.RollbackTran(); + throw ex; + } + } + else + { + return await _ExecuteCommandAsync(); + } + } + internal int _ExecuteCommand() + { + var result = 0; + var sqlobj = deleteobj.ToSql(); + + foreach (var item in Tables) + { + var newsqlobj = GetSqlObj(sqlobj, item.TableName); + result +=this.Context.Ado.ExecuteCommand(newsqlobj.Key, newsqlobj.Value); + } + return result; + } + + internal async Task _ExecuteCommandAsync() + { + var result = 0; + var sqlobj = deleteobj.ToSql(); + foreach (var item in Tables) + { + var newsqlobj = GetSqlObj(sqlobj, item.TableName); + result +=await this.Context.Ado.ExecuteCommandAsync(newsqlobj.Key, newsqlobj.Value); + } + return result; + } + + private KeyValuePair> GetSqlObj(KeyValuePair> keyValuePair,string asName) + { + List pars = new List(); + string sql = keyValuePair.Key; + if (keyValuePair.Value != null) + { + pars = keyValuePair.Value.Select(it => new SugarParameter(it.ParameterName, it.Value)).ToList(); + } + sql = Regex.Replace(sql, deleteobj.EntityInfo.DbTableName, asName,RegexOptions.IgnoreCase); + return new KeyValuePair>(sql,pars); + } + + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs index 538f39709..934fc2e07 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs @@ -21,49 +21,45 @@ namespace SqlSugar return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () => { - EntityInfo result = new EntityInfo(); - var sugarAttributeInfo = type.GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).Where(it => it is SugarTable).SingleOrDefault(); - if (sugarAttributeInfo.HasValue()) - { - var sugarTable = (SugarTable)sugarAttributeInfo; - result.DbTableName = sugarTable.TableName; - result.TableDescription = sugarTable.TableDescription; - result.IsDisabledUpdateAll = sugarTable.IsDisabledUpdateAll; - result.IsDisabledDelete = sugarTable.IsDisabledDelete; - } - if (this.Context.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService != null) { - if (result.DbTableName == null) - { - result.DbTableName = type.Name; - } - this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService(type,result); - } - result.Type = type; - result.EntityName = result.Type.Name; - result.Columns = new List(); - SetColumns(result); - return result; + return GetEntityInfoNoCache(type); }); } + + public EntityInfo GetEntityInfoNoCache(Type type) + { + EntityInfo result = new EntityInfo(); + var sugarAttributeInfo = type.GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).Where(it => it is SugarTable).SingleOrDefault(); + if (sugarAttributeInfo.HasValue()) + { + var sugarTable = (SugarTable)sugarAttributeInfo; + result.DbTableName = sugarTable.TableName; + result.TableDescription = sugarTable.TableDescription; + result.IsDisabledUpdateAll = sugarTable.IsDisabledUpdateAll; + result.IsDisabledDelete = sugarTable.IsDisabledDelete; + } + if (this.Context.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService != null) + { + if (result.DbTableName == null) + { + result.DbTableName = type.Name; + } + this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService(type, result); + } + result.Type = type; + result.EntityName = result.Type.Name; + result.Columns = new List(); + SetColumns(result); + return result; + } + public string GetTableName() { - var typeName = typeof(T).Name; - if (this.Context.MappingTables == null || this.Context.MappingTables.Count == 0) - { - var entity = this.GetEntityInfo(); - if (entity.DbTableName.HasValue()) return entity.DbTableName; - else return entity.EntityName; - } - else - { - var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == typeName); - return mappingInfo == null ? typeName : mappingInfo.DbTableName; - } + return GetTableName(typeof(T)); } public string GetTableName(Type entityType) { var typeName = entityType.Name; - if (this.Context.MappingTables == null || this.Context.MappingTables.Count == 0) + if (this.Context.MappingTables == null || this.Context.MappingTables.Count == 0 || !this.Context.MappingTables.Any(it => it.EntityName == typeName)) { var entity = this.GetEntityInfo(entityType); if (entity.DbTableName.HasValue()) return entity.DbTableName; @@ -96,27 +92,14 @@ namespace SqlSugar } public string GetDbColumnName(string propertyName) { - var isAny = this.GetEntityInfo().Columns.Any(it => it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase)); - Check.Exception(!isAny, "Property " + propertyName + " is Invalid"); - var typeName = typeof(T).Name; - if (this.Context.MappingColumns == null || this.Context.MappingColumns.Count == 0) - { - var column= this.GetEntityInfo().Columns.First(it => it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase)); - if (column.DbColumnName.HasValue()) return column.DbColumnName; - else return column.PropertyName; - } - else - { - var mappingInfo = this.Context.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.PropertyName == propertyName); - return mappingInfo == null ? propertyName : mappingInfo.DbColumnName; - } + return GetDbColumnName(propertyName,typeof(T)); } public string GetDbColumnName(string propertyName,Type entityType) { var isAny = this.GetEntityInfo(entityType).Columns.Any(it => it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase)); Check.Exception(!isAny, "Property " + propertyName + " is Invalid"); var typeName = entityType.Name; - if (this.Context.MappingColumns == null || this.Context.MappingColumns.Count == 0) + if (this.Context.MappingColumns == null || this.Context.MappingColumns.Count == 0 || !this.Context.MappingColumns.Any(it => it.EntityName == typeName && it.PropertyName == propertyName)) { var column = this.GetEntityInfo(entityType).Columns.First(it => it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase)); if (column.DbColumnName.HasValue()) return column.DbColumnName; diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FilterProvider/FilterProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FilterProvider/FilterProvider.cs index ff28e05f4..3151cff8d 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FilterProvider/FilterProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FilterProvider/FilterProvider.cs @@ -14,15 +14,15 @@ namespace SqlSugar { if (_Filters == null) _Filters = new List(); - if (this.Context.CurrentConnectionConfig.IsShardSameThread) - { - if (!_Filters.Select(it => it.FilterValue(this.Context).Sql).Contains(filter.FilterValue(this.Context).Sql)) - _Filters.Add(filter); - } - else - { + //if (this.Context.CurrentConnectionConfig.IsShardSameThread) + //{ + // if (!_Filters.Select(it => it.FilterValue(this.Context).Sql).Contains(filter.FilterValue(this.Context).Sql)) + // _Filters.Add(filter); + //} + //else + //{ _Filters.Add(filter); - } + //} return this; } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index e07c4733e..b7c2dbccd 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Data; using System.Linq; using System.Linq.Expressions; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -22,7 +23,7 @@ namespace SqlSugar public EntityInfo EntityInfo { get; set; } public List MappingColumnList { get; set; } private List IgnoreColumnNameList { get; set; } - private bool IsOffIdentity { get; set; } + internal bool IsOffIdentity { get; set; } public T[] InsertObjs { get; set; } public MappingTableList OldMappingTableList { get; set; } @@ -406,6 +407,42 @@ namespace SqlSugar result.AddSubList(tree); return result; } + public SplitInsertable SplitTable(SplitType splitType) + { + SplitTableContext helper = new SplitTableContext(Context) + { + EntityInfo = this.EntityInfo + }; + helper.CheckPrimaryKey(); + SplitInsertable result = new SplitInsertable(); + result.Context = this.Context; + result.EntityInfo = this.EntityInfo; + result.Helper = helper; + result.SplitType = splitType; + result.TableNames = new List>(); + foreach (var item in this.InsertObjs) + { + var splitFieldValue = helper.GetValue(splitType, item); + var tableName=helper.GetTableName(splitType, splitFieldValue); + result.TableNames.Add(new KeyValuePair(tableName,item)); + } + result.Inserable = this; + return result; + } + + public SplitInsertable SplitTable() + { + var splitTableAttribute = typeof(T).GetCustomAttribute(); + if (splitTableAttribute != null) + { + return SplitTable((splitTableAttribute as SplitTableAttribute).SplitType); + } + else + { + Check.Exception(true,$" {typeof(T).Name} need SplitTableAttribute"); + return null; + } + } #endregion diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/SplitInsertable.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/SplitInsertable.cs new file mode 100644 index 000000000..8e345031a --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/SplitInsertable.cs @@ -0,0 +1,226 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SplitInsertable where T:class ,new() + { + public SqlSugarProvider Context; + internal SplitTableContext Helper; + public EntityInfo EntityInfo; + public SplitType SplitType; + internal IInsertable Inserable { get; set; } + internal List> TableNames { get; set; } + + public int ExecuteCommand() + { + if (this.Context.Ado.Transaction == null) + { + try + { + this.Context.Ado.BeginTran(); + var result = _ExecuteCommand(); + this.Context.Ado.CommitTran(); + return result; + } + catch (Exception ex) + { + this.Context.Ado.RollbackTran(); + throw ex; + } + } + else + { + return _ExecuteCommand(); + } + } + public async Task ExecuteCommandAsync() + { + if (this.Context.Ado.Transaction == null) + { + try + { + this.Context.Ado.BeginTran(); + var result = await _ExecuteCommandAsync(); + this.Context.Ado.BeginTran(); + return result; + } + catch (Exception ex) + { + this.Context.Ado.RollbackTran(); + throw ex; + } + } + else + { + return await _ExecuteCommandAsync(); + } + } + + + public List ExecuteReturnSnowflakeIdList() + { + if (this.Context.Ado.Transaction == null) + { + try + { + this.Context.Ado.BeginTran(); + var result = _ExecuteReturnSnowflakeIdList(); + this.Context.Ado.CommitTran(); + return result; + } + catch (Exception ex) + { + this.Context.Ado.RollbackTran(); + throw ex; + } + } + else + { + return _ExecuteReturnSnowflakeIdList(); + } + } + public async Task> ExecuteReturnSnowflakeIdListAsync() + { + if (this.Context.Ado.Transaction == null) + { + try + { + this.Context.Ado.BeginTran(); + var result = await _ExecuteReturnSnowflakeIdListAsync(); + this.Context.Ado.BeginTran(); + return result; + } + catch (Exception ex) + { + this.Context.Ado.RollbackTran(); + throw ex; + } + } + else + { + return await _ExecuteReturnSnowflakeIdListAsync(); + } + } + + + public long ExecuteReturnSnowflakeId() + { + return ExecuteReturnSnowflakeIdList().FirstOrDefault(); + } + public async Task ExecuteReturnSnowflakeIdAsync() + { + var list = await ExecuteReturnSnowflakeIdListAsync(); + return list.FirstOrDefault(); + } + + + internal int _ExecuteCommand() + { + CreateTable(); + var result = 0; + var groups = TableNames.GroupBy(it => it.Key).ToList(); + var parent = ((InsertableProvider)Inserable); + var names = parent.InsertBuilder.DbColumnInfoList.GroupBy(it => it.DbColumnName).Select(i => i.Key).ToList(); + foreach (var item in groups) + { + var list = item.Select(it => it.Value as T).ToList(); + var groupInserable = (InsertableProvider)this.Context.Insertable(list); + groupInserable.InsertBuilder.TableWithString = parent.InsertBuilder.TableWithString; + groupInserable.RemoveCacheFunc = parent.RemoveCacheFunc; + groupInserable.diffModel = parent.diffModel; + groupInserable.IsEnableDiffLogEvent = parent.IsEnableDiffLogEvent; + groupInserable.InsertBuilder.IsNoInsertNull = parent.InsertBuilder.IsNoInsertNull; + groupInserable.IsOffIdentity = parent.IsOffIdentity; + result += groupInserable.AS(item.Key).InsertColumns(names.ToArray()).ExecuteCommand(); + } + return result; + } + internal async Task _ExecuteCommandAsync() + { + CreateTable(); + var result = 0; + var groups = TableNames.GroupBy(it => it.Key).ToList(); + var parent = ((InsertableProvider)Inserable); + var names = parent.InsertBuilder.DbColumnInfoList.GroupBy(it => it.DbColumnName).Select(i => i.Key).ToList(); + foreach (var item in groups) + { + var list = item.Select(it => it.Value as T).ToList(); + var groupInserable = (InsertableProvider)this.Context.Insertable(list); + groupInserable.InsertBuilder.TableWithString = parent.InsertBuilder.TableWithString; + groupInserable.RemoveCacheFunc = parent.RemoveCacheFunc; + groupInserable.diffModel = parent.diffModel; + groupInserable.IsEnableDiffLogEvent = parent.IsEnableDiffLogEvent; + groupInserable.InsertBuilder.IsNoInsertNull = parent.InsertBuilder.IsNoInsertNull; + groupInserable.IsOffIdentity = parent.IsOffIdentity; + result +=await groupInserable.AS(item.Key).InsertColumns(names.ToArray()).ExecuteCommandAsync(); + } + return result; + } + + internal List _ExecuteReturnSnowflakeIdList() + { + CreateTable(); + var result = new List(); + var groups = TableNames.GroupBy(it => it.Key).ToList(); + var parent = ((InsertableProvider)Inserable); + var names = parent.InsertBuilder.DbColumnInfoList.GroupBy(it => it.DbColumnName).Select(i => i.Key).ToList(); + foreach (var item in groups) + { + var list = item.Select(it => it.Value as T).ToList(); + var groupInserable = (InsertableProvider)this.Context.Insertable(list); + groupInserable.InsertBuilder.TableWithString = parent.InsertBuilder.TableWithString; + groupInserable.RemoveCacheFunc = parent.RemoveCacheFunc; + groupInserable.diffModel = parent.diffModel; + groupInserable.IsEnableDiffLogEvent = parent.IsEnableDiffLogEvent; + groupInserable.InsertBuilder.IsNoInsertNull = parent.InsertBuilder.IsNoInsertNull; + groupInserable.IsOffIdentity = parent.IsOffIdentity; + var idList= groupInserable.AS(item.Key).InsertColumns(names.ToArray()).ExecuteReturnSnowflakeIdList(); + result.AddRange(idList); + } + return result; + } + internal async Task> _ExecuteReturnSnowflakeIdListAsync() + { + CreateTable(); + var result = new List(); + var groups = TableNames.GroupBy(it => it.Key).ToList(); + var parent = ((InsertableProvider)Inserable); + var names = parent.InsertBuilder.DbColumnInfoList.GroupBy(it => it.DbColumnName).Select(i => i.Key).ToList(); + foreach (var item in groups) + { + var list = item.Select(it => it.Value as T).ToList(); + var groupInserable = (InsertableProvider)this.Context.Insertable(list); + groupInserable.InsertBuilder.TableWithString = parent.InsertBuilder.TableWithString; + groupInserable.RemoveCacheFunc = parent.RemoveCacheFunc; + groupInserable.diffModel = parent.diffModel; + groupInserable.IsEnableDiffLogEvent = parent.IsEnableDiffLogEvent; + groupInserable.InsertBuilder.IsNoInsertNull = parent.InsertBuilder.IsNoInsertNull; + groupInserable.IsOffIdentity = parent.IsOffIdentity; + var idList =await groupInserable.AS(item.Key).InsertColumns(names.ToArray()).ExecuteReturnSnowflakeIdListAsync(); + result.AddRange(idList); + } + return result; + } + + + private void CreateTable() + { + var isLog = this.Context.Ado.IsEnableLogEvent; + this.Context.Ado.IsEnableLogEvent = false; + foreach (var item in TableNames) + { + if (!this.Context.DbMaintenance.IsAnyTable(item.Key, false)) + { + this.Context.MappingTables.Add(EntityInfo.EntityName, item.Key); + this.Context.CodeFirst.InitTables(); + } + } + this.Context.Ado.IsEnableLogEvent = isLog; + this.Context.MappingTables.Add(EntityInfo.EntityName, EntityInfo.DbTableName); + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 9955e59a6..91edc7c4e 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -46,6 +46,57 @@ namespace SqlSugar return this.Context.EntityMaintenance.GetEntityInfo(); } } + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -790,10 +841,14 @@ namespace SqlSugar var selectValue = new SugarMapper(this.Context).GetSelectValue(this.QueryBuilder); return this.Select(selectValue); } + else if (this.QueryBuilder.EntityType == UtilConstants.ObjType || (this.QueryBuilder.AsTables != null && this.QueryBuilder.AsTables.Count == 1)||this.QueryBuilder.EntityName!=this.QueryBuilder.EntityType.Name) + { + return this.Select(this.SqlBuilder.SqlSelectAll); + } else { - - return this.Select(this.SqlBuilder.SqlSelectAll); + var selects = this.QueryBuilder.GetSelectValueByString(); + return this.Select(selects); } } @@ -819,9 +874,9 @@ namespace SqlSugar } public virtual ISugarQueryable MergeTable() { - 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 "); + Check.Exception(this.MapperAction != null || this.MapperActionWithCache != null,ErrorMessage.GetThrowMessage( "'Mapper’ needs to be written after ‘MergeTable’ ", "Mapper 只能在 MergeTable 之后使用")); + Check.Exception(this.QueryBuilder.SelectValue.IsNullOrEmpty(),ErrorMessage.GetThrowMessage( "MergeTable need to use Queryable.Select Method .", "使用MergeTable之前必须要有Queryable.Select方法")); + Check.Exception(this.QueryBuilder.Skip > 0 || this.QueryBuilder.Take > 0 || this.QueryBuilder.OrderByValue.HasValue(),ErrorMessage.GetThrowMessage( "MergeTable Queryable cannot Take Skip OrderBy PageToList ", "使用 MergeTable不能有 Take Skip OrderBy PageToList 等操作,你可以在Mergetable之后操作")); var sqlobj = this._ToSql(); var index = QueryBuilder.WhereIndex + 1; var result = this.Context.Queryable().AS(SqlBuilder.GetPackTable(sqlobj.Key, "MergeTable")).AddParameters(sqlobj.Value).Select("*").With(SqlWith.Null); @@ -834,7 +889,25 @@ namespace SqlSugar } return result; } - + public ISugarQueryable SplitTable(Func, IEnumerable> getTableNamesFunc) + { + SplitTableContext helper = new SplitTableContext(Context) + { + EntityInfo = this.EntityInfo + }; + this.Context.MappingTables.Add(this.EntityInfo.EntityName, this.EntityInfo.DbTableName); + var tables = getTableNamesFunc(helper.GetTables()); + List> tableQueryables = new List>(); + foreach (var item in tables) + { + tableQueryables.Add(this.Clone().AS(item.TableName)); + } + Check.Exception(tableQueryables.Count == 0, ErrorMessage.GetThrowMessage("SplitTable error . There are no tables after filtering", "SplitTable没有筛选出分表,请检查条件和数据库中的表")); + var unionall = this.Context._UnionAll(tableQueryables.ToArray()); + //var values= unionall.QueryBuilder.GetSelectValue; + //unionall.QueryBuilder.SelectValue = values; + return unionall; + } public ISugarQueryable Distinct() { QueryBuilder.IsDistinct = true; @@ -1627,6 +1700,12 @@ namespace SqlSugar { var firstPareamter = (express as LambdaExpression).Parameters.First(); this.QueryBuilder.TableShortName = firstPareamter.Name; + if (this.QueryBuilder.AsTables != null && this.QueryBuilder.AsTables.Count==1) + { + var tableinfo = this.QueryBuilder.AsTables.First(); + this.QueryBuilder.AsTables[tableinfo.Key] =" (SELECT * FROM " +this.QueryBuilder.AsTables.First().Value+")"; + this.QueryBuilder.SelectValue = this.QueryBuilder.TableShortName +".*"; + } } Check.Exception(result.JoinIndex > 10, ErrorMessage.GetThrowMessage("只支持12个表", "Only 12 tables are supported")); return result; @@ -2501,6 +2580,57 @@ namespace SqlSugar #region T2 public partial class QueryableProvider : QueryableProvider, ISugarQueryable { + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -2858,6 +2988,57 @@ namespace SqlSugar #region T3 public partial class QueryableProvider : QueryableProvider, ISugarQueryable { + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -3280,6 +3461,57 @@ namespace SqlSugar #region T4 public partial class QueryableProvider : QueryableProvider, ISugarQueryable { + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -3743,6 +3975,57 @@ namespace SqlSugar #region T5 public partial class QueryableProvider : QueryableProvider, ISugarQueryable { + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -4168,6 +4451,57 @@ namespace SqlSugar #region T6 public partial class QueryableProvider : QueryableProvider, ISugarQueryable { + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -4627,6 +4961,57 @@ namespace SqlSugar #region T7 public partial class QueryableProvider : QueryableProvider, ISugarQueryable { + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -5077,6 +5462,57 @@ namespace SqlSugar #region T8 public partial class QueryableProvider : QueryableProvider, ISugarQueryable { + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -5560,6 +5996,57 @@ namespace SqlSugar #region T9 public partial class QueryableProvider : QueryableProvider, ISugarQueryable { + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -5796,6 +6283,67 @@ namespace SqlSugar _OrderBy(expression, type); return this; } + + public new ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds) + { + if (isOrderBy) + base.OrderBy(orderFileds); + return this; + } + public new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } #endregion #region GroupBy @@ -5979,6 +6527,57 @@ namespace SqlSugar #region T10 public partial class QueryableProvider : QueryableProvider, ISugarQueryable { + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -6235,6 +6834,73 @@ namespace SqlSugar _OrderBy(expression, type); return this; } + + public new ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds) + { + if (isOrderBy) + base.OrderBy(orderFileds); + return this; + } + public new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } #endregion #region GroupBy @@ -6423,6 +7089,57 @@ namespace SqlSugar #region T11 public partial class QueryableProvider : QueryableProvider, ISugarQueryable { + public ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Left); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Inner); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } + public ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression) + { + this.Context.InitMappingInfo(); + var result = InstanceFactory.GetQueryable(this.Context.CurrentConnectionConfig); + result.SqlBuilder = this.SqlBuilder; + result.Context = this.Context; + var joinInfo = GetJoinInfo(joinExpression, JoinType.Right); + var sqlObject = joinQueryable.ToSql(); + string sql = sqlObject.Key; + this.QueryBuilder.LambdaExpressions.ParameterIndex += 100; + UtilMethods.RepairReplicationParameters(ref sql, sqlObject.Value.ToArray(), this.QueryBuilder.LambdaExpressions.ParameterIndex, ""); + joinInfo.TableName = "(" + sql + ")"; + this.QueryBuilder.Parameters.AddRange(sqlObject.Value); + result.QueryBuilder.JoinQueryInfos.Add(joinInfo); + result.QueryBuilder.LambdaExpressions.ParameterIndex = this.QueryBuilder.LambdaExpressions.ParameterIndex; + return result; + } public ISugarQueryable LeftJoin(Expression> joinExpression) { this.Context.InitMappingInfo(); @@ -6698,6 +7415,79 @@ namespace SqlSugar _OrderBy(expression, type); return this; } + + public new ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds) + { + if (isOrderBy) + base.OrderBy(orderFileds); + return this; + } + public new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } #endregion #region GroupBy @@ -7159,6 +7949,84 @@ namespace SqlSugar _OrderBy(expression, type); return this; } + public new ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds) + { + if (isOrderBy) + base.OrderBy(orderFileds); + return this; + } + public new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + { + if (isOrderBy) + _OrderBy(expression, type); + return this; + } #endregion #region GroupBy diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs index 3decc9355..5e89ea92f 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs @@ -162,7 +162,11 @@ namespace SqlSugar var copyObj = CopyEntityInfo(entityInfo); InitMappingInfo(copyObj); } - + public void InitMappingInfoNoCache(Type type) + { + var entityInfo = this.Context.EntityMaintenance.GetEntityInfoNoCache(type); + InitMappingInfo(entityInfo); + } private EntityInfo CopyEntityInfo(EntityInfo entityInfo) { EntityInfo result = new EntityInfo() diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index daaf5b60b..54c7c2bf6 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -504,6 +504,11 @@ namespace SqlSugar #endregion public virtual ISugarQueryable UnionAll(params ISugarQueryable[] queryables) where T : class, new() + { + return _UnionAll(queryables); + } + + internal ISugarQueryable _UnionAll(ISugarQueryable[] queryables) { var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig); Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null "); @@ -533,6 +538,7 @@ namespace SqlSugar return resulut.Select(sqlBuilder.SqlSelectAll); } } + public virtual ISugarQueryable UnionAll(List> queryables) where T : class, new() { Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null "); @@ -1127,5 +1133,40 @@ namespace SqlSugar } } #endregion + + #region Split table + public SplitTableContext SplitHelper() where T : class, new() + { + var result = new SplitTableContext(this.Context) + { + EntityInfo = this.Context.EntityMaintenance.GetEntityInfo() + }; + return result; + } + public SplitTableContextResult SplitHelper(T data) where T : class, new() + { + var result = new SplitTableContext(this.Context) + { + EntityInfo = this.Context.EntityMaintenance.GetEntityInfo() + }; + return new SplitTableContextResult() + { + Items = new List { data }, + Helper = result + }; + } + public SplitTableContextResult SplitHelper(List data) where T : class, new() + { + var result = new SplitTableContext(this.Context) + { + EntityInfo = this.Context.EntityMaintenance.GetEntityInfo() + }; + return new SplitTableContextResult() + { + Items = data, + Helper = result + }; + } + #endregion } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/SplitTableUpdateProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/SplitTableUpdateProvider.cs new file mode 100644 index 000000000..f59e65468 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/SplitTableUpdateProvider.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SplitTableUpdateProvider where T : class, new() + { + public SqlSugarProvider Context; + public UpdateableProvider updateobj; + + public IEnumerable Tables { get; set; } + + public int ExecuteCommand() + { + if (this.Context.Ado.Transaction == null) + { + try + { + this.Context.Ado.BeginTran(); + var result = _ExecuteCommand(); + this.Context.Ado.CommitTran(); + return result; + } + catch (Exception ex) + { + this.Context.Ado.RollbackTran(); + throw ex; + } + } + else + { + return _ExecuteCommand(); + } + } + public async Task ExecuteCommandAsync() + { + if (this.Context.Ado.Transaction == null) + { + try + { + this.Context.Ado.BeginTran(); + var result = await _ExecuteCommandAsync(); + this.Context.Ado.BeginTran(); + return result; + } + catch (Exception ex) + { + this.Context.Ado.RollbackTran(); + throw ex; + } + } + else + { + return await _ExecuteCommandAsync(); + } + } + private int _ExecuteCommand() + { + var result = 0; + var sqlobj = updateobj.ToSql(); + + foreach (var item in Tables) + { + var newsqlobj = GetSqlObj(sqlobj, item.TableName); + result += this.Context.Ado.ExecuteCommand(newsqlobj.Key, newsqlobj.Value); + } + return result; + } + + private async Task _ExecuteCommandAsync() + { + var result = 0; + var sqlobj = updateobj.ToSql(); + foreach (var item in Tables) + { + var newsqlobj = GetSqlObj(sqlobj, item.TableName); + result += await this.Context.Ado.ExecuteCommandAsync(newsqlobj.Key, newsqlobj.Value); + } + return result; + } + + private KeyValuePair> GetSqlObj(KeyValuePair> keyValuePair, string asName) + { + List pars = new List(); + string sql = keyValuePair.Key; + if (keyValuePair.Value != null) + { + pars = keyValuePair.Value.Select(it => new SugarParameter(it.ParameterName, it.Value)).ToList(); + } + sql = Regex.Replace(sql, updateobj.EntityInfo.DbTableName, asName, RegexOptions.IgnoreCase); + return new KeyValuePair>(sql, pars); + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index d6fb4807f..0c233aba6 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -89,6 +89,20 @@ namespace SqlSugar this.UpdateBuilder.TableWithString = lockString; return this; } + public SplitTableUpdateProvider SplitTable(Func, IEnumerable> getTableNamesFunc) + { + this.Context.MappingTables.Add(this.EntityInfo.EntityName, this.EntityInfo.DbTableName); + SplitTableUpdateProvider result = new SplitTableUpdateProvider(); + result.Context = this.Context; + SplitTableContext helper = new SplitTableContext(Context) + { + EntityInfo = this.EntityInfo + }; + var tables = getTableNamesFunc(helper.GetTables()); + result.Tables = tables; + result.updateobj= this; + return result; + } public IUpdateable RemoveDataCache() { this.RemoveCacheFunc = () => diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/ConnectionConfig.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/ConnectionConfig.cs index a7719d6d9..a6d088004 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/ConnectionConfig.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/ConnectionConfig.cs @@ -10,7 +10,7 @@ namespace SqlSugar public class ConnectionConfig { /// - /// + ///Connection unique code /// public dynamic ConfigId { get; set; } /// @@ -30,10 +30,14 @@ namespace SqlSugar /// public InitKeyType InitKeyType = InitKeyType.Attribute; /// - ///If true, there is only one connection instance in the same thread within the same connection string - [Obsolete("use SqlSugar.Ioc")] + /// Exception prompt language /// - public bool IsShardSameThread { get; set; } + public LanguageType LanguageType { get=>ErrorMessage.SugarLanguageType; set=>ErrorMessage.SugarLanguageType=value; } + /// + ///If true, there is only one connection instance in the same thread within the same connection string + //[Obsolete("use SqlSugar.Ioc")] + ///// + //public bool IsShardSameThread { get; set; } /// /// Configure External Services replace default services,For example, Redis storage /// @@ -48,10 +52,10 @@ namespace SqlSugar /// More Gobal Settings /// public ConnMoreSettings MoreSettings { get; set; } - /// - /// Used for debugging errors or BUG,Used for debugging, which has an impact on Performance - /// - public SugarDebugger Debugger { get; set; } + ///// + ///// Used for debugging errors or BUG,Used for debugging, which has an impact on Performance + ///// + //public SugarDebugger Debugger { get; set; } public string IndexSuffix { get; set; } @@ -75,7 +79,7 @@ namespace SqlSugar private ICacheService _ReflectionInoCache; private ICacheService _DataInfoCache; private IRazorService _RazorService; - + public ISplitTableService SplitTableService { get; set; } public IRazorService RazorService { get diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/SqlFilter.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/SqlFilter.cs index 0cc906be9..0c76e155f 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/SqlFilter.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/SqlFilter.cs @@ -32,6 +32,13 @@ namespace SqlSugar exp = expression; type = typeof(T); } + + public TableFilterItem(Type entityType,Expression expression) + { + exp = expression; + type = entityType; + } + private new string FilterName { get; set; } private new Func FilterValue { get; set; } private new bool IsJoinQuery { get; set; } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Enum/LanguageType.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Enum/LanguageType.cs new file mode 100644 index 000000000..9339d0010 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Enum/LanguageType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public enum LanguageType + { + Default=0, + Chinese=1, + English=2 + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExternalServiceInterface/ISplitTableService.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExternalServiceInterface/ISplitTableService.cs new file mode 100644 index 000000000..9c39d8b46 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExternalServiceInterface/ISplitTableService.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public interface ISplitTableService + { + List GetAllTables(ISqlSugarClient db,EntityInfo EntityInfo,List tableInfos); + string GetTableName(ISqlSugarClient db, EntityInfo EntityInfo); + string GetTableName(ISqlSugarClient db, EntityInfo EntityInfo, SplitType type); + string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object fieldValue); + object GetFieldValue(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object entityValue); + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/IntegrationServices/SplitTableService.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/IntegrationServices/SplitTableService.cs new file mode 100644 index 000000000..22bd9ae57 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/IntegrationServices/SplitTableService.cs @@ -0,0 +1,259 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class DateSplitTableService : ISplitTableService + { + #region Core + public virtual List GetAllTables(ISqlSugarClient db, EntityInfo EntityInfo, List tableInfos) + { + CheckTableName(EntityInfo.DbTableName); + var regex = EntityInfo.DbTableName.Replace("{year}", "([0-9]{2,4})").Replace("{day}", "([0-9]{1,2})").Replace("{month}", "([0-9]{1,2})"); + var currentTables = tableInfos.Where(it => Regex.IsMatch(it.Name, regex, RegexOptions.IgnoreCase)).Select(it => it.Name).Reverse().ToList(); + List result = new List(); + foreach (var item in currentTables) + { + SplitTableInfo tableInfo = new SplitTableInfo(); + tableInfo.TableName = item; + var math = Regex.Match(item, regex,RegexOptions.IgnoreCase); + var group1 = math.Groups[1].Value; + var group2 = math.Groups[2].Value; + var group3 = math.Groups[3].Value; + tableInfo.Date = GetDate(group1, group2, group3, EntityInfo.DbTableName); + //tableInfo.String = null; Time table, it doesn't work + //tableInfo.Long = null; Time table, it doesn't work + result.Add(tableInfo); + } + result = result.OrderByDescending(it => it.Date).ToList(); + return result; + } + public virtual string GetTableName(ISqlSugarClient db, EntityInfo EntityInfo) + { + var splitTableAttribute = EntityInfo.Type.GetCustomAttribute(); + if (splitTableAttribute != null) + { + var type=(splitTableAttribute as SplitTableAttribute).SplitType; + return GetTableName(db, EntityInfo, type); + } + else + { + return GetTableName(db, EntityInfo, SplitType.Day); + } + } + public virtual string GetTableName(ISqlSugarClient db, EntityInfo EntityInfo, SplitType splitType) + { + var date = db.GetDate(); + return GetTableNameByDate(EntityInfo, splitType, date); + } + public virtual string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object fieldValue) + { + var value = Convert.ToDateTime(fieldValue); + return GetTableNameByDate(entityInfo, splitType, value); + } + public virtual object GetFieldValue(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object entityValue) + { + var splitColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyInfo.GetCustomAttribute() != null); + if (splitColumn == null) + { + return db.GetDate(); + } + else + { + var value = splitColumn.PropertyInfo.GetValue(entityValue, null); + if (value == null) + { + return db.GetDate(); + } + else if (UtilMethods.GetUnderType(value.GetType()) != UtilConstants.DateType) + { + throw new Exception($"DateSplitTableService Split column {splitColumn.PropertyName} not DateTime " + splitType.ToString()); + } + else if (Convert.ToDateTime(value) == DateTime.MinValue) + { + return db.GetDate(); + } + else + { + return value; + } + } + } + public void VerifySplitType(SplitType splitType) + { + switch (splitType) + { + case SplitType.Day: + break; + case SplitType.Week: + break; + case SplitType.Month: + break; + case SplitType.Season: + break; + case SplitType.Year: + break; + default: + throw new Exception("DateSplitTableService no support " + splitType.ToString()); + } + } + + #endregion + + #region Common Helper + private string GetTableNameByDate(EntityInfo EntityInfo,SplitType splitType,DateTime date) + { + date = ConvertDateBySplitType(date, splitType); + return EntityInfo.DbTableName.Replace("{year}", date.Year + "").Replace("{day}", PadLeft2(date.Day + "")).Replace("{month}", PadLeft2(date.Month + "")); + } + + private DateTime GetDate(string group1, string group2, string group3, string dbTableName) + { + var yearIndex = dbTableName.IndexOf("{year}"); + var dayIndex = dbTableName.IndexOf("{day}"); + var monthIndex = dbTableName.IndexOf("{month}"); + List tables = new List(); + tables.Add(new SplitTableSort() { Name = "{year}", Sort = yearIndex }); + tables.Add(new SplitTableSort() { Name = "{day}", Sort = dayIndex }); + tables.Add(new SplitTableSort() { Name = "{month}", Sort = monthIndex }); + tables = tables.OrderBy(it => it.Sort).ToList(); + var year = ""; + var month = ""; + var day = ""; + if (tables[0].Name == "{year}") + { + year = group1; + } + if (tables[1].Name == "{year}") + { + year = group2; + } + if (tables[2].Name == "{year}") + { + year = group3; + } + if (tables[0].Name == "{month}") + { + month = group1; + } + if (tables[1].Name == "{month}") + { + month = group2; + } + if (tables[2].Name == "{month}") + { + month = group3; + } + if (tables[0].Name == "{day}") + { + day = group1; + } + if (tables[1].Name == "{day}") + { + day = group2; + } + if (tables[2].Name == "{day}") + { + day = group3; + } + return Convert.ToDateTime($"{year}-{month}-{day}"); + } + + private string PadLeft2(string str) + { + if (str.Length < 2) + { + return str.PadLeft(2, '0'); + } + else + { + return str; + } + } + + private static void CheckTableName(string dbTableName) + { + Check.Exception(!dbTableName.Contains("{year}"), ErrorMessage.GetThrowMessage("table name need {{year}}", "分表表名需要占位符 {{year}}")); + Check.Exception(!dbTableName.Contains("{month}"), ErrorMessage.GetThrowMessage("table name need {{month}}", "分表表名需要占位符 {{month}} ")); + Check.Exception(!dbTableName.Contains("{day}"), ErrorMessage.GetThrowMessage("table name need {{day}}", "分表表名需要占位符{{day}}")); + Check.Exception(Regex.Matches(dbTableName, @"\{year\}").Count > 1, ErrorMessage.GetThrowMessage(" There can only be one {{year}}", " 只能有一个 {{year}}")); + Check.Exception(Regex.Matches(dbTableName, @"\{month\}").Count > 1, ErrorMessage.GetThrowMessage("There can only be one {{month}}", "只能有一个 {{month}} ")); + Check.Exception(Regex.Matches(dbTableName, @"\{day\}").Count > 1, ErrorMessage.GetThrowMessage("There can only be one {{day}}", "只能有一个{{day}}")); + Check.Exception(Regex.IsMatch(dbTableName, @"\d\{|\}\d"), ErrorMessage.GetThrowMessage(" '{{' or '}}' can't be numbers nearby", "占位符相令一位不能是数字,比如 : 1{{day}}2 错误 , 正确: 1_{{day}}_2")); + } + #endregion + + #region Date Helper + private DateTime ConvertDateBySplitType(DateTime time, SplitType type) + { + switch (type) + { + case SplitType.Day: + return Convert.ToDateTime(time.ToString("yyyy-MM-dd")); + case SplitType.Week: + return GetMondayDate(time); + case SplitType.Month: + return Convert.ToDateTime(time.ToString("yyyy-MM-01")); + case SplitType.Season: + if (time.Month <= 3) + { + return Convert.ToDateTime(time.ToString("yyyy-01-01")); + } + else if (time.Month <= 6) + { + return Convert.ToDateTime(time.ToString("yyyy-04-01")); + } + else if (time.Month <= 9) + { + return Convert.ToDateTime(time.ToString("yyyy-07-01")); + } + else + { + return Convert.ToDateTime(time.ToString("yyyy-10-01")); + } + case SplitType.Year: + return Convert.ToDateTime(time.ToString("yyyy-01-01")); + default: + throw new Exception($"SplitType paramter error "); + } + } + private DateTime GetMondayDate() + { + return GetMondayDate(DateTime.Now); + } + private DateTime GetSundayDate() + { + return GetSundayDate(DateTime.Now); + } + private DateTime GetMondayDate(DateTime someDate) + { + int i = someDate.DayOfWeek - DayOfWeek.Monday; + if (i == -1) i = 6; + TimeSpan ts = new TimeSpan(i, 0, 0, 0); + return someDate.Subtract(ts); + } + private DateTime GetSundayDate(DateTime someDate) + { + int i = someDate.DayOfWeek - DayOfWeek.Sunday; + if (i != 0) i = 7 - i; + TimeSpan ts = new TimeSpan(i, 0, 0, 0); + return someDate.Add(ts); + } + + #endregion + + #region Private Models + internal class SplitTableSort + { + public string Name { get; set; } + public int Sort { get; set; } + } + #endregion + + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/ICodeFirst.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/ICodeFirst.cs index 46b7af02d..6eb0e6a4c 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/ICodeFirst.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/ICodeFirst.cs @@ -17,5 +17,6 @@ namespace SqlSugar void InitTables(); void InitTables(); void InitTables(); + SplitCodeFirstProvider SplitTables(); } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IDeleteable.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IDeleteable.cs index 789e057d1..9da83f812 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IDeleteable.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IDeleteable.cs @@ -36,6 +36,7 @@ namespace SqlSugar IDeleteable RemoveDataCache(string likeString); KeyValuePair> ToSql(); IDeleteable EnableQueryFilter(); + SplitTableDeleteProvider SplitTable(Func, IEnumerable> getTableNamesFunc); void AddQueue(); } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IQueryable.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IQueryable.cs index 0e894654f..31e2553f5 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IQueryable.cs @@ -19,6 +19,9 @@ namespace SqlSugar ISugarQueryable AS(string tableName); ISugarQueryable AS(string tableName); ISugarQueryable With(string withString); + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -179,9 +182,13 @@ namespace SqlSugar DataTable ToPivotTable(Func columnSelector,Expression> rowSelector,Func, TData> dataSelector); List ToPivotList(Func columnSelector, Expression> rowSelector, Func, TData> dataSelector); string ToPivotJson(Func columnSelector, Expression> rowSelector, Func, TData> dataSelector); + ISugarQueryable SplitTable(Func,IEnumerable> getTableNamesFunc); } public partial interface ISugarQueryable : ISugarQueryable { + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -270,6 +277,9 @@ namespace SqlSugar } public partial interface ISugarQueryable : ISugarQueryable { + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -369,6 +379,9 @@ namespace SqlSugar } public partial interface ISugarQueryable : ISugarQueryable { + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -476,6 +489,9 @@ namespace SqlSugar } public partial interface ISugarQueryable : ISugarQueryable { + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -579,6 +595,9 @@ namespace SqlSugar } public partial interface ISugarQueryable : ISugarQueryable { + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -689,6 +708,9 @@ namespace SqlSugar } public partial interface ISugarQueryable : ISugarQueryable { + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -798,6 +820,9 @@ namespace SqlSugar } public partial interface ISugarQueryable : ISugarQueryable { + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -915,6 +940,9 @@ namespace SqlSugar #region 9-12 public partial interface ISugarQueryable : ISugarQueryable { + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -966,6 +994,16 @@ namespace SqlSugar ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); + + new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); #endregion #region GroupBy @@ -1010,6 +1048,9 @@ namespace SqlSugar } public partial interface ISugarQueryable : ISugarQueryable { + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -1065,6 +1106,19 @@ namespace SqlSugar ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); + + + + new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); #endregion #region GroupBy @@ -1110,6 +1164,9 @@ namespace SqlSugar } public partial interface ISugarQueryable : ISugarQueryable { + ISugarQueryable LeftJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable InnerJoin(ISugarQueryable joinQueryable, Expression> joinExpression); + ISugarQueryable RightJoin(ISugarQueryable joinQueryable, Expression> joinExpression); ISugarQueryable LeftJoin(Expression> joinExpression); ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); @@ -1169,6 +1226,18 @@ namespace SqlSugar ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); + + new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); #endregion #region GroupBy @@ -1275,6 +1344,19 @@ namespace SqlSugar ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); ISugarQueryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc); + + new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); + ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc); #endregion #region GroupBy diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/ISqlSugarClient.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/ISqlSugarClient.cs index 5089d0b16..2d98e2e94 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/ISqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/ISqlSugarClient.cs @@ -43,6 +43,9 @@ namespace SqlSugar #endregion #region Other methods + SplitTableContext SplitHelper() where T : class, new(); + SplitTableContextResult SplitHelper(T data) where T : class, new(); + SplitTableContextResult SplitHelper(List data) where T : class, new(); DateTime GetDate(); //SimpleClient GetSimpleClient(); SimpleClient GetSimpleClient() where T : class, new(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IUpdateable.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IUpdateable.cs index 99129fbc2..0718cdc22 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IUpdateable.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IUpdateable.cs @@ -92,6 +92,7 @@ namespace SqlSugar IUpdateable CallEntityMethod(Expression> method); KeyValuePair> ToSql(); void AddQueue(); - + SplitTableUpdateProvider SplitTable(Func, IEnumerable> getTableNamesFunc); + } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/Insertable.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/Insertable.cs index 473e1e02e..e136e14e8 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/Insertable.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/Insertable.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace SqlSugar { - public partial interface IInsertable + public partial interface IInsertable where T :class,new() { InsertBuilder InsertBuilder { get; set; } int ExecuteCommand(); @@ -45,6 +45,9 @@ namespace SqlSugar SqlServerBlukCopy UseSqlServer(); MySqlBlukCopy UseMySql(); OracleBlukCopy UseOracle(); + + SplitInsertable SplitTable(); + SplitInsertable SplitTable(SplitType splitType); void AddQueue(); } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/MySql/SqlBuilder/MySqlBlukCopy.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/MySql/SqlBuilder/MySqlBlukCopy.cs index c174f1f0f..3d3ae6c28 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/MySql/SqlBuilder/MySqlBlukCopy.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/MySql/SqlBuilder/MySqlBlukCopy.cs @@ -42,10 +42,10 @@ namespace SqlSugar Type type = typeof(T); var entity = this.Context.EntityMaintenance.GetEntityInfo(); dt.TableName = this.Builder.GetTranslationColumnName(entity.DbTableName); - if (this.Context.MappingTables != null && this.Context.MappingTables.Any(it => it.EntityName == it.EntityName)) - { - dt.TableName = this.Builder.GetTranslationColumnName(this.Context.MappingTables.First(it => it.EntityName == it.EntityName).DbTableName); - } + //if (this.Context.MappingTables != null && this.Context.MappingTables.Any(it => it.EntityName == it.EntityName)) + //{ + // dt.TableName = this.Builder.GetTranslationColumnName(this.Context.MappingTables.First(it => it.EntityName == it.EntityName).DbTableName); + //} //创建属性的集合 List pList = new List(); //把所有的public属性加入到集合 并添加DataTable的列 diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/Sqlite/CodeFirst/SqliteCodeFirst.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/Sqlite/CodeFirst/SqliteCodeFirst.cs index 59f9c8aa6..61f967d2c 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/Sqlite/CodeFirst/SqliteCodeFirst.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/Sqlite/CodeFirst/SqliteCodeFirst.cs @@ -14,7 +14,7 @@ namespace SqlSugar Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1"); var tableName = GetTableName(entityInfo); - var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName); + var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName,false); ConvertColumns(dbColumns); var entityColumns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList(); var dropColumns = dbColumns diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTableAttribute.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTableAttribute.cs new file mode 100644 index 000000000..b7ccb43ea --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTableAttribute.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + [AttributeUsage(AttributeTargets.Class, Inherited = true)] + public class SplitTableAttribute : Attribute + { + public SplitType SplitType { get; set; } + public SplitTableAttribute(SplitType splitType) + { + this.SplitType = splitType; + } + } + + [AttributeUsage(AttributeTargets.Property, Inherited = true)] + public class SplitFieldAttribute : Attribute + { + + public SplitFieldAttribute() + { + + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTableContext.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTableContext.cs new file mode 100644 index 000000000..8fac07b99 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTableContext.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SplitTableContextResult + { + public List Items { get; set; } + public SplitTableContext Helper { get; set; } + + public string [] GetTableNames() + { + List result = new List(); + var attribute = typeof(T).GetCustomAttribute() as SplitTableAttribute; + Check.Exception(attribute == null, $" {typeof(T).Name} need SplitTableAttribute"); + foreach (var item in Items) + { + result.Add(Helper.GetTableName(Helper.GetValue(attribute.SplitType,item))); + } + return result.Distinct().ToArray(); + } + public string[] GetTableNames(SplitType splitType) + { + List result = new List();; + foreach (var item in Items) + { + result.Add(Helper.GetTableName(Helper.GetValue(splitType, item))); + } + return result.ToArray(); + } + } + public class SplitTableContext + { + internal SqlSugarProvider Context { get; set; } + internal EntityInfo EntityInfo { get; set; } + internal ISplitTableService Service { get; set; } + private SplitTableContext() { } + internal SplitTableContext(SqlSugarProvider context) + { + this.Context = context; + if (this.Context.CurrentConnectionConfig.ConfigureExternalServices != null&&this.Context.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService!=null) + { + Service = this.Context.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService; + } + else + { + Service = new DateSplitTableService(); + } + } + public List GetTables() + { + var oldIsEnableLogEvent = this.Context.Ado.IsEnableLogEvent; + this.Context.Ado.IsEnableLogEvent = false; + var tableInfos = this.Context.DbMaintenance.GetTableInfoList(false); + List result = Service.GetAllTables(this.Context,EntityInfo,tableInfos); + this.Context.Ado.IsEnableLogEvent = oldIsEnableLogEvent; + return result; + } + + public string GetDefaultTableName() + { + return Service.GetTableName(this.Context,EntityInfo); + } + public string GetTableName(SplitType splitType) + { + return Service.GetTableName(this.Context,EntityInfo, splitType); + } + public string GetTableName(SplitType splitType, object fieldValue) + { + return Service.GetTableName(this.Context,EntityInfo, splitType, fieldValue); + } + public string GetTableName(object fieldValue) + { + var attribute = EntityInfo.Type.GetCustomAttribute() as SplitTableAttribute; + Check.Exception(attribute == null, $" {EntityInfo.EntityName} need SplitTableAttribute"); + return Service.GetTableName(this.Context, EntityInfo, attribute.SplitType, fieldValue); + } + public object GetValue(SplitType splitType, object entityValue) + { + return Service.GetFieldValue(this.Context,EntityInfo, splitType, entityValue); + } + internal void CheckPrimaryKey() + { + Check.Exception(EntityInfo.Columns.Any(it => it.IsIdentity == true), ErrorMessage.GetThrowMessage("Split table can't IsIdentity=true", "分表禁止使用自增列")); + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTableInfo.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTableInfo.cs new file mode 100644 index 000000000..2eefea71c --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTableInfo.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SplitTableInfo + { + public string TableName { get; set; } + public DateTime Date { get; set; } + public String String { get; set; } + public decimal Decimal { get; set; } + public long Long { get; set; } + public int Int { get; set; } + public Byte[] ByteArray { get; set; } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitType.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitType.cs new file mode 100644 index 000000000..2e1080c50 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitType.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public enum SplitType + { + Day = 0, + Week = 1, + Month = 2, + Season = 3, + Year = 4, + _Custom01 = 5, + _Custom02 = 6, + _Custom03 = 7, + _Custom04 = 8, + _Custom05 = 9, + _Custom06 = 10, + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTypeExtensions.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTypeExtensions.cs new file mode 100644 index 000000000..2d5a06712 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SpliteTable/SplitTypeExtensions.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public static class SplitTableInfoExtensions + { + public static IEnumerable InTableNames(this List tables, params string[] tableNames) + { + return tables.Where(it => tableNames.Any(y => y.Equals(it.TableName, StringComparison.OrdinalIgnoreCase))); + } + public static IEnumerable ContainsTableNames(this List tables, params string[] tableNames) + { + List result = new List(); + foreach (var item in tables) + { + if (tableNames.Any(it => item.TableName.Contains(it))) + { + result.Add(item); + } + } + return result; + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarClient.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarClient.cs index 7c0a88eb7..83bbe2496 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarClient.cs @@ -37,7 +37,7 @@ namespace SqlSugar public SqlSugarClient(List configs) { - Check.Exception(configs.IsNullOrEmpty(), "List configs is null"); + Check.Exception(configs.IsNullOrEmpty(), "List configs is null or count=0"); InitConfigs(configs); var config = configs.First(); InitContext(config); @@ -846,21 +846,33 @@ namespace SqlSugar #endregion #region Helper + public SplitTableContext SplitHelper() where T:class,new() + { + return this.Context.SplitHelper(); + } + public SplitTableContextResult SplitHelper(T data) where T : class, new() + { + return this.Context.SplitHelper(data); + } + public SplitTableContextResult SplitHelper(List dataList) where T : class, new() + { + return this.Context.SplitHelper(dataList); + } private SqlSugarProvider GetContext() { SqlSugarProvider result = null; - if (IsSameThreadAndShard()) - { - result = SameThreadAndShard(); - } - else if (IsNoSameThreadAndShard()) - { - result = NoSameThreadAndShard(); - } - else - { + //if (IsSameThreadAndShard()) + //{ + // result = SameThreadAndShard(); + //} + //else if (IsNoSameThreadAndShard()) + //{ + // result = NoSameThreadAndShard(); + //} + //else + //{ result = Synchronization(); - } + //} ///Because SqlSugarScope implements thread safety //else if (IsSingleInstanceAsync()) //{ @@ -1003,15 +1015,15 @@ namespace SqlSugar return _ThreadId == Thread.CurrentThread.ManagedThreadId.ToString(); } - private bool IsNoSameThreadAndShard() - { - return CurrentConnectionConfig.IsShardSameThread && _ThreadId != Thread.CurrentThread.ManagedThreadId.ToString(); - } + //private bool IsNoSameThreadAndShard() + //{ + // return CurrentConnectionConfig.IsShardSameThread && _ThreadId != Thread.CurrentThread.ManagedThreadId.ToString(); + //} - private bool IsSameThreadAndShard() - { - return CurrentConnectionConfig.IsShardSameThread && _ThreadId == Thread.CurrentThread.ManagedThreadId.ToString(); - } + //private bool IsSameThreadAndShard() + //{ + // return CurrentConnectionConfig.IsShardSameThread && _ThreadId == Thread.CurrentThread.ManagedThreadId.ToString(); + //} private SqlSugarProvider CopyClient() { @@ -1051,7 +1063,7 @@ namespace SqlSugar _Context.Ado.IsEnableLogEvent = true; this.CurrentConnectionConfig = config; _ThreadId = Thread.CurrentThread.ManagedThreadId.ToString(); - if (_MappingColumns == null) + if (this.MappingTables == null) this.MappingTables = new MappingTableList(); if (this.MappingColumns == null) this.MappingColumns = new MappingColumnList(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarScope.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarScope.cs index 98448daed..7db6e93dd 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarScope.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/SqlSugarScope.cs @@ -596,6 +596,18 @@ namespace SqlSugar { return ScopedContext.Updateable(UpdateObjs); } + public SplitTableContext SplitHelper() where T : class, new() + { + return ScopedContext.SplitHelper(); + } + public SplitTableContextResult SplitHelper(T data) where T : class, new() + { + return ScopedContext.SplitHelper(data); + } + public SplitTableContextResult SplitHelper(List dataList) where T : class, new() + { + return ScopedContext.SplitHelper(dataList); + } public SqlSguarTransaction UseTran() { return ScopedContext.UseTran(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Utilities/ErrorMessage.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Utilities/ErrorMessage.cs index a72192ca0..b417414e3 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Utilities/ErrorMessage.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Utilities/ErrorMessage.cs @@ -6,6 +6,7 @@ namespace SqlSugar { internal static partial class ErrorMessage { + internal static LanguageType SugarLanguageType { get; set; } = LanguageType.Default; internal static string ObjNotExist { get @@ -43,10 +44,21 @@ namespace SqlSugar internal static string GetThrowMessage(string enMessage, string cnMessage, params string[] args) { - List formatArgs = new List() { enMessage, cnMessage }; - formatArgs.AddRange(args); - return string.Format(@"中文提示 : {1} -Chinese Message : {0}", formatArgs.ToArray()); + if (SugarLanguageType == LanguageType.Default) + { + List formatArgs = new List() { enMessage, cnMessage }; + formatArgs.AddRange(args); + return string.Format(@"中文提示 : {1} +English Message : {0}", formatArgs.ToArray()); + } + else if (SugarLanguageType == LanguageType.English) + { + return enMessage; + } + else + { + return cnMessage; + } } } }