-
Notifications
You must be signed in to change notification settings - Fork 0
/
fieldaccess.go
137 lines (119 loc) · 3.71 KB
/
fieldaccess.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
package pgtalk
import (
"fmt"
"strings"
)
type FieldAccess[T any] struct {
unimplementedBooleanExpression
ColumnInfo
valueFieldWriter fieldAccessFunc
valueToInsert T
}
func NewFieldAccess[T any](
info ColumnInfo,
writer func(dest any) any) FieldAccess[T] {
return FieldAccess[T]{
ColumnInfo: info,
valueFieldWriter: writer}
}
func (a FieldAccess[T]) Column() ColumnInfo { return a.ColumnInfo }
func (a FieldAccess[T]) FieldValueToScan(entity any) any {
return a.valueFieldWriter(entity)
}
// Set returns a new FieldAccess[T] with a value to set on a T.
func (a FieldAccess[T]) Set(v T) FieldAccess[T] {
a.valueToInsert = v
return a
}
// Get returns the value for its columnName from a map (row).
func (a FieldAccess[T]) Get(values map[string]any) any {
v, ok := values[a.columnName]
if !ok {
var none T
return none
}
return v
}
func (a FieldAccess[T]) ValueToInsert() any {
return a.valueToInsert
}
func (a FieldAccess[T]) Concat(resultName string, ex SQLExpression) ColumnAccessor {
return &computedField{
ResultName: resultName,
Expression: binaryExpression{
Left: a,
Operator: "||",
Right: ex,
}}
}
// Equals returns a SQLExpression
func (a FieldAccess[T]) Equals(operand any) binaryExpression {
if fat, ok := operand.(FieldAccess[T]); ok {
return makeBinaryOperator(a, "=", fat)
}
if t, ok := operand.(T); ok {
return makeBinaryOperator(a, "=", valuePrinter{v: t})
}
if anyp, ok := operand.(*QueryParameter); ok {
if _, ok := anyp.value.(T); ok {
return makeBinaryOperator(a, "=", anyp) // use parameter, not its value
}
}
return makeBinaryOperator(a, "=", valuePrinter{v: operand})
}
// LessThan returns a SQLExpression
func (a FieldAccess[T]) LessThan(operand any) binaryExpression {
if fat, ok := operand.(FieldAccess[T]); ok {
return makeBinaryOperator(a, "<", fat)
}
if t, ok := operand.(T); ok {
return makeBinaryOperator(a, "<", valuePrinter{v: t})
}
var t T
panic("expected a " + fmt.Sprintf("%T", t) + " got a " + fmt.Sprintf("%T", operand))
}
// GreaterThan returns a SQLExpression
func (a FieldAccess[T]) GreaterThan(operand any) binaryExpression {
if fat, ok := operand.(FieldAccess[T]); ok {
return makeBinaryOperator(a, ">", fat)
}
if t, ok := operand.(T); ok {
return makeBinaryOperator(a, ">", valuePrinter{v: t})
}
var t T
panic("expected a " + fmt.Sprintf("%T", t) + " got a " + fmt.Sprintf("%T", operand))
}
// In returns a binary expression to check that the value of the fieldAccess is in the values collection.
func (a FieldAccess[T]) In(values ...T) SQLExpression {
if len(values) == 0 {
return makeConstantExpression(false)
}
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 FieldAccess[T]) Compare(operator string, operand any) binaryExpression {
if !strings.Contains(validComparisonOperators, operator) {
panic("invalid comparison operator:" + operator)
}
return makeBinaryOperator(a, operator, valuePrinter{v: operand})
}
// TableAlias changes the table alias for this column accessor.
func (a FieldAccess[T]) TableAlias(alias string) FieldAccess[T] {
a.ColumnInfo = a.ColumnInfo.TableAlias(alias)
return a
}
// IsNull creates a SQL Expresion with IS NULL.
func (a FieldAccess[T]) IsNull() binaryExpression {
return makeBinaryOperator(a, "IS", valuePrinter{v: SQLLiteral{Literal: "NULL"}})
}
// IsNotNull creates a SQL Expresion with IS NOT NULL.
func (a FieldAccess[T]) IsNotNull() binaryExpression {
return makeBinaryOperator(a, "IS NOT", valuePrinter{v: SQLLiteral{Literal: "NULL"}})
}
// AppendScannable is part of ColumnAccessor
func (a FieldAccess[T]) AppendScannable(list []any) []any {
return append(list, &a.valueToInsert)
}