Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/Npgsql/NpgsqlCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,13 @@ void DeriveParametersForFunction()
{
var param = new NpgsqlParameter();

// TODO: Fix enums, composite types
param.NpgsqlDbType = c.Connection.Connector.TypeMapper.GetNpgsqlTypeByOid(types[i]);
(var npgsqlDbType, var postgresType) =
c.Connection.Connector.TypeMapper.GetTypeInfoByOid(types[i]);

param.DataTypeName = postgresType.DisplayName;
param.PostgresType = postgresType;
if (npgsqlDbType.HasValue)
param.NpgsqlDbType = npgsqlDbType.Value;

if (names != null && i < names.Length)
param.ParameterName = names[i];
Expand Down Expand Up @@ -517,15 +522,27 @@ void DeriveParametersForQuery()

for (var i = 0; i < paramTypeOIDs.Count; i++)
{
var param = statement.InputParameters[i];
var derivedType = connector.TypeMapper.GetNpgsqlTypeByOid(paramTypeOIDs[i]);
if (param.NpgsqlDbType != NpgsqlDbType.Unknown && param.NpgsqlDbType != derivedType)
try
{
var param = statement.InputParameters[i];
var paramOid = paramTypeOIDs[i];

(var npgsqlDbType, var postgresType) = connector.TypeMapper.GetTypeInfoByOid(paramOid);

if (param.NpgsqlDbType != NpgsqlDbType.Unknown && param.NpgsqlDbType != npgsqlDbType)
throw new NpgsqlException("The backend parser inferred different types for parameters with the same name. Please try explicit casting within your SQL statement or batch or use different placeholder names.");

param.DataTypeName = postgresType.DisplayName;
param.PostgresType = postgresType;
if (npgsqlDbType.HasValue)
param.NpgsqlDbType = npgsqlDbType.Value;
}
catch
{
connector.SkipUntil(BackendMessageCode.ReadyForQuery);
Parameters.Clear();
throw new NpgsqlException("The backend parser inferred different types for parameters with the same name. Please try explicit casting within your SQL statement or batch or use different placeholder names.");
throw;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good improvement on exception handling

}
param.NpgsqlDbType = derivedType;
}

var msg = connector.ReadMessage();
Expand Down
9 changes: 9 additions & 0 deletions src/Npgsql/NpgsqlParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.Reflection;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Npgsql.PostgresTypes;
using Npgsql.TypeHandling;
using Npgsql.TypeMapping;
using NpgsqlTypes;
Expand Down Expand Up @@ -503,6 +504,14 @@ public sealed override int Size
public NpgsqlParameterCollection Collection { get; set; }
#pragma warning restore CA2227

/// <summary>
/// The PostgreSQL data type, such as int4 or text, as discovered from pg_type.
/// This property is automatically set if parameters have been derived via
/// <see cref="NpgsqlCommandBuilder.DeriveParameters"/> and can be used to
/// acquire additional information about the parameters' data type.
/// </summary>
public PostgresType PostgresType { get; internal set; }

#endregion Other Properties

#region Internals
Expand Down
31 changes: 24 additions & 7 deletions src/Npgsql/TypeMapping/ConnectorTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,18 +427,35 @@ void BindRangeType(NpgsqlTypeHandler elementHandler, PostgresRangeType pgRangeTy

#endregion Binding

internal NpgsqlDbType GetNpgsqlTypeByOid(uint oid)
internal (NpgsqlDbType? npgsqlDbType, PostgresType postgresType) GetTypeInfoByOid(uint oid)
{
if (!DatabaseInfo.ByOID.TryGetValue(oid, out var pgType))
if (!DatabaseInfo.ByOID.TryGetValue(oid, out var postgresType))
throw new InvalidOperationException($"Couldn't find PostgreSQL type with OID {oid}");

if (!Mappings.TryGetValue(pgType.Name, out var mapping) && !Mappings.TryGetValue(pgType.FullName, out mapping))
throw new InvalidOperationException($"No mapping found for PostgreSQL type {pgType.DisplayName}");
// Try to find the postgresType in the mappings
if (TryGetMapping(postgresType, out var npgsqlTypeMapping))
return (npgsqlTypeMapping.NpgsqlDbType, postgresType);

if (!mapping.NpgsqlDbType.HasValue)
throw new InvalidOperationException($"Invalid parameter type: {oid}");
if (postgresType is PostgresArrayType arrayType)
{
// Try to find the Elements' postgresType in the mappings
if (TryGetMapping(arrayType.Element, out var elementNpgsqlTypeMapping))
return (elementNpgsqlTypeMapping.NpgsqlDbType | NpgsqlDbType.Array, postgresType);

// It might be an array of an unmapped enum or composite type
if (arrayType.Element is PostgresEnumType || arrayType.Element is PostgresCompositeType)
return (null, postgresType);
}

// It might be an unmapped enum type
if (postgresType is PostgresEnumType || postgresType is PostgresCompositeType)
return (null, postgresType);

throw new InvalidOperationException($"No mapping found for PostgreSQL type {postgresType.DisplayName}");

return mapping.NpgsqlDbType.Value;
bool TryGetMapping(PostgresType pgType, out NpgsqlTypeMapping mapping)
=> (Mappings.TryGetValue(pgType.Name, out mapping) ||
Mappings.TryGetValue(pgType.FullName, out mapping));
}
}
}
Loading