-
Notifications
You must be signed in to change notification settings - Fork 30
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 Filtering with QueryParameters #26
Conversation
Codecov Report
@@ Coverage Diff @@
## master #26 +/- ##
==========================================
+ Coverage 21.04% 21.36% +0.31%
==========================================
Files 6 6
Lines 1259 1512 +253
==========================================
+ Hits 265 323 +58
- Misses 994 1189 +195
Continue to review full report at Codecov.
|
Instead of |
Do we have an equivalent for delete, e.g.: Model.deleteAll(matching: MyQuery) { error in
...
} |
f7c3dff
to
ea6fdf4
Compare
Sources/SwiftKueryORM/Model.swift
Outdated
do { | ||
queryDictionary = try QueryEncoder().encode(queryParams) | ||
} catch { | ||
throw error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're just going to rethrow the error you don't need the do{}
at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, will fix
Sources/SwiftKueryORM/Model.swift
Outdated
throw RequestError(.ormQueryError, reason: "Could not extract values for Query Parameters") | ||
} | ||
|
||
var filter: Filter! = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section is deeply mysterious...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a comment to explain
var dictionariesTitleToValue = [[String: Any?]]() | ||
|
||
connection.connect { error in | ||
if let error = error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be a guard
to reduce the indentation level below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be done, also in another refactoring PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the guard (sadly, I don't think you can guard and bind variable inside the block, only outside?) But the else
looks redundant given the block return
s
/// - Parameter matching: Optional QueryParams to use | ||
/// - Returns: An array of model | ||
static func findAll<Q: QueryParams>(using db: Database? = nil, matching queryParams: Q, _ onCompletion: @escaping ([Self]?, RequestError?) -> Void) { | ||
guard let database = db ?? Database.default else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Getting a connection" is so common that maybe it should be factored out into a common function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be done in another refactoring PR?
ea6fdf4
to
02ac0d1
Compare
This should be good to go? @ianpartridge @seabaylea |
var queryDictionary: [String: String] = try QueryEncoder().encode(queryParams) | ||
|
||
var columns = table.columns.filter { queryDictionary[$0.name] != nil } | ||
var values = Array(queryDictionary.values) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to check that columns
and values
line up if a user can specify an incorrect column name (one that is not in the table.columns
) since the filter would drop that column but the corresponding value would not be dropped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very true, will make that check
var filter: Filter = (columns.removeFirst() == values.removeFirst()) | ||
for (column, value) in zip(columns, values) { | ||
filter = filter && (column == value) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this way of constructing the filter vulnerable to SQL injection (cf https://github.com/IBM-Swift/Swift-Kuery#sql-injection-prevention-using-parameterization)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This issue is pending... #19 but yes that should have been looked at in this PR
This PR enables filtering in the ORM. It uses the Query Parameters from Kitura.
By creating a struct as such:
And calling the api as such: