Exemplo n.º 1
0
        void RegisterBaseType(string name, uint oid, Type handlerType, TypeMappingAttribute mapping)
        {
            // Instantiate the type handler. If it has a constructor that accepts an NpgsqlConnector, use that to allow
            // the handler to make connector-specific adjustments. Otherwise (the normal case), use the default constructor.
            var handler = (TypeHandler)(
                handlerType.GetConstructor(new[] { typeof(TypeHandlerRegistry) }) != null
                    ? Activator.CreateInstance(handlerType, this)
                    : Activator.CreateInstance(handlerType)
                );

            handler.OID    = oid;
            OIDIndex[oid]  = handler;
            handler.PgName = name;

            if (mapping.NpgsqlDbType.HasValue)
            {
                var npgsqlDbType = mapping.NpgsqlDbType.Value;
                if (_byNpgsqlDbType.ContainsKey(npgsqlDbType))
                {
                    throw new Exception(String.Format("Two type handlers registered on same NpgsqlDbType {0}: {1} and {2}",
                                                      npgsqlDbType, _byNpgsqlDbType[npgsqlDbType].GetType().Name, handlerType.Name));
                }
                _byNpgsqlDbType[npgsqlDbType] = handler;
                handler.NpgsqlDbType          = npgsqlDbType;
            }

            foreach (var dbType in mapping.DbTypes)
            {
                if (_byDbType.ContainsKey(dbType))
                {
                    throw new Exception(String.Format("Two type handlers registered on same DbType {0}: {1} and {2}",
                                                      dbType, _byDbType[dbType].GetType().Name, handlerType.Name));
                }
                _byDbType[dbType] = handler;
            }

            foreach (var type in mapping.Types)
            {
                if (_byType.ContainsKey(type))
                {
                    throw new Exception(String.Format("Two type handlers registered on same .NET type {0}: {1} and {2}",
                                                      type, _byType[type].GetType().Name, handlerType.Name));
                }
                _byType[type] = handler;
            }
        }
Exemplo n.º 2
0
        void RegisterRangeType(Type handlerType, TypeMappingAttribute mapping, BackendTypeInfo backendInfo)
        {
            Contract.Requires(backendInfo.RangeSubtypeOID != 0);

            TypeHandler elementHandler;

            if (!_oidIndex.TryGetValue(backendInfo.RangeSubtypeOID, out elementHandler))
            {
                Log.ErrorFormat("Range type '{0}' refers to unknown subtype with OID {1}, skipping", backendInfo.Name, backendInfo.RangeSubtypeOID);
                return;
            }

            var rangeHandlerType = typeof(RangeHandler <>).MakeGenericType(elementHandler.GetFieldType());
            var handler          = (TypeHandler)Activator.CreateInstance(rangeHandlerType, elementHandler, backendInfo.Name);

            handler.PgName             = "range";
            handler.NpgsqlDbType       = NpgsqlDbType.Range | elementHandler.NpgsqlDbType;
            handler.OID                = backendInfo.OID;
            _oidIndex[backendInfo.OID] = handler;
            _byNpgsqlDbType.Add(handler.NpgsqlDbType, handler);

            RegisterArrayType(backendInfo.ArrayOID, handler, backendInfo.ArrayTextDelimiter);
        }