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 support for batch insert/exec with maps in addition to structs #501

Merged
merged 2 commits into from
Jan 24, 2021

Conversation

J-Zeitler
Copy link
Contributor

@J-Zeitler J-Zeitler commented Mar 20, 2019

This is a feature specific use case I have, where I use the same internal logic for importing different tables into a db.

Thus I would like to use the syntax found in the test case added in named_test.go

Use case is something like this:

type tableDump struct {
	tableName   string
	tableValues []map[string]interface{}
}

var dump tableDump
// ... read table data into `dump`

dbColKeys := safeParseKeys(dump)
placeholderKeys := prependEach(dbColKeys, ":")

_, err = db.NamedExec("INSERT INTO " + dump.tableName + "(" +
		strings.Join(dbColKeys, ",") + ")" +
		"VALUES " +
		"(" + strings.Join(placeholderKeys, ",") + ")",
                dump.tableValues,
)

@coveralls
Copy link

coveralls commented Mar 20, 2019

Pull Request Test Coverage Report for Build 90

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

Totals Coverage Status
Change from base Build 88: 0.05%
Covered Lines: 1127
Relevant Lines: 1558

💛 - Coveralls

@@ -157,6 +157,10 @@ func bindAnyArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interfa
// type, given a list of names to pull out of the struct. Used by public
// BindStruct interface.
func bindArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{}, error) {
if maparg, ok := arg.(map[string]interface{}); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

bindAnyArgs() already has:

func bindAnyArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{}, error) {
    if maparg, ok := arg.(map[string]interface{}); ok {
        return bindMapArgs(names, maparg)
    }
    return bindArgs(names, arg, m)
}

Copy link
Owner

Choose a reason for hiding this comment

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

There's only one use of bindArgs, and it seems safe to replace it with bindAnyArgs, so doing that in a separate branch.

@jmoiron jmoiron changed the base branch from master to pr-501 January 24, 2021 21:19
@jmoiron jmoiron merged commit 68949f7 into jmoiron:pr-501 Jan 24, 2021
@jmoiron jmoiron mentioned this pull request Jan 24, 2021
jmoiron added a commit that referenced this pull request Jan 24, 2021
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.

4 participants