-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebSqlTracer.js
216 lines (174 loc) · 8.98 KB
/
webSqlTracer.js
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
/* global define,module,require */
//Prints all webSQL and SQLite plugin queries to console.
//Influenced by WebSQL documentation found here: http://www.w3.org/TR/webdatabase/
//Uses https://github.com/umdjs/umd/blob/master/amdWeb.js to make it useful from browser globals as well as from AMD.
//Version 1.0.1.
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['underscore', 'jquery'], factory.bind(null, root));
} else if (typeof module !== "undefined" && module !== null && module.exports != null) { // jshint ignore:line
// Node module.
module.exports.webSqlTracer = factory(root, require('underscore'), require('jquery'));
} else {
// Browser globals
root.webSqlTracer = factory(root, root._, root.jQuery);
}
}(this,
//TODO: make it configurable to trace per table.
function (root, _, $) {
'use strict';
var originalExecuteSql,
originalTransaction,
originalReadTransaction,
originalOpenDatabase,
traceOnOpenSet = false,
dbSubscriptions = [], //dbSubscriptions[x] will contain db object. Index does not matter.
dbNames = [], //same indexes as dbSubscriptions.
traceInstalled = false,
namesCounter = {}; //nameCounters[name] counts previous name occurances
return {
//Parameters:
// tracePredicate is called with database name as parameter. Can be function or string. In later case it will be matched with === operator.
// traceCallback (optional) will be called with db parameter, after startTrace started on it.
traceOnOpen: function (tracePredicate, traceCallback) {
var that = this, tracePredicateStr;
if (traceOnOpenSet) {
throw new Error("Call stopTraceOnOpen() before calling traceOnOpen() for second time.");
}
traceOnOpenSet = true;
if (!originalOpenDatabase) {
originalOpenDatabase = root.openDatabase;
}
if (_.isString(tracePredicate)) {
tracePredicateStr = tracePredicate;
tracePredicate = function (name) { return name === tracePredicateStr; };
}
root.openDatabase = function (name, version) {
var db = originalOpenDatabase.apply(this, arguments);
if (tracePredicate(name)) {
that.startTrace(db, name, version).then(function () {
if (traceCallback) {
traceCallback(db);
}
});
}
return db;
};
},
stopTraceOnOpen: function () {
if (!traceOnOpenSet) {
throw new Error("Cannot call stopTraceOnOpen() before calling traceOnOpen().");
}
traceOnOpenSet = false;
root.openDatabase = originalOpenDatabase;
},
//Parameters:
// db is object returned from window.openDatabase().
// It can be any db, currently trace will be turned on globally for all dbs.
// dbName is optional.
// dbVersion is optional.
//returns promise
startTrace: function (db, dbName, dbVersion) {
var deferred = new $.Deferred(),
protoDatabase,
nameCount;
if (dbName) {
nameCount = namesCounter[dbName];
if (_.isUndefined(nameCount)) {
nameCount = 0;
}
namesCounter[dbName] = nameCount + 1;
if (nameCount > 0) {
dbName = dbName + "(" + nameCount + ")";
}
console.log("SQL TRACE : started tracing database " + dbName + (!_.isUndefined(dbVersion) ? " version '" + dbVersion + "'" : ""));
}
if (_.indexOf(dbSubscriptions, db) >= 0) {
throw new Error("startTrace was already started for this db");
}
if (!traceInstalled) {
db.transaction(function (t) { //just nop transaction to access Transaction.prototype.
var protoTransaction = Object.getPrototypeOf(t);
if (!originalExecuteSql) {
originalExecuteSql = protoTransaction.executeSql;
}
protoTransaction.executeSql = function (sqlStatement, args) {
var tx = this,
transactionDb = tx._transactionDb,
indexOfSubscription = _.indexOf(dbSubscriptions, transactionDb),
nameOfSubscription,
nameToPrint;
if (indexOfSubscription >= 0) {
nameOfSubscription = dbNames[indexOfSubscription];
nameToPrint = nameOfSubscription || "";
console.log("SQL TRACE " + nameToPrint + ": '" + sqlStatement + "'" + (args && !_.isEmpty(args) ? ", args=" + JSON.stringify(args) : ""));
}
originalExecuteSql.apply(this, arguments);
};
setTimeout(function () { //make sure resolve will execute last
deferred.resolve();
}, 0);
});
protoDatabase = Object.getPrototypeOf(db);
if (!originalTransaction) {
originalTransaction = protoDatabase.transaction;
originalReadTransaction = protoDatabase.readTransaction;
}
//Higher-order function to generate replacements for transaction() and readTransaction().
var makeTransaction = function (originalAnyTransaction) {
return function (callback, errorCallback, successCallback) {
var transactionDb = this;
originalAnyTransaction.call(this, function (t) {
//Add database property to t to make executeSql to know what db context we are in.
t._transactionDb = transactionDb;
callback(t); //Execute original callback parameter of transaction method.
}, errorCallback, successCallback);
};
};
protoDatabase.transaction = makeTransaction(originalTransaction);
protoDatabase.readTransaction = makeTransaction(originalReadTransaction);
traceInstalled = true;
} else {
deferred.resolve(); //resolve immediately
}
dbSubscriptions.push(db);
dbNames.push(dbName);
return deferred.promise();
},
//returns promise
stopTrace: function (db) {
var deferred = new $.Deferred(),
indexOfSubscription = _.indexOf(dbSubscriptions, db),
isLastSubscription,
dbName;
if (indexOfSubscription < 0) {
throw new Error("cannot stopTrace, trace was not started for this db");
}
isLastSubscription = (dbSubscriptions.length === 1);
if (isLastSubscription) {
//uninstall trace
var protoDatabase = Object.getPrototypeOf(db);
protoDatabase.transaction = originalTransaction;
protoDatabase.readTransaction = originalReadTransaction;
//no need to handle readTransaction separately, as t will be of type SQLTransactionCallback in both cases.
db.transaction(function (t) {
var protoTransaction = Object.getPrototypeOf(t);
protoTransaction.executeSql = originalExecuteSql;
deferred.resolve();
});
traceInstalled = false;
}
dbName = dbNames[indexOfSubscription];
console.log("SQL TRACE : stopped tracing database" + (dbName ? " " + dbName : ""));
//remove subscription from array
dbSubscriptions.splice(indexOfSubscription, 1);
dbNames.splice(indexOfSubscription, 1);
if (!isLastSubscription) {
deferred.resolve();
}
return deferred.promise();
}
};
}));