You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using scan.Row(s) (v2.5.0) with my custom types that implement sql.Scanner and I found that this package doesn't scan data correctly when sql.Scanner is a struct. Here's an example:
package main
import (
"context"
"database/sql"
"fmt"
"log"
"github.com/blockloop/scan/v2"
_ "github.com/go-sql-driver/mysql"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func main() {
dsn, cleanup, err := testDatabaseDSN()
if err != nil {
log.Fatal(err)
}
defer cleanup()
db, err := sql.Open("mysql", dsn)
if err != nil {
log.Println(err)
return
}
const createTableQuery = `
CREATE TABLE test
(
id int PRIMARY KEY AUTO_INCREMENT,
data int NOT NULL
);
INSERT INTO test (id, data)
VALUES (1, 123), (2, 234);
`
const (
selectOneQuery = `SELECT data FROM test WHERE id = 1`
selectAllQuery = `SELECT data FROM test`
)
if _, err := db.Exec(createTableQuery); err != nil {
log.Println(err)
return
}
// case 1. std single row works
{
var data customScanner
if err := db.QueryRow(selectOneQuery).Scan(&data); err != nil {
log.Println(err)
return
}
log.Println("std single: ", data)
}
// case 2. scan.Row doesn't
{
var data customScanner
rows, err := db.Query(selectOneQuery)
if err != nil {
log.Println(err)
return
}
if err := scan.Row(&data, rows); err != nil {
log.Println(err)
return
}
log.Println("scan.Row: ", data)
}
// case 3. std multiple rows works
{
rows, err := db.Query(selectAllQuery)
if err != nil {
log.Println(err)
return
}
defer rows.Close()
data := make([]customScanner, 2)
for i := 0; rows.Next(); i++ {
if err := rows.Scan(&data[i]); err != nil {
log.Println(err)
return
}
}
log.Println("std multi: ", data)
}
// case 4. scan.Rows doesn't work
{
var data []customScanner
rows, err := db.Query(selectAllQuery)
if err != nil {
log.Println(err)
return
}
if err := scan.Rows(&data, rows); err != nil {
log.Println(err)
return
}
log.Println("scan.Rows: ", data)
}
}
type customScanner struct {
v int64
}
func (c *customScanner) Scan(src any) error {
log.Println("custom scan")
switch v := src.(type) {
case int64:
*c = customScanner{v: v}
case nil:
return nil
default:
return fmt.Errorf("unsupported type %T", src)
}
return nil
}
func testDatabaseDSN() (string, func(), error) {
mysqlContainer, err := testcontainers.GenericContainer(context.Background(),
testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "percona/percona-server:8.0.36-28.1-multi",
ExposedPorts: []string{"3306/tcp"},
WaitingFor: wait.ForAll(
wait.ForListeningPort("3306/tcp"),
wait.ForLog("mysqld: ready for connections"),
),
Env: map[string]string{
"MYSQL_DATABASE": "test",
"MYSQL_ALLOW_EMPTY_PASSWORD": "yes",
},
},
Started: true,
})
if err != nil {
return "", nil, err
}
cleanup := func() {
_ = mysqlContainer.Terminate(context.Background())
}
host, err := mysqlContainer.Host(context.Background())
if err != nil {
cleanup()
return "", nil, err
}
port, err := mysqlContainer.MappedPort(context.Background(), "3306")
if err != nil {
cleanup()
return "", nil, err
}
dsn := fmt.Sprintf("root@(%s:%d)/test?tls=false&multiStatements=true", host, port.Int())
return dsn, cleanup, nil
}
I figured out that in this point my type is converted to *any thus it is not treated as sql.Scanner.
Expected result
I expect my scan implementation to be called by std and scan packages:
Bug description
Hello!
I am using scan.Row(s) (v2.5.0) with my custom types that implement
sql.Scanner
and I found that this package doesn't scan data correctly whensql.Scanner
is a struct. Here's an example:I figured out that in this point my type is converted to
*any
thus it is not treated assql.Scanner
.Expected result
I expect my scan implementation to be called by std and scan packages:
Actual result
My scan implementation is called by std package only. My output is:
The text was updated successfully, but these errors were encountered: