forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.go
171 lines (139 loc) · 2.63 KB
/
scan.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package types
import (
"encoding/hex"
"errors"
"fmt"
"reflect"
"time"
"github.com/go-pg/pg/internal"
)
func Scan(v interface{}, rd Reader, n int) error {
var err error
switch v := v.(type) {
case *string:
*v, err = ScanString(rd, n)
return err
case *[]byte:
*v, err = ScanBytes(rd, n)
return err
case *int:
*v, err = ScanInt(rd, n)
return err
case *int64:
*v, err = ScanInt64(rd, n)
return err
case *time.Time:
*v, err = ScanTime(rd, n)
return err
}
vv := reflect.ValueOf(v)
if !vv.IsValid() {
return errors.New("pg: Scan(nil)")
}
if vv.Kind() != reflect.Ptr {
return fmt.Errorf("pg: Scan(nonsettable %T)", v)
}
vv = vv.Elem()
if !vv.IsValid() {
return fmt.Errorf("pg: Scan(nonsettable %T)", v)
}
return ScanValue(vv, rd, n)
}
func ScanString(rd Reader, n int) (string, error) {
if n <= 0 {
return "", nil
}
b, err := rd.ReadFull()
if err != nil {
return "", err
}
return internal.BytesToString(b), nil
}
func ScanBytes(rd Reader, n int) ([]byte, error) {
if n <= 0 {
return nil, nil
}
tmp, err := rd.ReadFullTemp()
if err != nil {
return nil, err
}
if len(tmp) < 2 {
return nil, fmt.Errorf("pg: can't parse bytea: %q", tmp)
}
if tmp[0] != '\\' || tmp[1] != 'x' {
return nil, fmt.Errorf("pg: can't parse bytea: %q", tmp)
}
tmp = tmp[2:] // Trim off "\\x".
b := make([]byte, hex.DecodedLen(len(tmp)))
written, err := hex.Decode(b, tmp)
if err != nil {
return nil, err
}
return b[:written], err
}
func ScanInt(rd Reader, n int) (int, error) {
if n <= 0 {
return 0, nil
}
tmp, err := rd.ReadFullTemp()
if err != nil {
return 0, err
}
num, err := internal.Atoi(tmp)
if err != nil {
return 0, err
}
return num, nil
}
func ScanInt64(rd Reader, n int) (int64, error) {
if n <= 0 {
return 0, nil
}
tmp, err := rd.ReadFullTemp()
if err != nil {
return 0, err
}
num, err := internal.ParseInt(tmp, 10, 64)
if err != nil {
return 0, err
}
return num, nil
}
func ScanUint64(rd Reader, n int) (uint64, error) {
if n <= 0 {
return 0, nil
}
tmp, err := rd.ReadFullTemp()
if err != nil {
return 0, err
}
num, err := internal.ParseUint(tmp, 10, 64)
if err != nil {
return 0, err
}
return num, nil
}
func ScanFloat64(rd Reader, n int) (float64, error) {
if n <= 0 {
return 0, nil
}
tmp, err := rd.ReadFullTemp()
if err != nil {
return 0, err
}
num, err := internal.ParseFloat(tmp, 64)
if err != nil {
return 0, err
}
return num, nil
}
func ScanTime(rd Reader, n int) (time.Time, error) {
if n <= 0 {
return time.Time{}, nil
}
tmp, err := rd.ReadFullTemp()
if err != nil {
return time.Time{}, err
}
return ParseTime(tmp)
}