postgres-nio icon indicating copy to clipboard operation
postgres-nio copied to clipboard

Problem getting query metadata from a PostgresClient query

Open neilt opened this issue 1 year ago • 5 comments

I am trying to find a way to access the query metadata from a PostgresClient query. The following code fails on the return statement which should return the number of rows deleted. I cannot seem to find a workaround. Is there a way to access the query metadata.

    static func deleteFromTable(client: PostgresClient, columnValue: String, columnName: String) async throws -> Int {
        var myBindings = PostgresBindings()
        myBindings.append(PostgresData(string: tableName))
        myBindings.append(PostgresData(string: columnName))
        myBindings.append(PostgresData(string: columnValue))
        let myQuery = PostgresQuery(unsafeSQL: #"DELETE FROM $1 WHERE $2 = $3;"#,
                                    binds: myBindings)
        let rows = try await client.query(myQuery).collect()
        return rows.metadata.rows // <- fails Value of type '[PostgresRow]' has no member 'metadata'
    }

Postgres-nio 1.22.1

neilt avatar Nov 30 '24 19:11 neilt

What do you mean by "fails"? Do you get a compiler error or something? If so, what exactly?

MahdiBM avatar Nov 30 '24 19:11 MahdiBM

What do you mean by "fails"? Do you get a compiler error or something? If so, what exactly?

Edit the original. The error is "Value of type '[PostgresRow]' has no member 'metadata'"

neilt avatar Nov 30 '24 19:11 neilt

Ok so that's a compiler error.

What you need is https://github.com/vapor/postgres-nio/pull/504. Basically, yes, PostgresNIO currently does not expose the metadata at all, in those functions.

cc @fabianfett I'd assume you're not super happy with the API design in that PR? I'm not either, but I couldn't think of any better ways. I'm open to your guidance if you have better ideas around how to expose the query metadata.

MahdiBM avatar Nov 30 '24 19:11 MahdiBM

Yep, collectWithMetadata() would be what I need. Anything I can do to help this along?

neilt avatar Nov 30 '24 19:11 neilt

This is already available with Connection.query(). I need this to verify that INSERT/UPDATE statements have actually been successful and how many rows were affected.

https://github.com/vapor/postgres-nio/pull/95

duncangroenewald avatar Feb 25 '25 05:02 duncangroenewald