TDengine CodeFirst完善

This commit is contained in:
d4ilys
2024-09-13 14:18:36 +08:00
parent 962a5b9ba7
commit b4071a1f32
8 changed files with 259 additions and 62 deletions

View File

@@ -228,7 +228,7 @@ namespace FreeSql.Internal
if (common.CodeFirst.IsSyncStructureToLower) colattr.Name = colattr.Name.ToLower();
if (common.CodeFirst.IsSyncStructureToUpper) colattr.Name = colattr.Name.ToUpper();
if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false && common._orm.Ado.DataType != DataType.ClickHouse)
if ((colattr.IsNullable != true || colattr.IsIdentity == true || colattr.IsPrimary == true) && colattr.DbType.Contains("NOT NULL") == false && common._orm.Ado.DataType != DataType.ClickHouse && common._orm.Ado.DataType != DataType.TDengine)
{
colattr.IsNullable = false;
colattr.DbType = Regex.Replace(colattr.DbType, @"\bNULL\b", "").Trim() + " NOT NULL";
@@ -423,6 +423,10 @@ namespace FreeSql.Internal
else colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.TDengine:
colattr.DbType = Regex.Replace(colattr.DbType, charPattern, m =>
replaceCounter++ == 0 ? $"{m.Groups[1].Value}({strlen})" : m.Groups[0].Value);
break;
case DataType.MsAccess:
charPattern = @"(CHAR|CHAR2|CHARACTER|TEXT)\s*(\([^\)]*\))?";
if (strlen < 0) colattr.DbType = $"LONGTEXT{strNotNull}";

View File

@@ -10,7 +10,7 @@ namespace FreeSql.DataAnnotations
/// TDengine 超级表-子表
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class SubTableAttribute : TableAttribute
public class TDengineSubTableAttribute : TableAttribute
{
/// <summary>
/// 超表名称

View File

@@ -10,11 +10,8 @@ namespace FreeSql.DataAnnotations
/// TDengine 超级表
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class SuperTableAttribute : TableAttribute
public class TDengineSuperTableAttribute : TableAttribute
{
/// <summary>
/// 超表名称
/// </summary>
public string STableName { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FreeSql.DataAnnotations;
namespace FreeSql.Provider.TDengine.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class TDengineTagAttribute : ColumnAttribute
{
}
}

View File

@@ -156,18 +156,6 @@ namespace FreeSql.TDengine.Curd
return sb.Append(_tosqlAppendContent).ToString();
}
internal static string TDengineTableNameAdapter(ref TableInfo tableInfo, ref string tableName,
ref CommonUtils utils)
{
if (utils is TDengineUtils tDengineUtils)
{
return tDengineUtils.GenerateSTableName(tableName, tableInfo.Type);
}
return tableName;
}
public override ISelect<T1, T2> From<T2>(
Expression<Func<ISelectFromExpression<T1>, T2, ISelectFromExpression<T1>>> exp)
{

View File

@@ -6,17 +6,17 @@ using System.Threading.Tasks;
namespace FreeSql.TDengine.Describes
{
internal struct STableDescribe
internal class SuperTableDescribe
{
/// <summary>
/// 是否是超表
/// 超级表Type
/// </summary>
public bool IsSTable { get; set; }
public Type SuperTableType { get; set; }
/// <summary>
/// 超表名称
/// 超表名称
/// </summary>
public string STableName { get; set; }
public string SuperTableName { get; set; }
}
}

View File

@@ -3,7 +3,13 @@ using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using FreeSql.DataAnnotations;
using System.Reflection;
using FreeSql.Internal.ObjectPool;
using FreeSql.Provider.TDengine.Attributes;
namespace FreeSql.TDengine
{
@@ -14,36 +20,27 @@ namespace FreeSql.TDengine
{
}
static Dictionary<string, CsToDb<DbType>> _dicCsToDb = new Dictionary<string, CsToDb<DbType>>()
static readonly Dictionary<string, CsToDb<DbType>> DicCsToDb = new Dictionary<string, CsToDb<DbType>>()
{
{ typeof(bool).FullName, CsToDb.New(DbType.Boolean, "BOOL", "BOOL", null, false, null) },
{ typeof(bool?).FullName, CsToDb.New(DbType.Boolean, "BOOL", "BOOL", null, true, null) },
{ typeof(DateTime).FullName, CsToDb.New(DbType.DateTime, "TIMESTAMP", "TIMESTAMP", null, false, null) },
{ typeof(DateTime?).FullName, CsToDb.New(DbType.DateTime, "TIMESTAMP", "TIMESTAMP", null, true, null) },
{ typeof(TimeSpan).FullName, CsToDb.New(DbType.DateTime, "TIMESTAMP", "TIMESTAMP", null, false, null) },
{ typeof(TimeSpan?).FullName, CsToDb.New(DbType.DateTime, "TIMESTAMP", "TIMESTAMP", null, true, null) },
{ typeof(short).FullName, CsToDb.New(DbType.Int16, "SMALLINT", "SMALLINT", null, false, 0) },
{ typeof(short?).FullName, CsToDb.New(DbType.Int16, "SMALLINT", "SMALLINT", null, true, null) },
{ typeof(int).FullName, CsToDb.New(DbType.Int32, "INT", "INT", null, false, 0) },
{ typeof(int?).FullName, CsToDb.New(DbType.Int32, "INT", "INT", null, true, null) },
{ typeof(sbyte).FullName, CsToDb.New(DbType.SByte, "TINYINT", "TINYINT", null, false, 0) },
{ typeof(sbyte?).FullName, CsToDb.New(DbType.SByte, "TINYINT", "TINYINT", null, true, null) },
{ typeof(long).FullName, CsToDb.New(DbType.Int64, "BIGINT", "BIGINT", null, false, 0) },
{ typeof(long?).FullName, CsToDb.New(DbType.Int64, "BIGINT", "BIGINT", null, true, null) },
{ typeof(byte).FullName, CsToDb.New(DbType.Byte, "TINYINT UNSIGNED", "TINYINT UNSIGNED", null, false, 0) },
{
typeof(byte?).FullName,
CsToDb.New(DbType.Byte, "TINYINT UNSIGNED", "TINYINT UNSIGNED", null, true, null)
},
{
typeof(ushort).FullName,
CsToDb.New(DbType.UInt16, "SMALLINT UNSIGNED", "SMALLINT UNSIGNED", null, false, 0)
@@ -52,35 +49,234 @@ namespace FreeSql.TDengine
typeof(ushort?).FullName,
CsToDb.New(DbType.UInt16, "SMALLINT UNSIGNED", "SMALLINT UNSIGNED", null, true, null)
},
{ typeof(uint).FullName, CsToDb.New(DbType.UInt32, "INT UNSIGNED", "INT UNSIGNED", null, false, 0) },
{ typeof(uint?).FullName, CsToDb.New(DbType.UInt32, "INT UNSIGNED", "INT UNSIGNED", null, true, null) },
{ typeof(ulong).FullName, CsToDb.New(DbType.UInt64, "BIGINT UNSIGNED", "BIGINT UNSIGNED", null, false, 0) },
{
typeof(ulong?).FullName,
CsToDb.New(DbType.UInt64, "BIGINT UNSIGNED", "BIGINT UNSIGNED", null, true, null)
},
{ typeof(float).FullName, CsToDb.New(DbType.Single, "FLOAT", "FLOAT", null, false, 0) },
{ typeof(float?).FullName, CsToDb.New(DbType.Single, "FLOAT", "FLOAT", null, true, null) },
{ typeof(double).FullName, CsToDb.New(DbType.Double, "DOUBLE", "DOUBLE", null, false, 0) },
{ typeof(double?).FullName, CsToDb.New(DbType.Double, "DOUBLE", "DOUBLE", null, true, null) },
{ typeof(string).FullName, CsToDb.New(DbType.String, "NCHAR", "NCHAR", null, false, 0) },
{ typeof(string).FullName, CsToDb.New(DbType.String, "NCHAR", "NCHAR(255)", null, false, 0) },
};
public override DbInfoResult GetDbInfo(Type type)
{
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new DbInfoResult((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue);
if (DicCsToDb.TryGetValue(type.FullName, out var trydc))
return new DbInfoResult((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable,
trydc.defaultValue);
if (type.IsArray) return null;
return null;
}
protected override string GetComparisonDDLStatements(params TypeSchemaAndName[] objects)
{
throw new NotImplementedException();
Object<DbConnection> conn = null;
string database = null;
var sb = new StringBuilder();
try
{
conn = _orm.Ado.MasterPool.Get(TimeSpan.FromSeconds(5));
database = conn.Value.Database;
foreach (var obj in objects)
{
if (sb.Length > 0) sb.Append(Environment.NewLine);
var tb = obj.tableSchema;
if (tb == null)
throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.tableSchema.Type.FullName));
if (tb.Columns.Any() == false)
throw new Exception(
CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.tableSchema.Type.FullName));
var tbName = _commonUtils.SplitTableName(tb.DbName).First();
tbName = _commonUtils.QuoteSqlName(database, tbName);
if (!TryTableExists(tbName))
{
SuperTableHandle(ref tb, ref database, tb.Type, ref sb);
}
}
}
finally
{
try
{
if (string.IsNullOrEmpty(database) == false)
conn.Value.ChangeDatabase(database);
_orm.Ado.MasterPool.Return(conn);
}
catch
{
_orm.Ado.MasterPool.Return(conn, true);
}
}
var ddl = sb.Length == 0 ? null : sb.ToString();
Console.WriteLine(ddl);
return ddl;
}
private void CreateColumns(ref TableInfo tb, ref StringBuilder sb)
{
//创建表
foreach (var columnInfo in tb.ColumnsByPosition.Where(c =>
!c.Table.Properties[c.CsName].IsDefined(typeof(TDengineTagAttribute))))
{
sb.Append($" {Environment.NewLine} ").Append(_commonUtils.QuoteSqlName(columnInfo.Attribute.Name))
.Append(" ")
.Append(columnInfo.Attribute.DbType);
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1).Append($"{Environment.NewLine})");
}
private void SuperTableHandle(ref TableInfo tb, ref string database, Type type, ref StringBuilder sb)
{
//判断是否超级表
var subTableAttribute = type.GetCustomAttribute<TDengineSubTableAttribute>();
//要创建表的为子表
if (subTableAttribute != null)
{
if (_commonUtils is TDengineUtils utils)
{
var superTableDescribe = utils.GetSuperTableDescribe(type);
if (superTableDescribe == null) return;
var superTableName = _commonUtils.QuoteSqlName(database, superTableDescribe.SuperTableName);
//判断超表是否存在
if (!TryTableExists(superTableName))
{
//先创建超级表
CreateSuperTable(ref tb, ref sb, superTableName);
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
}
var subTableName = _commonUtils.QuoteSqlName(database, subTableAttribute.Name);
//创建子表
CreateSubTable(ref tb, ref sb, superTableName, subTableName);
}
}
//要创建的为超级表
else if (type.IsDefined(typeof(TDengineSuperTableAttribute)))
{
var superTableAttribute = type.GetCustomAttribute<TDengineSuperTableAttribute>();
if (superTableAttribute == null) return;
var tbName = _commonUtils.QuoteSqlName(database, superTableAttribute.Name);
CreateSuperTable(ref tb, ref sb, tbName);
}
//创建普通表
else
{
}
}
/// <summary>
/// 创建子表
/// </summary>
/// <param name="tb"></param>
/// <param name="sb"></param>
/// <param name="superTableName"></param>
private void CreateSubTable(ref TableInfo tb, ref StringBuilder sb, string superTableName, string subTableName)
{
sb.Append($"CREATE TABLE {subTableName}{Environment.NewLine}");
sb.Append($"USING {superTableName} (");
var columnInfos = tb.ColumnsByPosition.Where(c =>
c.Table.Properties[c.CsName].IsDefined(typeof(TDengineTagAttribute)));
var enumerable = columnInfos as ColumnInfo[] ?? columnInfos.ToArray();
var tagValues = new List<object>(enumerable.Count());
var tableInstance = Activator.CreateInstance(tb.Type);
foreach (var columnInfo in enumerable)
{
var tagValue = columnInfo.Table.Properties[columnInfo.CsName].GetValue(tableInstance);
tagValues.Add(tagValue);
sb.Append($" {Environment.NewLine} ").Append(_commonUtils.QuoteSqlName(columnInfo.Attribute.Name)).Append(",");
}
sb.Remove(sb.Length - 1, 1).Append($"{Environment.NewLine}) TAGS (");
foreach (var tagValue in tagValues)
{
sb.Append($" {Environment.NewLine} ").Append(HandleTagValue(tagValue)).Append(",");
}
sb.Remove(sb.Length - 1, 1).Append($"{Environment.NewLine});");
}
/// <summary>
/// 创建超级表
/// </summary>
/// <param name="tb"></param>
/// <param name="sb"></param>
/// <param name="superTableName"></param>
private void CreateSuperTable(ref TableInfo tb, ref StringBuilder sb, string superTableName)
{
sb.Append($"CREATE STABLE {superTableName} (");
CreateColumns(ref tb, ref sb);
sb.Append($" TAGS (");
foreach (var columnInfo in tb.ColumnsByPosition.Where(c =>
c.Table.Properties[c.CsName].IsDefined(typeof(TDengineTagAttribute))))
{
sb.Append($" {Environment.NewLine} ").Append(_commonUtils.QuoteSqlName(columnInfo.Attribute.Name))
.Append(" ")
.Append(columnInfo.Attribute.DbType);
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1).Append($"{Environment.NewLine});");
}
private bool TryTableExists(string tbName)
{
var flag = true;
try
{
var executeScalar = _orm.Ado.ExecuteScalar(CommandType.Text,
$"DESCRIBE {tbName}");
if (executeScalar == null)
{
flag = false;
}
}
catch (Exception e)
{
if (e.Message.Contains("Table does not exist"))
{
flag = false;
}
}
return flag;
}
private object HandleTagValue(object tagValue)
{
if (tagValue is DateTime || tagValue is string)
{
return $"\"{tagValue}\"";
}
else
{
return tagValue;
}
}
}
}

View File

@@ -164,7 +164,7 @@ namespace FreeSql.TDengine
public override string[] SplitTableName(string name)
{
throw new NotImplementedException();
return new[] { name };
}
public override string StringConcat(string[] objs, Type[] types)
@@ -183,33 +183,31 @@ namespace FreeSql.TDengine
}
//超表描述
private static readonly ConcurrentDictionary<string, Lazy<STableDescribe>> STableDescribes =
new ConcurrentDictionary<string, Lazy<STableDescribe>>();
private static readonly ConcurrentDictionary<Type, Lazy<SuperTableDescribe>> STableDescribes =
new ConcurrentDictionary<Type, Lazy<SuperTableDescribe>>();
/// <summary>
/// 生成超级表
/// 通过子表获取超级表描述
/// </summary>
/// <param name="tableName"></param>
/// <param name="subTableType"></param>
/// <returns></returns>
internal string GenerateSTableName(string tableName, Type tableType)
internal SuperTableDescribe GetSuperTableDescribe(Type subTableType)
{
var stableDescribe
= STableDescribes.GetOrAdd(tableName, key => new Lazy<STableDescribe>(() =>
var stableDescribe = STableDescribes.GetOrAdd(subTableType, key => new Lazy<SuperTableDescribe>(() =>
{
var describe = new STableDescribe
var sTableAttribute = subTableType.GetCustomAttribute<TDengineSubTableAttribute>();
if (sTableAttribute == null) return null;
var describe = new SuperTableDescribe
{
IsSTable = false
SuperTableName = sTableAttribute.SuperTableName,
SuperTableType = subTableType.BaseType
};
var sTableAttribute = tableType.GetCustomAttribute<SuperTableAttribute>();
if (sTableAttribute == null) return describe;
describe.IsSTable = true;
describe.STableName = sTableAttribute.STableName;
return describe;
}));
var stableDescribeValue = stableDescribe.Value;
return !stableDescribeValue.IsSTable ? tableName : $"{stableDescribeValue.STableName}.{tableName}";
return stableDescribeValue;
}
}
}