sqlite3_fts3_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (C) 2015 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. package sqlite3
  6. import (
  7. "database/sql"
  8. "os"
  9. "testing"
  10. )
  11. func TestFTS3(t *testing.T) {
  12. tempFilename := TempFilename()
  13. db, err := sql.Open("sqlite3", tempFilename)
  14. if err != nil {
  15. t.Fatal("Failed to open database:", err)
  16. }
  17. defer os.Remove(tempFilename)
  18. defer db.Close()
  19. _, err = db.Exec("DROP TABLE foo")
  20. _, err = db.Exec("CREATE VIRTUAL TABLE foo USING fts3(id INTEGER PRIMARY KEY, value TEXT)")
  21. if err != nil {
  22. t.Fatal("Failed to create table:", err)
  23. }
  24. _, err = db.Exec("INSERT INTO foo(id, value) VALUES(?, ?)", 1, `今日の 晩御飯は 天麩羅よ`)
  25. if err != nil {
  26. t.Fatal("Failed to insert value:", err)
  27. }
  28. _, err = db.Exec("INSERT INTO foo(id, value) VALUES(?, ?)", 2, `今日は いい 天気だ`)
  29. if err != nil {
  30. t.Fatal("Failed to insert value:", err)
  31. }
  32. rows, err := db.Query("SELECT id, value FROM foo WHERE value MATCH '今日* 天*'")
  33. if err != nil {
  34. t.Fatal("Unable to query foo table:", err)
  35. }
  36. defer rows.Close()
  37. for rows.Next() {
  38. var id int
  39. var value string
  40. if err := rows.Scan(&id, &value); err != nil {
  41. t.Error("Unable to scan results:", err)
  42. continue
  43. }
  44. if id == 1 && value != `今日の 晩御飯は 天麩羅よ` {
  45. t.Error("Value for id 1 should be `今日の 晩御飯は 天麩羅よ`, but:", value)
  46. } else if id == 2 && value != `今日は いい 天気だ` {
  47. t.Error("Value for id 2 should be `今日は いい 天気だ`, but:", value)
  48. }
  49. }
  50. rows, err = db.Query("SELECT value FROM foo WHERE value MATCH '今日* 天麩羅*'")
  51. if err != nil {
  52. t.Fatal("Unable to query foo table:", err)
  53. }
  54. defer rows.Close()
  55. var value string
  56. if !rows.Next() {
  57. t.Fatal("Result should be only one")
  58. }
  59. if err := rows.Scan(&value); err != nil {
  60. t.Fatal("Unable to scan results:", err)
  61. }
  62. if value != `今日の 晩御飯は 天麩羅よ` {
  63. t.Fatal("Value should be `今日の 晩御飯は 天麩羅よ`, but:", value)
  64. }
  65. if rows.Next() {
  66. t.Fatal("Result should be only one")
  67. }
  68. }