This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathsqlite-internal.ios.js
1019 lines (903 loc) · 31.8 KB
/
sqlite-internal.ios.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************************
* (c) 2015-2021 Master Technology
* Licensed under the MIT license or contact me for a support, changes, enhancements,
* and/or if you require a commercial licensing
*
* Any questions please feel free to email me or put a issue up on github
* [email protected] http://nativescript.tools
* Version 2.7.0 - iOS
***********************************************************************************/
/* global global, require, module */
"use strict";
const fs = require('@nativescript/core/file-system');
/* jshint undef: true, camelcase: false */
/* global Promise, NSFileManager, NSBundle, NSString, interop, sqlite3_open_v2, sqlite3_close, sqlite3_prepare_v2, sqlite3_step,
sqlite3_finalize, sqlite3_bind_null, sqlite3_bind_text, sqlite3_column_type, sqlite3_column_int64,
sqlite3_column_double, sqlite3_column_text, sqlite3_column_count, sqlite3_column_name, sqlite3_bind_blob */
let _DatabasePluginInits = [];
const TRANSIENT = new interop.Pointer(-1);
/***
* Converts a string to a UTF-8 char array and wraps it in an adopted pointer
* (which is GC managed and kept alive until it's referenced by a variable).
* The reason for doing it is that iOS runtime marshals JS string arguments via
* temporary buffers which are deallocated immediately after the call returns. In
* some cases however, we need them to stay alive for subsequent native calls
* because otherwise attempts to read the freed memory may lead to unpredictable
* app crashes.
* E.g. `sqlite3_step` function happens to use the stored `char *` passed as
* `dbname` to `sqlite3_open_v2`.
* @param str
* @returns {AdoptedPointer} object
*/
function toCharPtr(str) {
const objcStr = NSString.stringWithString(str);
// UTF8 strings can be encoded in 4 byte representing each character
// We are wasting a few bytes of memory, just to make sure we have enough room to copy it...
const bufferSize = ((str.length) * 4) + 1;
const buffer = interop.alloc(bufferSize);
objcStr.getCStringMaxLengthEncoding(buffer, bufferSize, NSUTF8StringEncoding);
return buffer;
}
/***
* Creates a Cursor Tracking Statement for reading result sets
* @param statement
* @param resultType
* @param valuesType
* @constructor
*/
function CursorStatement(statement, resultType, valuesType) {
this.statement = statement;
this.resultType = resultType;
this.valuesType = valuesType;
this.built = false;
this.columns = [];
}
//noinspection JSValidateJSDoc
/***
* Database Constructor
* @param dbname - Database Name
* @param options - options
* @param callback - Callback when Done
* @returns {Promise} object
* @constructor
*/
function Database(dbname, options, callback) {
if (!(this instanceof Database)) { // jshint ignore:line
//noinspection JSValidateTypes
return new Database(dbname, options, callback);
}
this._messageHandlers = [];
this._isOpen = false;
this._resultType = Database.RESULTSASARRAY;
this._valuesType = Database.VALUESARENATIVE;
if (typeof options === 'function') {
callback = options;
options = {};
} else {
options = options || {};
}
this._options = options;
// Check to see if it has a path, or if it is a relative dbname
// DBNAME = "" - is a Temporary Database
// DBNAME = ":memory:" - is a Memory only database
if (dbname !== "" && dbname !== ":memory:") {
let path;
if (dbname.indexOf('/') === -1) {
// noinspection JSUnresolvedVariable, JSUnresolvedFunction
path = fs.knownFolders.documents().path;
dbname = path + '/' + dbname;
} else {
path = dbname.substr(0, dbname.lastIndexOf('/') + 1);
}
// Create "databases" folder if it is missing. This causes issues on Emulators if it is missing
// So we create it if it is missing
try {
// noinspection JSUnresolvedVariable
if (!fs.File.exists(path)) {
//noinspection JSUnresolvedFunction
const fileManager = iosProperty(NSFileManager, NSFileManager.defaultManager);
//noinspection JSUnresolvedFunction
if (!fileManager.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(path, true, null, null)) {
console.warn("SQLITE.CONSTRUCTOR - Creating DB Folder Error");
}
}
}
catch (err) {
console.warn("SQLITE.CONSTRUCTOR - Creating DB Folder Error", err);
}
}
this._dbnamePtr = toCharPtr(dbname);
const self = this;
//noinspection JSUnresolvedFunction
return new Promise(function (resolve, reject) {
let error;
try {
let flags = 0;
if (typeof options.iosFlags !== 'undefined') {
flags = options.iosFlags;
}
self._db = new interop.Reference();
if (options && options.readOnly) {
// SQLITE_OPEN_FULLMUTEX = 65536, SQLITE_OPEN_READONLY = 1 ---- 1 | 65536 = 65537
error = sqlite3_open_v2(self._dbnamePtr, self._db, 65537 | flags, null);
} else {
// SQLITE_OPEN_FULLMUTEX = 65536, SQLITE_OPEN_CREATE = 4, SQLITE_OPEN_READWRITE = 2 --- 4 | 2 | 65536 = 65542
error = sqlite3_open_v2(self._dbnamePtr, self._db, 65542 | flags, null);
}
self._db = self._db.value;
} catch (err) {
if (callback) { callback(err, null); }
reject(err);
return;
}
if (error) {
if (callback) { callback(error, null); }
reject(error);
return;
}
self._isOpen = true;
let doneCnt = _DatabasePluginInits.length, doneHandled = 0;
const done = function(err) {
if (err) {
doneHandled = doneCnt; // We don't want any more triggers after this
if (callback) { callback(err, null); }
reject(err);
return;
}
doneHandled++;
if (doneHandled === doneCnt) {
if (callback) { callback(null, self); }
resolve(self);
}
};
if (doneCnt) {
try {
for (let i = 0; i < doneCnt; i++) {
_DatabasePluginInits[i].call(self, options, done);
}
} catch (err) {
done(err);
}
} else {
if (callback) { callback(null, self); }
resolve(self);
}
});
}
/***
* Constant that this structure is a sqlite structure
* @type {boolean}
*/
Database.prototype._isSqlite = true;
/***
* This gets or sets the database version
* @param valueOrCallback to set or callback(err, version)
* @returns Promise
*/
Database.prototype.version = function(valueOrCallback) {
return new Promise((resolve, reject) => {
if (typeof valueOrCallback === 'function') {
this.get('PRAGMA user_version', function (err, data) {
const value = data && parseInt(data[0], 10);
valueOrCallback(err, value);
if (err) { reject(err); }
else resolve(value);
}, Database.RESULTSASARRAY);
} else if (!isNaN(valueOrCallback + 0)) {
this.execSQL('PRAGMA user_version=' + (valueOrCallback + 0).toString()).then(resolve, reject);
} else {
this.get('PRAGMA user_version', undefined, undefined, Database.RESULTSASARRAY).then((data) => {
resolve(data && parseInt(data[0], 10))
}).catch(reject);
}
});
};
/***
* Is the database currently open
* @returns {boolean} - true if the db is open
*/
Database.prototype.isOpen = function() {
return this._isOpen;
};
/***
* Gets/Sets whether you get Arrays or Objects for the row values
* @param value - Database.RESULTSASARRAY or Database.RESULTSASOBJECT
* @returns {number} - Database.RESULTSASARRAY or Database.RESULTSASOBJECT
*/
Database.prototype.resultType = function(value) {
if (value === Database.RESULTSASARRAY) {
this._resultType = Database.RESULTSASARRAY;
} else if (value === Database.RESULTSASOBJECT) {
this._resultType = Database.RESULTSASOBJECT;
}
return this._resultType;
};
/***
* Gets/Sets whether you get Native or Strings for the row values
* @param value - Database.VALUESARENATIVE or Database.VALUESARESTRINGS
* @returns {number} - Database.VALUESARENATIVE or Database.VALUESARESTRINGS
*/
Database.prototype.valueType = function(value) {
if (value === Database.VALUESARENATIVE) {
this._valuesType = Database.VALUESARENATIVE;
} else if (value === Database.VALUESARESTRINGS) {
this._valuesType = Database.VALUESARESTRINGS;
}
return this._valuesType;
};
/**
* Dummy transaction function for public version
* @param callback
* @returns {Promise<T>}
*/
Database.prototype.begin = function(callback) {
throw new Error("Transactions are a Commercial version feature.");
};
/**
* Dummy prepare function for public version
* @param sql
* @returns {*}
*/
Database.prototype.prepare = function(sql) {
throw new Error("Prepared statements are a Commercial version feature.");
};
// noinspection JSUnusedLocalSymbols
/**
* Dummy sync enable tracking function for public version
* @returns {*}
*/
Database.prototype.enableTracking = function(tables, options, callback) {
throw new Error("Table sync is a Commercial version feature.");
};
/***
* Closes this database, any queries after this will fail with an error
* @param callback
* @returns Promise
*/
Database.prototype.close = function(callback) {
const self = this;
return new Promise( function (resolve, reject) {
if (!self._isOpen) {
if (callback) {
callback('SQLITE.CLOSE - Database is already closed');
}
reject('SQLITE.CLOSE - Database is already closed');
return;
}
sqlite3_close(self._db);
self._db = null;
self._isOpen = false;
if (self._dbnamePtr != null) {
interop.free(self._dbnamePtr);
self._dbnamePtr = null;
}
if (callback) {
callback(null, null);
}
resolve();
});
};
/***
* Exec SQL
* @param sql - sql to use
* @param params - optional array of parameters
* @param callback - (err, result) - can be last_row_id for insert, and rows affected for update/delete
* @returns Promise
*/
Database.prototype.execSQL = function(sql, params, callback) {
if (typeof params === 'function') {
callback = params;
params = undefined;
}
const self = this;
return new Promise( function(resolve, reject) {
let hasCallback = true;
if (typeof callback !== 'function') {
callback = reject;
hasCallback = false;
}
if (!self._isOpen) {
callback("SQLITE.EXECSQL - Database is not open");
return;
}
// Need to see if we have to run any status queries afterwords
let flags = 0;
let test = sql.trim().substr(0, 7).toLowerCase();
if (test === 'insert ') {
flags = 1;
} else if (test === 'update ' || test === 'delete ') {
flags = 2;
}
let res;
try {
let statement = new interop.Reference();
res = sqlite3_prepare_v2(self._db, sql, -1, statement, null);
statement = statement.value;
if (res) {
callback("SQLITE.ExecSQL Failed Prepare: " + res);
return;
}
if (params !== undefined) {
if (!self._bind(statement, params)) {
sqlite3_finalize(statement);
callback("SQLITE.ExecSQL Bind Error");
return;
}
}
let result = sqlite3_step(statement);
sqlite3_finalize(statement);
if (result && result !== 100 && result !== 101) {
callback("SQLITE.ExecSQL Failed " + result);
return;
}
} catch (Err) {
callback(Err, null);
return;
}
switch (flags) {
case 0:
if (hasCallback) {
callback();
}
resolve();
break;
case 1:
self.get('select last_insert_rowid()', function (err, data) {
if (hasCallback) {
callback(err, data && data[0]);
}
if (err) {
reject(err);
} else {
resolve(data && data[0]);
}
}, Database.RESULTSASARRAY | Database.VALUESARENATIVE);
break;
case 2:
self.get('select changes()', function (err, data) {
if (hasCallback) {
callback(err, data && data[0]);
}
if (err) {
reject(err);
} else {
resolve(data && data[0]);
}
}, Database.RESULTSASARRAY | Database.VALUESARENATIVE);
break;
default:
if (hasCallback) {
callback();
}
resolve();
}
});
};
/***
* Get the first record result set
* @param sql - sql to run
* @param params - optional
* @param callback - callback (error, results)
* @param mode - allows you to manually override the results set to be a array or object
* @returns Promise
*/
Database.prototype.get = function(sql, params, callback, mode) {
if (typeof params === 'function') {
mode = callback;
callback = params;
params = undefined;
}
const self = this;
return new Promise( function (resolve, reject) {
let hasCallback = true;
if (typeof callback !== 'function') {
callback = reject;
hasCallback = false;
}
if (!self._isOpen) {
callback("SQLITE.GET - Database is not open");
return;
}
let cursor;
try {
let statement = new interop.Reference();
let res = sqlite3_prepare_v2(self._db, sql, -1, statement, null);
if (res) {
callback("SQLITE.GET Failed Prepare: " + res);
return;
}
statement = statement.value;
let cursorStatement = new CursorStatement(statement, self._resultType, self._valuesType);
if (params !== undefined) {
if (!self._bind(statement, params)) {
sqlite3_finalize(statement);
callback("SQLITE.GET Bind Error");
return;
}
}
let result = sqlite3_step(statement);
if (result === 100) {
cursor = self._getResults(cursorStatement, mode);
}
sqlite3_finalize(statement);
if (result && result !== 100 && result !== 101) {
callback("SQLITE.GET - Step Error" + result);
return;
}
} catch (err) {
callback(err);
return;
}
// No Records
if (!cursor) {
if (hasCallback) {
callback(null, null);
}
resolve(null);
return;
}
if (hasCallback) {
callback(null, cursor);
}
resolve(cursor);
});
};
/***
* This returns the entire result set in a array of rows
* @param sql - Sql to run
* @param params - optional
* @param callback - (err, results)
* @param mode - set a specific return mode
* @returns Promise
*/
Database.prototype.all = function(sql, params, callback, mode) {
if (typeof params === 'function') {
callback = params;
params = undefined;
}
const self = this;
return new Promise(function(resolve, reject) {
let hasCallback = true;
if (typeof callback !== 'function') {
callback = reject;
hasCallback = false;
}
if (!self._isOpen) {
callback("SQLITE.ALL - Database is not open");
return;
}
let rows = [], res;
try {
let statement = new interop.Reference();
res = sqlite3_prepare_v2(self._db, sql, -1, statement, null);
if (res) {
callback("SQLITE.ALL - Prepare Error " + res);
return;
}
statement = statement.value;
let cursorStatement = new CursorStatement(statement, self._resultType, self._valuesType);
if (params !== undefined) {
if (!self._bind(statement, params)) {
sqlite3_finalize(statement);
callback("SQLITE.ALL Bind Error");
return;
}
}
let result;
do {
result = sqlite3_step(statement);
if (result === 100) {
let cursor = self._getResults(cursorStatement, mode);
if (cursor) {
rows.push(cursor);
}
} else if (result && result !== 101) {
sqlite3_finalize(statement);
callback("SQLITE.ALL - Database Error" + result);
return;
}
} while (result === 100);
sqlite3_finalize(statement);
} catch (err) {
callback(err, null);
return;
}
// No Records
if (rows.length === 0) {
if (hasCallback) {
callback(null, []);
}
resolve([]);
return;
}
if (hasCallback) {
callback(null, rows);
}
resolve(rows);
});
};
/***
* This sends each row of the result set to the "Callback" and at the end calls the complete callback upon completion
* @param sql - sql to run
* @param params - optional
* @param callback - callback (err, rowsResult)
* @param complete - callback (err, recordCount)
* @returns {Promise}
*/
Database.prototype.each = function(sql, params, callback, complete) {
if (typeof params === 'function') {
complete = callback;
callback = params;
params = undefined;
}
// Callback is required
if (typeof callback !== 'function') {
throw new Error("SQLITE.EACH - requires a callback");
}
const self = this;
return new Promise (function(resolve, reject) {
// Set the error Callback
let errorCB = complete || callback;
let count = 0, res;
try {
let statement = new interop.Reference();
res = sqlite3_prepare_v2(self._db, sql, -1, statement, null);
if (res) {
errorCB("SQLITE.EACH Error in Prepare" + res);
reject("SQLITE.EACH Error in Prepare" + res);
return;
}
statement = statement.value;
let cursorStatement = new CursorStatement(statement, self._resultType, self._valuesType);
if (params !== undefined) {
if (!self._bind(statement, params)) {
sqlite3_finalize(statement);
errorCB("SQLITE.EACH Bind Error");
reject("SQLITE.EACH Bind Error");
return;
}
}
let result;
do {
result = sqlite3_step(statement);
if (result === 100) {
let cursor = self._getResults(cursorStatement);
if (cursor) {
count++;
callback(null, cursor);
}
} else if (result && result !== 101) {
sqlite3_finalize(statement);
errorCB("SQLITE.EACH - Database Error " + result);
reject("SQLITE.EACH - Database Error " + result);
return;
}
} while (result === 100);
sqlite3_finalize(statement);
} catch (err) {
errorCB(err, null);
reject(err);
return;
}
if (complete) {
complete(null, count);
}
resolve(count);
});
};
/**
* Binds the Parameters in a Statement
* @param statement
* @param params
* @private
*/
Database.prototype._bind = function(statement, params) {
let param, res;
if (Array.isArray(params)) {
const count = params.length;
for (let i=0; i<count; ++i) {
if (params[i] == null) { // jshint ignore:line
res = sqlite3_bind_null(statement, i + 1);
} else if (params[i].isKindOfClass && (params[i].isKindOfClass(NSData.class()))) {
const obj = params[i];
res = sqlite3_bind_blob(statement, i + 1, obj.bytes, obj.length, TRANSIENT);
} else {
param = params[i].toString();
res = sqlite3_bind_text(statement, i+1, param, -1, TRANSIENT );
}
if (res) {
console.error("SQLITE.Binding Error ", res);
return false;
}
}
} else {
if (params == null) { // jshint ignore:line
res = sqlite3_bind_null(statement, 1);
} else if (params.isKindOfClass && (params.isKindOfClass(NSData.class()))) {
const obj = params;
res = sqlite3_bind_blob(statement, 1, obj.bytes, obj.length, TRANSIENT);
} else {
param = params.toString();
res = sqlite3_bind_text(statement, 1, param, -1, TRANSIENT );
}
if (res) {
console.error("SQLITE.Binding Error ", res);
return false;
}
}
return true;
};
Database.prototype._getNativeResult = function(statement, column) {
const resultType = sqlite3_column_type(statement, column);
switch (resultType) {
case 1: // Int
return sqlite3_column_int64(statement, column);
case 2: // Float
return sqlite3_column_double(statement, column);
case 3: // Text
//noinspection JSUnresolvedFunction
return NSString.stringWithUTF8String(sqlite3_column_text(statement, column)).toString();
case 4: // Blob
return NSData.dataWithBytesLength(sqlite3_column_blob(statement, column), sqlite3_column_bytes(statement, column));
case 5: // Null
return null;
default:
//noinspection JSUnresolvedFunction
return NSString.stringWithUTF8String(sqlite3_column_text(statement, column)).toString();
}
};
Database.prototype._getStringResult = function(statement, column) {
const resultType = sqlite3_column_type(statement, column);
switch (resultType) {
case 1: // Int
//return sqlite3_column_int(statement, column).toString();
return NSString.stringWithUTF8String(sqlite3_column_text(statement, column)).toString();
case 2: // Float
//return sqlite3_column_double(statement, column).toString();
return NSString.stringWithUTF8String(sqlite3_column_text(statement, column)).toString();
case 3: // Text
//noinspection JSUnresolvedFunction
return NSString.stringWithUTF8String(sqlite3_column_text(statement, column)).toString();
case 4: // Blob
return NSData.dataWithBytesLength(sqlite3_column_blob(statement, column), sqlite3_column_bytes(statement, column));
case 5: // Null
return null;
default:
//noinspection JSUnresolvedFunction
return NSString.stringWithUTF8String(sqlite3_column_text(statement, column)).toString();
}
};
Database.prototype._getResults = function(cursorStatement, mode) {
let resultType, valueType;
let statement = cursorStatement.statement;
let i;
if (!mode) {
resultType = cursorStatement.resultType;
valueType = cursorStatement.valuesType;
} else {
resultType = (mode & (Database.RESULTSASARRAY | Database.RESULTSASOBJECT));
valueType = (mode & (Database.VALUESARENATIVE | Database.VALUESARESTRINGS));
if (resultType <= 0) {
resultType = cursorStatement.resultType;
}
if (valueType <= 0) {
valueType = cursorStatement.valuesType;
}
}
// Track this statements information so we don't have to build it each time
if (!cursorStatement.built) {
cursorStatement.count = sqlite3_column_count(statement);
if (resultType === Database.RESULTSASOBJECT) {
for (i=0;i<cursorStatement.count;i++) {
//noinspection JSUnresolvedFunction
let cn = NSString.stringWithUTF8String(sqlite3_column_name(statement, i)).toString();
if (!cn || cursorStatement.columns.indexOf(cn) >= 0) {
cn = "column"+i;
}
cursorStatement.columns.push(cn);
}
}
cursorStatement.built=true;
}
let cnt = cursorStatement.count, data;
if (cnt === 0) { return null; }
if (resultType === Database.RESULTSASARRAY) {
data = [];
if (valueType === Database.VALUESARESTRINGS) {
for (i = 0; i < cnt; i++) {
data.push(this._getStringResult(statement, i));
}
} else {
for (i = 0; i < cnt; i++) {
data.push(this._getNativeResult(statement, i));
}
}
return data;
} else {
let colName = cursorStatement.columns;
data = {};
if (valueType === Database.VALUESARESTRINGS) {
for (i = 0; i < cnt; i++) {
data[colName[i]] = this._getStringResult(statement, i);
}
} else {
for (i = 0; i < cnt; i++) {
data[colName[i]] = this._getNativeResult(statement, i);
}
}
return data;
}
};
Database.prototype.notify = function(type, message) {
if (typeof global.postMessage === 'function') {
postMessage({id: -2, type: type, message: message});
} else {
console.error("SQLite: Not in a thread");
// Local Notify
this._notify(type, message);
}
};
Database.prototype._notify = function(type, message) {
if (type == null || typeof this._messageHandlers[type] === "undefined") {
return;
}
let handlers = this._messageHandlers[type];
try {
for (let i = 0; i < handlers; i++) {
handlers[i](message, type, this);
}
} catch (err) {
console.error("SQLite: Error in user code ", err, err.stack);
}
}
Database.prototype.addMessageHandler = function(type, callback) {
if (typeof this._messageHandlers[type] === 'undefined') {
this._messageHandlers[type] = [];
}
this._messageHandlers[type].push(callback);
};
Database.prototype.removeMessageHandler = function(type, callback) {
if (type != null && typeof this._messageHandlers[type] === "undefined") {
console.error("SQLite: This message handler " + type + " does not exist.");
return;
}
if (callback) {
// Remove all message handles that match this callback & this db...
for (let i = 0; i < this._messageHandlers[type].length; i++) {
if (this._messageHandlers[type][i].callback === callback) {
this._messageHandlers[type].splice(i, 1);
i--;
}
}
} else if (type != null) {
// Remove all message handlers for this type
this._messageHandlers[type] = [];
} else {
// Remove all message handlers for this database
this._messageHandlers = [];
}
};
/***
* Is this a SQLite object
* @param obj - possible sqlite object to check
* @returns {boolean}
*/
Database.isSqlite = function(obj) {
return obj && obj._isSqlite;
};
Database.exists = function(name) {
if (name.indexOf('/') === -1) {
name = fs.knownFolders.documents().path + '/' + name;
}
//noinspection JSUnresolvedFunction
const fileManager = iosProperty(NSFileManager, NSFileManager.defaultManager);
return fileManager.fileExistsAtPath(name);
};
Database.deleteDatabase = function(name) {
//noinspection JSUnresolvedFunction
const fileManager = iosProperty(NSFileManager, NSFileManager.defaultManager);
let path;
if (name.indexOf('/') === -1) {
path = fs.knownFolders.documents().path + '/';
} else {
path = name.substr(0, name.lastIndexOf('/') + 1);
name = name.substr(path.length);
}
if (!fileManager.fileExistsAtPath(path + name)) { return; }
// Need to remove the trailing .sqlite
let idx = name.lastIndexOf('.');
if (idx) {
name = name.substr(0,idx);
}
let files = fileManager.contentsOfDirectoryAtPathError(path, null);
if (!files) {
return;
}
for (let i = 0; i < files.count; i++) {
const fileName = files.objectAtIndex(i);
if (fileName.indexOf(name) !== -1) {
fileManager.removeItemAtPathError(path + fileName, null);
}
}
};
Database.copyDatabase = function (name, destName) {
//noinspection JSUnresolvedFunction
const fileManager = iosProperty(NSFileManager, NSFileManager.defaultManager);
let path;
if (name.indexOf('/') === -1) {
path = fs.knownFolders.documents().path + '/';
} else {
path = name.substr(0, name.lastIndexOf('/') + 1);
name = name.substr(path.length);
}
let source = fs.knownFolders.currentApp().path + '/' + name;
if (!destName) { destName = name; }
else if (destName.indexOf("/") >= 0) {
destName = destName.substring(destName.lastIndexOf('/') + 1);
}
let destination = path + destName;
fileManager.copyItemAtPathToPathError(source, destination, null);
};
function UsePlugin(loadedSrc, DBModule) {
if (loadedSrc.prototypes) {
for (let key in loadedSrc.prototypes) {
if (!loadedSrc.prototypes.hasOwnProperty(key)) { continue; }
if (DBModule.prototype[key]) {
DBModule.prototype["_"+key] = DBModule.prototype[key];
}
DBModule.prototype[key] = loadedSrc.prototypes[key];
}
}
if (loadedSrc.statics) {
for (let key in loadedSrc.statics) {
if (!loadedSrc.statics.hasOwnProperty(key)) { continue; }
DBModule[key] = loadedSrc.statics[key];
}
}
if (typeof loadedSrc.init === 'function') {
_DatabasePluginInits.push(loadedSrc.init);
}
}
function iosProperty(_this, property) {
if (typeof property === "function") {
// xCode < 8
return property.call(_this);
} else {
// xCode >= 8
return property;
}
}
// Literal Defines
Database.prototype.RESULTSASARRAY = Database.RESULTSASARRAY = 1;
Database.prototype.RESULTSASOBJECT = Database.RESULTSASOBJECT = 2;
Database.prototype.VALUESARENATIVE = Database.VALUESARENATIVE = 4;
Database.prototype.VALUESARESTRINGS = Database.VALUESARESTRINGS = 8;
/** These are optional plugins, must have a static require statement for webpack **/
TryLoadingCommercialPlugin();
TryLoadingEncryptionPlugin();
TryLoadingSyncPlugin();
function TryLoadingCommercialPlugin() {
try {
const sqlCom = require('nativescript-sqlite-commercial');
UsePlugin(sqlCom, Database);
}
catch (e) { /* Do Nothing if it doesn't exist as it is an optional plugin */
}
}
function TryLoadingEncryptionPlugin() {
try {