-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterval-null.go
72 lines (60 loc) · 1.84 KB
/
interval-null.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
package pgtypes
import (
"database/sql/driver"
"fmt"
"github.com/jackc/pgx"
)
// NullInterval represents an Interval that may be null.
// It implements the pgx.PgxScanner and pgx.Encoder interfaces so it may be used both as an argument to Query[Row] and a destination for ScanPgx.
//
// If Valid is false then the value is NULL.
type NullInterval struct {
Interval Interval
Valid bool
}
// ScanPgx implements the pgx.PgxScanner interface.
func (n *NullInterval) ScanPgx(vr *pgx.ValueReader) error {
if vr.Type().DataType != IntervalOid {
return pgx.SerializationError(fmt.Sprintf("NullInterval.ScanPgx cannot decode %s (OID %d)", vr.Type().DataTypeName, vr.Type().DataType))
}
if vr.Len() == -1 {
n.Interval, n.Valid = Interval{0, 0, 0, IntervalPgPrecision}, false
return nil
}
n.Valid = true
return n.Interval.ScanPgx(vr)
}
// FormatCode implements the pgx.Encoder interface.
func (n NullInterval) FormatCode() int16 { return pgx.BinaryFormatCode }
// Encode implements the pgx.Encoder interface.
func (n NullInterval) Encode(w *pgx.WriteBuf, oid pgx.Oid) error {
if oid != IntervalOid {
return pgx.SerializationError(fmt.Sprintf("NullInterval.Encode cannot encode into OID %d", oid))
}
if !n.Valid {
w.WriteInt32(-1)
return nil
}
return n.Interval.Encode(w, oid)
}
// Nullable returns valid NullInterval with Interval i.
func (i Interval) Nullable() NullInterval {
return NullInterval{i, true}
}
// Scan implements the sql.Scanner interface.
func (n *NullInterval) Scan(src interface{}) (err error) {
if src == nil {
*n = NullInterval{Interval: Interval{precision: IntervalPgPrecision}}
return nil
}
err = n.Interval.Scan(src)
n.Valid = err == nil
return
}
// Value implements the driver.Valuer interface.
func (n NullInterval) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Interval.Value()
}