refactor: consolidate query executions into two functions queryOne and queryAll

This commit is contained in:
Deluan
2020-02-01 14:48:22 -05:00
committed by Deluan Quintão
parent 7e65bb8f20
commit 7c4511e33a
6 changed files with 20 additions and 57 deletions
+3 -14
View File
@@ -103,28 +103,17 @@ func (r sqlRepository) queryAll(sq Sqlizer, response interface{}) error {
func (r sqlRepository) exists(existsQuery SelectBuilder) (bool, error) {
existsQuery = existsQuery.Columns("count(*) as count").From(r.tableName)
query, args, err := r.toSql(existsQuery)
if err != nil {
return false, err
}
var res struct{ Count int64 }
err = r.ormer.Raw(query, args...).QueryRow(&res)
err := r.queryOne(existsQuery, &res)
return res.Count > 0, err
}
func (r sqlRepository) count(countQuery SelectBuilder, options ...model.QueryOptions) (int64, error) {
countQuery = countQuery.Columns("count(*) as count").From(r.tableName)
countQuery = r.applyFilters(countQuery, options...)
query, args, err := r.toSql(countQuery)
if err != nil {
return 0, err
}
var res struct{ Count int64 }
err = r.ormer.Raw(query, args...).QueryRow(&res)
if err == orm.ErrNoRows {
return 0, model.ErrNotFound
}
return res.Count, nil
err := r.queryOne(countQuery, &res)
return res.Count, err
}
func (r sqlRepository) put(id string, m interface{}) (newId string, err error) {