-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpartiql_functions.go
49 lines (41 loc) · 1.31 KB
/
partiql_functions.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
package dynmgrm
import (
"database/sql/driver"
"errors"
"fmt"
"github.com/miyamo2/sqldav"
"gorm.io/gorm"
"regexp"
)
var (
reInvalidColumnName = regexp.MustCompile(`^[\w._]+$`)
ErrInvalidColumnName = errors.New("column name contains invalid characters")
)
var (
_ functionForPartiQLUpdates = (*listAppend)(nil)
)
// functionForPartiQLUpdates is an interface for PartiQL functions that can be used in updates.
type functionForPartiQLUpdates interface {
// expression returns a string that represents the function in the SQL query.
expression(db *gorm.DB, column string) string
// bindVariable returns a gorm.Valuer that represents the bind variable for the function.
bindVariable() driver.Valuer
}
// listAppend is a struct that implements functionForPartiQLUpdates interface for `list_append` function.
type listAppend struct {
value sqldav.List
}
func (la *listAppend) expression(db *gorm.DB, column string) string {
if !reInvalidColumnName.MatchString(column) {
db.AddError(ErrInvalidColumnName)
return ""
}
return fmt.Sprintf("list_append(%s, ", column)
}
func (la *listAppend) bindVariable() driver.Valuer {
return la.value
}
// ListAppend returns a functionForPartiQLUpdates implementation for `list_append` function.
func ListAppend(item ...interface{}) *listAppend {
return &listAppend{value: item}
}