Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieving the affected row count for a query is awkward #537

Closed
0xTim opened this issue Jul 8, 2018 · 2 comments
Closed

Retrieving the affected row count for a query is awkward #537

0xTim opened this issue Jul 8, 2018 · 2 comments
Labels
enhancement New feature or request

Comments

@0xTim
Copy link
Member

0xTim commented Jul 8, 2018

To get a query's affected row count, it is currently necessary to retrieve the metadata from the database handle and unwrap the optional, adding considerable boilerplate logic, typically of this form:

extension SQLQueryBuilder where Connection == MySQLConnection {
    public func runReturningAffectedRows() -> Future<UInt64> {
        return connection.query(query) { _ in  }.map {
            guard let metadata = (self.connection as MySQLConnection).lastMetadata else {
                throw MySQLError(identifier: "metadata", reason: "Connection did not return metadata after successful query.")
            }
            return metadata.affectedRows
        }
    }
}

This also smacks of thread-safety issues; while it does not actually exhibit any, it feels iffy for the metadata of a given query to be associated with the database itself. There should be a more consistent and query-bound way to receive the row count. As well, it should be possible to retrieve said count from other databases than just MySQL - both SQLite and PostgreSQL are known to have such values available.

@tanner0101 tanner0101 added the enhancement New feature or request label Jul 12, 2018
@tanner0101
Copy link
Member

Agreed, this would be a nice addition. First step will be adding support for fetching affected row counts from SQLite and PSQL.

@tanner0101
Copy link
Member

Track here vapor/fluent-kit#298

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants