Skip to content

Commit 69ef7b1

Browse files
committed
- 完善 ClickHouse/QuestDB/TDengine 支持 enum 映射;
1 parent 1db9561 commit 69ef7b1

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

Providers/FreeSql.Provider.ClickHouse/ClickHouseAdo/ClickHouseAdo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override object AddslashesProcessParam(object param, Type mapType, Column
5454
else if (param is char)
5555
return string.Concat("'", param.ToString().Replace("'", "''").Replace("\\", "\\\\").Replace('\0', ' '), "'");
5656
else if (param is Enum)
57-
return AddslashesTypeHandler(param.GetType(), param) ?? string.Concat("'", param.ToString().Replace("'", "''").Replace("\\", "\\\\"), "'"); //((Enum)val).ToInt64();
57+
return AddslashesTypeHandler(param.GetType(), param) ?? ((Enum)param).ToInt64();
5858
else if (decimal.TryParse(string.Concat(param), out var trydec))
5959
return param;
6060

Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,35 @@ public ClickHouseCodeFirst(IFreeSql orm, CommonUtils commonUtils, CommonExpressi
8282

8383
public override DbInfoResult GetDbInfo(Type type)
8484
{
85-
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc))
86-
return new DbInfoResult((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable,
87-
trydc.defaultValue);
85+
var isarray = type.FullName != "System.Byte[]" && type.IsArray;
86+
var elementType = isarray ? type.GetElementType() : type;
87+
var info = GetDbInfoNoneArray(elementType);
88+
if (info == null) return null;
89+
if (isarray == false) return new DbInfoResult((int)info.type, info.dbtype, info.dbtypeFull, info.isnullable, info.defaultValue);
90+
return new DbInfoResult((int)DbType.Object, $"Array({info.dbtype})", $"Array({info.dbtype})", null, Array.CreateInstance(elementType, 0));
91+
}
8892

89-
//判断是否是集合
90-
var isCollection = IsArray(type);
91-
if (isCollection.Item1)
93+
CsToDb<DbType> GetDbInfoNoneArray(Type type)
94+
{
95+
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return trydc;
96+
if (type.IsArray) return null;
97+
var enumType = type.IsEnum ? type : null;
98+
if (enumType == null && type.IsNullableType() && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
99+
if (enumType != null)
92100
{
93-
var genericType = isCollection.Item2;
94-
var genericTypeName = genericType?.FullName;
95-
var tryGetValue = _dicCsToDb.TryGetValue(genericTypeName, out var value);
96-
if (tryGetValue)
101+
var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
102+
CsToDb.New(DbType.Int32, "Int32", $"{(type.IsEnum ? "Int32" : "Nullable(Int32)")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) :
103+
CsToDb.New(DbType.Int64, "Int64", $"{(type.IsEnum ? "Int64" : "Nullable(Int64)")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue());
104+
if (_dicCsToDb.ContainsKey(type.FullName) == false)
97105
{
98-
var arrayDbType = $"Array({value.dbtype})";
99-
var defaultArray = new ArrayList(0);
100-
return new DbInfoResult(Convert.ToInt32(DbType.Object), arrayDbType, arrayDbType, false, defaultArray);
106+
lock (_dicCsToDbLock)
107+
{
108+
if (_dicCsToDb.ContainsKey(type.FullName) == false)
109+
_dicCsToDb.Add(type.FullName, newItem);
110+
}
101111
}
112+
return newItem;
102113
}
103-
104114
return null;
105115
}
106116

Providers/FreeSql.Provider.QuestDb/QuestDbCodeFirst.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,25 @@ public QuestDbCodeFirst(IFreeSql orm, CommonUtils commonUtils, CommonExpression
7272
public override DbInfoResult GetDbInfo(Type type)
7373
{
7474
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc))
75-
return new DbInfoResult((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable,
76-
trydc.defaultValue);
77-
if (type.IsArray)
78-
return null;
75+
return new DbInfoResult((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue);
76+
if (type.IsArray) return null;
77+
var enumType = type.IsEnum ? type : null;
78+
if (enumType == null && type.IsNullableType() && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
79+
if (enumType != null)
80+
{
81+
var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
82+
CsToDb.New(NpgsqlDbType.Bigint, "int", $"int{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) :
83+
CsToDb.New(NpgsqlDbType.Integer, "long", $"long{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue());
84+
if (_dicCsToDb.ContainsKey(type.FullName) == false)
85+
{
86+
lock (_dicCsToDbLock)
87+
{
88+
if (_dicCsToDb.ContainsKey(type.FullName) == false)
89+
_dicCsToDb.Add(type.FullName, newItem);
90+
}
91+
}
92+
return new DbInfoResult((int)newItem.type, newItem.dbtype, newItem.dbtypeFull, newItem.isnullable, newItem.defaultValue);
93+
}
7994
return null;
8095
}
8196

Providers/FreeSql.Provider.TDengine/TDengineCodeFirst.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public TDengineCodeFirst(IFreeSql orm, CommonUtils commonUtils, CommonExpression
2020
{
2121
}
2222

23+
static object DicCsToDbLock = new object();
2324
static readonly Dictionary<string, CsToDb<DbType>> DicCsToDb = new Dictionary<string, CsToDb<DbType>>()
2425
{
2526
{ typeof(bool).FullName, CsToDb.New(DbType.Boolean, "BOOL", "BOOL", null, false, null) },
@@ -69,6 +70,23 @@ public override DbInfoResult GetDbInfo(Type type)
6970
return new DbInfoResult((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable,
7071
trydc.defaultValue);
7172
if (type.IsArray) return null;
73+
var enumType = type.IsEnum ? type : null;
74+
if (enumType == null && type.IsNullableType() && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
75+
if (enumType != null)
76+
{
77+
var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
78+
CsToDb.New(DbType.Int32, "int", $"int{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) :
79+
CsToDb.New(DbType.Int64, "long", $"long{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue());
80+
if (DicCsToDb.ContainsKey(type.FullName) == false)
81+
{
82+
lock (DicCsToDbLock)
83+
{
84+
if (DicCsToDb.ContainsKey(type.FullName) == false)
85+
DicCsToDb.Add(type.FullName, newItem);
86+
}
87+
}
88+
return new DbInfoResult((int)newItem.type, newItem.dbtype, newItem.dbtypeFull, newItem.isnullable, newItem.defaultValue);
89+
}
7290
return null;
7391
}
7492

0 commit comments

Comments
 (0)