mattn / mattn/go-sqlite3

Conn.PrepareContext doesn't support multiple statements (separated by semi-colon)

Open
#933 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
9.2k
Forks
1.2k
Avg merge
19m
Merged PRs (30d)
4

Description

Hello, I noticed that sqlite3-go does not allow multiple statements separated by a semi-colon when directly preparing statements against a single Conn:

package main

import (
	"context"
	"database/sql"
	"fmt"

	// _ "modernc.org/sqlite"
	_ "github.com/mattn/go-sqlite3"
)

// You can only run a single statement inside a stmt on a conn with go-sqlite3.
func main() {
	// note: use "sqlite" for testing modernc.org/sqlite and "sqlite3" for go-sqlite3
	db, _ := sql.Open("sqlite3", ":memory:")
	conn, _ := db.Conn(context.Background())
	// One statement executes and the second is ignored; prints "4"
	stmt, _ := conn.PrepareContext(context.Background(), `select 2 + 2; select 4 + 4;`)
	rows, _ := stmt.Query()
	for rows.Next() {
		var result string
		rows.Scan(&result)
		fmt.Println(result)
	}

	// Both statments execute and the result of the last one is returned; prints "8"
	rows, _ = conn.QueryContext(context.Background(), `select 2 + 2; select 4 + 4;`)
	for rows.Next() {
		var result string
		rows.Scan(&result)
		fmt.Println(result)
	}
}

When stmt.Query runs, only the first statement executes, which is inconsistent with the behavior of conn.QueryContext and conn.Exec. I found an old issue about this behavior with Conn.Exec which was fixed in 2014.

Is this something that could be changed/fixed, or is it an intentional design choice? For a comparison, running this code against modernc.org/sqlite returns the same results (8 and 8) in both cases.

Thank you!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example with Conn.PrepareContext and stmt.Query, then compare it with Conn.QueryContext and Conn.Exec, which already process multiple statements. Trace the PrepareContext path and determine whether the prepared statement should execute all semicolon-separated statements; done means stmt.Query matches the connection-level behavior and returns the final result.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sqlite
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.