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

Feature request - UPDATE statement support #22

Open
sunshine69 opened this issue Sep 17, 2021 · 0 comments
Open

Feature request - UPDATE statement support #22

sunshine69 opened this issue Sep 17, 2021 · 0 comments

Comments

@sunshine69
Copy link

Hi,

It seems it is pretty convinient for SELECT but how about the usage of UPDATE statement?

I am thinking that something like loop over the colnames and set it but not sure how to map inverse , that is from the col name back to the struct field and extract the data from that field for update.

Something liek this

type Project struct {
  ID uint `sql:"id"`
  XX
  ...
}
func (p *Project) Update(dbc *sql.DB) {
	tx, _ := dbc.Begin()
	for colname := range sqlstruct.Columns(Project{}){
		sql := fmt.Sprintf(`UPDATE project SET %s = ? WHERE id = ?`, colname)
		stmt, _ := tx.Prepare(sql)
		stmt.Exec( p.XXX, p.ID )
	}
        tx.Commit()
}

how to extract the field quickly wihtout having to create a map[string]interface{} and with more work to assert type

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

No branches or pull requests

1 participant