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

NamedExec Bulk Insert Fix #718

Merged
merged 4 commits into from
Apr 8, 2021
Merged

Conversation

abraithwaite
Copy link
Contributor

@abraithwaite abraithwaite commented Mar 31, 2021

This change-set is how I addressed the issues of bulk inserts not being handled cleanly when they're not at the end of the query.

It works by first adding VALUES to the expression (not sure if this is universally applicable to all supported SQL dialects) then adding a submatch to the regular expression for the fields we want to repeat. Using the submatch, we're able to repeat the fields that we're concerned with to expand the insertion parameters.

Likely? Fixes #657, #694, #705, #709, #712, #713 ?

This change-set is how I addressed the issues of bulk inserts not being
handled cleanly when they're not at the end of the query.

It works by first adding VALUES to the expression (not sure if this is
universally applicable to all supported SQL dialects) then adding a
submatch to the regular expression for the fields we want to repeat.
Using the submatch, we're able to repeat the fields that we're concerned
with to expand the insertion parameters.
@abraithwaite abraithwaite force-pushed the alan/fix-named-batch branch from f7bb766 to 6258c9a Compare March 31, 2021 01:52
@coveralls
Copy link

coveralls commented Mar 31, 2021

Pull Request Test Coverage Report for Build 217

  • 9 of 9 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 73.897%

Totals Coverage Status
Change from base Build 194: 0.2%
Covered Lines: 1206
Relevant Lines: 1632

💛 - Coveralls

// defensive guard. loc should be len 4 representing the starting and
// ending index for the whole regex match and the starting + ending
// index for the single inside group
if len(loc[0]) != 4 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If loc[0] != 4, that indicates that the regex was changed and either the submatcher was removed or another submatcher was added.

@gsora
Copy link

gsora commented Apr 6, 2021

Just tried this patch myself and it works fine with INSERT ... ON CONFLICT DO UPDATE SET.

@@ -214,7 +218,7 @@ func TestNamedQueries(t *testing.T) {
}

_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
VALUES (:first_name, :last_name, :email) `, slsMap)
VALUES (:first_name, :last_name, :email) ;--`, slsMap)
Copy link
Owner

Choose a reason for hiding this comment

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

Is this extra comment necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The comment here is exercising the regex in the sense that anything after the closing paren is ignored.

I could update it to something more clear like ON CONFLICT perhaps? I imagine that most people are using that behavior moreso than random junk at the end.

Copy link
Owner

Choose a reason for hiding this comment

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

Happy with the comment, just did not want to add some extra load bearing requirement here. I think it's clear from the tests later on that these are testing some boundary conditions.

@jmoiron jmoiron merged commit 1723f86 into jmoiron:master Apr 8, 2021
@jmoiron
Copy link
Owner

jmoiron commented Apr 8, 2021

Tests looked good, tagged as v1.3.2

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

Successfully merging this pull request may close these issues.

NamedExec/slice does not work with Postgres INSERT ... ON CONFLICT
4 participants