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

scan.Row(s) doesn't handle structs which implement sql.Scanner correctly #80

Open
sashayakovtseva opened this issue Nov 12, 2024 · 0 comments · May be fixed by #81
Open

scan.Row(s) doesn't handle structs which implement sql.Scanner correctly #80

sashayakovtseva opened this issue Nov 12, 2024 · 0 comments · May be fixed by #81

Comments

@sashayakovtseva
Copy link

sashayakovtseva commented Nov 12, 2024

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 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:

custom scan
std single:  {123}
custom scan
scan.Row:  {123}
custom scan
custom scan
std multi:  [{123} {234}]
custom scan
custom scan
scan.Rows:  [{123} {234}]

Actual result

My scan implementation is called by std package only. My output is:

custom scan
std single:  {123}
scan.Row:  {0}
custom scan
custom scan
std multi:  [{123} {234}]
scan.Rows:  [{0} {0}]
@sashayakovtseva sashayakovtseva linked a pull request Nov 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant