func.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  1. /*
  2. ** 2002 February 23
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** This file contains the C functions that implement various SQL
  13. ** functions of SQLite.
  14. **
  15. ** There is only one exported symbol in this file - the function
  16. ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
  17. ** All other code has file scope.
  18. */
  19. #include "sqliteInt.h"
  20. #include <stdlib.h>
  21. #include <assert.h>
  22. #include "vdbeInt.h"
  23. /*
  24. ** Return the collating function associated with a function.
  25. */
  26. static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
  27. return context->pColl;
  28. }
  29. /*
  30. ** Indicate that the accumulator load should be skipped on this
  31. ** iteration of the aggregate loop.
  32. */
  33. static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
  34. context->skipFlag = 1;
  35. }
  36. /*
  37. ** Implementation of the non-aggregate min() and max() functions
  38. */
  39. static void minmaxFunc(
  40. sqlite3_context *context,
  41. int argc,
  42. sqlite3_value **argv
  43. ){
  44. int i;
  45. int mask; /* 0 for min() or 0xffffffff for max() */
  46. int iBest;
  47. CollSeq *pColl;
  48. assert( argc>1 );
  49. mask = sqlite3_user_data(context)==0 ? 0 : -1;
  50. pColl = sqlite3GetFuncCollSeq(context);
  51. assert( pColl );
  52. assert( mask==-1 || mask==0 );
  53. iBest = 0;
  54. if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  55. for(i=1; i<argc; i++){
  56. if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
  57. if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
  58. testcase( mask==0 );
  59. iBest = i;
  60. }
  61. }
  62. sqlite3_result_value(context, argv[iBest]);
  63. }
  64. /*
  65. ** Return the type of the argument.
  66. */
  67. static void typeofFunc(
  68. sqlite3_context *context,
  69. int NotUsed,
  70. sqlite3_value **argv
  71. ){
  72. const char *z = 0;
  73. UNUSED_PARAMETER(NotUsed);
  74. switch( sqlite3_value_type(argv[0]) ){
  75. case SQLITE_INTEGER: z = "integer"; break;
  76. case SQLITE_TEXT: z = "text"; break;
  77. case SQLITE_FLOAT: z = "real"; break;
  78. case SQLITE_BLOB: z = "blob"; break;
  79. default: z = "null"; break;
  80. }
  81. sqlite3_result_text(context, z, -1, SQLITE_STATIC);
  82. }
  83. /*
  84. ** Implementation of the length() function
  85. */
  86. static void lengthFunc(
  87. sqlite3_context *context,
  88. int argc,
  89. sqlite3_value **argv
  90. ){
  91. int len;
  92. assert( argc==1 );
  93. UNUSED_PARAMETER(argc);
  94. switch( sqlite3_value_type(argv[0]) ){
  95. case SQLITE_BLOB:
  96. case SQLITE_INTEGER:
  97. case SQLITE_FLOAT: {
  98. sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
  99. break;
  100. }
  101. case SQLITE_TEXT: {
  102. const unsigned char *z = sqlite3_value_text(argv[0]);
  103. if( z==0 ) return;
  104. len = 0;
  105. while( *z ){
  106. len++;
  107. SQLITE_SKIP_UTF8(z);
  108. }
  109. sqlite3_result_int(context, len);
  110. break;
  111. }
  112. default: {
  113. sqlite3_result_null(context);
  114. break;
  115. }
  116. }
  117. }
  118. /*
  119. ** Implementation of the abs() function.
  120. **
  121. ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
  122. ** the numeric argument X.
  123. */
  124. static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  125. assert( argc==1 );
  126. UNUSED_PARAMETER(argc);
  127. switch( sqlite3_value_type(argv[0]) ){
  128. case SQLITE_INTEGER: {
  129. i64 iVal = sqlite3_value_int64(argv[0]);
  130. if( iVal<0 ){
  131. if( (iVal<<1)==0 ){
  132. /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then
  133. ** abs(X) throws an integer overflow error since there is no
  134. ** equivalent positive 64-bit two complement value. */
  135. sqlite3_result_error(context, "integer overflow", -1);
  136. return;
  137. }
  138. iVal = -iVal;
  139. }
  140. sqlite3_result_int64(context, iVal);
  141. break;
  142. }
  143. case SQLITE_NULL: {
  144. /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
  145. sqlite3_result_null(context);
  146. break;
  147. }
  148. default: {
  149. /* Because sqlite3_value_double() returns 0.0 if the argument is not
  150. ** something that can be converted into a number, we have:
  151. ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
  152. ** cannot be converted to a numeric value.
  153. */
  154. double rVal = sqlite3_value_double(argv[0]);
  155. if( rVal<0 ) rVal = -rVal;
  156. sqlite3_result_double(context, rVal);
  157. break;
  158. }
  159. }
  160. }
  161. /*
  162. ** Implementation of the instr() function.
  163. **
  164. ** instr(haystack,needle) finds the first occurrence of needle
  165. ** in haystack and returns the number of previous characters plus 1,
  166. ** or 0 if needle does not occur within haystack.
  167. **
  168. ** If both haystack and needle are BLOBs, then the result is one more than
  169. ** the number of bytes in haystack prior to the first occurrence of needle,
  170. ** or 0 if needle never occurs in haystack.
  171. */
  172. static void instrFunc(
  173. sqlite3_context *context,
  174. int argc,
  175. sqlite3_value **argv
  176. ){
  177. const unsigned char *zHaystack;
  178. const unsigned char *zNeedle;
  179. int nHaystack;
  180. int nNeedle;
  181. int typeHaystack, typeNeedle;
  182. int N = 1;
  183. int isText;
  184. UNUSED_PARAMETER(argc);
  185. typeHaystack = sqlite3_value_type(argv[0]);
  186. typeNeedle = sqlite3_value_type(argv[1]);
  187. if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
  188. nHaystack = sqlite3_value_bytes(argv[0]);
  189. nNeedle = sqlite3_value_bytes(argv[1]);
  190. if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
  191. zHaystack = sqlite3_value_blob(argv[0]);
  192. zNeedle = sqlite3_value_blob(argv[1]);
  193. isText = 0;
  194. }else{
  195. zHaystack = sqlite3_value_text(argv[0]);
  196. zNeedle = sqlite3_value_text(argv[1]);
  197. isText = 1;
  198. }
  199. while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
  200. N++;
  201. do{
  202. nHaystack--;
  203. zHaystack++;
  204. }while( isText && (zHaystack[0]&0xc0)==0x80 );
  205. }
  206. if( nNeedle>nHaystack ) N = 0;
  207. sqlite3_result_int(context, N);
  208. }
  209. /*
  210. ** Implementation of the substr() function.
  211. **
  212. ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
  213. ** p1 is 1-indexed. So substr(x,1,1) returns the first character
  214. ** of x. If x is text, then we actually count UTF-8 characters.
  215. ** If x is a blob, then we count bytes.
  216. **
  217. ** If p1 is negative, then we begin abs(p1) from the end of x[].
  218. **
  219. ** If p2 is negative, return the p2 characters preceding p1.
  220. */
  221. static void substrFunc(
  222. sqlite3_context *context,
  223. int argc,
  224. sqlite3_value **argv
  225. ){
  226. const unsigned char *z;
  227. const unsigned char *z2;
  228. int len;
  229. int p0type;
  230. i64 p1, p2;
  231. int negP2 = 0;
  232. assert( argc==3 || argc==2 );
  233. if( sqlite3_value_type(argv[1])==SQLITE_NULL
  234. || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
  235. ){
  236. return;
  237. }
  238. p0type = sqlite3_value_type(argv[0]);
  239. p1 = sqlite3_value_int(argv[1]);
  240. if( p0type==SQLITE_BLOB ){
  241. len = sqlite3_value_bytes(argv[0]);
  242. z = sqlite3_value_blob(argv[0]);
  243. if( z==0 ) return;
  244. assert( len==sqlite3_value_bytes(argv[0]) );
  245. }else{
  246. z = sqlite3_value_text(argv[0]);
  247. if( z==0 ) return;
  248. len = 0;
  249. if( p1<0 ){
  250. for(z2=z; *z2; len++){
  251. SQLITE_SKIP_UTF8(z2);
  252. }
  253. }
  254. }
  255. if( argc==3 ){
  256. p2 = sqlite3_value_int(argv[2]);
  257. if( p2<0 ){
  258. p2 = -p2;
  259. negP2 = 1;
  260. }
  261. }else{
  262. p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
  263. }
  264. if( p1<0 ){
  265. p1 += len;
  266. if( p1<0 ){
  267. p2 += p1;
  268. if( p2<0 ) p2 = 0;
  269. p1 = 0;
  270. }
  271. }else if( p1>0 ){
  272. p1--;
  273. }else if( p2>0 ){
  274. p2--;
  275. }
  276. if( negP2 ){
  277. p1 -= p2;
  278. if( p1<0 ){
  279. p2 += p1;
  280. p1 = 0;
  281. }
  282. }
  283. assert( p1>=0 && p2>=0 );
  284. if( p0type!=SQLITE_BLOB ){
  285. while( *z && p1 ){
  286. SQLITE_SKIP_UTF8(z);
  287. p1--;
  288. }
  289. for(z2=z; *z2 && p2; p2--){
  290. SQLITE_SKIP_UTF8(z2);
  291. }
  292. sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
  293. }else{
  294. if( p1+p2>len ){
  295. p2 = len-p1;
  296. if( p2<0 ) p2 = 0;
  297. }
  298. sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
  299. }
  300. }
  301. /*
  302. ** Implementation of the round() function
  303. */
  304. #ifndef SQLITE_OMIT_FLOATING_POINT
  305. static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  306. int n = 0;
  307. double r;
  308. char *zBuf;
  309. assert( argc==1 || argc==2 );
  310. if( argc==2 ){
  311. if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
  312. n = sqlite3_value_int(argv[1]);
  313. if( n>30 ) n = 30;
  314. if( n<0 ) n = 0;
  315. }
  316. if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  317. r = sqlite3_value_double(argv[0]);
  318. /* If Y==0 and X will fit in a 64-bit int,
  319. ** handle the rounding directly,
  320. ** otherwise use printf.
  321. */
  322. if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
  323. r = (double)((sqlite_int64)(r+0.5));
  324. }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
  325. r = -(double)((sqlite_int64)((-r)+0.5));
  326. }else{
  327. zBuf = sqlite3_mprintf("%.*f",n,r);
  328. if( zBuf==0 ){
  329. sqlite3_result_error_nomem(context);
  330. return;
  331. }
  332. sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
  333. sqlite3_free(zBuf);
  334. }
  335. sqlite3_result_double(context, r);
  336. }
  337. #endif
  338. /*
  339. ** Allocate nByte bytes of space using sqlite3_malloc(). If the
  340. ** allocation fails, call sqlite3_result_error_nomem() to notify
  341. ** the database handle that malloc() has failed and return NULL.
  342. ** If nByte is larger than the maximum string or blob length, then
  343. ** raise an SQLITE_TOOBIG exception and return NULL.
  344. */
  345. static void *contextMalloc(sqlite3_context *context, i64 nByte){
  346. char *z;
  347. sqlite3 *db = sqlite3_context_db_handle(context);
  348. assert( nByte>0 );
  349. testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
  350. testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
  351. if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  352. sqlite3_result_error_toobig(context);
  353. z = 0;
  354. }else{
  355. z = sqlite3Malloc((int)nByte);
  356. if( !z ){
  357. sqlite3_result_error_nomem(context);
  358. }
  359. }
  360. return z;
  361. }
  362. /*
  363. ** Implementation of the upper() and lower() SQL functions.
  364. */
  365. static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  366. char *z1;
  367. const char *z2;
  368. int i, n;
  369. UNUSED_PARAMETER(argc);
  370. z2 = (char*)sqlite3_value_text(argv[0]);
  371. n = sqlite3_value_bytes(argv[0]);
  372. /* Verify that the call to _bytes() does not invalidate the _text() pointer */
  373. assert( z2==(char*)sqlite3_value_text(argv[0]) );
  374. if( z2 ){
  375. z1 = contextMalloc(context, ((i64)n)+1);
  376. if( z1 ){
  377. for(i=0; i<n; i++){
  378. z1[i] = (char)sqlite3Toupper(z2[i]);
  379. }
  380. sqlite3_result_text(context, z1, n, sqlite3_free);
  381. }
  382. }
  383. }
  384. static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  385. char *z1;
  386. const char *z2;
  387. int i, n;
  388. UNUSED_PARAMETER(argc);
  389. z2 = (char*)sqlite3_value_text(argv[0]);
  390. n = sqlite3_value_bytes(argv[0]);
  391. /* Verify that the call to _bytes() does not invalidate the _text() pointer */
  392. assert( z2==(char*)sqlite3_value_text(argv[0]) );
  393. if( z2 ){
  394. z1 = contextMalloc(context, ((i64)n)+1);
  395. if( z1 ){
  396. for(i=0; i<n; i++){
  397. z1[i] = sqlite3Tolower(z2[i]);
  398. }
  399. sqlite3_result_text(context, z1, n, sqlite3_free);
  400. }
  401. }
  402. }
  403. /*
  404. ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
  405. ** as VDBE code so that unused argument values do not have to be computed.
  406. ** However, we still need some kind of function implementation for this
  407. ** routines in the function table. The noopFunc macro provides this.
  408. ** noopFunc will never be called so it doesn't matter what the implementation
  409. ** is. We might as well use the "version()" function as a substitute.
  410. */
  411. #define noopFunc versionFunc /* Substitute function - never called */
  412. /*
  413. ** Implementation of random(). Return a random integer.
  414. */
  415. static void randomFunc(
  416. sqlite3_context *context,
  417. int NotUsed,
  418. sqlite3_value **NotUsed2
  419. ){
  420. sqlite_int64 r;
  421. UNUSED_PARAMETER2(NotUsed, NotUsed2);
  422. sqlite3_randomness(sizeof(r), &r);
  423. if( r<0 ){
  424. /* We need to prevent a random number of 0x8000000000000000
  425. ** (or -9223372036854775808) since when you do abs() of that
  426. ** number of you get the same value back again. To do this
  427. ** in a way that is testable, mask the sign bit off of negative
  428. ** values, resulting in a positive value. Then take the
  429. ** 2s complement of that positive value. The end result can
  430. ** therefore be no less than -9223372036854775807.
  431. */
  432. r = -(r & LARGEST_INT64);
  433. }
  434. sqlite3_result_int64(context, r);
  435. }
  436. /*
  437. ** Implementation of randomblob(N). Return a random blob
  438. ** that is N bytes long.
  439. */
  440. static void randomBlob(
  441. sqlite3_context *context,
  442. int argc,
  443. sqlite3_value **argv
  444. ){
  445. int n;
  446. unsigned char *p;
  447. assert( argc==1 );
  448. UNUSED_PARAMETER(argc);
  449. n = sqlite3_value_int(argv[0]);
  450. if( n<1 ){
  451. n = 1;
  452. }
  453. p = contextMalloc(context, n);
  454. if( p ){
  455. sqlite3_randomness(n, p);
  456. sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
  457. }
  458. }
  459. /*
  460. ** Implementation of the last_insert_rowid() SQL function. The return
  461. ** value is the same as the sqlite3_last_insert_rowid() API function.
  462. */
  463. static void last_insert_rowid(
  464. sqlite3_context *context,
  465. int NotUsed,
  466. sqlite3_value **NotUsed2
  467. ){
  468. sqlite3 *db = sqlite3_context_db_handle(context);
  469. UNUSED_PARAMETER2(NotUsed, NotUsed2);
  470. /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
  471. ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
  472. ** function. */
  473. sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
  474. }
  475. /*
  476. ** Implementation of the changes() SQL function.
  477. **
  478. ** IMP: R-62073-11209 The changes() SQL function is a wrapper
  479. ** around the sqlite3_changes() C/C++ function and hence follows the same
  480. ** rules for counting changes.
  481. */
  482. static void changes(
  483. sqlite3_context *context,
  484. int NotUsed,
  485. sqlite3_value **NotUsed2
  486. ){
  487. sqlite3 *db = sqlite3_context_db_handle(context);
  488. UNUSED_PARAMETER2(NotUsed, NotUsed2);
  489. sqlite3_result_int(context, sqlite3_changes(db));
  490. }
  491. /*
  492. ** Implementation of the total_changes() SQL function. The return value is
  493. ** the same as the sqlite3_total_changes() API function.
  494. */
  495. static void total_changes(
  496. sqlite3_context *context,
  497. int NotUsed,
  498. sqlite3_value **NotUsed2
  499. ){
  500. sqlite3 *db = sqlite3_context_db_handle(context);
  501. UNUSED_PARAMETER2(NotUsed, NotUsed2);
  502. /* IMP: R-52756-41993 This function is a wrapper around the
  503. ** sqlite3_total_changes() C/C++ interface. */
  504. sqlite3_result_int(context, sqlite3_total_changes(db));
  505. }
  506. /*
  507. ** A structure defining how to do GLOB-style comparisons.
  508. */
  509. struct compareInfo {
  510. u8 matchAll;
  511. u8 matchOne;
  512. u8 matchSet;
  513. u8 noCase;
  514. };
  515. /*
  516. ** For LIKE and GLOB matching on EBCDIC machines, assume that every
  517. ** character is exactly one byte in size. Also, all characters are
  518. ** able to participate in upper-case-to-lower-case mappings in EBCDIC
  519. ** whereas only characters less than 0x80 do in ASCII.
  520. */
  521. #if defined(SQLITE_EBCDIC)
  522. # define sqlite3Utf8Read(A) (*((*A)++))
  523. # define GlobUpperToLower(A) A = sqlite3UpperToLower[A]
  524. #else
  525. # define GlobUpperToLower(A) if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
  526. #endif
  527. static const struct compareInfo globInfo = { '*', '?', '[', 0 };
  528. /* The correct SQL-92 behavior is for the LIKE operator to ignore
  529. ** case. Thus 'a' LIKE 'A' would be true. */
  530. static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
  531. /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
  532. ** is case sensitive causing 'a' LIKE 'A' to be false */
  533. static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
  534. /*
  535. ** Compare two UTF-8 strings for equality where the first string can
  536. ** potentially be a "glob" expression. Return true (1) if they
  537. ** are the same and false (0) if they are different.
  538. **
  539. ** Globbing rules:
  540. **
  541. ** '*' Matches any sequence of zero or more characters.
  542. **
  543. ** '?' Matches exactly one character.
  544. **
  545. ** [...] Matches one character from the enclosed list of
  546. ** characters.
  547. **
  548. ** [^...] Matches one character not in the enclosed list.
  549. **
  550. ** With the [...] and [^...] matching, a ']' character can be included
  551. ** in the list by making it the first character after '[' or '^'. A
  552. ** range of characters can be specified using '-'. Example:
  553. ** "[a-z]" matches any single lower-case letter. To match a '-', make
  554. ** it the last character in the list.
  555. **
  556. ** This routine is usually quick, but can be N**2 in the worst case.
  557. **
  558. ** Hints: to match '*' or '?', put them in "[]". Like this:
  559. **
  560. ** abc[*]xyz Matches "abc*xyz" only
  561. */
  562. static int patternCompare(
  563. const u8 *zPattern, /* The glob pattern */
  564. const u8 *zString, /* The string to compare against the glob */
  565. const struct compareInfo *pInfo, /* Information about how to do the compare */
  566. u32 esc /* The escape character */
  567. ){
  568. u32 c, c2;
  569. int invert;
  570. int seen;
  571. u8 matchOne = pInfo->matchOne;
  572. u8 matchAll = pInfo->matchAll;
  573. u8 matchSet = pInfo->matchSet;
  574. u8 noCase = pInfo->noCase;
  575. int prevEscape = 0; /* True if the previous character was 'escape' */
  576. while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
  577. if( c==matchAll && !prevEscape ){
  578. while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
  579. || c == matchOne ){
  580. if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
  581. return 0;
  582. }
  583. }
  584. if( c==0 ){
  585. return 1;
  586. }else if( c==esc ){
  587. c = sqlite3Utf8Read(&zPattern);
  588. if( c==0 ){
  589. return 0;
  590. }
  591. }else if( c==matchSet ){
  592. assert( esc==0 ); /* This is GLOB, not LIKE */
  593. assert( matchSet<0x80 ); /* '[' is a single-byte character */
  594. while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
  595. SQLITE_SKIP_UTF8(zString);
  596. }
  597. return *zString!=0;
  598. }
  599. while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
  600. if( noCase ){
  601. GlobUpperToLower(c2);
  602. GlobUpperToLower(c);
  603. while( c2 != 0 && c2 != c ){
  604. c2 = sqlite3Utf8Read(&zString);
  605. GlobUpperToLower(c2);
  606. }
  607. }else{
  608. while( c2 != 0 && c2 != c ){
  609. c2 = sqlite3Utf8Read(&zString);
  610. }
  611. }
  612. if( c2==0 ) return 0;
  613. if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
  614. }
  615. return 0;
  616. }else if( c==matchOne && !prevEscape ){
  617. if( sqlite3Utf8Read(&zString)==0 ){
  618. return 0;
  619. }
  620. }else if( c==matchSet ){
  621. u32 prior_c = 0;
  622. assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
  623. seen = 0;
  624. invert = 0;
  625. c = sqlite3Utf8Read(&zString);
  626. if( c==0 ) return 0;
  627. c2 = sqlite3Utf8Read(&zPattern);
  628. if( c2=='^' ){
  629. invert = 1;
  630. c2 = sqlite3Utf8Read(&zPattern);
  631. }
  632. if( c2==']' ){
  633. if( c==']' ) seen = 1;
  634. c2 = sqlite3Utf8Read(&zPattern);
  635. }
  636. while( c2 && c2!=']' ){
  637. if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
  638. c2 = sqlite3Utf8Read(&zPattern);
  639. if( c>=prior_c && c<=c2 ) seen = 1;
  640. prior_c = 0;
  641. }else{
  642. if( c==c2 ){
  643. seen = 1;
  644. }
  645. prior_c = c2;
  646. }
  647. c2 = sqlite3Utf8Read(&zPattern);
  648. }
  649. if( c2==0 || (seen ^ invert)==0 ){
  650. return 0;
  651. }
  652. }else if( esc==c && !prevEscape ){
  653. prevEscape = 1;
  654. }else{
  655. c2 = sqlite3Utf8Read(&zString);
  656. if( noCase ){
  657. GlobUpperToLower(c);
  658. GlobUpperToLower(c2);
  659. }
  660. if( c!=c2 ){
  661. return 0;
  662. }
  663. prevEscape = 0;
  664. }
  665. }
  666. return *zString==0;
  667. }
  668. /*
  669. ** The sqlite3_strglob() interface.
  670. */
  671. int sqlite3_strglob(const char *zGlobPattern, const char *zString){
  672. return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0;
  673. }
  674. /*
  675. ** Count the number of times that the LIKE operator (or GLOB which is
  676. ** just a variation of LIKE) gets called. This is used for testing
  677. ** only.
  678. */
  679. #ifdef SQLITE_TEST
  680. int sqlite3_like_count = 0;
  681. #endif
  682. /*
  683. ** Implementation of the like() SQL function. This function implements
  684. ** the build-in LIKE operator. The first argument to the function is the
  685. ** pattern and the second argument is the string. So, the SQL statements:
  686. **
  687. ** A LIKE B
  688. **
  689. ** is implemented as like(B,A).
  690. **
  691. ** This same function (with a different compareInfo structure) computes
  692. ** the GLOB operator.
  693. */
  694. static void likeFunc(
  695. sqlite3_context *context,
  696. int argc,
  697. sqlite3_value **argv
  698. ){
  699. const unsigned char *zA, *zB;
  700. u32 escape = 0;
  701. int nPat;
  702. sqlite3 *db = sqlite3_context_db_handle(context);
  703. zB = sqlite3_value_text(argv[0]);
  704. zA = sqlite3_value_text(argv[1]);
  705. /* Limit the length of the LIKE or GLOB pattern to avoid problems
  706. ** of deep recursion and N*N behavior in patternCompare().
  707. */
  708. nPat = sqlite3_value_bytes(argv[0]);
  709. testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
  710. testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
  711. if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
  712. sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
  713. return;
  714. }
  715. assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */
  716. if( argc==3 ){
  717. /* The escape character string must consist of a single UTF-8 character.
  718. ** Otherwise, return an error.
  719. */
  720. const unsigned char *zEsc = sqlite3_value_text(argv[2]);
  721. if( zEsc==0 ) return;
  722. if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
  723. sqlite3_result_error(context,
  724. "ESCAPE expression must be a single character", -1);
  725. return;
  726. }
  727. escape = sqlite3Utf8Read(&zEsc);
  728. }
  729. if( zA && zB ){
  730. struct compareInfo *pInfo = sqlite3_user_data(context);
  731. #ifdef SQLITE_TEST
  732. sqlite3_like_count++;
  733. #endif
  734. sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
  735. }
  736. }
  737. /*
  738. ** Implementation of the NULLIF(x,y) function. The result is the first
  739. ** argument if the arguments are different. The result is NULL if the
  740. ** arguments are equal to each other.
  741. */
  742. static void nullifFunc(
  743. sqlite3_context *context,
  744. int NotUsed,
  745. sqlite3_value **argv
  746. ){
  747. CollSeq *pColl = sqlite3GetFuncCollSeq(context);
  748. UNUSED_PARAMETER(NotUsed);
  749. if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
  750. sqlite3_result_value(context, argv[0]);
  751. }
  752. }
  753. /*
  754. ** Implementation of the sqlite_version() function. The result is the version
  755. ** of the SQLite library that is running.
  756. */
  757. static void versionFunc(
  758. sqlite3_context *context,
  759. int NotUsed,
  760. sqlite3_value **NotUsed2
  761. ){
  762. UNUSED_PARAMETER2(NotUsed, NotUsed2);
  763. /* IMP: R-48699-48617 This function is an SQL wrapper around the
  764. ** sqlite3_libversion() C-interface. */
  765. sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
  766. }
  767. /*
  768. ** Implementation of the sqlite_source_id() function. The result is a string
  769. ** that identifies the particular version of the source code used to build
  770. ** SQLite.
  771. */
  772. static void sourceidFunc(
  773. sqlite3_context *context,
  774. int NotUsed,
  775. sqlite3_value **NotUsed2
  776. ){
  777. UNUSED_PARAMETER2(NotUsed, NotUsed2);
  778. /* IMP: R-24470-31136 This function is an SQL wrapper around the
  779. ** sqlite3_sourceid() C interface. */
  780. sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
  781. }
  782. /*
  783. ** Implementation of the sqlite_log() function. This is a wrapper around
  784. ** sqlite3_log(). The return value is NULL. The function exists purely for
  785. ** its side-effects.
  786. */
  787. static void errlogFunc(
  788. sqlite3_context *context,
  789. int argc,
  790. sqlite3_value **argv
  791. ){
  792. UNUSED_PARAMETER(argc);
  793. UNUSED_PARAMETER(context);
  794. sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
  795. }
  796. /*
  797. ** Implementation of the sqlite_compileoption_used() function.
  798. ** The result is an integer that identifies if the compiler option
  799. ** was used to build SQLite.
  800. */
  801. #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  802. static void compileoptionusedFunc(
  803. sqlite3_context *context,
  804. int argc,
  805. sqlite3_value **argv
  806. ){
  807. const char *zOptName;
  808. assert( argc==1 );
  809. UNUSED_PARAMETER(argc);
  810. /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
  811. ** function is a wrapper around the sqlite3_compileoption_used() C/C++
  812. ** function.
  813. */
  814. if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
  815. sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
  816. }
  817. }
  818. #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
  819. /*
  820. ** Implementation of the sqlite_compileoption_get() function.
  821. ** The result is a string that identifies the compiler options
  822. ** used to build SQLite.
  823. */
  824. #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  825. static void compileoptiongetFunc(
  826. sqlite3_context *context,
  827. int argc,
  828. sqlite3_value **argv
  829. ){
  830. int n;
  831. assert( argc==1 );
  832. UNUSED_PARAMETER(argc);
  833. /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
  834. ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
  835. */
  836. n = sqlite3_value_int(argv[0]);
  837. sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
  838. }
  839. #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
  840. /* Array for converting from half-bytes (nybbles) into ASCII hex
  841. ** digits. */
  842. static const char hexdigits[] = {
  843. '0', '1', '2', '3', '4', '5', '6', '7',
  844. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  845. };
  846. /*
  847. ** Implementation of the QUOTE() function. This function takes a single
  848. ** argument. If the argument is numeric, the return value is the same as
  849. ** the argument. If the argument is NULL, the return value is the string
  850. ** "NULL". Otherwise, the argument is enclosed in single quotes with
  851. ** single-quote escapes.
  852. */
  853. static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  854. assert( argc==1 );
  855. UNUSED_PARAMETER(argc);
  856. switch( sqlite3_value_type(argv[0]) ){
  857. case SQLITE_FLOAT: {
  858. double r1, r2;
  859. char zBuf[50];
  860. r1 = sqlite3_value_double(argv[0]);
  861. sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
  862. sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
  863. if( r1!=r2 ){
  864. sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
  865. }
  866. sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
  867. break;
  868. }
  869. case SQLITE_INTEGER: {
  870. sqlite3_result_value(context, argv[0]);
  871. break;
  872. }
  873. case SQLITE_BLOB: {
  874. char *zText = 0;
  875. char const *zBlob = sqlite3_value_blob(argv[0]);
  876. int nBlob = sqlite3_value_bytes(argv[0]);
  877. assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
  878. zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
  879. if( zText ){
  880. int i;
  881. for(i=0; i<nBlob; i++){
  882. zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
  883. zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
  884. }
  885. zText[(nBlob*2)+2] = '\'';
  886. zText[(nBlob*2)+3] = '\0';
  887. zText[0] = 'X';
  888. zText[1] = '\'';
  889. sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
  890. sqlite3_free(zText);
  891. }
  892. break;
  893. }
  894. case SQLITE_TEXT: {
  895. int i,j;
  896. u64 n;
  897. const unsigned char *zArg = sqlite3_value_text(argv[0]);
  898. char *z;
  899. if( zArg==0 ) return;
  900. for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
  901. z = contextMalloc(context, ((i64)i)+((i64)n)+3);
  902. if( z ){
  903. z[0] = '\'';
  904. for(i=0, j=1; zArg[i]; i++){
  905. z[j++] = zArg[i];
  906. if( zArg[i]=='\'' ){
  907. z[j++] = '\'';
  908. }
  909. }
  910. z[j++] = '\'';
  911. z[j] = 0;
  912. sqlite3_result_text(context, z, j, sqlite3_free);
  913. }
  914. break;
  915. }
  916. default: {
  917. assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
  918. sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
  919. break;
  920. }
  921. }
  922. }
  923. /*
  924. ** The unicode() function. Return the integer unicode code-point value
  925. ** for the first character of the input string.
  926. */
  927. static void unicodeFunc(
  928. sqlite3_context *context,
  929. int argc,
  930. sqlite3_value **argv
  931. ){
  932. const unsigned char *z = sqlite3_value_text(argv[0]);
  933. (void)argc;
  934. if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
  935. }
  936. /*
  937. ** The char() function takes zero or more arguments, each of which is
  938. ** an integer. It constructs a string where each character of the string
  939. ** is the unicode character for the corresponding integer argument.
  940. */
  941. static void charFunc(
  942. sqlite3_context *context,
  943. int argc,
  944. sqlite3_value **argv
  945. ){
  946. unsigned char *z, *zOut;
  947. int i;
  948. zOut = z = sqlite3_malloc( argc*4 );
  949. if( z==0 ){
  950. sqlite3_result_error_nomem(context);
  951. return;
  952. }
  953. for(i=0; i<argc; i++){
  954. sqlite3_int64 x;
  955. unsigned c;
  956. x = sqlite3_value_int64(argv[i]);
  957. if( x<0 || x>0x10ffff ) x = 0xfffd;
  958. c = (unsigned)(x & 0x1fffff);
  959. if( c<0x00080 ){
  960. *zOut++ = (u8)(c&0xFF);
  961. }else if( c<0x00800 ){
  962. *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
  963. *zOut++ = 0x80 + (u8)(c & 0x3F);
  964. }else if( c<0x10000 ){
  965. *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
  966. *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
  967. *zOut++ = 0x80 + (u8)(c & 0x3F);
  968. }else{
  969. *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
  970. *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
  971. *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
  972. *zOut++ = 0x80 + (u8)(c & 0x3F);
  973. } \
  974. }
  975. sqlite3_result_text(context, (char*)z, (int)(zOut-z), sqlite3_free);
  976. }
  977. /*
  978. ** The hex() function. Interpret the argument as a blob. Return
  979. ** a hexadecimal rendering as text.
  980. */
  981. static void hexFunc(
  982. sqlite3_context *context,
  983. int argc,
  984. sqlite3_value **argv
  985. ){
  986. int i, n;
  987. const unsigned char *pBlob;
  988. char *zHex, *z;
  989. assert( argc==1 );
  990. UNUSED_PARAMETER(argc);
  991. pBlob = sqlite3_value_blob(argv[0]);
  992. n = sqlite3_value_bytes(argv[0]);
  993. assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
  994. z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
  995. if( zHex ){
  996. for(i=0; i<n; i++, pBlob++){
  997. unsigned char c = *pBlob;
  998. *(z++) = hexdigits[(c>>4)&0xf];
  999. *(z++) = hexdigits[c&0xf];
  1000. }
  1001. *z = 0;
  1002. sqlite3_result_text(context, zHex, n*2, sqlite3_free);
  1003. }
  1004. }
  1005. /*
  1006. ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
  1007. */
  1008. static void zeroblobFunc(
  1009. sqlite3_context *context,
  1010. int argc,
  1011. sqlite3_value **argv
  1012. ){
  1013. i64 n;
  1014. sqlite3 *db = sqlite3_context_db_handle(context);
  1015. assert( argc==1 );
  1016. UNUSED_PARAMETER(argc);
  1017. n = sqlite3_value_int64(argv[0]);
  1018. testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
  1019. testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
  1020. if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1021. sqlite3_result_error_toobig(context);
  1022. }else{
  1023. sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
  1024. }
  1025. }
  1026. /*
  1027. ** The replace() function. Three arguments are all strings: call
  1028. ** them A, B, and C. The result is also a string which is derived
  1029. ** from A by replacing every occurrence of B with C. The match
  1030. ** must be exact. Collating sequences are not used.
  1031. */
  1032. static void replaceFunc(
  1033. sqlite3_context *context,
  1034. int argc,
  1035. sqlite3_value **argv
  1036. ){
  1037. const unsigned char *zStr; /* The input string A */
  1038. const unsigned char *zPattern; /* The pattern string B */
  1039. const unsigned char *zRep; /* The replacement string C */
  1040. unsigned char *zOut; /* The output */
  1041. int nStr; /* Size of zStr */
  1042. int nPattern; /* Size of zPattern */
  1043. int nRep; /* Size of zRep */
  1044. i64 nOut; /* Maximum size of zOut */
  1045. int loopLimit; /* Last zStr[] that might match zPattern[] */
  1046. int i, j; /* Loop counters */
  1047. assert( argc==3 );
  1048. UNUSED_PARAMETER(argc);
  1049. zStr = sqlite3_value_text(argv[0]);
  1050. if( zStr==0 ) return;
  1051. nStr = sqlite3_value_bytes(argv[0]);
  1052. assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
  1053. zPattern = sqlite3_value_text(argv[1]);
  1054. if( zPattern==0 ){
  1055. assert( sqlite3_value_type(argv[1])==SQLITE_NULL
  1056. || sqlite3_context_db_handle(context)->mallocFailed );
  1057. return;
  1058. }
  1059. if( zPattern[0]==0 ){
  1060. assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
  1061. sqlite3_result_value(context, argv[0]);
  1062. return;
  1063. }
  1064. nPattern = sqlite3_value_bytes(argv[1]);
  1065. assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
  1066. zRep = sqlite3_value_text(argv[2]);
  1067. if( zRep==0 ) return;
  1068. nRep = sqlite3_value_bytes(argv[2]);
  1069. assert( zRep==sqlite3_value_text(argv[2]) );
  1070. nOut = nStr + 1;
  1071. assert( nOut<SQLITE_MAX_LENGTH );
  1072. zOut = contextMalloc(context, (i64)nOut);
  1073. if( zOut==0 ){
  1074. return;
  1075. }
  1076. loopLimit = nStr - nPattern;
  1077. for(i=j=0; i<=loopLimit; i++){
  1078. if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
  1079. zOut[j++] = zStr[i];
  1080. }else{
  1081. u8 *zOld;
  1082. sqlite3 *db = sqlite3_context_db_handle(context);
  1083. nOut += nRep - nPattern;
  1084. testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
  1085. testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
  1086. if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1087. sqlite3_result_error_toobig(context);
  1088. sqlite3_free(zOut);
  1089. return;
  1090. }
  1091. zOld = zOut;
  1092. zOut = sqlite3_realloc(zOut, (int)nOut);
  1093. if( zOut==0 ){
  1094. sqlite3_result_error_nomem(context);
  1095. sqlite3_free(zOld);
  1096. return;
  1097. }
  1098. memcpy(&zOut[j], zRep, nRep);
  1099. j += nRep;
  1100. i += nPattern-1;
  1101. }
  1102. }
  1103. assert( j+nStr-i+1==nOut );
  1104. memcpy(&zOut[j], &zStr[i], nStr-i);
  1105. j += nStr - i;
  1106. assert( j<=nOut );
  1107. zOut[j] = 0;
  1108. sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
  1109. }
  1110. /*
  1111. ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
  1112. ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
  1113. */
  1114. static void trimFunc(
  1115. sqlite3_context *context,
  1116. int argc,
  1117. sqlite3_value **argv
  1118. ){
  1119. const unsigned char *zIn; /* Input string */
  1120. const unsigned char *zCharSet; /* Set of characters to trim */
  1121. int nIn; /* Number of bytes in input */
  1122. int flags; /* 1: trimleft 2: trimright 3: trim */
  1123. int i; /* Loop counter */
  1124. unsigned char *aLen = 0; /* Length of each character in zCharSet */
  1125. unsigned char **azChar = 0; /* Individual characters in zCharSet */
  1126. int nChar; /* Number of characters in zCharSet */
  1127. if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
  1128. return;
  1129. }
  1130. zIn = sqlite3_value_text(argv[0]);
  1131. if( zIn==0 ) return;
  1132. nIn = sqlite3_value_bytes(argv[0]);
  1133. assert( zIn==sqlite3_value_text(argv[0]) );
  1134. if( argc==1 ){
  1135. static const unsigned char lenOne[] = { 1 };
  1136. static unsigned char * const azOne[] = { (u8*)" " };
  1137. nChar = 1;
  1138. aLen = (u8*)lenOne;
  1139. azChar = (unsigned char **)azOne;
  1140. zCharSet = 0;
  1141. }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
  1142. return;
  1143. }else{
  1144. const unsigned char *z;
  1145. for(z=zCharSet, nChar=0; *z; nChar++){
  1146. SQLITE_SKIP_UTF8(z);
  1147. }
  1148. if( nChar>0 ){
  1149. azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
  1150. if( azChar==0 ){
  1151. return;
  1152. }
  1153. aLen = (unsigned char*)&azChar[nChar];
  1154. for(z=zCharSet, nChar=0; *z; nChar++){
  1155. azChar[nChar] = (unsigned char *)z;
  1156. SQLITE_SKIP_UTF8(z);
  1157. aLen[nChar] = (u8)(z - azChar[nChar]);
  1158. }
  1159. }
  1160. }
  1161. if( nChar>0 ){
  1162. flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
  1163. if( flags & 1 ){
  1164. while( nIn>0 ){
  1165. int len = 0;
  1166. for(i=0; i<nChar; i++){
  1167. len = aLen[i];
  1168. if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
  1169. }
  1170. if( i>=nChar ) break;
  1171. zIn += len;
  1172. nIn -= len;
  1173. }
  1174. }
  1175. if( flags & 2 ){
  1176. while( nIn>0 ){
  1177. int len = 0;
  1178. for(i=0; i<nChar; i++){
  1179. len = aLen[i];
  1180. if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
  1181. }
  1182. if( i>=nChar ) break;
  1183. nIn -= len;
  1184. }
  1185. }
  1186. if( zCharSet ){
  1187. sqlite3_free(azChar);
  1188. }
  1189. }
  1190. sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
  1191. }
  1192. /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
  1193. ** is only available if the SQLITE_SOUNDEX compile-time option is used
  1194. ** when SQLite is built.
  1195. */
  1196. #ifdef SQLITE_SOUNDEX
  1197. /*
  1198. ** Compute the soundex encoding of a word.
  1199. **
  1200. ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
  1201. ** soundex encoding of the string X.
  1202. */
  1203. static void soundexFunc(
  1204. sqlite3_context *context,
  1205. int argc,
  1206. sqlite3_value **argv
  1207. ){
  1208. char zResult[8];
  1209. const u8 *zIn;
  1210. int i, j;
  1211. static const unsigned char iCode[] = {
  1212. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1213. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1214. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1215. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1216. 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
  1217. 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
  1218. 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
  1219. 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
  1220. };
  1221. assert( argc==1 );
  1222. zIn = (u8*)sqlite3_value_text(argv[0]);
  1223. if( zIn==0 ) zIn = (u8*)"";
  1224. for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
  1225. if( zIn[i] ){
  1226. u8 prevcode = iCode[zIn[i]&0x7f];
  1227. zResult[0] = sqlite3Toupper(zIn[i]);
  1228. for(j=1; j<4 && zIn[i]; i++){
  1229. int code = iCode[zIn[i]&0x7f];
  1230. if( code>0 ){
  1231. if( code!=prevcode ){
  1232. prevcode = code;
  1233. zResult[j++] = code + '0';
  1234. }
  1235. }else{
  1236. prevcode = 0;
  1237. }
  1238. }
  1239. while( j<4 ){
  1240. zResult[j++] = '0';
  1241. }
  1242. zResult[j] = 0;
  1243. sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
  1244. }else{
  1245. /* IMP: R-64894-50321 The string "?000" is returned if the argument
  1246. ** is NULL or contains no ASCII alphabetic characters. */
  1247. sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
  1248. }
  1249. }
  1250. #endif /* SQLITE_SOUNDEX */
  1251. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1252. /*
  1253. ** A function that loads a shared-library extension then returns NULL.
  1254. */
  1255. static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
  1256. const char *zFile = (const char *)sqlite3_value_text(argv[0]);
  1257. const char *zProc;
  1258. sqlite3 *db = sqlite3_context_db_handle(context);
  1259. char *zErrMsg = 0;
  1260. if( argc==2 ){
  1261. zProc = (const char *)sqlite3_value_text(argv[1]);
  1262. }else{
  1263. zProc = 0;
  1264. }
  1265. if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
  1266. sqlite3_result_error(context, zErrMsg, -1);
  1267. sqlite3_free(zErrMsg);
  1268. }
  1269. }
  1270. #endif
  1271. /*
  1272. ** An instance of the following structure holds the context of a
  1273. ** sum() or avg() aggregate computation.
  1274. */
  1275. typedef struct SumCtx SumCtx;
  1276. struct SumCtx {
  1277. double rSum; /* Floating point sum */
  1278. i64 iSum; /* Integer sum */
  1279. i64 cnt; /* Number of elements summed */
  1280. u8 overflow; /* True if integer overflow seen */
  1281. u8 approx; /* True if non-integer value was input to the sum */
  1282. };
  1283. /*
  1284. ** Routines used to compute the sum, average, and total.
  1285. **
  1286. ** The SUM() function follows the (broken) SQL standard which means
  1287. ** that it returns NULL if it sums over no inputs. TOTAL returns
  1288. ** 0.0 in that case. In addition, TOTAL always returns a float where
  1289. ** SUM might return an integer if it never encounters a floating point
  1290. ** value. TOTAL never fails, but SUM might through an exception if
  1291. ** it overflows an integer.
  1292. */
  1293. static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
  1294. SumCtx *p;
  1295. int type;
  1296. assert( argc==1 );
  1297. UNUSED_PARAMETER(argc);
  1298. p = sqlite3_aggregate_context(context, sizeof(*p));
  1299. type = sqlite3_value_numeric_type(argv[0]);
  1300. if( p && type!=SQLITE_NULL ){
  1301. p->cnt++;
  1302. if( type==SQLITE_INTEGER ){
  1303. i64 v = sqlite3_value_int64(argv[0]);
  1304. p->rSum += v;
  1305. if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
  1306. p->overflow = 1;
  1307. }
  1308. }else{
  1309. p->rSum += sqlite3_value_double(argv[0]);
  1310. p->approx = 1;
  1311. }
  1312. }
  1313. }
  1314. static void sumFinalize(sqlite3_context *context){
  1315. SumCtx *p;
  1316. p = sqlite3_aggregate_context(context, 0);
  1317. if( p && p->cnt>0 ){
  1318. if( p->overflow ){
  1319. sqlite3_result_error(context,"integer overflow",-1);
  1320. }else if( p->approx ){
  1321. sqlite3_result_double(context, p->rSum);
  1322. }else{
  1323. sqlite3_result_int64(context, p->iSum);
  1324. }
  1325. }
  1326. }
  1327. static void avgFinalize(sqlite3_context *context){
  1328. SumCtx *p;
  1329. p = sqlite3_aggregate_context(context, 0);
  1330. if( p && p->cnt>0 ){
  1331. sqlite3_result_double(context, p->rSum/(double)p->cnt);
  1332. }
  1333. }
  1334. static void totalFinalize(sqlite3_context *context){
  1335. SumCtx *p;
  1336. p = sqlite3_aggregate_context(context, 0);
  1337. /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
  1338. sqlite3_result_double(context, p ? p->rSum : (double)0);
  1339. }
  1340. /*
  1341. ** The following structure keeps track of state information for the
  1342. ** count() aggregate function.
  1343. */
  1344. typedef struct CountCtx CountCtx;
  1345. struct CountCtx {
  1346. i64 n;
  1347. };
  1348. /*
  1349. ** Routines to implement the count() aggregate function.
  1350. */
  1351. static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
  1352. CountCtx *p;
  1353. p = sqlite3_aggregate_context(context, sizeof(*p));
  1354. if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
  1355. p->n++;
  1356. }
  1357. #ifndef SQLITE_OMIT_DEPRECATED
  1358. /* The sqlite3_aggregate_count() function is deprecated. But just to make
  1359. ** sure it still operates correctly, verify that its count agrees with our
  1360. ** internal count when using count(*) and when the total count can be
  1361. ** expressed as a 32-bit integer. */
  1362. assert( argc==1 || p==0 || p->n>0x7fffffff
  1363. || p->n==sqlite3_aggregate_count(context) );
  1364. #endif
  1365. }
  1366. static void countFinalize(sqlite3_context *context){
  1367. CountCtx *p;
  1368. p = sqlite3_aggregate_context(context, 0);
  1369. sqlite3_result_int64(context, p ? p->n : 0);
  1370. }
  1371. /*
  1372. ** Routines to implement min() and max() aggregate functions.
  1373. */
  1374. static void minmaxStep(
  1375. sqlite3_context *context,
  1376. int NotUsed,
  1377. sqlite3_value **argv
  1378. ){
  1379. Mem *pArg = (Mem *)argv[0];
  1380. Mem *pBest;
  1381. UNUSED_PARAMETER(NotUsed);
  1382. pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
  1383. if( !pBest ) return;
  1384. if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
  1385. if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
  1386. }else if( pBest->flags ){
  1387. int max;
  1388. int cmp;
  1389. CollSeq *pColl = sqlite3GetFuncCollSeq(context);
  1390. /* This step function is used for both the min() and max() aggregates,
  1391. ** the only difference between the two being that the sense of the
  1392. ** comparison is inverted. For the max() aggregate, the
  1393. ** sqlite3_user_data() function returns (void *)-1. For min() it
  1394. ** returns (void *)db, where db is the sqlite3* database pointer.
  1395. ** Therefore the next statement sets variable 'max' to 1 for the max()
  1396. ** aggregate, or 0 for min().
  1397. */
  1398. max = sqlite3_user_data(context)!=0;
  1399. cmp = sqlite3MemCompare(pBest, pArg, pColl);
  1400. if( (max && cmp<0) || (!max && cmp>0) ){
  1401. sqlite3VdbeMemCopy(pBest, pArg);
  1402. }else{
  1403. sqlite3SkipAccumulatorLoad(context);
  1404. }
  1405. }else{
  1406. sqlite3VdbeMemCopy(pBest, pArg);
  1407. }
  1408. }
  1409. static void minMaxFinalize(sqlite3_context *context){
  1410. sqlite3_value *pRes;
  1411. pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
  1412. if( pRes ){
  1413. if( pRes->flags ){
  1414. sqlite3_result_value(context, pRes);
  1415. }
  1416. sqlite3VdbeMemRelease(pRes);
  1417. }
  1418. }
  1419. /*
  1420. ** group_concat(EXPR, ?SEPARATOR?)
  1421. */
  1422. static void groupConcatStep(
  1423. sqlite3_context *context,
  1424. int argc,
  1425. sqlite3_value **argv
  1426. ){
  1427. const char *zVal;
  1428. StrAccum *pAccum;
  1429. const char *zSep;
  1430. int nVal, nSep;
  1431. assert( argc==1 || argc==2 );
  1432. if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  1433. pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
  1434. if( pAccum ){
  1435. sqlite3 *db = sqlite3_context_db_handle(context);
  1436. int firstTerm = pAccum->useMalloc==0;
  1437. pAccum->useMalloc = 2;
  1438. pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
  1439. if( !firstTerm ){
  1440. if( argc==2 ){
  1441. zSep = (char*)sqlite3_value_text(argv[1]);
  1442. nSep = sqlite3_value_bytes(argv[1]);
  1443. }else{
  1444. zSep = ",";
  1445. nSep = 1;
  1446. }
  1447. sqlite3StrAccumAppend(pAccum, zSep, nSep);
  1448. }
  1449. zVal = (char*)sqlite3_value_text(argv[0]);
  1450. nVal = sqlite3_value_bytes(argv[0]);
  1451. sqlite3StrAccumAppend(pAccum, zVal, nVal);
  1452. }
  1453. }
  1454. static void groupConcatFinalize(sqlite3_context *context){
  1455. StrAccum *pAccum;
  1456. pAccum = sqlite3_aggregate_context(context, 0);
  1457. if( pAccum ){
  1458. if( pAccum->accError==STRACCUM_TOOBIG ){
  1459. sqlite3_result_error_toobig(context);
  1460. }else if( pAccum->accError==STRACCUM_NOMEM ){
  1461. sqlite3_result_error_nomem(context);
  1462. }else{
  1463. sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
  1464. sqlite3_free);
  1465. }
  1466. }
  1467. }
  1468. /*
  1469. ** This routine does per-connection function registration. Most
  1470. ** of the built-in functions above are part of the global function set.
  1471. ** This routine only deals with those that are not global.
  1472. */
  1473. void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
  1474. int rc = sqlite3_overload_function(db, "MATCH", 2);
  1475. assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
  1476. if( rc==SQLITE_NOMEM ){
  1477. db->mallocFailed = 1;
  1478. }
  1479. }
  1480. /*
  1481. ** Set the LIKEOPT flag on the 2-argument function with the given name.
  1482. */
  1483. static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
  1484. FuncDef *pDef;
  1485. pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
  1486. 2, SQLITE_UTF8, 0);
  1487. if( ALWAYS(pDef) ){
  1488. pDef->funcFlags |= flagVal;
  1489. }
  1490. }
  1491. /*
  1492. ** Register the built-in LIKE and GLOB functions. The caseSensitive
  1493. ** parameter determines whether or not the LIKE operator is case
  1494. ** sensitive. GLOB is always case sensitive.
  1495. */
  1496. void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
  1497. struct compareInfo *pInfo;
  1498. if( caseSensitive ){
  1499. pInfo = (struct compareInfo*)&likeInfoAlt;
  1500. }else{
  1501. pInfo = (struct compareInfo*)&likeInfoNorm;
  1502. }
  1503. sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
  1504. sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
  1505. sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
  1506. (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
  1507. setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
  1508. setLikeOptFlag(db, "like",
  1509. caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
  1510. }
  1511. /*
  1512. ** pExpr points to an expression which implements a function. If
  1513. ** it is appropriate to apply the LIKE optimization to that function
  1514. ** then set aWc[0] through aWc[2] to the wildcard characters and
  1515. ** return TRUE. If the function is not a LIKE-style function then
  1516. ** return FALSE.
  1517. */
  1518. int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
  1519. FuncDef *pDef;
  1520. if( pExpr->op!=TK_FUNCTION
  1521. || !pExpr->x.pList
  1522. || pExpr->x.pList->nExpr!=2
  1523. ){
  1524. return 0;
  1525. }
  1526. assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  1527. pDef = sqlite3FindFunction(db, pExpr->u.zToken,
  1528. sqlite3Strlen30(pExpr->u.zToken),
  1529. 2, SQLITE_UTF8, 0);
  1530. if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
  1531. return 0;
  1532. }
  1533. /* The memcpy() statement assumes that the wildcard characters are
  1534. ** the first three statements in the compareInfo structure. The
  1535. ** asserts() that follow verify that assumption
  1536. */
  1537. memcpy(aWc, pDef->pUserData, 3);
  1538. assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
  1539. assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
  1540. assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
  1541. *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
  1542. return 1;
  1543. }
  1544. /*
  1545. ** All all of the FuncDef structures in the aBuiltinFunc[] array above
  1546. ** to the global function hash table. This occurs at start-time (as
  1547. ** a consequence of calling sqlite3_initialize()).
  1548. **
  1549. ** After this routine runs
  1550. */
  1551. void sqlite3RegisterGlobalFunctions(void){
  1552. /*
  1553. ** The following array holds FuncDef structures for all of the functions
  1554. ** defined in this file.
  1555. **
  1556. ** The array cannot be constant since changes are made to the
  1557. ** FuncDef.pHash elements at start-time. The elements of this array
  1558. ** are read-only after initialization is complete.
  1559. */
  1560. static SQLITE_WSD FuncDef aBuiltinFunc[] = {
  1561. FUNCTION(ltrim, 1, 1, 0, trimFunc ),
  1562. FUNCTION(ltrim, 2, 1, 0, trimFunc ),
  1563. FUNCTION(rtrim, 1, 2, 0, trimFunc ),
  1564. FUNCTION(rtrim, 2, 2, 0, trimFunc ),
  1565. FUNCTION(trim, 1, 3, 0, trimFunc ),
  1566. FUNCTION(trim, 2, 3, 0, trimFunc ),
  1567. FUNCTION(min, -1, 0, 1, minmaxFunc ),
  1568. FUNCTION(min, 0, 0, 1, 0 ),
  1569. AGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize ),
  1570. FUNCTION(max, -1, 1, 1, minmaxFunc ),
  1571. FUNCTION(max, 0, 1, 1, 0 ),
  1572. AGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize ),
  1573. FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
  1574. FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
  1575. FUNCTION(instr, 2, 0, 0, instrFunc ),
  1576. FUNCTION(substr, 2, 0, 0, substrFunc ),
  1577. FUNCTION(substr, 3, 0, 0, substrFunc ),
  1578. FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
  1579. FUNCTION(char, -1, 0, 0, charFunc ),
  1580. FUNCTION(abs, 1, 0, 0, absFunc ),
  1581. #ifndef SQLITE_OMIT_FLOATING_POINT
  1582. FUNCTION(round, 1, 0, 0, roundFunc ),
  1583. FUNCTION(round, 2, 0, 0, roundFunc ),
  1584. #endif
  1585. FUNCTION(upper, 1, 0, 0, upperFunc ),
  1586. FUNCTION(lower, 1, 0, 0, lowerFunc ),
  1587. FUNCTION(coalesce, 1, 0, 0, 0 ),
  1588. FUNCTION(coalesce, 0, 0, 0, 0 ),
  1589. FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
  1590. FUNCTION(hex, 1, 0, 0, hexFunc ),
  1591. FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
  1592. FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
  1593. FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
  1594. FUNCTION(random, 0, 0, 0, randomFunc ),
  1595. FUNCTION(randomblob, 1, 0, 0, randomBlob ),
  1596. FUNCTION(nullif, 2, 0, 1, nullifFunc ),
  1597. FUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
  1598. FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
  1599. FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
  1600. #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  1601. FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
  1602. FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
  1603. #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
  1604. FUNCTION(quote, 1, 0, 0, quoteFunc ),
  1605. FUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
  1606. FUNCTION(changes, 0, 0, 0, changes ),
  1607. FUNCTION(total_changes, 0, 0, 0, total_changes ),
  1608. FUNCTION(replace, 3, 0, 0, replaceFunc ),
  1609. FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
  1610. #ifdef SQLITE_SOUNDEX
  1611. FUNCTION(soundex, 1, 0, 0, soundexFunc ),
  1612. #endif
  1613. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1614. FUNCTION(load_extension, 1, 0, 0, loadExt ),
  1615. FUNCTION(load_extension, 2, 0, 0, loadExt ),
  1616. #endif
  1617. AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ),
  1618. AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ),
  1619. AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ),
  1620. /* AGGREGATE(count, 0, 0, 0, countStep, countFinalize ), */
  1621. {0,SQLITE_UTF8|SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
  1622. AGGREGATE(count, 1, 0, 0, countStep, countFinalize ),
  1623. AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize),
  1624. AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize),
  1625. LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
  1626. #ifdef SQLITE_CASE_SENSITIVE_LIKE
  1627. LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
  1628. LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
  1629. #else
  1630. LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
  1631. LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
  1632. #endif
  1633. };
  1634. int i;
  1635. FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
  1636. FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
  1637. for(i=0; i<ArraySize(aBuiltinFunc); i++){
  1638. sqlite3FuncDefInsert(pHash, &aFunc[i]);
  1639. }
  1640. sqlite3RegisterDateTimeFunctions();
  1641. #ifndef SQLITE_OMIT_ALTERTABLE
  1642. sqlite3AlterFunctions();
  1643. #endif
  1644. #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
  1645. sqlite3AnalyzeFunctions();
  1646. #endif
  1647. }