完善 DuckDB

This commit is contained in:
2881099
2024-08-21 18:53:01 +08:00
parent 109ad58c1d
commit 8ab5372a09
6 changed files with 287 additions and 28 deletions

View File

@ -53,8 +53,6 @@ namespace FreeSql.Duckdb
{ typeof(BitArray).FullName, CsToDb.New(DuckDBType.Bit, "bit", "bit", false, null, new BitArray(new byte[64])) },
{ typeof(BigInteger).FullName, CsToDb.New(DuckDBType.HugeInt, "hugeint","hugeint NOT NULL", false, false, 0) },{ typeof(BigInteger?).FullName, CsToDb.New(DuckDBType.HugeInt, "hugeint","hugeint", false, true, null) },
{ typeof(Dictionary<string, object>).FullName, CsToDb.New(DuckDBType.Struct, "struct", "struct", false, null, new Dictionary<string, object>()) },
};
public override DbInfoResult GetDbInfo(Type type)
@ -77,11 +75,6 @@ namespace FreeSql.Duckdb
if (type.IsGenericType)
{
var typeDefinition = type.GetGenericTypeDefinition();
//struct
if (typeDefinition == typeof(Dictionary<string, object>))
{
}
//map
if (typeDefinition == typeof(Dictionary<,>))
{

View File

@ -167,6 +167,20 @@ namespace FreeSql.Duckdb
if (left.StartsWith("(") || left.EndsWith(")")) left = $"[{left.TrimStart('(').TrimEnd(')')}]";
return $"case when {left} is null then 0 else len({left}) end";
}
if (objType.IsGenericType && objType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
left = objExp == null ? null : getExp(objExp);
switch (callExp.Method.Name)
{
case "get_Item": return $"element_at({left},{getExp(callExp.Arguments[argIndex])})[1]";
case "ContainsKey": return $"len(element_at({left},{getExp(callExp.Arguments[argIndex])})) > 0";
case "GetLength":
case "GetLongLength":
case "Count": return $"cardinality({left})";
case "Keys": return $"map_keys({left})";
case "Values": return $"map_values({left})";
}
}
}
break;
case ExpressionType.MemberAccess:
@ -185,6 +199,16 @@ namespace FreeSql.Duckdb
case "Count": return $"case when {left} is null then 0 else len({left}) end";
}
}
if (memParentExp.IsGenericType && memParentExp.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
var left = getExp(memExp.Expression);
switch (memExp.Member.Name)
{
case "Count": return $"cardinality({left})";
case "Keys": return $"map_keys({left})";
case "Values": return $"map_values({left})";
}
}
}
break;
case ExpressionType.NewArrayInit:

View File

@ -1,11 +1,12 @@
using FreeSql.Internal;
using FreeSql.Duckdb.Curd;
using FreeSql.Internal;
using FreeSql.Internal.CommonProvider;
using FreeSql.Duckdb.Curd;
using System;
using System.Data.Common;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Numerics;
using System.Threading;
namespace FreeSql.Duckdb
{
@ -45,6 +46,16 @@ namespace FreeSql.Duckdb
this.CodeFirst = new DuckdbCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
this.DbFirst = new DuckdbDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
if (connectionFactory != null) this.CodeFirst.IsNoneCommandParameter = true;
this.Aop.ConfigEntityProperty += (s, e) =>
{
//duckdb map 类型
if (e.Property.PropertyType.IsGenericType && e.Property.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
Utils.dicExecuteArrayRowReadClassOrTuple[e.Property.PropertyType] = true;
e.ModifyResult.DbType = CodeFirst.GetDbInfo(e.Property.PropertyType)?.dbtype;
}
};
}
~DuckdbProvider() => this.Dispose();

View File

@ -184,23 +184,23 @@ namespace FreeSql.Duckdb
return sb.ToString();
}
//struct
if (typeDefinition == typeof(Dictionary<string, object>))
{
var dict = value as Dictionary<string, object>;
if (dict.Count == 0) return "NULL";
var sb = new StringBuilder("{");
var idx = 0;
foreach (var key in dict.Keys)
{
var val = dict[key];
if (val == null) continue;
if (idx > 0) sb.Append(",");
sb.Append("'").Append(FormatSql("{0}", val, 1)).Append("':");
sb.Append(GetNoneParamaterSqlValue(specialParams, specialParamFlag, col, val.GetType(), val));
idx++;
}
return sb.Append("}").ToString();
}
//if (typeDefinition == typeof(Dictionary<string, object>))
//{
// var dict = value as Dictionary<string, object>;
// if (dict.Count == 0) return "NULL";
// var sb = new StringBuilder("{");
// var idx = 0;
// foreach (var key in dict.Keys)
// {
// var val = dict[key];
// if (val == null) continue;
// if (idx > 0) sb.Append(",");
// sb.Append("'").Append(FormatSql("{0}", val, 1)).Append("':");
// sb.Append(GetNoneParamaterSqlValue(specialParams, specialParamFlag, col, val.GetType(), val));
// idx++;
// }
// return sb.Append("}").ToString();
//}
//map
if (typeDefinition == typeof(Dictionary<,>))
{