This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
cols.go
191 lines (153 loc) · 3.34 KB
/
cols.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package gomodel
import (
"strings"
"github.com/cosiner/gomodel/utils"
)
type (
Cols interface {
// Cols return colum names
Names() []string
// String return columns string join with ",",
// result like "foo, bar"
String() string
// Paramed return columns string joind with "=?,", last "," was trimed,
// result like "foo=?, bar=?"
Paramed() string
// IncrParamed: result like "foo=foo+?, bar=bar+?"
IncrParamed() string
// OnlyParam return columns placeholdered string,
// each column was replaced with "?"
// result like "?, ?, ?, ?", count of "?" is colums length
OnlyParam() string
// Join append suffix string to each columns then join them with the seperator
Join(suffix, sep string) string
// Length return columns count
Length() int
}
// MultipleCols used for columns more than two
MultipleCols struct {
Cols []string
str string
paramed string
incrParamed string
onlyParamed string
}
// singleCol means only one column
SingleCol string
// emptyCols means there is no columns
emptyCols string
ColNum int
)
const _emptyCols = emptyCols("")
func (c *MultipleCols) Names() []string {
return c.Cols
}
func (c *MultipleCols) String() string {
if c.str == "" {
c.str = strings.Join(c.Cols, ",")
}
return c.str
}
func (c *MultipleCols) Paramed() string {
if c.paramed == "" {
c.paramed = utils.JoinStrings(c.Cols, "=?", ",")
}
return c.paramed
}
func (c *MultipleCols) IncrParamed() string {
if c.incrParamed == "" {
bytes := make([]byte, 0, len(c.Cols)*8)
for i, col := range c.Cols {
if i != 0 {
bytes = append(bytes, ',')
}
bytes = append(bytes, col...)
bytes = append(bytes, '=')
bytes = append(bytes, col...)
bytes = append(bytes, "+?"...)
}
c.incrParamed = string(bytes)
}
return c.incrParamed
}
func (c *MultipleCols) OnlyParam() string {
if c.onlyParamed == "" {
c.onlyParamed = OnlyParamed(len(c.Cols))
}
return c.onlyParamed
}
func (c *MultipleCols) Join(suffix, sep string) string {
return utils.JoinStrings(c.Cols, suffix, sep)
}
func (c *MultipleCols) Length() int {
return len(c.Cols)
}
func (c SingleCol) Names() []string {
return []string{string(c)}
}
func (c SingleCol) String() string {
return string(c)
}
func (c SingleCol) IncrParamed() string {
return string(c) + "=" + string(c) + "+?"
}
func (c SingleCol) Paramed() string {
return string(c) + "=?"
}
func (c SingleCol) OnlyParam() string {
return "?"
}
func (c SingleCol) Join(suffix, _ string) string {
return string(c) + suffix
}
func (c SingleCol) Length() int {
return 1
}
func (emptyCols) Names() []string {
return nil
}
func (emptyCols) String() string {
return ""
}
func (emptyCols) Paramed() string {
return ""
}
func (emptyCols) IncrParamed() string {
return ""
}
func (emptyCols) OnlyParam() string {
return ""
}
func (emptyCols) Join(_, _ string) string {
return ""
}
func (emptyCols) Length() int {
return 0
}
func OnlyParamed(n int) string {
switch n {
case 0:
return ""
case 1:
return "?"
case 2:
return "?,?"
case 3:
return "?,?,?"
case 4:
return "?,?,?,?"
case 5:
return "?,?,?,?,?"
case 6:
return "?,?,?,?,?,?"
case 7:
return "?,?,?,?,?,?,?"
case 8:
return "?,?,?,?,?,?,?,?"
case 9:
return "?,?,?,?,?,?,?,?,?"
case 10:
return "?,?,?,?,?,?,?,?,?,?"
}
return utils.JoinStrings(utils.MakeStrings("?", int(n)), "", ",")
}