/
append_field.go
52 lines (47 loc) · 953 Bytes
/
append_field.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
package types
import "github.com/go-pg/pg/internal/parser"
func AppendField(b []byte, field string, quote int) []byte {
return appendField(b, parser.NewString(field), quote)
}
func AppendFieldBytes(b []byte, field []byte, quote int) []byte {
return appendField(b, parser.New(field), quote)
}
func appendField(b []byte, p *parser.Parser, quote int) []byte {
var quoted bool
for p.Valid() {
c := p.Read()
switch c {
case '*':
if !quoted {
b = append(b, '*')
continue
}
case '.':
if quoted && quote == 1 {
b = append(b, '"')
quoted = false
}
b = append(b, '.')
if p.Skip('*') {
b = append(b, '*')
} else if quote == 1 {
b = append(b, '"')
quoted = true
}
continue
}
if !quoted && quote == 1 {
b = append(b, '"')
quoted = true
}
if c == '"' {
b = append(b, '"', '"')
} else {
b = append(b, c)
}
}
if quoted && quote == 1 {
b = append(b, '"')
}
return b
}