Return number of affected rows if sql query is update or delete

This commit is contained in:
Dan Sosedoff 2016-02-18 22:36:01 -06:00
parent 6edc384c05
commit 4b4f778360

View File

@ -226,8 +226,29 @@ func (client *Client) Query(query string) (*Result, error) {
}
func (client *Client) query(query string, args ...interface{}) (*Result, error) {
rows, err := client.db.Queryx(query, args...)
action := strings.ToLower(strings.Split(query, " ")[0])
if action == "update" || action == "delete" {
res, err := client.db.Exec(query, args...)
if err != nil {
return nil, err
}
affected, err := res.RowsAffected()
if err != nil {
return nil, err
}
result := Result{
Columns: []string{"Rows Affected"},
Rows: []Row{
Row{affected},
},
}
return &result, nil
}
rows, err := client.db.Queryx(query, args...)
if err != nil {
return nil, err
}