-
Notifications
You must be signed in to change notification settings - Fork 0
Add query execution utilities #18
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |
| "github.com/icinga/icinga-go-library/com" | ||
| "github.com/icinga/icinga-go-library/strcase" | ||
| "github.com/icinga/icinga-go-library/types" | ||
| "github.com/jmoiron/sqlx" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
|
|
@@ -43,6 +44,47 @@ func SplitOnDupId[T IDer]() com.BulkChunkSplitPolicy[T] { | |
| } | ||
| } | ||
|
|
||
| // InsertObtainID executes the given query and fetches the last inserted ID. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... here due to how special it is. |
||
| // | ||
| // Using this method for database tables that don't define an auto-incrementing ID, or none at all, | ||
| // will not work. The only supported column that can be retrieved with this method is id. | ||
| // This function expects [TxOrDB] as an executor of the provided query, and is usually a *[sqlx.Tx] or *[DB] instance. | ||
| // Returns the retrieved ID on success and error on any database inserting/retrieving failure. | ||
| func InsertObtainID(ctx context.Context, conn TxOrDB, stmt string, arg any) (int64, error) { | ||
| var resultID int64 | ||
| switch conn.DriverName() { | ||
| case PostgreSQL: | ||
| query := stmt + " RETURNING id" | ||
| ps, err := conn.PrepareNamedContext(ctx, query) | ||
| if err != nil { | ||
| return 0, errors.Wrapf(err, "cannot prepare %q", query) | ||
| } | ||
| // We're deferring the ps#Close invocation here just to be on the safe side, otherwise it's | ||
yhabteab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // closed manually later on and the error is handled gracefully (if any). | ||
| defer func() { _ = ps.Close() }() | ||
|
|
||
| if err := ps.GetContext(ctx, &resultID, arg); err != nil { | ||
| return 0, CantPerformQuery(err, query) | ||
| } | ||
|
|
||
| if err := ps.Close(); err != nil { | ||
| return 0, errors.Wrapf(err, "cannot close prepared statement %q", query) | ||
| } | ||
| default: | ||
| result, err := sqlx.NamedExecContext(ctx, conn, stmt, arg) | ||
| if err != nil { | ||
| return 0, CantPerformQuery(err, stmt) | ||
| } | ||
|
|
||
| resultID, err = result.LastInsertId() | ||
| if err != nil { | ||
| return 0, errors.Wrap(err, "cannot retrieve last inserted ID") | ||
| } | ||
| } | ||
|
|
||
| return resultID, nil | ||
| } | ||
|
|
||
| // setGaleraOpts sets the "wsrep_sync_wait" variable for each session ensures that causality checks are performed | ||
| // before execution and that each statement is executed on a fully synchronized node. Doing so prevents foreign key | ||
| // violation when inserting into dependent tables on different MariaDB/MySQL nodes. When using MySQL single nodes, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.