LuaSQLite3

Check-in [29a180ad4e]
Login

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

Overview
Comment:Added tests and example contributed by Dmitry Pashkevich
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | fsl_9 | trunk
Files: files | file ages | folders
SHA1: 29a180ad4e88a053a3020798e86345a4ddbf9ed8
User & Date: e 2013-04-05 17:20:28
References
2013-04-05
17:23 Closed ticket [c7b818d353]: Support for sqlite3_update_hook? plus 3 other changes artifact: cb51c59b4a user: e
Context
2013-04-08
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
2013-04-01
18:02
Add commit_hook and rollback_hook to complement update_hook. check-in: bb4d13eba2 user: e tags: trunk
Changes
Hide Diffs Unified Diffs Show Whitespace Changes Patch

Added 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
...
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
----------------------------

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}
  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
................................................................................
  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


function uh.test_xinsert3update1delete1()
  assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
    ud[op] = ud[op] + 1
  end, uh.udtbl))
  assert_nil( uh.db:commit_hook(function (ud) ud[1] = ud[1] + 1; return false end, uh.crtbl))
  assert_nil( uh.db:rollback_hook(function (ud) ud[2] = ud[2] + 1 end, uh.crtbl))
  assert_equal( sqlite3.OK, uh.db:exec("BEGIN;") )
  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( sqlite3.OK, uh.db:exec("COMMIT;") )
  --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() )
  assert_equal( 1, uh.crtbl[1] )
  assert_equal( 0, uh.crtbl[2] )
end

function uh.test_yinsert3update1delete1()
  assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
    ud[op] = ud[op] + 1
  end, uh.udtbl))
  assert_nil( uh.db:commit_hook(function (ud) ud[1] = ud[1] + 1; return false end, uh.crtbl))
  assert_nil( uh.db:rollback_hook(function (ud) ud[2] = ud[2] + 1 end, uh.crtbl))
  assert_equal( sqlite3.OK, uh.db:exec("BEGIN;") )
  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( sqlite3.OK, uh.db:exec("ROLLBACK;") )
  --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() )
  assert_equal( 0, uh.crtbl[1] )
  assert_equal( 1, uh.crtbl[2] )
end

function uh.test_zinsert3update1delete1()
  assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
    ud[op] = ud[op] + 1
  end, uh.udtbl))
  assert_nil( uh.db:commit_hook(function (ud) ud[1] = ud[1] + 1; return false end, uh.crtbl))
  assert_nil( uh.db:rollback_hook(function (ud) ud[2] = ud[2] + 1 end, uh.crtbl))
  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() )
  assert_equal( 5, uh.crtbl[1] )
  assert_equal( 0, uh.crtbl[2] )
end



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








<







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







504
505
506
507
508
509
510

511
512
513
514
515
516
517
...
589
590
591
592
593
594
595































































596
597
598
599
600
601
602
----------------------------

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
................................................................................
  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 --
--------------------------------------------