Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed the condition to use the default value #409

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
90 changes: 90 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gorp_test

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -181,3 +182,92 @@ AND field12 IN (:FieldIntList)
})
}
}

type comment struct {
ID int64 `db:"id,primarykey,autoincrement"`
Name string `db:"name,notnull,default:'NoName',size:200"`
Text string `db:"text,notnull,size:400"`
Number int `db:"number,notnull,default:'774'"`
Private bool `db:"private,notnull"`
}

func TestDbMap_DefaultTag_oneByOne(t *testing.T) {
tests := []struct {
name string
comment *comment
wantComment comment
}{
{"Use default",
&comment{Text: "Hey!", Private: false},
comment{ID: 1, Name: "NoName", Text: "Hey!", Number: 774, Private: false}},
{"Specify all property",
&comment{Name: "bob", Text: "Hello!", Number: 5, Private: true},
comment{ID: 1, Name: "bob", Text: "Hello!", Number: 5, Private: true}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dbmap := newDbMap()
dbmap.AddTableWithName(comment{}, "comments").SetKeys(true, "id")

err := dbmap.CreateTables()
if err != nil {
t.Errorf("failed to create tables:%v", err)
}
defer dropAndClose(dbmap)

err = dbmap.Insert(tt.comment)
if err != nil {
t.Errorf("failed to insert:%v", err)
}
var gotComment comment
err = dbmap.SelectOne(&gotComment, "SELECT * FROM comments ORDER BY id desc LIMIT 1")
if err != nil {
t.Errorf("failed to select:%v", err)
}
if !reflect.DeepEqual(gotComment, tt.wantComment) {
t.Errorf("gotComment = %+v, want %+v", gotComment, tt.wantComment)
}
})
}
}

func TestDbMap_DefaultTag_allAtOnce(t *testing.T) {
comments := []comment{
comment{Text: "Hey!", Private: false},
comment{Name: "bob", Text: "Hello!", Number: 5, Private: true},
}
wantComments := []comment{
comment{ID: 1, Name: "NoName", Text: "Hey!", Number: 774, Private: false},
comment{ID: 2, Name: "bob", Text: "Hello!", Number: 5, Private: true},
}

t.Run("Insert at once", func(t *testing.T) {
dbmap := newDbMap()
dbmap.AddTableWithName(comment{}, "comments").SetKeys(true, "id")

err := dbmap.CreateTables()
if err != nil {
t.Errorf("failed to create tables:%v", err)
}
defer dropAndClose(dbmap)

for i := range comments {
err = dbmap.Insert(&comments[i])
if err != nil {
t.Errorf("failed to insert:%v", err)
}
}

var gotComments []comment
_, err = dbmap.Select(&gotComments, "SELECT * FROM comments ORDER BY id")
if err != nil {
t.Errorf("failed to select:%v", err)
}
for i := range gotComments {
if !reflect.DeepEqual(gotComments[i], wantComments[i]) {
t.Errorf("gotComment = %+v, want %+v", gotComments[i], wantComments[i])
}
}
})
}
33 changes: 25 additions & 8 deletions table_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,19 @@ func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) {
} else {
if col.DefaultValue == "" {
s2.WriteString(t.dbmap.Dialect.BindVar(x))
if col == t.version {
plan.versField = col.fieldName
plan.argFields = append(plan.argFields, versFieldConst)
} else {
plan.argFields = append(plan.argFields, col.fieldName)
}
x++
} else {
s2.WriteString(col.DefaultValue)
val := elem.FieldByName(col.fieldName).Interface()
s2.WriteString(
fmt.Sprintf("case when %t or %s = %s then %s else %s end",
val == nil, t.dbmap.Dialect.BindVar(x), getZeroValueStringForSQL(val), col.DefaultValue, t.dbmap.Dialect.BindVar(x)))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using result of val == nil here doesn't seem right. You will end up with a plan that has case when true or ... or case when false or ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. It's my wrong push. 🙇
I'm still working on the fix...

}
if col == t.version {
plan.versField = col.fieldName
plan.argFields = append(plan.argFields, versFieldConst)
} else {
plan.argFields = append(plan.argFields, col.fieldName)
}
x++
}
first = false
}
Expand All @@ -161,6 +164,20 @@ func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) {
return plan.createBindInstance(elem, t.dbmap.TypeConverter)
}

func getZeroValueStringForSQL(i interface{}) (s string) {
switch i.(type) {
case bool:
s = "false"
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
s = "0"
case float32, float64:
s = "0.0"
default:
s = "''"
}
return
}

func (t *TableMap) bindUpdate(elem reflect.Value, colFilter ColumnFilter) (bindInstance, error) {
if colFilter == nil {
colFilter = acceptAllFilter
Expand Down
31 changes: 31 additions & 0 deletions table_bindings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package gorp

import "testing"

func Test_getZeroValueStringForSQL(t *testing.T) {
type args struct {
i interface{}
}
tests := []struct {
name string
args args
wantS string
}{
{"bool", args{i: true}, "false"},
{"int", args{i: -5}, "0"},
{"uint", args{i: 100}, "0"},
{"float", args{i: 12.3}, "0.0"},
{"string", args{i: "gorp"}, "''"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotS := getZeroValueStringForSQL(tt.args.i); gotS != tt.wantS {
t.Errorf("getZerovalueStringForSQL() = %v, want %v", gotS, tt.wantS)
}
})
}
}