-
Notifications
You must be signed in to change notification settings - Fork 0
/
drop_table.go
51 lines (38 loc) · 910 Bytes
/
drop_table.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
package tsbuilder
import (
"bytes"
"fmt"
"strings"
)
var _ tdEngineSqlBuilder = (*dropTableBuilder)(nil)
type dropTableBuilder struct {
tables []string
}
func NewDropTableBuilder() *dropTableBuilder {
return &dropTableBuilder{
tables: make([]string, 0),
}
}
func (s *dropTableBuilder) Tables(tables ...string) *dropTableBuilder {
s.tables = append(s.tables, tables...)
return s
}
func (s *dropTableBuilder) Build() (string, error) {
if err := s.validate(); err != nil {
return "", fmt.Errorf("validate error: %w", err)
}
b := bytes.NewBuffer([]byte{})
b.WriteString("DROP TABLE ")
for idx, table := range s.tables {
s.tables[idx] = "IF EXISTS " + table
}
b.WriteString(strings.Join(s.tables, ", "))
b.WriteString(";")
return b.String(), nil
}
func (s *dropTableBuilder) validate() error {
if len(s.tables) == 0 {
return fmt.Errorf("tables are required")
}
return nil
}