-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddl.go
112 lines (91 loc) · 2.2 KB
/
ddl.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package statement
import "github.com/brunotm/norm/internal/buffer"
// DDL represents a data definition statement.
type DDL struct {
comment []Statement
*Part
}
// Comment adds a SQL comment to the generated query.
// Each call to comment creates a new `-- <comment>` line.
func (s *DDL) Comment(c string, values ...interface{}) *DDL {
buf := buffer.New()
defer buf.Release()
_, _ = buf.WriteString("-- ")
_, _ = buf.WriteString(c)
p := &Part{}
p.Query = buf.String()
p.Values = values
s.comment = append(s.comment, p)
return s
}
// Create creates a new `CREATE` DDL statement.
func Create(query string, values ...interface{}) *DDL {
buf := buffer.New()
defer buf.Release()
_, _ = buf.WriteString("CREATE ")
_, _ = buf.WriteString(query)
return &DDL{
Part: &Part{
Query: buf.String(),
Values: values,
},
}
}
// Alter creates a new `ALTER` DDL statement.
func Alter(query string, values ...interface{}) *DDL {
buf := buffer.New()
defer buf.Release()
_, _ = buf.WriteString("ALTER ")
_, _ = buf.WriteString(query)
return &DDL{
Part: &Part{
Query: buf.String(),
Values: values,
},
}
}
// Drop creates a new `DROP` DDL statement.
func Drop(query string, values ...interface{}) *DDL {
buf := buffer.New()
defer buf.Release()
_, _ = buf.WriteString("DROP ")
_, _ = buf.WriteString(query)
return &DDL{
Part: &Part{
Query: buf.String(),
Values: values,
},
}
}
// Truncate creates a new `TRUNCATE` DDL statement.
func Truncate(query string, values ...interface{}) *DDL {
buf := buffer.New()
defer buf.Release()
_, _ = buf.WriteString("TRUNCATE ")
_, _ = buf.WriteString(query)
return &DDL{
Part: &Part{
Query: buf.String(),
Values: values,
},
}
}
// Build builds the statement into the given buffer.
func (s *DDL) Build(buf Buffer) (err error) {
for x := 0; x < len(s.comment); x++ {
if err = s.comment[x].Build(buf); err != nil {
return err
}
_, _ = buf.WriteString("\n")
}
return s.build(buf, true)
}
// String builds the statement and returns the resulting query string.
func (s *DDL) String() (q string, err error) {
buf := buffer.New()
defer buf.Release()
if err = s.Build(buf); err != nil {
return "", err
}
return buf.String(), nil
}