Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hood

import (
"database/sql"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -67,6 +68,21 @@ func (d *base) SetModelValue(driverValue, fieldValue reflect.Value) error {
} else {
panic(fmt.Sprintf("cannot set created value %T", driverValue.Elem().Interface()))
}
} else if fieldType == reflect.TypeOf(sql.NullBool{}) {
if b, ok := driverValue.Elem().Interface().(bool); ok {
fieldValue.Set(reflect.ValueOf(sql.NullBool{b, true}))
}
} else if fieldType == reflect.TypeOf(sql.NullFloat64{}) {
if f, ok := driverValue.Elem().Interface().(float64); ok {
fieldValue.Set(reflect.ValueOf(sql.NullFloat64{f, true}))
}
} else if fieldType == reflect.TypeOf(sql.NullInt64{}) {
if i, ok := driverValue.Elem().Interface().(int64); ok {
fieldValue.Set(reflect.ValueOf(sql.NullInt64{i, true}))
}
} else if fieldType == reflect.TypeOf(sql.NullString{}) {
str := string(driverValue.Elem().Bytes())
fieldValue.Set(reflect.ValueOf(sql.NullString{str, true}))
}
}
return nil
Expand Down
9 changes: 5 additions & 4 deletions mysql.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hood

import (
"database/sql"
"fmt"
"reflect"
"time"
Expand Down Expand Up @@ -38,20 +39,20 @@ func (d *mysql) SqlType(f interface{}, size int) string {
return "bigint"
case time.Time, Created, Updated:
return "timestamp"
case bool:
case bool, sql.NullBool:
return "boolean"
case int, int8, int16, int32, uint, uint8, uint16, uint32:
return "int"
case int64, uint64:
case int64, uint64, sql.NullInt64:
return "bigint"
case float32, float64:
case float32, float64, sql.NullFloat64:
return "double"
case []byte:
if size > 0 && size < 65532 {
return fmt.Sprintf("varbinary(%d)", size)
}
return "longblob"
case string:
case string, sql.NullString:
if size > 0 && size < 65532 {
return fmt.Sprintf("varchar(%d)", size)
}
Expand Down
9 changes: 5 additions & 4 deletions postgres.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hood

import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"strings"
Expand All @@ -27,17 +28,17 @@ func (d *postgres) SqlType(f interface{}, size int) string {
return "bigserial"
case time.Time, Created, Updated:
return "timestamp with time zone"
case bool:
case bool, sql.NullBool:
return "boolean"
case int, int8, int16, int32, uint, uint8, uint16, uint32:
return "integer"
case int64, uint64:
case int64, uint64, sql.NullInt64:
return "bigint"
case float32, float64:
case float32, float64, sql.NullFloat64:
return "double precision"
case []byte:
return "bytea"
case string:
case string, sql.NullString:
if size > 0 && size < 65532 {
return fmt.Sprintf("varchar(%d)", size)
}
Expand Down