优化TDengine查询

This commit is contained in:
d4ilys
2024-11-30 10:49:07 +08:00
parent f1daf80266
commit 01dc3a3719
4 changed files with 38 additions and 24 deletions

View File

@@ -1,11 +1,12 @@
using System;
using FreeSql.Internal;
using FreeSql.Internal.Model;
using FreeSql.Provider.TDengine.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using FreeSql.Internal;
using FreeSql.Internal.Model;
namespace FreeSql.TDengine.Curd
{
@@ -14,6 +15,7 @@ namespace FreeSql.TDengine.Curd
public TDengineSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere)
: base(orm, commonUtils, commonExpression, dywhere)
{
}
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select,

View File

@@ -150,12 +150,13 @@ namespace FreeSql.TDengine
if (superTableDescribe == null) return;
var superTableName = _commonUtils.QuoteSqlName(database, superTableDescribe.SuperTableName);
var superTableInfo = GetTableByEntity(superTableDescribe.SuperTableType);
//判断超表是否存在
if (!TryTableExists(superTableName))
{
//先创建超级表
CreateSuperTable(ref tb, ref sb, superTableName);
CreateSuperTable(ref superTableInfo, ref sb, superTableName);
_orm.Ado.ExecuteNonQuery(sb.ToString());
sb = sb.Clear();
}
@@ -163,7 +164,7 @@ namespace FreeSql.TDengine
var subTableName = _commonUtils.QuoteSqlName(database, subTableAttribute.Name);
//创建子表
CreateSubTable(ref tb, ref sb, superTableName, subTableName);
CreateSubTable(ref tb, ref sb, superTableName, subTableName, ref superTableInfo);
}
}
//要创建的为超级表
@@ -185,24 +186,26 @@ namespace FreeSql.TDengine
/// <summary>
/// 创建子表
/// </summary>
/// <param name="tb"></param>
/// <param name="childTableInfo"></param>
/// <param name="sb"></param>
/// <param name="superTableName"></param>
private void CreateSubTable(ref TableInfo tb, ref StringBuilder sb, string superTableName, string subTableName)
private void CreateSubTable(ref TableInfo childTableInfo, ref StringBuilder sb, string superTableName,
string subTableName, ref TableInfo
superTableInfo)
{
sb.Append($"CREATE TABLE {subTableName}{Environment.NewLine}");
sb.Append($"USING {superTableName} (");
var columnInfos = tb.ColumnsByPosition.Where(c =>
var tagCols = superTableInfo.ColumnsByPosition.Where(c =>
c.Table.Properties[c.CsName].IsDefined(typeof(TDengineTagAttribute))).ToArray();
var tagValues = new List<object>(columnInfos.Count());
var tagValues = new List<object>(tagCols.Count());
var tableInstance = Activator.CreateInstance(tb.Type);
var tableInstance = Activator.CreateInstance(childTableInfo.Type);
foreach (var columnInfo in columnInfos)
foreach (var columnInfo in tagCols)
{
var tagValue = columnInfo.Table.Properties[columnInfo.CsName].GetValue(tableInstance);
var tagValue = childTableInfo.Properties[columnInfo.CsName].GetValue(tableInstance);
tagValues.Add(tagValue);
sb.Append($" {Environment.NewLine} ").Append(_commonUtils.QuoteSqlName(columnInfo.Attribute.Name))
.Append(",");
@@ -230,8 +233,10 @@ namespace FreeSql.TDengine
CreateColumns(ref tb, ref sb);
sb.Append($" TAGS (");
foreach (var columnInfo in tb.ColumnsByPosition.Where(c =>
c.Table.Properties[c.CsName].IsDefined(typeof(TDengineTagAttribute))))
var columInfos = tb.ColumnsByPosition.Where(c =>
c.Table.Properties[c.CsName].IsDefined(typeof(TDengineTagAttribute)));
foreach (var columnInfo in columInfos)
{
sb.Append($" {Environment.NewLine} ").Append(_commonUtils.QuoteSqlName(columnInfo.Attribute.Name))
.Append(" ")

View File

@@ -4,6 +4,7 @@ using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Threading;
using FreeSql.Aop;
using FreeSql.Provider.TDengine.Attributes;
using FreeSql.TDengine.Curd;
using Newtonsoft.Json.Linq;
@@ -25,19 +26,22 @@ namespace FreeSql.TDengine
this.CodeFirst = new TDengineCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
//处理超级表查询问题
this.Aop.ConfigEntityProperty += (s, e) =>
{
if (e.Property.ReflectedType == null) return;
//this.Aop.ConfigEntityProperty += (s, e) =>
//{
// if (e.Property.ReflectedType == null)
// return;
if (e.Property.ReflectedType.BaseType == null) return;
// if (e.Property.ReflectedType.BaseType == null)
// return;
var propertyInfo = e.Property.ReflectedType.BaseType.GetProperty(e.Property.Name);
// var propertyInfo = e.Property.ReflectedType.BaseType.GetProperty(e.Property.Name);
if (propertyInfo == null) return;
// if (propertyInfo == null)
// return;
if (propertyInfo.GetCustomAttribute(typeof(TDengineTagAttribute)) != null)
e.ModifyResult.IsIgnore = true;
};
// if (propertyInfo.GetCustomAttribute(typeof(TDengineTagAttribute)) != null)
// e.ModifyResult.IsIgnore = true;
//};
//处理参数化
this.Aop.CommandBefore += (_, e) =>

View File

@@ -6,9 +6,11 @@ using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using FreeSql.DataAnnotations;
using FreeSql.TDengine.Describes;
using TDengine.Data.Client;
@@ -230,5 +232,6 @@ namespace FreeSql.TDengine
return stableDescribeValue;
}
}
}