-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
58 lines (48 loc) · 1.19 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
package squrl
import (
"fmt"
"strings"
)
func (s *SQURL) Delete() *SQURL {
t := deleteType
s.queryType = &t
return s
}
func (s *SQURL) formatDelete() (string, []any, error) {
if s.err != nil {
return "", []any{}, s.err
}
if s.parameters == nil {
parameters := []any{}
s.parameters = ¶meters
}
schema := ""
if s.schema != "" {
schema = fmt.Sprintf("%v.", s.schema)
}
query := fmt.Sprintf(`DELETE FROM %s"%s"%v`, schema, s.table, s.delimiter)
whereLength := 0
where := ""
if s.whereClauses != nil {
for i, clause := range *s.whereClauses {
whereLength++
if i == 0 {
where += fmt.Sprintf(`WHERE "%s".%s %v%v`, clause.Table, clause.Field, s.findClause(clause), s.delimiter)
} else {
where += fmt.Sprintf(`AND "%s".%s %v%v`, clause.Table, clause.Field, s.findClause(clause), s.delimiter)
}
}
}
query += where
if s.fields != nil {
i := 0
placeholders := make([]string, len(*s.fields))
for val := range *s.fields {
*s.parameters = append(*s.parameters, val)
placeholders[i] = fmt.Sprintf("$%v", i+1+whereLength)
i++
}
query += fmt.Sprintf(`RETURNING %v`, strings.Join(placeholders, ","+s.delimiter))
}
return query, *s.parameters, nil
}