sqlmath 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/.gitconfig +2 -2
  2. package/.github/workflows/ci.yml +73 -15
  3. package/.github/workflows/publish.yml +66 -0
  4. package/.gitignore +1 -5
  5. package/CHANGELOG.md +53 -2
  6. package/LICENSE +16 -22
  7. package/README.md +18 -219
  8. package/asset_image_folder_open_solid.svg +1 -0
  9. package/jslint.mjs +10998 -0
  10. package/jslint_ci.sh +1542 -728
  11. package/package.json +23 -8
  12. package/{sqlite-autoconf-3360000/sqlite3.c → sqlite3.c} +13116 -85
  13. package/sqlite3_ext.c +8372 -0
  14. package/{sqlite-autoconf-3360000/shell.c → sqlite3_shell.c} +330 -275
  15. package/sqlmath.mjs +1713 -0
  16. package/sqlmath_base.c +1832 -0
  17. package/sqlmath_custom.c +78 -0
  18. package/sqlmath_custom.cpp +0 -0
  19. package/sqlmath_custom.mjs +4 -0
  20. package/sqlmath_old.js +31038 -0
  21. package/extension-functions.c +0 -2047
  22. package/node_sqlite3.cc +0 -11877
  23. package/sqlite-autoconf-3360000/INSTALL +0 -370
  24. package/sqlite-autoconf-3360000/Makefile.am +0 -20
  25. package/sqlite-autoconf-3360000/Makefile.fallback +0 -19
  26. package/sqlite-autoconf-3360000/Makefile.in +0 -1028
  27. package/sqlite-autoconf-3360000/Makefile.msc +0 -1037
  28. package/sqlite-autoconf-3360000/README.txt +0 -113
  29. package/sqlite-autoconf-3360000/Replace.cs +0 -223
  30. package/sqlite-autoconf-3360000/aclocal.m4 +0 -10199
  31. package/sqlite-autoconf-3360000/compile +0 -347
  32. package/sqlite-autoconf-3360000/config.guess +0 -1480
  33. package/sqlite-autoconf-3360000/config.sub +0 -1801
  34. package/sqlite-autoconf-3360000/configure +0 -16135
  35. package/sqlite-autoconf-3360000/configure.ac +0 -285
  36. package/sqlite-autoconf-3360000/depcomp +0 -791
  37. package/sqlite-autoconf-3360000/install-sh +0 -508
  38. package/sqlite-autoconf-3360000/ltmain.sh +0 -11156
  39. package/sqlite-autoconf-3360000/missing +0 -215
  40. package/sqlite-autoconf-3360000/sqlite3.1 +0 -286
  41. package/sqlite-autoconf-3360000/sqlite3.h +0 -12353
  42. package/sqlite-autoconf-3360000/sqlite3.pc.in +0 -13
  43. package/sqlite-autoconf-3360000/sqlite3.rc +0 -83
  44. package/sqlite-autoconf-3360000/sqlite3ext.h +0 -663
  45. package/sqlite-autoconf-3360000/sqlite3rc.h +0 -3
  46. package/sqlite-autoconf-3360000/tea/Makefile.in +0 -440
  47. package/sqlite-autoconf-3360000/tea/README +0 -36
  48. package/sqlite-autoconf-3360000/tea/aclocal.m4 +0 -9
  49. package/sqlite-autoconf-3360000/tea/configure +0 -9989
  50. package/sqlite-autoconf-3360000/tea/configure.ac +0 -201
  51. package/sqlite-autoconf-3360000/tea/doc/sqlite3.n +0 -15
  52. package/sqlite-autoconf-3360000/tea/generic/tclsqlite3.c +0 -4016
  53. package/sqlite-autoconf-3360000/tea/license.terms +0 -6
  54. package/sqlite-autoconf-3360000/tea/pkgIndex.tcl.in +0 -7
  55. package/sqlite-autoconf-3360000/tea/tclconfig/install-sh +0 -528
  56. package/sqlite-autoconf-3360000/tea/tclconfig/tcl.m4 +0 -4168
  57. package/sqlite-autoconf-3360000/tea/win/makefile.vc +0 -419
  58. package/sqlite-autoconf-3360000/tea/win/nmakehlp.c +0 -815
  59. package/sqlite-autoconf-3360000/tea/win/rules.vc +0 -711
  60. package/sqlmath.js +0 -238
  61. package/test/backup.test.js +0 -279
  62. package/test/blob.test.js +0 -54
  63. package/test/cache.test.js +0 -42
  64. package/test/constants.test.js +0 -44
  65. package/test/database_fail.test.js +0 -153
  66. package/test/each.test.js +0 -39
  67. package/test/exec.test.js +0 -39
  68. package/test/extension.test.js +0 -26
  69. package/test/extension_functions.test.js +0 -29
  70. package/test/fts-content.test.js +0 -13
  71. package/test/interrupt.test.js +0 -80
  72. package/test/issue-108.test.js +0 -28
  73. package/test/json.test.js +0 -22
  74. package/test/map.test.js +0 -63
  75. package/test/named_columns.test.js +0 -38
  76. package/test/named_params.test.js +0 -69
  77. package/test/null_error.test.js +0 -41
  78. package/test/nw/.gitignore +0 -3
  79. package/test/nw/Makefile +0 -39
  80. package/test/nw/index.html +0 -14
  81. package/test/nw/package.json +0 -9
  82. package/test/open_close.test.js +0 -187
  83. package/test/other_objects.test.js +0 -98
  84. package/test/parallel_insert.test.js +0 -44
  85. package/test/prepare.test.js +0 -427
  86. package/test/profile.test.js +0 -57
  87. package/test/rerun.test.js +0 -50
  88. package/test/scheduling.test.js +0 -44
  89. package/test/serialization.test.js +0 -104
  90. package/test/support/createdb-electron.js +0 -10
  91. package/test/support/createdb.js +0 -47
  92. package/test/support/elmo.png +0 -0
  93. package/test/support/helper.js +0 -37
  94. package/test/support/script.sql +0 -70
  95. package/test/trace.test.js +0 -67
  96. package/test/unicode.test.js +0 -114
  97. package/test/upsert.test.js +0 -27
  98. package/test/verbose.test.js +0 -60
  99. package/test.js +0 -141
  100. package/test.slr.mjs +0 -212
@@ -1,2047 +0,0 @@
1
- /*jslint-disable*/
2
- /*
3
- shRawLibFetch
4
- {
5
- "fetchList": [
6
- {
7
- "url": "https://github.com/sqlite/sqlite/tree/master/contrib/download/extension-functions.c/download/extension-functions.c",
8
- "url2": "https://www.sqlite.org/contrib/download/extension-functions.c/download/extension-functions.c?get=25"
9
- }
10
- ]
11
- }
12
- - /\\* math.h *\\/
13
- - { "acos", 1, 0, SQLITE_UTF8, 0, acosFunc },
14
- - { "asin", 1, 0, SQLITE_UTF8, 0, asinFunc },
15
- - { "atan", 1, 0, SQLITE_UTF8, 0, atanFunc },
16
- - { "atn2", 2, 0, SQLITE_UTF8, 0, atn2Func },
17
- - /\\* XXX alias *\\/
18
- - { "atan2", 2, 0, SQLITE_UTF8, 0, atn2Func },
19
- - { "acosh", 1, 0, SQLITE_UTF8, 0, acoshFunc },
20
- - { "asinh", 1, 0, SQLITE_UTF8, 0, asinhFunc },
21
- - { "atanh", 1, 0, SQLITE_UTF8, 0, atanhFunc },
22
- -
23
- - { "difference", 2, 0, SQLITE_UTF8, 0, differenceFunc},
24
- - { "degrees", 1, 0, SQLITE_UTF8, 0, rad2degFunc },
25
- - { "radians", 1, 0, SQLITE_UTF8, 0, deg2radFunc },
26
- -
27
- - { "cos", 1, 0, SQLITE_UTF8, 0, cosFunc },
28
- - { "sin", 1, 0, SQLITE_UTF8, 0, sinFunc },
29
- - { "tan", 1, 0, SQLITE_UTF8, 0, tanFunc },
30
- - { "cot", 1, 0, SQLITE_UTF8, 0, cotFunc },
31
- - { "cosh", 1, 0, SQLITE_UTF8, 0, coshFunc },
32
- - { "sinh", 1, 0, SQLITE_UTF8, 0, sinhFunc },
33
- - { "tanh", 1, 0, SQLITE_UTF8, 0, tanhFunc },
34
- - { "coth", 1, 0, SQLITE_UTF8, 0, cothFunc },
35
- -
36
- - { "exp", 1, 0, SQLITE_UTF8, 0, expFunc },
37
- - { "log", 1, 0, SQLITE_UTF8, 0, logFunc },
38
- - { "log10", 1, 0, SQLITE_UTF8, 0, log10Func },
39
- - { "power", 2, 0, SQLITE_UTF8, 0, powerFunc },
40
- - { "sign", 1, 0, SQLITE_UTF8, 0, signFunc },
41
- - { "sqrt", 1, 0, SQLITE_UTF8, 0, sqrtFunc },
42
- - { "square", 1, 0, SQLITE_UTF8, 0, squareFunc },
43
- -
44
- - { "ceil", 1, 0, SQLITE_UTF8, 0, ceilFunc },
45
- - { "floor", 1, 0, SQLITE_UTF8, 0, floorFunc },
46
- -
47
- - { "pi", 0, 0, SQLITE_UTF8, 1, piFunc },
48
- +// hack-sqlite3
49
- + /\\* math.h *\\/
50
- + { "atn2", 2, 0, SQLITE_UTF8, 0, atn2Func },
51
- + { "cot", 1, 0, SQLITE_UTF8, 0, cotFunc },
52
- + { "coth", 1, 0, SQLITE_UTF8, 0, cothFunc },
53
- + { "difference", 2, 0, SQLITE_UTF8, 0, differenceFunc},
54
- + { "square", 1, 0, SQLITE_UTF8, 0, squareFunc },
55
-
56
- - zl = sqlite3Utf8CharLen(zi, -1);
57
- +// hack-sqlite3
58
- + zl = sqlite3Utf8CharLen2(zi, -1);
59
-
60
- - zl = sqlite3Utf8CharLen(zi, -1);
61
- +// hack-sqlite3
62
- + zl = sqlite3Utf8CharLen2(zi, -1);
63
-
64
- - zl = sqlite3Utf8CharLen(zi, -1);
65
- +// hack-sqlite3
66
- + zl = sqlite3Utf8CharLen2(zi, -1);
67
-
68
- - READ_UTF8(z, c);
69
- +// hack-sqlite3
70
- + READ_UTF8_2(z, c);
71
-
72
- -#define READ_UTF8(zIn, c) { \
73
- +// hack-sqlite3
74
- +#define READ_UTF8_2(zIn, c) { \
75
-
76
- -#include "sqlite3ext.h"
77
- +// hack-sqlite3
78
- +// #include "sqlite3ext.h"
79
-
80
- -GEN_MATH_WRAP_DOUBLE_1(logFunc, log)
81
- +// hack-sqlite3
82
- +GEN_MATH_WRAP_DOUBLE_1(logFunc2, log)
83
-
84
- -static int sqlite3Utf8CharLen(const char *z, int nByte){
85
- +// hack-sqlite3
86
- +static int sqlite3Utf8CharLen2(const char *z, int nByte){
87
-
88
- -static void piFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
89
- +// hack-sqlite3
90
- +static void piFunc2(sqlite3_context *context, int argc, sqlite3_value **argv){
91
-
92
- -static void signFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
93
- +// hack-sqlite3
94
- +static void signFunc2(sqlite3_context *context, int argc, sqlite3_value **argv){
95
-
96
- -typedef uint8_t u8;
97
- -typedef uint16_t u16;
98
- -typedef int64_t i64;
99
- +// hack-sqlite3
100
- +// typedef uint8_t u8;
101
- +// typedef uint16_t u16;
102
- +// typedef int64_t i64;
103
- */
104
- /*jslint-enable*/
105
-
106
-
107
- /*
108
- repo https://github.com/sqlite/sqlite/tree/master
109
- committed 2021-07-31T20:30:41Z
110
- */
111
-
112
-
113
- /*
114
- file https://github.com/sqlite/sqlite/tree/master/contrib/download/extension-functions.c/download/extension-functions.c
115
- */
116
- /*
117
- This library will provide common mathematical and string functions in
118
- SQL queries using the operating system libraries or provided
119
- definitions. It includes the following functions:
120
-
121
- Math: acos, asin, atan, atn2, atan2, acosh, asinh, atanh, difference,
122
- degrees, radians, cos, sin, tan, cot, cosh, sinh, tanh, coth, exp,
123
- log, log10, power, sign, sqrt, square, ceil, floor, pi.
124
-
125
- String: replicate, charindex, leftstr, rightstr, ltrim, rtrim, trim,
126
- replace, reverse, proper, padl, padr, padc, strfilter.
127
-
128
- Aggregate: stdev, variance, mode, median, lower_quartile,
129
- upper_quartile.
130
-
131
- The string functions ltrim, rtrim, trim, replace are included in
132
- recent versions of SQLite and so by default do not build.
133
-
134
- Compilation instructions:
135
- Compile this C source file into a dynamic library as follows:
136
- * Linux:
137
- gcc -fPIC -lm -shared extension-functions.c -o libsqlitefunctions.so
138
- * Mac OS X:
139
- gcc -fno-common -dynamiclib extension-functions.c -o libsqlitefunctions.dylib
140
- (You may need to add flags
141
- -I /opt/local/include/ -L/opt/local/lib -lsqlite3
142
- if your sqlite3 is installed from Mac ports, or
143
- -I /sw/include/ -L/sw/lib -lsqlite3
144
- if installed with Fink.)
145
- * Windows:
146
- 1. Install MinGW (http://www.mingw.org/) and you will get the gcc
147
- (gnu compiler collection)
148
- 2. add the path to your path variable (isn't done during the
149
- installation!)
150
- 3. compile:
151
- gcc -shared -I "path" -o libsqlitefunctions.so extension-functions.c
152
- (path = path of sqlite3ext.h; i.e. C:\programs\sqlite)
153
-
154
- Usage instructions for applications calling the sqlite3 API functions:
155
- In your application, call sqlite3_enable_load_extension(db,1) to
156
- allow loading external libraries. Then load the library libsqlitefunctions
157
- using sqlite3_load_extension; the third argument should be 0.
158
- See http://www.sqlite.org/cvstrac/wiki?p=LoadableExtensions.
159
- Select statements may now use these functions, as in
160
- SELECT cos(radians(inclination)) FROM satsum WHERE satnum = 25544;
161
-
162
- Usage instructions for the sqlite3 program:
163
- If the program is built so that loading extensions is permitted,
164
- the following will work:
165
- sqlite> SELECT load_extension('./libsqlitefunctions.so');
166
- sqlite> select cos(radians(45));
167
- 0.707106781186548
168
- Note: Loading extensions is by default prohibited as a
169
- security measure; see "Security Considerations" in
170
- http://www.sqlite.org/cvstrac/wiki?p=LoadableExtensions.
171
- If the sqlite3 program and library are built this
172
- way, you cannot use these functions from the program, you
173
- must write your own program using the sqlite3 API, and call
174
- sqlite3_enable_load_extension as described above, or else
175
- rebuilt the sqlite3 program to allow loadable extensions.
176
-
177
- Alterations:
178
- The instructions are for Linux, Mac OS X, and Windows; users of other
179
- OSes may need to modify this procedure. In particular, if your math
180
- library lacks one or more of the needed trig or log functions, comment
181
- out the appropriate HAVE_ #define at the top of file. If you do not
182
- wish to make a loadable module, comment out the define for
183
- COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE. If you are using a
184
- version of SQLite without the trim functions and replace, comment out
185
- the HAVE_TRIM #define.
186
-
187
- Liam Healy
188
-
189
- History:
190
- 2010-01-06 Correct check for argc in squareFunc, and add Windows
191
- compilation instructions.
192
- 2009-06-24 Correct check for argc in properFunc.
193
- 2008-09-14 Add check that memory was actually allocated after
194
- sqlite3_malloc or sqlite3StrDup, call sqlite3_result_error_nomem if
195
- not. Thanks to Robert Simpson.
196
- 2008-06-13 Change to instructions to indicate use of the math library
197
- and that program might work.
198
- 2007-10-01 Minor clarification to instructions.
199
- 2007-09-29 Compilation as loadable module is optional with
200
- COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE.
201
- 2007-09-28 Use sqlite3_extension_init and macros
202
- SQLITE_EXTENSION_INIT1, SQLITE_EXTENSION_INIT2, so that it works with
203
- sqlite3_load_extension. Thanks to Eric Higashino and Joe Wilson.
204
- New instructions for Mac compilation.
205
- 2007-09-17 With help from Joe Wilson and Nuno Luca, made use of
206
- external interfaces so that compilation is no longer dependent on
207
- SQLite source code. Merged source, header, and README into a single
208
- file. Added casts so that Mac will compile without warnings (unsigned
209
- and signed char).
210
- 2007-09-05 Included some definitions from sqlite 3.3.13 so that this
211
- will continue to work in newer versions of sqlite. Completed
212
- description of functions available.
213
- 2007-03-27 Revised description.
214
- 2007-03-23 Small cleanup and a bug fix on the code. This was mainly
215
- letting errno flag errors encountered in the math library and checking
216
- the result, rather than pre-checking. This fixes a bug in power that
217
- would cause an error if any non-positive number was raised to any
218
- power.
219
- 2007-02-07 posted by Mikey C to sqlite mailing list.
220
- Original code 2006 June 05 by relicoder.
221
-
222
- */
223
-
224
- //#include "config.h"
225
-
226
- #define COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE 1
227
- #define HAVE_ACOSH 1
228
- #define HAVE_ASINH 1
229
- #define HAVE_ATANH 1
230
- #define HAVE_SINH 1
231
- #define HAVE_COSH 1
232
- #define HAVE_TANH 1
233
- #define HAVE_LOG10 1
234
- #define HAVE_ISBLANK 1
235
- #define SQLITE_SOUNDEX 1
236
- #define HAVE_TRIM 1 /* LMH 2007-03-25 if sqlite has trim functions */
237
-
238
- #ifdef COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE
239
- // hack-sqlite3
240
- // #include "sqlite3ext.h"
241
- SQLITE_EXTENSION_INIT1
242
- #else
243
- #include "sqlite3.h"
244
- #endif
245
-
246
- #include <ctype.h>
247
- /* relicoder */
248
- #include <math.h>
249
- #include <string.h>
250
- #include <stdio.h>
251
- #include <errno.h> /* LMH 2007-03-25 */
252
-
253
- #include <stdlib.h>
254
- #include <assert.h>
255
-
256
- #ifndef _MAP_H_
257
- #define _MAP_H_
258
-
259
- #include <stdint.h>
260
-
261
- /*
262
- ** Simple binary tree implementation to use in median, mode and quartile calculations
263
- ** Tree is not necessarily balanced. That would require something like red&black trees of AVL
264
- */
265
-
266
- typedef int(*cmp_func)(const void *, const void *);
267
- typedef void(*map_iterator)(void*, int64_t, void*);
268
-
269
- typedef struct node{
270
- struct node *l;
271
- struct node *r;
272
- void* data;
273
- int64_t count;
274
- } node;
275
-
276
- typedef struct map{
277
- node *base;
278
- cmp_func cmp;
279
- short free;
280
- } map;
281
-
282
- /*
283
- ** creates a map given a comparison function
284
- */
285
- map map_make(cmp_func cmp);
286
-
287
- /*
288
- ** inserts the element e into map m
289
- */
290
- void map_insert(map *m, void *e);
291
-
292
- /*
293
- ** executes function iter over all elements in the map, in key increasing order
294
- */
295
- void map_iterate(map *m, map_iterator iter, void* p);
296
-
297
- /*
298
- ** frees all memory used by a map
299
- */
300
- void map_destroy(map *m);
301
-
302
- /*
303
- ** compares 2 integers
304
- ** to use with map_make
305
- */
306
- int int_cmp(const void *a, const void *b);
307
-
308
- /*
309
- ** compares 2 doubles
310
- ** to use with map_make
311
- */
312
- int double_cmp(const void *a, const void *b);
313
-
314
- #endif /* _MAP_H_ */
315
-
316
- // hack-sqlite3
317
- // typedef uint8_t u8;
318
- // typedef uint16_t u16;
319
- // typedef int64_t i64;
320
-
321
- static char *sqlite3StrDup( const char *z ) {
322
- char *res = sqlite3_malloc( strlen(z)+1 );
323
- return strcpy( res, z );
324
- }
325
-
326
- /*
327
- ** These are copied verbatim from fun.c so as to not have the names exported
328
- */
329
-
330
- /* LMH from sqlite3 3.3.13 */
331
- /*
332
- ** This table maps from the first byte of a UTF-8 character to the number
333
- ** of trailing bytes expected. A value '4' indicates that the table key
334
- ** is not a legal first byte for a UTF-8 character.
335
- */
336
- static const u8 xtra_utf8_bytes[256] = {
337
- /* 0xxxxxxx */
338
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
339
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
340
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
341
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
342
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
343
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
344
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
345
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
346
-
347
- /* 10wwwwww */
348
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
349
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
350
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
351
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
352
-
353
- /* 110yyyyy */
354
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
355
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
356
-
357
- /* 1110zzzz */
358
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
359
-
360
- /* 11110yyy */
361
- 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
362
- };
363
-
364
-
365
- /*
366
- ** This table maps from the number of trailing bytes in a UTF-8 character
367
- ** to an integer constant that is effectively calculated for each character
368
- ** read by a naive implementation of a UTF-8 character reader. The code
369
- ** in the READ_UTF8 macro explains things best.
370
- */
371
- static const int xtra_utf8_bits[] = {
372
- 0,
373
- 12416, /* (0xC0 << 6) + (0x80) */
374
- 925824, /* (0xE0 << 12) + (0x80 << 6) + (0x80) */
375
- 63447168 /* (0xF0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80 */
376
- };
377
-
378
- /*
379
- ** If a UTF-8 character contains N bytes extra bytes (N bytes follow
380
- ** the initial byte so that the total character length is N+1) then
381
- ** masking the character with utf8_mask[N] must produce a non-zero
382
- ** result. Otherwise, we have an (illegal) overlong encoding.
383
- */
384
- static const int utf_mask[] = {
385
- 0x00000000,
386
- 0xffffff80,
387
- 0xfffff800,
388
- 0xffff0000,
389
- };
390
-
391
- /* LMH salvaged from sqlite3 3.3.13 source code src/utf.c */
392
- // hack-sqlite3
393
- #define READ_UTF8_2(zIn, c) { \
394
- int xtra; \
395
- c = *(zIn)++; \
396
- xtra = xtra_utf8_bytes[c]; \
397
- switch( xtra ){ \
398
- case 4: c = (int)0xFFFD; break; \
399
- case 3: c = (c<<6) + *(zIn)++; \
400
- case 2: c = (c<<6) + *(zIn)++; \
401
- case 1: c = (c<<6) + *(zIn)++; \
402
- c -= xtra_utf8_bits[xtra]; \
403
- if( (utf_mask[xtra]&c)==0 \
404
- || (c&0xFFFFF800)==0xD800 \
405
- || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
406
- } \
407
- }
408
-
409
- static int sqlite3ReadUtf8(const unsigned char *z){
410
- int c;
411
- // hack-sqlite3
412
- READ_UTF8_2(z, c);
413
- return c;
414
- }
415
-
416
- #define SKIP_UTF8(zIn) { \
417
- zIn += (xtra_utf8_bytes[*(u8 *)zIn] + 1); \
418
- }
419
-
420
- /*
421
- ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
422
- ** return the number of unicode characters in pZ up to (but not including)
423
- ** the first 0x00 byte. If nByte is not less than zero, return the
424
- ** number of unicode characters in the first nByte of pZ (or up to
425
- ** the first 0x00, whichever comes first).
426
- */
427
- // hack-sqlite3
428
- static int sqlite3Utf8CharLen2(const char *z, int nByte){
429
- int r = 0;
430
- const char *zTerm;
431
- if( nByte>=0 ){
432
- zTerm = &z[nByte];
433
- }else{
434
- zTerm = (const char *)(-1);
435
- }
436
- assert( z<=zTerm );
437
- while( *z!=0 && z<zTerm ){
438
- SKIP_UTF8(z);
439
- r++;
440
- }
441
- return r;
442
- }
443
-
444
- /*
445
- ** X is a pointer to the first byte of a UTF-8 character. Increment
446
- ** X so that it points to the next character. This only works right
447
- ** if X points to a well-formed UTF-8 string.
448
- */
449
- #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
450
- #define sqliteCharVal(X) sqlite3ReadUtf8(X)
451
-
452
- /*
453
- ** This is a macro that facilitates writting wrappers for math.h functions
454
- ** it creates code for a function to use in SQlite that gets one numeric input
455
- ** and returns a floating point value.
456
- **
457
- ** Could have been implemented using pointers to functions but this way it's inline
458
- ** and thus more efficient. Lower * ranking though...
459
- **
460
- ** Parameters:
461
- ** name: function name to de defined (eg: sinFunc)
462
- ** function: function defined in math.h to wrap (eg: sin)
463
- ** domain: boolean condition that CAN'T happen in terms of the input parameter rVal
464
- ** (eg: rval<0 for sqrt)
465
- */
466
- /* LMH 2007-03-25 Changed to use errno and remove domain; no pre-checking for errors. */
467
- #define GEN_MATH_WRAP_DOUBLE_1(name, function) \
468
- static void name(sqlite3_context *context, int argc, sqlite3_value **argv){\
469
- double rVal = 0.0, val;\
470
- assert( argc==1 );\
471
- switch( sqlite3_value_type(argv[0]) ){\
472
- case SQLITE_NULL: {\
473
- sqlite3_result_null(context);\
474
- break;\
475
- }\
476
- default: {\
477
- rVal = sqlite3_value_double(argv[0]);\
478
- errno = 0;\
479
- val = function(rVal);\
480
- if (errno == 0) {\
481
- sqlite3_result_double(context, val);\
482
- } else {\
483
- sqlite3_result_error(context, strerror(errno), errno);\
484
- }\
485
- break;\
486
- }\
487
- }\
488
- }\
489
-
490
-
491
- /*
492
- ** Example of GEN_MATH_WRAP_DOUBLE_1 usage
493
- ** this creates function sqrtFunc to wrap the math.h standard function sqrt(x)=x^0.5
494
- */
495
- GEN_MATH_WRAP_DOUBLE_1(sqrtFunc, sqrt)
496
-
497
- /* trignometric functions */
498
- GEN_MATH_WRAP_DOUBLE_1(acosFunc, acos)
499
- GEN_MATH_WRAP_DOUBLE_1(asinFunc, asin)
500
- GEN_MATH_WRAP_DOUBLE_1(atanFunc, atan)
501
-
502
- /*
503
- ** Many of systems don't have inverse hyperbolic trig functions so this will emulate
504
- ** them on those systems in terms of log and sqrt (formulas are too trivial to demand
505
- ** written proof here)
506
- */
507
-
508
- #ifndef HAVE_ACOSH
509
- static double acosh(double x){
510
- return log(x + sqrt(x*x - 1.0));
511
- }
512
- #endif
513
-
514
- GEN_MATH_WRAP_DOUBLE_1(acoshFunc, acosh)
515
-
516
- #ifndef HAVE_ASINH
517
- static double asinh(double x){
518
- return log(x + sqrt(x*x + 1.0));
519
- }
520
- #endif
521
-
522
- GEN_MATH_WRAP_DOUBLE_1(asinhFunc, asinh)
523
-
524
- #ifndef HAVE_ATANH
525
- static double atanh(double x){
526
- return (1.0/2.0)*log((1+x)/(1-x)) ;
527
- }
528
- #endif
529
-
530
- GEN_MATH_WRAP_DOUBLE_1(atanhFunc, atanh)
531
-
532
- /*
533
- ** math.h doesn't require cot (cotangent) so it's defined here
534
- */
535
- static double cot(double x){
536
- return 1.0/tan(x);
537
- }
538
-
539
- GEN_MATH_WRAP_DOUBLE_1(sinFunc, sin)
540
- GEN_MATH_WRAP_DOUBLE_1(cosFunc, cos)
541
- GEN_MATH_WRAP_DOUBLE_1(tanFunc, tan)
542
- GEN_MATH_WRAP_DOUBLE_1(cotFunc, cot)
543
-
544
- static double coth(double x){
545
- return 1.0/tanh(x);
546
- }
547
-
548
- /*
549
- ** Many systems don't have hyperbolic trigonometric functions so this will emulate
550
- ** them on those systems directly from the definition in terms of exp
551
- */
552
- #ifndef HAVE_SINH
553
- static double sinh(double x){
554
- return (exp(x)-exp(-x))/2.0;
555
- }
556
- #endif
557
-
558
- GEN_MATH_WRAP_DOUBLE_1(sinhFunc, sinh)
559
-
560
- #ifndef HAVE_COSH
561
- static double cosh(double x){
562
- return (exp(x)+exp(-x))/2.0;
563
- }
564
- #endif
565
-
566
- GEN_MATH_WRAP_DOUBLE_1(coshFunc, cosh)
567
-
568
- #ifndef HAVE_TANH
569
- static double tanh(double x){
570
- return sinh(x)/cosh(x);
571
- }
572
- #endif
573
-
574
- GEN_MATH_WRAP_DOUBLE_1(tanhFunc, tanh)
575
-
576
- GEN_MATH_WRAP_DOUBLE_1(cothFunc, coth)
577
-
578
- /*
579
- ** Some systems lack log in base 10. This will emulate it
580
- */
581
-
582
- #ifndef HAVE_LOG10
583
- static double log10(double x){
584
- static double l10 = -1.0;
585
- if( l10<0.0 ){
586
- l10 = log(10.0);
587
- }
588
- return log(x)/l10;
589
- }
590
- #endif
591
-
592
- // hack-sqlite3
593
- GEN_MATH_WRAP_DOUBLE_1(logFunc2, log)
594
- GEN_MATH_WRAP_DOUBLE_1(log10Func, log10)
595
- GEN_MATH_WRAP_DOUBLE_1(expFunc, exp)
596
-
597
- /*
598
- ** Fallback for systems where math.h doesn't define M_PI
599
- */
600
- #undef M_PI
601
- #ifndef M_PI
602
- /*
603
- ** static double PI = acos(-1.0);
604
- ** #define M_PI (PI)
605
- */
606
- #define M_PI 3.14159265358979323846
607
- #endif
608
-
609
- /* Convert Degrees into Radians */
610
- static double deg2rad(double x){
611
- return x*M_PI/180.0;
612
- }
613
-
614
- /* Convert Radians into Degrees */
615
- static double rad2deg(double x){
616
- return 180.0*x/M_PI;
617
- }
618
-
619
- GEN_MATH_WRAP_DOUBLE_1(rad2degFunc, rad2deg)
620
- GEN_MATH_WRAP_DOUBLE_1(deg2radFunc, deg2rad)
621
-
622
- /* constant function that returns the value of PI=3.1415... */
623
- // hack-sqlite3
624
- static void piFunc2(sqlite3_context *context, int argc, sqlite3_value **argv){
625
- sqlite3_result_double(context, M_PI);
626
- }
627
-
628
- /*
629
- ** Implements the sqrt function, it has the peculiarity of returning an integer when the
630
- ** the argument is an integer.
631
- ** Since SQLite isn't strongly typed (almost untyped actually) this is a bit pedantic
632
- */
633
- static void squareFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
634
- i64 iVal = 0;
635
- double rVal = 0.0;
636
- assert( argc==1 );
637
- switch( sqlite3_value_type(argv[0]) ){
638
- case SQLITE_INTEGER: {
639
- iVal = sqlite3_value_int64(argv[0]);
640
- sqlite3_result_int64(context, iVal*iVal);
641
- break;
642
- }
643
- case SQLITE_NULL: {
644
- sqlite3_result_null(context);
645
- break;
646
- }
647
- default: {
648
- rVal = sqlite3_value_double(argv[0]);
649
- sqlite3_result_double(context, rVal*rVal);
650
- break;
651
- }
652
- }
653
- }
654
-
655
- /*
656
- ** Wraps the pow math.h function
657
- ** When both the base and the exponent are integers the result should be integer
658
- ** (see sqrt just before this). Here the result is always double
659
- */
660
- /* LMH 2007-03-25 Changed to use errno; no pre-checking for errors. Also removes
661
- but that was present in the pre-checking that called sqlite3_result_error on
662
- a non-positive first argument, which is not always an error. */
663
- static void powerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
664
- double r1 = 0.0;
665
- double r2 = 0.0;
666
- double val;
667
-
668
- assert( argc==2 );
669
-
670
- if( sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL ){
671
- sqlite3_result_null(context);
672
- }else{
673
- r1 = sqlite3_value_double(argv[0]);
674
- r2 = sqlite3_value_double(argv[1]);
675
- errno = 0;
676
- val = pow(r1,r2);
677
- if (errno == 0) {
678
- sqlite3_result_double(context, val);
679
- } else {
680
- sqlite3_result_error(context, strerror(errno), errno);
681
- }
682
- }
683
- }
684
-
685
- /*
686
- ** atan2 wrapper
687
- */
688
- static void atn2Func(sqlite3_context *context, int argc, sqlite3_value **argv){
689
- double r1 = 0.0;
690
- double r2 = 0.0;
691
-
692
- assert( argc==2 );
693
-
694
- if( sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL ){
695
- sqlite3_result_null(context);
696
- }else{
697
- r1 = sqlite3_value_double(argv[0]);
698
- r2 = sqlite3_value_double(argv[1]);
699
- sqlite3_result_double(context, atan2(r1,r2));
700
- }
701
- }
702
-
703
- /*
704
- ** Implementation of the sign() function
705
- ** return one of 3 possibilities +1,0 or -1 when the argument is respectively
706
- ** positive, 0 or negative.
707
- ** When the argument is NULL the result is also NULL (completly conventional)
708
- */
709
- // hack-sqlite3
710
- static void signFunc2(sqlite3_context *context, int argc, sqlite3_value **argv){
711
- double rVal=0.0;
712
- i64 iVal=0;
713
- assert( argc==1 );
714
- switch( sqlite3_value_type(argv[0]) ){
715
- case SQLITE_INTEGER: {
716
- iVal = sqlite3_value_int64(argv[0]);
717
- iVal = ( iVal > 0) ? 1: ( iVal < 0 ) ? -1: 0;
718
- sqlite3_result_int64(context, iVal);
719
- break;
720
- }
721
- case SQLITE_NULL: {
722
- sqlite3_result_null(context);
723
- break;
724
- }
725
- default: {
726
- /* 2nd change below. Line for abs was: if( rVal<0 ) rVal = rVal * -1.0; */
727
-
728
- rVal = sqlite3_value_double(argv[0]);
729
- rVal = ( rVal > 0) ? 1: ( rVal < 0 ) ? -1: 0;
730
- sqlite3_result_double(context, rVal);
731
- break;
732
- }
733
- }
734
- }
735
-
736
-
737
- /*
738
- ** smallest integer value not less than argument
739
- */
740
- static void ceilFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
741
- double rVal=0.0;
742
- i64 iVal=0;
743
- assert( argc==1 );
744
- switch( sqlite3_value_type(argv[0]) ){
745
- case SQLITE_INTEGER: {
746
- i64 iVal = sqlite3_value_int64(argv[0]);
747
- sqlite3_result_int64(context, iVal);
748
- break;
749
- }
750
- case SQLITE_NULL: {
751
- sqlite3_result_null(context);
752
- break;
753
- }
754
- default: {
755
- rVal = sqlite3_value_double(argv[0]);
756
- sqlite3_result_int64(context, (i64) ceil(rVal));
757
- break;
758
- }
759
- }
760
- }
761
-
762
- /*
763
- ** largest integer value not greater than argument
764
- */
765
- static void floorFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
766
- double rVal=0.0;
767
- i64 iVal=0;
768
- assert( argc==1 );
769
- switch( sqlite3_value_type(argv[0]) ){
770
- case SQLITE_INTEGER: {
771
- i64 iVal = sqlite3_value_int64(argv[0]);
772
- sqlite3_result_int64(context, iVal);
773
- break;
774
- }
775
- case SQLITE_NULL: {
776
- sqlite3_result_null(context);
777
- break;
778
- }
779
- default: {
780
- rVal = sqlite3_value_double(argv[0]);
781
- sqlite3_result_int64(context, (i64) floor(rVal));
782
- break;
783
- }
784
- }
785
- }
786
-
787
- /*
788
- ** Given a string (s) in the first argument and an integer (n) in the second returns the
789
- ** string that constains s contatenated n times
790
- */
791
- static void replicateFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
792
- unsigned char *z; /* input string */
793
- unsigned char *zo; /* result string */
794
- i64 iCount; /* times to repeat */
795
- i64 nLen; /* length of the input string (no multibyte considerations) */
796
- i64 nTLen; /* length of the result string (no multibyte considerations) */
797
- i64 i=0;
798
-
799
- if( argc!=2 || SQLITE_NULL==sqlite3_value_type(argv[0]) )
800
- return;
801
-
802
- iCount = sqlite3_value_int64(argv[1]);
803
-
804
- if( iCount<0 ){
805
- sqlite3_result_error(context, "domain error", -1);
806
- }else{
807
-
808
- nLen = sqlite3_value_bytes(argv[0]);
809
- nTLen = nLen*iCount;
810
- z=sqlite3_malloc(nTLen+1);
811
- zo=sqlite3_malloc(nLen+1);
812
- if (!z || !zo){
813
- sqlite3_result_error_nomem(context);
814
- if (z) sqlite3_free(z);
815
- if (zo) sqlite3_free(zo);
816
- return;
817
- }
818
- strcpy((char*)zo, (char*)sqlite3_value_text(argv[0]));
819
-
820
- for(i=0; i<iCount; ++i){
821
- strcpy((char*)(z+i*nLen), (char*)zo);
822
- }
823
-
824
- sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
825
- sqlite3_free(z);
826
- sqlite3_free(zo);
827
- }
828
- }
829
-
830
- /*
831
- ** Some systems (win32 among others) don't have an isblank function, this will emulate it.
832
- ** This function is not UFT-8 safe since it only analyses a byte character.
833
- */
834
- #ifndef HAVE_ISBLANK
835
- int isblank(char c){
836
- return( ' '==c || '\t'==c );
837
- }
838
- #endif
839
-
840
- static void properFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
841
- const unsigned char *z; /* input string */
842
- unsigned char *zo; /* output string */
843
- unsigned char *zt; /* iterator */
844
- char r;
845
- int c=1;
846
-
847
- assert( argc==1);
848
- if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
849
- sqlite3_result_null(context);
850
- return;
851
- }
852
-
853
- z = sqlite3_value_text(argv[0]);
854
- zo = (unsigned char *)sqlite3StrDup((char *) z);
855
- if (!zo) {
856
- sqlite3_result_error_nomem(context);
857
- return;
858
- }
859
- zt = zo;
860
-
861
- while( (r = *(z++))!=0 ){
862
- if( isblank(r) ){
863
- c=1;
864
- }else{
865
- if( c==1 ){
866
- r = toupper(r);
867
- }else{
868
- r = tolower(r);
869
- }
870
- c=0;
871
- }
872
- *(zt++) = r;
873
- }
874
- *zt = '\0';
875
-
876
- sqlite3_result_text(context, (char*)zo, -1, SQLITE_TRANSIENT);
877
- sqlite3_free(zo);
878
- }
879
-
880
- /*
881
- ** given an input string (s) and an integer (n) adds spaces at the begining of s
882
- ** until it has a length of n characters.
883
- ** When s has a length >=n it's a NOP
884
- ** padl(NULL) = NULL
885
- */
886
- static void padlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
887
- i64 ilen; /* length to pad to */
888
- i64 zl; /* length of the input string (UTF-8 chars) */
889
- int i = 0;
890
- const char *zi; /* input string */
891
- char *zo; /* output string */
892
- char *zt;
893
-
894
- assert( argc==2 );
895
-
896
- if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
897
- sqlite3_result_null(context);
898
- }else{
899
- zi = (char *)sqlite3_value_text(argv[0]);
900
- ilen = sqlite3_value_int64(argv[1]);
901
- /* check domain */
902
- if(ilen<0){
903
- sqlite3_result_error(context, "domain error", -1);
904
- return;
905
- }
906
- // hack-sqlite3
907
- zl = sqlite3Utf8CharLen2(zi, -1);
908
- if( zl>=ilen ){
909
- /* string is longer than the requested pad length, return the same string (dup it) */
910
- zo = sqlite3StrDup(zi);
911
- if (!zo){
912
- sqlite3_result_error_nomem(context);
913
- return;
914
- }
915
- sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
916
- }else{
917
- zo = sqlite3_malloc(strlen(zi)+ilen-zl+1);
918
- if (!zo){
919
- sqlite3_result_error_nomem(context);
920
- return;
921
- }
922
- zt = zo;
923
- for(i=1; i+zl<=ilen; ++i){
924
- *(zt++)=' ';
925
- }
926
- /* no need to take UTF-8 into consideration here */
927
- strcpy(zt,zi);
928
- }
929
- sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
930
- sqlite3_free(zo);
931
- }
932
- }
933
-
934
- /*
935
- ** given an input string (s) and an integer (n) appends spaces at the end of s
936
- ** until it has a length of n characters.
937
- ** When s has a length >=n it's a NOP
938
- ** padl(NULL) = NULL
939
- */
940
- static void padrFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
941
- i64 ilen; /* length to pad to */
942
- i64 zl; /* length of the input string (UTF-8 chars) */
943
- i64 zll; /* length of the input string (bytes) */
944
- int i = 0;
945
- const char *zi; /* input string */
946
- char *zo; /* output string */
947
- char *zt;
948
-
949
- assert( argc==2 );
950
-
951
- if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
952
- sqlite3_result_null(context);
953
- }else{
954
- zi = (char *)sqlite3_value_text(argv[0]);
955
- ilen = sqlite3_value_int64(argv[1]);
956
- /* check domain */
957
- if(ilen<0){
958
- sqlite3_result_error(context, "domain error", -1);
959
- return;
960
- }
961
- // hack-sqlite3
962
- zl = sqlite3Utf8CharLen2(zi, -1);
963
- if( zl>=ilen ){
964
- /* string is longer than the requested pad length, return the same string (dup it) */
965
- zo = sqlite3StrDup(zi);
966
- if (!zo){
967
- sqlite3_result_error_nomem(context);
968
- return;
969
- }
970
- sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
971
- }else{
972
- zll = strlen(zi);
973
- zo = sqlite3_malloc(zll+ilen-zl+1);
974
- if (!zo){
975
- sqlite3_result_error_nomem(context);
976
- return;
977
- }
978
- zt = strcpy(zo,zi)+zll;
979
- for(i=1; i+zl<=ilen; ++i){
980
- *(zt++) = ' ';
981
- }
982
- *zt = '\0';
983
- }
984
- sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
985
- sqlite3_free(zo);
986
- }
987
- }
988
-
989
- /*
990
- ** given an input string (s) and an integer (n) appends spaces at the end of s
991
- ** and adds spaces at the begining of s until it has a length of n characters.
992
- ** Tries to add has many characters at the left as at the right.
993
- ** When s has a length >=n it's a NOP
994
- ** padl(NULL) = NULL
995
- */
996
- static void padcFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
997
- i64 ilen; /* length to pad to */
998
- i64 zl; /* length of the input string (UTF-8 chars) */
999
- i64 zll; /* length of the input string (bytes) */
1000
- int i = 0;
1001
- const char *zi; /* input string */
1002
- char *zo; /* output string */
1003
- char *zt;
1004
-
1005
- assert( argc==2 );
1006
-
1007
- if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
1008
- sqlite3_result_null(context);
1009
- }else{
1010
- zi = (char *)sqlite3_value_text(argv[0]);
1011
- ilen = sqlite3_value_int64(argv[1]);
1012
- /* check domain */
1013
- if(ilen<0){
1014
- sqlite3_result_error(context, "domain error", -1);
1015
- return;
1016
- }
1017
- // hack-sqlite3
1018
- zl = sqlite3Utf8CharLen2(zi, -1);
1019
- if( zl>=ilen ){
1020
- /* string is longer than the requested pad length, return the same string (dup it) */
1021
- zo = sqlite3StrDup(zi);
1022
- if (!zo){
1023
- sqlite3_result_error_nomem(context);
1024
- return;
1025
- }
1026
- sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
1027
- }else{
1028
- zll = strlen(zi);
1029
- zo = sqlite3_malloc(zll+ilen-zl+1);
1030
- if (!zo){
1031
- sqlite3_result_error_nomem(context);
1032
- return;
1033
- }
1034
- zt = zo;
1035
- for(i=1; 2*i+zl<=ilen; ++i){
1036
- *(zt++) = ' ';
1037
- }
1038
- strcpy(zt, zi);
1039
- zt+=zll;
1040
- for(; i+zl<=ilen; ++i){
1041
- *(zt++) = ' ';
1042
- }
1043
- *zt = '\0';
1044
- }
1045
- sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
1046
- sqlite3_free(zo);
1047
- }
1048
- }
1049
-
1050
- /*
1051
- ** given 2 string (s1,s2) returns the string s1 with the characters NOT in s2 removed
1052
- ** assumes strings are UTF-8 encoded
1053
- */
1054
- static void strfilterFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1055
- const char *zi1; /* first parameter string (searched string) */
1056
- const char *zi2; /* second parameter string (vcontains valid characters) */
1057
- const char *z1;
1058
- const char *z21;
1059
- const char *z22;
1060
- char *zo; /* output string */
1061
- char *zot;
1062
- int c1 = 0;
1063
- int c2 = 0;
1064
-
1065
- assert( argc==2 );
1066
-
1067
- if( sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL ){
1068
- sqlite3_result_null(context);
1069
- }else{
1070
- zi1 = (char *)sqlite3_value_text(argv[0]);
1071
- zi2 = (char *)sqlite3_value_text(argv[1]);
1072
- /*
1073
- ** maybe I could allocate less, but that would imply 2 passes, rather waste
1074
- ** (possibly) some memory
1075
- */
1076
- zo = sqlite3_malloc(strlen(zi1)+1);
1077
- if (!zo){
1078
- sqlite3_result_error_nomem(context);
1079
- return;
1080
- }
1081
- zot = zo;
1082
- z1 = zi1;
1083
- while( (c1=sqliteCharVal((unsigned char *)z1))!=0 ){
1084
- z21=zi2;
1085
- while( (c2=sqliteCharVal((unsigned char *)z21))!=0 && c2!=c1 ){
1086
- sqliteNextChar(z21);
1087
- }
1088
- if( c2!=0){
1089
- z22=z21;
1090
- sqliteNextChar(z22);
1091
- strncpy(zot, z21, z22-z21);
1092
- zot+=z22-z21;
1093
- }
1094
- sqliteNextChar(z1);
1095
- }
1096
- *zot = '\0';
1097
-
1098
- sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
1099
- sqlite3_free(zo);
1100
- }
1101
- }
1102
-
1103
- /*
1104
- ** Given a string z1, retutns the (0 based) index of it's first occurence
1105
- ** in z2 after the first s characters.
1106
- ** Returns -1 when there isn't a match.
1107
- ** updates p to point to the character where the match occured.
1108
- ** This is an auxiliary function.
1109
- */
1110
- static int _substr(const char* z1, const char* z2, int s, const char** p){
1111
- int c = 0;
1112
- int rVal=-1;
1113
- const char* zt1;
1114
- const char* zt2;
1115
- int c1,c2;
1116
-
1117
- if( '\0'==*z1 ){
1118
- return -1;
1119
- }
1120
-
1121
- while( (sqliteCharVal((unsigned char *)z2) != 0) && (c++)<s){
1122
- sqliteNextChar(z2);
1123
- }
1124
-
1125
- c = 0;
1126
- while( (sqliteCharVal((unsigned char *)z2)) != 0 ){
1127
- zt1 = z1;
1128
- zt2 = z2;
1129
-
1130
- do{
1131
- c1 = sqliteCharVal((unsigned char *)zt1);
1132
- c2 = sqliteCharVal((unsigned char *)zt2);
1133
- sqliteNextChar(zt1);
1134
- sqliteNextChar(zt2);
1135
- }while( c1 == c2 && c1 != 0 && c2 != 0 );
1136
-
1137
- if( c1 == 0 ){
1138
- rVal = c;
1139
- break;
1140
- }
1141
-
1142
- sqliteNextChar(z2);
1143
- ++c;
1144
- }
1145
- if(p){
1146
- *p=z2;
1147
- }
1148
- return rVal >=0 ? rVal+s : rVal;
1149
- }
1150
-
1151
- /*
1152
- ** given 2 input strings (s1,s2) and an integer (n) searches from the nth character
1153
- ** for the string s1. Returns the position where the match occured.
1154
- ** Characters are counted from 1.
1155
- ** 0 is returned when no match occurs.
1156
- */
1157
-
1158
- static void charindexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1159
- const u8 *z1; /* s1 string */
1160
- u8 *z2; /* s2 string */
1161
- int s=0;
1162
- int rVal=0;
1163
-
1164
- assert( argc==3 ||argc==2);
1165
-
1166
- if( SQLITE_NULL==sqlite3_value_type(argv[0]) || SQLITE_NULL==sqlite3_value_type(argv[1])){
1167
- sqlite3_result_null(context);
1168
- return;
1169
- }
1170
-
1171
- z1 = sqlite3_value_text(argv[0]);
1172
- if( z1==0 ) return;
1173
- z2 = (u8*) sqlite3_value_text(argv[1]);
1174
- if(argc==3){
1175
- s = sqlite3_value_int(argv[2])-1;
1176
- if(s<0){
1177
- s=0;
1178
- }
1179
- }else{
1180
- s = 0;
1181
- }
1182
-
1183
- rVal = _substr((char *)z1,(char *)z2,s,NULL);
1184
- sqlite3_result_int(context, rVal+1);
1185
- }
1186
-
1187
- /*
1188
- ** given a string (s) and an integer (n) returns the n leftmost (UTF-8) characters
1189
- ** if the string has a length<=n or is NULL this function is NOP
1190
- */
1191
- static void leftFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1192
- int c=0;
1193
- int cc=0;
1194
- int l=0;
1195
- const unsigned char *z; /* input string */
1196
- const unsigned char *zt;
1197
- unsigned char *rz; /* output string */
1198
-
1199
- assert( argc==2);
1200
-
1201
- if( SQLITE_NULL==sqlite3_value_type(argv[0]) || SQLITE_NULL==sqlite3_value_type(argv[1])){
1202
- sqlite3_result_null(context);
1203
- return;
1204
- }
1205
-
1206
- z = sqlite3_value_text(argv[0]);
1207
- l = sqlite3_value_int(argv[1]);
1208
- zt = z;
1209
-
1210
- while( sqliteCharVal(zt) && c++<l)
1211
- sqliteNextChar(zt);
1212
-
1213
- cc=zt-z;
1214
-
1215
- rz = sqlite3_malloc(zt-z+1);
1216
- if (!rz){
1217
- sqlite3_result_error_nomem(context);
1218
- return;
1219
- }
1220
- strncpy((char*) rz, (char*) z, zt-z);
1221
- *(rz+cc) = '\0';
1222
- sqlite3_result_text(context, (char*)rz, -1, SQLITE_TRANSIENT);
1223
- sqlite3_free(rz);
1224
- }
1225
-
1226
- /*
1227
- ** given a string (s) and an integer (n) returns the n rightmost (UTF-8) characters
1228
- ** if the string has a length<=n or is NULL this function is NOP
1229
- */
1230
- static void rightFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1231
- int l=0;
1232
- int c=0;
1233
- int cc=0;
1234
- const char *z;
1235
- const char *zt;
1236
- const char *ze;
1237
- char *rz;
1238
-
1239
- assert( argc==2);
1240
-
1241
- if( SQLITE_NULL == sqlite3_value_type(argv[0]) || SQLITE_NULL == sqlite3_value_type(argv[1])){
1242
- sqlite3_result_null(context);
1243
- return;
1244
- }
1245
-
1246
- z = (char *)sqlite3_value_text(argv[0]);
1247
- l = sqlite3_value_int(argv[1]);
1248
- zt = z;
1249
-
1250
- while( sqliteCharVal((unsigned char *)zt)!=0){
1251
- sqliteNextChar(zt);
1252
- ++c;
1253
- }
1254
-
1255
- ze = zt;
1256
- zt = z;
1257
-
1258
- cc=c-l;
1259
- if(cc<0)
1260
- cc=0;
1261
-
1262
- while( cc-- > 0 ){
1263
- sqliteNextChar(zt);
1264
- }
1265
-
1266
- rz = sqlite3_malloc(ze-zt+1);
1267
- if (!rz){
1268
- sqlite3_result_error_nomem(context);
1269
- return;
1270
- }
1271
- strcpy((char*) rz, (char*) (zt));
1272
- sqlite3_result_text(context, (char*)rz, -1, SQLITE_TRANSIENT);
1273
- sqlite3_free(rz);
1274
- }
1275
-
1276
- #ifndef HAVE_TRIM
1277
- /*
1278
- ** removes the whitespaces at the begining of a string.
1279
- */
1280
- const char* ltrim(const char* s){
1281
- while( *s==' ' )
1282
- ++s;
1283
- return s;
1284
- }
1285
-
1286
- /*
1287
- ** removes the whitespaces at the end of a string.
1288
- ** !mutates the input string!
1289
- */
1290
- void rtrim(char* s){
1291
- char* ss = s+strlen(s)-1;
1292
- while( ss>=s && *ss==' ' )
1293
- --ss;
1294
- *(ss+1)='\0';
1295
- }
1296
-
1297
- /*
1298
- ** Removes the whitespace at the begining of a string
1299
- */
1300
- static void ltrimFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1301
- const char *z;
1302
-
1303
- assert( argc==1);
1304
-
1305
- if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
1306
- sqlite3_result_null(context);
1307
- return;
1308
- }
1309
- z = sqlite3_value_text(argv[0]);
1310
- sqlite3_result_text(context, ltrim(z), -1, SQLITE_TRANSIENT);
1311
- }
1312
-
1313
- /*
1314
- ** Removes the whitespace at the end of a string
1315
- */
1316
- static void rtrimFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1317
- const char *z;
1318
- char *rz;
1319
- /* try not to change data in argv */
1320
-
1321
- assert( argc==1);
1322
-
1323
- if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
1324
- sqlite3_result_null(context);
1325
- return;
1326
- }
1327
- z = sqlite3_value_text(argv[0]);
1328
- rz = sqlite3StrDup(z);
1329
- rtrim(rz);
1330
- sqlite3_result_text(context, rz, -1, SQLITE_TRANSIENT);
1331
- sqlite3_free(rz);
1332
- }
1333
-
1334
- /*
1335
- ** Removes the whitespace at the begining and end of a string
1336
- */
1337
- static void trimFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1338
- const char *z;
1339
- char *rz;
1340
- /* try not to change data in argv */
1341
-
1342
- assert( argc==1);
1343
-
1344
- if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
1345
- sqlite3_result_null(context);
1346
- return;
1347
- }
1348
- z = sqlite3_value_text(argv[0]);
1349
- rz = sqlite3StrDup(z);
1350
- rtrim(rz);
1351
- sqlite3_result_text(context, ltrim(rz), -1, SQLITE_TRANSIENT);
1352
- sqlite3_free(rz);
1353
- }
1354
- #endif
1355
-
1356
- /*
1357
- ** given a pointer to a string s1, the length of that string (l1), a new string (s2)
1358
- ** and it's length (l2) appends s2 to s1.
1359
- ** All lengths in bytes.
1360
- ** This is just an auxiliary function
1361
- */
1362
- // static void _append(char **s1, int l1, const char *s2, int l2){
1363
- // *s1 = realloc(*s1, (l1+l2+1)*sizeof(char));
1364
- // strncpy((*s1)+l1, s2, l2);
1365
- // *(*(s1)+l1+l2) = '\0';
1366
- // }
1367
-
1368
- #ifndef HAVE_TRIM
1369
-
1370
- /*
1371
- ** given strings s, s1 and s2 replaces occurrences of s1 in s by s2
1372
- */
1373
- static void replaceFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1374
- const char *z1; /* string s (first parameter) */
1375
- const char *z2; /* string s1 (second parameter) string to look for */
1376
- const char *z3; /* string s2 (third parameter) string to replace occurrences of s1 with */
1377
- int lz1;
1378
- int lz2;
1379
- int lz3;
1380
- int lzo=0;
1381
- char *zo=0;
1382
- int ret=0;
1383
- const char *zt1;
1384
- const char *zt2;
1385
-
1386
- assert( 3==argc );
1387
-
1388
- if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
1389
- sqlite3_result_null(context);
1390
- return;
1391
- }
1392
-
1393
- z1 = sqlite3_value_text(argv[0]);
1394
- z2 = sqlite3_value_text(argv[1]);
1395
- z3 = sqlite3_value_text(argv[2]);
1396
- /* handle possible null values */
1397
- if( 0==z2 ){
1398
- z2="";
1399
- }
1400
- if( 0==z3 ){
1401
- z3="";
1402
- }
1403
-
1404
- lz1 = strlen(z1);
1405
- lz2 = strlen(z2);
1406
- lz3 = strlen(z3);
1407
-
1408
- #if 0
1409
- /* special case when z2 is empty (or null) nothing will be changed */
1410
- if( 0==lz2 ){
1411
- sqlite3_result_text(context, z1, -1, SQLITE_TRANSIENT);
1412
- return;
1413
- }
1414
- #endif
1415
-
1416
- zt1=z1;
1417
- zt2=z1;
1418
-
1419
- while(1){
1420
- ret=_substr(z2,zt1 , 0, &zt2);
1421
-
1422
- if( ret<0 )
1423
- break;
1424
-
1425
- _append(&zo, lzo, zt1, zt2-zt1);
1426
- lzo+=zt2-zt1;
1427
- _append(&zo, lzo, z3, lz3);
1428
- lzo+=lz3;
1429
-
1430
- zt1=zt2+lz2;
1431
- }
1432
- _append(&zo, lzo, zt1, lz1-(zt1-z1));
1433
- sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
1434
- sqlite3_free(zo);
1435
- }
1436
- #endif
1437
-
1438
- /*
1439
- ** given a string returns the same string but with the characters in reverse order
1440
- */
1441
- static void reverseFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1442
- const char *z;
1443
- const char *zt;
1444
- char *rz;
1445
- char *rzt;
1446
- int l = 0;
1447
- int i = 0;
1448
-
1449
- assert( 1==argc );
1450
-
1451
- if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
1452
- sqlite3_result_null(context);
1453
- return;
1454
- }
1455
- z = (char *)sqlite3_value_text(argv[0]);
1456
- l = strlen(z);
1457
- rz = sqlite3_malloc(l+1);
1458
- if (!rz){
1459
- sqlite3_result_error_nomem(context);
1460
- return;
1461
- }
1462
- rzt = rz+l;
1463
- *(rzt--) = '\0';
1464
-
1465
- zt=z;
1466
- while( sqliteCharVal((unsigned char *)zt)!=0 ){
1467
- z=zt;
1468
- sqliteNextChar(zt);
1469
- for(i=1; zt-i>=z; ++i){
1470
- *(rzt--)=*(zt-i);
1471
- }
1472
- }
1473
-
1474
- sqlite3_result_text(context, rz, -1, SQLITE_TRANSIENT);
1475
- sqlite3_free(rz);
1476
- }
1477
-
1478
- /*
1479
- ** An instance of the following structure holds the context of a
1480
- ** stdev() or variance() aggregate computation.
1481
- ** implementaion of http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Algorithm_II
1482
- ** less prone to rounding errors
1483
- */
1484
- typedef struct StdevCtx StdevCtx;
1485
- struct StdevCtx {
1486
- double rM;
1487
- double rS;
1488
- i64 cnt; /* number of elements */
1489
- };
1490
-
1491
- /*
1492
- ** An instance of the following structure holds the context of a
1493
- ** mode() or median() aggregate computation.
1494
- ** Depends on structures defined in map.c (see map & map)
1495
- ** These aggregate functions only work for integers and floats although
1496
- ** they could be made to work for strings. This is usually considered meaningless.
1497
- ** Only usuall order (for median), no use of collation functions (would this even make sense?)
1498
- */
1499
- typedef struct ModeCtx ModeCtx;
1500
- struct ModeCtx {
1501
- i64 riM; /* integer value found so far */
1502
- double rdM; /* double value found so far */
1503
- i64 cnt; /* number of elements so far */
1504
- double pcnt; /* number of elements smaller than a percentile */
1505
- i64 mcnt; /* maximum number of occurrences (for mode) */
1506
- i64 mn; /* number of occurrences (for mode and percentiles) */
1507
- i64 is_double; /* whether the computation is being done for doubles (>0) or integers (=0) */
1508
- map* m; /* map structure used for the computation */
1509
- int done; /* whether the answer has been found */
1510
- };
1511
-
1512
- /*
1513
- ** called for each value received during a calculation of stdev or variance
1514
- */
1515
- static void varianceStep(sqlite3_context *context, int argc, sqlite3_value **argv){
1516
- StdevCtx *p;
1517
-
1518
- double delta;
1519
- double x;
1520
-
1521
- assert( argc==1 );
1522
- p = sqlite3_aggregate_context(context, sizeof(*p));
1523
- /* only consider non-null values */
1524
- if( SQLITE_NULL != sqlite3_value_numeric_type(argv[0]) ){
1525
- p->cnt++;
1526
- x = sqlite3_value_double(argv[0]);
1527
- delta = (x-p->rM);
1528
- p->rM += delta/p->cnt;
1529
- p->rS += delta*(x-p->rM);
1530
- }
1531
- }
1532
-
1533
- /*
1534
- ** called for each value received during a calculation of mode of median
1535
- */
1536
- static void modeStep(sqlite3_context *context, int argc, sqlite3_value **argv){
1537
- ModeCtx *p;
1538
- i64 xi=0;
1539
- double xd=0.0;
1540
- i64 *iptr;
1541
- double *dptr;
1542
- int type;
1543
-
1544
- assert( argc==1 );
1545
- type = sqlite3_value_numeric_type(argv[0]);
1546
-
1547
- if( type == SQLITE_NULL)
1548
- return;
1549
-
1550
- p = sqlite3_aggregate_context(context, sizeof(*p));
1551
-
1552
- if( 0==(p->m) ){
1553
- p->m = calloc(1, sizeof(map));
1554
- if( type==SQLITE_INTEGER ){
1555
- /* map will be used for integers */
1556
- *(p->m) = map_make(int_cmp);
1557
- p->is_double = 0;
1558
- }else{
1559
- p->is_double = 1;
1560
- /* map will be used for doubles */
1561
- *(p->m) = map_make(double_cmp);
1562
- }
1563
- }
1564
-
1565
- ++(p->cnt);
1566
-
1567
- if( 0==p->is_double ){
1568
- xi = sqlite3_value_int64(argv[0]);
1569
- iptr = (i64*)calloc(1,sizeof(i64));
1570
- *iptr = xi;
1571
- map_insert(p->m, iptr);
1572
- }else{
1573
- xd = sqlite3_value_double(argv[0]);
1574
- dptr = (double*)calloc(1,sizeof(double));
1575
- *dptr = xd;
1576
- map_insert(p->m, dptr);
1577
- }
1578
- }
1579
-
1580
- /*
1581
- ** Auxiliary function that iterates all elements in a map and finds the mode
1582
- ** (most frequent value)
1583
- */
1584
- static void modeIterate(void* e, i64 c, void* pp){
1585
- i64 ei;
1586
- double ed;
1587
- ModeCtx *p = (ModeCtx*)pp;
1588
-
1589
- if( 0==p->is_double ){
1590
- ei = *(int*)(e);
1591
-
1592
- if( p->mcnt==c ){
1593
- ++p->mn;
1594
- }else if( p->mcnt<c ){
1595
- p->riM = ei;
1596
- p->mcnt = c;
1597
- p->mn=1;
1598
- }
1599
- }else{
1600
- ed = *(double*)(e);
1601
-
1602
- if( p->mcnt==c ){
1603
- ++p->mn;
1604
- }else if(p->mcnt<c){
1605
- p->rdM = ed;
1606
- p->mcnt = c;
1607
- p->mn=1;
1608
- }
1609
- }
1610
- }
1611
-
1612
- /*
1613
- ** Auxiliary function that iterates all elements in a map and finds the median
1614
- ** (the value such that the number of elements smaller is equal the the number of
1615
- ** elements larger)
1616
- */
1617
- static void medianIterate(void* e, i64 c, void* pp){
1618
- i64 ei;
1619
- double ed;
1620
- double iL;
1621
- double iR;
1622
- int il;
1623
- int ir;
1624
- ModeCtx *p = (ModeCtx*)pp;
1625
-
1626
- if(p->done>0)
1627
- return;
1628
-
1629
- iL = p->pcnt;
1630
- iR = p->cnt - p->pcnt;
1631
- il = p->mcnt + c;
1632
- ir = p->cnt - p->mcnt;
1633
-
1634
- if( il >= iL ){
1635
- if( ir >= iR ){
1636
- ++p->mn;
1637
- if( 0==p->is_double ){
1638
- ei = *(int*)(e);
1639
- p->riM += ei;
1640
- }else{
1641
- ed = *(double*)(e);
1642
- p->rdM += ed;
1643
- }
1644
- }else{
1645
- p->done=1;
1646
- }
1647
- }
1648
- p->mcnt+=c;
1649
- }
1650
-
1651
- /*
1652
- ** Returns the mode value
1653
- */
1654
- static void modeFinalize(sqlite3_context *context){
1655
- ModeCtx *p;
1656
- p = sqlite3_aggregate_context(context, 0);
1657
- if( p && p->m ){
1658
- map_iterate(p->m, modeIterate, p);
1659
- map_destroy(p->m);
1660
- free(p->m);
1661
-
1662
- if( 1==p->mn ){
1663
- if( 0==p->is_double )
1664
- sqlite3_result_int64(context, p->riM);
1665
- else
1666
- sqlite3_result_double(context, p->rdM);
1667
- }
1668
- }
1669
- }
1670
-
1671
- /*
1672
- ** auxiliary function for percentiles
1673
- */
1674
- static void _medianFinalize(sqlite3_context *context){
1675
- ModeCtx *p;
1676
- p = (ModeCtx*) sqlite3_aggregate_context(context, 0);
1677
- if( p && p->m ){
1678
- p->done=0;
1679
- map_iterate(p->m, medianIterate, p);
1680
- map_destroy(p->m);
1681
- free(p->m);
1682
-
1683
- if( 0==p->is_double )
1684
- if( 1==p->mn )
1685
- sqlite3_result_int64(context, p->riM);
1686
- else
1687
- sqlite3_result_double(context, p->riM*1.0/p->mn);
1688
- else
1689
- sqlite3_result_double(context, p->rdM/p->mn);
1690
- }
1691
- }
1692
-
1693
- /*
1694
- ** Returns the median value
1695
- */
1696
- static void medianFinalize(sqlite3_context *context){
1697
- ModeCtx *p;
1698
- p = (ModeCtx*) sqlite3_aggregate_context(context, 0);
1699
- if( p!=0 ){
1700
- p->pcnt = (p->cnt)/2.0;
1701
- _medianFinalize(context);
1702
- }
1703
- }
1704
-
1705
- /*
1706
- ** Returns the lower_quartile value
1707
- */
1708
- static void lower_quartileFinalize(sqlite3_context *context){
1709
- ModeCtx *p;
1710
- p = (ModeCtx*) sqlite3_aggregate_context(context, 0);
1711
- if( p!=0 ){
1712
- p->pcnt = (p->cnt)/4.0;
1713
- _medianFinalize(context);
1714
- }
1715
- }
1716
-
1717
- /*
1718
- ** Returns the upper_quartile value
1719
- */
1720
- static void upper_quartileFinalize(sqlite3_context *context){
1721
- ModeCtx *p;
1722
- p = (ModeCtx*) sqlite3_aggregate_context(context, 0);
1723
- if( p!=0 ){
1724
- p->pcnt = (p->cnt)*3/4.0;
1725
- _medianFinalize(context);
1726
- }
1727
- }
1728
-
1729
- /*
1730
- ** Returns the stdev value
1731
- */
1732
- static void stdevFinalize(sqlite3_context *context){
1733
- StdevCtx *p;
1734
- p = sqlite3_aggregate_context(context, 0);
1735
- if( p && p->cnt>1 ){
1736
- sqlite3_result_double(context, sqrt(p->rS/(p->cnt-1)));
1737
- }else{
1738
- sqlite3_result_double(context, 0.0);
1739
- }
1740
- }
1741
-
1742
- /*
1743
- ** Returns the variance value
1744
- */
1745
- static void varianceFinalize(sqlite3_context *context){
1746
- StdevCtx *p;
1747
- p = sqlite3_aggregate_context(context, 0);
1748
- if( p && p->cnt>1 ){
1749
- sqlite3_result_double(context, p->rS/(p->cnt-1));
1750
- }else{
1751
- sqlite3_result_double(context, 0.0);
1752
- }
1753
- }
1754
-
1755
- #ifdef SQLITE_SOUNDEX
1756
-
1757
- /* relicoder factored code */
1758
- /*
1759
- ** Calculates the soundex value of a string
1760
- */
1761
-
1762
- static void soundex(const u8 *zIn, char *zResult){
1763
- int i, j;
1764
- static const unsigned char iCode[] = {
1765
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1766
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1767
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1768
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1769
- 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
1770
- 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
1771
- 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
1772
- 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
1773
- };
1774
-
1775
- for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
1776
- if( zIn[i] ){
1777
- zResult[0] = toupper(zIn[i]);
1778
- for(j=1; j<4 && zIn[i]; i++){
1779
- int code = iCode[zIn[i]&0x7f];
1780
- if( code>0 ){
1781
- zResult[j++] = code + '0';
1782
- }
1783
- }
1784
- while( j<4 ){
1785
- zResult[j++] = '0';
1786
- }
1787
- zResult[j] = 0;
1788
- }else{
1789
- strcpy(zResult, "?000");
1790
- }
1791
- }
1792
-
1793
- /*
1794
- ** computes the number of different characters between the soundex value fo 2 strings
1795
- */
1796
- static void differenceFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1797
- char zResult1[8];
1798
- char zResult2[8];
1799
- char *zR1 = zResult1;
1800
- char *zR2 = zResult2;
1801
- int rVal = 0;
1802
- int i = 0;
1803
- const u8 *zIn1;
1804
- const u8 *zIn2;
1805
-
1806
- assert( argc==2 );
1807
-
1808
- if( sqlite3_value_type(argv[0])==SQLITE_NULL || sqlite3_value_type(argv[1])==SQLITE_NULL ){
1809
- sqlite3_result_null(context);
1810
- return;
1811
- }
1812
-
1813
- zIn1 = (u8*)sqlite3_value_text(argv[0]);
1814
- zIn2 = (u8*)sqlite3_value_text(argv[1]);
1815
-
1816
- soundex(zIn1, zR1);
1817
- soundex(zIn2, zR2);
1818
-
1819
- for(i=0; i<4; ++i){
1820
- if( sqliteCharVal((unsigned char *)zR1)==sqliteCharVal((unsigned char *)zR2) )
1821
- ++rVal;
1822
- sqliteNextChar(zR1);
1823
- sqliteNextChar(zR2);
1824
- }
1825
- sqlite3_result_int(context, rVal);
1826
- }
1827
- #endif
1828
-
1829
- /*
1830
- ** This function registered all of the above C functions as SQL
1831
- ** functions. This should be the only routine in this file with
1832
- ** external linkage.
1833
- */
1834
- int RegisterExtensionFunctions(sqlite3 *db){
1835
- static const struct FuncDef {
1836
- char *zName;
1837
- signed char nArg;
1838
- u8 argType; /* 0: none. 1: db 2: (-1) */
1839
- u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
1840
- u8 needCollSeq;
1841
- void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
1842
- } aFuncs[] = {
1843
- // hack-sqlite3
1844
- /* math.h */
1845
- { "atn2", 2, 0, SQLITE_UTF8, 0, atn2Func },
1846
- { "cot", 1, 0, SQLITE_UTF8, 0, cotFunc },
1847
- { "coth", 1, 0, SQLITE_UTF8, 0, cothFunc },
1848
- { "difference", 2, 0, SQLITE_UTF8, 0, differenceFunc},
1849
- { "square", 1, 0, SQLITE_UTF8, 0, squareFunc },
1850
-
1851
-
1852
- /* string */
1853
- { "replicate", 2, 0, SQLITE_UTF8, 0, replicateFunc },
1854
- { "charindex", 2, 0, SQLITE_UTF8, 0, charindexFunc },
1855
- { "charindex", 3, 0, SQLITE_UTF8, 0, charindexFunc },
1856
- { "leftstr", 2, 0, SQLITE_UTF8, 0, leftFunc },
1857
- { "rightstr", 2, 0, SQLITE_UTF8, 0, rightFunc },
1858
- #ifndef HAVE_TRIM
1859
- { "ltrim", 1, 0, SQLITE_UTF8, 0, ltrimFunc },
1860
- { "rtrim", 1, 0, SQLITE_UTF8, 0, rtrimFunc },
1861
- { "trim", 1, 0, SQLITE_UTF8, 0, trimFunc },
1862
- { "replace", 3, 0, SQLITE_UTF8, 0, replaceFunc },
1863
- #endif
1864
- { "reverse", 1, 0, SQLITE_UTF8, 0, reverseFunc },
1865
- { "proper", 1, 0, SQLITE_UTF8, 0, properFunc },
1866
- { "padl", 2, 0, SQLITE_UTF8, 0, padlFunc },
1867
- { "padr", 2, 0, SQLITE_UTF8, 0, padrFunc },
1868
- { "padc", 2, 0, SQLITE_UTF8, 0, padcFunc },
1869
- { "strfilter", 2, 0, SQLITE_UTF8, 0, strfilterFunc },
1870
- };
1871
- /* Aggregate functions */
1872
- static const struct FuncDefAgg {
1873
- char *zName;
1874
- signed char nArg;
1875
- u8 argType;
1876
- u8 needCollSeq;
1877
- void (*xStep)(sqlite3_context*,int,sqlite3_value**);
1878
- void (*xFinalize)(sqlite3_context*);
1879
- } aAggs[] = {
1880
- { "stdev", 1, 0, 0, varianceStep, stdevFinalize },
1881
- { "variance", 1, 0, 0, varianceStep, varianceFinalize },
1882
- { "mode", 1, 0, 0, modeStep, modeFinalize },
1883
- { "median", 1, 0, 0, modeStep, medianFinalize },
1884
- { "lower_quartile", 1, 0, 0, modeStep, lower_quartileFinalize },
1885
- { "upper_quartile", 1, 0, 0, modeStep, upper_quartileFinalize },
1886
- };
1887
- int i;
1888
-
1889
- for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
1890
- void *pArg = 0;
1891
- switch( aFuncs[i].argType ){
1892
- case 1: pArg = db; break;
1893
- case 2: pArg = (void *)(-1); break;
1894
- }
1895
- //sqlite3CreateFunc
1896
- /* LMH no error checking */
1897
- sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
1898
- aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
1899
- #if 0
1900
- if( aFuncs[i].needCollSeq ){
1901
- struct FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1902
- strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1903
- if( pFunc && aFuncs[i].needCollSeq ){
1904
- pFunc->needCollSeq = 1;
1905
- }
1906
- }
1907
- #endif
1908
- }
1909
-
1910
- for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
1911
- void *pArg = 0;
1912
- switch( aAggs[i].argType ){
1913
- case 1: pArg = db; break;
1914
- case 2: pArg = (void *)(-1); break;
1915
- }
1916
- //sqlite3CreateFunc
1917
- /* LMH no error checking */
1918
- sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
1919
- pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
1920
- #if 0
1921
- if( aAggs[i].needCollSeq ){
1922
- struct FuncDefAgg *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
1923
- strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
1924
- if( pFunc && aAggs[i].needCollSeq ){
1925
- pFunc->needCollSeq = 1;
1926
- }
1927
- }
1928
- #endif
1929
- }
1930
- return 0;
1931
- }
1932
-
1933
- #ifdef COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE
1934
- int sqlite3_extension_init(
1935
- sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi){
1936
- SQLITE_EXTENSION_INIT2(pApi);
1937
- RegisterExtensionFunctions(db);
1938
- return 0;
1939
- }
1940
- #endif /* COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE */
1941
-
1942
- map map_make(cmp_func cmp){
1943
- map r;
1944
- r.cmp=cmp;
1945
- r.base = 0;
1946
-
1947
- return r;
1948
- }
1949
-
1950
- void* xcalloc(size_t nmemb, size_t size, char* s){
1951
- void* ret = calloc(nmemb, size);
1952
- return ret;
1953
- }
1954
-
1955
- void xfree(void* p){
1956
- free(p);
1957
- }
1958
-
1959
- void node_insert(node** n, cmp_func cmp, void *e){
1960
- int c;
1961
- node* nn;
1962
- if(*n==0){
1963
- nn = (node*)xcalloc(1,sizeof(node), "for node");
1964
- nn->data = e;
1965
- nn->count = 1;
1966
- *n=nn;
1967
- }else{
1968
- c=cmp((*n)->data,e);
1969
- if(0==c){
1970
- ++((*n)->count);
1971
- xfree(e);
1972
- }else if(c>0){
1973
- /* put it right here */
1974
- node_insert(&((*n)->l), cmp, e);
1975
- }else{
1976
- node_insert(&((*n)->r), cmp, e);
1977
- }
1978
- }
1979
- }
1980
-
1981
- void map_insert(map *m, void *e){
1982
- node_insert(&(m->base), m->cmp, e);
1983
- }
1984
-
1985
- void node_iterate(node *n, map_iterator iter, void* p){
1986
- if(n){
1987
- if(n->l)
1988
- node_iterate(n->l, iter, p);
1989
- iter(n->data, n->count, p);
1990
- if(n->r)
1991
- node_iterate(n->r, iter, p);
1992
- }
1993
- }
1994
-
1995
- void map_iterate(map *m, map_iterator iter, void* p){
1996
- node_iterate(m->base, iter, p);
1997
- }
1998
-
1999
- void node_destroy(node *n){
2000
- if(0!=n){
2001
- xfree(n->data);
2002
- if(n->l)
2003
- node_destroy(n->l);
2004
- if(n->r)
2005
- node_destroy(n->r);
2006
-
2007
- xfree(n);
2008
- }
2009
- }
2010
-
2011
- void map_destroy(map *m){
2012
- node_destroy(m->base);
2013
- }
2014
-
2015
- int int_cmp(const void *a, const void *b){
2016
- int64_t aa = *(int64_t *)(a);
2017
- int64_t bb = *(int64_t *)(b);
2018
- /* printf("cmp %d <=> %d\n",aa,bb); */
2019
- if(aa==bb)
2020
- return 0;
2021
- else if(aa<bb)
2022
- return -1;
2023
- else
2024
- return 1;
2025
- }
2026
-
2027
- int double_cmp(const void *a, const void *b){
2028
- double aa = *(double *)(a);
2029
- double bb = *(double *)(b);
2030
- /* printf("cmp %d <=> %d\n",aa,bb); */
2031
- if(aa==bb)
2032
- return 0;
2033
- else if(aa<bb)
2034
- return -1;
2035
- else
2036
- return 1;
2037
- }
2038
-
2039
- void print_elem(void *e, int64_t c, void* p){
2040
- int ee = *(int*)(e);
2041
- printf("%d => %lld\n", ee,c);
2042
- }
2043
-
2044
-
2045
- /*
2046
- file none
2047
- */