Skip to content

Mechanism for writing unmapped composites #5415

Description

@roji

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.

Activity

  1. added this to the Backlog milestone on Nov 19, 2023
  2. NicolasDorier commented on Nov 22, 2023

    @NicolasDorier

    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

    You define some interfaces in Npgsql:

    interface ICustomObject
    {
    	string DbType { get; }
    	void Write(CustomObjectWriter writer);
    }
    interface ICustomObjects
    {
            string ObjectType { get; }
    	void Write(CustomObjectsWriter writer);
    }
    class CustomObjectWriter
    {
    	public void Write(TimeSpan time) => ...;
    	public void Write(string text) => ...;
    }

    Example how to implement them:

    class PointsObject : ICustomObjects
    {
    	public string ObjectType => "point";
    	public double[] x;
    	public double[] y;
    	public void Write(CustomObjectsWriter writer)
    	{
    		writer.StartArray();
    		for (int i = 0; i < x.Length; i++)
    		{
    			writer.Write(x[i]);
    			writer.Write(y[i]);
    			writer.NextObject();
    		}
    		writer.EndArray();
    	}
    }
    class MyCustomObject : ICustomObject
    {
    	public string DbType => "custom_object";
    
    	string FullName { get; set; }
    	public void Write(CustomObjectWriter writer)
    	{
    		var s = FullName.Split(' ');
    		writer.Write(s[0]);
    		writer.Write(s[1]);
    	}
    }

    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)

  3. 0UserName commented on Jan 28, 2025

    @0UserName

    @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.

  4. roji commented on Jan 28, 2025

    @roji
    MemberAuthor

    /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.

  5. 0UserName commented on Jan 28, 2025

    @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.

    Got it.


    Is it possible to get any comments on the plugin architecture in general?

  6. roji commented on Jan 28, 2025

    @roji
    MemberAuthor

    Is it possible to get any comments on the plugin architecture in general?

    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.

  7. 0UserName commented on Jan 28, 2025

    @0UserName

    I mean comments about the plugin code. About the approach used to emulate DataTable as an array of composite types, etc

  8. 0UserName commented on Jan 28, 2025

    @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.

    Done. Please take a look if possible.

  9. added theissue type on Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions