-
Notifications
You must be signed in to change notification settings - Fork 1
/
stmt_with.go
66 lines (50 loc) · 1.37 KB
/
stmt_with.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
package dbr
import (
"fmt"
)
type StmtWith struct {
withs withs
isRecursive bool
dbr *Dbr
connections *connections
}
func newStmtWith(dbr *Dbr, connections *connections, name string, isRecursive bool, builder Builder) *StmtWith {
return &StmtWith{
dbr: dbr,
connections: connections,
withs: withs{newWith(name, builder)},
isRecursive: isRecursive,
}
}
func (w *StmtWith) With(name string, builder Builder) *StmtWith {
w.withs = append(w.withs, &with{name: name, builder: builder})
return w
}
func (w *StmtWith) Select(column ...interface{}) *StmtSelect {
columns := newColumns(w.connections.Read, false)
columns.list = column
return newStmtSelect(w.dbr, w.connections.Read, w, columns)
}
func (w *StmtWith) Insert() *StmtInsert {
return newStmtInsert(w.dbr, w.connections.Write, w)
}
func (w *StmtWith) Update(table interface{}) *StmtUpdate {
return newStmtUpdate(w.dbr, w.connections.Write, w, table)
}
func (w *StmtWith) Delete() *StmtDelete {
return newStmtDelete(w.dbr, w.connections.Write, w)
}
func (w *StmtWith) Build() (string, error) {
if len(w.withs) == 0 {
return "", nil
}
withs, err := w.withs.Build()
if err != nil {
return "", err
}
var recursive string
if w.isRecursive {
recursive = fmt.Sprintf("%s ", constFunctionRecursive)
}
return fmt.Sprintf("%s %s%s", constFunctionWith, recursive, withs), nil
}