-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutationset.go
152 lines (138 loc) · 3.65 KB
/
mutationset.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
package pgtalk
import (
"context"
"fmt"
"io"
)
const (
MutationDelete = iota
MutationInsert
MutationUpdate
)
type MutationSet[T any] struct {
tableInfo TableInfo
selectors []ColumnAccessor
condition SQLExpression
returning []ColumnAccessor
operationType int
}
func MakeMutationSet[T any](tableInfo TableInfo, selectors []ColumnAccessor, operationType int) MutationSet[T] {
return MutationSet[T]{
tableInfo: tableInfo,
selectors: selectors,
condition: EmptyCondition,
operationType: operationType}
}
// SQLOn returns the full SQL mutation query
func (m MutationSet[T]) SQLOn(w WriteContext) {
if m.operationType == MutationInsert {
fmt.Fprint(w, "INSERT INTO ")
fmt.Fprintf(w, "%s.%s", m.tableInfo.Schema, m.tableInfo.Name)
fmt.Fprint(w, " (")
m.columnsSectionOn(m.selectors, w)
fmt.Fprint(w, ")\nVALUES (")
m.valuesSectionOn(w)
fmt.Fprint(w, ")")
if len(m.returning) > 0 {
fmt.Fprint(w, "\nRETURNING ")
m.columnsSectionOn(m.returning, w)
}
return
}
if m.operationType == MutationDelete {
fmt.Fprint(w, "DELETE FROM\n")
m.tableInfo.SQLOn(w)
if m.condition != EmptyCondition {
fmt.Fprint(w, "\nWHERE ")
m.condition.SQLOn(w)
}
if len(m.returning) > 0 {
fmt.Fprint(w, "\nRETURNING ")
m.columnsSectionOn(m.returning, w)
}
return
}
if m.operationType == MutationUpdate {
fmt.Fprint(w, "UPDATE ")
m.tableInfo.SQLOn(w)
fmt.Fprint(w, "\nSET ")
m.setSectionOn(w)
if m.condition != EmptyCondition {
fmt.Fprint(w, "\nWHERE ")
}
m.condition.SQLOn(w)
if len(m.returning) > 0 {
fmt.Fprint(w, "\nRETURNING ")
m.columnsSectionOn(m.returning, w)
}
return
}
}
func (m MutationSet[T]) Where(condition SQLExpression) MutationSet[T] {
m.condition = condition
return m
}
func (m MutationSet[T]) Returning(columns ...ColumnAccessor) MutationSet[T] {
m.returning = columns
return m
}
// todo
func (m MutationSet[T]) On() MutationSet[T] {
return m
}
// Pre: must be run inside transaction.
// The iterator is closed unless it has data to return (set via Returning).
// The iterator must be closed on error or after consuming all data.
func (m MutationSet[T]) Exec(ctx context.Context, conn querier, parameters ...*QueryParameter) ResultIterator[T] {
// first collect parameters with query indices
params := m.valuesToInsert(parameters)
// then compose SQL
query := SQL(m)
rows, err := conn.Query(ctx, query, params...)
if err == nil && !m.canProduceResults() {
rows.Close()
}
return &resultIterator[T]{queryError: err, rows: rows, selectors: m.returning, params: params}
}
// valuesToInsert returns the parameters values for the mutation query.
// These are composed of all selectors and query arguments.
func (m MutationSet[T]) valuesToInsert(params []*QueryParameter) []any {
args := make([]any, len(m.selectors)+len(params))
for i, each := range m.selectors {
args[i] = each.ValueToInsert()
}
for i, each := range params {
argIndex := len(m.selectors) + i
// update queryIndex
each.queryIndex = argIndex + 1
args[argIndex] = each.value
}
return args
}
func (m MutationSet[T]) canProduceResults() bool {
return len(m.returning) > 0
}
func (m MutationSet[T]) columnsSectionOn(which []ColumnAccessor, buf io.Writer) {
for i, each := range which {
if i > 0 {
io.WriteString(buf, ",")
}
io.WriteString(buf, each.Name())
}
}
func (m MutationSet[T]) valuesSectionOn(buf io.Writer) {
for i := range m.selectors {
if i > 0 {
io.WriteString(buf, ",")
}
fmt.Fprintf(buf, "$%d", i+1)
}
}
func (m MutationSet[T]) setSectionOn(w io.Writer) {
for i, each := range m.selectors {
if i > 0 {
io.WriteString(w, ",")
}
fmt.Fprintf(w, "%s = $%d", each.Name(), i+1)
}
}