PageRenderTime 29ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/usr/src/lib/libsqlite/src/func.c

https://bitbucket.org/a3217055/illumos-2
C | 766 lines | 546 code | 46 blank | 174 comment | 117 complexity | 31776b90cbbf1b5a089858d060bb6ffa MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0, BSD-3-Clause, LGPL-2.0, 0BSD, BSD-2-Clause, BSD-3-Clause-No-Nuclear-License-2014, AGPL-1.0, GPL-2.0
  1. #pragma ident "%Z%%M% %I% %E% SMI"
  2. /*
  3. ** 2002 February 23
  4. **
  5. ** The author disclaims copyright to this source code. In place of
  6. ** a legal notice, here is a blessing:
  7. **
  8. ** May you do good and not evil.
  9. ** May you find forgiveness for yourself and forgive others.
  10. ** May you share freely, never taking more than you give.
  11. **
  12. *************************************************************************
  13. ** This file contains the C functions that implement various SQL
  14. ** functions of SQLite.
  15. **
  16. ** There is only one exported symbol in this file - the function
  17. ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
  18. ** All other code has file scope.
  19. **
  20. ** $Id: func.c,v 1.43.2.3 2004/07/18 23:03:11 drh Exp $
  21. */
  22. #include <ctype.h>
  23. #include <math.h>
  24. #include <stdlib.h>
  25. #include <sys/u8_textprep.h>
  26. #include <assert.h>
  27. #include "sqliteInt.h"
  28. #include "os.h"
  29. /*
  30. ** Implementation of the non-aggregate min() and max() functions
  31. */
  32. static void minmaxFunc(sqlite_func *context, int argc, const char **argv){
  33. const char *zBest;
  34. int i;
  35. int (*xCompare)(const char*, const char*);
  36. int mask; /* 0 for min() or 0xffffffff for max() */
  37. if( argc==0 ) return;
  38. mask = (int)sqlite_user_data(context);
  39. zBest = argv[0];
  40. if( zBest==0 ) return;
  41. if( argv[1][0]=='n' ){
  42. xCompare = sqliteCompare;
  43. }else{
  44. xCompare = strcmp;
  45. }
  46. for(i=2; i<argc; i+=2){
  47. if( argv[i]==0 ) return;
  48. if( (xCompare(argv[i], zBest)^mask)<0 ){
  49. zBest = argv[i];
  50. }
  51. }
  52. sqlite_set_result_string(context, zBest, -1);
  53. }
  54. /*
  55. ** Return the type of the argument.
  56. */
  57. static void typeofFunc(sqlite_func *context, int argc, const char **argv){
  58. assert( argc==2 );
  59. sqlite_set_result_string(context, argv[1], -1);
  60. }
  61. /*
  62. ** Implementation of the length() function
  63. */
  64. static void lengthFunc(sqlite_func *context, int argc, const char **argv){
  65. const char *z;
  66. int len;
  67. assert( argc==1 );
  68. z = argv[0];
  69. if( z==0 ) return;
  70. #ifdef SQLITE_UTF8
  71. for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
  72. #else
  73. len = strlen(z);
  74. #endif
  75. sqlite_set_result_int(context, len);
  76. }
  77. /*
  78. ** Implementation of the abs() function
  79. */
  80. static void absFunc(sqlite_func *context, int argc, const char **argv){
  81. const char *z;
  82. assert( argc==1 );
  83. z = argv[0];
  84. if( z==0 ) return;
  85. if( z[0]=='-' && isdigit(z[1]) ) z++;
  86. sqlite_set_result_string(context, z, -1);
  87. }
  88. /*
  89. ** Implementation of the substr() function
  90. */
  91. static void substrFunc(sqlite_func *context, int argc, const char **argv){
  92. const char *z;
  93. #ifdef SQLITE_UTF8
  94. const char *z2;
  95. int i;
  96. #endif
  97. int p1, p2, len;
  98. assert( argc==3 );
  99. z = argv[0];
  100. if( z==0 ) return;
  101. p1 = atoi(argv[1]?argv[1]:0);
  102. p2 = atoi(argv[2]?argv[2]:0);
  103. #ifdef SQLITE_UTF8
  104. for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
  105. #else
  106. len = strlen(z);
  107. #endif
  108. if( p1<0 ){
  109. p1 += len;
  110. if( p1<0 ){
  111. p2 += p1;
  112. p1 = 0;
  113. }
  114. }else if( p1>0 ){
  115. p1--;
  116. }
  117. if( p1+p2>len ){
  118. p2 = len-p1;
  119. }
  120. #ifdef SQLITE_UTF8
  121. for(i=0; i<p1 && z[i]; i++){
  122. if( (z[i]&0xc0)==0x80 ) p1++;
  123. }
  124. while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
  125. for(; i<p1+p2 && z[i]; i++){
  126. if( (z[i]&0xc0)==0x80 ) p2++;
  127. }
  128. while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
  129. #endif
  130. if( p2<0 ) p2 = 0;
  131. sqlite_set_result_string(context, &z[p1], p2);
  132. }
  133. /*
  134. ** Implementation of the round() function
  135. */
  136. static void roundFunc(sqlite_func *context, int argc, const char **argv){
  137. int n;
  138. double r;
  139. char zBuf[100];
  140. assert( argc==1 || argc==2 );
  141. if( argv[0]==0 || (argc==2 && argv[1]==0) ) return;
  142. n = argc==2 ? atoi(argv[1]) : 0;
  143. if( n>30 ) n = 30;
  144. if( n<0 ) n = 0;
  145. r = sqliteAtoF(argv[0], 0);
  146. sprintf(zBuf,"%.*f",n,r);
  147. sqlite_set_result_string(context, zBuf, -1);
  148. }
  149. /*
  150. ** Implementation of the upper() and lower() SQL functions.
  151. */
  152. static void upperFunc(sqlite_func *context, int argc, const char **argv){
  153. unsigned char *z;
  154. int i;
  155. if( argc<1 || argv[0]==0 ) return;
  156. z = (unsigned char*)sqlite_set_result_string(context, argv[0], -1);
  157. if( z==0 ) return;
  158. for(i=0; z[i]; i++){
  159. if( islower(z[i]) ) z[i] = toupper(z[i]);
  160. }
  161. }
  162. static void lowerFunc(sqlite_func *context, int argc, const char **argv){
  163. unsigned char *z;
  164. int i;
  165. if( argc<1 || argv[0]==0 ) return;
  166. z = (unsigned char*)sqlite_set_result_string(context, argv[0], -1);
  167. if( z==0 ) return;
  168. for(i=0; z[i]; i++){
  169. if( isupper(z[i]) ) z[i] = tolower(z[i]);
  170. }
  171. }
  172. /*
  173. * A utility wrapper around u8_textprep_str() that returns an allocated
  174. * string. The result must be freed or passed to
  175. * sqlite_set_result_string().
  176. *
  177. * This is a Solaris-specific function, though it could be made
  178. * portable. u8_textprep_str() and friends are CDDL'ed. This code was
  179. * added to this file without changing the public domain notice, and
  180. * therefore is in the public domain as well.
  181. */
  182. static
  183. char *
  184. utf8textprep(const char *s, int flag)
  185. {
  186. char *res = NULL;
  187. char *outs;
  188. size_t inlen, outlen, inbytesleft, outbytesleft;
  189. int rc, err;
  190. /*
  191. * u8_textprep_str() does not allocate memory. The input and
  192. * output buffers may differ in size (though that would be more
  193. * likely when normalization is done). We have to loop over it...
  194. *
  195. * To improve the chances that we can avoid looping we add 10
  196. * bytes of output buffer room the first go around.
  197. */
  198. inlen = inbytesleft = strlen(s);
  199. outlen = outbytesleft = inlen + 10;
  200. if ((res = sqliteMalloc(outlen)) == NULL)
  201. return (NULL);
  202. outs = res;
  203. while ((rc = u8_textprep_str((char *)s, &inbytesleft, outs,
  204. &outbytesleft, flag, U8_UNICODE_LATEST, &err)) < 0 &&
  205. err == E2BIG) {
  206. if ((res = sqliteRealloc(res, outlen + inbytesleft)) == NULL)
  207. return (NULL);
  208. /* adjust input/output buffer pointers */
  209. s += (inlen - inbytesleft);
  210. outs = res + outlen - outbytesleft;
  211. /* adjust outbytesleft and outlen */
  212. outlen += inbytesleft;
  213. outbytesleft += inbytesleft;
  214. }
  215. if (rc < 0) {
  216. free(res);
  217. res = NULL;
  218. return (NULL);
  219. }
  220. res[outlen - outbytesleft] = '\0';
  221. return (res);
  222. }
  223. /*
  224. * A Unicode-capable case-folding (to lower) function
  225. *
  226. * See block comment for case_fold_utf8().
  227. */
  228. static
  229. void
  230. lower_utf8Func(sqlite_func *context, int argc, const char **argv)
  231. {
  232. char *lower = NULL;
  233. /*
  234. * SQLite functions can take many arguments, but this function
  235. * uses only one, and we call sqlite_create_function() with
  236. * nArg == 1.
  237. */
  238. assert(argc <= 1);
  239. if (argv[0] != NULL)
  240. lower = utf8textprep(argv[0], U8_TEXTPREP_TOLOWER);
  241. out:
  242. (void) sqlite_set_result_string(context, lower, -1);
  243. free(lower);
  244. }
  245. static
  246. void
  247. upper_utf8Func(sqlite_func *context, int argc, const char **argv)
  248. {
  249. char *upper = NULL;
  250. /*
  251. * SQLite functions can take many arguments, but this function
  252. * uses only one, and we call sqlite_create_function() with
  253. * nArg == 1.
  254. */
  255. assert(argc <= 1);
  256. if (argv[0] != NULL)
  257. upper = utf8textprep(argv[0], U8_TEXTPREP_TOUPPER);
  258. out:
  259. (void) sqlite_set_result_string(context, upper, -1);
  260. free(upper);
  261. }
  262. /*
  263. ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
  264. ** All three do the same thing. They return the first non-NULL
  265. ** argument.
  266. */
  267. static void ifnullFunc(sqlite_func *context, int argc, const char **argv){
  268. int i;
  269. for(i=0; i<argc; i++){
  270. if( argv[i] ){
  271. sqlite_set_result_string(context, argv[i], -1);
  272. break;
  273. }
  274. }
  275. }
  276. /*
  277. ** Implementation of random(). Return a random integer.
  278. */
  279. static void randomFunc(sqlite_func *context, int argc, const char **argv){
  280. int r;
  281. sqliteRandomness(sizeof(r), &r);
  282. sqlite_set_result_int(context, r);
  283. }
  284. /*
  285. ** Implementation of the last_insert_rowid() SQL function. The return
  286. ** value is the same as the sqlite_last_insert_rowid() API function.
  287. */
  288. static void last_insert_rowid(sqlite_func *context, int arg, const char **argv){
  289. sqlite *db = sqlite_user_data(context);
  290. sqlite_set_result_int(context, sqlite_last_insert_rowid(db));
  291. }
  292. /*
  293. ** Implementation of the change_count() SQL function. The return
  294. ** value is the same as the sqlite_changes() API function.
  295. */
  296. static void change_count(sqlite_func *context, int arg, const char **argv){
  297. sqlite *db = sqlite_user_data(context);
  298. sqlite_set_result_int(context, sqlite_changes(db));
  299. }
  300. /*
  301. ** Implementation of the last_statement_change_count() SQL function. The
  302. ** return value is the same as the sqlite_last_statement_changes() API function.
  303. */
  304. static void last_statement_change_count(sqlite_func *context, int arg,
  305. const char **argv){
  306. sqlite *db = sqlite_user_data(context);
  307. sqlite_set_result_int(context, sqlite_last_statement_changes(db));
  308. }
  309. /*
  310. ** Implementation of the like() SQL function. This function implements
  311. ** the build-in LIKE operator. The first argument to the function is the
  312. ** string and the second argument is the pattern. So, the SQL statements:
  313. **
  314. ** A LIKE B
  315. **
  316. ** is implemented as like(A,B).
  317. */
  318. static void likeFunc(sqlite_func *context, int arg, const char **argv){
  319. if( argv[0]==0 || argv[1]==0 ) return;
  320. sqlite_set_result_int(context,
  321. sqliteLikeCompare((const unsigned char*)argv[0],
  322. (const unsigned char*)argv[1]));
  323. }
  324. /*
  325. ** Implementation of the glob() SQL function. This function implements
  326. ** the build-in GLOB operator. The first argument to the function is the
  327. ** string and the second argument is the pattern. So, the SQL statements:
  328. **
  329. ** A GLOB B
  330. **
  331. ** is implemented as glob(A,B).
  332. */
  333. static void globFunc(sqlite_func *context, int arg, const char **argv){
  334. if( argv[0]==0 || argv[1]==0 ) return;
  335. sqlite_set_result_int(context,
  336. sqliteGlobCompare((const unsigned char*)argv[0],
  337. (const unsigned char*)argv[1]));
  338. }
  339. /*
  340. ** Implementation of the NULLIF(x,y) function. The result is the first
  341. ** argument if the arguments are different. The result is NULL if the
  342. ** arguments are equal to each other.
  343. */
  344. static void nullifFunc(sqlite_func *context, int argc, const char **argv){
  345. if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){
  346. sqlite_set_result_string(context, argv[0], -1);
  347. }
  348. }
  349. /*
  350. ** Implementation of the VERSION(*) function. The result is the version
  351. ** of the SQLite library that is running.
  352. */
  353. static void versionFunc(sqlite_func *context, int argc, const char **argv){
  354. sqlite_set_result_string(context, sqlite_version, -1);
  355. }
  356. /*
  357. ** EXPERIMENTAL - This is not an official function. The interface may
  358. ** change. This function may disappear. Do not write code that depends
  359. ** on this function.
  360. **
  361. ** Implementation of the QUOTE() function. This function takes a single
  362. ** argument. If the argument is numeric, the return value is the same as
  363. ** the argument. If the argument is NULL, the return value is the string
  364. ** "NULL". Otherwise, the argument is enclosed in single quotes with
  365. ** single-quote escapes.
  366. */
  367. static void quoteFunc(sqlite_func *context, int argc, const char **argv){
  368. if( argc<1 ) return;
  369. if( argv[0]==0 ){
  370. sqlite_set_result_string(context, "NULL", 4);
  371. }else if( sqliteIsNumber(argv[0]) ){
  372. sqlite_set_result_string(context, argv[0], -1);
  373. }else{
  374. int i,j,n;
  375. char *z;
  376. for(i=n=0; argv[0][i]; i++){ if( argv[0][i]=='\'' ) n++; }
  377. z = sqliteMalloc( i+n+3 );
  378. if( z==0 ) return;
  379. z[0] = '\'';
  380. for(i=0, j=1; argv[0][i]; i++){
  381. z[j++] = argv[0][i];
  382. if( argv[0][i]=='\'' ){
  383. z[j++] = '\'';
  384. }
  385. }
  386. z[j++] = '\'';
  387. z[j] = 0;
  388. sqlite_set_result_string(context, z, j);
  389. sqliteFree(z);
  390. }
  391. }
  392. #ifdef SQLITE_SOUNDEX
  393. /*
  394. ** Compute the soundex encoding of a word.
  395. */
  396. static void soundexFunc(sqlite_func *context, int argc, const char **argv){
  397. char zResult[8];
  398. const char *zIn;
  399. int i, j;
  400. static const unsigned char iCode[] = {
  401. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  402. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  403. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  404. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  405. 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
  406. 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
  407. 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
  408. 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
  409. };
  410. assert( argc==1 );
  411. zIn = argv[0];
  412. for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
  413. if( zIn[i] ){
  414. zResult[0] = toupper(zIn[i]);
  415. for(j=1; j<4 && zIn[i]; i++){
  416. int code = iCode[zIn[i]&0x7f];
  417. if( code>0 ){
  418. zResult[j++] = code + '0';
  419. }
  420. }
  421. while( j<4 ){
  422. zResult[j++] = '0';
  423. }
  424. zResult[j] = 0;
  425. sqlite_set_result_string(context, zResult, 4);
  426. }else{
  427. sqlite_set_result_string(context, "?000", 4);
  428. }
  429. }
  430. #endif
  431. #ifdef SQLITE_TEST
  432. /*
  433. ** This function generates a string of random characters. Used for
  434. ** generating test data.
  435. */
  436. static void randStr(sqlite_func *context, int argc, const char **argv){
  437. static const unsigned char zSrc[] =
  438. "abcdefghijklmnopqrstuvwxyz"
  439. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  440. "0123456789"
  441. ".-!,:*^+=_|?/<> ";
  442. int iMin, iMax, n, r, i;
  443. unsigned char zBuf[1000];
  444. if( argc>=1 ){
  445. iMin = atoi(argv[0]);
  446. if( iMin<0 ) iMin = 0;
  447. if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
  448. }else{
  449. iMin = 1;
  450. }
  451. if( argc>=2 ){
  452. iMax = atoi(argv[1]);
  453. if( iMax<iMin ) iMax = iMin;
  454. if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
  455. }else{
  456. iMax = 50;
  457. }
  458. n = iMin;
  459. if( iMax>iMin ){
  460. sqliteRandomness(sizeof(r), &r);
  461. r &= 0x7fffffff;
  462. n += r%(iMax + 1 - iMin);
  463. }
  464. assert( n<sizeof(zBuf) );
  465. sqliteRandomness(n, zBuf);
  466. for(i=0; i<n; i++){
  467. zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
  468. }
  469. zBuf[n] = 0;
  470. sqlite_set_result_string(context, zBuf, n);
  471. }
  472. #endif
  473. /*
  474. ** An instance of the following structure holds the context of a
  475. ** sum() or avg() aggregate computation.
  476. */
  477. typedef struct SumCtx SumCtx;
  478. struct SumCtx {
  479. double sum; /* Sum of terms */
  480. int cnt; /* Number of elements summed */
  481. };
  482. /*
  483. ** Routines used to compute the sum or average.
  484. */
  485. static void sumStep(sqlite_func *context, int argc, const char **argv){
  486. SumCtx *p;
  487. if( argc<1 ) return;
  488. p = sqlite_aggregate_context(context, sizeof(*p));
  489. if( p && argv[0] ){
  490. p->sum += sqliteAtoF(argv[0], 0);
  491. p->cnt++;
  492. }
  493. }
  494. static void sumFinalize(sqlite_func *context){
  495. SumCtx *p;
  496. p = sqlite_aggregate_context(context, sizeof(*p));
  497. sqlite_set_result_double(context, p ? p->sum : 0.0);
  498. }
  499. static void avgFinalize(sqlite_func *context){
  500. SumCtx *p;
  501. p = sqlite_aggregate_context(context, sizeof(*p));
  502. if( p && p->cnt>0 ){
  503. sqlite_set_result_double(context, p->sum/(double)p->cnt);
  504. }
  505. }
  506. /*
  507. ** An instance of the following structure holds the context of a
  508. ** variance or standard deviation computation.
  509. */
  510. typedef struct StdDevCtx StdDevCtx;
  511. struct StdDevCtx {
  512. double sum; /* Sum of terms */
  513. double sum2; /* Sum of the squares of terms */
  514. int cnt; /* Number of terms counted */
  515. };
  516. #if 0 /* Omit because math library is required */
  517. /*
  518. ** Routines used to compute the standard deviation as an aggregate.
  519. */
  520. static void stdDevStep(sqlite_func *context, int argc, const char **argv){
  521. StdDevCtx *p;
  522. double x;
  523. if( argc<1 ) return;
  524. p = sqlite_aggregate_context(context, sizeof(*p));
  525. if( p && argv[0] ){
  526. x = sqliteAtoF(argv[0], 0);
  527. p->sum += x;
  528. p->sum2 += x*x;
  529. p->cnt++;
  530. }
  531. }
  532. static void stdDevFinalize(sqlite_func *context){
  533. double rN = sqlite_aggregate_count(context);
  534. StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
  535. if( p && p->cnt>1 ){
  536. double rCnt = cnt;
  537. sqlite_set_result_double(context,
  538. sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
  539. }
  540. }
  541. #endif
  542. /*
  543. ** The following structure keeps track of state information for the
  544. ** count() aggregate function.
  545. */
  546. typedef struct CountCtx CountCtx;
  547. struct CountCtx {
  548. int n;
  549. };
  550. /*
  551. ** Routines to implement the count() aggregate function.
  552. */
  553. static void countStep(sqlite_func *context, int argc, const char **argv){
  554. CountCtx *p;
  555. p = sqlite_aggregate_context(context, sizeof(*p));
  556. if( (argc==0 || argv[0]) && p ){
  557. p->n++;
  558. }
  559. }
  560. static void countFinalize(sqlite_func *context){
  561. CountCtx *p;
  562. p = sqlite_aggregate_context(context, sizeof(*p));
  563. sqlite_set_result_int(context, p ? p->n : 0);
  564. }
  565. /*
  566. ** This function tracks state information for the min() and max()
  567. ** aggregate functions.
  568. */
  569. typedef struct MinMaxCtx MinMaxCtx;
  570. struct MinMaxCtx {
  571. char *z; /* The best so far */
  572. char zBuf[28]; /* Space that can be used for storage */
  573. };
  574. /*
  575. ** Routines to implement min() and max() aggregate functions.
  576. */
  577. static void minmaxStep(sqlite_func *context, int argc, const char **argv){
  578. MinMaxCtx *p;
  579. int (*xCompare)(const char*, const char*);
  580. int mask; /* 0 for min() or 0xffffffff for max() */
  581. assert( argc==2 );
  582. if( argv[0]==0 ) return; /* Ignore NULL values */
  583. if( argv[1][0]=='n' ){
  584. xCompare = sqliteCompare;
  585. }else{
  586. xCompare = strcmp;
  587. }
  588. mask = (int)sqlite_user_data(context);
  589. assert( mask==0 || mask==-1 );
  590. p = sqlite_aggregate_context(context, sizeof(*p));
  591. if( p==0 || argc<1 ) return;
  592. if( p->z==0 || (xCompare(argv[0],p->z)^mask)<0 ){
  593. int len;
  594. if( p->zBuf[0] ){
  595. sqliteFree(p->z);
  596. }
  597. len = strlen(argv[0]);
  598. if( len < sizeof(p->zBuf)-1 ){
  599. p->z = &p->zBuf[1];
  600. p->zBuf[0] = 0;
  601. }else{
  602. p->z = sqliteMalloc( len+1 );
  603. p->zBuf[0] = 1;
  604. if( p->z==0 ) return;
  605. }
  606. strcpy(p->z, argv[0]);
  607. }
  608. }
  609. static void minMaxFinalize(sqlite_func *context){
  610. MinMaxCtx *p;
  611. p = sqlite_aggregate_context(context, sizeof(*p));
  612. if( p && p->z && p->zBuf[0]<2 ){
  613. sqlite_set_result_string(context, p->z, strlen(p->z));
  614. }
  615. if( p && p->zBuf[0] ){
  616. sqliteFree(p->z);
  617. }
  618. }
  619. /*
  620. ** This function registered all of the above C functions as SQL
  621. ** functions. This should be the only routine in this file with
  622. ** external linkage.
  623. */
  624. void sqliteRegisterBuiltinFunctions(sqlite *db){
  625. static struct {
  626. char *zName;
  627. signed char nArg;
  628. signed char dataType;
  629. u8 argType; /* 0: none. 1: db 2: (-1) */
  630. void (*xFunc)(sqlite_func*,int,const char**);
  631. } aFuncs[] = {
  632. { "min", -1, SQLITE_ARGS, 0, minmaxFunc },
  633. { "min", 0, 0, 0, 0 },
  634. { "max", -1, SQLITE_ARGS, 2, minmaxFunc },
  635. { "max", 0, 0, 2, 0 },
  636. { "typeof", 1, SQLITE_TEXT, 0, typeofFunc },
  637. { "length", 1, SQLITE_NUMERIC, 0, lengthFunc },
  638. { "substr", 3, SQLITE_TEXT, 0, substrFunc },
  639. { "abs", 1, SQLITE_NUMERIC, 0, absFunc },
  640. { "round", 1, SQLITE_NUMERIC, 0, roundFunc },
  641. { "round", 2, SQLITE_NUMERIC, 0, roundFunc },
  642. { "upper", 1, SQLITE_TEXT, 0, upperFunc },
  643. { "lower", 1, SQLITE_TEXT, 0, lowerFunc },
  644. { "lower_utf8", 1, SQLITE_TEXT, 0, lower_utf8Func },
  645. { "upper_utf8", 1, SQLITE_TEXT, 0, upper_utf8Func },
  646. { "coalesce", -1, SQLITE_ARGS, 0, ifnullFunc },
  647. { "coalesce", 0, 0, 0, 0 },
  648. { "coalesce", 1, 0, 0, 0 },
  649. { "ifnull", 2, SQLITE_ARGS, 0, ifnullFunc },
  650. { "random", -1, SQLITE_NUMERIC, 0, randomFunc },
  651. { "like", 2, SQLITE_NUMERIC, 0, likeFunc },
  652. { "glob", 2, SQLITE_NUMERIC, 0, globFunc },
  653. { "nullif", 2, SQLITE_ARGS, 0, nullifFunc },
  654. { "sqlite_version",0,SQLITE_TEXT, 0, versionFunc },
  655. { "quote", 1, SQLITE_ARGS, 0, quoteFunc },
  656. { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
  657. { "change_count", 0, SQLITE_NUMERIC, 1, change_count },
  658. { "last_statement_change_count",
  659. 0, SQLITE_NUMERIC, 1, last_statement_change_count },
  660. #ifdef SQLITE_SOUNDEX
  661. { "soundex", 1, SQLITE_TEXT, 0, soundexFunc},
  662. #endif
  663. #ifdef SQLITE_TEST
  664. { "randstr", 2, SQLITE_TEXT, 0, randStr },
  665. #endif
  666. };
  667. static struct {
  668. char *zName;
  669. signed char nArg;
  670. signed char dataType;
  671. u8 argType;
  672. void (*xStep)(sqlite_func*,int,const char**);
  673. void (*xFinalize)(sqlite_func*);
  674. } aAggs[] = {
  675. { "min", 1, 0, 0, minmaxStep, minMaxFinalize },
  676. { "max", 1, 0, 2, minmaxStep, minMaxFinalize },
  677. { "sum", 1, SQLITE_NUMERIC, 0, sumStep, sumFinalize },
  678. { "avg", 1, SQLITE_NUMERIC, 0, sumStep, avgFinalize },
  679. { "count", 0, SQLITE_NUMERIC, 0, countStep, countFinalize },
  680. { "count", 1, SQLITE_NUMERIC, 0, countStep, countFinalize },
  681. #if 0
  682. { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep, stdDevFinalize },
  683. #endif
  684. };
  685. static const char *azTypeFuncs[] = { "min", "max", "typeof" };
  686. int i;
  687. for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
  688. void *pArg;
  689. switch( aFuncs[i].argType ){
  690. case 0: pArg = 0; break;
  691. case 1: pArg = db; break;
  692. case 2: pArg = (void*)(-1); break;
  693. }
  694. sqlite_create_function(db, aFuncs[i].zName,
  695. aFuncs[i].nArg, aFuncs[i].xFunc, pArg);
  696. if( aFuncs[i].xFunc ){
  697. sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
  698. }
  699. }
  700. for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
  701. void *pArg;
  702. switch( aAggs[i].argType ){
  703. case 0: pArg = 0; break;
  704. case 1: pArg = db; break;
  705. case 2: pArg = (void*)(-1); break;
  706. }
  707. sqlite_create_aggregate(db, aAggs[i].zName,
  708. aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg);
  709. sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType);
  710. }
  711. for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
  712. int n = strlen(azTypeFuncs[i]);
  713. FuncDef *p = sqliteHashFind(&db->aFunc, azTypeFuncs[i], n);
  714. while( p ){
  715. p->includeTypes = 1;
  716. p = p->pNext;
  717. }
  718. }
  719. sqliteRegisterDateTimeFunctions(db);
  720. }