2018-06-20 23:50:53 +03:00
|
|
|
// Copyright (C) 2018 The Go-SQLite3 Authors.
|
2018-06-14 12:03:53 +03:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-06-20 23:50:53 +03:00
|
|
|
// +build cgo
|
2018-06-14 12:03:53 +03:00
|
|
|
// +build sqlite_stat4
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStat4(t *testing.T) {
|
|
|
|
tempFilename := TempFilename(t)
|
|
|
|
defer os.Remove(tempFilename)
|
|
|
|
|
|
|
|
db, err := sql.Open("sqlite3", "file:"+tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec("drop table foo")
|
|
|
|
_, err = db.Exec("create table foo (id integer)")
|
|
|
|
_, err = db.Exec("create index idx_id on foo(id)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
tx.Exec("insert into foo(id) VALUES(?)", i)
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
if _, err := db.Exec("ANALYZE"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exists := 0
|
|
|
|
if err := db.QueryRow("select count(type) from sqlite_master where type = 'table' and name = 'sqlite_stat4';").Scan(&exists); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists != 1 {
|
2018-06-24 23:57:57 +03:00
|
|
|
t.Fatal("Failed to enable STAT4")
|
2018-06-14 12:03:53 +03:00
|
|
|
}
|
|
|
|
}
|