-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
strings.go
50 lines (47 loc) · 799 Bytes
/
strings.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
package trdsql
import (
"encoding/hex"
"fmt"
"strconv"
"time"
"unicode/utf8"
)
// ValString converts database value to string.
func ValString(v any) string {
switch t := v.(type) {
case nil:
return ""
case string:
return t
case []byte:
if ok := utf8.Valid(t); ok {
return string(t)
}
return `\x` + hex.EncodeToString(t)
case int:
return strconv.Itoa(t)
case int32:
return strconv.FormatInt(int64(t), 10)
case int64:
return strconv.FormatInt(t, 10)
case time.Time:
return t.Format(time.RFC3339)
default:
return fmt.Sprint(v)
}
}
func replaceNULL(nullString string, v any) any {
switch t := v.(type) {
case nil:
return nil
case string:
if t == nullString {
return nil
}
case []byte:
if string(t) == nullString {
return nil
}
}
return v
}