forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hstore.go
53 lines (44 loc) · 904 Bytes
/
hstore.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
package types
import (
"database/sql"
"fmt"
"reflect"
)
type Hstore struct {
v reflect.Value
append AppenderFunc
scan ScannerFunc
}
var _ ValueAppender = (*Hstore)(nil)
var _ sql.Scanner = (*Hstore)(nil)
func NewHstore(vi interface{}) *Hstore {
v := reflect.ValueOf(vi)
if !v.IsValid() {
panic(fmt.Errorf("pg.Hstore(nil)"))
}
v = reflect.Indirect(v)
if v.Kind() != reflect.Map {
panic(fmt.Errorf("pg.Hstore(unsupported %s)", v.Type()))
}
return &Hstore{
v: v,
append: HstoreAppender(v.Type()),
scan: HstoreScanner(v.Type()),
}
}
func (h *Hstore) Value() interface{} {
if h.v.IsValid() {
return h.v.Interface()
}
return nil
}
func (h *Hstore) AppendValue(b []byte, quote int) ([]byte, error) {
b = h.append(b, h.v, quote)
return b, nil
}
func (h *Hstore) Scan(b interface{}) error {
if b == nil {
return h.scan(h.v, nil)
}
return h.scan(h.v, b.([]byte))
}