-
-
Notifications
You must be signed in to change notification settings - Fork 61
Adds multirow insert method #153
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
Conversation
6a0c250
to
5d51f3c
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #153 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 100 100
Lines 2635 2636 +1
=========================================
+ Hits 2635 2636 +1
|
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've been meaning to upstream basically this exact helper myself! 🙂 I have two nits:
- I'd rather it not be just another overload of
values(_:)
, but have a unique name - consider an attempt to insert into a table with a single column of array type. I named my versionmultiValues(_:)
, but I'm not at all convinced that's an ideal name either and would actually welcome a bit of bikeshedding on the matter. - My implementation is a bit more generic:
Even though the input gets converted back to
extension SQLInsertBuilder { @discardableResult public func multiValues<S1, S2>(_ valueSets: S1) -> Self where S1: Sequence, S2: Sequence, S1.Element == S2, S2.Element == SQLExpression { valueSets.reduce(self) { $0.values(Array($1)) } } }
Array
in the end regardless, accepting generic sequences allows for cleaner syntax when using, for example, the methods fromswift-algorithms
to process the data to be inserted.
I think |
c5b1f51
to
e2ad748
Compare
@gwynne Sorry it took so long to get back to this one. I've adjusted it to use your implementation (which is great!), and named it |
@NeedleInAJayStack Any chance you'd be able to update this to resolve the conflicts? I'd be willing to merge it as SQLKit 3's last new feature. |
Sure, I can have that done in the next day or so if that works |
e2ad748
to
e2b955b
Compare
Hey @gwynne, I've resolved the merge conflicts and updated the tests to the new test formatting. I wasn't sure if any of the tests in the original PR were obsoleted by new work, so just let me know if you'd like any removed/organized differently. Thanks! |
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.
Nope, everything looks fine, thanks for your work on this!
These changes are now available in 3.32.0
This adds functionality to do a multi-row inserts with a single
values
method call.Previously, to insert multiple rows a user had to call
values
repeatedly:This was a bit awkward when inserting rows from an array, where an instance of the builder had to be saved off and edited:
This MR simplifies the mutli-row insert situation by adding a
values
method overload that accepts a nested array:NOTE
This functionality was only added to the
SQLExpression
version ofvalues
, NOT theEncodable
version ofvalues
. There are known issues with[Encodable]
conforming toEncodable
that prevent adequate type checks.For example, if a
values(_ rows: [[Encodable]])
is defined, it is undeterministic since the same call would also matchvalues(_ rows: [Encodable])
. If instead we changevalues(_ rows: [Encodable])
to detect[[Encodable]]
cases at runtime, then it is difficult to guarantee that a caller hasn't mixedEncodable
and[Encodable]
values.