mysql 添加测试代码

This commit is contained in:
xuejiaming 2023-03-16 18:23:23 +08:00
parent c59eeaa551
commit 8b7cb48fd1
5 changed files with 171 additions and 2 deletions

View File

@ -44,6 +44,18 @@ namespace Sample.MySql.Controllers
public virtual IQueryable<SysTest> Select => this.GetAll();
}
/// <summary>
/// 接口限速
/// </summary>
public class RateLimitAttribute : Attribute
{
public RateLimitAttribute(int limitCount = 1)
{
Console.WriteLine("------------RateLimitAttribute----------");
}
}
[ApiController]
[Route("[controller]/[action]")]
public class WeatherForecastController : ControllerBase
@ -71,6 +83,16 @@ namespace Sample.MySql.Controllers
shardingTableCreator.CreateTable<SysUserMod>("ds0","20220921");
return _defaultTableDbContext.Set<SysTest>();
}
[HttpGet]
public async Task<IActionResult> Getxx()
{
var test = new Test();
test.UtcTime=DateTime.Now;
await _defaultTableDbContext.AddAsync(test);
var saveChangesAsync = await _defaultTableDbContext.SaveChangesAsync();
return Ok(saveChangesAsync);
}
[HttpGet]
public async Task<IActionResult> Get()
{

View File

@ -18,6 +18,8 @@ namespace Sample.MySql.DbContexts
{
public DbSet<DynamicTable> DynamicTables { get; set; }
public DbSet<SysUserMod> SysUserMod { get; set; }
public DbSet<TestMod> TestMods { get; set; }
public DbSet<Test> Tests { get; set; }
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
{

View File

@ -0,0 +1,109 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.VirtualRoutes.Abstractions;
namespace Sample.MySql.Domain.Entities
{
[Table("test", Schema = "test")]
public class Test
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime UtcTime { get; set; }
}
public abstract class AbstractShardingQuarterKeyDateTimeVirtualTableRoute<TEntity> : AbstractShardingTimeKeyDateTimeVirtualTableRoute<TEntity> where TEntity : class
{
public override bool AutoCreateTableByTime()
{
return false;
}
public abstract DateTime GetBeginTime();
private static DateTime GetCurrentQuarterFirstDay([NotNull] DateTime time)
{
return time.Date.AddMonths(0 - (time.Month - 1) % 3).AddDays(1 - time.Day);
}
private static DateTime GetNextQuarterFirstDay([NotNull] DateTime time)
{
return GetCurrentQuarterFirstDay(time).AddMonths(3);
}
protected override List<string> CalcTailsOnStart()
{
var beginTime = GetCurrentQuarterFirstDay(GetBeginTime());
var tails = new List<string>();
//提前创建表
var nowTimeStamp = GetCurrentQuarterFirstDay(DateTime.Now);
if (beginTime > nowTimeStamp)
throw new ArgumentException("begin time error");
var currentTimeStamp = beginTime;
while (currentTimeStamp <= nowTimeStamp)
{
var tail = ShardingKeyToTail(currentTimeStamp);
tails.Add(tail);
currentTimeStamp = GetNextQuarterFirstDay(currentTimeStamp);
}
return tails;
}
protected override string TimeFormatToTail(DateTime time)
{
var quarter = (time.Month - 1) / 3 + 1;
return $"{time:yyyy}0{quarter}";
}
public override Func<string, bool> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail => string.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan:
{
var currentQuarter = GetCurrentQuarterFirstDay(shardingKey);
//处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回
if (currentQuarter == shardingKey)
return tail => string.Compare(tail, t, StringComparison.Ordinal) < 0;
return tail => string.Compare(tail, t, StringComparison.Ordinal) <= 0;
}
case ShardingOperatorEnum.LessThanOrEqual:
return tail => string.Compare(tail, t, StringComparison.Ordinal) <= 0;
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
Console.WriteLine($"shardingOperator is not equal scan all table tail");
return tail => true;
}
}
public override string[] GetCronExpressions()
{
return new[]
{
"0 59 23 28,29,30,31 1,4,7,10 ?",
"0 0 0 1 1,4,7,10 ?",
"0 1 0 1 1,4,7,10 ?",
};
}
public override string[] GetJobCronExpressions()
{
return base.GetJobCronExpressions().Concat(new[] { "0 0 0 1 1,4,7,10 ?" }).Distinct().ToArray();
}
}
public class TestTableRoute : AbstractShardingQuarterKeyDateTimeVirtualTableRoute<Test>
{
protected override bool EnableHintRoute => true;
public override DateTime GetBeginTime()
{
return new DateTime(2023, 1, 1);
}
public override void Configure(EntityMetadataTableBuilder<Test> builder)
{
builder.ShardingProperty(x => x.UtcTime);
}
}
}

View File

@ -0,0 +1,35 @@
using Sample.MySql.Domain.Entities;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions;
namespace Sample.MySql.Shardings
{
public class SysUserLogByMonthDSRoute:AbstractShardingOperatorVirtualDataSourceRoute<SysUserLogByMonth,DateTime>
{
public override string ShardingKeyToDataSourceName(object shardingKey)
{
throw new NotImplementedException();
}
public override List<string> GetAllDataSourceNames()
{
throw new NotImplementedException();
}
public override bool AddDataSourceName(string dataSourceName)
{
throw new NotImplementedException();
}
public override void Configure(EntityMetadataDataSourceBuilder<SysUserLogByMonth> builder)
{
throw new NotImplementedException();
}
public override Func<string, bool> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
{
throw new NotImplementedException();
}
}
}

View File

@ -79,6 +79,7 @@ namespace Sample.MySql
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
o.AddShardingTableRoute<TestModRoute>();
o.AddShardingTableRoute<TestModItemRoute>();
o.AddShardingTableRoute<TestTableRoute>();
o.AddParallelTableGroupNode(new ParallelTableGroupNode(new List<ParallelTableComparerType>()
{
new ParallelTableComparerType(typeof(TestMod)),
@ -105,7 +106,7 @@ namespace Sample.MySql
o.UseShardingQuery((conStr, builder) =>
{
builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
.UseLoggerFactory(efLogger)
.UseLoggerFactory(loggerFactory1)
.EnableSensitiveDataLogging();
//.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});
@ -113,7 +114,7 @@ namespace Sample.MySql
{
builder
.UseMySql(connection, new MySqlServerVersion(new Version()))
.UseLoggerFactory(efLogger)
.UseLoggerFactory(loggerFactory1)
.EnableSensitiveDataLogging();
//.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});