-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
137 lines (123 loc) · 3.1 KB
/
helper.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
package sqlite
import (
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"strings"
"time"
)
func LoadTime(stmt *Stmt, key string) time.Time {
value := stmt.GetInt64(key)
return time.Unix(value, 0).UTC()
}
func LoadBool(stmt *Stmt, key string) bool {
return stmt.GetInt64(key) == 1
}
func LoadJsonMap[T any](stmt *Stmt, col string) (map[string]T, error) {
var mapper map[string]T
err := json.NewDecoder(stmt.GetReader(col)).Decode(&mapper)
// NOTE: we need to check for io.EOF because json.NewDecoder returns io.EOF when the input is empty
// this is not an error, we can just return an empty slice
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
if mapper == nil {
return map[string]T{}, nil
}
return mapper, nil
}
func LoadJsonArray[T any](stmt *Stmt, col string) ([]T, error) {
var array []T
err := json.NewDecoder(stmt.GetReader(col)).Decode(&array)
// NOTE: we need to check for io.EOF because json.NewDecoder returns io.EOF when the input is empty
// this is not an error, we can just return an empty slice
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
if array == nil {
return []T{}, nil
}
return array, nil
}
func LoadJsonStruct[T any](stmt *Stmt, col string) (*T, error) {
var obj T
err := json.NewDecoder(stmt.GetReader(col)).Decode(&obj)
// NOTE: we need to check for io.EOF because json.NewDecoder returns io.EOF when the input is empty
// this is not an error, we can just return an empty obj
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
return &obj, nil
}
// Placeholders returns a string of ? separated by commas
func Placeholders(count int) string {
var sb strings.Builder
placeholders(count, &sb)
return sb.String()
}
func placeholders(count int, sb *strings.Builder) {
for i := 0; i < count; i++ {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString("?")
}
}
func GroupPlaceholdersStringBuilder(numRows, numCols int, sb *strings.Builder) {
for i := 0; i < numRows; i++ {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString("(")
placeholders(numCols, sb)
sb.WriteString(")")
}
}
// (?, ?), (?, ?), (?, ?)
func GroupPlaceholders(numRows, numCols int) string {
var sb strings.Builder
GroupPlaceholdersStringBuilder(numRows, numCols, &sb)
return sb.String()
}
func ShowSql(sql string, args ...any) string {
var temp2 []string
temp := strings.FieldsFunc(sql, func(r rune) bool {
switch r {
case '\t', '\n', ' ':
return true
default:
return false
}
})
for _, tmp := range temp {
if tmp != "" {
temp2 = append(temp2, tmp)
}
}
newArgs := []any{}
for _, arg := range args {
switch v := arg.(type) {
case string:
arg = fmt.Sprintf("'%s'", v)
case time.Time:
arg = fmt.Sprintf("%d", v.Unix())
default:
if v == nil {
arg = "NULL"
break
}
kind := reflect.TypeOf(v).Kind()
if kind == reflect.Slice || kind == reflect.Map {
b, _ := json.Marshal(v)
arg = fmt.Sprintf("'%v'", string(b))
break
}
arg = fmt.Sprintf("%v", v)
}
newArgs = append(newArgs, arg)
}
format := strings.ReplaceAll(strings.Join(temp2, " "), "?", "%v")
return fmt.Sprintf(format, newArgs...)
}