Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jan 7, 2025
2 parents 3d6b41b + bced16f commit df7337b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
7 changes: 6 additions & 1 deletion XCode/Cache/FieldCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ private void Init()
}
}

private IList<TEntity> Search() => Entity<TEntity>.FindAll(Where?.GroupBy(_field), OrderBy, _Unique.Count("group_count")! & _field, 0, MaxRows);
private IList<TEntity> Search()
{
Expression? exp = Where?.GroupBy(_field);
exp ??= _field.GroupBy();
return Entity<TEntity>.FindAll(exp, OrderBy, _Unique.Count("group_count")! & _field, 0, MaxRows);
}

private IDictionary<String, String> GetAll()
{
Expand Down
4 changes: 2 additions & 2 deletions XCode/Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,9 @@ public override Boolean Valid(DataMethod method)
if (uk == null || this[uk.Name] is String str2 && str2.Length > 50) uk = Meta.Unique;

if (uk != null)
throw new ArgumentOutOfRangeException(fi.Name, $"[{fi.Name}/{fi.DisplayName}]长度限制{fi.Length}字符[{uk.Name}={this[uk.Name]}]");
throw new ArgumentOutOfRangeException(fi.Name, $"[{fi.Name}/{fi.DisplayName}@{factory.TableName}]长度限制{fi.Length}字符[{uk.Name}={this[uk.Name]}]");
else
throw new ArgumentOutOfRangeException(fi.Name, $"[{fi.Name}/{fi.DisplayName}]长度限制{fi.Length}字符");
throw new ArgumentOutOfRangeException(fi.Name, $"[{fi.Name}/{fi.DisplayName}@{factory.TableName}]长度限制{fi.Length}字符");
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions XCode/Entity/EntityExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ public static Int32 BatchUpdate<T>(this IEnumerable<T> list, BatchOption? option
/// 简单来说:对于一行记录,如果Insert 成功则返回1,如果需要执行的是update 则返回2
/// Oracle返回值:无论是插入还是更新返回的都始终为-1
/// </returns>
public static Int32 Upsert<T>(this IEnumerable<T> list, IDataColumn[]? columns, ICollection<String>? updateColumns = null, ICollection<String>? addColumns = null, IEntitySession? session = null) where T : IEntity
public static Int32 Upsert<T>(this IEnumerable<T> list, IDataColumn[]? columns = null, ICollection<String>? updateColumns = null, ICollection<String>? addColumns = null, IEntitySession? session = null) where T : IEntity
{
var option = new BatchOption(columns, updateColumns, addColumns);
return BatchUpsert(list, option, session);
Expand Down Expand Up @@ -951,7 +951,7 @@ public static Int32 BatchUpsert<T>(this IEnumerable<T> list, BatchOption? option
/// do update success =2次(insert 1次+update 1次),
/// 简单来说:如果Insert 成功则返回1,如果需要执行的是update 则返回2,
/// </returns>
public static Int32 Upsert(this IEntity entity, IDataColumn[]? columns, ICollection<String>? updateColumns = null, ICollection<String>? addColumns = null, IEntitySession? session = null)
public static Int32 Upsert(this IEntity entity, IDataColumn[]? columns = null, ICollection<String>? updateColumns = null, ICollection<String>? addColumns = null, IEntitySession? session = null)
{
var option = new BatchOption(columns, updateColumns, addColumns);
return Upsert(entity, option, session);
Expand Down
44 changes: 37 additions & 7 deletions XUnitTest.XCode/EntityTests/ShardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public void 跨年Shards()
};

var start = "2023-12-31".ToDateTime();
var end = DateTime.Today;
var end = start.AddDays(300);
var fi = policy.Field;
var where = fi >= start & fi < end;

Expand Down Expand Up @@ -526,18 +526,48 @@ public void 单日Shards()
{
var policy = new TimeShardPolicy(Log2._.CreateTime, Log2.Meta.Factory)
{
TablePolicy = "{0}_{1:yyyyMM}",
Step = TimeSpan.FromDays(31),
TablePolicy = "{0}_{1:yyyyMMdd}",
Step = TimeSpan.FromDays(1),
};

// 起止都是整数日期,末尾加1天
var start = "2024/7/25 00:00:00".ToDateTime();
var start = "2024/7/30 00:00:00".ToDateTime();
var end = start.AddDays(3);
var fi = policy.Field;
var where = fi.Equal(start);
//var where = fi.Equal(start);
var where = fi.Between(start, end);

var shards = policy.Shards(where);
Assert.NotNull(shards);
Assert.Equal(1, shards.Length);
Assert.Equal("Log2_202407", shards[0].TableName);
Assert.Equal(4, shards.Length);
Assert.Equal("Log2_20240730", shards[0].TableName);
Assert.Equal("Log2_20240731", shards[1].TableName);
Assert.Equal("Log2_20240801", shards[2].TableName);
Assert.Equal("Log2_20240802", shards[3].TableName);
}

[Fact]
public void 循环天表Shards()
{
var policy = new TimeShardPolicy(Log2._.CreateTime, Log2.Meta.Factory)
{
TablePolicy = "{0}_{1:dd}",
Step = TimeSpan.FromDays(1),
};

// 起止都是整数日期,末尾加1天
var start = "2024/7/30 00:00:00".ToDateTime();
var end = start.AddDays(3);
var fi = policy.Field;
//var where = fi.Equal(start);
var where = fi.Between(start, end);

var shards = policy.Shards(where);
Assert.NotNull(shards);
Assert.Equal(4, shards.Length);
Assert.Equal("Log2_30", shards[0].TableName);
Assert.Equal("Log2_31", shards[1].TableName);
Assert.Equal("Log2_01", shards[2].TableName);
Assert.Equal("Log2_02", shards[3].TableName);
}
}
2 changes: 1 addition & 1 deletion XUnitTest.XCode/Membership/UserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void StringLength()
var user = new User { Name = Rand.NextString(64) };
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => user.Insert());
Assert.Equal("Name", ex.ParamName);
Assert.Equal("[Name/名称]长度限制50字符[ID=0] (Parameter 'Name')", ex.Message);
Assert.Equal("[Name/名称@User]长度限制50字符[ID=0] (Parameter 'Name')", ex.Message);
}

[Fact]
Expand Down

0 comments on commit df7337b

Please sign in to comment.