2019-11-18 12:03:31 +03:00
|
|
|
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
2015-02-24 05:27:09 +03:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2024-01-25 16:36:27 +03:00
|
|
|
//go:build cgo
|
2019-11-18 12:03:31 +03:00
|
|
|
// +build cgo
|
|
|
|
|
2015-02-24 05:27:09 +03:00
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFTS3(t *testing.T) {
|
2015-06-05 17:02:14 +03:00
|
|
|
tempFilename := TempFilename(t)
|
2015-11-03 15:52:28 +03:00
|
|
|
defer os.Remove(tempFilename)
|
2015-02-24 05:27:09 +03:00
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec("DROP TABLE foo")
|
|
|
|
_, err = db.Exec("CREATE VIRTUAL TABLE foo USING fts3(id INTEGER PRIMARY KEY, value TEXT)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(?, ?)", 1, `今日の 晩御飯は 天麩羅よ`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to insert value:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(?, ?)", 2, `今日は いい 天気だ`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to insert value:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.Query("SELECT id, value FROM foo WHERE value MATCH '今日* 天*'")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to query foo table:", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var id int
|
|
|
|
var value string
|
|
|
|
|
|
|
|
if err := rows.Scan(&id, &value); err != nil {
|
|
|
|
t.Error("Unable to scan results:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if id == 1 && value != `今日の 晩御飯は 天麩羅よ` {
|
|
|
|
t.Error("Value for id 1 should be `今日の 晩御飯は 天麩羅よ`, but:", value)
|
|
|
|
} else if id == 2 && value != `今日は いい 天気だ` {
|
|
|
|
t.Error("Value for id 2 should be `今日は いい 天気だ`, but:", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err = db.Query("SELECT value FROM foo WHERE value MATCH '今日* 天麩羅*'")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to query foo table:", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var value string
|
|
|
|
if !rows.Next() {
|
|
|
|
t.Fatal("Result should be only one")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Scan(&value); err != nil {
|
|
|
|
t.Fatal("Unable to scan results:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if value != `今日の 晩御飯は 天麩羅よ` {
|
|
|
|
t.Fatal("Value should be `今日の 晩御飯は 天麩羅よ`, but:", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
t.Fatal("Result should be only one")
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 19:07:23 +03:00
|
|
|
|
|
|
|
func TestFTS4(t *testing.T) {
|
2015-11-02 05:56:49 +03:00
|
|
|
tempFilename := TempFilename(t)
|
2015-11-03 15:52:28 +03:00
|
|
|
defer os.Remove(tempFilename)
|
2015-09-20 19:07:23 +03:00
|
|
|
db, err := sql.Open("sqlite3", tempFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open database:", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec("DROP TABLE foo")
|
|
|
|
_, err = db.Exec("CREATE VIRTUAL TABLE foo USING fts4(tokenize=unicode61, id INTEGER PRIMARY KEY, value TEXT)")
|
2016-04-22 16:13:07 +03:00
|
|
|
switch {
|
|
|
|
case err != nil && err.Error() == "unknown tokenizer: unicode61":
|
|
|
|
t.Skip("FTS4 not supported")
|
|
|
|
case err != nil:
|
2015-09-20 19:07:23 +03:00
|
|
|
t.Fatal("Failed to create table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(?, ?)", 1, `février`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to insert value:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.Query("SELECT value FROM foo WHERE value MATCH 'fevrier'")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to query foo table:", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var value string
|
|
|
|
if !rows.Next() {
|
|
|
|
t.Fatal("Result should be only one")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Scan(&value); err != nil {
|
|
|
|
t.Fatal("Unable to scan results:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if value != `février` {
|
|
|
|
t.Fatal("Value should be `février`, but:", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
t.Fatal("Result should be only one")
|
|
|
|
}
|
|
|
|
}
|