Skip to content

Feature request - UPDATE statement support #22

@sunshine69

Description

@sunshine69

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions