-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.go
122 lines (112 loc) · 2.74 KB
/
sql.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
package dalgo2sql
import (
"fmt"
"github.com/dal-go/dalgo/dal"
"reflect"
"slices"
"strings"
"time"
)
type operation = int
const (
insert operation = iota
update
)
type query struct {
text string
args []interface{}
}
func processPrimaryKey(primaryKey []string, key *dal.Key, f func(i int, name string, v any)) {
if len(primaryKey) == 1 {
f(0, primaryKey[0], key.ID)
return
}
id := reflect.ValueOf(key.ID).Interface()
for i, pk := range primaryKey {
var v any
switch id := id.(type) { // TODO(ask-stackoverflow): how to avoid this switch?
case []string:
v = id[i]
case []int:
v = id[i]
case []int8:
v = id[i]
case []int16:
v = id[i]
case []int32:
v = id[i]
case []int64:
v = id[i]
case []time.Time:
v = id[i]
default:
panic(fmt.Sprintf("unsupported type for primary key value %T", id))
}
f(i, pk, v)
}
}
func buildSingleRecordQuery(o operation, options Options, record dal.Record) (query query) {
key := record.Key()
collection := getRecordsetName(key)
pk := options.PrimaryKeyFieldNames(key)
switch o {
case insert:
query.text = "INSERT INTO " + collection
case update:
query.text = fmt.Sprintf("UPDATE %v SET", collection)
}
var cols []string
var argPlaceholders []string
record.SetError(nil)
data := record.Data()
val := reflect.ValueOf(data)
if kind := val.Kind(); kind == reflect.Interface || kind == reflect.Ptr {
val = val.Elem()
}
valType := val.Type()
if key.ID != nil && o == insert {
if len(pk) == 0 {
panic(fmt.Sprintf("record key has value but no primary key defined for: '%s'", collection))
}
processPrimaryKey(pk, key, func(i int, name string, v any) {
cols = append(cols, name)
query.args = append(query.args, v)
argPlaceholders = append(argPlaceholders, "?")
})
}
setColsCount := 0
for i := 0; i < val.NumField(); i++ {
name := valType.Field(i).Name
if slices.Contains(pk, name) {
continue
}
cols = append(cols, name)
query.args = append(query.args, val.Field(i).Interface())
switch o {
case insert:
argPlaceholders = append(argPlaceholders, "?")
case update:
argPlaceholders = append(argPlaceholders, valType.Field(i).Name+" = ?")
setColsCount++
}
}
switch o {
case insert:
query.text += fmt.Sprintf("(%v) VALUES (%v)",
strings.Join(cols, ", "),
strings.Join(argPlaceholders, ", "),
)
case update:
if setColsCount == 0 {
panic(fmt.Sprintf("no fields to update for: '%s'", collection))
}
var pkConditions []string
processPrimaryKey(pk, key, func(i int, name string, v any) {
pkConditions = append(pkConditions, name+" = ?")
query.args = append(query.args, v)
})
query.text += strings.Join(argPlaceholders, ",\n") +
fmt.Sprintf(" WHERE %v = ?", strings.Join(pkConditions, " AND "))
}
return query
}