1
- const sqlite = require ( '@proplugins/ nativescript-sqlite' ) ;
1
+ const sqlite = require ( 'nativescript-sqlite' ) ;
2
2
const ObservableArray = require ( "@nativescript/core/data/observable-array" ) . ObservableArray ;
3
+ const FileSystemAccess = require ( "@nativescript/core/file-system/file-system-access" ) . FileSystemAccess ;
3
4
4
5
5
6
//var Tracing = require('./tracing.js');
6
7
//Tracing(sqlite, {ignore: ["close", "resultType", "valueType", "_toStringArray", "_getResultEngine"], disableAddedFunction: true});
7
8
8
- var dbname = 'name_db.sqlite' ;
9
- var db = null ;
10
- var page = null ;
9
+ let dbname = 'name_db.sqlite' ;
10
+ let db = null ;
11
+ let page = null ;
11
12
12
- var data = new ObservableArray ( ) ;
13
+ const data = new ObservableArray ( ) ;
13
14
14
15
if ( sqlite . HAS_COMMERCIAL ) {
15
16
console . log ( "Using Commercial" ) ;
@@ -36,34 +37,40 @@ if (sqlite.HAS_SYNC) {
36
37
}
37
38
38
39
39
- exports . pageLoaded = function ( args ) {
40
+ exports . pageLoaded = async function ( args ) {
40
41
page = args . object ;
41
42
page . bindingContext = { names : data } ;
42
43
43
44
if ( ! sqlite . exists ( dbname ) ) {
44
45
sqlite . copyDatabase ( dbname ) ;
45
46
}
46
- new sqlite ( dbname , { key : 'testing' , multithreading : ! ! sqlite . HAS_COMMERCIAL , migrate : true } , function ( err , dbConnection ) {
47
- if ( err ) {
48
- console . log ( err , err . stack ) ;
49
- }
50
- db = dbConnection ;
51
- db . resultType ( sqlite . RESULTSASOBJECT ) ;
47
+ try {
48
+ let myDb = await sqlite ( dbname , { key : 'testing' , multithreading : ! ! sqlite . HAS_COMMERCIAL , migrate : true } ,
49
+ ) ;
52
50
53
- db . version ( ) . then ( function ( results ) {
54
- console . log ( "User Version: " , results , typeof results , Number . isNumber ( results ) ) ; //, String.isString(results));
55
- } ) ;
51
+ let b = function ( err , dbConnection ) {
52
+ if ( err ) {
53
+ console . log ( err , err . stack ) ;
54
+ }
55
+ db = dbConnection ;
56
+ db . resultType ( sqlite . RESULTSASOBJECT ) ;
56
57
57
- if ( sqlite . HAS_ENCRYPTION ) {
58
- db . get ( "PRAGMA cipher_version;" ) . then ( function ( results ) {
59
- console . log ( "Cipher version" , results [ 'cipher_version' ] ) ;
58
+ db . version ( ) . then ( function ( results ) {
59
+ console . log ( "User Version: " , results , typeof results , Number . isNumber ( results ) ) ; //, String.isString(results));
60
60
} ) ;
61
- }
62
61
63
- reloadData ( ) ;
64
- } ) . catch ( function ( val ) {
65
- console . log ( "sqlite error" , val ) ;
66
- } ) ;
62
+ if ( sqlite . HAS_ENCRYPTION ) {
63
+ db . get ( "PRAGMA cipher_version;" ) . then ( function ( results ) {
64
+ console . log ( "Cipher version" , results [ 'cipher_version' ] ) ;
65
+ } ) ;
66
+ }
67
+
68
+ reloadData ( ) ;
69
+ } ;
70
+ b ( null , myDb ) ;
71
+ } catch ( err ) {
72
+ console . log ( "sqlite error" , err , err . stack ) ;
73
+ }
67
74
} ;
68
75
69
76
exports . addNewName = function ( ) {
@@ -144,7 +151,19 @@ function reloadData() {
144
151
} ) ;
145
152
}
146
153
154
+ // So we only keep one copy in memory; we cache this...
155
+ let blob ;
156
+ function loadBlob ( ) {
157
+ if ( blob != null ) { return ; }
158
+ let fsa = new FileSystemAccess ( ) ;
159
+ blob = fsa . readSync ( fsa . getCurrentAppPath ( ) + "/icon.sqlite" , ( err ) => {
160
+ console . error ( "Error" , err ) ;
161
+ } ) ;
162
+ }
163
+
164
+
147
165
function setupTests ( callback ) {
166
+ loadBlob ( ) ;
148
167
data . push ( { name : 'Creating tables and data...' , css : 'one' } ) ;
149
168
db . execSQL ( 'drop table if exists tests;' , function ( err ) {
150
169
if ( err ) { console . log ( "!---- Drop Err" , err ) ; }
@@ -177,21 +196,36 @@ function setupTests(callback) {
177
196
178
197
function checkRowOfData ( inData , validData ) {
179
198
if ( Array . isArray ( inData ) ) {
180
- for ( var i = 0 ; i < inData . length ; i ++ ) {
199
+ for ( let i = 0 ; i < inData . length ; i ++ ) {
181
200
if ( typeof inData [ i ] === "number" ) {
182
201
if ( inData [ i ] !== validData [ i ] ) {
183
- if ( inData [ i ] - 0.1 > validData [ i ] || inData [ i ] + 0.1 < validData [ i ] ) {
202
+ if ( inData [ i ] - 0.1 > validData [ i ] || inData [ i ] + 0.1 < validData [ i ] ) {
184
203
return ( { status : false , field : i } ) ;
185
204
}
186
205
}
187
206
} else {
188
207
if ( inData [ i ] !== validData [ i ] ) {
189
- return ( { status : false , field : i } ) ;
208
+ if ( inData [ i ] . count && inData [ i ] . count === validData [ i ] . count ) {
209
+ for ( let j = 0 ; j < inData [ i ] . count ; j ++ ) {
210
+ if ( inData [ i ] [ j ] !== validData [ i ] [ j ] ) {
211
+ return ( { status : false , field : i } ) ;
212
+ }
213
+ }
214
+ if ( inData [ i ] . length && inData [ i ] . length === validData [ i ] . length ) {
215
+ for ( let j = 0 ; j < inData [ i ] . length ; j ++ ) {
216
+ if ( inData [ i ] [ j ] !== validData [ i ] [ j ] ) {
217
+ return ( { status : false , field : i } ) ;
218
+ }
219
+ }
220
+ } else {
221
+ return ( { status : false , field : i } ) ;
222
+ }
223
+ }
190
224
}
191
225
}
192
226
}
193
227
} else {
194
- for ( var key in inData ) {
228
+ for ( let key in inData ) {
195
229
if ( inData . hasOwnProperty ( key ) ) {
196
230
if ( typeof inData [ key ] === "number" ) {
197
231
if ( inData [ key ] !== validData [ key ] ) {
@@ -215,9 +249,9 @@ function runATest(options, callback) {
215
249
//console.log("!-------------- Starting Test", options.name);
216
250
217
251
//data.push({name: "Starting test"+options.name});
218
- var checkResults = function ( err , inData ) {
252
+ const checkResults = function ( err , inData ) {
219
253
//console.log("!-------------- Checking Results", options.name, "Error: ", err, "Data:", inData);
220
- var passed = true ;
254
+ let passed = true ;
221
255
if ( err ) {
222
256
console . log ( "!------------ Error" , err . toString ( ) ) ;
223
257
data . push ( { name : options . name + " test failed with: " , css : 'one' } ) ;
@@ -236,8 +270,8 @@ function runATest(options, callback) {
236
270
return callback ( passed ) ;
237
271
}
238
272
//console.log("!------------ Data Returned", inData.length, inData);
239
- for ( var i = 0 ; i < inData . length ; i ++ ) {
240
- var result = checkRowOfData ( inData [ i ] , options . results [ i ] ) ;
273
+ for ( let i = 0 ; i < inData . length ; i ++ ) {
274
+ let result = checkRowOfData ( inData [ i ] , options . results [ i ] ) ;
241
275
if ( ! result . status ) {
242
276
passed = false ;
243
277
data . push ( { name : options . name + " test failed on row: " + i + ", field: " + result . field , css : 'one' } ) ;
@@ -248,17 +282,17 @@ function runATest(options, callback) {
248
282
callback ( passed ) ;
249
283
} ;
250
284
251
- var checkRow = 0 ;
252
- var checksPassed = true ;
253
- var checkEachResults = function ( err , inData ) {
285
+ let checkRow = 0 ;
286
+ let checksPassed = true ;
287
+ const checkEachResults = function ( err , inData ) {
254
288
if ( ! checksPassed ) return ;
255
289
if ( err ) {
256
290
data . push ( { name : options . name + " test failed with " + err . toString ( ) , css : 'one' } ) ;
257
291
console . log ( options . name + " test failed with " , err . toString ( ) ) ;
258
292
checksPassed = false ;
259
293
return ;
260
294
}
261
- var result = checkRowOfData ( inData , options . results [ checkRow ] ) ;
295
+ const result = checkRowOfData ( inData , options . results [ checkRow ] ) ;
262
296
if ( ! result . status ) {
263
297
checksPassed = false ;
264
298
data . push ( { name : options . name + " test failed on row: " + checkRow + ", field: " + result . field , css : 'one' } ) ;
@@ -470,7 +504,7 @@ function setupPreparedTests(callback) {
470
504
if ( err ) {
471
505
console . log ( "!---- Drop Err" , err ) ;
472
506
}
473
- db . execSQL ( 'create table preparetests (`int_field` integer, `num_field` numeric, `real_field` real, `text_field` text)' , function ( err ) {
507
+ db . execSQL ( 'create table preparetests (`int_field` integer, `num_field` numeric, `real_field` real, `text_field` text, `blob_field` blob )' , function ( err ) {
474
508
if ( err ) {
475
509
data . push ( { name : 'Failed to create tables and data...' , css : 'one' } ) ;
476
510
console . log ( "!---- Create Table err" , err ) ;
@@ -509,9 +543,9 @@ function runPreparedTests(callback) {
509
543
} ,
510
544
{
511
545
name : 'Commit/Prepare All' , sql : 'select * from preparetests order by int_field' , results : [
512
- [ 1 , 1.2 , 2.4 , 'Text1' ] ,
513
- [ 2 , 2.4 , 3.6 , 'Text2' ] ,
514
- [ 3 , 3.6 , 4.8 , 'Text3' ]
546
+ [ 1 , 1.2 , 2.4 , 'Text1' , blob ] ,
547
+ [ 2 , 2.4 , 3.6 , 'Text2' , blob ] ,
548
+ [ 3 , 3.6 , 4.8 , 'Text3' , blob ]
515
549
] , use : 1
516
550
} ] ;
517
551
runTestGroup ( tests , callback ) ;
@@ -528,18 +562,18 @@ function createPreparedData(rollback, callback) {
528
562
}
529
563
try {
530
564
console . log ( "!------------- Create Prepared Tests" ) ;
531
- var prepared = db . prepare ( "insert into preparetests (int_field, num_field, real_field, text_field) values (?,?,?,?);" ) ;
565
+ var prepared = db . prepare ( "insert into preparetests (int_field, num_field, real_field, text_field, blob_field ) values (?, ?,?,?,?);" ) ;
532
566
} catch ( err ) {
533
567
console . log ( "Error creating prepare data" , err ) ;
534
568
}
535
569
db . begin ( ) ;
536
- prepared . execute ( [ 1 , 1.2 , 2.4 , "Text1" ] , function ( err ) {
570
+ prepared . execute ( [ 1 , 1.2 , 2.4 , "Text1" , blob ] , function ( err ) {
537
571
if ( err ) {
538
572
data . push ( { name : 'Failed to insert data...' , 'css' : 'one' } ) ;
539
573
console . log ( "!---- Insert err" , err , err . stack ) ;
540
574
return ;
541
575
}
542
- prepared . execute ( [ [ 2 , 2.4 , 3.6 , "Text2" ] , [ 3 , 3.6 , 4.8 , "Text3" ] ] , function ( err2 ) {
576
+ prepared . execute ( [ [ 2 , 2.4 , 3.6 , "Text2" , blob ] , [ 3 , 3.6 , 4.8 , "Text3" , blob ] ] , function ( err2 ) {
543
577
if ( err2 ) {
544
578
data . push ( { name : 'Failed to create tables and data...' , css : 'one' } ) ;
545
579
console . log ( "!---- Insert err" , err , err && err . stack ) ;
@@ -570,6 +604,8 @@ function runTests() {
570
604
runPreparedTests ( function ( ) {
571
605
data . push ( { name : "-----------------------------" , css : 'two' } ) ;
572
606
data . push ( { name : 'Tests completed...' , css : 'two' } ) ;
607
+ console . log ( "-----------------------------" ) ;
608
+ console . log ( "Tests completed!" ) ;
573
609
} ) ;
574
610
} ) ;
575
611
} ) ;
0 commit comments