Exemplo n.º 1
0
 public virtual INpgsqlTypeMapper AddMapping(NpgsqlTypeMapping mapping)
 {
     if (Mappings.ContainsKey(mapping.PgTypeName))
     {
         RemoveMapping(mapping.PgTypeName);
     }
     Mappings[mapping.PgTypeName] = mapping;
     return(this);
 }
        public override INpgsqlTypeMapper AddMapping(NpgsqlTypeMapping mapping)
        {
            CheckReady();

            base.AddMapping(mapping);
            BindType(mapping, _connector, true);
            IsModified = true;
            return(this);
        }
 void BindType(NpgsqlTypeMapping mapping, NpgsqlConnector connector, bool throwOnError)
 {
     // Binding can occur at two different times:
     // 1. When a user adds a mapping for a specific connection (and exception should bubble up to them)
     // 2. When binding the global mappings, in which case we want to log rather than throw
     // (i.e. missing database type for some unused defined binding shouldn't fail the connection)
     try
     {
         DoBindType(mapping, connector);
     }
     catch (Exception e)
     {
         if (throwOnError)
         {
             throw;
         }
         Log.Warn($"Exception while binding type {mapping.PgTypeName}", e);
     }
 }
        public override INpgsqlTypeMapper AddMapping(NpgsqlTypeMapping mapping)
        {
            Lock.EnterWriteLock();
            try
            {
                base.AddMapping(mapping);
                _changeCounter++;

                if (mapping.NpgsqlDbType.HasValue)
                {
                    foreach (var dbType in mapping.DbTypes)
                    {
                        _dbTypeToNpgsqlDbType[dbType] = mapping.NpgsqlDbType.Value;
                    }

                    if (mapping.InferredDbType.HasValue)
                    {
                        _npgsqlDbTypeToDbType[mapping.NpgsqlDbType.Value] = mapping.InferredDbType.Value;
                    }

                    foreach (var clrType in mapping.ClrTypes)
                    {
                        _typeToNpgsqlDbType[clrType] = mapping.NpgsqlDbType.Value;
                    }
                }

                if (mapping.InferredDbType.HasValue)
                {
                    foreach (var clrType in mapping.ClrTypes)
                    {
                        _typeToDbType[clrType] = mapping.InferredDbType.Value;
                    }
                }

                return(this);
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
        void DoBindType(NpgsqlTypeMapping mapping, NpgsqlConnector connector)
        {
            var pgName = mapping.PgTypeName;
            var found  = pgName.IndexOf('.') == -1
                ? DatabaseInfo.ByName.TryGetValue(pgName, out var pgType)  // No dot, partial type name
                : DatabaseInfo.ByFullName.TryGetValue(pgName, out pgType); // Full type name with namespace

            if (!found)
            {
                throw new ArgumentException($"A PostgreSQL type with the name {mapping.PgTypeName} was not found in the database");
            }
            if (pgType == null)
            {
                throw new ArgumentException($"More than one PostgreSQL type was found with the name {mapping.PgTypeName}, please specify a full name including schema");
            }
            if (pgType is PostgresDomainType)
            {
                throw new NotSupportedException("Cannot add a mapping to a PostgreSQL domain type");
            }

            var handler = mapping.TypeHandlerFactory.Create(pgType, connector.Connection);

            BindType(handler, pgType, mapping.NpgsqlDbType, mapping.DbTypes, mapping.ClrTypes);
        }