Merge pull request #706 from sulnedinfind/master

add binary UUID support to postgres
This commit is contained in:
Jinzhu 2015-10-29 22:23:35 +08:00
commit 0589d36e64
1 changed files with 20 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"database/sql/driver" "database/sql/driver"
"fmt" "fmt"
"reflect" "reflect"
"strings"
"time" "time"
"github.com/lib/pq/hstore" "github.com/lib/pq/hstore"
@ -52,13 +53,31 @@ func (postgres) SqlTag(value reflect.Value, size int, autoIncrease bool) string
return "hstore" return "hstore"
} }
default: default:
if _, ok := value.Interface().([]byte); ok { if isByteArray(value) {
if isUUID(value) {
return "uuid"
}
return "bytea" return "bytea"
} }
} }
panic(fmt.Sprintf("invalid sql type %s (%s) for postgres", value.Type().Name(), value.Kind().String())) panic(fmt.Sprintf("invalid sql type %s (%s) for postgres", value.Type().Name(), value.Kind().String()))
} }
var byteType = reflect.TypeOf(uint8(0))
func isByteArray(value reflect.Value) bool {
return value.Kind() == reflect.Array && value.Type().Elem() == byteType
}
func isUUID(value reflect.Value) bool {
if value.Type().Len() != 16 {
return false
}
typename := value.Type().Name()
lower := strings.ToLower(typename)
return "uuid" == lower || "guid" == lower
}
func (s postgres) ReturningStr(tableName, key string) string { func (s postgres) ReturningStr(tableName, key string) string {
return fmt.Sprintf("RETURNING %v.%v", tableName, key) return fmt.Sprintf("RETURNING %v.%v", tableName, key)
} }