| 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 sqlite_vtable || vtable |
| 7 |
// +build sqlite_vtable vtable |
| 8 |
|
| 9 |
package sqlite3 |
| 10 |
|
| 11 |
import ( |
| 12 |
"database/sql" |
| 13 |
"errors" |
| 14 |
"fmt" |
| 15 |
"os" |
| 16 |
"reflect" |
| 17 |
"strings" |
| 18 |
"testing" |
| 19 |
) |
| 20 |
|
| 21 |
type testModule struct { |
| 22 |
t *testing.T |
| 23 |
intarray []int |
| 24 |
} |
| 25 |
|
| 26 |
type testVTab struct { |
| 27 |
intarray []int |
| 28 |
} |
| 29 |
|
| 30 |
type testVTabCursor struct { |
| 31 |
vTab *testVTab |
| 32 |
index int |
| 33 |
} |
| 34 |
|
| 35 |
func (m testModule) Create(c *SQLiteConn, args []string) (VTab, error) { |
| 36 |
if len(args) != 6 { |
| 37 |
m.t.Fatal("six arguments expected") |
| 38 |
} |
| 39 |
if args[0] != "test" { |
| 40 |
m.t.Fatal("module name") |
| 41 |
} |
| 42 |
if args[1] != "main" { |
| 43 |
m.t.Fatal("db name") |
| 44 |
} |
| 45 |
if args[2] != "vtab" { |
| 46 |
m.t.Fatal("table name") |
| 47 |
} |
| 48 |
if args[3] != "'1'" { |
| 49 |
m.t.Fatal("first arg") |
| 50 |
} |
| 51 |
if args[4] != "2" { |
| 52 |
m.t.Fatal("second arg") |
| 53 |
} |
| 54 |
if args[5] != "three" { |
| 55 |
m.t.Fatal("third argsecond arg") |
| 56 |
} |
| 57 |
err := c.DeclareVTab("CREATE TABLE x(test TEXT)") |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
return &testVTab{m.intarray}, nil |
| 62 |
} |
| 63 |
|
| 64 |
func (m testModule) Connect(c *SQLiteConn, args []string) (VTab, error) { |
| 65 |
return m.Create(c, args) |
| 66 |
} |
| 67 |
|
| 68 |
func (m testModule) DestroyModule() {} |
| 69 |
|
| 70 |
func (v *testVTab) BestIndex(cst []InfoConstraint, ob []InfoOrderBy) (*IndexResult, error) { |
| 71 |
used := make([]bool, 0, len(cst)) |
| 72 |
for range cst { |
| 73 |
used = append(used, false) |
| 74 |
} |
| 75 |
return &IndexResult{ |
| 76 |
Used: used, |
| 77 |
IdxNum: 0, |
| 78 |
IdxStr: "test-index", |
| 79 |
AlreadyOrdered: true, |
| 80 |
EstimatedCost: 100, |
| 81 |
EstimatedRows: 200, |
| 82 |
}, nil |
| 83 |
} |
| 84 |
|
| 85 |
func (v *testVTab) Disconnect() error { |
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
func (v *testVTab) Destroy() error { |
| 90 |
return nil |
| 91 |
} |
| 92 |
|
| 93 |
func (v *testVTab) Open() (VTabCursor, error) { |
| 94 |
return &testVTabCursor{v, 0}, nil |
| 95 |
} |
| 96 |
|
| 97 |
func (vc *testVTabCursor) Close() error { |
| 98 |
return nil |
| 99 |
} |
| 100 |
|
| 101 |
func (vc *testVTabCursor) Filter(idxNum int, idxStr string, vals []any) error { |
| 102 |
vc.index = 0 |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
func (vc *testVTabCursor) Next() error { |
| 107 |
vc.index++ |
| 108 |
return nil |
| 109 |
} |
| 110 |
|
| 111 |
func (vc *testVTabCursor) EOF() bool { |
| 112 |
return vc.index >= len(vc.vTab.intarray) |
| 113 |
} |
| 114 |
|
| 115 |
func (vc *testVTabCursor) Column(c *SQLiteContext, col int) error { |
| 116 |
if col != 0 { |
| 117 |
return fmt.Errorf("column index out of bounds: %d", col) |
| 118 |
} |
| 119 |
c.ResultInt(vc.vTab.intarray[vc.index]) |
| 120 |
return nil |
| 121 |
} |
| 122 |
|
| 123 |
func (vc *testVTabCursor) Rowid() (int64, error) { |
| 124 |
return int64(vc.index), nil |
| 125 |
} |
| 126 |
|
| 127 |
func TestCreateModule(t *testing.T) { |
| 128 |
tempFilename := TempFilename(t) |
| 129 |
defer os.Remove(tempFilename) |
| 130 |
intarray := []int{1, 2, 3} |
| 131 |
sql.Register("sqlite3_TestCreateModule", &SQLiteDriver{ |
| 132 |
ConnectHook: func(conn *SQLiteConn) error { |
| 133 |
return conn.CreateModule("test", testModule{t, intarray}) |
| 134 |
}, |
| 135 |
}) |
| 136 |
db, err := sql.Open("sqlite3_TestCreateModule", tempFilename) |
| 137 |
if err != nil { |
| 138 |
t.Fatalf("could not open db: %v", err) |
| 139 |
} |
| 140 |
_, err = db.Exec("CREATE VIRTUAL TABLE vtab USING test('1', 2, three)") |
| 141 |
if err != nil { |
| 142 |
t.Fatalf("could not create vtable: %v", err) |
| 143 |
} |
| 144 |
|
| 145 |
var i, value int |
| 146 |
rows, err := db.Query("SELECT rowid, * FROM vtab WHERE test = '3'") |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("couldn't select from virtual table: %v", err) |
| 149 |
} |
| 150 |
for rows.Next() { |
| 151 |
rows.Scan(&i, &value) |
| 152 |
if intarray[i] != value { |
| 153 |
t.Fatalf("want %v but %v", intarray[i], value) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
_, err = db.Exec("DROP TABLE vtab") |
| 158 |
if err != nil { |
| 159 |
t.Fatalf("couldn't drop virtual table: %v", err) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func TestVUpdate(t *testing.T) { |
| 164 |
tempFilename := TempFilename(t) |
| 165 |
defer os.Remove(tempFilename) |
| 166 |
|
| 167 |
// create module |
| 168 |
updateMod := &vtabUpdateModule{t, make(map[string]*vtabUpdateTable)} |
| 169 |
|
| 170 |
// register module |
| 171 |
sql.Register("sqlite3_TestVUpdate", &SQLiteDriver{ |
| 172 |
ConnectHook: func(conn *SQLiteConn) error { |
| 173 |
return conn.CreateModule("updatetest", updateMod) |
| 174 |
}, |
| 175 |
}) |
| 176 |
|
| 177 |
// connect |
| 178 |
db, err := sql.Open("sqlite3_TestVUpdate", tempFilename) |
| 179 |
if err != nil { |
| 180 |
t.Fatalf("could not open db: %v", err) |
| 181 |
} |
| 182 |
|
| 183 |
// create test table |
| 184 |
_, err = db.Exec(`CREATE VIRTUAL TABLE vt USING updatetest(f1 integer, f2 text, f3 text)`) |
| 185 |
if err != nil { |
| 186 |
t.Fatalf("could not create updatetest vtable vt, got: %v", err) |
| 187 |
} |
| 188 |
|
| 189 |
// check that table is defined properly |
| 190 |
if len(updateMod.tables) != 1 { |
| 191 |
t.Fatalf("expected exactly 1 table to exist, got: %d", len(updateMod.tables)) |
| 192 |
} |
| 193 |
if _, ok := updateMod.tables["vt"]; !ok { |
| 194 |
t.Fatalf("expected table `vt` to exist in tables") |
| 195 |
} |
| 196 |
|
| 197 |
// check nothing in updatetest |
| 198 |
rows, err := db.Query(`select * from vt`) |
| 199 |
if err != nil { |
| 200 |
t.Fatalf("could not query vt, got: %v", err) |
| 201 |
} |
| 202 |
i, err := getRowCount(rows) |
| 203 |
if err != nil { |
| 204 |
t.Fatalf("expected no error, got: %v", err) |
| 205 |
} |
| 206 |
if i != 0 { |
| 207 |
t.Fatalf("expected no rows in vt, got: %d", i) |
| 208 |
} |
| 209 |
|
| 210 |
_, err = db.Exec(`delete from vt where f1 = 'yes'`) |
| 211 |
if err != nil { |
| 212 |
t.Fatalf("expected error on delete, got nil") |
| 213 |
} |
| 214 |
|
| 215 |
// test bad column name |
| 216 |
_, err = db.Exec(`insert into vt (f4) values('a')`) |
| 217 |
if err == nil { |
| 218 |
t.Fatalf("expected error on insert, got nil") |
| 219 |
} |
| 220 |
|
| 221 |
// insert to vt |
| 222 |
res, err := db.Exec(`insert into vt (f1, f2, f3) values (115, 'b', 'c'), (116, 'd', 'e')`) |
| 223 |
if err != nil { |
| 224 |
t.Fatalf("expected no error on insert, got: %v", err) |
| 225 |
} |
| 226 |
n, err := res.RowsAffected() |
| 227 |
if err != nil { |
| 228 |
t.Fatalf("expected no error, got: %v", err) |
| 229 |
} |
| 230 |
if n != 2 { |
| 231 |
t.Fatalf("expected 1 row affected, got: %d", n) |
| 232 |
} |
| 233 |
|
| 234 |
// check vt table |
| 235 |
vt := updateMod.tables["vt"] |
| 236 |
if len(vt.data) != 2 { |
| 237 |
t.Fatalf("expected table vt to have exactly 2 rows, got: %d", len(vt.data)) |
| 238 |
} |
| 239 |
if !reflect.DeepEqual(vt.data[0], []any{int64(115), "b", "c"}) { |
| 240 |
t.Fatalf("expected table vt entry 0 to be [115 b c], instead: %v", vt.data[0]) |
| 241 |
} |
| 242 |
if !reflect.DeepEqual(vt.data[1], []any{int64(116), "d", "e"}) { |
| 243 |
t.Fatalf("expected table vt entry 1 to be [116 d e], instead: %v", vt.data[1]) |
| 244 |
} |
| 245 |
|
| 246 |
// query vt |
| 247 |
var f1 int |
| 248 |
var f2, f3 string |
| 249 |
err = db.QueryRow(`select * from vt where f1 = 115`).Scan(&f1, &f2, &f3) |
| 250 |
if err != nil { |
| 251 |
t.Fatalf("expected no error on vt query, got: %v", err) |
| 252 |
} |
| 253 |
|
| 254 |
// check column values |
| 255 |
if f1 != 115 || f2 != "b" || f3 != "c" { |
| 256 |
t.Errorf("expected f1==115, f2==b, f3==c, got: %d, %q, %q", f1, f2, f3) |
| 257 |
} |
| 258 |
|
| 259 |
// update vt |
| 260 |
res, err = db.Exec(`update vt set f1=117, f2='f' where f3='e'`) |
| 261 |
if err != nil { |
| 262 |
t.Fatalf("expected no error, got: %v", err) |
| 263 |
} |
| 264 |
n, err = res.RowsAffected() |
| 265 |
if err != nil { |
| 266 |
t.Fatalf("expected no error, got: %v", err) |
| 267 |
} |
| 268 |
if n != 1 { |
| 269 |
t.Fatalf("expected exactly one row updated, got: %d", n) |
| 270 |
} |
| 271 |
|
| 272 |
// check vt table |
| 273 |
if len(vt.data) != 2 { |
| 274 |
t.Fatalf("expected table vt to have exactly 2 rows, got: %d", len(vt.data)) |
| 275 |
} |
| 276 |
if !reflect.DeepEqual(vt.data[0], []any{int64(115), "b", "c"}) { |
| 277 |
t.Fatalf("expected table vt entry 0 to be [115 b c], instead: %v", vt.data[0]) |
| 278 |
} |
| 279 |
if !reflect.DeepEqual(vt.data[1], []any{int64(117), "f", "e"}) { |
| 280 |
t.Fatalf("expected table vt entry 1 to be [117 f e], instead: %v", vt.data[1]) |
| 281 |
} |
| 282 |
|
| 283 |
// a rowid-changing update cannot be expressed via VTabUpdater and |
| 284 |
// must be rejected instead of updating the wrong row |
| 285 |
_, err = db.Exec(`update vt set rowid = rowid + 10 where f1 = 117`) |
| 286 |
if err == nil { |
| 287 |
t.Fatalf("expected error on rowid-changing update, got nil") |
| 288 |
} |
| 289 |
if !strings.Contains(err.Error(), "does not support changing the rowid") { |
| 290 |
t.Fatalf("unexpected error on rowid-changing update: %v", err) |
| 291 |
} |
| 292 |
if !reflect.DeepEqual(vt.data[1], []any{int64(117), "f", "e"}) { |
| 293 |
t.Fatalf("expected table vt entry 1 to be unchanged, instead: %v", vt.data[1]) |
| 294 |
} |
| 295 |
|
| 296 |
// delete from vt |
| 297 |
res, err = db.Exec(`delete from vt where f1 = 117`) |
| 298 |
if err != nil { |
| 299 |
t.Fatalf("expected no error, got: %v", err) |
| 300 |
} |
| 301 |
n, err = res.RowsAffected() |
| 302 |
if err != nil { |
| 303 |
t.Fatalf("expected no error, got: %v", err) |
| 304 |
} |
| 305 |
if n != 1 { |
| 306 |
t.Fatalf("expected exactly one row deleted, got: %d", n) |
| 307 |
} |
| 308 |
|
| 309 |
// check vt table |
| 310 |
if len(vt.data) != 1 { |
| 311 |
t.Fatalf("expected table vt to have exactly 1 row, got: %d", len(vt.data)) |
| 312 |
} |
| 313 |
if !reflect.DeepEqual(vt.data[0], []any{int64(115), "b", "c"}) { |
| 314 |
t.Fatalf("expected table vt entry 0 to be [115 b c], instead: %v", vt.data[0]) |
| 315 |
} |
| 316 |
|
| 317 |
// check updatetest has 1 result |
| 318 |
rows, err = db.Query(`select * from vt`) |
| 319 |
if err != nil { |
| 320 |
t.Fatalf("could not query vt, got: %v", err) |
| 321 |
} |
| 322 |
i, err = getRowCount(rows) |
| 323 |
if err != nil { |
| 324 |
t.Fatalf("expected no error, got: %v", err) |
| 325 |
} |
| 326 |
if i != 1 { |
| 327 |
t.Fatalf("expected 1 row in vt, got: %d", i) |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
func getRowCount(rows *sql.Rows) (int, error) { |
| 332 |
var i int |
| 333 |
for rows.Next() { |
| 334 |
i++ |
| 335 |
} |
| 336 |
return i, nil |
| 337 |
} |
| 338 |
|
| 339 |
type vtabUpdateModule struct { |
| 340 |
t *testing.T |
| 341 |
tables map[string]*vtabUpdateTable |
| 342 |
} |
| 343 |
|
| 344 |
func (m *vtabUpdateModule) Create(c *SQLiteConn, args []string) (VTab, error) { |
| 345 |
if len(args) < 2 { |
| 346 |
return nil, errors.New("must declare at least one column") |
| 347 |
} |
| 348 |
|
| 349 |
// get database name, table name, and column declarations ... |
| 350 |
dbname, tname, decls := args[1], args[2], args[3:] |
| 351 |
|
| 352 |
// extract column names + types from parameters declarations |
| 353 |
cols, typs := make([]string, len(decls)), make([]string, len(decls)) |
| 354 |
for i := 0; i < len(decls); i++ { |
| 355 |
n, typ := decls[i], "" |
| 356 |
if j := strings.IndexAny(n, " \t\n"); j != -1 { |
| 357 |
typ, n = strings.TrimSpace(n[j+1:]), n[:j] |
| 358 |
} |
| 359 |
cols[i], typs[i] = n, typ |
| 360 |
} |
| 361 |
|
| 362 |
// declare table |
| 363 |
err := c.DeclareVTab(fmt.Sprintf(`CREATE TABLE "%s"."%s" (%s)`, dbname, tname, strings.Join(decls, ","))) |
| 364 |
if err != nil { |
| 365 |
return nil, err |
| 366 |
} |
| 367 |
|
| 368 |
// create table |
| 369 |
vtab := &vtabUpdateTable{m.t, dbname, tname, cols, typs, make([][]any, 0)} |
| 370 |
m.tables[tname] = vtab |
| 371 |
return vtab, nil |
| 372 |
} |
| 373 |
|
| 374 |
func (m *vtabUpdateModule) Connect(c *SQLiteConn, args []string) (VTab, error) { |
| 375 |
return m.Create(c, args) |
| 376 |
} |
| 377 |
|
| 378 |
func (m *vtabUpdateModule) DestroyModule() {} |
| 379 |
|
| 380 |
type vtabUpdateTable struct { |
| 381 |
t *testing.T |
| 382 |
db string |
| 383 |
name string |
| 384 |
cols []string |
| 385 |
typs []string |
| 386 |
data [][]any |
| 387 |
} |
| 388 |
|
| 389 |
func (t *vtabUpdateTable) Open() (VTabCursor, error) { |
| 390 |
return &vtabUpdateCursor{t, 0}, nil |
| 391 |
} |
| 392 |
|
| 393 |
func (t *vtabUpdateTable) BestIndex(cst []InfoConstraint, ob []InfoOrderBy) (*IndexResult, error) { |
| 394 |
return &IndexResult{Used: make([]bool, len(cst))}, nil |
| 395 |
} |
| 396 |
|
| 397 |
func (t *vtabUpdateTable) Disconnect() error { |
| 398 |
return nil |
| 399 |
} |
| 400 |
|
| 401 |
func (t *vtabUpdateTable) Destroy() error { |
| 402 |
return nil |
| 403 |
} |
| 404 |
|
| 405 |
func (t *vtabUpdateTable) Insert(id any, vals []any) (int64, error) { |
| 406 |
var i int64 |
| 407 |
if id == nil { |
| 408 |
i, t.data = int64(len(t.data)), append(t.data, vals) |
| 409 |
return i, nil |
| 410 |
} |
| 411 |
|
| 412 |
var ok bool |
| 413 |
i, ok = id.(int64) |
| 414 |
if !ok { |
| 415 |
return 0, fmt.Errorf("id is invalid type: %T", id) |
| 416 |
} |
| 417 |
|
| 418 |
t.data[i] = vals |
| 419 |
|
| 420 |
return i, nil |
| 421 |
} |
| 422 |
|
| 423 |
func (t *vtabUpdateTable) Update(id any, vals []any) error { |
| 424 |
i, ok := id.(int64) |
| 425 |
if !ok { |
| 426 |
return fmt.Errorf("id is invalid type: %T", id) |
| 427 |
} |
| 428 |
|
| 429 |
if int(i) >= len(t.data) || i < 0 { |
| 430 |
return fmt.Errorf("invalid row id %d", i) |
| 431 |
} |
| 432 |
|
| 433 |
t.data[int(i)] = vals |
| 434 |
|
| 435 |
return nil |
| 436 |
} |
| 437 |
|
| 438 |
func (t *vtabUpdateTable) Delete(id any) error { |
| 439 |
i, ok := id.(int64) |
| 440 |
if !ok { |
| 441 |
return fmt.Errorf("id is invalid type: %T", id) |
| 442 |
} |
| 443 |
|
| 444 |
if int(i) >= len(t.data) || i < 0 { |
| 445 |
return fmt.Errorf("invalid row id %d", i) |
| 446 |
} |
| 447 |
|
| 448 |
t.data = append(t.data[:i], t.data[i+1:]...) |
| 449 |
|
| 450 |
return nil |
| 451 |
} |
| 452 |
|
| 453 |
type vtabUpdateCursor struct { |
| 454 |
t *vtabUpdateTable |
| 455 |
i int |
| 456 |
} |
| 457 |
|
| 458 |
func (c *vtabUpdateCursor) Column(ctxt *SQLiteContext, col int) error { |
| 459 |
switch x := c.t.data[c.i][col].(type) { |
| 460 |
case []byte: |
| 461 |
ctxt.ResultBlob(x) |
| 462 |
case bool: |
| 463 |
ctxt.ResultBool(x) |
| 464 |
case float64: |
| 465 |
ctxt.ResultDouble(x) |
| 466 |
case int: |
| 467 |
ctxt.ResultInt(x) |
| 468 |
case int64: |
| 469 |
ctxt.ResultInt64(x) |
| 470 |
case nil: |
| 471 |
ctxt.ResultNull() |
| 472 |
case string: |
| 473 |
ctxt.ResultText(x) |
| 474 |
default: |
| 475 |
ctxt.ResultText(fmt.Sprintf("%v", x)) |
| 476 |
} |
| 477 |
|
| 478 |
return nil |
| 479 |
} |
| 480 |
|
| 481 |
func (c *vtabUpdateCursor) Filter(ixNum int, ixName string, vals []any) error { |
| 482 |
return nil |
| 483 |
} |
| 484 |
|
| 485 |
func (c *vtabUpdateCursor) Next() error { |
| 486 |
c.i++ |
| 487 |
return nil |
| 488 |
} |
| 489 |
|
| 490 |
func (c *vtabUpdateCursor) EOF() bool { |
| 491 |
return c.i >= len(c.t.data) |
| 492 |
} |
| 493 |
|
| 494 |
func (c *vtabUpdateCursor) Rowid() (int64, error) { |
| 495 |
return int64(c.i), nil |
| 496 |
} |
| 497 |
|
| 498 |
func (c *vtabUpdateCursor) Close() error { |
| 499 |
return nil |
| 500 |
} |
| 501 |
|
| 502 |
type testModuleEponymousOnly struct { |
| 503 |
t *testing.T |
| 504 |
intarray []int |
| 505 |
} |
| 506 |
|
| 507 |
type testVTabEponymousOnly struct { |
| 508 |
intarray []int |
| 509 |
} |
| 510 |
|
| 511 |
type testVTabCursorEponymousOnly struct { |
| 512 |
vTab *testVTabEponymousOnly |
| 513 |
index int |
| 514 |
} |
| 515 |
|
| 516 |
func (m testModuleEponymousOnly) EponymousOnlyModule() {} |
| 517 |
|
| 518 |
func (m testModuleEponymousOnly) Create(c *SQLiteConn, args []string) (VTab, error) { |
| 519 |
err := c.DeclareVTab("CREATE TABLE x(test INT)") |
| 520 |
if err != nil { |
| 521 |
return nil, err |
| 522 |
} |
| 523 |
return &testVTabEponymousOnly{m.intarray}, nil |
| 524 |
} |
| 525 |
|
| 526 |
func (m testModuleEponymousOnly) Connect(c *SQLiteConn, args []string) (VTab, error) { |
| 527 |
return m.Create(c, args) |
| 528 |
} |
| 529 |
|
| 530 |
func (m testModuleEponymousOnly) DestroyModule() {} |
| 531 |
|
| 532 |
func (v *testVTabEponymousOnly) BestIndex(cst []InfoConstraint, ob []InfoOrderBy) (*IndexResult, error) { |
| 533 |
used := make([]bool, 0, len(cst)) |
| 534 |
for range cst { |
| 535 |
used = append(used, false) |
| 536 |
} |
| 537 |
return &IndexResult{ |
| 538 |
Used: used, |
| 539 |
IdxNum: 0, |
| 540 |
IdxStr: "test-index", |
| 541 |
AlreadyOrdered: true, |
| 542 |
EstimatedCost: 100, |
| 543 |
EstimatedRows: 200, |
| 544 |
}, nil |
| 545 |
} |
| 546 |
|
| 547 |
func (v *testVTabEponymousOnly) Disconnect() error { |
| 548 |
return nil |
| 549 |
} |
| 550 |
|
| 551 |
func (v *testVTabEponymousOnly) Destroy() error { |
| 552 |
return nil |
| 553 |
} |
| 554 |
|
| 555 |
func (v *testVTabEponymousOnly) Open() (VTabCursor, error) { |
| 556 |
return &testVTabCursorEponymousOnly{v, 0}, nil |
| 557 |
} |
| 558 |
|
| 559 |
func (vc *testVTabCursorEponymousOnly) Close() error { |
| 560 |
return nil |
| 561 |
} |
| 562 |
|
| 563 |
func (vc *testVTabCursorEponymousOnly) Filter(idxNum int, idxStr string, vals []any) error { |
| 564 |
vc.index = 0 |
| 565 |
return nil |
| 566 |
} |
| 567 |
|
| 568 |
func (vc *testVTabCursorEponymousOnly) Next() error { |
| 569 |
vc.index++ |
| 570 |
return nil |
| 571 |
} |
| 572 |
|
| 573 |
func (vc *testVTabCursorEponymousOnly) EOF() bool { |
| 574 |
return vc.index >= len(vc.vTab.intarray) |
| 575 |
} |
| 576 |
|
| 577 |
func (vc *testVTabCursorEponymousOnly) Column(c *SQLiteContext, col int) error { |
| 578 |
if col != 0 { |
| 579 |
return fmt.Errorf("column index out of bounds: %d", col) |
| 580 |
} |
| 581 |
c.ResultInt(vc.vTab.intarray[vc.index]) |
| 582 |
return nil |
| 583 |
} |
| 584 |
|
| 585 |
func (vc *testVTabCursorEponymousOnly) Rowid() (int64, error) { |
| 586 |
return int64(vc.index), nil |
| 587 |
} |
| 588 |
|
| 589 |
func TestCreateModuleEponymousOnly(t *testing.T) { |
| 590 |
tempFilename := TempFilename(t) |
| 591 |
defer os.Remove(tempFilename) |
| 592 |
intarray := []int{1, 2, 3} |
| 593 |
sql.Register("sqlite3_TestCreateModuleEponymousOnly", &SQLiteDriver{ |
| 594 |
ConnectHook: func(conn *SQLiteConn) error { |
| 595 |
return conn.CreateModule("test", testModuleEponymousOnly{t, intarray}) |
| 596 |
}, |
| 597 |
}) |
| 598 |
db, err := sql.Open("sqlite3_TestCreateModuleEponymousOnly", tempFilename) |
| 599 |
if err != nil { |
| 600 |
t.Fatalf("could not open db: %v", err) |
| 601 |
} |
| 602 |
|
| 603 |
var i, value int |
| 604 |
rows, err := db.Query("SELECT rowid, * FROM test") |
| 605 |
if err != nil { |
| 606 |
t.Fatalf("couldn't select from virtual table: %v", err) |
| 607 |
} |
| 608 |
for rows.Next() { |
| 609 |
err := rows.Scan(&i, &value) |
| 610 |
if err != nil { |
| 611 |
t.Fatal(err) |
| 612 |
} |
| 613 |
if intarray[i] != value { |
| 614 |
t.Fatalf("want %v but %v", intarray[i], value) |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
_, err = db.Exec("DROP TABLE test") |
| 619 |
if err != nil { |
| 620 |
t.Logf("couldn't drop virtual table: %v", err) |
| 621 |
} |
| 622 |
} |