sqlstruct icon indicating copy to clipboard operation
sqlstruct copied to clipboard

Feature request - UPDATE statement support

Open sunshine69 opened this issue 4 years ago • 0 comments

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

sunshine69 avatar Sep 17 '21 08:09 sunshine69