-
Notifications
You must be signed in to change notification settings - Fork 2
/
delete.go
62 lines (53 loc) · 1.58 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
package sol
import (
"strings"
"github.com/aodin/sol/dialect"
)
// DeleteStmt is the internal representation of a DELETE statement.
type DeleteStmt struct {
ConditionalStmt
table *TableElem
}
// String outputs the parameter-less DELETE statement in a neutral dialect.
func (stmt DeleteStmt) String() string {
compiled, _ := stmt.Compile(&defaultDialect{}, Params())
return compiled
}
// Compile outputs the DELETE statement using the given dialect and parameters.
// An error may be returned because of a pre-existing error or because
// an error occurred during compilation.
func (stmt DeleteStmt) Compile(d dialect.Dialect, ps *Parameters) (string, error) {
// Return immediately if there are existing errors
if err := stmt.Error(); err != nil {
return "", err
}
// Being building the statement
compiled := []string{DELETE, FROM, stmt.table.Name()}
if stmt.where != nil {
cc, err := stmt.where.Compile(d, ps)
if err != nil {
return "", err
}
compiled = append(compiled, WHERE, cc)
}
return strings.Join(compiled, WHITESPACE), nil
}
// Where adds a conditional WHERE clause to the DELETE statement.
func (stmt DeleteStmt) Where(clauses ...Clause) DeleteStmt {
if len(clauses) > 1 {
// By default, multiple where clauses will be joined will AllOf
stmt.where = AllOf(clauses...)
} else if len(clauses) == 1 {
stmt.where = clauses[0]
}
return stmt
}
// Delete creates a DELETE statement for the given table.
func Delete(table *TableElem) (stmt DeleteStmt) {
if table == nil {
stmt.AddMeta("sol: attempting to DELETE a nil table")
return
}
stmt.table = table
return
}