Skip to content

Parameterized arguments inside a custom function are not used #1409

@0xB1a60

Description

@0xB1a60

Hello,

It appears that parameterized arguments passed to Exec inside a ConnectHook callback are not being applied correctly.

Using the example below, I register a custom SQLite function that performs an INSERT using parameter placeholders:

package main

import (
	"database/sql"
	"database/sql/driver"
	"fmt"
	"log"

	"github.com/mattn/go-sqlite3"
)

func init() {
	sql.Register("sqlite3_with_extensions", &sqlite3.SQLiteDriver{
		ConnectHook: func(conn *sqlite3.SQLiteConn) error {

			return conn.RegisterFunc("my_function", func() error {
				_, err := conn.Exec(`INSERT INTO kvs (key, value) VALUES (?, ?)`, []driver.Value{
					500,
					"VALUE",
				})
				if err != nil {
					log.Fatal(err)
				}
				return err
			}, false)
		},
	})
}

func main() {
	db, err := sql.Open("sqlite3_with_extensions", ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	_, err = db.Exec(`
		CREATE TABLE kvs (
			key INTEGER PRIMARY KEY,
			value TEXT
		)
	`)
	if err != nil {
		log.Fatal(err)
	}

	_, err = db.Exec("SELECT my_function()")
	if err != nil {
		log.Fatal(err)
	}

	row := db.QueryRow("SELECT * from kvs")

	var k int
	var v sql.NullString
	if err := row.Scan(&k, &v); err != nil {
		log.Fatal(err)
	}

	fmt.Println(fmt.Sprintf("K: %d, V: %s", k, v.String))
}

The following returns:
K: 1, V:

Expected:
A row is inserted with key: 500 and value: "VALUE"

Actual:
A row is inserted with key: 1 and value: NULL (default values)

It seems that the values supplied in the []driver.Value argument to conn.Exec() are being ignored when the call is made from within ConnectHook.

Is this expected behavior, or is it a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions