- 修复 DateOnly/TimeOnly 映射问题;#1868 #1855 #1763 #939 #991

This commit is contained in:
2881099
2024-08-21 02:55:16 +08:00
parent 486015a3c2
commit 2334fe2450
36 changed files with 584 additions and 76 deletions

View File

@ -71,7 +71,7 @@ namespace FreeSql.Duckdb
else if (param is TimeOnly || param is TimeOnly?)
{
var ts = (TimeOnly)param;
return $"'{ts.Hour}:{ts.Minute}:{ts.Second}.{ts.Millisecond}'";
return $"time '{ts.Hour}:{ts.Minute}:{ts.Second}'";
}
#endif

View File

@ -41,7 +41,7 @@ namespace FreeSql.Duckdb
{ typeof(DateTime).FullName, CsToDb.New(DuckDBType.Timestamp, "timestamp", "timestamp NOT NULL", false, false, new DateTime(1970,1,1)) },{ typeof(DateTime?).FullName, CsToDb.New(DuckDBType.Timestamp, "timestamp", "timestamp", false, true, null) },
#if net60
{ typeof(TimeOnly).FullName, CsToDb.New(DuckDBType.Time, "time", "time NOT NULL", false, false, 0) },{ typeof(TimeOnly?).FullName, CsToDb.New(DuckDBType.Time, "time", "time", false, true, null) },
{ typeof(TimeOnly).FullName, CsToDb.New(DuckDBType.Time, "time", "time NOT NULL", false, false, TimeOnly.MinValue) },{ typeof(TimeOnly?).FullName, CsToDb.New(DuckDBType.Time, "time", "time", false, true, null) },
{ typeof(DateOnly).FullName, CsToDb.New(DuckDBType.Date, "date", "date NOT NULL", false, false, new DateTime(1970,1,1)) },{ typeof(DateOnly?).FullName, CsToDb.New(DuckDBType.Date, "date", "date", false, true, null) },
#endif
@ -53,17 +53,47 @@ 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)
{
//int[]..List<int>
var isarray = type.FullName != "System.Byte[]" && type.IsArray;
var elementType = isarray ? type.GetElementType() : type;
Type elementType = isarray ? type.GetElementType().NullableTypeOrThis() : type;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
{
isarray = true;
elementType = type.GenericTypeArguments[0].NullableTypeOrThis();
}
var info = GetDbInfoNoneArray(elementType);
if (info == null) return null;
if (isarray == false) return new DbInfoResult((int)info.type, info.dbtype, info.dbtypeFull, info.isnullable, info.defaultValue);
var dbtypefull = Regex.Replace(info.dbtypeFull, $@"{info.dbtype}(\s*\([^\)]+\))?", "$0[]").Replace(" NOT NULL", "");
return new DbInfoResult((int)(info.type | DuckDBType.Array), $"{info.dbtype}[]", dbtypefull, null, Array.CreateInstance(elementType, 0));
if (isarray)
{
var dbtypefull = Regex.Replace(info.dbtypeFull, $@"{info.dbtype}(\s*\([^\)]+\))?", "$0[]").Replace(" NOT NULL", "");
return new DbInfoResult((int)(info.type | DuckDBType.Array), $"{info.dbtype}[]", dbtypefull, null, Array.CreateInstance(elementType, 0));
}
if (type.IsGenericType)
{
var typeDefinition = type.GetGenericTypeDefinition();
//struct
if (typeDefinition == typeof(Dictionary<string, object>))
{
}
//map
if (typeDefinition == typeof(Dictionary<,>))
{
var tkeyInfo = GetDbInfoNoneArray(type.GenericTypeArguments[0].NullableTypeOrThis());
if (tkeyInfo == null) return null;
var tvalInfo = GetDbInfoNoneArray(type.GenericTypeArguments[1].NullableTypeOrThis());
if (tvalInfo == null) return null;
var dbtype = $"map({tkeyInfo.dbtype},{tvalInfo.dbtype})";
return new DbInfoResult((int)(info.type | DuckDBType.Array), dbtype, dbtype, null, Array.CreateInstance(elementType, 0));
}
}
return new DbInfoResult((int)info.type, info.dbtype, info.dbtypeFull, info.isnullable, info.defaultValue);
}
CsToDb<DuckDBType> GetDbInfoNoneArray(Type type)
{

View File

@ -17,6 +17,10 @@ namespace FreeSql.Duckdb
{
if (Interlocked.Exchange(ref _firstInit, 0) == 1) //不能放在 static ctor .NetFramework 可能报初始化类型错误
{
#if net60
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(DateOnly)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(TimeOnly)] = true;
#endif
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(BigInteger)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(BitArray)] = true;
Select0Provider._dicMethodDataReaderGetValue[typeof(Guid)] = typeof(DbDataReader).GetMethod("GetGuid", new Type[] { typeof(int) });

View File

@ -18,7 +18,7 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
<Version>3.5.100-preview20240725</Version>
<Version>3.5.100-preview20240819</Version>
<PackageReadmeFile>readme.md</PackageReadmeFile>
</PropertyGroup>