-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
60 lines (46 loc) · 1.05 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
package tsbuilder
import (
"bytes"
"fmt"
"strings"
)
var _ tdEngineSqlBuilder = (*deleteBuilder)(nil)
type deleteBuilder struct {
from string
whereConditions []string
}
func NewDeleteBuilder() *deleteBuilder {
return &deleteBuilder{
whereConditions: make([]string, 0),
}
}
func (s *deleteBuilder) From(from string) *deleteBuilder {
s.from = from
return s
}
func (s *deleteBuilder) Where(conditions ...string) *deleteBuilder {
s.whereConditions = append(s.whereConditions, conditions...)
return s
}
func (s *deleteBuilder) Build() (string, error) {
if err := s.validate(); err != nil {
return "", fmt.Errorf("validate error: %w", err)
}
b := bytes.NewBuffer([]byte{})
b.WriteString("DELETE FROM ")
// add from
b.WriteString(s.from + " ")
// add where conditions
if len(s.whereConditions) > 0 {
b.WriteString("WHERE ")
b.WriteString(strings.Join(s.whereConditions, " AND "))
}
b.WriteString(";")
return b.String(), nil
}
func (s *deleteBuilder) validate() error {
if s.from == "" {
return ErrEmptyTableName
}
return nil
}