Skip to content

Commit 7179f9f

Browse files
authored
sql: add Native method to Type (#104)
Native converts a value of Type into a driver.Value. This is required for compatibility with the database/sql API.
1 parent c868dd4 commit 7179f9f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

sql/type.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sql
22

33
import (
4+
"database/sql/driver"
45
"fmt"
56
"reflect"
67
"strconv"
@@ -43,6 +44,7 @@ type Type interface {
4344
Check(interface{}) bool
4445
Convert(interface{}) (interface{}, error)
4546
Compare(interface{}, interface{}) int
47+
Native(interface{}) driver.Value
4648
}
4749

4850
var Null = nullType{}
@@ -75,6 +77,10 @@ func (t nullType) Compare(a interface{}, b interface{}) int {
7577
return 0
7678
}
7779

80+
func (t nullType) Native(v interface{}) driver.Value {
81+
return driver.Value(nil)
82+
}
83+
7884
var Integer = integerType{}
7985

8086
type integerType struct{}
@@ -99,6 +105,10 @@ func (t integerType) Compare(a interface{}, b interface{}) int {
99105
return compareInt32(a, b)
100106
}
101107

108+
func (t integerType) Native(v interface{}) driver.Value {
109+
return driver.Value(int64(v.(int32)))
110+
}
111+
102112
var BigInteger = bigIntegerType{}
103113

104114
type bigIntegerType struct{}
@@ -123,6 +133,10 @@ func (t bigIntegerType) Compare(a interface{}, b interface{}) int {
123133
return compareInt64(a, b)
124134
}
125135

136+
func (t bigIntegerType) Native(v interface{}) driver.Value {
137+
return driver.Value(v.(int64))
138+
}
139+
126140
// TimestampWithTimezone is a timestamp with timezone.
127141
var TimestampWithTimezone = timestampWithTimeZoneType{}
128142

@@ -148,6 +162,10 @@ func (t timestampWithTimeZoneType) Compare(a interface{}, b interface{}) int {
148162
return compareTimestamp(a, b)
149163
}
150164

165+
func (t timestampWithTimeZoneType) Native(v interface{}) driver.Value {
166+
return driver.Value(v.(time.Time))
167+
}
168+
151169
var String = stringType{}
152170

153171
type stringType struct{}
@@ -172,6 +190,10 @@ func (t stringType) Compare(a interface{}, b interface{}) int {
172190
return compareString(a, b)
173191
}
174192

193+
func (t stringType) Native(v interface{}) driver.Value {
194+
return driver.Value(v.(string))
195+
}
196+
175197
var Boolean Type = booleanType{}
176198

177199
type booleanType struct{}
@@ -196,6 +218,10 @@ func (t booleanType) Compare(a interface{}, b interface{}) int {
196218
return compareBool(a, b)
197219
}
198220

221+
func (t booleanType) Native(v interface{}) driver.Value {
222+
return driver.Value(v.(bool))
223+
}
224+
199225
var Float Type = floatType{}
200226

201227
type floatType struct{}
@@ -220,6 +246,10 @@ func (t floatType) Compare(a interface{}, b interface{}) int {
220246
return compareFloat64(a, b)
221247
}
222248

249+
func (t floatType) Native(v interface{}) driver.Value {
250+
return driver.Value(v.(float64))
251+
}
252+
223253
func checkString(v interface{}) bool {
224254
_, ok := v.(string)
225255
return ok

0 commit comments

Comments
 (0)