diff --git a/postgres.go b/postgres.go index db15b114..3753f12b 100644 --- a/postgres.go +++ b/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) }