summaryrefslogtreecommitdiffstats
path: root/common/unqlite/unqlite_jx9.c
blob: 7362c589e1779d76b0aa1011920b29a87c741168 (plain)
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
/*
 * Symisc unQLite: An Embeddable NoSQL (Post Modern) Database Engine.
 * Copyright (C) 2012-2013, Symisc Systems http://unqlite.org/
 * Version 1.1.6
 * For information on licensing, redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES
 * please contact Symisc Systems via:
 *       legal@symisc.net
 *       licensing@symisc.net
 *       contact@symisc.net
 * or visit:
 *      http://unqlite.org/licensing.html
 */
 /* $SymiscID: unql_jx9.c v1.2 FreeBSD 2013-01-24 22:45 stable <chm@symisc.net> $ */
#ifndef UNQLITE_AMALGAMATION
#include "unqliteInt.h"
#endif
/* 
 * This file implements UnQLite functions (db_exists(), db_create(), db_put(), db_get(), etc.) for the
 * underlying Jx9 Virtual Machine. 
 */
/*
 * string db_version(void)
 *   Return the current version of the unQLite database engine.
 * Parameter
 *   None
 * Return
 *    unQLite version number (string).
 */
static int unqliteBuiltin_db_version(jx9_context *pCtx,int argc,jx9_value **argv)
{
	SXUNUSED(argc); /* cc warning */
	SXUNUSED(argv);
	jx9_result_string(pCtx,UNQLITE_VERSION,(int)sizeof(UNQLITE_VERSION)-1);
	return JX9_OK;
}
/*
 * string db_errlog(void)
 *   Return the database error log.
 * Parameter
 *   None
 * Return
 *    Database error log (string).
 */
static int unqliteBuiltin_db_errlog(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_vm *pVm;
	SyBlob *pErr;
	
	SXUNUSED(argc); /* cc warning */
	SXUNUSED(argv);

	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Point to the error log */
	pErr = &pVm->pDb->sErr;
	/* Return the log */
	jx9_result_string(pCtx,(const char *)SyBlobData(pErr),(int)SyBlobLength(pErr));
	return JX9_OK;
}
/*
 * string db_copyright(void)
 * string db_credits(void)
 *   Return the unQLite database engine copyright notice.
 * Parameter
 *   None
 * Return
 *    Copyright notice.
 */
static int unqliteBuiltin_db_credits(jx9_context *pCtx,int argc,jx9_value **argv)
{
	SXUNUSED(argc); /* cc warning */
	SXUNUSED(argv);
	jx9_result_string(pCtx,UNQLITE_COPYRIGHT,(int)sizeof(UNQLITE_COPYRIGHT)-1);
	return JX9_OK;
}
/*
 * string db_sig(void)
 *   Return the unQLite database engine unique signature.
 * Parameter
 *   None
 * Return
 *    unQLite signature.
 */
static int unqliteBuiltin_db_sig(jx9_context *pCtx,int argc,jx9_value **argv)
{
	SXUNUSED(argc); /* cc warning */
	SXUNUSED(argv);
	jx9_result_string(pCtx,UNQLITE_IDENT,sizeof(UNQLITE_IDENT)-1);
	return JX9_OK;
}
/*
 * bool collection_exists(string $name)
 * bool db_exits(string $name)
 *   Check if a given collection exists in the underlying database.
 * Parameter
 *   name: Lookup name
 * Return
 *    TRUE if the collection exits. FALSE otherwise.
 */
static int unqliteBuiltin_collection_exists(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Perform the lookup */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	/* Lookup result */
	jx9_result_bool(pCtx,pCol ? 1 : 0);
	return JX9_OK;
}
/*
 * bool collection_create(string $name)
 * bool db_create(string $name)
 *   Create a new collection.
 * Parameter
 *   name: Collection name
 * Return
 *    TRUE if the collection was successfuly created. FALSE otherwise.
 */
static int unqliteBuiltin_collection_create(jx9_context *pCtx,int argc,jx9_value **argv)
{
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	int rc;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Try to create the collection */
	rc = unqliteCreateCollection(pVm,&sName);
	/* Return the result to the caller */
	jx9_result_bool(pCtx,rc == UNQLITE_OK ? 1 : 0);
	return JX9_OK;
}
/*
 * value db_fetch(string $col_name)
 * value db_get(string $col_name)
 *   Fetch the current record from a given collection and advance
 *   the record cursor.
 * Parameter
 *   col_name: Collection name
 * Return
 *    Record content success. NULL on failure (No more records to retrieve).
 */
static int unqliteBuiltin_db_fetch_next(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	int rc;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name");
		/* Return null */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return null */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol ){
		/* Fetch the current record */
		jx9_value *pValue;
		pValue = jx9_context_new_scalar(pCtx);
		if( pValue == 0 ){
			jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Jx9 is running out of memory");
			jx9_result_null(pCtx);
			return JX9_OK;
		}else{
			rc = unqliteCollectionFetchNextRecord(pCol,pValue);
			if( rc == UNQLITE_OK ){
				jx9_result_value(pCtx,pValue);
				/* pValue will be automatically released as soon we return from this function */
			}else{
				/* Return null */
				jx9_result_null(pCtx);
			}
		}
	}else{
		/* No such collection, return null */
		jx9_result_null(pCtx);
	}
	return JX9_OK;
}
/*
 * value db_fetch_by_id(string $col_name,int64 $record_id)
 * value db_get_by_id(string $col_name,int64 $record_id)
 *   Fetch a record using its unique ID from a given collection.
 * Parameter
 *   col_name:  Collection name
 *   record_id: Record number (__id field of a JSON object)
 * Return
 *    Record content success. NULL on failure (No such record).
 */
static int unqliteBuiltin_db_fetch_by_id(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	jx9_int64 nId;
	int nByte;
	int rc;
	/* Extract collection name */
	if( argc < 2 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name and/or record ID");
		/* Return NULL */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return NULL */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	/* Extract the record ID */
	nId = jx9_value_to_int(argv[1]);
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol ){
		/* Fetch the desired record */
		jx9_value *pValue;
		pValue = jx9_context_new_scalar(pCtx);
		if( pValue == 0 ){
			jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Jx9 is running out of memory");
			jx9_result_null(pCtx);
			return JX9_OK;
		}else{
			rc = unqliteCollectionFetchRecordById(pCol,nId,pValue);
			if( rc == UNQLITE_OK ){
				jx9_result_value(pCtx,pValue);
				/* pValue will be automatically released as soon we return from this function */
			}else{
				/* No such record, return null */
				jx9_result_null(pCtx);
			}
		}
	}else{
		/* No such collection, return null */
		jx9_result_null(pCtx);
	}
	return JX9_OK;
}
/*
 * array db_fetch_all(string $col_name,[callback filter_callback])
 * array db_get_all(string $col_name,[callback filter_callback])
 *   Retrieve all records of a given collection and apply the given
 *   callback if available to filter records.
 * Parameter
 *   col_name: Collection name
 * Return
 *    Contents of the collection (JSON array) on success. NULL on failure.
 */
static int unqliteBuiltin_db_fetch_all(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	int rc;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name");
		/* Return NULL */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return NULL */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol ){
		jx9_value *pValue,*pArray,*pCallback = 0;
		jx9_value sResult; /* Callback result */
		/* Allocate an empty scalar value and an empty JSON array */
		pArray = jx9_context_new_array(pCtx);
		pValue = jx9_context_new_scalar(pCtx);
		jx9MemObjInit(pCtx->pVm,&sResult);
		if( pValue == 0 || pArray == 0 ){
			jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Jx9 is running out of memory");
			jx9_result_null(pCtx);
			return JX9_OK;
		}
		if( argc > 1 && jx9_value_is_callable(argv[1]) ){
			pCallback = argv[1];
		}
		unqliteCollectionResetRecordCursor(pCol);
		/* Fetch collection records one after one */
		while( UNQLITE_OK == unqliteCollectionFetchNextRecord(pCol,pValue) ){
			if( pCallback ){
				jx9_value *apArg[2];
				/* Invoke the filter callback */
				apArg[0] = pValue;
				rc = jx9VmCallUserFunction(pCtx->pVm,pCallback,1,apArg,&sResult);
				if( rc == JX9_OK ){
					int iResult; /* Callback result */
					/* Extract callback result */
					iResult = jx9_value_to_bool(&sResult);
					if( !iResult ){
						/* Discard the result */
						unqliteCollectionCacheRemoveRecord(pCol,unqliteCollectionCurrentRecordId(pCol) - 1);
						continue;
					}
				}
			}
			/* Put the value in the JSON array */
			jx9_array_add_elem(pArray,0,pValue);
			/* Release the value */
			jx9_value_null(pValue);
		}
		jx9MemObjRelease(&sResult);
		/* Finally, return our array */
		jx9_result_value(pCtx,pArray);
		/* pValue will be automatically released as soon we return from
		 * this foreign function.
		 */
	}else{
		/* No such collection, return null */
		jx9_result_null(pCtx);
	}
	return JX9_OK;
}
/*
 * int64 db_last_record_id(string $col_name)
 *   Return the ID of the last inserted record.
 * Parameter
 *   col_name: Collection name
 * Return
 *    Record ID (64-bit integer) on success. FALSE on failure.
 */
static int unqliteBuiltin_db_last_record_id(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol ){
		jx9_result_int64(pCtx,unqliteCollectionLastRecordId(pCol));
	}else{
		/* No such collection, return FALSE */
		jx9_result_bool(pCtx,0);
	}
	return JX9_OK;
}
/*
 * inr64 db_current_record_id(string $col_name)
 *   Return the current record ID.
 * Parameter
 *   col_name: Collection name
 * Return
 *    Current record ID (64-bit integer) on success. FALSE on failure.
 */
static int unqliteBuiltin_db_current_record_id(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol ){
		jx9_result_int64(pCtx,unqliteCollectionCurrentRecordId(pCol));
	}else{
		/* No such collection, return FALSE */
		jx9_result_bool(pCtx,0);
	}
	return JX9_OK;
}
/*
 * bool db_reset_record_cursor(string $col_name)
 *   Reset the record ID cursor.
 * Parameter
 *   col_name: Collection name
 * Return
 *    TRUE on success. FALSE on failure.
 */
static int unqliteBuiltin_db_reset_record_cursor(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol ){
		unqliteCollectionResetRecordCursor(pCol);
		jx9_result_bool(pCtx,1);
	}else{
		/* No such collection */
		jx9_result_bool(pCtx,0);
	}
	return JX9_OK;
}
/*
 * int64 db_total_records(string $col_name)
 *   Return the total number of inserted records in the given collection.
 * Parameter
 *   col_name: Collection name
 * Return
 *    Total number of records on success. FALSE on failure.
 */
static int unqliteBuiltin_db_total_records(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol ){
		unqlite_int64 nRec;
		nRec = unqliteCollectionTotalRecords(pCol);
		jx9_result_int64(pCtx,nRec);
	}else{
		/* No such collection */
		jx9_result_bool(pCtx,0);
	}
	return JX9_OK;
}
/*
 * string db_creation_date(string $col_name)
 *   Return the creation date of the given collection.
 * Parameter
 *   col_name: Collection name
 * Return
 *    Creation date on success. FALSE on failure.
 */
static int unqliteBuiltin_db_creation_date(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol ){
		Sytm *pTm = &pCol->sCreation;
		jx9_result_string_format(pCtx,"%d-%d-%d %02d:%02d:%02d",
			pTm->tm_year,pTm->tm_mon,pTm->tm_mday,
			pTm->tm_hour,pTm->tm_min,pTm->tm_sec
			);
	}else{
		/* No such collection */
		jx9_result_bool(pCtx,0);
	}
	return JX9_OK;
}
/*
 * bool db_store(string $col_name,...)
 * bool db_put(string $col_name,...)
 *   Store one or more JSON values in a given collection.
 * Parameter
 *   col_name: Collection name
 * Return
 *    TRUE on success. FALSE on failure.
 */
static int unqliteBuiltin_db_store(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	int rc;
	int i;
	/* Extract collection name */
	if( argc < 2 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name and/or records");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol == 0 ){
		jx9_context_throw_error_format(pCtx,JX9_CTX_ERR,"No such collection '%z'",&sName);
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	/* Store the given values */
	for( i = 1 ; i < argc ; ++i ){
		rc = unqliteCollectionPut(pCol,argv[i],0);
		if( rc != UNQLITE_OK){
			jx9_context_throw_error_format(pCtx,JX9_CTX_ERR,
				"Error while storing record %d in collection '%z'",i,&sName
				);
			/* Return false */
			jx9_result_bool(pCtx,0);
			return JX9_OK;
		}
	}
	/* All done, return TRUE */
	jx9_result_bool(pCtx,1);
	return JX9_OK;
}
/*
 * bool db_drop_collection(string $col_name)
 * bool collection_delete(string $col_name)
 *   Remove a given collection from the database.
 * Parameter
 *   col_name: Collection name
 * Return
 *    TRUE on success. FALSE on failure.
 */
static int unqliteBuiltin_db_drop_col(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	int rc;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol == 0 ){
		jx9_context_throw_error_format(pCtx,JX9_CTX_ERR,"No such collection '%z'",&sName);
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	/* Drop the collection */
	rc = unqliteDropCollection(pCol);
	/* Processing result */
	jx9_result_bool(pCtx,rc == UNQLITE_OK);
	return JX9_OK;
}
/*
 * bool db_drop_record(string $col_name,int64 record_id)
 *   Remove a given record from a collection.
 * Parameter
 *   col_name: Collection name.
 *   record_id: ID of the record.
 * Return
 *    TRUE on success. FALSE on failure.
 */
static int unqliteBuiltin_db_drop_record(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	jx9_int64 nId;
	int nByte;
	int rc;
	/* Extract collection name */
	if( argc < 2 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name and/or records");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol == 0 ){
		jx9_context_throw_error_format(pCtx,JX9_CTX_ERR,"No such collection '%z'",&sName);
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	/* Extract the record ID */
	nId = jx9_value_to_int64(argv[1]);
	/* Drop the record */
	rc = unqliteCollectionDropRecord(pCol,nId,1,1);
	/* Processing result */
	jx9_result_bool(pCtx,rc == UNQLITE_OK);
	return JX9_OK;
}
/*
 * bool db_set_schema(string $col_name, object $json_object)
 *   Set a schema for a given collection.
 * Parameter
 *   col_name: Collection name.
 *   json_object: Collection schema (Must be a JSON object).
 * Return
 *    TRUE on success. FALSE on failure.
 */
static int unqliteBuiltin_db_set_schema(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	int rc;
	/* Extract collection name */
	if( argc < 2 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name and/or db scheme");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	if( !jx9_value_is_json_object(argv[1]) ){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection scheme");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	rc = UNQLITE_NOOP;
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol ){
		/* Set the collection scheme */
		rc = unqliteCollectionSetSchema(pCol,argv[1]);
	}else{
		jx9_context_throw_error_format(pCtx,JX9_CTX_WARNING,
			"No such collection '%z'",
			&sName
			);
	}
	/* Processing result */
	jx9_result_bool(pCtx,rc == UNQLITE_OK);
	return JX9_OK;
}
/*
 * object db_get_schema(string $col_name)
 *   Return the schema associated with a given collection.
 * Parameter
 *   col_name: Collection name
 * Return
 *    Collection schema on success. null otherwise.
 */
static int unqliteBuiltin_db_get_schema(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_col *pCol;
	const char *zName;
	unqlite_vm *pVm;
	SyString sName;
	int nByte;
	/* Extract collection name */
	if( argc < 1 ){
		/* Missing arguments */
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Missing collection name and/or db scheme");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	zName = jx9_value_to_string(argv[0],&nByte);
	if( nByte < 1){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR,"Invalid collection name");
		/* Return false */
		jx9_result_bool(pCtx,0);
		return JX9_OK;
	}
	SyStringInitFromBuf(&sName,zName,nByte);
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Fetch the collection */
	pCol = unqliteCollectionFetch(pVm,&sName,UNQLITE_VM_AUTO_LOAD);
	if( pCol ){
		/* Return the collection schema */
		jx9_result_value(pCtx,&pCol->sSchema);
	}else{
		jx9_context_throw_error_format(pCtx,JX9_CTX_WARNING,
			"No such collection '%z'",
			&sName
			);
		jx9_result_null(pCtx);
	}
	return JX9_OK;
}
/*
 * bool db_begin(void)
 *   Manually begin a write transaction.
 * Parameter
 *   None
 * Return
 *    TRUE on success. FALSE otherwise.
 */
static int unqliteBuiltin_db_begin(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_vm *pVm;
	unqlite *pDb;
	int rc;
	SXUNUSED(argc); /* cc warning */
	SXUNUSED(argv);
	/* Point to the unqlite Vm */
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Point to the underlying database handle  */
	pDb = pVm->pDb;
	/* Begin the transaction */
	rc = unqlitePagerBegin(pDb->sDB.pPager);
	/* result */
	jx9_result_bool(pCtx,rc == UNQLITE_OK );
	return JX9_OK;
}
/*
 * bool db_commit(void)
 *   Manually commit a transaction.
 * Parameter
 *   None
 * Return
 *    TRUE if the transaction was successfuly commited. FALSE otherwise.
 */
static int unqliteBuiltin_db_commit(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_vm *pVm;
	unqlite *pDb;
	int rc;
	SXUNUSED(argc); /* cc warning */
	SXUNUSED(argv);
	/* Point to the unqlite Vm */
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Point to the underlying database handle  */
	pDb = pVm->pDb;
	/* Commit the transaction if any */
	rc = unqlitePagerCommit(pDb->sDB.pPager);
	/* Commit result */
	jx9_result_bool(pCtx,rc == UNQLITE_OK );
	return JX9_OK;
}
/*
 * bool db_rollback(void)
 *   Manually rollback a transaction.
 * Parameter
 *   None
 * Return
 *    TRUE if the transaction was successfuly rolled back. FALSE otherwise
 */
static int unqliteBuiltin_db_rollback(jx9_context *pCtx,int argc,jx9_value **argv)
{
	unqlite_vm *pVm;
	unqlite *pDb;
	int rc;
	SXUNUSED(argc); /* cc warning */
	SXUNUSED(argv);
	/* Point to the unqlite Vm */
	pVm = (unqlite_vm *)jx9_context_user_data(pCtx);
	/* Point to the underlying database handle  */
	pDb = pVm->pDb;
	/* Rollback the transaction if any */
	rc = unqlitePagerRollback(pDb->sDB.pPager,TRUE);
	/* Rollback result */
	jx9_result_bool(pCtx,rc == UNQLITE_OK );
	return JX9_OK;
}
/*
 * Register all the UnQLite foreign functions defined above.
 */
UNQLITE_PRIVATE int unqliteRegisterJx9Functions(unqlite_vm *pVm)
{
	static const jx9_builtin_func aBuiltin[] = {
		{ "db_version" , unqliteBuiltin_db_version },
		{ "db_copyright", unqliteBuiltin_db_credits },
		{ "db_credits" , unqliteBuiltin_db_credits },
		{ "db_sig" ,     unqliteBuiltin_db_sig     },
		{ "db_errlog",   unqliteBuiltin_db_errlog  },
		{ "collection_exists", unqliteBuiltin_collection_exists },
		{ "db_exists",         unqliteBuiltin_collection_exists }, 
		{ "collection_create", unqliteBuiltin_collection_create },
		{ "db_create",         unqliteBuiltin_collection_create },
		{ "db_fetch",          unqliteBuiltin_db_fetch_next     },
		{ "db_get",            unqliteBuiltin_db_fetch_next     },
		{ "db_fetch_by_id",    unqliteBuiltin_db_fetch_by_id    },
		{ "db_get_by_id",      unqliteBuiltin_db_fetch_by_id    },
		{ "db_fetch_all",      unqliteBuiltin_db_fetch_all      },
		{ "db_get_all",        unqliteBuiltin_db_fetch_all      },
		{ "db_last_record_id", unqliteBuiltin_db_last_record_id },
		{ "db_current_record_id", unqliteBuiltin_db_current_record_id },
		{ "db_reset_record_cursor", unqliteBuiltin_db_reset_record_cursor },
		{ "db_total_records",  unqliteBuiltin_db_total_records  },
		{ "db_creation_date",  unqliteBuiltin_db_creation_date  },
		{ "db_store",          unqliteBuiltin_db_store          },
		{ "db_put",            unqliteBuiltin_db_store          },
		{ "db_drop_collection", unqliteBuiltin_db_drop_col      },
		{ "collection_delete", unqliteBuiltin_db_drop_col       },
		{ "db_drop_record",    unqliteBuiltin_db_drop_record    },
		{ "db_set_schema",     unqliteBuiltin_db_set_schema     },
		{ "db_get_schema",     unqliteBuiltin_db_get_schema     },
		{ "db_begin",          unqliteBuiltin_db_begin          },
		{ "db_commit",         unqliteBuiltin_db_commit         },
		{ "db_rollback",       unqliteBuiltin_db_rollback       },
	};
	int rc = UNQLITE_OK;
	sxu32 n;
	/* Register the unQLite functions defined above in the Jx9 call table */
	for( n = 0 ; n < SX_ARRAYSIZE(aBuiltin) ; ++n ){
		rc = jx9_create_function(pVm->pJx9Vm,aBuiltin[n].zName,aBuiltin[n].xFunc,pVm);
	}
	return rc;
}