You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Npgsql support mapping PG composite types to arbitrary .NET types, but we also want to support interacting with composite types without mapping them. Support for reading composite types has been done in #3558 (via nested data readers), but it's not yet possible to write them.
Possible options:
Accept a DbDataReader as a parameter value; that's consistent with how we do reading. However:
DbDataReader is an abstraction, and AFAIK there's no easy/nice implementations that users can use to just instantiate, populate and give us.
Using DbDataReader, there isn't a way to properly represent the difference between a nested composite, and an array of the same nested composite that happens to contain one element - see test NestedDataReaderTests.Composite on the read side. On the read side this isn't so important, but on the right side Npgsql would write the wrong thing and the operation would fail.
Accept a DataTable as a parameter value.
DataTable is readily available in .NET, and unlike DbDataReader can just be instantiated and used - that's great.
However, we'd probably require users to fill in the DataTable's schema, which is a heavy and cumbersome.
DataTable also has lots of other metadata that isn't relevant here, since it models a full relational table (primary key constraints, computed columns with expressions...)
ValueTuple/Tuple/ITuple
Very lightweight and easy, but has no support for field names; this means that binding the .NET tuple values to the PG composite fields can only happen positionally. This is potentially a big pit of failure - a name-based binding seem important here.
While you could try to piggy bag on the DbDataReader abstraction, I believe you don't have to. I may be wrong but I don't believe that any other db providers are using a common abstraction for custom types at this point.
I think you can take a more generic approach such as
If I want to pass a custom object array in parameters of a query, either you could use a ICustomObject[] or a ICustomsObject. (the latter needed to avoid creating 1 CLR object per item passed)
@roji , I wrote npgsql.tvp that adds the ability to pass DataTable as an array of composite type. I briefly tested it on simple models like this one:
CREATE TYPE schema.compositeType AS
(
id integer,
name text,
email text,
phone text
)
public sealed class TVP
{
/// <summary>
///
/// </summary>
public int Id
{
get;
set;
}
/// <summary>
///
/// </summary>
public string Name
{
get;
set;
}
/// <summary>
///
/// </summary>
public string Email
{
get;
set;
}
/// <summary>
///
/// </summary>
public string Phone
{
get;
set;
}
public TravelerTableValued(int id, string name, string email, string phone)
{
Id = id;
Name = name;
Email = email;
Phone = phone;
}
}
However, for a number of needs there are no public methods, so calls are implemented through UnsafeAccessor (see the Accessors directory). Is it possible to make them public? I would make the edits myself, but I want to know that it is possible.
Also, is it possible to add a method to PgSerializerOptions that resolves PostgreSQL types (needed in resolver)?
I would be glad to hear feedback on these issues, as well as on other flaws that probably exist.
/cc @NinoFloris for exposing more things from our converters architecture. @0UserName in these cases, the easiest way forward is probably for you to make a draft PR on what new public APIs you'd like to see, and then we can discuss things over that.
in these cases, the easiest way forward is probably for you to make a draft PR on what new public APIs you'd like to see, and then we can discuss things over that.
Got it.
Is it possible to get any comments on the plugin architecture in general?
in these cases, the easiest way forward is probably for you to make a draft PR on what new public APIs you'd like to see, and then we can discuss things over that.
Activity
NicolasDorier commentedon Nov 22, 2023
While you could try to piggy bag on the
DbDataReaderabstraction, I believe you don't have to. I may be wrong but I don't believe that any other db providers are using a common abstraction for custom types at this point.I think you can take a more generic approach such as
You define some interfaces in Npgsql:
Example how to implement them:
If I want to pass a custom object array in parameters of a query, either you could use a
ICustomObject[]or aICustomsObject. (the latter needed to avoid creating 1 CLR object per item passed)0UserName commentedon Jan 28, 2025
@roji , I wrote npgsql.tvp that adds the ability to pass
DataTableas an array of composite type. I briefly tested it on simple models like this one:However, for a number of needs there are no public methods, so calls are implemented through
UnsafeAccessor(see the Accessors directory). Is it possible to make them public? I would make the edits myself, but I want to know that it is possible.Also, is it possible to add a method to
PgSerializerOptionsthat resolves PostgreSQL types (needed in resolver)?I would be glad to hear feedback on these issues, as well as on other flaws that probably exist.
roji commentedon Jan 28, 2025
/cc @NinoFloris for exposing more things from our converters architecture. @0UserName in these cases, the easiest way forward is probably for you to make a draft PR on what new public APIs you'd like to see, and then we can discuss things over that.
0UserName commentedon Jan 28, 2025
Got it.
Is it possible to get any comments on the plugin architecture in general?
roji commentedon Jan 28, 2025
What exactly do you mean? We don't have any documentation on it, if that's what you're looking for - you'll have to figure it out from the code.
0UserName commentedon Jan 28, 2025
I mean comments about the plugin code. About the approach used to emulate
DataTableas an array of composite types, etc0UserName commentedon Jan 28, 2025
Done. Please take a look if possible.