forked from mirror/gorm
Merge pull request #706 from sulnedinfind/master
add binary UUID support to postgres
This commit is contained in:
commit
0589d36e64
21
postgres.go
21
postgres.go
|
@ -5,6 +5,7 @@ import (
|
|||
"database/sql/driver"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq/hstore"
|
||||
|
@ -52,13 +53,31 @@ func (postgres) SqlTag(value reflect.Value, size int, autoIncrease bool) string
|
|||
return "hstore"
|
||||
}
|
||||
default:
|
||||
if _, ok := value.Interface().([]byte); ok {
|
||||
if isByteArray(value) {
|
||||
if isUUID(value) {
|
||||
return "uuid"
|
||||
}
|
||||
return "bytea"
|
||||
}
|
||||
}
|
||||
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 {
|
||||
return fmt.Sprintf("RETURNING %v.%v", tableName, key)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue