Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: add Native value to Type. #104

Merged
merged 1 commit into from
Feb 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions sql/type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sql

import (
"database/sql/driver"
"fmt"
"reflect"
"strconv"
Expand Down Expand Up @@ -43,6 +44,7 @@ type Type interface {
Check(interface{}) bool
Convert(interface{}) (interface{}, error)
Compare(interface{}, interface{}) int
Native(interface{}) driver.Value
}

var Null = nullType{}
Expand Down Expand Up @@ -75,6 +77,10 @@ func (t nullType) Compare(a interface{}, b interface{}) int {
return 0
}

func (t nullType) Native(v interface{}) driver.Value {
return driver.Value(nil)
}

var Integer = integerType{}

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

func (t integerType) Native(v interface{}) driver.Value {
return driver.Value(int64(v.(int32)))
}

var BigInteger = bigIntegerType{}

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

func (t bigIntegerType) Native(v interface{}) driver.Value {
return driver.Value(v.(int64))
}

// TimestampWithTimezone is a timestamp with timezone.
var TimestampWithTimezone = timestampWithTimeZoneType{}

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

func (t timestampWithTimeZoneType) Native(v interface{}) driver.Value {
return driver.Value(v.(time.Time))
}

var String = stringType{}

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

func (t stringType) Native(v interface{}) driver.Value {
return driver.Value(v.(string))
}

var Boolean Type = booleanType{}

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

func (t booleanType) Native(v interface{}) driver.Value {
return driver.Value(v.(bool))
}

var Float Type = floatType{}

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

func (t floatType) Native(v interface{}) driver.Value {
return driver.Value(v.(float64))
}

func checkString(v interface{}) bool {
_, ok := v.(string)
return ok
Expand Down