LuaSQLite3

Check-in [5282faf755]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Updated example and test files from Dmitry
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5282faf7552fa42d187f1e1ab0ec2e1ced980725
User & Date: e 2013-04-08 16:52:47
Context
2013-04-08
17:08
Update rockspec and HISTORY check-in: 393151db27 user: e tags: fsl_9b, trunk
16:52
Updated example and test files from Dmitry check-in: 5282faf755 user: e tags: trunk
2013-04-05
17:20
Added tests and example contributed by Dmitry Pashkevich check-in: 29a180ad4e user: e tags: fsl_9, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added examples/hooks_advanced.lua.



























































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

local sqlite3 = require("lsqlite3")
local db

optbl = { [sqlite3.UPDATE] = "UPDATE";
          [sqlite3.INSERT] = "INSERT";
          [sqlite3.DELETE] = "DELETE"
        }
setmetatable(optbl,
	{__index=function(t,n) return string.format("Unknown op %d",n) end})


function update_hook(ud, op, dname, tname, rowid)
  print("Sqlite Update Hook:", optbl[op], dname, tname, rowid)
end

function commit_hook(ud)
  print("<Sqlite Commit Hook>")
end

function rollback_hook(ud)
  print("<Sqlite Rollback Hook>")
end

-- db:exec wrapper with result assertion
function db_exec(stmt)
  if db:exec(stmt) ~= sqlite3.OK then
    print("Sqlite ERROR:        ", db:errmsg())
  end
end

-- debug output for database table
function db_print_tables(...)
  for i = 1,select('#',...) do
    name = select(i,...)
    print(string.format("\n%s contents:", name))
    for row in db:nrows("SELECT * FROM " .. name) do
      print(row.id, row.content)
    end
  end
end

local tests = {}

-- runs specified test with all necessary setup
-- @param string name
function run_test(name)
  -- banner
    print(string.format([[


  ==========================
   %s
  ==========================
  ]], name))

  -- setup
  db = sqlite3.open_memory()
  local udtbl = {0, 0, 0}
  db:update_hook(update_hook, udtbl)
  db:commit_hook(commit_hook, udtbl)
  db:rollback_hook(rollback_hook, udtbl)

  -- run test
  tests[name]()

  -- destroy
  db:close()
end


function tests.insert_select()
  db_exec[[
    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY, content VARCHAR );

    INSERT INTO T1 VALUES (NULL, 'Hello World');
    INSERT INTO T1 VALUES (NULL, 'Hello Lua');
    INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');

    INSERT INTO T2 SELECT * FROM T1;
  ]]

  db_print_tables('T1', 'T2')
end

function tests.trigger_insert()
  db_exec[[
    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY, content VARCHAR );

    CREATE TRIGGER after_insert_T1
    AFTER INSERT ON T1
    FOR EACH ROW
    BEGIN
      INSERT INTO T2 VALUES(NEW.id, NEW.content);
    END;

    INSERT INTO T1 VALUES (NULL, 'Hello World');
    INSERT INTO T1 VALUES (NULL, 'Hello Lua');
    INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');
  ]]

  db_print_tables('T1', 'T2')
end

function tests.cascade_delete()
  db_exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON DELETE CASCADE, content VARCHAR );
    CREATE TABLE T3 ( id INTEGER PRIMARY KEY REFERENCES T2 ON DELETE CASCADE, content VARCHAR );

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T1 VALUES (NULL, 'c');
    INSERT INTO T1 VALUES (NULL, 'd');

    INSERT INTO T2 SELECT * FROM T1;
    INSERT INTO T3 SELECT * FROM T2;

    DELETE FROM T1 WHERE id < 3;
  ]]

  db_print_tables('T1', 'T2', 'T3')
end

function tests.cascade_update()
  db_exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON UPDATE CASCADE, content VARCHAR );
    CREATE TABLE T3 ( id INTEGER PRIMARY KEY REFERENCES T2 ON UPDATE CASCADE, content VARCHAR );

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T1 VALUES (NULL, 'c');
    INSERT INTO T1 VALUES (NULL, 'd');

    INSERT INTO T2 SELECT * FROM T1;
    INSERT INTO T3 SELECT * FROM T2;

    UPDATE T1 SET id = id + 10 WHERE id < 3;
  ]]

  db_print_tables('T1', 'T2', 'T3')
end

-- hook "anomaly"
-- implicit one-statement transaction rollback demonstration
function tests.cascade_update_restrict()
  db_exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON UPDATE RESTRICT, content VARCHAR );
    CREATE TABLE T3 ( id INTEGER PRIMARY KEY REFERENCES T2 ON UPDATE RESTRICT, content VARCHAR );

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T1 VALUES (NULL, 'c');
    INSERT INTO T1 VALUES (NULL, 'd');

    INSERT INTO T2 SELECT * FROM T1;
    INSERT INTO T3 SELECT * FROM T2;

    -- this update gets reverted but the update_hook with rowid=11 *DOES* get triggered
    UPDATE T1 SET id = id + 10 WHERE id < 3;
  ]]

  db_print_tables('T1', 'T2', 'T3')
end

-- hook "anomaly"
-- case is analogous to cascade_update_restrict
function tests.cascade_delete_restrict()
  db_exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON DELETE RESTRICT, content VARCHAR );
    CREATE TABLE T3 ( id INTEGER PRIMARY KEY REFERENCES T2 ON DELETE RESTRICT, content VARCHAR );

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T1 VALUES (NULL, 'c');
    INSERT INTO T1 VALUES (NULL, 'd');

    INSERT INTO T2 SELECT * FROM T1;
    INSERT INTO T3 SELECT * FROM T2;

    DELETE FROM T1 WHERE id < 3;
  ]]

  db_print_tables('T1', 'T2', 'T3')
end

-- no anomalies here
function tests.fk_violate_insert()
  db_exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR);

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');

    INSERT INTO T2 VALUES(99, 'xxx');
  ]]

  db_print_tables('T1', 'T2')
end

-- hook "anomaly"
function tests.fk_violate_update()
  db_exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR);

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T2 VALUES(1, 'a');

    -- update doesn't succeed but we get a hook for tuple with rowid=99 in T2
    UPDATE T2 SET id = 99 WHERE id = 1;
  ]]

  db_print_tables('T1', 'T2')
end

-- like fk_violate_update but wrapped inside an explicit transaction
function tests.transaction_fk_violate_update()
  db_exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR);

    BEGIN TRANSACTION;
      INSERT INTO T1 VALUES (NULL, 'a');
      INSERT INTO T1 VALUES (NULL, 'b');
      INSERT INTO T2 VALUES(1, 'a');

      -- Doesn't trigger rollback hook because the implicit update statement transaction
      -- is nested inside our explicit transaction. However we *do* get an error.
      UPDATE T2 SET id = 99 WHERE id = 1;
    COMMIT;
  ]]

  db_print_tables('T1', 'T2')
end

function tests.cascade_update_setnull()
  db_exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY, content INTEGER REFERENCES T1(id) ON UPDATE SET NULL);

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T1 VALUES (NULL, 'c');
    INSERT INTO T1 VALUES (NULL, 'd');

    INSERT INTO T2 SELECT NULL, id FROM T1;

    UPDATE T1 SET id = id + 10 WHERE id < 3;
  ]]

    db_print_tables('T1', 'T2')
end

function tests.transaction_commit()
  db_exec[[
    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );

    BEGIN TRANSACTION;
      INSERT INTO T1 VALUES (NULL, 'Hello World');
      INSERT INTO T1 VALUES (NULL, 'Hello Lua');
    COMMIT;
  ]]

  db_print_tables('T1')
end

function tests.transaction_rollback()
  db_exec[[
    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );

    BEGIN TRANSACTION;
      INSERT INTO T1 VALUES (NULL, 'Hello World');
      INSERT INTO T1 VALUES (NULL, 'Hello Lua');
    ROLLBACK;
  ]]

  db_print_tables('T1')
end

function tests.savepoint_nested_commit()
  db_exec[[
    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );

    SAVEPOINT S1;
      INSERT INTO T1 VALUES (NULL, 'Hello World');
      INSERT INTO T1 VALUES (NULL, 'Hello Lua');

      SAVEPOINT S2;
        INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');
        -- nested commit doesn't trigger commit_hook
      RELEASE S2;

      INSERT INTO T1 VALUES (NULL, 'Hello transactions');
    RELEASE S1;
  ]]

  db_print_tables('T1')
end

function tests.savepoint_nested_rollback()
  db_exec[[
    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );

    SAVEPOINT S1;
      INSERT INTO T1 VALUES (NULL, 'Hello World');
      INSERT INTO T1 VALUES (NULL, 'Hello Lua');

      SAVEPOINT S2;
        INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');
        -- nested rollback doesn't trigger rollback_hook
      ROLLBACK TO S2;

      INSERT INTO T1 VALUES (NULL, 'Hello transactions');
    RELEASE S1;
  ]]

  db_print_tables('T1')
end


-- run_test('fk_violate_insert')

for k,v in pairs(tests) do
    run_test(k)
end

Deleted examples/update_hook_advanced.lua.

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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

local sqlite3 = require("lsqlite3")
local db

optbl = { [sqlite3.UPDATE] = "UPDATE";
          [sqlite3.INSERT] = "INSERT";
          [sqlite3.DELETE] = "DELETE"
        }
setmetatable(optbl,
	{__index=function(t,n) return string.format("Unknown op %d",n) end})


function update_hook(ud, op, dname, tname, rowid)
  print("Sqlite Update Hook:", optbl[op], dname, tname, rowid)
end

local tests = {}

-- runs specified test with all necessary setup
-- @param string name
function run_test(name)
  -- banner
    print(string.format([[


  ==========================
   %s
  ==========================
  ]], name))

  -- setup
  db = sqlite3.open_memory()
  local udtbl = {0, 0, 0}
  db:update_hook(update_hook, udtbl)

  -- run test
  tests[name]()

  -- destroy
  db:close()
end

function tests.insert_select()
  db:exec[[
    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY, content VARCHAR );

    INSERT INTO T1 VALUES (NULL, 'Hello World');
    INSERT INTO T1 VALUES (NULL, 'Hello Lua');
    INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');

    INSERT INTO T2 SELECT * FROM T1;
  ]]

  print("\nT1 contents:")
  for row in db:nrows("SELECT * FROM T1") do
    print(row.id, row.content)
  end

  print("\nT2 contents:")
  for row in db:nrows("SELECT * FROM T2") do
    print(row.id, row.content)
  end
end

function tests.trigger_insert()
  db:exec[[
    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY, content VARCHAR );

    CREATE TRIGGER after_insert_T1
    AFTER INSERT ON T1
    FOR EACH ROW
    BEGIN
      INSERT INTO T2 VALUES(NEW.id, NEW.content);
    END;

    INSERT INTO T1 VALUES (NULL, 'Hello World');
    INSERT INTO T1 VALUES (NULL, 'Hello Lua');
    INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');
  ]]

  print("\nT1 contents:")
  for row in db:nrows("SELECT * FROM T1") do
    print(row.id, row.content)
  end

  print("\nT2 contents:")
  for row in db:nrows("SELECT * FROM T2") do
    print(row.id, row.content)
  end
end

function tests.cascade_delete()
  db:exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON DELETE CASCADE, content VARCHAR );
    CREATE TABLE T3 ( id INTEGER PRIMARY KEY REFERENCES T2 ON DELETE CASCADE, content VARCHAR );

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T1 VALUES (NULL, 'c');
    INSERT INTO T1 VALUES (NULL, 'd');

    INSERT INTO T2 SELECT * FROM T1;
    INSERT INTO T3 SELECT * FROM T2;

    DELETE FROM T1 WHERE id < 3;
  ]]

  print("\nT1 contents:")
  for row in db:nrows("SELECT * FROM T1") do
    print(row.id, row.content)
  end

  print("\nT2 contents:")
  for row in db:nrows("SELECT * FROM T2") do
    print(row.id, row.content)
  end

  print("\nT3 contents:")
  for row in db:nrows("SELECT * FROM T3") do
    print(row.id, row.content)
  end
end

function tests.cascade_update_update()
  db:exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON UPDATE CASCADE, content VARCHAR );
    CREATE TABLE T3 ( id INTEGER PRIMARY KEY REFERENCES T2 ON UPDATE CASCADE, content VARCHAR );

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T1 VALUES (NULL, 'c');
    INSERT INTO T1 VALUES (NULL, 'd');

    INSERT INTO T2 SELECT * FROM T1;
    INSERT INTO T3 SELECT * FROM T2;

    UPDATE T1 SET id = id + 10 WHERE id < 3;
  ]]

  print("\nT1 contents:")
  for row in db:nrows("SELECT * FROM T1") do
    print(row.id, row.content)
  end

  print("\nT2 contents:")
  for row in db:nrows("SELECT * FROM T2") do
    print(row.id, row.content)
  end

  print("\nT3 contents:")
  for row in db:nrows("SELECT * FROM T3") do
    print(row.id, row.content)
  end
end

-- hook "anomaly"
-- implicit one-statement transaction rollback demonstration
function tests.cascade_update_restrict()
  db:exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON UPDATE RESTRICT, content VARCHAR );
    CREATE TABLE T3 ( id INTEGER PRIMARY KEY REFERENCES T2 ON UPDATE RESTRICT, content VARCHAR );

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T1 VALUES (NULL, 'c');
    INSERT INTO T1 VALUES (NULL, 'd');

    INSERT INTO T2 SELECT * FROM T1;
    INSERT INTO T3 SELECT * FROM T2;

    -- this update gets reverted but the update_hook with rowid=11 *DOES* get triggered
    UPDATE T1 SET id = id + 10 WHERE id < 3;
  ]]

  print("\nT1 contents:")
  for row in db:nrows("SELECT * FROM T1") do
    print(row.id, row.content)
  end

  print("\nT2 contents:")
  for row in db:nrows("SELECT * FROM T2") do
    print(row.id, row.content)
  end

  print("\nT3 contents:")
  for row in db:nrows("SELECT * FROM T3") do
    print(row.id, row.content)
  end
end

-- hook "anomaly"
-- case is analogous to cascade_update_restrict
function tests.cascade_delete_restrict()
  db:exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON DELETE RESTRICT, content VARCHAR );
    CREATE TABLE T3 ( id INTEGER PRIMARY KEY REFERENCES T2 ON DELETE RESTRICT, content VARCHAR );

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T1 VALUES (NULL, 'c');
    INSERT INTO T1 VALUES (NULL, 'd');

    INSERT INTO T2 SELECT * FROM T1;
    INSERT INTO T3 SELECT * FROM T2;

    DELETE FROM T1 WHERE id < 3;
  ]]

  print("\nT1 contents:")
  for row in db:nrows("SELECT * FROM T1") do
    print(row.id, row.content)
  end

  print("\nT2 contents:")
  for row in db:nrows("SELECT * FROM T2") do
    print(row.id, row.content)
  end

  print("\nT3 contents:")
  for row in db:nrows("SELECT * FROM T3") do
    print(row.id, row.content)
  end
end

-- no anomalies here
function tests.fk_violate_insert()
  db:exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR);

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');

    INSERT INTO T2 VALUES(99, 'xxx');
  ]]

  print("\nT1 contents:")
  for row in db:nrows("SELECT * FROM T1") do
    print(row.id, row.content)
  end

  print("\nT2 contents:")
  for row in db:nrows("SELECT * FROM T2") do
    print(row.id, row.content)
  end
end

-- hook "anomaly"
function tests.fk_violate_update()
  db:exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR);

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T2 VALUES(1, 'a');

    -- update doesn't succeed but we get a hook for tuple with rowid=99 in T2
    UPDATE T2 SET id = 99 WHERE id = 1;
  ]]

  print("\nT1 contents:")
  for row in db:nrows("SELECT * FROM T1") do
    print(row.id, row.content)
  end

  print("\nT2 contents:")
  for row in db:nrows("SELECT * FROM T2") do
    print(row.id, row.content)
  end
end

function tests.cascade_update_setnull()
  db:exec[[
    PRAGMA foreign_keys = ON;

    CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY, content INTEGER REFERENCES T1(id) ON UPDATE SET NULL);

    INSERT INTO T1 VALUES (NULL, 'a');
    INSERT INTO T1 VALUES (NULL, 'b');
    INSERT INTO T1 VALUES (NULL, 'c');
    INSERT INTO T1 VALUES (NULL, 'd');

    INSERT INTO T2 SELECT NULL, id FROM T1;

    UPDATE T1 SET id = id + 10 WHERE id < 3;
  ]]

  print("\nT1 contents:")
  for row in db:nrows("SELECT * FROM T1") do
    print(row.id, row.content)
  end

  print("\nT2 contents:")
  for row in db:nrows("SELECT * FROM T2") do
    print(row.id, row.content)
  end
end


-- run_test('fk_violate_update')

for k,v in pairs(tests) do
    run_test(k)
end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<








































































































































































































































































































































































































































































































































































































































































Changes to test/tests-sqlite3.lua.

504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523












524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546

547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591

















































































































592


593
594



595

596




















































































































597
598
599
600
601
602
603
----------------------------

uh = lunit_TestCase("Update Hook")

function uh.setup()
  uh.db = assert( sqlite3.open_memory() )
  uh.udtbl = {[sqlite3.INSERT]=0, [sqlite3.UPDATE]=0, [sqlite3.DELETE]=0}
  uh.uttblsz = function () local sz = 0; for _,_ in pairs(uh.udtbl) do sz = sz + 1 end return sz end
  assert_number( uh.db:exec("CREATE TABLE test ( id INTEGER PRIMARY KEY, content VARCHAR );") )
end

function uh.teardown()
  assert_number( uh.db:close() )
end

function uh.test_create()
  assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
    --print("Sqlite Update Hook:", op, dname, tname, rowid)
    ud[op] = ud[op] + 1
  end, uh.udtbl))












end

function uh.test_insert1()
  assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
    ud[op] = ud[op] + 1
  end, uh.udtbl))
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
  --for k,v in pairs(uh.udtbl) do print(k,v) end
  assert_equal( 1, uh.udtbl[sqlite3.INSERT] )
end

function uh.test_insert3()
  assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
    ud[op] = ud[op] + 1
  end, uh.udtbl))
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
  --for k,v in pairs(uh.udtbl) do print(k,v) end
  assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
  assert_equal( 3, uh.uttblsz() )

end

function uh.test_insert3update1()
  assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
    ud[op] = ud[op] + 1
  end, uh.udtbl))
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
  assert_equal( sqlite3.OK, uh.db:exec("UPDATE test SET content = 'Hello Again World' WHERE id = 1;") )
  --for k,v in pairs(uh.udtbl) do print(k,v) end
  assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 1, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
  assert_equal( 3, uh.uttblsz() )
end

function uh.test_insert3delete1()
  assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
    ud[op] = ud[op] + 1
  end, uh.udtbl))
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
  assert_equal( sqlite3.OK, uh.db:exec("DELETE FROM test WHERE id = 2;") )
  --for k,v in pairs(uh.udtbl) do print(k,v) end
  assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 1, uh.udtbl[sqlite3.DELETE] )
  assert_equal( 3, uh.uttblsz() )
end

function uh.test_insert3update1delete1()
  assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
    ud[op] = ud[op] + 1
  end, uh.udtbl))
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
  assert_equal( sqlite3.OK, uh.db:exec("UPDATE test SET content = 'Hello Again World' WHERE id = 1;") )
  assert_equal( sqlite3.OK, uh.db:exec("DELETE FROM test WHERE id = 2;") )
  --for k,v in pairs(uh.udtbl) do print(k,v) end
  assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 1, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 1, uh.udtbl[sqlite3.DELETE] )

















































































































  assert_equal( 3, uh.uttblsz() )


end





























































































































--------------------------------------------
-- Tests loop break and statement reusage --
--------------------------------------------









|
<
<
<
<
<
<
<
<




>
>
>
>
>
>
>
>
>
>
>
>



<
<
<

<




<
<
<



<



<
>


|
<
<
<




<



<


|
<
<
<




<



<


|
<
<
<





<



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>


>
>
>

>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







504
505
506
507
508
509
510
511








512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530



531

532
533
534
535



536
537
538

539
540
541

542
543
544
545



546
547
548
549

550
551
552

553
554
555



556
557
558
559

560
561
562

563
564
565



566
567
568
569
570

571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
----------------------------

uh = lunit_TestCase("Update Hook")

function uh.setup()
  uh.db = assert( sqlite3.open_memory() )
  uh.udtbl = {[sqlite3.INSERT]=0, [sqlite3.UPDATE]=0, [sqlite3.DELETE]=0}
  uh.crtbl = {0, 0}








  assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
    --print("Sqlite Update Hook:", op, dname, tname, rowid)
    ud[op] = ud[op] + 1
  end, uh.udtbl))
  uh.uttblsz = function () local sz = 0; for _,_ in pairs(uh.udtbl) do sz = sz + 1 end return sz end
  -- enable foreign keys!
  assert_number( uh.db:exec("PRAGMA foreign_keys = ON;") )
  assert_number( uh.db:exec("CREATE TABLE test ( id INTEGER PRIMARY KEY, content VARCHAR );") )
  assert_number( uh.db:exec("CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );") )
  assert_number( uh.db:exec("CREATE TABLE T2 ( id INTEGER PRIMARY KEY, content VARCHAR );") )
end

function uh.teardown()
  --for k,v in pairs(uh.udtbl) do print(k,v) end
  assert_equal( 3, uh.uttblsz() )
  assert_number( uh.db:close() )
end

function uh.test_insert1()



  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )

  assert_equal( 1, uh.udtbl[sqlite3.INSERT] )
end

function uh.test_insert3()



  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )

  assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 0, uh.udtbl[sqlite3.DELETE] )


end

function uh.test_insert3_update1()



  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
  assert_equal( sqlite3.OK, uh.db:exec("UPDATE test SET content = 'Hello Again World' WHERE id = 1;") )

  assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 1, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 0, uh.udtbl[sqlite3.DELETE] )

end

function uh.test_insert3_delete1()



  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
  assert_equal( sqlite3.OK, uh.db:exec("DELETE FROM test WHERE id = 2;") )

  assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 1, uh.udtbl[sqlite3.DELETE] )

end

function uh.test_insert3_update1_delete1()



  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
  assert_equal( sqlite3.OK, uh.db:exec("UPDATE test SET content = 'Hello Again World' WHERE id = 1;") )
  assert_equal( sqlite3.OK, uh.db:exec("DELETE FROM test WHERE id = 2;") )

  assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 1, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 1, uh.udtbl[sqlite3.DELETE] )
end

function uh.test_insert_select()
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T2 SELECT * FROM T1;") )
  assert_equal( 6, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end

function uh.test_trigger_insert()
  assert_equal( sqlite3.OK, uh.db:exec([[
    CREATE TRIGGER after_insert_T1
    AFTER INSERT ON T1
    FOR EACH ROW
    BEGIN
      INSERT INTO T2 VALUES(NEW.id, NEW.content);
    END;
  ]]) )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');") )
  assert_equal( 6, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end

function uh.test_cascade_update()
  assert_equal( sqlite3.OK, uh.db:exec("DROP TABLE T2;") )
  assert_equal( sqlite3.OK, uh.db:exec([[
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON UPDATE CASCADE, content VARCHAR );
  ]]) )

  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'c');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'd');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T2 SELECT * FROM T1;") )
  assert_equal( sqlite3.OK, uh.db:exec("UPDATE T1 SET id = id + 10 WHERE id < 3;") )

  assert_equal( 8, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 4, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end

function uh.test_cascade_update_restrict()
  assert_equal( sqlite3.OK, uh.db:exec("DROP TABLE T2;") )
  assert_equal( sqlite3.OK, uh.db:exec([[
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON UPDATE RESTRICT, content VARCHAR );
  ]]) )

  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'c');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'd');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T2 SELECT * FROM T1;") )
  -- the update_hook with rowid=11 *DOES* get triggered before the RESTRICT constraint is enforced
  assert_equal( sqlite3.CONSTRAINT, uh.db:exec("UPDATE T1 SET id = id + 10 WHERE id < 3;") )

  assert_equal( 8, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 1, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end

function uh.test_cascade_delete_restrict()
  assert_equal( sqlite3.OK, uh.db:exec("DROP TABLE T2;") )
  assert_equal( sqlite3.OK, uh.db:exec([[
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON DELETE RESTRICT, content VARCHAR );
  ]]) )

  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'c');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'd');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T2 SELECT * FROM T1;") )
  -- the update_hook with rowid=1 *DOES* get triggered before the RESTRICT constraint is enforced
  assert_equal( sqlite3.CONSTRAINT, uh.db:exec("DELETE FROM T1 WHERE id < 3;") )

  assert_equal( 8, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 1, uh.udtbl[sqlite3.DELETE] )
end

function uh.test_fk_violate_insert()
  assert_equal( sqlite3.OK, uh.db:exec("DROP TABLE T2;") )
  assert_equal( sqlite3.OK, uh.db:exec([[
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR );
  ]]) )

  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
  -- no update hook triggered here
  assert_equal( sqlite3.CONSTRAINT, uh.db:exec("INSERT INTO T2 VALUES(99, 'xxx')") )

  assert_equal( 2, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end

function uh.test_fk_violate_update()
  assert_equal( sqlite3.OK, uh.db:exec("DROP TABLE T2;") )
  assert_equal( sqlite3.OK, uh.db:exec([[
    CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR );
  ]]) )

  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
  assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T2 VALUES (1, 'a');") )
  -- update hook triggered
  assert_equal( sqlite3.CONSTRAINT, uh.db:exec("UPDATE T2 SET id = 99 WHERE id = 1;") )

  assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
  assert_equal( 1, uh.udtbl[sqlite3.UPDATE] )
  assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end

----------------------------------------------
-- Tests for commit_hook and rollback_hook  --
----------------------------------------------

crh = lunit_TestCase("Commit/Rollback Hook")

function crh.setup()
  crh.db = assert( sqlite3.open_memory() )
  crh.crtbl = {0, 0}
  assert_number( crh.db:exec("PRAGMA foreign_keys = ON;") )
  assert_number( crh.db:exec("CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );") )
  assert_number( crh.db:exec("CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR );") )

  -- reset commit hook return value
  crh.commit_hook_returnvalue = false

  assert_nil( crh.db:commit_hook(function (ud)
    -- print('Commit hook')
    ud[1] = ud[1] + 1;
    return crh.commit_hook_returnvalue
  end, crh.crtbl))

  assert_nil( crh.db:rollback_hook(function (ud)
    -- print('Rollback hook')
    ud[2] = ud[2] + 1
  end, crh.crtbl))
end

function crh.teardown()
  assert_number( crh.db:close() )
end

function crh.test_simple_transaction_commit()
  assert_equal( sqlite3.OK, crh.db:exec("BEGIN;") )
  assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, crh.db:exec("UPDATE T1 SET content = 'Hello Again World' WHERE id = 1;") )
  assert_equal( sqlite3.OK, crh.db:exec("DELETE FROM T1 WHERE id = 2;") )
  assert_equal( sqlite3.OK, crh.db:exec("COMMIT;") )
  assert_equal( 1, crh.crtbl[1] )
  assert_equal( 0, crh.crtbl[2] )
end

function crh.test_simple_transaction_prohibit()
  -- if the commit hook returns anything except false or nil, the commit would turn into rollback
  crh.commit_hook_returnvalue = 1

  assert_equal( sqlite3.OK, crh.db:exec("BEGIN;") )
  assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, crh.db:exec("UPDATE T1 SET content = 'Hello Again World' WHERE id = 1;") )
  assert_equal( sqlite3.OK, crh.db:exec("DELETE FROM T1 WHERE id = 2;") )
  assert_equal( sqlite3.CONSTRAINT, crh.db:exec("COMMIT;") )
  -- commit hook gets called and returns 1 triggering a rollback
  assert_equal( 1, crh.crtbl[1] )
  assert_equal( 1, crh.crtbl[2] )
end

function crh.test_simple_transaction_rollback()
  assert_equal( sqlite3.OK, crh.db:exec("BEGIN;") )
  assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, crh.db:exec("UPDATE T1 SET content = 'Hello Again World' WHERE id = 1;") )
  assert_equal( sqlite3.OK, crh.db:exec("DELETE FROM T1 WHERE id = 2;") )
  assert_equal( sqlite3.OK, crh.db:exec("ROLLBACK;") )
  assert_equal( 0, crh.crtbl[1] )
  assert_equal( 1, crh.crtbl[2] )
end

function crh.test_statement_level_transaction()
  assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, crh.db:exec("UPDATE T1 SET content = 'Hello Again World' WHERE id = 1;") )
  assert_equal( sqlite3.OK, crh.db:exec("DELETE FROM T1 WHERE id = 2;") )
  assert_equal( 3, crh.crtbl[1] )
  assert_equal( 0, crh.crtbl[2] )
end

function crh.test_statement_level_fk_violate_insert()
  assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
  assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
  -- implicit statement-level transaction rollback + error
  assert_equal( sqlite3.CONSTRAINT, crh.db:exec("INSERT INTO T2 VALUES(99, 'xxx')") )
  assert_equal( 2, crh.crtbl[1] )
  assert_equal( 1, crh.crtbl[2] )
end

function crh.test_transaction_fk_violate_update()
  assert_equal( sqlite3.OK, crh.db:exec("BEGIN;") )
  assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T2 VALUES (1, 'xyz');") )
  -- Doesn't trigger rollback hook because the implicit update statement transaction
  -- is nested inside our explicit transaction. However we *do* get an error.
  assert_equal( sqlite3.CONSTRAINT, crh.db:exec("UPDATE T2 SET id = 99 WHERE id = 1;") )
  -- rollback explicitly
  assert_equal( sqlite3.OK, crh.db:exec("ROLLBACK;") )

  assert_equal( 0, crh.crtbl[1] )
  assert_equal( 1, crh.crtbl[2] )
end

function crh.test_savepoint_nested_commit()
  assert_equal( sqlite3.OK, crh.db:exec("SAVEPOINT S1;") )
  assert_equal( sqlite3.OK, crh.db:exec(" INSERT INTO T1 VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, crh.db:exec(" INSERT INTO T1 VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, crh.db:exec(" SAVEPOINT S2;") )
  assert_equal( sqlite3.OK, crh.db:exec("  INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');") )
  -- nested transactions don't trigger commit/rollback hooks
  assert_equal( sqlite3.OK, crh.db:exec(" RELEASE S2;") )
  assert_equal( sqlite3.OK, crh.db:exec("RELEASE S1;") )
  assert_equal( 1, crh.crtbl[1] )
  assert_equal( 0, crh.crtbl[2] )
end

function crh.test_savepoint_nested_rollback()
  assert_equal( sqlite3.OK, crh.db:exec("SAVEPOINT S1;") )
  assert_equal( sqlite3.OK, crh.db:exec(" INSERT INTO T1 VALUES (NULL, 'Hello World');") )
  assert_equal( sqlite3.OK, crh.db:exec(" INSERT INTO T1 VALUES (NULL, 'Hello Lua');") )
  assert_equal( sqlite3.OK, crh.db:exec(" SAVEPOINT S2;") )
  assert_equal( sqlite3.OK, crh.db:exec("  INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');") )
  -- nested transactions don't trigger commit/rollback hooks
  assert_equal( sqlite3.OK, crh.db:exec(" ROLLBACK TO S2;") )
  assert_equal( sqlite3.OK, crh.db:exec("RELEASE S1;") )
  assert_equal( 1, crh.crtbl[1] )
  assert_equal( 0, crh.crtbl[2] )
end


--------------------------------------------
-- Tests loop break and statement reusage --
--------------------------------------------