sqlite3_fts3_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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(t)
  13. defer os.Remove(tempFilename)
  14. db, err := sql.Open("sqlite3", tempFilename)
  15. if err != nil {
  16. t.Fatal("Failed to open database:", err)
  17. }
  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. }
  69. func TestFTS4(t *testing.T) {
  70. tempFilename := TempFilename(t)
  71. defer os.Remove(tempFilename)
  72. db, err := sql.Open("sqlite3", tempFilename)
  73. if err != nil {
  74. t.Fatal("Failed to open database:", err)
  75. }
  76. defer db.Close()
  77. _, err = db.Exec("DROP TABLE foo")
  78. _, err = db.Exec("CREATE VIRTUAL TABLE foo USING fts4(tokenize=unicode61, id INTEGER PRIMARY KEY, value TEXT)")
  79. switch {
  80. case err != nil && err.Error() == "unknown tokenizer: unicode61":
  81. t.Skip("FTS4 not supported")
  82. case err != nil:
  83. t.Fatal("Failed to create table:", err)
  84. }
  85. _, err = db.Exec("INSERT INTO foo(id, value) VALUES(?, ?)", 1, `février`)
  86. if err != nil {
  87. t.Fatal("Failed to insert value:", err)
  88. }
  89. rows, err := db.Query("SELECT value FROM foo WHERE value MATCH 'fevrier'")
  90. if err != nil {
  91. t.Fatal("Unable to query foo table:", err)
  92. }
  93. defer rows.Close()
  94. var value string
  95. if !rows.Next() {
  96. t.Fatal("Result should be only one")
  97. }
  98. if err := rows.Scan(&value); err != nil {
  99. t.Fatal("Unable to scan results:", err)
  100. }
  101. if value != `février` {
  102. t.Fatal("Value should be `février`, but:", value)
  103. }
  104. if rows.Next() {
  105. t.Fatal("Result should be only one")
  106. }
  107. }