-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
120 lines (107 loc) · 2.64 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
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
}
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
}
return array, 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...)
}