-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_int32.go
96 lines (81 loc) · 2.33 KB
/
access_int32.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
package pgtalk
import (
"fmt"
"strings"
"github.com/jackc/pgx/v5/pgtype"
)
// int32Access can Read a column value (int4) and Write a column value and Set a struct field (int32).
type int32Access struct {
unimplementedBooleanExpression
ColumnInfo
fieldWriter fieldAccessFunc
valueToInsert int32
}
func NewInt32Access(
info ColumnInfo,
valueWriter func(dest any) any) int32Access {
return int32Access{
ColumnInfo: info,
fieldWriter: valueWriter}
}
func (a int32Access) BetweenAnd(begin int32, end int32) betweenAnd {
return makeBetweenAnd(a, valuePrinter{v: begin}, valuePrinter{v: end})
}
func (a int32Access) FieldValueToScan(entity any) any {
return a.fieldWriter(entity)
}
func (a int32Access) ValueToInsert() any {
return a.valueToInsert
}
func (a int32Access) Set(v int32) int32Access {
a.valueToInsert = v
return a
}
func (a int32Access) In(values ...int32) 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 int32Access) Equals(intLike any) binaryExpression {
if i, ok := intLike.(int); ok {
return makeBinaryOperator(a, "=", valuePrinter{v: i})
}
if ia, ok := intLike.(int32Access); ok {
return makeBinaryOperator(a, "=", ia)
}
if p, ok := intLike.(*QueryParameter); ok {
return makeBinaryOperator(a, "=", p)
}
if p, ok := intLike.(FieldAccess[pgtype.Int4]); ok {
return makeBinaryOperator(a, "=", p)
}
panic(fmt.Sprintf("int or Int32Access or *QueryParameter expected, got %T", intLike))
}
func (a int32Access) Compare(op string, i int) binaryExpression {
if !strings.Contains(validComparisonOperators, op) {
panic("invalid comparison operator:" + op)
}
return makeBinaryOperator(a, op, valuePrinter{v: i})
}
func (a int32Access) Column() ColumnInfo { return a.ColumnInfo }
func (a int32Access) TableAlias(alias string) int32Access {
a.ColumnInfo = a.ColumnInfo.TableAlias(alias)
return a
}
// AppendScannable is part of ColumnAccessor
func (a int32Access) AppendScannable(list []any) []any {
return append(list, &a.valueToInsert)
}
// Get returns the value for its columnName from a map (row).
func (a int32Access) Get(values map[string]any) any {
v, ok := values[a.columnName]
if !ok {
return int32(0)
}
return v
}