Skip to content

feat: NamedFields / NamedInsertFields / NamedUpdateFields#999

Open
sonnemusk wants to merge 1 commit into
jmoiron:masterfrom
sonnemusk:feat/named-field-helpers
Open

feat: NamedFields / NamedInsertFields / NamedUpdateFields#999
sonnemusk wants to merge 1 commit into
jmoiron:masterfrom
sonnemusk:feat/named-field-helpers

Conversation

@sonnemusk

Copy link
Copy Markdown

Summary

Helpers to build named-query INSERT/UPDATE column lists from a struct or map without hand-writing every :name (issue #410).

type Client struct {
    ID     string `db:"id"`
    Name   string `db:"client_name"`
    Secret string `db:"client_secret"`
}

cols, vals, _ := sqlx.NamedInsertFields(client)
// cols: id, client_name, client_secret
// vals: :id, :client_name, :client_secret
db.NamedExec("INSERT INTO client ("+cols+") VALUES ("+vals+")", client)

set, _ := sqlx.NamedUpdateFields(client)
// set: id=:id, client_name=:client_name, client_secret=:client_secret
db.NamedExec("UPDATE client SET "+set+" WHERE id=:id", client)

Uses the same db tag / NameMapper rules as NamedExec and StructScan. Fields tagged db:"-" are omitted.

Does not generate full SQL or touch table metadata — only field name lists.

Addresses #410

Build INSERT column lists and UPDATE SET clauses from a struct or map
using the same db-tag rules as NamedExec, so callers need not hand-write
":id, :name" lists (issue jmoiron#410).
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.

1 participant