-
Notifications
You must be signed in to change notification settings - Fork 0
/
referencer.go
212 lines (168 loc) · 4.84 KB
/
referencer.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package sqluct
import (
"fmt"
"sort"
"strings"
"github.com/Masterminds/squirrel"
)
// QuoteANSI adds double quotes to symbols names.
//
// Suitable for PostgreSQL, MySQL in ANSI SQL_MODE, SQLite statements.
func QuoteANSI(tableAndColumn ...string) string {
res := strings.Builder{}
for i, item := range tableAndColumn {
if i != 0 {
res.WriteString(".")
}
res.WriteString(`"`)
res.WriteString(strings.ReplaceAll(item, `"`, `""`))
res.WriteString(`"`)
}
return res.String()
}
// QuoteBackticks quotes symbol names with backticks.
//
// Suitable for MySQL, SQLite statements.
func QuoteBackticks(tableAndColumn ...string) string {
res := strings.Builder{}
for i, item := range tableAndColumn {
if i != 0 {
res.WriteString(".")
}
res.WriteString("`")
res.WriteString(strings.ReplaceAll(item, "`", "``"))
res.WriteString("`")
}
return res.String()
}
// QuoteNoop does not add any quotes to symbol names.
//
// Used in Referencer by default.
func QuoteNoop(tableAndColumn ...string) string {
return strings.Join(tableAndColumn, ".")
}
// Referencer maintains a list of string references to fields and table aliases.
type Referencer struct {
Mapper *Mapper
// IdentifierQuoter is formatter of column and table names.
// Default QuoteNoop.
IdentifierQuoter func(tableAndColumn ...string) string
refs map[interface{}]Quoted
columnNames map[interface{}]string
structColumns map[interface{}][]string
}
// ColumnsOf makes a Mapper option to prefix columns with table alias.
//
// Argument is either a structure pointer or string alias.
func (r *Referencer) ColumnsOf(rowStructPtr interface{}) func(o *Options) {
var table Quoted
switch v := rowStructPtr.(type) {
case string:
table = r.Q(v)
case Quoted:
table = v
default:
t, found := r.refs[rowStructPtr]
if !found {
panic("row structure pointer needs to be added first with AddTableAlias")
}
table = t
}
return func(o *Options) {
o.PrepareColumn = func(col string) string {
return string(table + "." + r.Q(col))
}
}
}
// AddTableAlias creates string references for row pointer and all suitable field pointers in it.
//
// Empty alias is not added to column reference.
func (r *Referencer) AddTableAlias(rowStructPtr interface{}, alias string) {
f, err := mapper(r.Mapper).FindColumnNames(rowStructPtr)
if err != nil {
panic(err)
}
if r.refs == nil {
r.refs = make(map[interface{}]Quoted, len(f)+1)
}
if r.columnNames == nil {
r.columnNames = make(map[interface{}]string, len(f))
}
if r.structColumns == nil {
r.structColumns = make(map[interface{}][]string)
}
if alias != "" {
r.refs[rowStructPtr] = r.Q(alias)
}
columns := make([]string, 0, len(f))
for ptr, fieldName := range f {
var col string
if alias == "" {
col = string(r.Q(fieldName))
} else {
col = string(r.Q(alias, fieldName))
}
columns = append(columns, col)
r.refs[ptr] = Quoted(col)
r.columnNames[ptr] = fieldName
}
sort.Strings(columns)
r.structColumns[rowStructPtr] = columns
}
// Quoted is a string that can be interpolated into an SQL statement as is.
type Quoted string
// Q quotes identifier.
func (r *Referencer) Q(tableAndColumn ...string) Quoted {
if r.IdentifierQuoter == nil {
return Quoted(QuoteNoop(tableAndColumn...))
}
return Quoted(r.IdentifierQuoter(tableAndColumn...))
}
// Ref returns reference string for struct or field pointer that was previously added with AddTableAlias.
//
// It panics if pointer is unknown.
func (r *Referencer) Ref(ptr interface{}) string {
if ref, found := r.refs[ptr]; found {
return string(ref)
}
panic(errUnknownFieldOrRow)
}
// Col returns unescaped column name for field pointer that was previously added with AddTableAlias.
//
// It panics if pointer is unknown.
// Might be used with Options.Columns.
func (r *Referencer) Col(ptr interface{}) string {
if col, found := r.columnNames[ptr]; found {
return col
}
panic(errUnknownFieldOrRow)
}
// Fmt formats according to a format specified replacing ptrs with their reference strings where possible.
//
// It panics if pointer is unknown or is not a Quoted string.
func (r *Referencer) Fmt(format string, ptrs ...interface{}) string {
args := make([]interface{}, 0, len(ptrs))
for i, fieldPtr := range ptrs {
if q, ok := fieldPtr.(Quoted); ok {
args = append(args, string(q))
continue
}
if ref, found := r.refs[fieldPtr]; found {
args = append(args, ref)
} else {
panic(fmt.Errorf("%w at position %d", errUnknownFieldOrRow, i))
}
}
return fmt.Sprintf(format, args...)
}
// Cols returns column references of a row structure.
func (r *Referencer) Cols(ptr interface{}) []string {
if cols, found := r.structColumns[ptr]; found {
return cols
}
panic(errUnknownFieldOrRow)
}
// Eq is a shortcut for squirrel.Eq{r.Ref(ptr): val}.
func (r *Referencer) Eq(ptr interface{}, val interface{}) squirrel.Eq {
return squirrel.Eq{r.Ref(ptr): val}
}