-
Notifications
You must be signed in to change notification settings - Fork 38
/
into.go
156 lines (125 loc) · 3.2 KB
/
into.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package sm
import (
"fmt"
"io"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/mysql/dialect"
)
type intoChain struct {
into into
}
func (i *intoChain) Apply(q *dialect.SelectQuery) {
q.SetInto(i.into)
}
func (i *intoChain) CharacterSet(c string) *intoChain {
i.into.characterSet = c
return i
}
func (i *intoChain) FieldsTerminatedBy(str string) *intoChain {
i.into.hasFieldOpt = true
i.into.fieldOptions.terminatedBy = str
return i
}
func (i *intoChain) FieldsEnclosedBy(str string) *intoChain {
i.into.hasFieldOpt = true
i.into.fieldOptions.enclosedByOptional = false
i.into.fieldOptions.enclosedBy = str
return i
}
func (i *intoChain) FieldsOptionallyEnclosedBy(str string) *intoChain {
i.into.hasFieldOpt = true
i.into.fieldOptions.enclosedByOptional = true
i.into.fieldOptions.enclosedBy = str
return i
}
func (i *intoChain) FieldsEscapedBy(str string) *intoChain {
i.into.hasFieldOpt = true
i.into.fieldOptions.escapedBy = str
return i
}
func (i *intoChain) LinesStartingBy(str string) *intoChain {
i.into.hasLineOpt = true
i.into.lineOptions.startingBy = str
return i
}
func (i *intoChain) LinesTerminatedBy(str string) *intoChain {
i.into.hasLineOpt = true
i.into.lineOptions.terminatedBy = str
return i
}
type into struct {
vars []string
dumpfile string
outfile string
// OUTFILE options
characterSet string
hasFieldOpt bool
fieldOptions fieldOptions
hasLineOpt bool
lineOptions lineOptions
}
func (i into) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
// If it has vars, use INTO var_name, var_name ...
if len(i.vars) > 0 {
return bob.ExpressSlice(w, d, start, i.vars, "INTO @", ", @", "")
}
// If dumpfile is present, use INTO DUMPFILE 'file_name'
if i.dumpfile != "" {
_, err := fmt.Fprintf(w, "INTO DUMPFILE '%s'", i.dumpfile)
return nil, err
}
// If no outfile, return nothing
if i.outfile == "" {
return nil, nil
}
_, err := fmt.Fprintf(w, "INTO OUTFILE '%s'", i.dumpfile)
if err != nil {
return nil, err
}
_, err = bob.ExpressIf(w, d, start, i.characterSet,
i.characterSet != "", "\nCHARACTER SET ", "")
if err != nil {
return nil, err
}
_, err = bob.ExpressIf(w, d, start, i.fieldOptions, i.hasFieldOpt, "\n", "")
if err != nil {
return nil, err
}
return nil, nil
}
type fieldOptions struct {
terminatedBy string
escapedBy string
enclosedBy string
enclosedByOptional bool
}
func (f fieldOptions) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
w.Write([]byte("FIELDS"))
if f.terminatedBy != "" {
fmt.Fprintf(w, " TERMINATED BY '%s'", f.terminatedBy)
}
if f.enclosedBy != "" {
if f.enclosedByOptional {
w.Write([]byte(" OPTIONALLY"))
}
fmt.Fprintf(w, " ENCLOSED BY '%s'", f.enclosedBy)
}
if f.escapedBy != "" {
fmt.Fprintf(w, " ESCAPED BY '%s'", f.escapedBy)
}
return nil, nil
}
type lineOptions struct {
startingBy string
terminatedBy string
}
func (l lineOptions) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
w.Write([]byte("LINES"))
if l.startingBy != "" {
fmt.Fprintf(w, " STARTINGBY '%s'", l.terminatedBy)
}
if l.terminatedBy != "" {
fmt.Fprintf(w, " TERMINATED BY '%s'", l.terminatedBy)
}
return nil, nil
}