| 1 |
// Copyright (C) 2019 Yasuhiro Matsumoto <[email protected]>. |
| 2 |
// |
| 3 |
// Use of this source code is governed by an MIT-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
//go:build cgo |
| 7 |
|
| 8 |
package sqlite3 |
| 9 |
|
| 10 |
import ( |
| 11 |
"context" |
| 12 |
"database/sql" |
| 13 |
"path/filepath" |
| 14 |
"reflect" |
| 15 |
"testing" |
| 16 |
) |
| 17 |
|
| 18 |
// TestStmtCacheLRUEviction verifies that when the prepared-statement cache is |
| 19 |
// full, the least-recently-used entry is evicted to make room for a new one. |
| 20 |
// Without eviction, the first N queries to enter the cache would squat on |
| 21 |
// every slot forever and any subsequently-prepared query (even a hot one) |
| 22 |
// would never benefit from caching. |
| 23 |
func TestStmtCacheLRUEviction(t *testing.T) { |
| 24 |
d := SQLiteDriver{} |
| 25 |
conn, err := d.Open(":memory:?_stmt_cache_size=2") |
| 26 |
if err != nil { |
| 27 |
t.Fatal(err) |
| 28 |
} |
| 29 |
defer conn.Close() |
| 30 |
|
| 31 |
c := conn.(*SQLiteConn) |
| 32 |
ctx := context.Background() |
| 33 |
|
| 34 |
prepareAndClose := func(q string) { |
| 35 |
t.Helper() |
| 36 |
stmt, err := c.prepareWithCache(ctx, q) |
| 37 |
if err != nil { |
| 38 |
t.Fatalf("prepareWithCache(%q): %v", q, err) |
| 39 |
} |
| 40 |
if err := stmt.Close(); err != nil { |
| 41 |
t.Fatalf("Close(%q): %v", q, err) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
q1 := "SELECT 1" |
| 46 |
q2 := "SELECT 2" |
| 47 |
q3 := "SELECT 3" |
| 48 |
|
| 49 |
// Fill the cache with q1 and q2. |
| 50 |
prepareAndClose(q1) |
| 51 |
prepareAndClose(q2) |
| 52 |
if got, want := len(c.stmtCache), 2; got != want { |
| 53 |
t.Fatalf("after filling: len(stmtCache) = %d, want %d", got, want) |
| 54 |
} |
| 55 |
if cacheCount(c, q1) != 1 || cacheCount(c, q2) != 1 { |
| 56 |
t.Fatalf("after filling: expected q1 and q2 cached, got %#v", cacheKeys(c)) |
| 57 |
} |
| 58 |
|
| 59 |
// Insert q3. q1 is the oldest entry and should be evicted. |
| 60 |
prepareAndClose(q3) |
| 61 |
if got, want := len(c.stmtCache), 2; got != want { |
| 62 |
t.Fatalf("after q3: len(stmtCache) = %d, want %d", got, want) |
| 63 |
} |
| 64 |
if cacheCount(c, q1) != 0 { |
| 65 |
t.Fatalf("after q3: q1 should have been evicted, cache=%#v", cacheKeys(c)) |
| 66 |
} |
| 67 |
if cacheCount(c, q2) != 1 || cacheCount(c, q3) != 1 { |
| 68 |
t.Fatalf("after q3: expected q2 and q3 cached, got %#v", cacheKeys(c)) |
| 69 |
} |
| 70 |
|
| 71 |
// Touching q2 should make q3 the oldest (the entry at index 0). |
| 72 |
prepareAndClose(q2) |
| 73 |
if len(c.stmtCache) == 0 || c.stmtCache[0].cacheKey != q3 { |
| 74 |
var head string |
| 75 |
if len(c.stmtCache) > 0 { |
| 76 |
head = c.stmtCache[0].cacheKey |
| 77 |
} |
| 78 |
t.Fatalf("after touching q2: expected q3 at stmtCache[0] (LRU), got %q", head) |
| 79 |
} |
| 80 |
|
| 81 |
// Insert q1 again. Now q3 should be evicted (q2 is newer). |
| 82 |
prepareAndClose(q1) |
| 83 |
if cacheCount(c, q3) != 0 { |
| 84 |
t.Fatalf("after reinserting q1: q3 should have been evicted, cache=%#v", cacheKeys(c)) |
| 85 |
} |
| 86 |
if cacheCount(c, q1) != 1 || cacheCount(c, q2) != 1 { |
| 87 |
t.Fatalf("after reinserting q1: expected q1 and q2 cached, got %#v", cacheKeys(c)) |
| 88 |
} |
| 89 |
if got, want := len(c.stmtCache), 2; got != want { |
| 90 |
t.Fatalf("after reinserting q1: len(stmtCache) = %d, want %d", got, want) |
| 91 |
} |
| 92 |
|
| 93 |
// Sanity-check: no dangling entries past len(stmtCache). |
| 94 |
tail := c.stmtCache[:cap(c.stmtCache)] |
| 95 |
for i := len(c.stmtCache); i < len(tail); i++ { |
| 96 |
if tail[i] != nil { |
| 97 |
t.Fatalf("stmtCache tail slot %d = %p, expected nil", i, tail[i]) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// TestStmtCacheReuseReturnsSameHandle verifies that a cached prepare reuses |
| 103 |
// the underlying sqlite3_stmt rather than preparing a fresh one. |
| 104 |
func TestStmtCacheReuseReturnsSameHandle(t *testing.T) { |
| 105 |
d := SQLiteDriver{} |
| 106 |
conn, err := d.Open(":memory:?_stmt_cache_size=4") |
| 107 |
if err != nil { |
| 108 |
t.Fatal(err) |
| 109 |
} |
| 110 |
defer conn.Close() |
| 111 |
|
| 112 |
c := conn.(*SQLiteConn) |
| 113 |
if !c.stmtCacheEnabled { |
| 114 |
t.Skip("statement cache disabled on this SQLite runtime") |
| 115 |
} |
| 116 |
ctx := context.Background() |
| 117 |
|
| 118 |
const q = "SELECT 42" |
| 119 |
stmt1, err := c.prepareWithCache(ctx, q) |
| 120 |
if err != nil { |
| 121 |
t.Fatal(err) |
| 122 |
} |
| 123 |
h1 := stmt1.(*SQLiteStmt).s |
| 124 |
if err := stmt1.Close(); err != nil { |
| 125 |
t.Fatal(err) |
| 126 |
} |
| 127 |
|
| 128 |
stmt2, err := c.prepareWithCache(ctx, q) |
| 129 |
if err != nil { |
| 130 |
t.Fatal(err) |
| 131 |
} |
| 132 |
h2 := stmt2.(*SQLiteStmt).s |
| 133 |
if err := stmt2.Close(); err != nil { |
| 134 |
t.Fatal(err) |
| 135 |
} |
| 136 |
|
| 137 |
if h1 != h2 { |
| 138 |
t.Fatalf("expected cached prepare to reuse sqlite3_stmt handle, got %p vs %p", h1, h2) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// TestStmtCacheSchemaChange verifies that a schema change does not let the |
| 143 |
// cache hand back an expired statement whose captured column metadata still |
| 144 |
// describes the old schema (issue #1447). Both same-connection DDL and DDL |
| 145 |
// issued through a second database handle must invalidate the cache. |
| 146 |
func TestStmtCacheSchemaChange(t *testing.T) { |
| 147 |
fn := filepath.Join(t.TempDir(), "schemachange.db") |
| 148 |
db, err := sql.Open("sqlite3", "file:"+fn+"?_stmt_cache_size=8") |
| 149 |
if err != nil { |
| 150 |
t.Fatal(err) |
| 151 |
} |
| 152 |
defer db.Close() |
| 153 |
db.SetMaxOpenConns(1) |
| 154 |
|
| 155 |
if _, err := db.Exec("CREATE TABLE t (a TEXT)"); err != nil { |
| 156 |
t.Fatal(err) |
| 157 |
} |
| 158 |
if _, err := db.Exec("INSERT INTO t VALUES ('x')"); err != nil { |
| 159 |
t.Fatal(err) |
| 160 |
} |
| 161 |
|
| 162 |
queryCols := func() []string { |
| 163 |
rows, err := db.Query("SELECT * FROM t") |
| 164 |
if err != nil { |
| 165 |
t.Fatal(err) |
| 166 |
} |
| 167 |
defer rows.Close() |
| 168 |
cols, err := rows.Columns() |
| 169 |
if err != nil { |
| 170 |
t.Fatal(err) |
| 171 |
} |
| 172 |
return cols |
| 173 |
} |
| 174 |
|
| 175 |
// Populate the cache. |
| 176 |
if cols := queryCols(); !reflect.DeepEqual(cols, []string{"a"}) { |
| 177 |
t.Fatalf("initial columns: got %v, want [a]", cols) |
| 178 |
} |
| 179 |
|
| 180 |
// Same-connection DDL. |
| 181 |
if _, err := db.Exec("ALTER TABLE t ADD COLUMN b TEXT"); err != nil { |
| 182 |
t.Fatal(err) |
| 183 |
} |
| 184 |
if cols := queryCols(); !reflect.DeepEqual(cols, []string{"a", "b"}) { |
| 185 |
t.Fatalf("columns after same-connection ALTER: got %v, want [a b]", cols) |
| 186 |
} |
| 187 |
|
| 188 |
// DDL through a second database handle (different connection). |
| 189 |
db2, err := sql.Open("sqlite3", "file:"+fn) |
| 190 |
if err != nil { |
| 191 |
t.Fatal(err) |
| 192 |
} |
| 193 |
if _, err := db2.Exec("ALTER TABLE t ADD COLUMN c TEXT"); err != nil { |
| 194 |
db2.Close() |
| 195 |
t.Fatal(err) |
| 196 |
} |
| 197 |
db2.Close() |
| 198 |
if cols := queryCols(); !reflect.DeepEqual(cols, []string{"a", "b", "c"}) { |
| 199 |
t.Fatalf("columns after cross-connection ALTER: got %v, want [a b c]", cols) |
| 200 |
} |
| 201 |
|
| 202 |
// The row data must scan consistently with the new column set. |
| 203 |
var a string |
| 204 |
var b, c any |
| 205 |
if err := db.QueryRow("SELECT * FROM t").Scan(&a, &b, &c); err != nil { |
| 206 |
t.Fatal(err) |
| 207 |
} |
| 208 |
if a != "x" || b != nil || c != nil { |
| 209 |
t.Fatalf("row after ALTERs: got (%q, %v, %v), want (\"x\", <nil>, <nil>)", a, b, c) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
// TestStmtCacheTempSchemaChange verifies that DDL on the temp schema also |
| 214 |
// invalidates cached statements referencing it. |
| 215 |
func TestStmtCacheTempSchemaChange(t *testing.T) { |
| 216 |
db, err := sql.Open("sqlite3", ":memory:?_stmt_cache_size=8") |
| 217 |
if err != nil { |
| 218 |
t.Fatal(err) |
| 219 |
} |
| 220 |
defer db.Close() |
| 221 |
db.SetMaxOpenConns(1) |
| 222 |
|
| 223 |
if _, err := db.Exec("CREATE TEMP TABLE tt (a TEXT)"); err != nil { |
| 224 |
t.Fatal(err) |
| 225 |
} |
| 226 |
rows, err := db.Query("SELECT * FROM tt") |
| 227 |
if err != nil { |
| 228 |
t.Fatal(err) |
| 229 |
} |
| 230 |
rows.Close() // cached |
| 231 |
|
| 232 |
if _, err := db.Exec("ALTER TABLE tt ADD COLUMN b TEXT"); err != nil { |
| 233 |
t.Fatal(err) |
| 234 |
} |
| 235 |
rows, err = db.Query("SELECT * FROM tt") |
| 236 |
if err != nil { |
| 237 |
t.Fatal(err) |
| 238 |
} |
| 239 |
defer rows.Close() |
| 240 |
cols, err := rows.Columns() |
| 241 |
if err != nil { |
| 242 |
t.Fatal(err) |
| 243 |
} |
| 244 |
if want := []string{"a", "b"}; !reflect.DeepEqual(cols, want) { |
| 245 |
t.Fatalf("columns after temp ALTER: got %v, want %v", cols, want) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// TestStmtCacheInTransaction verifies that DDL inside an explicit |
| 250 |
// transaction is honored by queries later in the same transaction (the |
| 251 |
// cache reports a miss there instead of probing the schema). |
| 252 |
func TestStmtCacheInTransaction(t *testing.T) { |
| 253 |
db, err := sql.Open("sqlite3", ":memory:?_stmt_cache_size=8") |
| 254 |
if err != nil { |
| 255 |
t.Fatal(err) |
| 256 |
} |
| 257 |
defer db.Close() |
| 258 |
db.SetMaxOpenConns(1) |
| 259 |
|
| 260 |
if _, err := db.Exec("CREATE TABLE t (a TEXT)"); err != nil { |
| 261 |
t.Fatal(err) |
| 262 |
} |
| 263 |
rows, err := db.Query("SELECT * FROM t") |
| 264 |
if err != nil { |
| 265 |
t.Fatal(err) |
| 266 |
} |
| 267 |
rows.Close() // cached |
| 268 |
|
| 269 |
tx, err := db.Begin() |
| 270 |
if err != nil { |
| 271 |
t.Fatal(err) |
| 272 |
} |
| 273 |
if _, err := tx.Exec("ALTER TABLE t ADD COLUMN b TEXT"); err != nil { |
| 274 |
t.Fatal(err) |
| 275 |
} |
| 276 |
rows, err = tx.Query("SELECT * FROM t") |
| 277 |
if err != nil { |
| 278 |
t.Fatal(err) |
| 279 |
} |
| 280 |
cols, err := rows.Columns() |
| 281 |
rows.Close() |
| 282 |
if err != nil { |
| 283 |
t.Fatal(err) |
| 284 |
} |
| 285 |
if want := []string{"a", "b"}; !reflect.DeepEqual(cols, want) { |
| 286 |
t.Fatalf("columns inside transaction after ALTER: got %v, want %v", cols, want) |
| 287 |
} |
| 288 |
if err := tx.Commit(); err != nil { |
| 289 |
t.Fatal(err) |
| 290 |
} |
| 291 |
|
| 292 |
// After commit the next lookup probes again and must see the change. |
| 293 |
rows, err = db.Query("SELECT * FROM t") |
| 294 |
if err != nil { |
| 295 |
t.Fatal(err) |
| 296 |
} |
| 297 |
defer rows.Close() |
| 298 |
cols, err = rows.Columns() |
| 299 |
if err != nil { |
| 300 |
t.Fatal(err) |
| 301 |
} |
| 302 |
if want := []string{"a", "b"}; !reflect.DeepEqual(cols, want) { |
| 303 |
t.Fatalf("columns after commit: got %v, want %v", cols, want) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
func cacheKeys(c *SQLiteConn) map[string]int { |
| 308 |
out := make(map[string]int) |
| 309 |
for _, s := range c.stmtCache { |
| 310 |
out[s.cacheKey]++ |
| 311 |
} |
| 312 |
return out |
| 313 |
} |
| 314 |
|
| 315 |
func cacheCount(c *SQLiteConn, q string) int { |
| 316 |
n := 0 |
| 317 |
for _, s := range c.stmtCache { |
| 318 |
if s.cacheKey == q { |
| 319 |
n++ |
| 320 |
} |
| 321 |
} |
| 322 |
return n |
| 323 |
} |