-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
73 lines (64 loc) · 1.3 KB
/
util.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
package dao
import (
"crypto/md5"
"database/sql"
"encoding/hex"
"io"
)
// ColumnValues return dynamic column values by the sql.ColumnType
func ColumnValues(columns []*sql.ColumnType) []interface{} {
values := make([]interface{}, len(columns))
for i, column := range columns {
switch column.DatabaseTypeName() {
case "TEXT":
values[i] = &sql.NullString{}
case "BIGINT", "INT8":
values[i] = &sql.NullInt64{}
case "INT", "INT4":
values[i] = &sql.NullInt32{}
case "DOUBLE", "FLOAT", "FLOAT8":
values[i] = &sql.NullFloat64{}
default:
values[i] = &sql.NullString{}
}
}
return values
}
// ParseColumnValues return true value of column value.
// Retrun nil if the column value is not valid, like nullable.
func ParseColumnValues(value interface{}) interface{} {
switch v := value.(type) {
case *sql.NullInt32:
if v.Valid {
return v.Int32
}
case *sql.NullInt64:
if v.Valid {
return v.Int64
}
case *sql.NullFloat64:
if v.Valid {
return v.Float64
}
case *sql.NullBool:
if v.Valid {
return v.Bool
}
case *sql.NullString:
if v.Valid {
return v.String
}
case *sql.NullTime:
if v.Valid {
return v.Time
}
default:
return nil
}
return nil
}
func Md5(msg string) string {
h := md5.New()
io.WriteString(h, msg)
return hex.EncodeToString(h.Sum(nil))
}