-
Notifications
You must be signed in to change notification settings - Fork 0
/
alter_table.go
58 lines (47 loc) · 1.31 KB
/
alter_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
52
53
54
55
56
57
58
package sqls
import (
"fmt"
"strings"
)
type constraint struct {
name string
options string
}
type alterTableBuilder struct {
builder *sqlBuilder
table string
addConstraint []constraint
}
func newAlterTableBuilder() *alterTableBuilder {
builder := &alterTableBuilder{}
builder.builder = newSqlBuilder()
builder.addConstraint = make([]constraint, 0)
return builder
}
func ALTER_TABLE(table string) *alterTableBuilder {
s := newAlterTableBuilder()
s.table = table
return s
}
func (s *alterTableBuilder) ADD_CONSTRAINT(name string, options string) *alterTableBuilder {
s.addConstraint = append(s.addConstraint, constraint{name: name, options: options})
return s
}
func (s *alterTableBuilder) Param(v any) string {
return s.builder.Param(v)
}
func (s *alterTableBuilder) Params() []any {
return s.builder.Params(s.String())
}
func (s *alterTableBuilder) String() string {
var sqlString string
sqlString += s.builder.join("ALTER TABLE", "", []string{s.table}, "", "")
if len(s.addConstraint) > 0 {
var addConstraint []string
for _, constraint := range s.addConstraint {
addConstraint = append(addConstraint, fmt.Sprintf("ADD CONSTRAINT %s %s", constraint.name, constraint.options))
}
sqlString += s.builder.join("", "", addConstraint, ", ", "")
}
return strings.Trim(sqlString, " ")
}