-
Notifications
You must be signed in to change notification settings - Fork 2
/
delete.go
115 lines (96 loc) · 2.62 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package qb
import (
"fmt"
"strings"
"github.com/Kasita-Inc/gadget/errors"
)
/*
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
tbl_name[.*] [, tbl_name[.*]] ...
FROM table_references
[WHERE where_condition]
*/
// DeleteQuery for removing rows from a database
type DeleteQuery struct {
tables []Table
from Table
joins []*Join
where *whereCondition
// NICE TO HAVE: Add orderby and limit logic, order by and limit only apply to single table case
err error
}
// GetAlias of the passed table name in this query
func (q *DeleteQuery) GetAlias(tableName string) string {
return tableName
}
// From sets the primary table the query will find rows in.
func (q *DeleteQuery) From(table Table) *DeleteQuery {
q.from = table
return q
}
// InnerJoin with another table in the database.
func (q *DeleteQuery) InnerJoin(table Table) *Join {
join := NewJoin(Inner, Right, table)
q.joins = append(q.joins, join)
return join
}
// OuterJoin with another table in the database.
func (q *DeleteQuery) OuterJoin(direction JoinDirection, table Table) *Join {
join := NewJoin(Outer, direction, table)
q.joins = append(q.joins, join)
return join
}
// Where determines what rows to delete from.
func (q *DeleteQuery) Where(condition *ConditionExpression) *DeleteQuery {
q.where.expression = condition
return q
}
// Validate that this query is executable
func (q *DeleteQuery) Validate() bool {
if nil == q.from && len(q.tables) == 0 {
q.err = errors.New("at least one table must be specified to delete from")
return false
}
if nil == q.where.expression {
q.err = errors.New("delete requires a where clause")
return false
}
for _, join := range q.joins {
if nil != join.err {
q.err = join.err
return false
}
}
return true
}
// SQL representation of this delete query.
func (q *DeleteQuery) SQL() (string, []interface{}, error) {
if !q.Validate() {
return "", nil, q.err
}
lines := []string{"DELETE"}
values := []interface{}{}
rowsInLines := make([]string, len(q.tables))
if len(q.tables) == 1 && nil == q.from {
q.from = q.tables[0]
} else {
for i, table := range q.tables {
rowsInLines[i] = fmt.Sprintf("`%s`", table.GetName())
}
lines = append(lines, strings.Join(rowsInLines, ", "))
}
// FROM
lines = append(lines, fmt.Sprintf("FROM `%s`", q.from.GetName()))
// JOIN
for _, join := range q.joins {
joinSQL, joinValues := join.SQL()
lines = append(lines, joinSQL)
values = append(values, joinValues...)
}
// WHERE
if where, whereValues, ok := q.where.sql(); ok {
lines = append(lines, "WHERE", where)
values = append(values, whereValues...)
}
return strings.Join(lines, " "), values, q.err
}