-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpre_update_hook_test.go
More file actions
181 lines (165 loc) · 4.93 KB
/
pre_update_hook_test.go
File metadata and controls
181 lines (165 loc) · 4.93 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package sqlite_test
import (
"database/sql"
"fmt"
"slices"
"testing"
"modernc.org/sqlite"
sqlite3 "modernc.org/sqlite/lib"
)
func TestPreUpdateHook(t *testing.T) {
connStr := ":memory:"
driverName := "sqlite_pre_update_hook_test"
var (
insertCount int
insertNewValues []any
updateCount int
updateOldValues []any
updateNewValues []any
deleteCount int
deleteOldValues []any
commitCount int
rollbackCount int
)
var testDriver sqlite.Driver
testDriver.RegisterConnectionHook(func(conn sqlite.ExecQuerierContext, dsn string) error {
if hooker, ok := conn.(sqlite.HookRegisterer); ok {
hooker.RegisterPreUpdateHook(func(data sqlite.SQLitePreUpdateData) {
switch data.Op {
case sqlite3.SQLITE_INSERT:
insertCount++
insertNewValues = make([]any, data.Count())
err := data.New(insertNewValues...)
if err != nil {
t.Fatal(err)
}
case sqlite3.SQLITE_UPDATE:
updateCount++
updateOldValues = make([]any, data.Count())
err := data.Old(updateOldValues...)
if err != nil {
t.Fatal(err)
}
updateNewValues = make([]any, data.Count())
err = data.New(updateNewValues...)
if err != nil {
t.Fatal(err)
}
case sqlite3.SQLITE_DELETE:
deleteCount++
deleteOldValues = make([]any, data.Count())
err := data.Old(deleteOldValues...)
if err != nil {
t.Fatal(err)
}
}
})
hooker.RegisterCommitHook(func() int32 {
commitCount++
return 0
})
hooker.RegisterRollbackHook(func() {
rollbackCount++
})
}
return nil
})
sql.Register(driverName, &testDriver)
db, err := sql.Open(driverName, connStr)
if err != nil {
t.Fatal(err)
}
expectInsertValues := []any{int64(42), 3.1415, "Test", "will be nil"}
expectUpdateValues := []any{int64(43), 1.5, "Test update", nil}
_, err = db.Exec(`
CREATE TABLE in_memory_test(id INTEGER PRIMARY KEY, f FLOAT, t TEXT, x ANY);
INSERT INTO in_memory_test VALUES(42, 3.1415, 'Test', 'will be nil');
UPDATE in_memory_test SET id = 43, f = 1.5, t = 'Test update', x = null;
DELETE FROM in_memory_test;
SELECT 1;
`)
if err != nil {
t.Fatal(err)
}
if insertCount != 1 {
t.Errorf("pre update hook: expect %d inserts call, got %d", 1, insertCount)
}
if !slices.Equal(insertNewValues, expectInsertValues) {
t.Errorf("pre update hook: expect %v as inserted new values, got %v", expectInsertValues, insertNewValues)
}
if updateCount != 1 {
t.Errorf("pre update hook: expect %d updates call, got %d", 1, updateCount)
}
if !slices.Equal(updateOldValues, expectInsertValues) {
t.Errorf("pre update hook: expect %v as updated old values, got %v", expectInsertValues, updateOldValues)
}
if !slices.Equal(updateNewValues, expectUpdateValues) {
t.Errorf("pre update hook: expect %v as updated new values, got %v", expectUpdateValues, updateNewValues)
}
if deleteCount != 1 {
t.Errorf("pre update hook: expect %d deletes call, got %d", 1, deleteCount)
}
if !slices.Equal(deleteOldValues, expectUpdateValues) {
t.Errorf("pre update hook: expect %v as deleted old values, got %v", expectUpdateValues, deleteOldValues)
}
if commitCount != 4 {
t.Errorf("commit hook: expect %d, got %d", 4, commitCount)
}
if rollbackCount != 0 {
t.Errorf("rollback hook: expect %d, got %d", 0, rollbackCount)
}
}
func TestPreUpdateHookLargeRowID(t *testing.T) {
driverName := fmt.Sprintf("sqlite_preupdate_large_rowid_%p", t)
var gotOldRowID, gotNewRowID int64
var gotOp int32
var testDriver sqlite.Driver
testDriver.RegisterConnectionHook(func(conn sqlite.ExecQuerierContext, dsn string) error {
if hooker, ok := conn.(sqlite.HookRegisterer); ok {
hooker.RegisterPreUpdateHook(func(data sqlite.SQLitePreUpdateData) {
gotOp = data.Op
gotOldRowID = data.OldRowID
gotNewRowID = data.NewRowID
})
}
return nil
})
sql.Register(driverName, &testDriver)
db, err := sql.Open(driverName, ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
_, err = db.Exec(`CREATE TABLE t(id INTEGER PRIMARY KEY, v TEXT)`)
if err != nil {
t.Fatal(err)
}
const largeRowID = int64(1) << 40 // 1 TiB — well beyond int32 range
_, err = db.Exec(`INSERT INTO t VALUES(?, 'hello')`, largeRowID)
if err != nil {
t.Fatal(err)
}
if gotOp != sqlite3.SQLITE_INSERT {
t.Fatalf("expected INSERT op, got %d", gotOp)
}
if gotNewRowID != largeRowID {
t.Errorf("NewRowID: got %d, want %d", gotNewRowID, largeRowID)
}
// Also test UPDATE with large rowids
const newLargeRowID = int64(1)<<40 + 1
gotOldRowID = 0
gotNewRowID = 0
_, err = db.Exec(`UPDATE t SET id = ?, v = 'updated' WHERE id = ?`, newLargeRowID, largeRowID)
if err != nil {
t.Fatal(err)
}
if gotOp != sqlite3.SQLITE_UPDATE {
t.Fatalf("expected UPDATE op, got %d", gotOp)
}
if gotOldRowID != largeRowID {
t.Errorf("OldRowID after UPDATE: got %d, want %d", gotOldRowID, largeRowID)
}
if gotNewRowID != newLargeRowID {
t.Errorf("NewRowID after UPDATE: got %d, want %d", gotNewRowID, newLargeRowID)
}
}