From da2bf8a0f356364fb8189a5a276b5acf77839d30 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Tue, 24 Feb 2015 11:27:09 +0900 Subject: [PATCH] Add FTS3 feature. Close #176 --- sqlite3.go | 2 ++ sqlite3_fts3_test.go | 83 ++++++++++++++++++++++++++++++++++++++++++++ sqlite3_windows.go | 1 - 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 sqlite3_fts3_test.go diff --git a/sqlite3.go b/sqlite3.go index 18ab545..605474c 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -7,6 +7,8 @@ package sqlite3 /* #cgo CFLAGS: -std=gnu99 +#cgo CFLAGS: -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE +#cgo CFLAGS: -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS #include #include #include diff --git a/sqlite3_fts3_test.go b/sqlite3_fts3_test.go new file mode 100644 index 0000000..a1cd217 --- /dev/null +++ b/sqlite3_fts3_test.go @@ -0,0 +1,83 @@ +// Copyright (C) 2015 Yasuhiro Matsumoto . +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +package sqlite3 + +import ( + "database/sql" + "os" + "testing" +) + +func TestFTS3(t *testing.T) { + tempFilename := TempFilename() + db, err := sql.Open("sqlite3", tempFilename) + if err != nil { + t.Fatal("Failed to open database:", err) + } + defer os.Remove(tempFilename) + 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") + } +} diff --git a/sqlite3_windows.go b/sqlite3_windows.go index 84eb457..f47272d 100644 --- a/sqlite3_windows.go +++ b/sqlite3_windows.go @@ -9,6 +9,5 @@ package sqlite3 #cgo CFLAGS: -I. -fno-stack-check -fno-stack-protector -mno-stack-arg-probe #cgo windows,386 CFLAGS: -D_localtime32=localtime #cgo LDFLAGS: -lmingwex -lmingw32 -#cgo CFLAGS: -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE */ import "C"