类型映射小功告成

This commit is contained in:
28810 2018-12-26 21:18:59 +08:00
parent dbe6451c56
commit 9711b1d9a8
4 changed files with 74 additions and 10 deletions

View File

@ -41,10 +41,10 @@
| (IPAddress Address, int Subnet) \| (IPAddress Address, int Subnet)? | - | - | cidr | | (IPAddress Address, int Subnet) \| (IPAddress Address, int Subnet)? | - | - | cidr |
| IPAddress | - | - | inet | | IPAddress | - | - | inet |
| PhysicalAddress | - | - | macaddr | | PhysicalAddress | - | - | macaddr |
| NpgsqlRange<int> \| NpgsqlRange<int>? | - | - | int4range | | NpgsqlRange\<int\> \| NpgsqlRange\<int\>? | - | - | int4range |
| NpgsqlRange<long> \| NpgsqlRange<long>? | - | - | int8range | | NpgsqlRange\<long\> \| NpgsqlRange\<long\>? | - | - | int8range |
| NpgsqlRange<decimal> \| NpgsqlRange<decimal>? | - | - | numrange | | NpgsqlRange\<decimal\> \| NpgsqlRange\<decimal\>? | - | - | numrange |
| NpgsqlRange<DateTime> \| NpgsqlRange<DateTime>? | - | - | tsrange | | NpgsqlRange\<DateTime\> \| NpgsqlRange\<DateTime\>? | - | - | tsrange |
| PostgisPoint | - | - | geometry | | PostgisPoint | - | - | geometry |
| PostgisLineString | - | - | geometry | | PostgisLineString | - | - | geometry |
| PostgisPolygon | - | - | geometry | | PostgisPolygon | - | - | geometry |
@ -52,6 +52,7 @@
| PostgisMultiLineString | - | - | geometry | | PostgisMultiLineString | - | - | geometry |
| PostgisMultiPolygon | - | - | geometry | | PostgisMultiPolygon | - | - | geometry |
| PostgisGeometry | - | - | geometry | | PostgisGeometry | - | - | geometry |
| PostgisGeometryCollection | - | - | geometry |
| Dictionary<string, string> | - | - | hstore | | Dictionary<string, string> | - | - | hstore |
| JToken | - | - | jsonb | | JToken | - | - | jsonb |
| JObject | - | - | jsonb | | JObject | - | - | jsonb |

View File

@ -275,7 +275,7 @@ namespace FreeSql.Tests.PostgreSQL {
testFielLongArrayNullable = new long?[] { 500, 600, 700, null, 999, 1000 }, testFielLongArrayNullable = new long?[] { 500, 600, 700, null, 999, 1000 },
testFielLongNullable = long.MinValue testFielLongNullable = long.MinValue
}; };
item2.Id = (int)insert.AppendData(item2).ExecuteIdentity(); var item3 = insert.AppendData(item2).ExecuteInserted();
var newitem2 = select.Where(a => a.Id == item2.Id).ToOne(); var newitem2 = select.Where(a => a.Id == item2.Id).ToOne();
var items = select.ToList(); var items = select.ToList();

View File

@ -90,9 +90,8 @@ namespace FreeSql.Tests.SqlServer {
testFieldUShort = ushort.MaxValue, testFieldUShort = ushort.MaxValue,
testFieldUShortNullable = ushort.MinValue, testFieldUShortNullable = ushort.MinValue,
testFielLongNullable = long.MinValue testFielLongNullable = long.MinValue
}; };
item2.Id = (int)insert.AppendData(item2).ExecuteIdentity(); var item3 = insert.AppendData(item2).ExecuteInserted();
var newitem2 = select.Where(a => a.Id == item2.Id).ToOne(); var newitem2 = select.Where(a => a.Id == item2.Id).ToOne();
var items = select.ToList(); var items = select.ToList();

View File

@ -1,11 +1,16 @@
using FreeSql.DataAnnotations; using FreeSql.DataAnnotations;
using FreeSql.Internal.Model; using FreeSql.Internal.Model;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Npgsql.LegacyPostgis;
using NpgsqlTypes;
using System; using System;
using System.Collections;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
@ -92,7 +97,66 @@ namespace FreeSql.Internal {
return ret.ToArray(); return ret.ToArray();
} }
static Dictionary<string, bool> dicExecuteArrayRowReadClassOrTuple = new Dictionary<string, bool> {
{ typeof(bool ).FullName, true },
{ typeof(sbyte).FullName, true },
{ typeof(short).FullName, true },
{ typeof(int).FullName, true },
{ typeof(long).FullName, true },
{ typeof(byte).FullName, true },
{ typeof(ushort).FullName, true },
{ typeof(uint).FullName, true },
{ typeof(ulong).FullName, true },
{ typeof(double).FullName, true },
{ typeof(float).FullName, true },
{ typeof(decimal).FullName, true },
{ typeof(TimeSpan).FullName, true },
{ typeof(DateTime).FullName, true },
{ typeof(DateTimeOffset).FullName, true },
{ typeof(byte[]).FullName, true },
{ typeof(string).FullName, true },
{ typeof(Guid).FullName, true },
{ typeof(MygisPoint).FullName, true },
{ typeof(MygisLineString).FullName, true },
{ typeof(MygisPolygon).FullName, true },
{ typeof(MygisMultiPoint).FullName, true },
{ typeof(MygisMultiLineString).FullName, true },
{ typeof(MygisMultiPolygon).FullName, true },
{ typeof(BitArray).FullName, true },
{ typeof(NpgsqlPoint).FullName, true },
{ typeof(NpgsqlLine).FullName, true },
{ typeof(NpgsqlLSeg).FullName, true },
{ typeof(NpgsqlBox).FullName, true },
{ typeof(NpgsqlPath).FullName, true },
{ typeof(NpgsqlPolygon).FullName, true },
{ typeof(NpgsqlCircle).FullName, true },
{ typeof((IPAddress Address, int Subnet)).FullName, true },
{ typeof(IPAddress).FullName, true },
{ typeof(PhysicalAddress).FullName, true },
{ typeof(NpgsqlRange<int>).FullName, true },
{ typeof(NpgsqlRange<long>).FullName, true },
{ typeof(NpgsqlRange<decimal>).FullName, true },
{ typeof(NpgsqlRange<DateTime>).FullName, true },
{ typeof(PostgisPoint).FullName, true },
{ typeof(PostgisLineString).FullName, true },
{ typeof(PostgisPolygon).FullName, true },
{ typeof(PostgisMultiPoint).FullName, true },
{ typeof(PostgisMultiLineString).FullName, true },
{ typeof(PostgisMultiPolygon).FullName, true },
{ typeof(PostgisGeometry).FullName, true },
{ typeof(PostgisGeometryCollection).FullName, true },
{ typeof(Dictionary<string, string>).FullName, true },
{ typeof(JToken).FullName, true },
{ typeof(JObject).FullName, true },
{ typeof(JArray).FullName, true },
};
internal static (object value, int dataIndex) ExecuteArrayRowReadClassOrTuple(Type type, Dictionary<string, int> names, object[] row, int dataIndex = 0) { internal static (object value, int dataIndex) ExecuteArrayRowReadClassOrTuple(Type type, Dictionary<string, int> names, object[] row, int dataIndex = 0) {
if (type.IsArray) return (GetDataReaderValue(type, row[dataIndex]), dataIndex + 1);
var typeGeneric = type;
if (typeGeneric.FullName.StartsWith("System.Nullable`1[")) typeGeneric = type.GenericTypeArguments.First();
if (typeGeneric.IsEnum ||
dicExecuteArrayRowReadClassOrTuple.ContainsKey(typeGeneric.FullName))
return (GetDataReaderValue(type, row[dataIndex]), dataIndex + 1);
if (type.Namespace == "System" && (type.FullName == "System.String" || type.IsValueType)) { //值类型,或者元组 if (type.Namespace == "System" && (type.FullName == "System.String" || type.IsValueType)) { //值类型,或者元组
bool isTuple = type.Name.StartsWith("ValueTuple`"); bool isTuple = type.Name.StartsWith("ValueTuple`");
if (isTuple) { if (isTuple) {
@ -108,7 +172,7 @@ namespace FreeSql.Internal {
var constructor = type.GetConstructor(types); var constructor = type.GetConstructor(types);
return (constructor?.Invoke(parms), dataIndex); return (constructor?.Invoke(parms), dataIndex);
} }
return (dataIndex >= row.Length || row[dataIndex] == DBNull.Value ? null : Convert.ChangeType(row[dataIndex], type), dataIndex + 1); return (dataIndex >= row.Length || (row[dataIndex] ?? DBNull.Value) == DBNull.Value ? null : GetDataReaderValue(type, row[dataIndex]), dataIndex + 1);
} }
if (type == typeof(object) && names != null) { if (type == typeof(object) && names != null) {
dynamic expando = new System.Dynamic.ExpandoObject(); //动态类型字段 可读可写 dynamic expando = new System.Dynamic.ExpandoObject(); //动态类型字段 可读可写
@ -118,8 +182,7 @@ namespace FreeSql.Internal {
return (expando, names.Count); return (expando, names.Count);
} }
//类注入属性 //类注入属性
var consturct = type.GetConstructor(new Type[0]); var value = type.GetConstructor(new Type[0]).Invoke(new object[0]);
var value = consturct.Invoke(new object[0]);
var ps = type.GetProperties(); var ps = type.GetProperties();
foreach(var p in ps) { foreach(var p in ps) {
var tryidx = dataIndex; var tryidx = dataIndex;
@ -150,6 +213,7 @@ namespace FreeSql.Internal {
if (type.IsArray) { if (type.IsArray) {
var elementType = type.GetElementType(); var elementType = type.GetElementType();
var valueArr = value as Array; var valueArr = value as Array;
if (elementType == valueArr.GetType().GetElementType()) return value;
var len = valueArr.GetLength(0); var len = valueArr.GetLength(0);
var ret = Array.CreateInstance(elementType, len); var ret = Array.CreateInstance(elementType, len);
for (var a = 0; a < len; a++) { for (var a = 0; a < len; a++) {