forked from browseraudit/browseraudit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgresql.go
More file actions
59 lines (51 loc) · 1.3 KB
/
postgresql.go
File metadata and controls
59 lines (51 loc) · 1.3 KB
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
package main
// PostgreSQL-specific library functions
import (
"database/sql/driver"
"errors"
"strconv"
"strings"
)
type IntSlice []int
type NullIntSlice struct {
IntSlice IntSlice
Valid bool // true if IntSlice is not NULL
}
// Scans a Postgres integer array into an IntSlice
// http://nerglish.tumblr.com/post/90671946112/reading-arrays-from-postgres-in-golang
func (s *IntSlice) Scan(src interface{}) error {
asBytes, ok := src.([]byte)
if !ok {
return error(errors.New("Scan source was not []byte"))
}
asString := string(asBytes)
(*s) = strToIntSlice(asString)
return nil
}
// Converts a string representing a Postgres integer array into an IntSlice
// http://nerglish.tumblr.com/post/90671946112/reading-arrays-from-postgres-in-golang
func strToIntSlice(s string) []int {
r := strings.Trim(s, "{}")
a := make([]int, 0, 10)
for _, t := range strings.Split(r, ",") {
i,_ := strconv.Atoi(t)
a = append(a, i)
}
return a
}
func (n *NullIntSlice) Scan(value interface{}) error {
asBytes, ok := value.([]byte)
if !ok {
n.IntSlice, n.Valid = make([]int, 0), false
return nil
}
asString := string(asBytes)
n.IntSlice, n.Valid = strToIntSlice(asString), true
return nil
}
func (n NullIntSlice) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.IntSlice, nil
}