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

Add support for copying query builders. #621

Merged
merged 2 commits into from
Apr 10, 2019
Merged

Add support for copying query builders. #621

merged 2 commits into from
Apr 10, 2019

Conversation

MrMage
Copy link
Contributor

@MrMage MrMage commented Mar 22, 2019

Creates a copy of the current query builder. Useful for constructing two similar, but not identical, queries.

Example: two queries sorted by different criteria.

let baseQuery = try User.query(on: conn).filter(\.name == "foo")
let sortedByID = baseQuery.copy().sort(\.id, .ascending)
let sortedByName = baseQuery.copy().sort(\.name, .ascending)

Example: two queries, one sorted, one counting. (Used e.g. for pagination; see https://github.com/nodes-vapor/paginator/blob/0c1ce194d3ea4564c782e94ba3ea4f09b5d87ad1/Sources/Paginator/Paginatable/QueryBuilderPaginatable.swift#L55.)

let baseQuery = try User.query(on: conn).filter(\.name == "foo")
let sortedQuery = baseQuery.copy().sort(\.id, .ascending)
let countQuery = baseQuery.copy().count()

(Without the copy, this will add the ORDER BY clause to the query, which Postgres chokes on.)

@MrMage MrMage force-pushed the querybuilder-copy branch from f3b5135 to 4519e6a Compare March 22, 2019 13:40
@MrMage MrMage force-pushed the querybuilder-copy branch from 4519e6a to 41c94f3 Compare March 22, 2019 13:41
@MrMage MrMage requested a review from tanner0101 March 22, 2019 13:44
@MrMage
Copy link
Contributor Author

MrMage commented Mar 27, 2019

@tanner0101 would you mind taking a look at this? (CI is broken by an outdated Swift version, unrelated to this PR.)

@tanner0101 tanner0101 added the enhancement New feature or request label Apr 10, 2019
@tanner0101
Copy link
Member

LGTM, thanks @MrMage :)

@tanner0101 tanner0101 merged commit b915c32 into 3 Apr 10, 2019
@penny-coin
Copy link

Hey @MrMage, you just merged a pull request, have a coin!

You now have 23 coins.

@tanner0101 tanner0101 deleted the querybuilder-copy branch April 10, 2019 18:34
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

Successfully merging this pull request may close these issues.

3 participants