|
3 | 3 | using System.Collections.Generic;
|
4 | 4 | using System.Data;
|
5 | 5 | using System.Linq;
|
| 6 | +using System.Net; |
| 7 | +using System.Net.NetworkInformation; |
6 | 8 | using System.Text;
|
7 | 9 | using System.Threading;
|
8 | 10 | using System.Threading.Tasks;
|
@@ -573,8 +575,58 @@ public override string GetLastInsertIdSqlSuffix<T>()
|
573 | 575 |
|
574 | 576 | return "; " + SelectIdentitySql;
|
575 | 577 | }
|
| 578 | + |
| 579 | + public Dictionary<Type,NpgsqlDbType> TypesMap { get; } = new Dictionary<Type, NpgsqlDbType> |
| 580 | + { |
| 581 | + [typeof(bool)] = NpgsqlDbType.Boolean, |
| 582 | + [typeof(short)] = NpgsqlDbType.Smallint, |
| 583 | + [typeof(int)] = NpgsqlDbType.Integer, |
| 584 | + [typeof(long)] = NpgsqlDbType.Bigint, |
| 585 | + [typeof(float)] = NpgsqlDbType.Real, |
| 586 | + [typeof(double)] = NpgsqlDbType.Double, |
| 587 | + [typeof(decimal)] = NpgsqlDbType.Numeric, |
| 588 | + [typeof(string)] = NpgsqlDbType.Text, |
| 589 | + [typeof(char[])] = NpgsqlDbType.Varchar, |
| 590 | + [typeof(char)] = NpgsqlDbType.Char, |
| 591 | + [typeof(NpgsqlPoint)] = NpgsqlDbType.Point, |
| 592 | + [typeof(NpgsqlLSeg)] = NpgsqlDbType.LSeg, |
| 593 | + [typeof(NpgsqlPath)] = NpgsqlDbType.Path, |
| 594 | + [typeof(NpgsqlPolygon)] = NpgsqlDbType.Polygon, |
| 595 | + [typeof(NpgsqlLine)] = NpgsqlDbType.Line, |
| 596 | + [typeof(NpgsqlCircle)] = NpgsqlDbType.Circle, |
| 597 | + [typeof(NpgsqlBox)] = NpgsqlDbType.Box, |
| 598 | + [typeof(BitArray)] = NpgsqlDbType.Varbit, |
| 599 | + [typeof(IDictionary<string, string>)] = NpgsqlDbType.Hstore, |
| 600 | + [typeof(Guid)] = NpgsqlDbType.Uuid, |
| 601 | + [typeof(NpgsqlInet)] = NpgsqlDbType.Cidr, |
| 602 | + [typeof(ValueTuple<IPAddress,int>)] = NpgsqlDbType.Inet, |
| 603 | + [typeof(IPAddress)] = NpgsqlDbType.Inet, |
| 604 | + [typeof(PhysicalAddress)] = NpgsqlDbType.MacAddr, |
| 605 | + [typeof(NpgsqlTsQuery)] = NpgsqlDbType.TsQuery, |
| 606 | + [typeof(NpgsqlTsVector)] = NpgsqlDbType.TsVector, |
| 607 | + [typeof(NpgsqlDate)] = NpgsqlDbType.Date, |
| 608 | + [typeof(DateTime)] = NpgsqlDbType.Timestamp, |
| 609 | + [typeof(DateTimeOffset)] = NpgsqlDbType.TimestampTz, |
| 610 | + [typeof(TimeSpan)] = NpgsqlDbType.Time, |
| 611 | + [typeof(NpgsqlTimeSpan)] = NpgsqlDbType.Time, |
| 612 | + [typeof(byte[])] = NpgsqlDbType.Bytea, |
| 613 | + [typeof(uint)] = NpgsqlDbType.Oid, |
| 614 | + [typeof(uint[])] = NpgsqlDbType.Oidvector, |
| 615 | + }; |
576 | 616 |
|
577 |
| - public static Dictionary<string, NpgsqlDbType> NativeTypes = new Dictionary<string, NpgsqlDbType> { |
| 617 | + public NpgsqlDbType GetDbType<T>() => GetDbType(typeof(T)); |
| 618 | + public NpgsqlDbType GetDbType(Type type) |
| 619 | + { |
| 620 | + if (PostgreSqlDialect.Instance.TypesMap.TryGetValue(type, out var paramType)) |
| 621 | + return paramType; |
| 622 | + var genericEnum = type.GetTypeWithGenericTypeDefinitionOf(typeof(IEnumerable<>)); |
| 623 | + if (genericEnum != null) |
| 624 | + return GetDbType(genericEnum.GenericTypeArguments[0]) | NpgsqlDbType.Array; |
| 625 | + |
| 626 | + throw new NotSupportedException($"Type '{type.Name}' not found in 'TypesMap'"); |
| 627 | + } |
| 628 | + |
| 629 | + public Dictionary<string, NpgsqlDbType> NativeTypes = new Dictionary<string, NpgsqlDbType> { |
578 | 630 | { "json", NpgsqlDbType.Json },
|
579 | 631 | { "jsonb", NpgsqlDbType.Jsonb },
|
580 | 632 | { "hstore", NpgsqlDbType.Hstore },
|
|
0 commit comments