extensionfunctions.c 52 KB

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