mo icon indicating copy to clipboard operation
mo copied to clipboard

Failed scanning row for mysql with Option

Open yflau opened this issue 2 years ago • 3 comments

For example

CREATE TABLE t (optional VARCHAR(64));

rows, err := db.Query("SELECT optional FROM t  LIMIT 1?")
for rows.Next() {
  var o mo.Option[string]
  if err := rows.Scan(&o); err != nil {
	  t.Errorf("Failed scanning row: %s", err.Error())
	  continue
  }
}

then will got Failed scanning row: sql: Scan error on column index 1, name "optional": failed to scan Option[T], and the reason is that type of arg src of Scan is []uint8, and failed here when assert type is string, refer to sql.NullString, we can use the function sql.convertAssign to replace the driver.DefaultParameterConverter.ConvertValue to resolve it, but the convertAssign is private, and maybe won't be public(refer to https://github.com/golang/go/issues/24258), so we can copy out the convertAssign and make some modification or write a new convert function? what do you think? Or have some other solution?

yflau avatar Apr 04 '23 09:04 yflau

Can you test with v1.9.0 please ?

samber avatar Sep 25 '23 13:09 samber

@samber I just tried with both the v1.9.0 and the v1.11.0, both of them have the same issue.

gsiffert avatar Jan 16 '24 14:01 gsiffert

Similar issue here as well. We are about to fork this repository and make use of the sql.convertAssign function.

graineri avatar Apr 10 '24 23:04 graineri

-> v1.13.0

samber avatar Jul 16 '24 09:07 samber