2025-04-21 09:45:30 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
2025-04-22 22:11:55 +08:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2025-04-29 23:48:47 +08:00
|
|
|
|
using System.Diagnostics;
|
2025-04-17 20:28:50 +08:00
|
|
|
|
using System.Reflection;
|
2025-04-22 22:11:55 +08:00
|
|
|
|
using System.Reflection.Metadata.Ecma335;
|
2025-04-17 20:28:50 +08:00
|
|
|
|
using System.Text;
|
2025-04-22 16:44:47 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2025-04-17 20:28:50 +08:00
|
|
|
|
using Apache.IoTDB;
|
2025-04-02 14:06:40 +08:00
|
|
|
|
using Apache.IoTDB.DataStructure;
|
2025-04-22 16:44:47 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Enums;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Extensions;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Helpers;
|
2025-04-15 16:05:07 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Models;
|
2025-05-08 14:42:13 +08:00
|
|
|
|
using JiShe.CollectBus.IoTDB.Attributes;
|
2025-04-17 20:28:50 +08:00
|
|
|
|
using JiShe.CollectBus.IoTDB.Context;
|
|
|
|
|
|
using JiShe.CollectBus.IoTDB.Interface;
|
2025-04-21 14:20:49 +08:00
|
|
|
|
using JiShe.CollectBus.IoTDB.Model;
|
2025-04-17 20:28:50 +08:00
|
|
|
|
using JiShe.CollectBus.IoTDB.Options;
|
2025-04-03 15:38:31 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-04-21 14:20:49 +08:00
|
|
|
|
using Volo.Abp.DependencyInjection;
|
2025-04-21 09:45:30 +08:00
|
|
|
|
using Volo.Abp.Domain.Entities;
|
2025-05-07 16:37:26 +08:00
|
|
|
|
using JiShe.CollectBus.Analyzers.Shared;
|
2025-05-08 22:44:01 +08:00
|
|
|
|
using JiShe.CollectBus.IoTDB.Exceptions;
|
2025-04-02 14:06:40 +08:00
|
|
|
|
|
2025-04-17 20:28:50 +08:00
|
|
|
|
namespace JiShe.CollectBus.IoTDB.Provider
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// IoTDB数据源
|
|
|
|
|
|
/// </summary>
|
2025-04-22 23:44:37 +08:00
|
|
|
|
public class IoTDbProvider : IIoTDbProvider, ITransientDependency
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-04-21 10:17:40 +08:00
|
|
|
|
private static readonly ConcurrentDictionary<Type, DeviceMetadata> MetadataCache = new();
|
|
|
|
|
|
private readonly ILogger<IoTDbProvider> _logger;
|
|
|
|
|
|
private readonly IIoTDbSessionFactory _sessionFactory;
|
2025-04-21 22:57:49 +08:00
|
|
|
|
private readonly IoTDBRuntimeContext _runtimeContext;
|
2025-04-07 16:44:25 +08:00
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
private IIoTDbSessionPool CurrentSession =>
|
2025-04-11 11:56:23 +08:00
|
|
|
|
_sessionFactory.GetSessionPool(_runtimeContext.UseTableSessionPool);
|
|
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// IoTDbProvider
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
|
/// <param name="sessionFactory"></param>
|
|
|
|
|
|
/// <param name="runtimeContext"></param>
|
|
|
|
|
|
public IoTDbProvider(
|
|
|
|
|
|
ILogger<IoTDbProvider> logger,
|
|
|
|
|
|
IIoTDbSessionFactory sessionFactory,
|
2025-04-21 22:57:49 +08:00
|
|
|
|
IoTDBRuntimeContext runtimeContext)
|
2025-04-07 16:44:25 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_sessionFactory = sessionFactory;
|
|
|
|
|
|
_runtimeContext = runtimeContext;
|
|
|
|
|
|
|
2025-04-02 14:06:40 +08:00
|
|
|
|
}
|
2025-04-11 17:06:30 +08:00
|
|
|
|
|
2025-04-02 14:06:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 插入数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="entity"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-07 16:44:25 +08:00
|
|
|
|
public async Task InsertAsync<T>(T entity) where T : IoTEntity
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-04-21 22:57:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-04-22 16:44:47 +08:00
|
|
|
|
var metadata = await GetMetadata<T>();
|
2025-04-02 14:06:40 +08:00
|
|
|
|
|
2025-04-21 22:57:49 +08:00
|
|
|
|
var tablet = BuildTablet(new[] { entity }, metadata);
|
2025-04-11 11:56:23 +08:00
|
|
|
|
|
2025-04-21 22:57:49 +08:00
|
|
|
|
await CurrentSession.InsertAsync(tablet);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-04-29 23:48:47 +08:00
|
|
|
|
_logger.LogError(ex, $"{nameof(InsertAsync)} IoTDB插入{typeof(T).Name}的数据时发生异常");
|
2025-04-21 22:57:49 +08:00
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2025-04-02 14:06:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 批量插入数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-07 16:44:25 +08:00
|
|
|
|
public async Task BatchInsertAsync<T>(IEnumerable<T> entities) where T : IoTEntity
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-04-21 22:57:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-04-22 16:44:47 +08:00
|
|
|
|
var metadata = await GetMetadata<T>();
|
2025-04-02 14:06:40 +08:00
|
|
|
|
|
2025-04-21 22:57:49 +08:00
|
|
|
|
var batchSize = 1000;
|
|
|
|
|
|
var batches = entities.Chunk(batchSize);
|
2025-04-02 14:06:40 +08:00
|
|
|
|
|
2025-04-21 22:57:49 +08:00
|
|
|
|
foreach (var batch in batches)
|
|
|
|
|
|
{
|
|
|
|
|
|
var tablet = BuildTablet(batch, metadata);
|
|
|
|
|
|
await CurrentSession.InsertAsync(tablet);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-04-29 23:48:47 +08:00
|
|
|
|
_logger.LogError(ex, $"{nameof(BatchInsertAsync)} IoTDB批量插入{typeof(T).Name}的数据时发生异常");
|
2025-04-21 22:57:49 +08:00
|
|
|
|
throw;
|
2025-04-02 14:06:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-22 16:44:47 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 批量插入数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="deviceMetadata">设备元数据</param>
|
|
|
|
|
|
/// <param name="entities"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task BatchInsertAsync<T>(DeviceMetadata deviceMetadata, IEnumerable<T> entities) where T : IoTEntity
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
var batchSize = 1000;
|
|
|
|
|
|
var batches = entities.Chunk(batchSize);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var batch in batches)
|
|
|
|
|
|
{
|
|
|
|
|
|
var tablet = BuildTablet(batch, deviceMetadata);
|
|
|
|
|
|
await CurrentSession.InsertAsync(tablet);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-04-29 23:48:47 +08:00
|
|
|
|
_logger.LogError(ex, $"{nameof(BatchInsertAsync)} IoTDB批量插入{typeof(T).Name}的数据时发生异常");
|
2025-04-22 16:44:47 +08:00
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-03 15:38:31 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-22 16:44:47 +08:00
|
|
|
|
public async Task<object> DeleteAsync<T>(IoTDBQueryOptions options) where T : IoTEntity
|
2025-04-03 15:38:31 +08:00
|
|
|
|
{
|
2025-04-21 22:57:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-04-22 16:44:47 +08:00
|
|
|
|
var query = await BuildDeleteSQL<T>(options);
|
2025-04-29 23:48:47 +08:00
|
|
|
|
var result = await CurrentSession.ExecuteQueryStatementAsync(query);
|
|
|
|
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2025-04-21 22:57:49 +08:00
|
|
|
|
|
2025-04-29 23:48:47 +08:00
|
|
|
|
if (!result.HasNext())
|
2025-04-21 22:57:49 +08:00
|
|
|
|
{
|
2025-04-29 23:48:47 +08:00
|
|
|
|
_logger.LogWarning($"{typeof(T).Name} IoTDB删除{typeof(T).Name}的数据时,没有返回受影响记录数量。");
|
2025-04-21 22:57:49 +08:00
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2025-04-03 15:38:31 +08:00
|
|
|
|
|
2025-04-21 22:57:49 +08:00
|
|
|
|
//获取唯一结果行
|
2025-04-29 23:48:47 +08:00
|
|
|
|
var row = result.Next();
|
|
|
|
|
|
await result.Close();
|
|
|
|
|
|
var dataResult = row.Values[0];
|
|
|
|
|
|
return dataResult;
|
2025-04-21 22:57:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
2025-04-03 15:38:31 +08:00
|
|
|
|
{
|
2025-04-29 23:48:47 +08:00
|
|
|
|
_logger.LogError(ex, $"{nameof(DeleteAsync)} IoTDB删除{typeof(T).Name}的数据时发生异常");
|
2025-04-21 22:57:49 +08:00
|
|
|
|
throw;
|
2025-04-03 15:38:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-22 16:44:47 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取设备元数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<DeviceMetadata> GetMetadata<T>() where T : IoTEntity
|
|
|
|
|
|
{
|
2025-05-07 16:37:26 +08:00
|
|
|
|
var accessor = SourceEntityAccessorFactory.GetAccessor<T>();
|
|
|
|
|
|
|
2025-05-08 10:28:23 +08:00
|
|
|
|
var columns = CollectColumnMetadata<T>(accessor);
|
2025-04-22 16:44:47 +08:00
|
|
|
|
var metadata = BuildDeviceMetadata<T>(columns);
|
|
|
|
|
|
var metaData = MetadataCache.AddOrUpdate(
|
|
|
|
|
|
typeof(T),
|
|
|
|
|
|
addValueFactory: t => metadata, // 如果键不存在,用此值添加
|
|
|
|
|
|
updateValueFactory: (t, existingValue) =>
|
|
|
|
|
|
{
|
2025-05-08 10:28:23 +08:00
|
|
|
|
var columns = CollectColumnMetadata(accessor);
|
2025-04-22 16:44:47 +08:00
|
|
|
|
var metadata = BuildDeviceMetadata<T>(columns);
|
|
|
|
|
|
|
|
|
|
|
|
//对现有值 existingValue 进行修改,返回新值
|
|
|
|
|
|
existingValue.ColumnNames = metadata.ColumnNames;
|
|
|
|
|
|
return existingValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2025-05-08 17:21:20 +08:00
|
|
|
|
metadata.EntityType = accessor.EntityType;
|
|
|
|
|
|
|
2025-04-22 16:44:47 +08:00
|
|
|
|
return await Task.FromResult(metaData);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-03 15:38:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查询数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-22 16:44:47 +08:00
|
|
|
|
public async Task<BusPagedResult<T>> QueryAsync<T>(IoTDBQueryOptions options) where T : IoTEntity, new()
|
2025-04-03 15:38:31 +08:00
|
|
|
|
{
|
2025-04-21 22:57:49 +08:00
|
|
|
|
try
|
2025-04-03 15:38:31 +08:00
|
|
|
|
{
|
2025-04-29 23:48:47 +08:00
|
|
|
|
var stopwatch2 = Stopwatch.StartNew();
|
|
|
|
|
|
|
2025-04-22 22:11:55 +08:00
|
|
|
|
var query = await BuildQuerySQL<T>(options);
|
2025-04-21 22:57:49 +08:00
|
|
|
|
var sessionDataSet = await CurrentSession.ExecuteQueryStatementAsync(query);
|
2025-04-03 15:38:31 +08:00
|
|
|
|
|
2025-05-08 14:42:13 +08:00
|
|
|
|
|
2025-04-29 23:48:47 +08:00
|
|
|
|
_logger.LogWarning($"{nameof(QueryAsync)} 主题的任务 {options.TableNameOrTreePath} 路径批次{options.PageIndex}任务数据读取完成,共消耗{stopwatch2.ElapsedMilliseconds}毫秒。");
|
2025-04-21 22:57:49 +08:00
|
|
|
|
var result = new BusPagedResult<T>
|
|
|
|
|
|
{
|
|
|
|
|
|
TotalCount = await GetTotalCount<T>(options),
|
2025-04-22 16:44:47 +08:00
|
|
|
|
Items = await ParseResults<T>(sessionDataSet, options.PageSize),
|
|
|
|
|
|
PageIndex = options.PageIndex,
|
|
|
|
|
|
PageSize = options.PageSize,
|
2025-04-22 22:11:55 +08:00
|
|
|
|
|
2025-04-21 22:57:49 +08:00
|
|
|
|
};
|
2025-04-29 23:48:47 +08:00
|
|
|
|
stopwatch2.Stop();
|
|
|
|
|
|
_logger.LogWarning($"{nameof(QueryAsync)} 主题的任务 {options.TableNameOrTreePath} 路径批次{options.PageIndex}任务数据读取完成,共消耗{stopwatch2.ElapsedMilliseconds}毫秒。");
|
|
|
|
|
|
//int totalPageCount = (int)Math.Ceiling((double)result.TotalCount / options.PageSize);
|
2025-04-21 22:57:49 +08:00
|
|
|
|
|
2025-04-29 23:48:47 +08:00
|
|
|
|
if (result.Items.Count() < result.PageSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
result.HasNext = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
result.HasNext = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//result.HasNext = result.Items.Count() > 0 ? result.Items.Count() < result.PageSize : false;
|
2025-04-22 16:44:47 +08:00
|
|
|
|
|
2025-04-21 22:57:49 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-04-22 17:58:14 +08:00
|
|
|
|
CurrentSession.Dispose();
|
2025-04-29 23:48:47 +08:00
|
|
|
|
_logger.LogError(ex, $"{nameof(QueryAsync)} IoTDB查询{typeof(T).Name}的数据时发生异常");
|
2025-04-21 22:57:49 +08:00
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2025-04-03 15:38:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
///// <summary>
|
|
|
|
|
|
///// 构建Tablet
|
|
|
|
|
|
///// </summary>
|
|
|
|
|
|
///// <typeparam name="T"></typeparam>
|
|
|
|
|
|
///// <param name="entities">表实体</param>
|
|
|
|
|
|
///// <param name="metadata">设备元数据</param></param>
|
|
|
|
|
|
///// <returns></returns>
|
|
|
|
|
|
//private Tablet BuildTablet<T>(IEnumerable<T> entities, DeviceMetadata metadata) where T : IoTEntity
|
|
|
|
|
|
//{
|
|
|
|
|
|
// var timestamps = new List<long>();
|
|
|
|
|
|
// var values = new List<List<object>>();
|
|
|
|
|
|
// var devicePaths = new HashSet<string>();
|
|
|
|
|
|
// List<string> tempColumnNames = new List<string>();
|
|
|
|
|
|
// tempColumnNames.AddRange(metadata.ColumnNames);
|
|
|
|
|
|
|
|
|
|
|
|
// var accessor = SourceEntityAccessorFactory.GetAccessor<T>();
|
|
|
|
|
|
|
|
|
|
|
|
// var memberCache = new Dictionary<string, EntityMemberInfo>(); // 缓存优化查询
|
|
|
|
|
|
// // 预构建成员缓存(Key: NameOrPath)
|
|
|
|
|
|
// foreach (var member in accessor.MemberList)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// memberCache[member.NameOrPath] = member;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// if (accessor.EntityType == null || metadata.EntityType == null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构{nameof(Tablet)}时 {nameof(T)}的EntityType 没有指定,属于异常情况,-101");
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// if (metadata.EntityType != accessor.EntityType)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构{nameof(Tablet)}时 {nameof(T)}的EntityType 和{nameof(DeviceMetadata)}的 EntityType 不一致,属于异常情况,-102");
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// if (metadata.EntityType == EntityTypeEnum.TreeModel && _runtimeContext.UseTableSessionPool == true)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构{nameof(Tablet)}时 tree模型不能使用table模型Session连接,属于异常情况,-103");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else if (metadata.EntityType == EntityTypeEnum.TableModel && _runtimeContext.UseTableSessionPool == false)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构{nameof(Tablet)}时 table模型不能使用tree模型Session连接,属于异常情况,-104");
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// string tableNameOrTreePath = string.Empty;
|
|
|
|
|
|
// var tableNameOrTreePathAttribute = typeof(T).GetCustomAttribute<TableNameOrTreePathAttribute>();
|
|
|
|
|
|
// if (tableNameOrTreePathAttribute != null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// tableNameOrTreePath = tableNameOrTreePathAttribute.TableNameOrTreePath;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// foreach (var entity in entities)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// timestamps.Add(entity.Timestamps);
|
|
|
|
|
|
// var rowValues = new List<object>();
|
|
|
|
|
|
|
|
|
|
|
|
// foreach (var measurement in metadata.ColumnNames)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (!memberCache.TryGetValue(measurement, out var member))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构时{accessor.EntityName}没有找到{measurement}对应的member信息,-105");
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// var value = member.GetValue(entity);
|
|
|
|
|
|
|
|
|
|
|
|
// // 特性查询优化
|
|
|
|
|
|
// var attributes = member.CustomAttributes ?? Enumerable.Empty<Attribute>();
|
|
|
|
|
|
// var singleMeasuringAttr = attributes.OfType<SingleMeasuringAttribute>().FirstOrDefault();
|
|
|
|
|
|
// if (singleMeasuringAttr != null)//如果是单侧点
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
|
|
// var tupleItem1Key = $"{member.NameOrPath}.Item1";
|
|
|
|
|
|
// if (!memberCache.TryGetValue(tupleItem1Key, out var tuple1Member))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构时{accessor.EntityName} 没有找到{measurement}对应的member Item1 信息,-106");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// int indexOf = metadata.ColumnNames.IndexOf(measurement);
|
|
|
|
|
|
// tempColumnNames[indexOf] = (string)tuple1Member.GetValue(entity);
|
|
|
|
|
|
|
|
|
|
|
|
// var tupleItem2Key = $"{member.NameOrPath}.Item2";
|
|
|
|
|
|
// if (!memberCache.TryGetValue(tupleItem2Key, out var tuple2Member))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// throw new ArgumentException($"{nameof(BuildTablet)} 构建存储结构时{accessor.EntityName} 没有找到{measurement}对应的member Item2 信息,-107");
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// value = tuple2Member.GetValue(entity);
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// if (value != null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var tempValue = member.DeclaredTypeName.ToUpper() switch
|
|
|
|
|
|
// {
|
|
|
|
|
|
// "DATETIME" => Convert.ToDateTime(value).GetDateTimeOffset().ToUnixTimeNanoseconds(),
|
|
|
|
|
|
// _ => value
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
// rowValues.Add(tempValue);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// rowValues.Add(value);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// values.Add(rowValues);
|
|
|
|
|
|
|
|
|
|
|
|
// //如果指定了路径
|
|
|
|
|
|
// if (!string.IsNullOrWhiteSpace(tableNameOrTreePath))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// devicePaths.Add(tableNameOrTreePath);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (!_runtimeContext.UseTableSessionPool)//树模型
|
|
|
|
|
|
// {
|
|
|
|
|
|
// devicePaths.Add(DevicePathBuilder.GetDevicePath(entity));
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// devicePaths.Add(DevicePathBuilder.GetTableName<T>());
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// if (devicePaths.Count > 1)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// throw new Exception($"{nameof(BuildTablet)} 构建Tablet《{typeof(T).Name}》时,批量插入的设备路径不一致。");
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// return _runtimeContext.UseTableSessionPool
|
|
|
|
|
|
// ? BuildTableSessionTablet(metadata, devicePaths.First(), tempColumnNames, values, timestamps)
|
|
|
|
|
|
// : BuildSessionTablet(metadata, devicePaths.First(), tempColumnNames,values, timestamps);
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
2025-04-02 14:06:40 +08:00
|
|
|
|
/// <summary>
|
2025-04-07 16:44:25 +08:00
|
|
|
|
/// 构建Tablet
|
2025-04-02 14:06:40 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2025-05-08 22:44:01 +08:00
|
|
|
|
/// <param name="entities">表实体集合</param>
|
2025-04-07 16:44:25 +08:00
|
|
|
|
/// <param name="metadata">设备元数据</param></param>
|
2025-04-02 14:06:40 +08:00
|
|
|
|
/// <returns></returns>
|
2025-04-07 16:44:25 +08:00
|
|
|
|
private Tablet BuildTablet<T>(IEnumerable<T> entities, DeviceMetadata metadata) where T : IoTEntity
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-05-08 22:44:01 +08:00
|
|
|
|
// 前置校验
|
|
|
|
|
|
ValidateMetadataAndAccessor<T>(metadata, out var accessor);
|
2025-05-07 16:37:26 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
// 初始化数据结构
|
|
|
|
|
|
var (timestamps, values, devicePaths) = (new List<long>(), new List<List<object>>(), new HashSet<string>());
|
|
|
|
|
|
var tempColumnNames = new List<string>(metadata.ColumnNames);
|
|
|
|
|
|
var memberCache = BuildMemberCache(accessor);
|
|
|
|
|
|
var tableNameOrTreePath = GetTableNameOrTreePath<T>();
|
|
|
|
|
|
|
|
|
|
|
|
// 处理每个实体
|
|
|
|
|
|
foreach (var entity in entities)
|
2025-05-08 17:21:20 +08:00
|
|
|
|
{
|
2025-05-08 22:44:01 +08:00
|
|
|
|
ProcessEntity(entity, accessor, metadata, memberCache, tempColumnNames, timestamps, values);
|
|
|
|
|
|
UpdateDevicePaths(entity, tableNameOrTreePath, devicePaths);
|
2025-05-08 17:21:20 +08:00
|
|
|
|
}
|
2025-04-21 09:45:30 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
// 后置校验与返回
|
|
|
|
|
|
ValidateDevicePaths(devicePaths);
|
|
|
|
|
|
// return CreateFinalTablet(metadata, devicePaths.First(), tempColumnNames, values, timestamps);
|
|
|
|
|
|
return _runtimeContext.UseTableSessionPool
|
|
|
|
|
|
? BuildTableSessionTablet(metadata, devicePaths.First(), tempColumnNames, values, timestamps)
|
|
|
|
|
|
: BuildSessionTablet(metadata, devicePaths.First(), tempColumnNames, values, timestamps);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ValidateMetadataAndAccessor<T>(DeviceMetadata metadata, out ISourceEntityAccessor<T> accessor) where T : IoTEntity
|
|
|
|
|
|
{
|
|
|
|
|
|
accessor = SourceEntityAccessorFactory.GetAccessor<T>();
|
|
|
|
|
|
|
2025-05-08 17:21:20 +08:00
|
|
|
|
if (accessor.EntityType == null || metadata.EntityType == null)
|
2025-04-21 09:45:30 +08:00
|
|
|
|
{
|
2025-05-08 22:44:01 +08:00
|
|
|
|
throw new IoTException($"{nameof(BuildTablet)} 构建IoTDB数据结构时,EntityType未指定", -101);
|
2025-04-21 09:45:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-08 17:21:20 +08:00
|
|
|
|
if (metadata.EntityType != accessor.EntityType)
|
2025-04-21 09:45:30 +08:00
|
|
|
|
{
|
2025-05-08 22:44:01 +08:00
|
|
|
|
throw new IoTException($"{nameof(BuildTablet)} 构建IoTDB数据结构时,EntityType不一致", -102);
|
2025-04-21 09:45:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
bool isTableModel = accessor.EntityType == EntityTypeEnum.TableModel;
|
|
|
|
|
|
if (_runtimeContext.UseTableSessionPool != isTableModel)
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-05-08 22:44:01 +08:00
|
|
|
|
throw new IoTException($"{nameof(BuildTablet)} 构建IoTDB数据结构时,Session类型不匹配: 预期{(isTableModel ? "Table" : "Tree")}模型", isTableModel ? -104 : -103);
|
2025-04-21 09:45:30 +08:00
|
|
|
|
}
|
2025-05-08 22:44:01 +08:00
|
|
|
|
}
|
2025-04-21 09:45:30 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理实体并获取值
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="entity"></param>
|
|
|
|
|
|
/// <param name="accessor"></param>
|
|
|
|
|
|
/// <param name="metadata"></param>
|
|
|
|
|
|
/// <param name="memberCache"></param>
|
|
|
|
|
|
/// <param name="tempColumnNames"></param>
|
|
|
|
|
|
/// <param name="timestamps"></param>
|
|
|
|
|
|
/// <param name="values"></param>
|
|
|
|
|
|
/// <exception cref="IoTException"></exception>
|
|
|
|
|
|
private void ProcessEntity<T>(
|
|
|
|
|
|
T entity,
|
|
|
|
|
|
ISourceEntityAccessor<T> accessor,
|
|
|
|
|
|
DeviceMetadata metadata,
|
|
|
|
|
|
Dictionary<string, EntityMemberInfo> memberCache,
|
|
|
|
|
|
List<string> tempColumnNames,
|
|
|
|
|
|
List<long> timestamps,
|
|
|
|
|
|
List<List<object>> values) where T : IoTEntity
|
|
|
|
|
|
{
|
|
|
|
|
|
timestamps.Add(entity.Timestamps);
|
|
|
|
|
|
var rowValues = new object[metadata.ColumnNames.Count];
|
2025-04-21 22:57:49 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
Parallel.ForEach(metadata.ColumnNames, (measurement, state, index) =>
|
2025-04-21 14:20:49 +08:00
|
|
|
|
{
|
2025-05-08 22:44:01 +08:00
|
|
|
|
if (!memberCache.TryGetValue(measurement, out var member))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new IoTException($"{nameof(BuildTablet)} 构建IoTDB数据结构时,找不到成员: {measurement}", -105);
|
|
|
|
|
|
}
|
2025-04-21 14:20:49 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
object value = ResolveMemberValue(entity, member, memberCache, tempColumnNames, (int)index);
|
|
|
|
|
|
rowValues[index] = ConvertValueByType(member, value);
|
|
|
|
|
|
});
|
2025-05-08 17:21:20 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
values.Add(rowValues.ToList());
|
|
|
|
|
|
}
|
2025-05-08 17:21:20 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
private object ResolveMemberValue<T>(
|
|
|
|
|
|
T entity,
|
|
|
|
|
|
EntityMemberInfo member,
|
|
|
|
|
|
Dictionary<string, EntityMemberInfo> memberCache,
|
|
|
|
|
|
List<string> tempColumnNames,
|
|
|
|
|
|
int columnIndex) where T : IoTEntity
|
|
|
|
|
|
{
|
|
|
|
|
|
// 单测点逻辑
|
|
|
|
|
|
if (member.CustomAttributes?.OfType<SingleMeasuringAttribute>().FirstOrDefault() is { } attr)
|
|
|
|
|
|
{
|
|
|
|
|
|
var tuple1Key = $"{member.NameOrPath}.Item1";
|
|
|
|
|
|
var tuple2Key = $"{member.NameOrPath}.Item2";
|
2025-05-08 17:21:20 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
if (!memberCache.TryGetValue(tuple1Key, out var tuple1) || !memberCache.TryGetValue(tuple2Key, out var tuple2))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new IoTException($"{nameof(BuildTablet)} 构建IoTDB数据结构时,单侧点元组成员缺失", -106);
|
|
|
|
|
|
}
|
2025-05-08 10:28:23 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
tempColumnNames[columnIndex] = (string)tuple1.GetValue(entity);
|
|
|
|
|
|
return tuple2.GetValue(entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
return member.GetValue(entity);
|
|
|
|
|
|
}
|
2025-05-08 10:28:23 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置实体的成员值
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="member"></param>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private object ConvertValueByType(EntityMemberInfo member, object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return member.DeclaredTypeName switch
|
|
|
|
|
|
{
|
|
|
|
|
|
"DATETIME" => Convert.ToDateTime(value).GetDateTimeOffset().ToUnixTimeNanoseconds(),
|
|
|
|
|
|
_ => value
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理设备路径
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="entity"></param>
|
|
|
|
|
|
/// <param name="tableNameOrTreePath"></param>
|
|
|
|
|
|
/// <param name="devicePaths"></param>
|
|
|
|
|
|
private void UpdateDevicePaths<T>(
|
|
|
|
|
|
T entity,
|
|
|
|
|
|
string tableNameOrTreePath,
|
|
|
|
|
|
HashSet<string> devicePaths) where T : IoTEntity
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tableNameOrTreePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
devicePaths.Add(tableNameOrTreePath);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-04-02 14:06:40 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
var path = _runtimeContext.UseTableSessionPool
|
|
|
|
|
|
? DevicePathBuilder.GetTableName<T>()
|
|
|
|
|
|
: DevicePathBuilder.GetDevicePath(entity);
|
|
|
|
|
|
devicePaths.Add(path);
|
|
|
|
|
|
}
|
2025-04-21 14:20:49 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 验证设备路径
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="devicePaths"></param>
|
|
|
|
|
|
private void ValidateDevicePaths(HashSet<string> devicePaths)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (devicePaths.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new IoTException($"{nameof(BuildTablet)} 构建IoTDB数据结构时,设备路径集合为空", -108);
|
2025-04-21 14:20:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-07 16:44:25 +08:00
|
|
|
|
if (devicePaths.Count > 1)
|
2025-04-03 15:38:31 +08:00
|
|
|
|
{
|
2025-05-08 22:44:01 +08:00
|
|
|
|
var paths = string.Join(", ", devicePaths.Take(3));
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new IoTException($"{nameof(BuildTablet)} 构建IoTDB数据结构时,设备路径不一致。检测到路径: {paths}...", -109);
|
|
|
|
|
|
}
|
2025-04-03 15:38:31 +08:00
|
|
|
|
}
|
2025-05-08 22:44:01 +08:00
|
|
|
|
}
|
2025-04-07 16:44:25 +08:00
|
|
|
|
|
2025-05-08 22:44:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 缓存优化:避免重复反射
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private string GetTableNameOrTreePath<T>()
|
|
|
|
|
|
{
|
|
|
|
|
|
return AttributeCache<T>.TableNameOrTreePath;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 特性缓存辅助类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
private static class AttributeCache<T>
|
|
|
|
|
|
{
|
|
|
|
|
|
public static readonly string TableNameOrTreePath;
|
|
|
|
|
|
|
|
|
|
|
|
static AttributeCache()
|
|
|
|
|
|
{
|
|
|
|
|
|
var attr = typeof(T).GetCustomAttribute<TableNameOrTreePathAttribute>();
|
|
|
|
|
|
TableNameOrTreePath = attr?.TableNameOrTreePath ?? string.Empty;
|
|
|
|
|
|
}
|
2025-04-07 16:44:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构建tree模型的Tablet
|
|
|
|
|
|
/// </summary>
|
2025-04-21 09:45:30 +08:00
|
|
|
|
/// <param name="metadata">已解析的设备数据元数据</param>
|
|
|
|
|
|
/// <param name="devicePath">设备路径</param>
|
2025-05-08 22:44:01 +08:00
|
|
|
|
/// <param name="columns">数据列集合</param>
|
2025-04-21 09:45:30 +08:00
|
|
|
|
/// <param name="values">数据集合</param>
|
|
|
|
|
|
/// <param name="timestamps">时间戳集合</param>
|
2025-04-07 16:44:25 +08:00
|
|
|
|
/// <returns></returns>
|
2025-05-08 22:44:01 +08:00
|
|
|
|
private Tablet BuildSessionTablet(DeviceMetadata metadata, string devicePath, List<string> columns, List<List<object>> values, List<long> timestamps)
|
2025-04-07 16:44:25 +08:00
|
|
|
|
{
|
2025-04-21 09:45:30 +08:00
|
|
|
|
//todo 树模型需要去掉TAG类型和ATTRIBUTE类型的字段,只需要保留FIELD类型字段即可
|
|
|
|
|
|
|
2025-04-07 16:44:25 +08:00
|
|
|
|
return new Tablet(
|
|
|
|
|
|
devicePath,
|
2025-05-08 22:44:01 +08:00
|
|
|
|
columns,
|
2025-04-07 16:44:25 +08:00
|
|
|
|
metadata.DataTypes,
|
|
|
|
|
|
values,
|
|
|
|
|
|
timestamps
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构建表模型的Tablet
|
|
|
|
|
|
/// </summary>
|
2025-04-21 09:45:30 +08:00
|
|
|
|
/// <param name="metadata">已解析的设备数据元数据</param>
|
|
|
|
|
|
/// <param name="tableName">表名称</param>
|
2025-05-08 22:44:01 +08:00
|
|
|
|
/// <param name="columns">数据列集合</param>
|
2025-04-21 09:45:30 +08:00
|
|
|
|
/// <param name="values">数据集合</param>
|
|
|
|
|
|
/// <param name="timestamps">时间戳集合</param>
|
2025-04-07 16:44:25 +08:00
|
|
|
|
/// <returns></returns>
|
2025-05-08 22:44:01 +08:00
|
|
|
|
private Tablet BuildTableSessionTablet(DeviceMetadata metadata, string tableName, List<string> columns,List<List<object>> values, List<long> timestamps)
|
2025-04-07 16:44:25 +08:00
|
|
|
|
{
|
|
|
|
|
|
var tablet = new Tablet(
|
2025-04-21 09:45:30 +08:00
|
|
|
|
tableName,
|
2025-05-08 22:44:01 +08:00
|
|
|
|
columns,
|
2025-04-07 16:44:25 +08:00
|
|
|
|
metadata.ColumnCategories,
|
|
|
|
|
|
metadata.DataTypes,
|
|
|
|
|
|
values,
|
|
|
|
|
|
timestamps
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return tablet;
|
2025-04-02 14:06:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-04-03 15:38:31 +08:00
|
|
|
|
/// 构建查询语句
|
2025-04-02 14:06:40 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-22 16:44:47 +08:00
|
|
|
|
private async Task<string> BuildQuerySQL<T>(IoTDBQueryOptions options) where T : IoTEntity
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-04-22 16:44:47 +08:00
|
|
|
|
var metadata = await GetMetadata<T>();
|
2025-04-22 22:11:55 +08:00
|
|
|
|
var sb = new StringBuilder("SELECT TIME as Timestamps,");
|
2025-04-03 15:38:31 +08:00
|
|
|
|
sb.AppendJoin(", ", metadata.ColumnNames);
|
2025-04-07 16:44:25 +08:00
|
|
|
|
sb.Append($" FROM {options.TableNameOrTreePath}");
|
2025-04-02 14:06:40 +08:00
|
|
|
|
|
2025-04-03 15:38:31 +08:00
|
|
|
|
if (options.Conditions.Any())
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-04-03 15:38:31 +08:00
|
|
|
|
sb.Append(" WHERE ");
|
|
|
|
|
|
sb.AppendJoin(" AND ", options.Conditions.Select(TranslateCondition));
|
|
|
|
|
|
}
|
2025-04-02 14:06:40 +08:00
|
|
|
|
|
2025-04-22 16:44:47 +08:00
|
|
|
|
sb.Append($" LIMIT {options.PageSize} OFFSET {options.PageIndex * options.PageSize}");
|
2025-04-03 15:38:31 +08:00
|
|
|
|
return sb.ToString();
|
2025-04-02 14:06:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-04-03 15:38:31 +08:00
|
|
|
|
/// 构建删除语句
|
2025-04-02 14:06:40 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-22 16:44:47 +08:00
|
|
|
|
private async Task<string> BuildDeleteSQL<T>(IoTDBQueryOptions options) where T : IoTEntity
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-04-22 16:44:47 +08:00
|
|
|
|
var metadata = await GetMetadata<T>();
|
2025-04-07 16:44:25 +08:00
|
|
|
|
var sb = new StringBuilder();
|
2025-04-03 15:38:31 +08:00
|
|
|
|
|
2025-04-07 16:44:25 +08:00
|
|
|
|
if (!_runtimeContext.UseTableSessionPool)
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.Append("DELETE ");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.Append("DROP ");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-08 17:44:42 +08:00
|
|
|
|
sb.Append($" FROM {options.TableNameOrTreePath}");
|
2025-04-03 15:38:31 +08:00
|
|
|
|
|
|
|
|
|
|
sb.AppendJoin(", ", metadata.ColumnNames);
|
2025-04-02 14:06:40 +08:00
|
|
|
|
|
|
|
|
|
|
if (options.Conditions.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.Append(" WHERE ");
|
|
|
|
|
|
sb.AppendJoin(" AND ", options.Conditions.Select(TranslateCondition));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 将查询条件转换为SQL语句
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="condition"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotSupportedException"></exception>
|
|
|
|
|
|
private string TranslateCondition(QueryCondition condition)
|
|
|
|
|
|
{
|
|
|
|
|
|
return condition.Operator switch
|
|
|
|
|
|
{
|
2025-04-22 22:11:55 +08:00
|
|
|
|
">" => condition.IsNumber ? $"{condition.Field} > {condition.Value}" : $"{condition.Field} > '{condition.Value}'",
|
2025-04-22 16:44:47 +08:00
|
|
|
|
"<" => condition.IsNumber ? $"{condition.Field} < {condition.Value}" : $"{condition.Field} < '{condition.Value}'",
|
|
|
|
|
|
"=" => condition.IsNumber ? $"{condition.Field} = {condition.Value}" : $"{condition.Field} = '{condition.Value}'",
|
|
|
|
|
|
_ => throw new NotSupportedException($"{nameof(TranslateCondition)} 将查询条件转换为SQL语句时操作符 {condition.Operator} 属于异常情况")
|
2025-04-02 14:06:40 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取查询条件的总数量
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-22 16:44:47 +08:00
|
|
|
|
private async Task<int> GetTotalCount<T>(IoTDBQueryOptions options) where T : IoTEntity
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-04-07 16:44:25 +08:00
|
|
|
|
var countQuery = $"SELECT COUNT(*) FROM {options.TableNameOrTreePath}";
|
2025-04-02 14:06:40 +08:00
|
|
|
|
if (options.Conditions.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
countQuery += " WHERE " + string.Join(" AND ", options.Conditions.Select(TranslateCondition));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-11 11:56:23 +08:00
|
|
|
|
var result = await CurrentSession.ExecuteQueryStatementAsync(countQuery);
|
2025-04-29 23:48:47 +08:00
|
|
|
|
if (result == null)
|
2025-04-22 23:44:37 +08:00
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 23:48:47 +08:00
|
|
|
|
if (!result.HasNext())
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-22 23:44:37 +08:00
|
|
|
|
var count = Convert.ToInt32(result.Next().Values[0]);
|
|
|
|
|
|
await result.Close();
|
|
|
|
|
|
|
|
|
|
|
|
return count;
|
2025-04-02 14:06:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 解析查询结果
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="dataSet"></param>
|
|
|
|
|
|
/// <param name="pageSize"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-22 16:44:47 +08:00
|
|
|
|
private async Task<IEnumerable<T>> ParseResults<T>(SessionDataSet dataSet, int pageSize) where T : IoTEntity, new()
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
var results = new List<T>();
|
2025-04-22 16:44:47 +08:00
|
|
|
|
var metadata = await GetMetadata<T>();
|
2025-04-02 14:06:40 +08:00
|
|
|
|
|
2025-05-08 17:21:20 +08:00
|
|
|
|
var accessor = SourceEntityAccessorFactory.GetAccessor<T>();
|
2025-05-08 22:44:01 +08:00
|
|
|
|
var memberCache = BuildMemberCache(accessor);
|
2025-04-22 23:44:37 +08:00
|
|
|
|
|
|
|
|
|
|
var columns = new List<string>() { "Timestamps" };
|
|
|
|
|
|
var dataTypes = new List<TSDataType>() { TSDataType.TIMESTAMP };
|
|
|
|
|
|
columns.AddRange(metadata.ColumnNames);
|
|
|
|
|
|
dataTypes.AddRange(metadata.DataTypes);
|
2025-04-02 17:23:52 +08:00
|
|
|
|
|
2025-05-07 17:20:10 +08:00
|
|
|
|
|
2025-04-02 14:06:40 +08:00
|
|
|
|
while (dataSet.HasNext() && results.Count < pageSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
var record = dataSet.Next();
|
|
|
|
|
|
var entity = new T
|
|
|
|
|
|
{
|
2025-04-02 17:23:52 +08:00
|
|
|
|
Timestamps = record.Timestamps
|
2025-04-02 14:06:40 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-04-22 23:44:37 +08:00
|
|
|
|
foreach (var measurement in columns)
|
2025-04-02 14:06:40 +08:00
|
|
|
|
{
|
2025-04-22 23:44:37 +08:00
|
|
|
|
int indexOf = columns.IndexOf(measurement);
|
2025-04-22 16:44:47 +08:00
|
|
|
|
var value = record.Values[indexOf];
|
2025-04-22 23:44:37 +08:00
|
|
|
|
TSDataType tSDataType = dataTypes[indexOf];
|
2025-04-02 17:23:52 +08:00
|
|
|
|
|
2025-05-08 17:21:20 +08:00
|
|
|
|
if (!memberCache.TryGetValue(measurement, out var member) && !(value is System.DBNull))
|
2025-04-22 23:44:37 +08:00
|
|
|
|
{
|
2025-05-08 17:21:20 +08:00
|
|
|
|
throw new Exception($"{nameof(ParseResults)} 解析查询结果 {accessor.EntityName} 属性赋值出现异常,没有找到{measurement}对应的 member信息");
|
|
|
|
|
|
}
|
2025-04-22 22:11:55 +08:00
|
|
|
|
|
2025-05-08 17:21:20 +08:00
|
|
|
|
dynamic tempValue = GetTSDataValue(tSDataType, value);
|
2025-05-07 17:20:10 +08:00
|
|
|
|
|
2025-05-08 17:21:20 +08:00
|
|
|
|
if (measurement.ToLower().EndsWith("time"))
|
|
|
|
|
|
{
|
|
|
|
|
|
member.Setter(entity, TimestampHelper.ConvertToDateTime(tempValue, TimestampUnit.Nanoseconds));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
member.Setter(entity, tempValue);
|
2025-04-02 17:23:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-02 14:06:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
results.Add(entity);
|
2025-04-22 23:44:37 +08:00
|
|
|
|
|
2025-04-02 14:06:40 +08:00
|
|
|
|
}
|
2025-04-22 23:44:37 +08:00
|
|
|
|
await dataSet.Close();
|
2025-04-02 14:06:40 +08:00
|
|
|
|
return results;
|
|
|
|
|
|
}
|
2025-04-03 15:38:31 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取设备元数据的列
|
|
|
|
|
|
/// </summary>
|
2025-05-08 10:28:23 +08:00
|
|
|
|
/// <param name="accessor"></param>
|
2025-04-03 15:38:31 +08:00
|
|
|
|
/// <returns></returns>
|
2025-05-08 10:28:23 +08:00
|
|
|
|
private List<ColumnInfo> CollectColumnMetadata<T>(ISourceEntityAccessor<T> accessor)
|
2025-04-03 15:38:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
var columns = new List<ColumnInfo>();
|
2025-05-08 22:44:01 +08:00
|
|
|
|
var memberCache = BuildMemberCache(accessor);
|
2025-05-08 17:21:20 +08:00
|
|
|
|
|
|
|
|
|
|
foreach (var member in accessor.MemberList)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 过滤元组子项
|
|
|
|
|
|
if (member.NameOrPath.Contains(".Item")) continue;
|
|
|
|
|
|
|
|
|
|
|
|
// 类型名称处理
|
|
|
|
|
|
Type declaredType = member.DeclaredType;
|
|
|
|
|
|
var underlyingType = Nullable.GetUnderlyingType(declaredType);
|
|
|
|
|
|
string declaredTypeName = underlyingType?.Name ?? member.DeclaredTypeName;
|
|
|
|
|
|
|
|
|
|
|
|
// 特性查询优化
|
|
|
|
|
|
var attributes = member.CustomAttributes ?? Enumerable.Empty<Attribute>();
|
|
|
|
|
|
var tagAttr = attributes.OfType<TAGColumnAttribute>().FirstOrDefault();
|
|
|
|
|
|
var attrColumn = attributes.OfType<ATTRIBUTEColumnAttribute>().FirstOrDefault();
|
|
|
|
|
|
var fieldColumn = attributes.OfType<FIELDColumnAttribute>().FirstOrDefault();
|
|
|
|
|
|
var singleMeasuringAttr = attributes.OfType<SingleMeasuringAttribute>().FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
// 构建ColumnInfo
|
|
|
|
|
|
ColumnInfo? column = null;
|
|
|
|
|
|
if (tagAttr != null)
|
2025-05-08 14:42:13 +08:00
|
|
|
|
{
|
2025-05-08 17:21:20 +08:00
|
|
|
|
column = new ColumnInfo(member.NameOrPath, ColumnCategory.TAG, GetDataTypeFromTypeName(declaredTypeName), false);
|
2025-05-08 14:42:13 +08:00
|
|
|
|
}
|
2025-05-08 17:21:20 +08:00
|
|
|
|
else if (attrColumn != null)
|
2025-04-21 14:20:49 +08:00
|
|
|
|
{
|
2025-05-08 17:21:20 +08:00
|
|
|
|
column = new ColumnInfo(member.NameOrPath, ColumnCategory.ATTRIBUTE, GetDataTypeFromTypeName(declaredTypeName), false);
|
2025-04-21 14:20:49 +08:00
|
|
|
|
}
|
2025-05-08 17:21:20 +08:00
|
|
|
|
else if (fieldColumn != null)
|
2025-04-21 14:20:49 +08:00
|
|
|
|
{
|
2025-05-08 17:21:20 +08:00
|
|
|
|
column = new ColumnInfo(member.NameOrPath, ColumnCategory.FIELD, GetDataTypeFromTypeName(declaredTypeName), false);
|
2025-04-21 14:20:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-08 17:21:20 +08:00
|
|
|
|
// 单测模式处理
|
|
|
|
|
|
if (singleMeasuringAttr != null && column == null)
|
2025-04-10 23:31:43 +08:00
|
|
|
|
{
|
2025-05-08 17:21:20 +08:00
|
|
|
|
var tupleItemKey = $"{member.NameOrPath}.Item2";
|
|
|
|
|
|
if (!memberCache.TryGetValue(tupleItemKey, out var tupleMember))
|
2025-05-08 14:42:13 +08:00
|
|
|
|
{
|
2025-05-08 17:21:20 +08:00
|
|
|
|
throw new Exception($"{nameof(CollectColumnMetadata)} {accessor.EntityName} {member.NameOrPath} 单侧点属性解析异常");
|
2025-05-08 14:42:13 +08:00
|
|
|
|
}
|
2025-05-08 17:21:20 +08:00
|
|
|
|
column = new ColumnInfo(member.NameOrPath, ColumnCategory.FIELD, GetDataTypeFromTypeName(tupleMember.DeclaredTypeName), true);
|
2025-04-10 23:31:43 +08:00
|
|
|
|
}
|
2025-04-11 17:06:30 +08:00
|
|
|
|
|
2025-05-08 17:21:20 +08:00
|
|
|
|
if (column.HasValue) columns.Add(column.Value);
|
2025-04-03 15:38:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
return columns;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构建设备元数据
|
|
|
|
|
|
/// </summary>
|
2025-04-21 09:45:30 +08:00
|
|
|
|
/// <param name="typeInfo">待解析的类</param>
|
|
|
|
|
|
/// <param name="columns">已处理好的数据列</param>
|
2025-04-03 15:38:31 +08:00
|
|
|
|
/// <returns></returns>
|
2025-04-21 09:45:30 +08:00
|
|
|
|
private DeviceMetadata BuildDeviceMetadata<T>(List<ColumnInfo> columns) where T : IoTEntity
|
2025-04-03 15:38:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
var metadata = new DeviceMetadata();
|
|
|
|
|
|
|
2025-04-11 11:56:23 +08:00
|
|
|
|
//先检查是不是单侧点模型
|
|
|
|
|
|
if (columns.Any(c => c.IsSingleMeasuring))
|
|
|
|
|
|
{
|
|
|
|
|
|
metadata.IsSingleMeasuring = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//按业务逻辑顺序处理(TAG -> ATTRIBUTE -> FIELD)
|
2025-04-03 15:38:31 +08:00
|
|
|
|
var groupedColumns = columns
|
|
|
|
|
|
.GroupBy(c => c.Category)
|
|
|
|
|
|
.ToDictionary(g => g.Key, g => g.ToList());
|
2025-04-21 14:20:49 +08:00
|
|
|
|
|
2025-04-03 15:38:31 +08:00
|
|
|
|
ProcessCategory(groupedColumns, ColumnCategory.TAG, metadata);
|
|
|
|
|
|
ProcessCategory(groupedColumns, ColumnCategory.ATTRIBUTE, metadata);
|
|
|
|
|
|
ProcessCategory(groupedColumns, ColumnCategory.FIELD, metadata);
|
|
|
|
|
|
|
|
|
|
|
|
return metadata;
|
2025-04-02 17:23:52 +08:00
|
|
|
|
}
|
2025-04-03 15:38:31 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理不同列类型的逻辑
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="groupedColumns"></param>
|
|
|
|
|
|
/// <param name="category"></param>
|
|
|
|
|
|
/// <param name="metadata"></param>
|
|
|
|
|
|
private void ProcessCategory(IReadOnlyDictionary<ColumnCategory, List<ColumnInfo>> groupedColumns, ColumnCategory category, DeviceMetadata metadata)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (groupedColumns.TryGetValue(category, out var cols))
|
|
|
|
|
|
{
|
|
|
|
|
|
metadata.ColumnNames.AddRange(cols.Select(c => c.Name));
|
|
|
|
|
|
metadata.ColumnCategories.AddRange(cols.Select(c => c.Category));
|
|
|
|
|
|
metadata.DataTypes.AddRange(cols.Select(c => c.DataType));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 数据列结构
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly struct ColumnInfo
|
|
|
|
|
|
{
|
2025-04-10 23:31:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 列名
|
|
|
|
|
|
/// </summary>
|
2025-04-03 15:38:31 +08:00
|
|
|
|
public string Name { get; }
|
2025-04-10 23:31:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否是单测点
|
|
|
|
|
|
/// </summary>
|
2025-04-11 11:56:23 +08:00
|
|
|
|
public bool IsSingleMeasuring { get; }
|
2025-04-10 23:31:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 列类型
|
|
|
|
|
|
/// </summary>
|
2025-04-03 15:38:31 +08:00
|
|
|
|
public ColumnCategory Category { get; }
|
2025-04-10 23:31:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 数据类型
|
|
|
|
|
|
/// </summary>
|
2025-04-03 15:38:31 +08:00
|
|
|
|
public TSDataType DataType { get; }
|
|
|
|
|
|
|
2025-04-11 11:56:23 +08:00
|
|
|
|
public ColumnInfo(string name, ColumnCategory category, TSDataType dataType, bool isSingleMeasuring)
|
2025-04-03 15:38:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
Name = name;
|
|
|
|
|
|
Category = category;
|
|
|
|
|
|
DataType = dataType;
|
2025-04-10 23:31:43 +08:00
|
|
|
|
IsSingleMeasuring = isSingleMeasuring;
|
2025-04-03 15:38:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据类型名称获取对应的 IoTDB 数据类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="typeName">类型名称(不区分大小写)</param>
|
|
|
|
|
|
/// <returns>对应的 TSDataType,默认返回 TSDataType.STRING</returns>
|
|
|
|
|
|
private TSDataType GetDataTypeFromTypeName(string typeName)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(typeName))
|
2025-04-07 16:44:25 +08:00
|
|
|
|
return TSDataType.STRING;
|
2025-04-03 15:38:31 +08:00
|
|
|
|
|
|
|
|
|
|
return DataTypeMap.TryGetValue(typeName.Trim(), out var dataType)
|
|
|
|
|
|
? dataType
|
|
|
|
|
|
: TSDataType.STRING;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据类型名称获取 IoTDB 数据类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly IReadOnlyDictionary<string, TSDataType> DataTypeMap =
|
|
|
|
|
|
new Dictionary<string, TSDataType>(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
|
{
|
|
|
|
|
|
["BOOLEAN"] = TSDataType.BOOLEAN,
|
|
|
|
|
|
["INT32"] = TSDataType.INT32,
|
|
|
|
|
|
["INT64"] = TSDataType.INT64,
|
|
|
|
|
|
["FLOAT"] = TSDataType.FLOAT,
|
|
|
|
|
|
["DOUBLE"] = TSDataType.DOUBLE,
|
|
|
|
|
|
["TEXT"] = TSDataType.TEXT,
|
|
|
|
|
|
["NULLTYPE"] = TSDataType.NONE,
|
2025-04-21 22:57:49 +08:00
|
|
|
|
["DATETIME"] = TSDataType.TIMESTAMP,
|
2025-04-03 15:38:31 +08:00
|
|
|
|
["DATE"] = TSDataType.DATE,
|
|
|
|
|
|
["BLOB"] = TSDataType.BLOB,
|
2025-04-08 17:44:42 +08:00
|
|
|
|
["DECIMAL"] = TSDataType.STRING,
|
2025-04-03 15:38:31 +08:00
|
|
|
|
["STRING"] = TSDataType.STRING
|
|
|
|
|
|
};
|
2025-04-08 17:44:42 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据类型名称获取 IoTDB 数据默认值
|
|
|
|
|
|
/// </summary>
|
2025-04-11 11:56:23 +08:00
|
|
|
|
private readonly IReadOnlyDictionary<string, object> DataTypeDefaultValueMap =
|
2025-04-08 17:44:42 +08:00
|
|
|
|
new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
|
{
|
|
|
|
|
|
["BOOLEAN"] = false,
|
|
|
|
|
|
["INT32"] = 0,
|
|
|
|
|
|
["INT64"] = 0,
|
|
|
|
|
|
["FLOAT"] = 0.0f,
|
|
|
|
|
|
["DOUBLE"] = 0.0d,
|
|
|
|
|
|
["TEXT"] = string.Empty,
|
|
|
|
|
|
["NULLTYPE"] = null,
|
2025-04-21 22:57:49 +08:00
|
|
|
|
["DATETIME"] = null,
|
2025-04-08 17:44:42 +08:00
|
|
|
|
["DATE"] = null,
|
|
|
|
|
|
["BLOB"] = null,
|
|
|
|
|
|
["DECIMAL"] = "0.0",
|
|
|
|
|
|
["STRING"] = string.Empty
|
|
|
|
|
|
};
|
2025-04-22 22:11:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// IoTDB 数据类型与.net类型映射
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="tSDataType"></param>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private dynamic GetTSDataValue(TSDataType tSDataType, object value) =>
|
|
|
|
|
|
tSDataType switch
|
|
|
|
|
|
{
|
|
|
|
|
|
TSDataType.BOOLEAN => Convert.ToBoolean(value),
|
|
|
|
|
|
TSDataType.INT32 => Convert.ToInt32(value),
|
|
|
|
|
|
TSDataType.INT64 => Convert.ToInt64(value),
|
|
|
|
|
|
TSDataType.FLOAT => Convert.ToDouble(value),
|
|
|
|
|
|
TSDataType.DOUBLE => Convert.ToDouble(value),
|
|
|
|
|
|
TSDataType.TEXT => Convert.ToString(value),
|
|
|
|
|
|
TSDataType.NONE => null,
|
|
|
|
|
|
TSDataType.TIMESTAMP => Convert.ToInt64(value),
|
|
|
|
|
|
TSDataType.DATE => Convert.ToDateTime(value),
|
|
|
|
|
|
TSDataType.BLOB => Convert.ToByte(value),
|
|
|
|
|
|
TSDataType.STRING => Convert.ToString(value),
|
|
|
|
|
|
_ => Convert.ToString(value)
|
|
|
|
|
|
};
|
2025-05-08 22:44:01 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 缓存实体属性信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="accessor"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private Dictionary<string, EntityMemberInfo> BuildMemberCache<T>(ISourceEntityAccessor<T> accessor)
|
|
|
|
|
|
{
|
|
|
|
|
|
var cache = new Dictionary<string, EntityMemberInfo>(StringComparer.Ordinal);
|
|
|
|
|
|
foreach (var member in accessor.MemberList)
|
|
|
|
|
|
{
|
|
|
|
|
|
cache[member.NameOrPath] = member;
|
|
|
|
|
|
}
|
|
|
|
|
|
return cache;
|
|
|
|
|
|
}
|
2025-04-02 14:06:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|