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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package bundb
import (
"context"
"database/sql"
"reflect"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect"
)
// UpsertQuery is a wrapper around an insert query that can update if an insert fails.
// Doesn't implement the full set of Bun query methods, but we can add more if we need them.
// See https://bun.uptrace.dev/guide/query-insert.html#upsert
type UpsertQuery struct {
db bun.IDB
model interface{}
constraints []string
columns []string
}
func NewUpsert(idb bun.IDB) *UpsertQuery {
// note: passing in rawtx as conn iface so no double query-hook
// firing when passed through the bun.Tx.Query___() functions.
return &UpsertQuery{db: idb}
}
// Model sets the model or models to upsert.
func (u *UpsertQuery) Model(model interface{}) *UpsertQuery {
u.model = model
return u
}
// Constraint sets the columns or indexes that are used to check for conflicts.
// This is required.
func (u *UpsertQuery) Constraint(constraints ...string) *UpsertQuery {
u.constraints = constraints
return u
}
// Column sets the columns to update if an insert does't happen.
// If empty, all columns not being used for constraints will be updated.
// Cannot overlap with Constraint.
func (u *UpsertQuery) Column(columns ...string) *UpsertQuery {
u.columns = columns
return u
}
// insertDialect errors if we're using a dialect in which we don't know how to upsert.
func (u *UpsertQuery) insertDialect() error {
dialectName := u.db.Dialect().Name()
switch dialectName {
case dialect.PG, dialect.SQLite:
return nil
default:
// FUTURE: MySQL has its own variation on upserts, but the syntax is different.
return gtserror.Newf("UpsertQuery: upsert not supported by SQL dialect: %s", dialectName)
}
}
// insertConstraints checks that we have constraints and returns them.
func (u *UpsertQuery) insertConstraints() ([]string, error) {
if len(u.constraints) == 0 {
return nil, gtserror.New("UpsertQuery: upserts require at least one constraint column or index, none provided")
}
return u.constraints, nil
}
// insertColumns returns the non-constraint columns we'll be updating.
func (u *UpsertQuery) insertColumns(constraints []string) ([]string, error) {
// Constraints as a set.
constraintSet := make(map[string]struct{}, len(constraints))
for _, constraint := range constraints {
constraintSet[constraint] = struct{}{}
}
var columns []string
var err error
if len(u.columns) == 0 {
columns, err = u.insertColumnsDefault(constraintSet)
} else {
columns, err = u.insertColumnsSpecified(constraintSet)
}
if err != nil {
return nil, err
}
if len(columns) == 0 {
return nil, gtserror.New("UpsertQuery: there are no columns to update when upserting")
}
return columns, nil
}
// hasElem returns whether the type has an element and can call [reflect.Type.Elem] without panicking.
func hasElem(modelType reflect.Type) bool {
switch modelType.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Pointer, reflect.Slice:
return true
default:
return false
}
}
// insertColumnsDefault returns all non-constraint columns from the model schema.
func (u *UpsertQuery) insertColumnsDefault(constraintSet map[string]struct{}) ([]string, error) {
// Get underlying struct type.
modelType := reflect.TypeOf(u.model)
for hasElem(modelType) {
modelType = modelType.Elem()
}
table := u.db.Dialect().Tables().Get(modelType)
if table == nil {
return nil, gtserror.Newf("UpsertQuery: couldn't find the table schema for model: %v", u.model)
}
columns := make([]string, 0, len(u.columns))
for _, field := range table.Fields {
column := field.Name
if _, overlaps := constraintSet[column]; !overlaps {
columns = append(columns, column)
}
}
return columns, nil
}
// insertColumnsSpecified ensures constraints and specified columns to update don't overlap.
func (u *UpsertQuery) insertColumnsSpecified(constraintSet map[string]struct{}) ([]string, error) {
overlapping := make([]string, 0, min(len(u.constraints), len(u.columns)))
for _, column := range u.columns {
if _, overlaps := constraintSet[column]; overlaps {
overlapping = append(overlapping, column)
}
}
if len(overlapping) > 0 {
return nil, gtserror.Newf(
"UpsertQuery: the following columns can't be used for both constraints and columns to update: %s",
strings.Join(overlapping, ", "),
)
}
return u.columns, nil
}
// insert tries to create a Bun insert query from an upsert query.
func (u *UpsertQuery) insertQuery() (*bun.InsertQuery, error) {
var err error
err = u.insertDialect()
if err != nil {
return nil, err
}
constraints, err := u.insertConstraints()
if err != nil {
return nil, err
}
columns, err := u.insertColumns(constraints)
if err != nil {
return nil, err
}
// Build the parts of the query that need us to generate SQL.
constraintIDPlaceholders := make([]string, 0, len(constraints))
constraintIDs := make([]interface{}, 0, len(constraints))
for _, constraint := range constraints {
constraintIDPlaceholders = append(constraintIDPlaceholders, "?")
constraintIDs = append(constraintIDs, bun.Ident(constraint))
}
onSQL := "CONFLICT (" + strings.Join(constraintIDPlaceholders, ", ") + ") DO UPDATE"
setClauses := make([]string, 0, len(columns))
setIDs := make([]interface{}, 0, 2*len(columns))
for _, column := range columns {
setClauses = append(setClauses, "? = ?")
// "excluded" is a special table that contains only the row involved in a conflict.
setIDs = append(setIDs, bun.Ident(column), bun.Ident("excluded."+column))
}
setSQL := strings.Join(setClauses, ", ")
insertQuery := u.db.
NewInsert().
Model(u.model).
On(onSQL, constraintIDs...).
Set(setSQL, setIDs...)
return insertQuery, nil
}
// Exec builds a Bun insert query from the upsert query, and executes it.
func (u *UpsertQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
insertQuery, err := u.insertQuery()
if err != nil {
return nil, err
}
return insertQuery.Exec(ctx, dest...)
}
// Scan builds a Bun insert query from the upsert query, and scans it.
func (u *UpsertQuery) Scan(ctx context.Context, dest ...interface{}) error {
insertQuery, err := u.insertQuery()
if err != nil {
return err
}
return insertQuery.Scan(ctx, dest...)
}
|