forked from Rennbon/go-sqlmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stubs_test.go
80 lines (72 loc) · 1.51 KB
/
stubs_test.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
package sqlmock
import (
"database/sql/driver"
"errors"
"fmt"
"strconv"
"time"
)
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}
type NullInt struct {
Integer int
Valid bool
}
// Satisfy sql.Scanner interface
func (ni *NullInt) Scan(value interface{}) error {
switch v := value.(type) {
case nil:
ni.Integer, ni.Valid = 0, false
case int64:
const maxUint = ^uint(0)
const maxInt = int(maxUint >> 1)
const minInt = -maxInt - 1
if v > int64(maxInt) || v < int64(minInt) {
return errors.New("value out of int range")
}
ni.Integer, ni.Valid = int(v), true
case []byte:
n, err := strconv.Atoi(string(v))
if err != nil {
return err
}
ni.Integer, ni.Valid = n, true
case string:
n, err := strconv.Atoi(v)
if err != nil {
return err
}
ni.Integer, ni.Valid = n, true
default:
return fmt.Errorf("can't convert %T to integer", value)
}
return nil
}
// Satisfy sql.Valuer interface.
func (ni NullInt) Value() (driver.Value, error) {
if !ni.Valid {
return nil, nil
}
return int64(ni.Integer), nil
}
// Satisfy sql.Scanner interface
func (nt *NullTime) Scan(value interface{}) error {
switch v := value.(type) {
case nil:
nt.Time, nt.Valid = time.Time{}, false
case time.Time:
nt.Time, nt.Valid = v, true
default:
return fmt.Errorf("can't convert %T to time.Time", value)
}
return nil
}
// Satisfy sql.Valuer interface.
func (nt NullTime) Value() (driver.Value, error) {
if !nt.Valid {
return nil, nil
}
return nt.Time, nil
}