@@ -7,37 +7,66 @@ exports.test = function(SQL, assert){
77 "INSERT INTO consoles VALUES (2, 'Microsoft', 'Xbox');"
88 ) ;
99
10- // {operation: undefined, tableName: undefined, rowId: undefined};
10+ // {operation: undefined, databaseName: undefined, tableName: undefined, rowId: undefined};
1111 var updateHookCalls = [ ]
1212
13- db . updateHook ( function ( operation , tableName , rowId ) {
14- updateHookCalls . push ( { operation, tableName, rowId} ) ;
13+ db . updateHook ( function ( operation , databaseName , tableName , rowId ) {
14+ updateHookCalls . push ( { operation, databaseName , tableName, rowId} ) ;
1515 } ) ;
1616
1717 // INSERT
1818 db . exec ( "INSERT INTO consoles VALUES (3, 'Sega', 'Saturn');" ) ;
1919
20- assert . deepEqual ( updateHookCalls , [ { operation : 'insert' , tableName : 'consoles' , rowId : 3 } ] , 'insert a single row' ) ;
21- updateHookCalls = [ ]
20+ assert . deepEqual ( updateHookCalls , [
21+ { operation : "insert" , databaseName : "main" , tableName : "consoles" , rowId : 3 }
22+ ] , "insert a single row" ) ;
2223
2324 // UPDATE
25+ updateHookCalls = [ ]
2426 db . exec ( "UPDATE consoles SET name = 'Playstation 5' WHERE id = 1" ) ;
2527
26- assert . deepEqual ( updateHookCalls , [ { operation : 'update' , tableName : 'consoles' , rowId : 1 } ] , 'update a single row' ) ;
27- updateHookCalls = [ ]
28+ assert . deepEqual ( updateHookCalls , [
29+ { operation : "update" , databaseName : "main" , tableName : "consoles" , rowId : 1 }
30+ ] , "update a single row" ) ;
2831
2932 // UPDATE (multiple rows)
33+ updateHookCalls = [ ]
3034 db . exec ( "UPDATE consoles SET name = name + ' [legacy]' WHERE id IN (2,3)" ) ;
3135
3236 assert . deepEqual ( updateHookCalls , [
33- { operation : 'update' , tableName : 'consoles' , rowId : 2 } ,
34- { operation : 'update' , tableName : 'consoles' , rowId : 3 } ,
35- ] , 'update two rows' ) ;
36- updateHookCalls = [ ]
37+ { operation : "update" , databaseName : "main" , tableName : "consoles" , rowId : 2 } ,
38+ { operation : "update" , databaseName : "main" , tableName : "consoles" , rowId : 3 } ,
39+ ] , "update two rows" ) ;
3740
3841 // DELETE
42+ updateHookCalls = [ ]
3943 db . exec ( "DELETE FROM consoles WHERE company = 'Sega'" ) ;
4044
41- assert . deepEqual ( updateHookCalls , [ { operation : 'delete' , tableName : 'consoles' , rowId : 3 } ] , 'delete a single row' ) ;
45+ assert . deepEqual ( updateHookCalls , [
46+ { operation : "delete" , databaseName : "main" , tableName : "consoles" , rowId : 3 }
47+ ] , "delete a single row" ) ;
48+
49+ // UNREGISTER
4250 updateHookCalls = [ ]
51+
52+ db . updateHook ( null ) ;
53+
54+ db . exec ( "DELETE FROM consoles WHERE company = 'Microsoft'" ) ;
55+
56+ assert . deepEqual ( updateHookCalls , [ ] , "unregister the update hook" ) ;
57+
58+ // REGISTER AGAIN
59+ updateHookCalls = [ ]
60+
61+ db . updateHook ( function ( operation , databaseName , tableName , rowId ) {
62+ updateHookCalls . push ( { operation, databaseName, tableName, rowId} ) ;
63+ } ) ;
64+
65+ // need a where clause, just running "DELETE FROM consoles" would result in
66+ // a TRUNCATE and not yield any update hook callbacks
67+ db . exec ( "DELETE FROM consoles WHERE id > 0" ) ;
68+
69+ assert . deepEqual ( updateHookCalls , [
70+ { operation : 'delete' , databaseName : 'main' , tableName : 'consoles' , rowId : 1 }
71+ ] , "register the update hook again" ) ;
4372}
0 commit comments