|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using JetBrains.Annotations; |
| 10 | + |
| 11 | +namespace Npgsql.PostgresTypes |
| 12 | +{ |
| 13 | + class PostgresBaseType : PostgresType |
| 14 | + { |
| 15 | + [CanBeNull] |
| 16 | + readonly DbType[] _dbTypes; |
| 17 | + [CanBeNull] |
| 18 | + readonly Type[] _clrTypes; |
| 19 | + |
| 20 | + [CanBeNull] |
| 21 | + ConstructorInfo _ctorWithRegistry; |
| 22 | + [CanBeNull] |
| 23 | + ConstructorInfo _ctorWithoutRegistry; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Constructs an unsupported base type (no handler exists in Npgsql for this type) |
| 27 | + /// </summary> |
| 28 | + internal PostgresBaseType(string ns, string name, uint oid) : base(ns, name, oid) |
| 29 | + { |
| 30 | + _dbTypes = new DbType[0]; |
| 31 | + _clrTypes = new Type[0]; |
| 32 | + } |
| 33 | + |
| 34 | + internal PostgresBaseType(string ns, string name, uint oid, Type handlerType, TypeMappingAttribute mapping) |
| 35 | + : base(ns, name, oid) |
| 36 | + { |
| 37 | + HandlerType = handlerType; |
| 38 | + NpgsqlDbType = mapping.NpgsqlDbType; |
| 39 | + _dbTypes = mapping.DbTypes; |
| 40 | + _clrTypes = mapping.ClrTypes; |
| 41 | + } |
| 42 | + |
| 43 | + internal override void AddTo(TypeHandlerRegistry.AvailablePostgresTypes types) |
| 44 | + { |
| 45 | + base.AddTo(types); |
| 46 | + |
| 47 | + if (_dbTypes != null) |
| 48 | + foreach (var dbType in _dbTypes) |
| 49 | + types.ByDbType[dbType] = this; |
| 50 | + if (_clrTypes != null) |
| 51 | + foreach (var type in _clrTypes) |
| 52 | + types.ByClrType[type] = this; |
| 53 | + } |
| 54 | + |
| 55 | + internal override TypeHandler Activate(TypeHandlerRegistry registry) |
| 56 | + { |
| 57 | + if (HandlerType == null) |
| 58 | + { |
| 59 | + registry.ByOID[OID] = registry.UnrecognizedTypeHandler; |
| 60 | + return registry.UnrecognizedTypeHandler; |
| 61 | + } |
| 62 | + |
| 63 | + var handler = InstantiateHandler(registry); |
| 64 | + |
| 65 | + registry.ByOID[OID] = handler; |
| 66 | + |
| 67 | + if (NpgsqlDbType.HasValue) |
| 68 | + { |
| 69 | + var value = NpgsqlDbType.Value; |
| 70 | + if (registry.ByNpgsqlDbType.ContainsKey(value)) |
| 71 | + throw new Exception($"Two type handlers registered on same NpgsqlDbType {NpgsqlDbType}: {registry.ByNpgsqlDbType[value].GetType().Name} and {HandlerType.Name}"); |
| 72 | + registry.ByNpgsqlDbType[NpgsqlDbType.Value] = handler; |
| 73 | + } |
| 74 | + |
| 75 | + if (_dbTypes != null) |
| 76 | + { |
| 77 | + foreach (var dbType in _dbTypes) |
| 78 | + { |
| 79 | + if (registry.ByDbType.ContainsKey(dbType)) |
| 80 | + throw new Exception($"Two type handlers registered on same DbType {dbType}: {registry.ByDbType[dbType].GetType().Name} and {HandlerType.Name}"); |
| 81 | + registry.ByDbType[dbType] = handler; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (_clrTypes != null) |
| 86 | + { |
| 87 | + foreach (var type in _clrTypes) |
| 88 | + { |
| 89 | + if (registry.ByType.ContainsKey(type)) |
| 90 | + throw new Exception($"Two type handlers registered on same .NET type {type}: {registry.ByType[type].GetType().Name} and {HandlerType.Name}"); |
| 91 | + registry.ByType[type] = handler; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return handler; |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Instantiate the type handler. If it has a constructor that accepts a TypeHandlerRegistry, use that to allow |
| 100 | + /// the handler to make connector-specific adjustments. Otherwise (the normal case), use the default constructor. |
| 101 | + /// </summary> |
| 102 | + /// <param name="registry"></param> |
| 103 | + /// <returns></returns> |
| 104 | + TypeHandler InstantiateHandler(TypeHandlerRegistry registry) |
| 105 | + { |
| 106 | + Debug.Assert(HandlerType != null); |
| 107 | + |
| 108 | + if (_ctorWithRegistry == null && _ctorWithoutRegistry == null) |
| 109 | + { |
| 110 | + var ctors = HandlerType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); |
| 111 | + _ctorWithRegistry = ( |
| 112 | + from c in ctors |
| 113 | + let p = c.GetParameters() |
| 114 | + where p.Length == 2 && p[0].ParameterType == typeof(PostgresType) && p[1].ParameterType == typeof(TypeHandlerRegistry) |
| 115 | + select c |
| 116 | + ).FirstOrDefault(); |
| 117 | + |
| 118 | + if (_ctorWithRegistry == null) |
| 119 | + { |
| 120 | + _ctorWithoutRegistry = ( |
| 121 | + from c in ctors |
| 122 | + let p = c.GetParameters() |
| 123 | + where p.Length == 1 && p[0].ParameterType == typeof(PostgresType) |
| 124 | + select c |
| 125 | + ).FirstOrDefault(); |
| 126 | + if (_ctorWithoutRegistry == null) |
| 127 | + throw new Exception($"Type handler type {HandlerType.Name} does not have an appropriate constructor"); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (_ctorWithRegistry != null) |
| 132 | + return (TypeHandler)_ctorWithRegistry.Invoke(new object[] { this, registry }); |
| 133 | + Debug.Assert(_ctorWithoutRegistry != null); |
| 134 | + return (TypeHandler)_ctorWithoutRegistry.Invoke(new object[] { this }); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments