-
Notifications
You must be signed in to change notification settings - Fork 2
/
msqta.orm.websql.js
343 lines (289 loc) · 9.86 KB
/
msqta.orm.websql.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
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
MSQTA._ORM.WebSQL = {
Schema: function( schemaDefinition ) {
return MSQTA._Helpers.instantiateSchema( this.constructor._ORM, MSQTA._Schema.WebSQL, schemaDefinition, 'webSQL', arguments );
},
_open: function() {
// put in a close to handle various open at "the same time"
(function( self ) {
MSQTA._Helpers.blockWindow();
self._testigoDB = window.openDatabase( '__msqta__', 1, '', MSQTA._Helpers.webSQLSize );
self._testigoDB.transaction( function( tx ) {
if( self.devMode ) {
console.log( 'MSQTA.ORM: creating if not exists (first run) the table "databases" on "__msqta__" internal database' );
}
tx.executeSql( 'CREATE TABLE IF NOT EXISTS databases( id INTEGER PRIMARY KEY, name TEXT UNIQUE, schemas TEXT )' );
tx.executeSql( 'SELECT * FROM databases WHERE name = "' + self._name + '"', [], function( tx, results ) {
self._open2( results );
} );
} );
})( this );
},
_open2: function( results ) {
var rows = results.rows;
// store here all the schemaKeepTrack definitions
this._schemasDefinition = rows.length ? JSON.parse( rows.item( 0 ).schemas ) : {};
// this holds all the internal queries that are made when
// a schema is initialized, these queries are more important
// that this._queries in terms at the moment of execute the next query
this._queriesInternal = [];
if( this.devMode ) {
console.log( 'MSQTA.ORM: creating/opening the "' + this._name + '" user database' );
}
this._userDB = window.openDatabase( this._name, 1, '', MSQTA._Helpers.webSQLSize );
// create the __currents_ids__ table
(function( self ) {
self._userDB.transaction( function( tx ) {
if( self.devMode ) {
console.log( 'MSQTA.ORM: creating if not exists (first run) the table __currents_ids__ on "' + self._name + '" user database' );
}
tx.executeSql( 'CREATE TABLE IF NOT EXISTS __currents_ids__( id INTEGER PRIMARY KEY, table_name TEXT UNIQUE, last_id INTEGER )' );
}, null, function() {
MSQTA._Helpers.unblockWindow();
self._initCallback.call( self.initContext, true );
self._initSchemas();
} );
})( this );
},
_initSchema: function( Schema ) {
this._schemasToInit.push( Schema );
if( !this._isBlocked ) {
this._isBlocked = true;
this._initSchemas();
}
},
_initSchemas: function() {
var self = this,
Schema,
databaseName = this._name;
if( this._schemasToInit.length ) {
MSQTA._Helpers.blockWindow();
// call _init2 using the Schema instance
Schema = this._schemasToInit.shift();
Schema._init2();
} else {
MSQTA._Helpers.unblockWindow();
this._endSchemasInitialization();
}
},
_endSchemasInitialization: function() {
this._isBlocked = false;
},
_saveSchemaOnTestigoDatabase: function( callback, context, arg ) {
var self = this,
databaseName = this._name,
schemasDefinition = this._schemasDefinition;
if( this.devMode ) {
console.log( 'MSQTA-ORM: saving schema definition in the testigo database to keep tracking future changes on it' );
}
this._testigoDB.transaction( function( tx ) {
tx.executeSql( 'REPLACE INTO databases( name, schemas ) VALUES( "' + databaseName + '", ' + "'" + JSON.stringify( schemasDefinition ) + "'" + ')' );
}, null, function() {
// true for success
callback.call( context, arg );
} );
},
_deleteUserDatabase: function( callback, context ) {
this.destroy( callback, context );
},
_deleteUserSchema: function( Schema, queryData ) {
var self = this,
schemaName = Schema._name;
delete this._Schemas[schemaName];
delete this._schemasDefinition[schemaName];
MSQTA._Helpers.dimSchemaInstance( Schema );
this._userDB.transaction( function( tx ) {
if( self.devMode ) {
console.log( 'MSQTA-ORM: stop tracking "last inserted id" from this schema' );
}
// stop tracking last_id from this table
tx.executeSql( 'DELETE FROM __currents_ids__ WHERE table_name = "' + schemaName + '"' );
}, null, function() {
self._saveSchemaOnTestigoDatabase( queryData.userCallback, queryData.userContext, true );
} );
},
/**
* @context SQLTransaction
*/
_error: function( error ) {
// if we use a throw Error the application will die
console.error( 'MSQTA-ORM: query has failed: \n\t' + 'code: ' + error.code + '\n\t' + 'message: ' + error.message );
},
/***************************************/
_transaction: function( queryData ) {
var callback = queryData.userCallback,
context = queryData.userContext;
// use default callback
if( !callback ) {
queryData.userCallback = MSQTA._Helpers.defaultCallback;
}
// use window as context
if( !context ) {
queryData.userContext = window;
}
// only allow a query per time
if( this._isWaiting ) {
if( queryData.isInternal ) {
this._queriesInternal.push( queryData );
} else {
this._queries.push( queryData );
}
return;
}
this._isWaiting = true;
MSQTA._Helpers.blockWindow();
this._transaction2( queryData );
},
_transaction2: function( queryData ) {
// save a refenrece for when the transaction is done
this._lastQuery = queryData;
// save a reference used in the success and error functions
var self = this,
// update an update at time is executed, so we need to keep tracking manually the affected rows
rowsAffected = 0,
// keep track of all new inserted ids
allIDs = [],
query = queryData.query;
if( !Array.isArray( query ) ) {
query = [ query ];
}
// when you trigger multiple update queries, we need to keep
// track the rows affected in the operation
var noop = queryData.isUpdate ? function( tx, results ) {
rowsAffected += results.rowsAffected;
// you can insert multiples rows in an single call the put method
// we need to keep track these new ids
} : ( queryData.isInsert ? function( tx, results ) {
allIDs.push( results.insertId );
// do nothing
} : MSQTA._Helpers.noop );
var success = function( tx, results ) {
if( queryData.isUpdate ) {
// sum the last executed one
queryData.returnValue = results.rowsAffected + rowsAffected;
} else if( queryData.isInsert ) {
queryData.returnValue = results.insertId;
// only a one row has been inserted
if( !allIDs.length ) {
allIDs.push( results.insertId );
}
queryData.insertedIDs = allIDs;
} else if( queryData.isDelete ) {
queryData.returnValue = results.rowsAffected;
}
self._results( results );
};
var error = function( error ) {
self._error( error );
self._results( false );
};
this._userDB.transaction( function( tx ) {
var q,
l = query.length;
while( l-- ) {
q = query.shift();
if( self.devMode ) {
console.log( 'MSQTA-ORM: executing the query: \n\t' + q );
}
tx.executeSql( q, queryData.replacements ? queryData.replacements.shift() : [], l ? noop : success );
}
}, error );
},
/**
* @context SQLTransaction
*/
_results: function( results ) {
var queryData = this._lastQuery;
this._isWaiting = false;
// comes from _error()
if( !results ) {
queryData.returnValue = false;
}
// still more processing (only select clauses falls here)
if( queryData.internalCallback ) {
// go to the original caller
queryData.internalCallback.call( queryData.internalContext, results, queryData );
// get back with the user
} else {
// only delete, update, insert quries falls here
queryData.userCallback.call( queryData.userContext, queryData.returnValue, queryData.insertedIDs );
}
this._continue();
},
_continue: function() {
MSQTA._Helpers.unblockWindow();
if( !this._isWaiting ) {
// more queries to be executed in the queue
if( this._queriesInternal.length ) {
this._transaction( this._queriesInternal.shift() );
} else if( this._queries.length && !this._isBlocked ) {
this._transaction( this._queries.shift() );
}
}
},
/***************************************/
/***************************************/
batch: function( data, callback, context ) {
var databaseName = this._name,
batchData;
if( !Array.isArray( data ) || !data.length ) {
MSQTA._Errors.batch1( databaseName, data );
}
if( !callback ) {
callback = MSQTA._Helpers.defaultCallback;
}
if( !context ) {
context = window;
}
// agrup arguments for a better manipulation
batchData = {
data: data,
callback: callback,
context: context
};
if( this._isBatchMode ) {
this._batchsStack.push( batchData );
return;
}
// start batch mode, this means that the methods set, put and del
// will not execute the query, instead them will be return the querty string
this._isBatchMode = true;
this._batch( batchData );
},
_batch: function( batchData ) {
var data = batchData.data,
typeValids = [ 'set', 'put', 'del' ],
queryData, Schema, type,
i = 0, l = data.length;
for( ; i < l; i++ ) {
queryData = data[i];
Schema = queryData.schema;
if( !( Schema instanceof MSQTA._Schema ) ) {
MSQTA._Errors.batch2( Schema );
}
type = queryData.type.toLowerCase();
if( typeValids.indexOf( type ) === -1 ) {
MSQTA._Errors.batch3( type );
}
// save the queries
this._queries.push( Schema[type]( queryData.data ) );
}
// the last one will the return point
var t = this._queries[this._queries.length-1];
t.userCallback = batchData.callback;
t.userContext = batchData.context;
this._isBatchMode = false;
// exec the queries
this._continue();
// this happends when batch is called when another
// batch process is running
if( this._batchsStack.length ) {
this._batch( this._batchsStack.shift() );
}
},
/***************************************/
/***************************************/
destroy: function( callback, context ) {
console.error( 'MSQTA: destroy: deleting a database is not implemented in webSQL standard and will never do.\n To delete a database you need to do manually.' );
( callback || MSQTA._Helpers.noop ).call( context || window, false );
}
};