sunkaixuna
3 years ago
13 changed files with 164 additions and 1 deletions
@ -0,0 +1,36 @@ |
|||
using SqlSugar; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace OrmTest |
|||
{ |
|||
public class DemoN_SplitTable |
|||
{ |
|||
public static void Init() |
|||
{ |
|||
Console.WriteLine(""); |
|||
Console.WriteLine("#### CodeFirst Start ####"); |
|||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() |
|||
{ |
|||
DbType = DbType.SqlServer, |
|||
ConnectionString = Config.ConnectionString, |
|||
InitKeyType = InitKeyType.Attribute, |
|||
IsAutoCloseConnection = true |
|||
}); |
|||
db.CodeFirst.SplitTables().InitTables<OrderSpliteTest>(); |
|||
db.Queryable<OrderSpliteTest>().SplitTable(it => it.Take(3)).ToList(); |
|||
Console.WriteLine("#### CodeFirst end ####"); |
|||
} |
|||
|
|||
[SqlSugar.SugarTable("Order{year}{month}{day}")] |
|||
public class OrderSpliteTest |
|||
{ |
|||
[SugarColumn(IsPrimaryKey =true)] |
|||
public Guid Pk{ get; set; } |
|||
public string Name { get; set; } |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,38 @@ |
|||
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<T>() |
|||
{ |
|||
this.Context.InitMappingInfo<T>(); |
|||
SplitTableHelper helper = new SplitTableHelper() |
|||
{ |
|||
Context = this.Context, |
|||
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>() |
|||
}; |
|||
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); |
|||
this.Context.CodeFirst.InitTables(typeof(T)); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, helper.GetDefaultTableName()); |
|||
this.Context.CodeFirst.InitTables(typeof(T)); |
|||
} |
|||
this.Context.MappingTables = oldMapingTables; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,13 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
public class SplitInsertable |
|||
{ |
|||
|
|||
} |
|||
} |
@ -0,0 +1,28 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Text.RegularExpressions; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
internal class SplitTableHelper |
|||
{ |
|||
public SqlSugarProvider Context { get; set; } |
|||
public EntityInfo EntityInfo { get; set; } |
|||
public List<string> GetTables() |
|||
{ |
|||
|
|||
var tableInfos = this.Context.DbMaintenance.GetTableInfoList(false); |
|||
var regex = EntityInfo.DbTableName.Replace("{year}", "[0-9]{4}").Replace("{day}", "[0-9]{2}").Replace("{month}", "[0-9]{4}"); |
|||
var currentTables = tableInfos.Where(it => Regex.IsMatch(it.Name, regex, RegexOptions.IgnoreCase)).Select(it => it.Name).Reverse().ToList(); |
|||
return currentTables; |
|||
} |
|||
|
|||
public string GetDefaultTableName() |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,15 @@ |
|||
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=1 |
|||
} |
|||
} |
Loading…
Reference in new issue