mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Update TDengine
This commit is contained in:
parent
6bde63bb39
commit
88eb91b717
@ -12,6 +12,8 @@ namespace SqlSugar
|
|||||||
internal IInsertable<T> thisValue;
|
internal IInsertable<T> thisValue;
|
||||||
internal Func<string, T, string> getChildTableNamefunc;
|
internal Func<string, T, string> getChildTableNamefunc;
|
||||||
|
|
||||||
|
internal SqlSugarProvider Context;
|
||||||
|
|
||||||
public int ExecuteCommand()
|
public int ExecuteCommand()
|
||||||
{
|
{
|
||||||
var provider = (InsertableProvider<T>)thisValue;
|
var provider = (InsertableProvider<T>)thisValue;
|
||||||
@ -19,6 +21,44 @@ namespace SqlSugar
|
|||||||
var attr = typeof(T).GetCustomAttribute<STableAttribute>();
|
var attr = typeof(T).GetCustomAttribute<STableAttribute>();
|
||||||
Check.ExceptionEasy(attr == null || attr?.Tag1 == null, $"", $"{nameof(T)}缺少特性STableAttribute和Tag1");
|
Check.ExceptionEasy(attr == null || attr?.Tag1 == null, $"", $"{nameof(T)}缺少特性STableAttribute和Tag1");
|
||||||
// 根据所有非空的 Tag 进行分组
|
// 根据所有非空的 Tag 进行分组
|
||||||
|
var groups = GetGroupInfos(inserObjects, attr);
|
||||||
|
foreach (var item in groups)
|
||||||
|
{
|
||||||
|
var childTableName = getChildTableNamefunc(attr.STableName, item.First());
|
||||||
|
this.Context.Utilities.PageEach(item, 500, pageItems =>
|
||||||
|
{
|
||||||
|
var sTableName = provider.SqlBuilder.GetTranslationColumnName(attr.STableName);
|
||||||
|
var tags = new List<string>();
|
||||||
|
List<string> tagValues = GetTagValues(pageItems, attr);
|
||||||
|
var tagString = string.Join(",", tagValues.Where(v => !string.IsNullOrEmpty(v)).Select(v => $"'{v.ToSqlFilter()}'"));
|
||||||
|
tags.Add(tagString);
|
||||||
|
this.Context.Ado.ExecuteCommand($"CREATE TABLE IF NOT EXISTS {childTableName} USING {sTableName} TAGS ({tagString})");
|
||||||
|
this.Context.Insertable(pageItems).AS(childTableName).ExecuteCommand();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return inserObjects.Count();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<string> GetTagValues(List<T> pageItems, STableAttribute attr)
|
||||||
|
{
|
||||||
|
var tagValues = new List<string>();
|
||||||
|
var obj = pageItems.First();
|
||||||
|
if (attr.Tag1 != null)
|
||||||
|
tagValues.Add(obj.GetType().GetProperty(attr.Tag1)?.GetValue(obj)?.ToString());
|
||||||
|
|
||||||
|
if (attr.Tag2 != null)
|
||||||
|
tagValues.Add(obj.GetType().GetProperty(attr.Tag2)?.GetValue(obj)?.ToString());
|
||||||
|
|
||||||
|
if (attr.Tag3 != null)
|
||||||
|
tagValues.Add(obj.GetType().GetProperty(attr.Tag3)?.GetValue(obj)?.ToString());
|
||||||
|
|
||||||
|
if (attr.Tag4 != null)
|
||||||
|
tagValues.Add(obj.GetType().GetProperty(attr.Tag4)?.GetValue(obj)?.ToString());
|
||||||
|
return tagValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<IGrouping<string, T>> GetGroupInfos(T[] inserObjects, STableAttribute? attr)
|
||||||
|
{
|
||||||
var groups = inserObjects.GroupBy(it =>
|
var groups = inserObjects.GroupBy(it =>
|
||||||
{
|
{
|
||||||
// 动态生成分组键
|
// 动态生成分组键
|
||||||
@ -39,7 +79,7 @@ namespace SqlSugar
|
|||||||
// 将非空的 Tag 值用下划线连接作为分组键
|
// 将非空的 Tag 值用下划线连接作为分组键
|
||||||
return string.Join("_", groupKey.Where(k => !string.IsNullOrEmpty(k)));
|
return string.Join("_", groupKey.Where(k => !string.IsNullOrEmpty(k)));
|
||||||
});
|
});
|
||||||
return inserObjects.Count();
|
return groups;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ namespace SqlSugar.TDengine
|
|||||||
{
|
{
|
||||||
TagInserttable<T> result = new TagInserttable<T>();
|
TagInserttable<T> result = new TagInserttable<T>();
|
||||||
result.thisValue = thisValue;
|
result.thisValue = thisValue;
|
||||||
|
result.Context = ((InsertableProvider<T>)thisValue).Context;
|
||||||
result.getChildTableNamefunc = getChildTableNamefunc;
|
result.getChildTableNamefunc = getChildTableNamefunc;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,30 @@ namespace TDengineTest
|
|||||||
public static void InsertUsingTag(SqlSugarClient db)
|
public static void InsertUsingTag(SqlSugarClient db)
|
||||||
{
|
{
|
||||||
db.CodeFirst.InitTables<SUsingTagModel>();
|
db.CodeFirst.InitTables<SUsingTagModel>();
|
||||||
|
|
||||||
|
db.Insertable(new List<SUsingTagModel>(){
|
||||||
|
new SUsingTagModel()
|
||||||
|
{
|
||||||
|
Boolean = true,
|
||||||
|
Tag1 = "a",
|
||||||
|
Ts = DateTime.Now
|
||||||
|
|
||||||
|
},
|
||||||
|
new SUsingTagModel()
|
||||||
|
{
|
||||||
|
Boolean = false,
|
||||||
|
Tag1 = "a",
|
||||||
|
Ts = DateTime.Now
|
||||||
|
|
||||||
|
},
|
||||||
|
new SUsingTagModel()
|
||||||
|
{
|
||||||
|
Boolean = true,
|
||||||
|
Tag1 = "b",
|
||||||
|
Ts = DateTime.Now
|
||||||
|
}})
|
||||||
|
.SetTDengineChildTableName((stableName, it) => $"{stableName}_{it.Tag1}" /*设置子表名字*/ )
|
||||||
|
.ExecuteCommand();
|
||||||
}
|
}
|
||||||
[STableAttribute(STableName = "SUsingTagModel", Tag1 = nameof(Tag1))]
|
[STableAttribute(STableName = "SUsingTagModel", Tag1 = nameof(Tag1))]
|
||||||
public class SUsingTagModel
|
public class SUsingTagModel
|
||||||
|
Loading…
Reference in New Issue
Block a user