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 more options in findAll #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

AnthonyAmanse
Copy link
Contributor

This commit adds in options:

  • ORDER BY clause for sorting
  • OFFSET and LIMIT for retrieving a portion of the rows

This builds the appropriate SQL queries for the options listed above.
This commit also adds in initial tests for the new options.

Signed-off-by: AnthonyAmanse [email protected]

@CLAassistant
Copy link

CLAassistant commented May 30, 2018

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
0 out of 2 committers have signed the CLA.

❌ djones6
❌ AnthonyAmanse
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov-io
Copy link

codecov-io commented May 30, 2018

Codecov Report

Merging #38 into master will increase coverage by 0.04%.
The diff coverage is 33.33%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #38      +/-   ##
=========================================
+ Coverage   30.05%   30.1%   +0.04%     
=========================================
  Files           6       6              
  Lines        1364    1385      +21     
=========================================
+ Hits          410     417       +7     
- Misses        954     968      +14
Flag Coverage Δ
#SwiftKueryORM 30.1% <33.33%> (+0.04%) ⬆️
Impacted Files Coverage Δ
Sources/SwiftKueryORM/Model.swift 38.36% <33.33%> (-0.15%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9d9618d...3c72ff7. Read the comment docs.

@EnriqueL8
Copy link
Contributor

EnriqueL8 commented May 31, 2018

Hey @AnthonyAmanse, this looks really good! Just a few comments:

  • We have some work planned around QueryParams and filtering so it would be good to just have these options in the findAll calls without QueryParams
  • Some verification needs to take place with the OrderBy parameter to prevent SQL Injection - for example verifying that the column is actually in the table? or using the Parameter() function from SwiftKuery

Thank You

@EnriqueL8
Copy link
Contributor

One more suggestion: would be to make the orderBy parameter accept multiple OrderBy instance since for example we would want to order by ascending name and descending age. The definition could be: orderBy: OrderBy? ...

@AnthonyAmanse AnthonyAmanse force-pushed the additional-options branch 2 times, most recently from 65a9655 to 3c72ff7 Compare June 22, 2018 18:43
Copy link
Contributor Author

@AnthonyAmanse AnthonyAmanse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EnriqueL8 Thanks for the feedback. Updated the function to accept a variadic parameter of orderBy. Not sure what to do with verification for columns though

var column: Column?
do {
let table = try Person.getTable()
column = table.columns.first(where: {$0.name == "age"})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EnriqueL8 Would this be enough for a verification if the column exists? Since the orderBy parameter is an OrderBy buildable that doesn't accept just any string, verification would have to be done outside of findAll function

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annoyingly yes - the OrderBy only accepts a Field type that can be for example a Column but also a RawField.
We could use Order that accepts a String and then find a Column matching that String and then create the OrderBy case.

XCTAssertNil(error, "Find Failed: \(String(describing: error))")
}
performTest(asyncTasks: { expectation in
Person.findAll(orderBy: OrderBy.DESC(column!)) { array, error in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This need to be .ASC

@EnriqueL8
Copy link
Contributor

@AnthonyAmanse Sorry - this needs now a rebase since your Swift-Kuery 2.0.0 PR just got merged and addressing the comments

@AnthonyAmanse
Copy link
Contributor Author

@EnriqueL8 No worries. Changes made, used Order instead of OrderBy in functions. Also added a test case if column is not found in table

let query = Select(from: table)
var query = Select(from: table)
var orderByArray: [OrderBy] = []
for order in order {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you extract this logic into a function?

@AnthonyAmanse
Copy link
Contributor Author

@EnriqueL8 Moved the logic to getOrderBy

/**
This function constructs an array of OrderBy from the Order values
*/
private static func getOrderBy(order: [Order], table: Table) throws -> [OrderBy] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method looks quite familiar to the above method, maybe we could call this method from the above to remove code duplication

var orderByArray: [OrderBy] = []
for order in order {
guard let column = table.columns.first(where: {$0.name == order.value}) else {
throw RequestError(.ormInternalError, reason: "Column \(order.value) not found")
Copy link
Contributor

@EnriqueL8 EnriqueL8 Jul 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of throwing an .ormInternalError, we should throw .ormQueryError because it failed during the construction of the Query?

guard let column = table.columns.first(where: {$0.name == order.value}) else {
throw RequestError(.ormInternalError, reason: "Column \(order.value) not found")
}
if case .asc(order.value) = order {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the order.value? And just have .asc(_)?

self.numberOfRows = numberOfRows
if let sortedByAge = sortedByAge {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we sort the array programatically? By using array.sort? In the case of having to change the rows we would also have to update these two lines.

Copy link
Contributor

@EnriqueL8 EnriqueL8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - I promise these are the last changes that I suggest!

@AnthonyAmanse
Copy link
Contributor Author

@EnriqueL8 Feedbacks are always great! Merged the getOrderBy function instead of overloading. Sorted the rows using rows.sort instead of hardcoding it.

@EnriqueL8
Copy link
Contributor

EnriqueL8 commented Jul 18, 2018

LGTM - Updated Branch because it is out-of-date
Needs a Squash

This commit adds in options:
* ORDER BY clause for sorting
* OFFSET and LIMIT for retrieving a portion of the rows

This builds the appropriate SQL queries for the options listed above.
This commit also adds in initial tests for the new options.

Signed-off-by: AnthonyAmanse <[email protected]>
@kilnerm kilnerm self-requested a review January 7, 2019 10:03
@kilnerm kilnerm added the enhancement New feature or request label Jan 7, 2019
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.

6 participants