...
1 package sqlxx
2
3 import (
4 "fmt"
5 "strings"
6
7 "github.com/fatih/structs"
8
9 "github.com/ory/x/stringslice"
10 )
11
12 func keys(t interface{}, exclude []string) []string {
13 s := structs.New(t)
14 var keys []string
15 for _, field := range s.Fields() {
16 key := strings.Split(field.Tag("db"), ",")[0]
17 if len(key) > 0 && key != "-" && !stringslice.Has(exclude, key) {
18 keys = append(keys, key)
19 }
20 }
21
22 return keys
23 }
24
25
26
27
28
29
30
31
32
33
34
35
36
37 func NamedInsertArguments(t interface{}, exclude ...string) (columns string, arguments string) {
38 keys := keys(t, exclude)
39 return strings.Join(keys, ", "),
40 ":" + strings.Join(keys, ", :")
41 }
42
43
44
45
46
47
48
49
50
51
52
53
54 func NamedUpdateArguments(t interface{}, exclude ...string) string {
55 keys := keys(t, exclude)
56 statements := make([]string, len(keys))
57
58 for k, key := range keys {
59 statements[k] = fmt.Sprintf("%s=:%s", key, key)
60 }
61
62 return strings.Join(statements, ", ")
63 }
64
View as plain text