forked from fluge/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
part.go
55 lines (47 loc) · 950 Bytes
/
part.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
package squirrel
import (
"fmt"
"io"
)
type part struct {
pred interface{}
args []interface{}
}
func newPart(pred interface{}, args ...interface{}) Sqlizer {
return &part{pred, args}
}
func (p part) ToSql() (sql string, args []interface{}) {
switch pred := p.pred.(type) {
case nil:
// no-op
case Sqlizer:
sql, args= pred.ToSql()
case string:
sql = pred
args = p.args
default:
panic(fmt.Errorf("expected string or Sqlizer, not %T", pred))
}
return
}
func appendToSql(parts []Sqlizer, w io.Writer, sep string, args []interface{}) ([]interface{}, error) {
length := len(parts)
for i, p := range parts {
partSql, partArgs := p.ToSql()
if len(partSql) == 0 {
continue
}
_, err := io.WriteString(w, partSql)
if err != nil {
return nil, err
}
if i < length-1 {
_, err := io.WriteString(w, sep)
if err != nil {
return nil, err
}
}
args = append(args, partArgs...)
}
return args, nil
}