diff --git a/builder/insert_test.go b/builder/insert_test.go index 63fcd2c..6ff5424 100644 --- a/builder/insert_test.go +++ b/builder/insert_test.go @@ -2,6 +2,7 @@ package builder_test import ( "database/sql" + "database/sql/driver" "fmt" "testing" "time" @@ -495,9 +496,29 @@ func TestInsert_Valuer(t *testing.T) { NamedQuery: "INSERT INTO table (email, login) VALUES (:arg_1, :arg_2)", Args: []interface{}{"tech@ulule.com", sql.NullInt64{}}, }, + { + Name: "nil valuer", + Builder: loukoum. + Insert("table"). + Columns("email", "login"). + Values("tech@ulule.com", (*valuer)(nil)), + String: "INSERT INTO table (email, login) VALUES ('tech@ulule.com', NULL)", + Query: "INSERT INTO table (email, login) VALUES ($1, $2)", + NamedQuery: "INSERT INTO table (email, login) VALUES (:arg_1, :arg_2)", + Args: []interface{}{"tech@ulule.com", (*valuer)(nil)}, + }, }) } +type valuer struct{} + +func (valuer) Value() (driver.Value, error) { + return nil, nil +} +func (*valuer) Scan(src interface{}) error { + return nil +} + func TestInsert_Set(t *testing.T) { RunBuilderTests(t, []BuilderTest{ { diff --git a/format/format.go b/format/format.go index fe57bc7..a6e0d52 100644 --- a/format/format.go +++ b/format/format.go @@ -5,6 +5,7 @@ import ( "database/sql/driver" "encoding/hex" "fmt" + "reflect" "strconv" "time" ) @@ -23,6 +24,12 @@ func Value(arg interface{}) string { // nolint: gocyclo case time.Time: return Time(value) case driver.Valuer: + reflectvalue := reflect.ValueOf(value) + if reflectvalue.Kind() == reflect.Ptr && + reflectvalue.IsNil() { + return "NULL" + } + v, err := value.Value() if err != nil { panic("loukoum: was not able to retrieve valuer value")