-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_text.go
90 lines (74 loc) · 2.31 KB
/
access_text.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
package pgtalk
import (
"strings"
"github.com/jackc/pgx/v5/pgtype"
)
type textAccess struct {
unimplementedBooleanExpression
ColumnInfo
valueFieldWriter fieldAccessFunc
valueToInsert string
}
func NewTextAccess(info ColumnInfo, writer fieldAccessFunc) textAccess {
return textAccess{ColumnInfo: info, valueFieldWriter: writer}
}
func (a textAccess) Set(v string) textAccess {
a.valueToInsert = v
return a
}
func (a textAccess) ValueToInsert() any {
return a.valueToInsert
}
func (a textAccess) Equals(textLike any) binaryExpression {
return a.Compare("=", textLike)
}
func (a textAccess) Compare(op string, stringOrTextAccess any) binaryExpression {
if !strings.Contains(validComparisonOperators, op) {
panic("invalid comparison operator:" + op)
}
if s, ok := stringOrTextAccess.(string); ok {
return makeBinaryOperator(a, op, newLiteralString(s))
}
if ta, ok := stringOrTextAccess.(textAccess); ok {
return makeBinaryOperator(a, op, ta)
}
if p, ok := stringOrTextAccess.(*QueryParameter); ok {
return makeBinaryOperator(a, op, p)
}
panic("string or TextAccess expected")
}
func (a textAccess) FieldValueToScan(entity any) any {
return a.valueFieldWriter(entity)
}
func (a textAccess) Like(pattern string) binaryExpression {
return makeBinaryOperator(a, "LIKE", newLiteralString(pattern))
}
// ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale.
func (a textAccess) ILike(pattern string) binaryExpression {
return makeBinaryOperator(a, "ILIKE", newLiteralString(pattern))
}
func (a textAccess) In(values ...string) binaryExpression {
vs := make([]any, len(values))
for i := 0; i < len(values); i++ {
vs[i] = values[i]
}
return makeBinaryOperator(a, "IN", valuesPrinter{vs: vs})
}
func (a textAccess) Column() ColumnInfo { return a.ColumnInfo }
// TableAlias changes the table alias for this column accessor.
func (a textAccess) TableAlias(alias string) textAccess {
a.ColumnInfo = a.ColumnInfo.TableAlias(alias)
return a
}
// AppendScannable is part of ColumnAccessor
func (a textAccess) AppendScannable(list []any) []any {
return append(list, &a.valueToInsert)
}
// Get returns the value for its columnName from a map (row).
func (a textAccess) Get(values map[string]any) any {
v, ok := values[a.columnName]
if !ok {
return pgtype.Text{}
}
return v
}