Skip to content

Commit c1a1466

Browse files
authored
Merge pull request #1440 from mattn/fix-limit-example-stmt-leak
Close prepared statement and fail if limit is not enforced in limit example
2 parents 210e260 + 18f2ec3 commit c1a1466

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

_example/limit/limit.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func bulkInsert(db *sql.DB, query string, args []any) (err error) {
3232
if err != nil {
3333
return
3434
}
35+
defer stmt.Close()
3536

3637
_, err = stmt.Exec(args...)
3738
if err != nil {
@@ -89,11 +90,13 @@ func main() {
8990

9091
query, args = createBulkInsertQuery(num, num)
9192
err = bulkInsert(db, query, args)
92-
if err != nil {
93-
if err != nil {
94-
log.Printf("expect failed since SQLITE_LIMIT_VARIABLE_NUMBER is too small: %v", err)
95-
}
93+
if err == nil {
94+
log.Fatal("expected failure since SQLITE_LIMIT_VARIABLE_NUMBER is too small, but insert succeeded")
95+
}
96+
if !strings.Contains(err.Error(), "too many SQL variables") {
97+
log.Fatalf("expected too many SQL variables error, got: %v", err)
9698
}
99+
log.Printf("expect failed since SQLITE_LIMIT_VARIABLE_NUMBER is too small: %v", err)
97100

98101
bigLimitVariableNumber := 999999
99102
sqlite3conn.SetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, bigLimitVariableNumber)
@@ -104,9 +107,7 @@ func main() {
104107
query, args = createBulkInsertQuery(500, num+num)
105108
err = bulkInsert(db, query, args)
106109
if err != nil {
107-
if err != nil {
108-
log.Fatal(err)
109-
}
110+
log.Fatal(err)
110111
}
111112

112113
log.Println("no error if SQLITE_LIMIT_VARIABLE_NUMBER > 999")

0 commit comments

Comments
 (0)