Skip to content

Commit

Permalink
- 增加 IAsTable 自动分表支持不规则时间间隔;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Nov 22, 2023
1 parent 14b15a8 commit 5260c85
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 161 deletions.
269 changes: 118 additions & 151 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class A
public B B { get; set; }
}

[Table(Name = "as_table_log_{yyyyMMddHH}", AsTable = "createtime=2022-1-1 11(1 month)")]
[Table(Name = "as_table_log_{yyyyMMddHH}", AsTable = "createtime=2022-1-1 11(12,1 month)")]
class AsTableLog
{
public Guid id { get; set; }
Expand Down Expand Up @@ -589,7 +589,7 @@ static void Main(string[] args)
//.UseConnectionString(DataType.ClickHouse, "DataCompress=False;BufferSize=32768;SocketTimeout=10000;CheckCompressedHash=False;Encrypt=False;Compressor=lz4;Host=192.168.0.121;Port=8125;Database=PersonnelLocation;Username=root;Password=123")
.UseMonitorCommand(cmd =>
{
//Console.WriteLine(cmd.CommandText + "\r\n");
Console.WriteLine(cmd.CommandText + "\r\n");
//cmd.CommandText = null; //不执行

//if (cmd.CommandText.StartsWith(""))
Expand All @@ -599,43 +599,122 @@ static void Main(string[] args)
.Build();
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion
fsql.Select<User1>().First();
var running = true;
for (var a = 0; a < 100; a++)
{
new Thread(() =>
{
while (running)
{
try
{
fsql.Select<User1>().First();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Task.Run(async () =>
{
try
{
await fsql.Select<User1>().FirstAsync();
}
catch (Exception ex)
{
Console.WriteLine("Async:" + ex.Message);
}
});
}
}).Start();
}

Console.ReadKey();
running = false;
fsql.Dispose();
return;

var x01sql01 = fsql.Select<Main1>()
fsql.Select<AsTableLog>().Where(a => a.createtime > DateTime.Now && a.createtime < DateTime.Now.AddMonths(1)).ToList();
//var table = fsql.CodeFirst.GetTableByEntity(typeof(AsTableLog));
//table.SetAsTable(new ModAsTableImpl(fsql), table.ColumnsByCs[nameof(AsTableLog.click)]);

var testitems = new[]
{
new AsTableLog{ msg = "msg01", createtime = DateTime.Parse("2022-1-1 13:00:11"), click = 1 },
new AsTableLog{ msg = "msg02", createtime = DateTime.Parse("2022-1-2 14:00:12"), click = 2 },
new AsTableLog{ msg = "msg03", createtime = DateTime.Parse("2022-2-2 15:00:13"), click = 3 },
new AsTableLog{ msg = "msg04", createtime = DateTime.Parse("2022-2-8 15:00:13"), click = 4 },
new AsTableLog{ msg = "msg05", createtime = DateTime.Parse("2022-3-8 15:00:13"), click = 5 },
new AsTableLog{ msg = "msg06", createtime = DateTime.Parse("2022-4-8 15:00:13"), click = 6 },
new AsTableLog{ msg = "msg07", createtime = DateTime.Parse("2022-6-8 15:00:13"), click = 7 },
new AsTableLog{ msg = "msg08", createtime = DateTime.Parse("2022-7-1"), click = 9},
new AsTableLog{ msg = "msg09", createtime = DateTime.Parse("2022-7-1 11:00:00"), click = 10},
new AsTableLog{ msg = "msg10", createtime = DateTime.Parse("2022-7-1 12:00:00"), click = 10}
};
var sqlatb = fsql.Insert(testitems).NoneParameter();
var sqlat = sqlatb.ToSql();
var sqlatr = sqlatb.ExecuteAffrows();

//var sqlatc1 = fsql.Delete<AsTableLog>().Where(a => a.click < 0);
//var sqlatca1 = sqlatc1.ToSql();
//var sqlatcr1 = sqlatc1.ExecuteAffrows();

var sqlatc1 = fsql.Delete<AsTableLog>().Where(a => a.id == Guid.NewGuid() && a.createtime == DateTime.Parse("2022-3-8 15:00:13"));
var sqlatca1 = sqlatc1.ToSql();
var sqlatcr1 = sqlatc1.ExecuteAffrows();

var sqlatc2 = fsql.Delete<AsTableLog>().Where(a => a.id == Guid.NewGuid() && a.createtime == DateTime.Parse("2021-3-8 15:00:13"));
var sqlatca2 = sqlatc2.ToSql();
var sqlatcr2 = sqlatc2.ExecuteAffrows();

var sqlatc = fsql.Delete<AsTableLog>().Where(a => a.id == Guid.NewGuid() && a.createtime.Between(DateTime.Parse("2022-3-1"), DateTime.Parse("2022-5-1")));
var sqlatca = sqlatc.ToSql();
var sqlatcr = sqlatc.ExecuteAffrows();

var sqlatd1 = fsql.Update<AsTableLog>().SetSource(testitems[0]);
var sqlatd101 = sqlatd1.ToSql();
var sqlatd102 = sqlatd1.ExecuteAffrows();

var sqlatd2 = fsql.Update<AsTableLog>().SetSource(testitems[5]);
var sqlatd201 = sqlatd2.ToSql();
var sqlatd202 = sqlatd2.ExecuteAffrows();

var sqlatd3 = fsql.Update<AsTableLog>().SetSource(testitems);
var sqlatd301 = sqlatd3.ToSql();
var sqlatd302 = sqlatd3.ExecuteAffrows();

var sqlatd4 = fsql.Update<AsTableLog>(Guid.NewGuid()).Set(a => a.msg == "newmsg");
var sqlatd401 = sqlatd4.ToSql();
var sqlatd402 = sqlatd4.ExecuteAffrows();

var sqlatd5 = fsql.Update<AsTableLog>(Guid.NewGuid()).Set(a => a.msg == "newmsg").Where(a => a.createtime.Between(DateTime.Parse("2022-3-1"), DateTime.Parse("2022-5-1")));
var sqlatd501 = sqlatd5.ToSql();
var sqlatd502 = sqlatd5.ExecuteAffrows();

var sqlatd6 = fsql.Update<AsTableLog>(Guid.NewGuid()).Set(a => a.msg == "newmsg").Where(a => a.createtime > DateTime.Parse("2022-3-1") && a.createtime < DateTime.Parse("2022-5-1"));
var sqlatd601 = sqlatd6.ToSql();
var sqlatd602 = sqlatd6.ExecuteAffrows();

var sqlatd7 = fsql.Update<AsTableLog>(Guid.NewGuid()).Set(a => a.msg == "newmsg").Where(a => a.createtime > DateTime.Parse("2022-3-1"));
var sqlatd701 = sqlatd7.ToSql();
var sqlatd702 = sqlatd7.ExecuteAffrows();

var sqlatd8 = fsql.Update<AsTableLog>(Guid.NewGuid()).Set(a => a.msg == "newmsg").Where(a => a.createtime < DateTime.Parse("2022-5-1"));
var sqlatd801 = sqlatd8.ToSql();
var sqlatd802 = sqlatd8.ExecuteAffrows();

var sqlatd12 = fsql.InsertOrUpdate<AsTableLog>().SetSource(testitems[0]);
var sqlatd1201 = sqlatd12.ToSql();
var sqlatd1202 = sqlatd12.ExecuteAffrows();

var sqlatd22 = fsql.InsertOrUpdate<AsTableLog>().SetSource(testitems[5]);
var sqlatd2201 = sqlatd22.ToSql();
var sqlatd2202 = sqlatd22.ExecuteAffrows();

var sqlatd32 = fsql.InsertOrUpdate<AsTableLog>().SetSource(testitems);
var sqlatd3201 = sqlatd32.ToSql();
var sqlatd3202 = sqlatd32.ExecuteAffrows();

var sqls1 = fsql.Select<AsTableLog>().OrderBy(a => a.createtime).Limit(10);
var sqls101 = sqls1.ToSql();
var sqls102 = sqls1.ToList();

var sqls2 = fsql.Select<AsTableLog>().Where(a => a.createtime.Between(DateTime.Parse("2022-3-1"), DateTime.Parse("2022-5-1")));
var sqls201 = sqls2.ToSql();
var sqls202 = sqls2.ToList();

var sqls3 = fsql.Select<AsTableLog>().Where(a => a.createtime > DateTime.Parse("2022-3-1") && a.createtime < DateTime.Parse("2022-5-1"));
var sqls301 = sqls3.ToSql();
var sqls302 = sqls3.ToList();

var sqls4 = fsql.Select<AsTableLog>().Where(a => a.createtime > DateTime.Parse("2022-3-1"));
var sqls401 = sqls4.ToSql();
var sqls402 = sqls4.ToList();

var sqls5 = fsql.Select<AsTableLog>().Where(a => a.createtime < DateTime.Parse("2022-5-1"));
var sqls501 = sqls5.ToSql();
var sqls502 = sqls5.ToList();

var sqls6 = fsql.Select<AsTableLog>().Where(a => a.createtime < DateTime.Parse("2022-5-1")).Limit(10).OrderBy(a => a.createtime);
var sqls601 = sqls6.ToSql();
var sqls602 = sqls6.ToList();

var sqls7 = fsql.Select<AsTableLog>().Where(a => a.createtime < DateTime.Parse("2022-5-1")).ToAggregate(g => new
{
sum1 = g.Sum(g.Key.click),
cou1 = g.Count(),
avg1 = g.Avg(g.Key.click),
min = g.Min(g.Key.click),
max = g.Max(g.Key.click)
});

var x01sql01 = fsql.Select<Main1>()
.Include(a => a.Test1)
.Include(a => a.Test2)
.Include(a => a.Test3)
Expand Down Expand Up @@ -763,7 +842,6 @@ static void Main(string[] args)
title = "one plus pro"
}.Save();

fsql.Select<AsTableLog>().Where(a => a.createtime > DateTime.Now && a.createtime < DateTime.Now.AddMonths(1)).ToList();

using (var uow = fsql.CreateUnitOfWork())
{
Expand Down Expand Up @@ -914,118 +992,7 @@ static void Main(string[] args)
.ToSql();


//var table = fsql.CodeFirst.GetTableByEntity(typeof(AsTableLog));
//table.SetAsTable(new ModAsTableImpl(fsql), table.ColumnsByCs[nameof(AsTableLog.click)]);

var testitems = new[]
{
new AsTableLog{ msg = "msg01", createtime = DateTime.Parse("2022-1-1 13:00:11"), click = 1 },
new AsTableLog{ msg = "msg02", createtime = DateTime.Parse("2022-1-2 14:00:12"), click = 2 },
new AsTableLog{ msg = "msg03", createtime = DateTime.Parse("2022-2-2 15:00:13"), click = 3 },
new AsTableLog{ msg = "msg04", createtime = DateTime.Parse("2022-2-8 15:00:13"), click = 4 },
new AsTableLog{ msg = "msg05", createtime = DateTime.Parse("2022-3-8 15:00:13"), click = 5 },
new AsTableLog{ msg = "msg06", createtime = DateTime.Parse("2022-4-8 15:00:13"), click = 6 },
new AsTableLog{ msg = "msg07", createtime = DateTime.Parse("2022-6-8 15:00:13"), click = 7 },
new AsTableLog{ msg = "msg08", createtime = DateTime.Parse("2022-7-1"), click = 9},
new AsTableLog{ msg = "msg09", createtime = DateTime.Parse("2022-7-1 11:00:00"), click = 10},
new AsTableLog{ msg = "msg10", createtime = DateTime.Parse("2022-7-1 12:00:00"), click = 10}
};
var sqlatb = fsql.Insert(testitems).NoneParameter();
var sqlat = sqlatb.ToSql();
var sqlatr = sqlatb.ExecuteAffrows();

//var sqlatc1 = fsql.Delete<AsTableLog>().Where(a => a.click < 0);
//var sqlatca1 = sqlatc1.ToSql();
//var sqlatcr1 = sqlatc1.ExecuteAffrows();

var sqlatc1 = fsql.Delete<AsTableLog>().Where(a => a.id == Guid.NewGuid() && a.createtime == DateTime.Parse("2022-3-8 15:00:13"));
var sqlatca1 = sqlatc1.ToSql();
var sqlatcr1 = sqlatc1.ExecuteAffrows();

var sqlatc2 = fsql.Delete<AsTableLog>().Where(a => a.id == Guid.NewGuid() && a.createtime == DateTime.Parse("2021-3-8 15:00:13"));
var sqlatca2 = sqlatc2.ToSql();
var sqlatcr2 = sqlatc2.ExecuteAffrows();

var sqlatc = fsql.Delete<AsTableLog>().Where(a => a.id == Guid.NewGuid() && a.createtime.Between(DateTime.Parse("2022-3-1"), DateTime.Parse("2022-5-1")));
var sqlatca = sqlatc.ToSql();
var sqlatcr = sqlatc.ExecuteAffrows();

var sqlatd1 = fsql.Update<AsTableLog>().SetSource(testitems[0]);
var sqlatd101 = sqlatd1.ToSql();
var sqlatd102 = sqlatd1.ExecuteAffrows();

var sqlatd2 = fsql.Update<AsTableLog>().SetSource(testitems[5]);
var sqlatd201 = sqlatd2.ToSql();
var sqlatd202 = sqlatd2.ExecuteAffrows();

var sqlatd3 = fsql.Update<AsTableLog>().SetSource(testitems);
var sqlatd301 = sqlatd3.ToSql();
var sqlatd302 = sqlatd3.ExecuteAffrows();

var sqlatd4 = fsql.Update<AsTableLog>(Guid.NewGuid()).Set(a => a.msg == "newmsg");
var sqlatd401 = sqlatd4.ToSql();
var sqlatd402 = sqlatd4.ExecuteAffrows();

var sqlatd5 = fsql.Update<AsTableLog>(Guid.NewGuid()).Set(a => a.msg == "newmsg").Where(a => a.createtime.Between(DateTime.Parse("2022-3-1"), DateTime.Parse("2022-5-1")));
var sqlatd501 = sqlatd5.ToSql();
var sqlatd502 = sqlatd5.ExecuteAffrows();

var sqlatd6 = fsql.Update<AsTableLog>(Guid.NewGuid()).Set(a => a.msg == "newmsg").Where(a => a.createtime > DateTime.Parse("2022-3-1") && a.createtime < DateTime.Parse("2022-5-1"));
var sqlatd601 = sqlatd6.ToSql();
var sqlatd602 = sqlatd6.ExecuteAffrows();

var sqlatd7 = fsql.Update<AsTableLog>(Guid.NewGuid()).Set(a => a.msg == "newmsg").Where(a => a.createtime > DateTime.Parse("2022-3-1"));
var sqlatd701 = sqlatd7.ToSql();
var sqlatd702 = sqlatd7.ExecuteAffrows();

var sqlatd8 = fsql.Update<AsTableLog>(Guid.NewGuid()).Set(a => a.msg == "newmsg").Where(a => a.createtime < DateTime.Parse("2022-5-1"));
var sqlatd801 = sqlatd8.ToSql();
var sqlatd802 = sqlatd8.ExecuteAffrows();

var sqlatd12 = fsql.InsertOrUpdate<AsTableLog>().SetSource(testitems[0]);
var sqlatd1201 = sqlatd12.ToSql();
var sqlatd1202 = sqlatd12.ExecuteAffrows();

var sqlatd22 = fsql.InsertOrUpdate<AsTableLog>().SetSource(testitems[5]);
var sqlatd2201 = sqlatd22.ToSql();
var sqlatd2202 = sqlatd22.ExecuteAffrows();

var sqlatd32 = fsql.InsertOrUpdate<AsTableLog>().SetSource(testitems);
var sqlatd3201 = sqlatd32.ToSql();
var sqlatd3202 = sqlatd32.ExecuteAffrows();

var sqls1 = fsql.Select<AsTableLog>().OrderBy(a => a.createtime);
var sqls101 = sqls1.ToSql();
var sqls102 = sqls1.ToList();

var sqls2 = fsql.Select<AsTableLog>().Where(a => a.createtime.Between(DateTime.Parse("2022-3-1"), DateTime.Parse("2022-5-1")));
var sqls201 = sqls2.ToSql();
var sqls202 = sqls2.ToList();

var sqls3 = fsql.Select<AsTableLog>().Where(a => a.createtime > DateTime.Parse("2022-3-1") && a.createtime < DateTime.Parse("2022-5-1"));
var sqls301 = sqls3.ToSql();
var sqls302 = sqls3.ToList();

var sqls4 = fsql.Select<AsTableLog>().Where(a => a.createtime > DateTime.Parse("2022-3-1"));
var sqls401 = sqls4.ToSql();
var sqls402 = sqls4.ToList();

var sqls5 = fsql.Select<AsTableLog>().Where(a => a.createtime < DateTime.Parse("2022-5-1"));
var sqls501 = sqls5.ToSql();
var sqls502 = sqls5.ToList();

var sqls6 = fsql.Select<AsTableLog>().Where(a => a.createtime < DateTime.Parse("2022-5-1")).Limit(10).OrderBy(a => a.createtime);
var sqls601 = sqls6.ToSql();
var sqls602 = sqls6.ToList();

var sqls7 = fsql.Select<AsTableLog>().Where(a => a.createtime < DateTime.Parse("2022-5-1")).ToAggregate(g => new
{
sum1 = g.Sum(g.Key.click),
cou1 = g.Count(),
avg1 = g.Avg(g.Key.click),
min = g.Min(g.Key.click),
max = g.Max(g.Key.click)
});



var iouSetSql01 = fsql.InsertOrUpdate<User1>()
Expand Down
22 changes: 12 additions & 10 deletions FreeSql/DataAnnotations/TableAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal void ParseAsTable(TableInfo tb)
{
if (string.IsNullOrEmpty(AsTable) == false)
{
var atm = Regex.Match(AsTable, @"([\w_\d]+)\s*=\s*(\d\d\d\d)\s*\-\s*(\d\d?)\s*\-\s*(\d\d?)\s*( [\d:]+)?\((\d+)\s*(year|month|day|hour)\)", RegexOptions.IgnoreCase);
var atm = Regex.Match(AsTable, @"([\w_\d]+)\s*=\s*(\d\d\d\d)\s*\-\s*(\d\d?)\s*\-\s*(\d\d?)\s*( [\d:]+)?\(([\d,]+)\s*(year|month|day|hour)\)", RegexOptions.IgnoreCase);
if (atm.Success == false)
throw new Exception(CoreStrings.AsTable_PropertyName_FormatError(AsTable));

Expand All @@ -65,11 +65,12 @@ internal void ParseAsTable(TableInfo tb)
else if (hhmmss.Length == 2) beginTime = $"{beginTime} {hhmmss[0]}:{hhmmss[1]}:0";
else if (hhmmss.Length == 3) beginTime = $"{beginTime} {hhmmss[0]}:{hhmmss[1]}:{hhmmss[2]}";
}
int.TryParse(atm.Groups[6].Value, out var atm6);
var intervals = atm.Groups[6].Value.Split(',').Select(a => int.TryParse(a, out var atm6) ? atm6 : 0).Where(a => a > 0).ToArray();
string atm7 = atm.Groups[7].Value.ToLower();
tb.AsTableImpl = new DateTimeAsTableImpl(Name, DateTime.Parse(beginTime), dt =>
tb.AsTableImpl = new DateTimeAsTableImpl(Name, DateTime.Parse(beginTime), (dt, idx) =>
{
switch (atm7)
var atm6 = idx >= 0 && idx < intervals.Length ? intervals[idx] : intervals.Last();
switch (atm7)
{
case "year": return dt.AddYears(atm6);
case "month": return dt.AddMonths(atm6);
Expand All @@ -89,19 +90,19 @@ public interface IAsTable
string[] GetTableNamesByColumnValueRange(object columnValue1, object columnValue2);
string[] GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, SelectTableInfo tb, CommonUtils commonUtils);
}
class DateTimeAsTableImpl : IAsTable
class DateTimeAsTableImpl : IAsTable
{
readonly object _lock = new object();
readonly List<string> _allTables = new List<string>();
readonly List<DateTime> _allTablesTime = new List<DateTime>();
readonly DateTime _beginTime;
DateTime _lastTime;
Func<DateTime, DateTime> _nextTimeFunc;
Func<DateTime, int, DateTime> _nextTimeFunc;
string _tableName;
Match _tableNameFormat;
static Regex _regTableNameFormat = new Regex(@"\{([^\\}]+)\}");

public DateTimeAsTableImpl(string tableName, DateTime beginTime, Func<DateTime, DateTime> nextTimeFunc)
public DateTimeAsTableImpl(string tableName, DateTime beginTime, Func<DateTime, int, DateTime> nextTimeFunc)
{
if (nextTimeFunc == null) throw new ArgumentException(CoreStrings.Cannot_Be_NULL_Name("nextTimeFunc"));
//beginTime = beginTime.Date; //日期部分作为开始
Expand All @@ -116,7 +117,8 @@ public DateTimeAsTableImpl(string tableName, DateTime beginTime, Func<DateTime,
int GetTimestamp(DateTime dt) => (int)dt.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
void ExpandTable(DateTime beginTime, DateTime endTime)
{
if (beginTime > endTime) endTime = _nextTimeFunc(beginTime);
var index = 0;
if (beginTime > endTime) endTime = _nextTimeFunc(beginTime, -1);
lock (_lock)
{
while (beginTime <= endTime)
Expand All @@ -127,7 +129,7 @@ void ExpandTable(DateTime beginTime, DateTime endTime)
_allTables.Insert(0, name);
_allTablesTime.Insert(0, beginTime);
_lastTime = beginTime;
beginTime = _nextTimeFunc(beginTime);
beginTime = _nextTimeFunc(beginTime, index++);
}
}
}
Expand All @@ -153,7 +155,7 @@ public string GetTableNameByColumnValue(object columnValue, bool autoExpand = fa
{
var dt = ParseColumnValue(columnValue);
if (dt < _beginTime) throw new Exception(CoreStrings.SubTableFieldValue_CannotLessThen(dt.ToString("yyyy-MM-dd HH:mm:ss"), _beginTime.ToString("yyyy-MM-dd HH:mm:ss")));
var tmpTime = _nextTimeFunc(_lastTime);
var tmpTime = _nextTimeFunc(_lastTime, -1);
if (dt >= tmpTime && autoExpand)
{
// 自动建表
Expand Down
Loading

0 comments on commit 5260c85

Please sign in to comment.