Esempio n. 1
0
File: update.go Progetto: Repo2/y
// Update saves object changes in db after version validation
func (c *Changer) Update(db sq.BaseRunner) (err error) {
	version := c.proxy.version()
	if !version.Valid {
		log.Panicf(
			"y/update: You must load \"%s\" before update it", c.proxy.schema.table)
	}
	pk := c.proxy.primary()
	// add old version to search condition
	pk[_version] = version.Int64
	// find changes
	clauses := c.modify()
	if len(clauses) == 0 {
		return
	}
	// set new version
	version.Int64++
	clauses[_version] = version.Int64
	// save
	result, err := builder{c.proxy.schema}.forUpdate(clauses, sq.Eq(pk)).RunWith(db).Exec()
	if err == nil {
		count, _ := result.RowsAffected()
		if count != 1 {
			err = ErrNoAffectedRows
		}
	}
	return
}
Esempio n. 2
0
File: delete.go Progetto: Repo2/y
// DeleteBy removes a proxy by values
func DeleteBy(db sq.BaseRunner, p *Proxy, by Values) (int64, error) {
	result, err := builder{p.schema}.forDelete(sq.Eq(by)).RunWith(db).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}
Esempio n. 3
0
File: proxy.go Progetto: Repo2/y
func (p *Proxy) loadBy(db sq.BaseRunner, eq Values) error {
	return p.findByEq(sq.Eq(eq)).Load(db)
}