forked from slicebit/qb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatement.go
63 lines (52 loc) · 1.27 KB
/
statement.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
package qb
import (
"fmt"
"strings"
)
const defaultDelimiter = "\n"
// Statement creates a new query and returns its pointer
func Statement() *Stmt {
return &Stmt{
clauses: []string{},
bindings: []interface{}{},
delimiter: defaultDelimiter,
bindingIndex: 0,
}
}
// Stmt is the base abstraction for all sql queries
type Stmt struct {
clauses []string
bindings []interface{}
delimiter string
bindingIndex int
}
// SetDelimiter sets the delimiter of query
func (s *Stmt) SetDelimiter(delimiter string) {
s.delimiter = delimiter
}
// AddClause appends a new clause to current query
func (s *Stmt) AddClause(clause string) {
s.clauses = append(s.clauses, clause)
}
// AddBinding appends a new binding to current query
func (s *Stmt) AddBinding(bindings ...interface{}) {
for _, v := range bindings {
s.bindings = append(s.bindings, v)
}
}
// Clauses returns all clauses of current query
func (s *Stmt) Clauses() []string {
return s.clauses
}
// Bindings returns all bindings of current query
func (s *Stmt) Bindings() []interface{} {
return s.bindings
}
// SQL returns the query struct sql statement
func (s *Stmt) SQL() string {
if len(s.clauses) > 0 {
sql := fmt.Sprintf("%s;", strings.Join(s.clauses, s.delimiter))
return sql
}
return ""
}