Example #1
0
File: sql.go Project: go-crud/crud
func (c *CRUD) Delete(id interface{}) error {
	queryString := fmt.Sprintf("DELETE FROM %s WHERE id=?", c.tableName)
	result, err := c.db.Exec(queryString, id)
	if i, _ := result.RowsAffected(); i == 0 {
		return errs.NewNotFound("not found")
	}
	return err
}
Example #2
0
File: sql.go Project: go-crud/crud
func (c *CRUD) Exist(id interface{}) (bool, error) {
	var count int
	err := db.Get(&count, "SELECT count(*) FROM "+c.tableName+" WHERE id=?", id)

	if count == 0 {
		return false, errs.NewNotFound("not found")
	}

	return true, err
}
Example #3
0
File: sql.go Project: go-crud/crud
func (c *CRUD) Update(id interface{}, v map[string]interface{}) error {
	keys, vals := readUpdateKey(v)
	vals = append(vals, id)
	queryString := fmt.Sprintf("UPDATE %s SET %s WHERE id=?", c.tableName, keys)
	result, err := c.db.Exec(queryString, vals...)

	if i, _ := result.RowsAffected(); i == 0 {
		return errs.NewNotFound("not found")
	}

	return errs.Sql(err)
}