mirror of https://github.com/go-gorm/gorm.git
Handle sql.NullValues in search conditions
This commit is contained in:
parent
8d97fdb172
commit
328e9401a0
16
do.go
16
do.go
|
@ -372,7 +372,7 @@ func (s *Do) related(value interface{}, foreign_keys ...string) {
|
||||||
if from_from {
|
if from_from {
|
||||||
s.where(foreign_value).query()
|
s.where(foreign_value).query()
|
||||||
} else {
|
} else {
|
||||||
query := fmt.Sprintf("%v = %v", toSnake(foreign_key), foreign_value)
|
query := fmt.Sprintf("%v = %v", toSnake(foreign_key), s.addToVars(foreign_value))
|
||||||
s.where(query).query()
|
s.where(query).query()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -529,6 +529,8 @@ func (s *Do) buildWhereCondition(clause map[string]interface{}) (str string) {
|
||||||
}
|
}
|
||||||
case int, int64, int32:
|
case int, int64, int32:
|
||||||
return s.primaryCondiation(s.addToVars(query))
|
return s.primaryCondiation(s.addToVars(query))
|
||||||
|
case sql.NullInt64:
|
||||||
|
return s.primaryCondiation(s.addToVars(query.(sql.NullInt64).Int64))
|
||||||
case []int64, []int, []int32, []string:
|
case []int64, []int, []int32, []string:
|
||||||
str = fmt.Sprintf("(%v in (?))", s.model.primaryKeyDb())
|
str = fmt.Sprintf("(%v in (?))", s.model.primaryKeyDb())
|
||||||
clause["args"] = []interface{}{query}
|
clause["args"] = []interface{}{query}
|
||||||
|
@ -557,10 +559,16 @@ func (s *Do) buildWhereCondition(clause map[string]interface{}) (str string) {
|
||||||
temp_marks = append(temp_marks, s.addToVars(values.Index(i).Addr().Interface()))
|
temp_marks = append(temp_marks, s.addToVars(values.Index(i).Addr().Interface()))
|
||||||
}
|
}
|
||||||
str = strings.Replace(str, "?", strings.Join(temp_marks, ","), 1)
|
str = strings.Replace(str, "?", strings.Join(temp_marks, ","), 1)
|
||||||
|
default:
|
||||||
|
switch arg.(type) {
|
||||||
|
case sql.NullInt64, sql.NullFloat64, sql.NullBool, sql.NullString:
|
||||||
|
value := reflect.ValueOf(arg).Field(0).Interface()
|
||||||
|
str = strings.Replace(str, "?", s.addToVars(value), 1)
|
||||||
default:
|
default:
|
||||||
str = strings.Replace(str, "?", s.addToVars(arg), 1)
|
str = strings.Replace(str, "?", s.addToVars(arg), 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -615,10 +623,16 @@ func (s *Do) buildNotCondition(clause map[string]interface{}) (str string) {
|
||||||
temp_marks = append(temp_marks, s.addToVars(values.Index(i).Addr().Interface()))
|
temp_marks = append(temp_marks, s.addToVars(values.Index(i).Addr().Interface()))
|
||||||
}
|
}
|
||||||
str = strings.Replace(str, "?", strings.Join(temp_marks, ","), 1)
|
str = strings.Replace(str, "?", strings.Join(temp_marks, ","), 1)
|
||||||
|
default:
|
||||||
|
switch arg.(type) {
|
||||||
|
case sql.NullInt64, sql.NullFloat64, sql.NullBool, sql.NullString:
|
||||||
|
value := reflect.ValueOf(arg).Field(0).Interface()
|
||||||
|
str = strings.Replace(not_equal_sql, "?", s.addToVars(value), 1)
|
||||||
default:
|
default:
|
||||||
str = strings.Replace(not_equal_sql, "?", s.addToVars(arg), 1)
|
str = strings.Replace(not_equal_sql, "?", s.addToVars(arg), 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ type User struct {
|
||||||
DeletedAt time.Time // DeletedAt: Time of record is deleted, refer Soft Delete for more
|
DeletedAt time.Time // DeletedAt: Time of record is deleted, refer Soft Delete for more
|
||||||
Emails []Email // Embedded structs
|
Emails []Email // Embedded structs
|
||||||
BillingAddress Address // Embedded struct
|
BillingAddress Address // Embedded struct
|
||||||
BillingAddressId int64 // Embedded struct's foreign key
|
BillingAddressId sql.NullInt64 // Embedded struct's foreign key
|
||||||
ShippingAddress Address // Embedded struct
|
ShippingAddress Address // Embedded struct
|
||||||
ShippingAddressId int64 // Embedded struct's foreign key
|
ShippingAddressId int64 // Embedded struct's foreign key
|
||||||
CreditCard CreditCard
|
CreditCard CreditCard
|
||||||
|
@ -32,7 +32,7 @@ type User struct {
|
||||||
type CreditCard struct {
|
type CreditCard struct {
|
||||||
Id int64
|
Id int64
|
||||||
Number string
|
Number string
|
||||||
UserId int64
|
UserId sql.NullInt64
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
DeletedAt time.Time
|
DeletedAt time.Time
|
||||||
|
|
2
model.go
2
model.go
|
@ -108,7 +108,7 @@ func (m *Model) fields(operation string) (fields []Field) {
|
||||||
} else {
|
} else {
|
||||||
switch value.Interface().(type) {
|
switch value.Interface().(type) {
|
||||||
case sql.NullInt64, sql.NullFloat64, sql.NullBool, sql.NullString:
|
case sql.NullInt64, sql.NullFloat64, sql.NullBool, sql.NullString:
|
||||||
field.IsBlank = value.FieldByName("Valid").Interface().(bool)
|
field.IsBlank = !value.FieldByName("Valid").Interface().(bool)
|
||||||
default:
|
default:
|
||||||
m := &Model{data: value.Interface(), driver: m.driver}
|
m := &Model{data: value.Interface(), driver: m.driver}
|
||||||
fields := m.columnsHasValue("other")
|
fields := m.columnsHasValue("other")
|
||||||
|
|
Loading…
Reference in New Issue