提交测试代码

This commit is contained in:
xuejiaming 2022-05-29 21:27:24 +08:00
parent 5d3aa6dbc2
commit 78a59dd181
6 changed files with 73 additions and 12 deletions

View File

@ -0,0 +1,58 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Sample.AutoCreateIfPresent.Controllers;
[ApiController]
[Route("[controller]/[action]")]
public class TestController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private readonly DefaultDbContext _defaultDbContext;
public TestController(ILogger<WeatherForecastController> logger,DefaultDbContext defaultDbContext)
{
_logger = logger;
_defaultDbContext = defaultDbContext;
}
public IActionResult HelloWorld()
{
return Ok("hello world");
}
public async Task<IActionResult> Query()
{
var list =await _defaultDbContext.Set<OrderByHour>().ToListAsync();
return Ok(list);
}
public async Task<IActionResult> Insert()
{
var orderByHour = new OrderByHour();
orderByHour.Id = Guid.NewGuid().ToString("n");
orderByHour.Name=$"Name:"+ Guid.NewGuid().ToString("n");
var dateTime = DateTime.Now;
orderByHour.CreateTime = dateTime.AddHours(new Random().Next(1, 20));
await _defaultDbContext.AddAsync(orderByHour);
await _defaultDbContext.SaveChangesAsync();
return Ok();
}
public async Task<IActionResult> Query1()
{
var list = await _defaultDbContext.Set<AreaDevice>().ToListAsync();
return Ok(list);
}
public async Task<IActionResult> Insert1()
{
var list = new List<string>(){"A","B","C","D","E", "F", "G" };
var orderByHour = new AreaDevice();
orderByHour.Id = Guid.NewGuid().ToString("n");
orderByHour.Area = list[new Random().Next(0, list.Count)];
var dateTime = DateTime.Now;
orderByHour.CreateTime = dateTime.AddHours(new Random().Next(1, 20));
await _defaultDbContext.AddAsync(orderByHour);
await _defaultDbContext.SaveChangesAsync();
return Ok();
}
}

View File

@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

View File

@ -14,6 +14,7 @@ using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.TableCreator;
using ShardingCore.TableExists;
/*
* @Author: xjm
@ -43,6 +44,11 @@ namespace Sample.AutoCreateIfPresent
_shardingTableCreator = shardingTableCreator;
}
public override string ShardingKeyToTail(object shardingKey)
{
var dateTime = (DateTime)shardingKey;
return ShardingKeyFormat(dateTime);
}
private string ShardingKeyFormat(DateTime dateTime)
{
var tail = $"{dateTime:yyyyMMddHH}";
@ -50,12 +56,6 @@ namespace Sample.AutoCreateIfPresent
return tail;
}
public override string ShardingKeyToTail(object shardingKey)
{
var dateTime = (DateTime)shardingKey;
return ShardingKeyFormat(dateTime);
}
/// <summary>
/// 如果你是非mysql数据库请自行实现这个方法返回当前类在数据库已经存在的后缀
/// 仅启动时调用
@ -134,6 +134,7 @@ namespace Sample.AutoCreateIfPresent
if (!_tails.TryGetValue(shardingKeyToTail,out var _))
{
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(OrderByHour));
//必须先执行AddPhysicTable在进行CreateTable
_virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, shardingKeyToTail));
try
{

View File

@ -16,8 +16,8 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// builder.Services.AddEndpointsApiExplorer();
// builder.Services.AddSwaggerGen();
builder.Services.AddShardingDbContext<DefaultDbContext>()
.AddEntityConfig(o =>
{
@ -30,7 +30,7 @@ builder.Services.AddShardingDbContext<DefaultDbContext>()
.AddConfig(o =>
{
o.ConfigId = "c1";
o.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=shardingTest;userid=root;password=L6yBtV6qNENrwBy7;");
o.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=shardingTest;userid=root;password=root;");
o.UseShardingQuery((conn, b) =>
{
b.UseMySql(conn, new MySqlServerVersion(new Version())).UseLoggerFactory(efLogger);
@ -46,8 +46,8 @@ var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
// app.UseSwagger();
// app.UseSwaggerUI();
}
app.Services.GetRequiredService<IShardingBootstrapper>().Start();

View File

@ -39,7 +39,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes
/// <returns></returns>
IPhysicTable RouteWithValue(List<IPhysicTable> allPhysicTables, object shardingKey);
/// <summary>
/// 获取所有的目前数据库存在的尾巴
/// 获取所有的目前数据库存在的尾巴,仅启动时调用
/// get all tails in the db
/// </summary>
/// <returns></returns>

View File

@ -77,6 +77,7 @@ namespace ShardingCore.VirtualRoutes.Abstractions
var allVirtualDataSources = virtualDataSourceManager.GetAllVirtualDataSources();
var now = DateTime.Now.AddMinutes(IncrementMinutes);
var tail = ConvertNowToTail(now);
//必须先执行AddPhysicTable在进行CreateTable
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
foreach (var virtualDataSource in allVirtualDataSources)
{