添加注释和部分异常说明

This commit is contained in:
xuejiaming 2024-07-22 11:22:24 +08:00
parent 64dc7d90e9
commit a2008bce88
5 changed files with 190 additions and 120 deletions

View File

@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
@ -56,12 +57,12 @@ namespace Sample.MySql.Controllers
/// </summary>
public class RateLimitAttribute : Attribute
{
public RateLimitAttribute(int limitCount = 1)
{
Console.WriteLine("------------RateLimitAttribute----------");
}
}
[ApiController]
[Route("[controller]/[action]")]
public class WeatherForecastController : ControllerBase
@ -73,7 +74,10 @@ namespace Sample.MySql.Controllers
private readonly IShardingRuntimeContext _shardingRuntimeContext;
private readonly ABC _abc;
public WeatherForecastController(IShardingRuntimeContext<DefaultShardingDbContext> defaultShardingRuntimeContext,IServiceProvider serviceProvider,UnShardingDbContext unShardingDbContext,DefaultShardingDbContext defaultTableDbContext,IShardingRuntimeContext shardingRuntimeContext)
public WeatherForecastController(
IShardingRuntimeContext<DefaultShardingDbContext> defaultShardingRuntimeContext,
IServiceProvider serviceProvider, UnShardingDbContext unShardingDbContext,
DefaultShardingDbContext defaultTableDbContext, IShardingRuntimeContext shardingRuntimeContext)
{
_defaultShardingRuntimeContext = defaultShardingRuntimeContext;
_serviceProvider = serviceProvider;
@ -83,25 +87,44 @@ namespace Sample.MySql.Controllers
_abc = new ABC(_defaultTableDbContext);
}
public (string id,string name___11) aaa()
{
var sql= from u in _defaultTableDbContext.Set<SysTest>()
where u.Id == "123"
select (id:u.Id,name___11:u.UserId);
return sql.FirstOrDefault();
}
public IQueryable<SysTest> GetAll()
{
var valueTuple = aaa();
// valueTuple.
var sql = from u in _defaultTableDbContext.Set<SysTest>()
where u.Id == "123"
select new
{
Id=u.Id,
Name_____1111=u.UserId
};
var sysTests = sql.Where(o=>o.Name_____1111=="456").ToList();
var dbContextOptionsBuilder = new DbContextOptionsBuilder<DefaultShardingDbContext>();
var dbContextOptionBuilderCreator = _shardingRuntimeContext.GetDbContextOptionBuilderCreator();
dbContextOptionsBuilder.UseDefaultSharding<DefaultShardingDbContext>(_shardingRuntimeContext);
using (var dbContext = new DefaultShardingDbContext(dbContextOptionsBuilder.Options))
{
}
foreach (var dataSourceDbContext in _defaultTableDbContext.GetShardingExecutor().GetCurrentDbContexts())
{
foreach (var keyValuePair in dataSourceDbContext.Value.GetCurrentContexts())
{
}
}
}
}
((IResettableService)_defaultTableDbContext).ResetState();
var dataSourceRouteManager = _shardingRuntimeContext.GetDataSourceRouteManager();
@ -131,13 +154,13 @@ namespace Sample.MySql.Controllers
[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()
{
@ -255,11 +278,15 @@ namespace Sample.MySql.Controllers
.Select(o => new ssss() { Id = o.Id, C = sysTests.Count(x => x.Id == o.Id) }).ToList();
var resultX = await _defaultTableDbContext.Set<SysUserMod>()
.Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync();
var resultY = await _defaultTableDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3");
var shardingFirstOrDefaultAsyncxxx = await _defaultTableDbContext.Set<SysUserLogByMonth>().Where(o=>o.Time==DateTime.Now).ToListAsync();
var resultY = await _defaultTableDbContext.Set<SysUserMod>()
.FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3");
var shardingFirstOrDefaultAsyncxxx = await _defaultTableDbContext.Set<SysUserLogByMonth>()
.Where(o => o.Time == DateTime.Now).ToListAsync();
var result = await _defaultTableDbContext.Set<SysTest>().AnyAsync();
var result22 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "2"&&o.Name=="ds1").ToListAsync();
var result1 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "2" || o.Id == "3").ToListAsync();
var result22 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "2" && o.Name == "ds1")
.ToListAsync();
var result1 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "2" || o.Id == "3")
.ToListAsync();
var result2 = await _defaultTableDbContext.Set<SysUserLogByMonth>().Skip(1).Take(10).ToListAsync();
var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set<SysUserLogByMonth>().ToListAsync();
var shardingCountAsync = await _defaultTableDbContext.Set<SysUserMod>().CountAsync();
@ -285,18 +312,24 @@ namespace Sample.MySql.Controllers
return Ok(1);
}
[HttpGet]
public async Task<IActionResult> Get1()
{
var resultX = await _defaultTableDbContext.Set<SysUserMod>()
.Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync();
Console.WriteLine("-----------------------------------------------------------------------------------------------------");
var resultY = await _defaultTableDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3");
Console.WriteLine("-----------------------------------------------------------------------------------------------------");
Console.WriteLine(
"-----------------------------------------------------------------------------------------------------");
var resultY = await _defaultTableDbContext.Set<SysUserMod>()
.FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3");
Console.WriteLine(
"-----------------------------------------------------------------------------------------------------");
var result = await _defaultTableDbContext.Set<SysTest>().AnyAsync();
Console.WriteLine("-----------------------------------------------------------------------------------------------------");
Console.WriteLine(
"-----------------------------------------------------------------------------------------------------");
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get2()
{
@ -314,6 +347,7 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get3()
{
@ -324,6 +358,7 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get4()
{
@ -336,6 +371,7 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get6()
{
@ -347,6 +383,7 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get7()
{
@ -358,6 +395,7 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get8()
{
@ -368,15 +406,18 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get9()
{
var sysUserMod = await _defaultTableDbContext.Set<SysUserMod>().Where(o=>o.Name=="11231").AllAsync(o=>o.Id=="1123");
var sysUserMod = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Name == "11231")
.AllAsync(o => o.Id == "1123");
// var sysUserMods1 = await _defaultTableDbContext.Set<SysUserMod>().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync();
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get10()
{
@ -384,10 +425,12 @@ namespace Sample.MySql.Controllers
var dataSourceDbContexts = shardingDbContextExecutor.GetCurrentDbContexts();
var sourceDbContexts = dataSourceDbContexts.Values;
var dataSourceDbContext = sourceDbContexts.FirstOrDefault();
var dbConnection = dataSourceDbContext.GetCurrentContexts().FirstOrDefault().Value.Database.GetDbConnection();
var dbConnection = dataSourceDbContext.GetCurrentContexts().FirstOrDefault().Value.Database
.GetDbConnection();
var sysUserMod1 = await _defaultTableDbContext.Set<SysTest>().Where(o=>o.UserId=="11231").AllAsync(o=>o.Id=="1123"&&string.Compare(o.UserId,"123")>0);
var sysUserMod1 = await _defaultTableDbContext.Set<SysTest>().Where(o => o.UserId == "11231")
.AllAsync(o => o.Id == "1123" && string.Compare(o.UserId, "123") > 0);
var sysUserMod2 = await _defaultTableDbContext.Set<SysTest>().AllAsync(o => o.Id == "1123");
var dateTime = new DateTime(2020, 1, 1);
var sysUserMod3 = await _defaultTableDbContext.Set<SysUserLogByMonth>().Where(o => o.Time > dateTime)
@ -397,6 +440,7 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get11()
{
@ -411,6 +455,7 @@ namespace Sample.MySql.Controllers
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get12()
{
@ -420,6 +465,7 @@ namespace Sample.MySql.Controllers
var tests = sysTests.ToList();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get13()
{
@ -469,7 +515,6 @@ namespace Sample.MySql.Controllers
}
[HttpGet]
public async Task<IActionResult> Get16()
{
@ -497,6 +542,7 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok(sysUserMods);
}
[HttpGet]
public async Task<IActionResult> Get18()
{
@ -511,11 +557,13 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok(sysUserMods);
}
[HttpGet]
public async Task<IActionResult> Get20()
{
var dateTime = new DateTime(2022, 1, 1);
var queryable = _defaultTableDbContext.Set<SysUserLogByMonth>().Where(o=>o.Time>dateTime).Select(o=>o.Id);
var queryable = _defaultTableDbContext.Set<SysUserLogByMonth>().Where(o => o.Time > dateTime)
.Select(o => o.Id);
var sysUserMods = await _defaultTableDbContext.SysUserMod.AsNoTracking()
.Where(o => queryable.Contains(o.Id)).ToListAsync();
@ -523,6 +571,7 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok(sysUserMods);
}
[HttpGet]
public async Task<IActionResult> Get211()
{
@ -556,11 +605,11 @@ namespace Sample.MySql.Controllers
public void get11()
{
var mySqlConnection = new MySqlConnection("链接字符串");
var dbContextOptions1 = new DbContextOptionsBuilder<UnShardingDbContext>().UseMySql(mySqlConnection,new MySqlServerVersion(new Version()),b=>
{
b.EnableRetryOnFailure(10, TimeSpan.FromSeconds(10), null);
}).Options;
var dbContextOptions2 = new DbContextOptionsBuilder<UnShardingDbContext>().UseMySql(mySqlConnection,new MySqlServerVersion(new Version())).Options;
var dbContextOptions1 = new DbContextOptionsBuilder<UnShardingDbContext>().UseMySql(mySqlConnection,
new MySqlServerVersion(new Version()),
b => { b.EnableRetryOnFailure(10, TimeSpan.FromSeconds(10), null); }).Options;
var dbContextOptions2 = new DbContextOptionsBuilder<UnShardingDbContext>()
.UseMySql(mySqlConnection, new MySqlServerVersion(new Version())).Options;
var unShardingDbContext1 = new UnShardingDbContext(dbContextOptions1); //映射到202301模型
var unShardingDbContext2 = new UnShardingDbContext(dbContextOptions2); //映射到202302模型
// unShardingDbContext2.Database.CreateExecutionStrategy()

View File

@ -9,6 +9,12 @@ namespace ShardingCore.Core.QueryTrackers
{
public interface IQueryTracker
{
/// <summary>
/// 追踪数据对象
/// </summary>
/// <param name="entity"></param>
/// <param name="shardingDbContext"></param>
/// <returns></returns>
public object Track(object entity,IShardingDbContext shardingDbContext);
}
}

View File

@ -30,7 +30,14 @@ namespace ShardingCore.Core.RuntimeContexts
public interface IShardingRuntimeContext
{
/// <summary>
/// 上下文类型
/// </summary>
Type DbContextType { get; }
/// <summary>
/// 模型锁提供者
/// </summary>
/// <returns></returns>
IModelCacheLockerProvider GetModelCacheLockerProvider();
IDbContextTypeAware GetDbContextTypeAware();
IShardingProvider GetShardingProvider();

View File

@ -6,6 +6,10 @@ using ShardingCore.Sharding.ShardingComparision.Abstractions;
namespace ShardingCore.Sharding.Internals
{
/// <summary>
/// 内存分片比较器
/// </summary>
/// <typeparam name="T"></typeparam>
public class InMemoryShardingComparer<T> : IComparer<T>
{
private readonly IShardingComparer _shardingComparer;

View File

@ -74,6 +74,10 @@ namespace ShardingCore.Sharding.MergeEngines.Enumerables
}
else
{
if (_tableSequenceOrderConfig == null)
{
throw new ShardingCoreException("_tableSequenceOrderConfig is null,plz config IPaginationConfiguration.");
}
var appendAsc = _tableSequenceOrderConfig.AppendAsc;
if (appendAsc)