-
Notifications
You must be signed in to change notification settings - Fork 3
/
query_sql_delete.go
38 lines (32 loc) · 1.11 KB
/
query_sql_delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package lore
import (
"errors"
"github.com/Masterminds/squirrel"
)
/*
BuildSqlDelete provides the entrypoint for specializing a generic Query as a DELETE query on the
table for the given ModelInterface. This directly returns a new squirrel.DeleteBuilder that can be
placed back into the Query instance via SetSqlBuilder; the underlying SQL has the form:
`DELETE FROM <DbTableName>`.
*/
func (q *Query) BuildSqlDelete() squirrel.DeleteBuilder {
return newSquirrelStatementBuilder().
Delete(q.modelInterface.DbTableName())
}
/*
BuildSqlDeleteModelByPrimaryKey wraps BuildSqlDelete to perform the delete on the table row with the
matching primary key/value of this Query's ModelInterface's model instance. Alias for
`query.BuildSqlDelete().Where(<primary key and value>)`.
*/
func (q *Query) BuildSqlDeleteModelByPrimaryKey() (squirrel.DeleteBuilder, error) {
mi := q.modelInterface
// Return error if primary key is empty.
pk := mi.DbPrimaryFieldKey()
if pk == "" {
return q.BuildSqlDelete(), errors.New(_ERR_EMPTY_PRIMARY_KEY)
}
return q.BuildSqlDelete().
Where(squirrel.Eq{
pk: mi.DbPrimaryFieldValue(),
}), nil
}