-
Notifications
You must be signed in to change notification settings - Fork 3
/
extension-functions.c
1973 lines (1735 loc) · 51.6 KB
/
extension-functions.c
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
/*
This library will provide common mathematical and string functions in
SQL queries using the operating system libraries or provided
definitions. It includes the following functions:
Math: acos, asin, atan, atn2, atan2, acosh, asinh, atanh, difference,
degrees, radians, cos, sin, tan, cot, cosh, sinh, tanh, coth, exp,
log, log10, power, sign, sqrt, square, ceil, floor, pi.
String: replicate, charindex, leftstr, rightstr, ltrim, rtrim, trim,
replace, reverse, proper, padl, padr, padc, strfilter.
Aggregate: stdev, variance, mode, median, lower_quartile,
upper_quartile.
The string functions ltrim, rtrim, trim, replace are included in
recent versions of SQLite and so by default do not build.
Compilation instructions:
Compile this C source file into a dynamic library as follows:
* Linux:
gcc -fPIC -lm -shared extension-functions.c -o libsqlitefunctions.so
* Mac OS X:
gcc -fno-common -dynamiclib extension-functions.c -o libsqlitefunctions.dylib
(You may need to add flags
-I /opt/local/include/ -L/opt/local/lib -lsqlite3
if your sqlite3 is installed from Mac ports, or
-I /sw/include/ -L/sw/lib -lsqlite3
if installed with Fink.)
* Windows:
1. Install MinGW (http://www.mingw.org/) and you will get the gcc
(gnu compiler collection)
2. add the path to your path variable (isn't done during the
installation!)
3. compile:
gcc -shared -I "path" -o libsqlitefunctions.so extension-functions.c
(path = path of sqlite3ext.h; i.e. C:\programs\sqlite)
Usage instructions for applications calling the sqlite3 API functions:
In your application, call sqlite3_enable_load_extension(db,1) to
allow loading external libraries. Then load the library libsqlitefunctions
using sqlite3_load_extension; the third argument should be 0.
See http://www.sqlite.org/cvstrac/wiki?p=LoadableExtensions.
Select statements may now use these functions, as in
SELECT cos(radians(inclination)) FROM satsum WHERE satnum = 25544;
Usage instructions for the sqlite3 program:
If the program is built so that loading extensions is permitted,
the following will work:
sqlite> SELECT load_extension('./libsqlitefunctions.so');
sqlite> select cos(radians(45));
0.707106781186548
Note: Loading extensions is by default prohibited as a
security measure; see "Security Considerations" in
http://www.sqlite.org/cvstrac/wiki?p=LoadableExtensions.
If the sqlite3 program and library are built this
way, you cannot use these functions from the program, you
must write your own program using the sqlite3 API, and call
sqlite3_enable_load_extension as described above, or else
rebuilt the sqlite3 program to allow loadable extensions.
Alterations:
The instructions are for Linux, Mac OS X, and Windows; users of other
OSes may need to modify this procedure. In particular, if your math
library lacks one or more of the needed trig or log functions, comment
out the appropriate HAVE_ #define at the top of file. If you do not
wish to make a loadable module, comment out the define for
COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE. If you are using a
version of SQLite without the trim functions and replace, comment out
the HAVE_TRIM #define.
Liam Healy
History:
2010-01-06 Correct check for argc in squareFunc, and add Windows
compilation instructions.
2009-06-24 Correct check for argc in properFunc.
2008-09-14 Add check that memory was actually allocated after
sqlite3_malloc or sqlite3StrDup, call sqlite3_result_error_nomem if
not. Thanks to Robert Simpson.
2008-06-13 Change to instructions to indicate use of the math library
and that program might work.
2007-10-01 Minor clarification to instructions.
2007-09-29 Compilation as loadable module is optional with
COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE.
2007-09-28 Use sqlite3_extension_init and macros
SQLITE_EXTENSION_INIT1, SQLITE_EXTENSION_INIT2, so that it works with
sqlite3_load_extension. Thanks to Eric Higashino and Joe Wilson.
New instructions for Mac compilation.
2007-09-17 With help from Joe Wilson and Nuno Luca, made use of
external interfaces so that compilation is no longer dependent on
SQLite source code. Merged source, header, and README into a single
file. Added casts so that Mac will compile without warnings (unsigned
and signed char).
2007-09-05 Included some definitions from sqlite 3.3.13 so that this
will continue to work in newer versions of sqlite. Completed
description of functions available.
2007-03-27 Revised description.
2007-03-23 Small cleanup and a bug fix on the code. This was mainly
letting errno flag errors encountered in the math library and checking
the result, rather than pre-checking. This fixes a bug in power that
would cause an error if any non-positive number was raised to any
power.
2007-02-07 posted by Mikey C to sqlite mailing list.
Original code 2006 June 05 by relicoder.
*/
//#include "config.h"
#define COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE 1
#define HAVE_ACOSH 1
#define HAVE_ASINH 1
#define HAVE_ATANH 1
#define HAVE_SINH 1
#define HAVE_COSH 1
#define HAVE_TANH 1
#define HAVE_LOG10 1
#define HAVE_ISBLANK 1
#define SQLITE_SOUNDEX 1
#define HAVE_TRIM 1 /* LMH 2007-03-25 if sqlite has trim functions */
#ifdef COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#else
#include "sqlite3.h"
#endif
#include <ctype.h>
/* relicoder */
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <errno.h> /* LMH 2007-03-25 */
#include <stdlib.h>
#include <assert.h>
#ifndef _MAP_H_
#define _MAP_H_
#include <stdint.h>
/*
** Simple binary tree implementation to use in median, mode and quartile calculations
** Tree is not necessarily balanced. That would require something like red&black trees of AVL
*/
typedef int(*cmp_func)(const void *, const void *);
typedef void(*map_iterator)(void*, int64_t, void*);
typedef struct node{
struct node *l;
struct node *r;
void* data;
int64_t count;
} node;
typedef struct map{
node *base;
cmp_func cmp;
short free;
} map;
/*
** creates a map given a comparison function
*/
map map_make(cmp_func cmp);
/*
** inserts the element e into map m
*/
void map_insert(map *m, void *e);
/*
** executes function iter over all elements in the map, in key increasing order
*/
void map_iterate(map *m, map_iterator iter, void* p);
/*
** frees all memory used by a map
*/
void map_destroy(map *m);
/*
** compares 2 integers
** to use with map_make
*/
int int_cmp(const void *a, const void *b);
/*
** compares 2 doubles
** to use with map_make
*/
int double_cmp(const void *a, const void *b);
#endif /* _MAP_H_ */
typedef uint8_t u8;
typedef uint16_t u16;
typedef int64_t i64;
static char *sqlite3StrDup( const char *z ) {
char *res = sqlite3_malloc( strlen(z)+1 );
return strcpy( res, z );
}
/*
** These are copied verbatim from fun.c so as to not have the names exported
*/
/* LMH from sqlite3 3.3.13 */
/*
** This table maps from the first byte of a UTF-8 character to the number
** of trailing bytes expected. A value '4' indicates that the table key
** is not a legal first byte for a UTF-8 character.
*/
static const u8 xtra_utf8_bytes[256] = {
/* 0xxxxxxx */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10wwwwww */
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
/* 110yyyyy */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 1110zzzz */
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
/* 11110yyy */
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
};
/*
** This table maps from the number of trailing bytes in a UTF-8 character
** to an integer constant that is effectively calculated for each character
** read by a naive implementation of a UTF-8 character reader. The code
** in the READ_UTF8 macro explains things best.
*/
static const int xtra_utf8_bits[] = {
0,
12416, /* (0xC0 << 6) + (0x80) */
925824, /* (0xE0 << 12) + (0x80 << 6) + (0x80) */
63447168 /* (0xF0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80 */
};
/*
** If a UTF-8 character contains N bytes extra bytes (N bytes follow
** the initial byte so that the total character length is N+1) then
** masking the character with utf8_mask[N] must produce a non-zero
** result. Otherwise, we have an (illegal) overlong encoding.
*/
static const int utf_mask[] = {
0x00000000,
0xffffff80,
0xfffff800,
0xffff0000,
};
/* LMH salvaged from sqlite3 3.3.13 source code src/utf.c */
#define READ_UTF8(zIn, c) { \
int xtra; \
c = *(zIn)++; \
xtra = xtra_utf8_bytes[c]; \
switch( xtra ){ \
case 4: c = (int)0xFFFD; break; \
case 3: c = (c<<6) + *(zIn)++; \
case 2: c = (c<<6) + *(zIn)++; \
case 1: c = (c<<6) + *(zIn)++; \
c -= xtra_utf8_bits[xtra]; \
if( (utf_mask[xtra]&c)==0 \
|| (c&0xFFFFF800)==0xD800 \
|| (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
} \
}
static int sqlite3ReadUtf8(const unsigned char *z){
int c;
READ_UTF8(z, c);
return c;
}
#define SKIP_UTF8(zIn) { \
zIn += (xtra_utf8_bytes[*(u8 *)zIn] + 1); \
}
/*
** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
** return the number of unicode characters in pZ up to (but not including)
** the first 0x00 byte. If nByte is not less than zero, return the
** number of unicode characters in the first nByte of pZ (or up to
** the first 0x00, whichever comes first).
*/
static int sqlite3Utf8CharLen(const char *z, int nByte){
int r = 0;
const char *zTerm;
if( nByte>=0 ){
zTerm = &z[nByte];
}else{
zTerm = (const char *)(-1);
}
assert( z<=zTerm );
while( *z!=0 && z<zTerm ){
SKIP_UTF8(z);
r++;
}
return r;
}
/*
** X is a pointer to the first byte of a UTF-8 character. Increment
** X so that it points to the next character. This only works right
** if X points to a well-formed UTF-8 string.
*/
#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
#define sqliteCharVal(X) sqlite3ReadUtf8(X)
/*
** This is a macro that facilitates writting wrappers for math.h functions
** it creates code for a function to use in SQlite that gets one numeric input
** and returns a floating point value.
**
** Could have been implemented using pointers to functions but this way it's inline
** and thus more efficient. Lower * ranking though...
**
** Parameters:
** name: function name to de defined (eg: sinFunc)
** function: function defined in math.h to wrap (eg: sin)
** domain: boolean condition that CAN'T happen in terms of the input parameter rVal
** (eg: rval<0 for sqrt)
*/
/* LMH 2007-03-25 Changed to use errno and remove domain; no pre-checking for errors. */
#define GEN_MATH_WRAP_DOUBLE_1(name, function) \
static void name(sqlite3_context *context, int argc, sqlite3_value **argv){\
double rVal = 0.0, val;\
assert( argc==1 );\
switch( sqlite3_value_type(argv[0]) ){\
case SQLITE_NULL: {\
sqlite3_result_null(context);\
break;\
}\
default: {\
rVal = sqlite3_value_double(argv[0]);\
errno = 0;\
val = function(rVal);\
if (errno == 0) {\
sqlite3_result_double(context, val);\
} else {\
sqlite3_result_error(context, strerror(errno), errno);\
}\
break;\
}\
}\
}\
/*
** Example of GEN_MATH_WRAP_DOUBLE_1 usage
** this creates function sqrtFunc to wrap the math.h standard function sqrt(x)=x^0.5
*/
GEN_MATH_WRAP_DOUBLE_1(sqrtFunc, sqrt)
/* trignometric functions */
GEN_MATH_WRAP_DOUBLE_1(acosFunc, acos)
GEN_MATH_WRAP_DOUBLE_1(asinFunc, asin)
GEN_MATH_WRAP_DOUBLE_1(atanFunc, atan)
/*
** Many of systems don't have inverse hyperbolic trig functions so this will emulate
** them on those systems in terms of log and sqrt (formulas are too trivial to demand
** written proof here)
*/
#ifndef HAVE_ACOSH
static double acosh(double x){
return log(x + sqrt(x*x - 1.0));
}
#endif
GEN_MATH_WRAP_DOUBLE_1(acoshFunc, acosh)
#ifndef HAVE_ASINH
static double asinh(double x){
return log(x + sqrt(x*x + 1.0));
}
#endif
GEN_MATH_WRAP_DOUBLE_1(asinhFunc, asinh)
#ifndef HAVE_ATANH
static double atanh(double x){
return (1.0/2.0)*log((1+x)/(1-x)) ;
}
#endif
GEN_MATH_WRAP_DOUBLE_1(atanhFunc, atanh)
/*
** math.h doesn't require cot (cotangent) so it's defined here
*/
static double cot(double x){
return 1.0/tan(x);
}
GEN_MATH_WRAP_DOUBLE_1(sinFunc, sin)
GEN_MATH_WRAP_DOUBLE_1(cosFunc, cos)
GEN_MATH_WRAP_DOUBLE_1(tanFunc, tan)
GEN_MATH_WRAP_DOUBLE_1(cotFunc, cot)
static double coth(double x){
return 1.0/tanh(x);
}
/*
** Many systems don't have hyperbolic trigonometric functions so this will emulate
** them on those systems directly from the definition in terms of exp
*/
#ifndef HAVE_SINH
static double sinh(double x){
return (exp(x)-exp(-x))/2.0;
}
#endif
GEN_MATH_WRAP_DOUBLE_1(sinhFunc, sinh)
#ifndef HAVE_COSH
static double cosh(double x){
return (exp(x)+exp(-x))/2.0;
}
#endif
GEN_MATH_WRAP_DOUBLE_1(coshFunc, cosh)
#ifndef HAVE_TANH
static double tanh(double x){
return sinh(x)/cosh(x);
}
#endif
GEN_MATH_WRAP_DOUBLE_1(tanhFunc, tanh)
GEN_MATH_WRAP_DOUBLE_1(cothFunc, coth)
/*
** Some systems lack log in base 10. This will emulate it
*/
#ifndef HAVE_LOG10
static double log10(double x){
static double l10 = -1.0;
if( l10<0.0 ){
l10 = log(10.0);
}
return log(x)/l10;
}
#endif
GEN_MATH_WRAP_DOUBLE_1(logFunc, log)
GEN_MATH_WRAP_DOUBLE_1(log10Func, log10)
GEN_MATH_WRAP_DOUBLE_1(expFunc, exp)
/*
** Fallback for systems where math.h doesn't define M_PI
*/
#undef M_PI
#ifndef M_PI
/*
** static double PI = acos(-1.0);
** #define M_PI (PI)
*/
#define M_PI 3.14159265358979323846
#endif
/* Convert Degrees into Radians */
static double deg2rad(double x){
return x*M_PI/180.0;
}
/* Convert Radians into Degrees */
static double rad2deg(double x){
return 180.0*x/M_PI;
}
GEN_MATH_WRAP_DOUBLE_1(rad2degFunc, rad2deg)
GEN_MATH_WRAP_DOUBLE_1(deg2radFunc, deg2rad)
/* constant function that returns the value of PI=3.1415... */
static void piFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
sqlite3_result_double(context, M_PI);
}
/*
** Implements the sqrt function, it has the peculiarity of returning an integer when the
** the argument is an integer.
** Since SQLite isn't strongly typed (almost untyped actually) this is a bit pedantic
*/
static void squareFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
i64 iVal = 0;
double rVal = 0.0;
assert( argc==1 );
switch( sqlite3_value_type(argv[0]) ){
case SQLITE_INTEGER: {
iVal = sqlite3_value_int64(argv[0]);
sqlite3_result_int64(context, iVal*iVal);
break;
}
case SQLITE_NULL: {
sqlite3_result_null(context);
break;
}
default: {
rVal = sqlite3_value_double(argv[0]);
sqlite3_result_double(context, rVal*rVal);
break;
}
}
}
/*
** Wraps the pow math.h function
** When both the base and the exponent are integers the result should be integer
** (see sqrt just before this). Here the result is always double
*/
/* LMH 2007-03-25 Changed to use errno; no pre-checking for errors. Also removes
but that was present in the pre-checking that called sqlite3_result_error on
a non-positive first argument, which is not always an error. */
static void powerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
double r1 = 0.0;
double r2 = 0.0;
double val;
assert( argc==2 );
if( sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL ){
sqlite3_result_null(context);
}else{
r1 = sqlite3_value_double(argv[0]);
r2 = sqlite3_value_double(argv[1]);
errno = 0;
val = pow(r1,r2);
if (errno == 0) {
sqlite3_result_double(context, val);
} else {
sqlite3_result_error(context, strerror(errno), errno);
}
}
}
/*
** atan2 wrapper
*/
static void atn2Func(sqlite3_context *context, int argc, sqlite3_value **argv){
double r1 = 0.0;
double r2 = 0.0;
assert( argc==2 );
if( sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL ){
sqlite3_result_null(context);
}else{
r1 = sqlite3_value_double(argv[0]);
r2 = sqlite3_value_double(argv[1]);
sqlite3_result_double(context, atan2(r1,r2));
}
}
/*
** Implementation of the sign() function
** return one of 3 possibilities +1,0 or -1 when the argument is respectively
** positive, 0 or negative.
** When the argument is NULL the result is also NULL (completly conventional)
*/
static void signFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
double rVal=0.0;
i64 iVal=0;
assert( argc==1 );
switch( sqlite3_value_type(argv[0]) ){
case SQLITE_INTEGER: {
iVal = sqlite3_value_int64(argv[0]);
iVal = ( iVal > 0) ? 1: ( iVal < 0 ) ? -1: 0;
sqlite3_result_int64(context, iVal);
break;
}
case SQLITE_NULL: {
sqlite3_result_null(context);
break;
}
default: {
/* 2nd change below. Line for abs was: if( rVal<0 ) rVal = rVal * -1.0; */
rVal = sqlite3_value_double(argv[0]);
rVal = ( rVal > 0) ? 1: ( rVal < 0 ) ? -1: 0;
sqlite3_result_double(context, rVal);
break;
}
}
}
/*
** smallest integer value not less than argument
*/
static void ceilFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
double rVal=0.0;
i64 iVal=0;
assert( argc==1 );
switch( sqlite3_value_type(argv[0]) ){
case SQLITE_INTEGER: {
i64 iVal = sqlite3_value_int64(argv[0]);
sqlite3_result_int64(context, iVal);
break;
}
case SQLITE_NULL: {
sqlite3_result_null(context);
break;
}
default: {
rVal = sqlite3_value_double(argv[0]);
sqlite3_result_int64(context, (i64) ceil(rVal));
break;
}
}
}
/*
** largest integer value not greater than argument
*/
static void floorFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
double rVal=0.0;
i64 iVal=0;
assert( argc==1 );
switch( sqlite3_value_type(argv[0]) ){
case SQLITE_INTEGER: {
i64 iVal = sqlite3_value_int64(argv[0]);
sqlite3_result_int64(context, iVal);
break;
}
case SQLITE_NULL: {
sqlite3_result_null(context);
break;
}
default: {
rVal = sqlite3_value_double(argv[0]);
sqlite3_result_int64(context, (i64) floor(rVal));
break;
}
}
}
/*
** Given a string (s) in the first argument and an integer (n) in the second returns the
** string that constains s contatenated n times
*/
static void replicateFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
unsigned char *z; /* input string */
unsigned char *zo; /* result string */
i64 iCount; /* times to repeat */
i64 nLen; /* length of the input string (no multibyte considerations) */
i64 nTLen; /* length of the result string (no multibyte considerations) */
i64 i=0;
if( argc!=2 || SQLITE_NULL==sqlite3_value_type(argv[0]) )
return;
iCount = sqlite3_value_int64(argv[1]);
if( iCount<0 ){
sqlite3_result_error(context, "domain error", -1);
}else{
nLen = sqlite3_value_bytes(argv[0]);
nTLen = nLen*iCount;
z=sqlite3_malloc(nTLen+1);
zo=sqlite3_malloc(nLen+1);
if (!z || !zo){
sqlite3_result_error_nomem(context);
if (z) sqlite3_free(z);
if (zo) sqlite3_free(zo);
return;
}
strcpy((char*)zo, (char*)sqlite3_value_text(argv[0]));
for(i=0; i<iCount; ++i){
strcpy((char*)(z+i*nLen), (char*)zo);
}
sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
sqlite3_free(z);
sqlite3_free(zo);
}
}
/*
** Some systems (win32 among others) don't have an isblank function, this will emulate it.
** This function is not UFT-8 safe since it only analyses a byte character.
*/
#ifndef HAVE_ISBLANK
int isblank(char c){
return( ' '==c || '\t'==c );
}
#endif
static void properFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
const unsigned char *z; /* input string */
unsigned char *zo; /* output string */
unsigned char *zt; /* iterator */
char r;
int c=1;
assert( argc==1);
if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
sqlite3_result_null(context);
return;
}
z = sqlite3_value_text(argv[0]);
zo = (unsigned char *)sqlite3StrDup((char *) z);
if (!zo) {
sqlite3_result_error_nomem(context);
return;
}
zt = zo;
while( (r = *(z++))!=0 ){
if( isblank(r) ){
c=1;
}else{
if( c==1 ){
r = toupper(r);
}else{
r = tolower(r);
}
c=0;
}
*(zt++) = r;
}
*zt = '\0';
sqlite3_result_text(context, (char*)zo, -1, SQLITE_TRANSIENT);
sqlite3_free(zo);
}
/*
** given an input string (s) and an integer (n) adds spaces at the begining of s
** until it has a length of n characters.
** When s has a length >=n it's a NOP
** padl(NULL) = NULL
*/
static void padlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
i64 ilen; /* length to pad to */
i64 zl; /* length of the input string (UTF-8 chars) */
int i = 0;
const char *zi; /* input string */
char *zo; /* output string */
char *zt;
assert( argc==2 );
if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
sqlite3_result_null(context);
}else{
zi = (char *)sqlite3_value_text(argv[0]);
ilen = sqlite3_value_int64(argv[1]);
/* check domain */
if(ilen<0){
sqlite3_result_error(context, "domain error", -1);
return;
}
zl = sqlite3Utf8CharLen(zi, -1);
if( zl>=ilen ){
/* string is longer than the requested pad length, return the same string (dup it) */
zo = sqlite3StrDup(zi);
if (!zo){
sqlite3_result_error_nomem(context);
return;
}
sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
}else{
zo = sqlite3_malloc(strlen(zi)+ilen-zl+1);
if (!zo){
sqlite3_result_error_nomem(context);
return;
}
zt = zo;
for(i=1; i+zl<=ilen; ++i){
*(zt++)=' ';
}
/* no need to take UTF-8 into consideration here */
strcpy(zt,zi);
}
sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
sqlite3_free(zo);
}
}
/*
** given an input string (s) and an integer (n) appends spaces at the end of s
** until it has a length of n characters.
** When s has a length >=n it's a NOP
** padl(NULL) = NULL
*/
static void padrFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
i64 ilen; /* length to pad to */
i64 zl; /* length of the input string (UTF-8 chars) */
i64 zll; /* length of the input string (bytes) */
int i = 0;
const char *zi; /* input string */
char *zo; /* output string */
char *zt;
assert( argc==2 );
if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
sqlite3_result_null(context);
}else{
zi = (char *)sqlite3_value_text(argv[0]);
ilen = sqlite3_value_int64(argv[1]);
/* check domain */
if(ilen<0){
sqlite3_result_error(context, "domain error", -1);
return;
}
zl = sqlite3Utf8CharLen(zi, -1);
if( zl>=ilen ){
/* string is longer than the requested pad length, return the same string (dup it) */
zo = sqlite3StrDup(zi);
if (!zo){
sqlite3_result_error_nomem(context);
return;
}
sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
}else{
zll = strlen(zi);
zo = sqlite3_malloc(zll+ilen-zl+1);
if (!zo){
sqlite3_result_error_nomem(context);
return;
}
zt = strcpy(zo,zi)+zll;
for(i=1; i+zl<=ilen; ++i){
*(zt++) = ' ';
}
*zt = '\0';
}
sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
sqlite3_free(zo);
}
}
/*
** given an input string (s) and an integer (n) appends spaces at the end of s
** and adds spaces at the begining of s until it has a length of n characters.
** Tries to add has many characters at the left as at the right.
** When s has a length >=n it's a NOP
** padl(NULL) = NULL
*/
static void padcFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
i64 ilen; /* length to pad to */
i64 zl; /* length of the input string (UTF-8 chars) */
i64 zll; /* length of the input string (bytes) */
int i = 0;
const char *zi; /* input string */
char *zo; /* output string */
char *zt;
assert( argc==2 );
if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
sqlite3_result_null(context);
}else{
zi = (char *)sqlite3_value_text(argv[0]);
ilen = sqlite3_value_int64(argv[1]);
/* check domain */
if(ilen<0){
sqlite3_result_error(context, "domain error", -1);
return;
}
zl = sqlite3Utf8CharLen(zi, -1);
if( zl>=ilen ){
/* string is longer than the requested pad length, return the same string (dup it) */
zo = sqlite3StrDup(zi);
if (!zo){
sqlite3_result_error_nomem(context);
return;
}
sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
}else{
zll = strlen(zi);
zo = sqlite3_malloc(zll+ilen-zl+1);
if (!zo){
sqlite3_result_error_nomem(context);
return;
}
zt = zo;
for(i=1; 2*i+zl<=ilen; ++i){
*(zt++) = ' ';
}
strcpy(zt, zi);
zt+=zll;
for(; i+zl<=ilen; ++i){
*(zt++) = ' ';
}
*zt = '\0';
}
sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
sqlite3_free(zo);
}
}
/*
** given 2 string (s1,s2) returns the string s1 with the characters NOT in s2 removed
** assumes strings are UTF-8 encoded
*/
static void strfilterFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
const char *zi1; /* first parameter string (searched string) */
const char *zi2; /* second parameter string (vcontains valid characters) */
const char *z1;
const char *z21;
const char *z22;
char *zo; /* output string */
char *zot;
int c1 = 0;
int c2 = 0;
assert( argc==2 );
if( sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL ){
sqlite3_result_null(context);
}else{
zi1 = (char *)sqlite3_value_text(argv[0]);
zi2 = (char *)sqlite3_value_text(argv[1]);
/*
** maybe I could allocate less, but that would imply 2 passes, rather waste
** (possibly) some memory
*/
zo = sqlite3_malloc(strlen(zi1)+1);
if (!zo){
sqlite3_result_error_nomem(context);
return;
}
zot = zo;
z1 = zi1;
while( (c1=sqliteCharVal((unsigned char *)z1))!=0 ){
z21=zi2;
while( (c2=sqliteCharVal((unsigned char *)z21))!=0 && c2!=c1 ){
sqliteNextChar(z21);
}
if( c2!=0){
z22=z21;
sqliteNextChar(z22);
strncpy(zot, z21, z22-z21);
zot+=z22-z21;
}
sqliteNextChar(z1);
}
*zot = '\0';
sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
sqlite3_free(zo);
}
}
/*
** Given a string z1, retutns the (0 based) index of it's first occurence
** in z2 after the first s characters.
** Returns -1 when there isn't a match.
** updates p to point to the character where the match occured.
** This is an auxiliary function.
*/
static int _substr(const char* z1, const char* z2, int s, const char** p){
int c = 0;
int rVal=-1;
const char* zt1;
const char* zt2;
int c1,c2;
if( '\0'==*z1 ){
return -1;
}
while( (sqliteCharVal((unsigned char *)z2) != 0) && (c++)<s){
sqliteNextChar(z2);
}
c = 0;
while( (sqliteCharVal((unsigned char *)z2)) != 0 ){