Skip to content

Commit 370fec2

Browse files
linxGnujulienschmidt
authored andcommittedNov 17, 2017
statement: Fix conversion of Valuer (go-sql-driver#710)
Updates go-sql-driver#709 Fixes go-sql-driver#706
1 parent b816f3d commit 370fec2

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed
 

‎AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Kamil Dziedzic <kamil at klecza.pl>
4646
Kevin Malachowski <kevin at chowski.com>
4747
Lennart Rudolph <lrudolph at hmc.edu>
4848
Leonardo YongUk Kim <dalinaum at gmail.com>
49+
Linh Tran Tuan <linhduonggnu at gmail.com>
4950
Lion Yang <lion at aosc.xyz>
5051
Luca Looz <luca.looz92 at gmail.com>
5152
Lucas Liu <extrafliu at gmail.com>

‎connection_go18.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,6 @@ func (mc *mysqlConn) startWatcher() {
197197
}
198198

199199
func (mc *mysqlConn) CheckNamedValue(nv *driver.NamedValue) (err error) {
200-
value, err := converter{}.ConvertValue(nv.Value)
201-
if err != nil {
202-
return driver.ErrSkip
203-
}
204-
nv.Value = value
200+
nv.Value, err = converter{}.ConvertValue(nv.Value)
205201
return
206202
}

‎driver_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,55 @@ func TestValuer(t *testing.T) {
529529
})
530530
}
531531

532+
type testValuerWithValidation struct {
533+
value string
534+
}
535+
536+
func (tv testValuerWithValidation) Value() (driver.Value, error) {
537+
if len(tv.value) == 0 {
538+
return nil, fmt.Errorf("Invalid string valuer. Value must not be empty")
539+
}
540+
541+
return tv.value, nil
542+
}
543+
544+
func TestValuerWithValidation(t *testing.T) {
545+
runTests(t, dsn, func(dbt *DBTest) {
546+
in := testValuerWithValidation{"a_value"}
547+
var out string
548+
var rows *sql.Rows
549+
550+
dbt.mustExec("CREATE TABLE testValuer (value VARCHAR(255)) CHARACTER SET utf8")
551+
dbt.mustExec("INSERT INTO testValuer VALUES (?)", in)
552+
553+
rows = dbt.mustQuery("SELECT value FROM testValuer")
554+
defer rows.Close()
555+
556+
if rows.Next() {
557+
rows.Scan(&out)
558+
if in.value != out {
559+
dbt.Errorf("Valuer: %v != %s", in, out)
560+
}
561+
} else {
562+
dbt.Errorf("Valuer: no data")
563+
}
564+
565+
if _, err := dbt.db.Exec("INSERT INTO testValuer VALUES (?)", testValuerWithValidation{""}); err == nil {
566+
dbt.Errorf("Failed to check valuer error")
567+
}
568+
569+
if _, err := dbt.db.Exec("INSERT INTO testValuer VALUES (?)", nil); err != nil {
570+
dbt.Errorf("Failed to check nil")
571+
}
572+
573+
if _, err := dbt.db.Exec("INSERT INTO testValuer VALUES (?)", map[string]bool{}); err == nil {
574+
dbt.Errorf("Failed to check not valuer")
575+
}
576+
577+
dbt.mustExec("DROP TABLE IF EXISTS testValuer")
578+
})
579+
}
580+
532581
type timeTests struct {
533582
dbtype string
534583
tlayout string

‎statement.go

+6
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ func (c converter) ConvertValue(v interface{}) (driver.Value, error) {
137137
return v, nil
138138
}
139139

140+
if v != nil {
141+
if valuer, ok := v.(driver.Valuer); ok {
142+
return valuer.Value()
143+
}
144+
}
145+
140146
rv := reflect.ValueOf(v)
141147
switch rv.Kind() {
142148
case reflect.Ptr:

0 commit comments

Comments
 (0)
Please sign in to comment.