Browse Source

Split table

pull/9/head
sunkaixuna 3 years ago
parent
commit
563b32f64a
  1. 1
      Src/Asp.Net/SqlServerTest/UnitTest/Main.cs
  2. 36
      Src/Asp.Net/SqlServerTest/UnitTest/USplit.cs
  3. 22
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs
  4. 6
      Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs
  5. 6
      Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs
  6. 6
      Src/Asp.Net/SqlSugar/SqlSugarClient.cs
  7. 6
      Src/Asp.Net/SqlSugar/SqlSugarScope.cs

1
Src/Asp.Net/SqlServerTest/UnitTest/Main.cs

@ -32,6 +32,7 @@ namespace OrmTest
public static void Init()
{
SplitTest();
Filter();
Insert();
Insert2();

36
Src/Asp.Net/SqlServerTest/UnitTest/USplit.cs

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
namespace OrmTest
{
public partial class NewUnitTest {
public static void SplitTest()
{
var db = Db;
db.CodeFirst.SplitTables().InitTables<SplitTestTable>();
db.Queryable<SplitTestTable>().Where(it => it.Name.Contains("a")).SplitTable(tas => tas.Take(3)).ToList();
var table2019=Db.SplitHelper<SplitTestTable>().GetTableName("2019-12-1");
db.Queryable<SplitTestTable>().Where(it => it.Name.Contains("a")).SplitTable(tas => tas.InTableNames(table2019)).ToList();
db.Queryable<SplitTestTable>().Where(it => it.Id == 1).SplitTable(tas => tas.Where(y => y.TableName.Contains("2019"))).ToList();
}
}
[SplitTable(SplitType.Year)]
[SugarTable("SplitTestTable_{year}{month}{day}")]
public class SplitTestTable
{
[SugarColumn(IsPrimaryKey =true)]
public long Id { get; set; }
public string Name { get; set; }
[SplitField]
public string CreateTime { get; set; }
}
}

22
Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs

@ -10,29 +10,43 @@ namespace SqlSugar
{
public SqlSugarProvider Context;
public void InitTables<T>()
{
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<T>()
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo(type)
};
helper.CheckPrimaryKey();
var tables = helper.GetTables();
//var oldMapingTables = this.Context.MappingTables;
if (tables.Count >0)
if (tables.Count > 0)
{
foreach (var item in tables)
{
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, item.TableName);
this.Context.CodeFirst.InitTables(typeof(T));
this.Context.CodeFirst.InitTables(type);
}
}
else
{
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, helper.GetDefaultTableName());
this.Context.CodeFirst.InitTables(typeof(T));
this.Context.CodeFirst.InitTables(type);
}
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, helper.EntityInfo.DbTableName);
}
public void InitTables<T>(Type [] types)
{
foreach (var type in types)
{
InitTables(type);
}
}
}
}

6
Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs

@ -1135,7 +1135,7 @@ namespace SqlSugar
#endregion
#region Split table
public SplitTableContext SplitHelper<T>()
public SplitTableContext SplitHelper<T>() where T : class, new()
{
var result = new SplitTableContext(this.Context)
{
@ -1143,7 +1143,7 @@ namespace SqlSugar
};
return result;
}
public SplitTableContextResult<T> SplitHelper<T>(T data)
public SplitTableContextResult<T> SplitHelper<T>(T data) where T : class, new()
{
var result = new SplitTableContext(this.Context)
{
@ -1155,7 +1155,7 @@ namespace SqlSugar
Helper = result
};
}
public SplitTableContextResult<T> SplitHelper<T>(List<T> data)
public SplitTableContextResult<T> SplitHelper<T>(List<T> data) where T : class, new()
{
var result = new SplitTableContext(this.Context)
{

6
Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs

@ -43,9 +43,9 @@ namespace SqlSugar
#endregion
#region Other methods
SplitTableContext SplitHelper<T>();
SplitTableContextResult<T> SplitHelper<T>(T data);
SplitTableContextResult<T> SplitHelper<T>(List<T> data);
SplitTableContext SplitHelper<T>() where T : class, new();
SplitTableContextResult<T> SplitHelper<T>(T data) where T : class, new();
SplitTableContextResult<T> SplitHelper<T>(List<T> data) where T : class, new();
DateTime GetDate();
//SimpleClient GetSimpleClient();
SimpleClient<T> GetSimpleClient<T>() where T : class, new();

6
Src/Asp.Net/SqlSugar/SqlSugarClient.cs

@ -846,15 +846,15 @@ namespace SqlSugar
#endregion
#region Helper
public SplitTableContext SplitHelper<T>()
public SplitTableContext SplitHelper<T>() where T:class,new()
{
return this.Context.SplitHelper<T>();
}
public SplitTableContextResult<T> SplitHelper<T>(T data)
public SplitTableContextResult<T> SplitHelper<T>(T data) where T : class, new()
{
return this.Context.SplitHelper(data);
}
public SplitTableContextResult<T> SplitHelper<T>(List<T> dataList)
public SplitTableContextResult<T> SplitHelper<T>(List<T> dataList) where T : class, new()
{
return this.Context.SplitHelper(dataList);
}

6
Src/Asp.Net/SqlSugar/SqlSugarScope.cs

@ -596,15 +596,15 @@ namespace SqlSugar
{
return ScopedContext.Updateable(UpdateObjs);
}
public SplitTableContext SplitHelper<T>()
public SplitTableContext SplitHelper<T>() where T : class, new()
{
return ScopedContext.SplitHelper<T>();
}
public SplitTableContextResult<T> SplitHelper<T>(T data)
public SplitTableContextResult<T> SplitHelper<T>(T data) where T : class, new()
{
return ScopedContext.SplitHelper(data);
}
public SplitTableContextResult<T> SplitHelper<T>(List<T> dataList)
public SplitTableContextResult<T> SplitHelper<T>(List<T> dataList) where T : class, new()
{
return ScopedContext.SplitHelper(dataList);
}

Loading…
Cancel
Save