-
Notifications
You must be signed in to change notification settings - Fork 5
/
func_collation_test.go
74 lines (63 loc) · 1.41 KB
/
func_collation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package sqlite_test
import (
"database/sql"
. "go.riyazali.net/sqlite"
"strings"
"testing"
)
// caseInsensitive collation sequence matcher
var caseInsensitive = func(a, b string) int {
if strings.EqualFold(a, b) {
return 0
} else {
return 1
}
}
func TestCollation(t *testing.T) {
var err error
Register(func(api *ExtensionApi) (_ ErrorCode, err error) {
if err = api.CreateCollation("no_case", caseInsensitive); err != nil {
return SQLITE_ERROR, err
}
var conn = api.Connection()
if err = conn.Exec("CREATE TABLE x (value TEXT)", nil); err != nil {
return SQLITE_ERROR, err
}
var stmt *Stmt
if stmt, _, err = conn.Prepare("INSERT INTO x VALUES ($1)"); err != nil {
return SQLITE_ERROR, err
}
for _, v := range []string{"aa", "aA", "Aa", "AA", "bb"} {
stmt.BindText(1, v)
if _, err = stmt.Step(); err == nil {
err = stmt.Reset()
}
if err != nil {
return SQLITE_ERROR, err
}
}
return SQLITE_OK, nil
})
var db *sql.DB
if db, err = Connect(Memory); err != nil {
t.Fatal(err)
}
defer db.Close()
var stmt *sql.Stmt
if stmt, err = db.Prepare("SELECT * FROM x where value = 'aa' COLLATE no_case;"); err != nil {
t.Fatal(err)
}
defer stmt.Close()
var rows *sql.Rows
if rows, err = stmt.Query(); err != nil {
t.Fatal(err)
}
defer rows.Close()
var count = 0
for rows.Next() {
count++
}
if count != 4 {
t.Fatalf("invalid count: got %d", count)
}
}