forked from slicebit/qb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
statement.go
75 lines (63 loc) · 1.61 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
64
65
66
67
68
69
70
71
72
73
74
75
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
}
// Text is for executing raw sql
// It parses the sql and generates clauses from
func (s *Stmt) Text(sql string) {
sql = strings.Replace(sql, ";", "", -1)
sql = strings.Replace(sql, "\t", "", -1)
sql = strings.Trim(sql, "\n")
clauses := strings.Split(sql, "\n")
for _, c := range clauses {
s.clauses = append(s.clauses, c)
}
}
// SetDelimiter sets the delimiter of query
func (s *Stmt) SetDelimiter(delimiter string) {
s.delimiter = delimiter
}
// AddSQLClause appends a new clause to current query
func (s *Stmt) AddSQLClause(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)
}
}
// SQLClauses returns all clauses of current query
func (s *Stmt) SQLClauses() []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 ""
}