icu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. ** 2007 May 6
  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. ** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
  13. **
  14. ** This file implements an integration between the ICU library
  15. ** ("International Components for Unicode", an open-source library
  16. ** for handling unicode data) and SQLite. The integration uses
  17. ** ICU to provide the following to SQLite:
  18. **
  19. ** * An implementation of the SQL regexp() function (and hence REGEXP
  20. ** operator) using the ICU uregex_XX() APIs.
  21. **
  22. ** * Implementations of the SQL scalar upper() and lower() functions
  23. ** for case mapping.
  24. **
  25. ** * Integration of ICU and SQLite collation sequences.
  26. **
  27. ** * An implementation of the LIKE operator that uses ICU to
  28. ** provide case-independent matching.
  29. */
  30. #if !defined(SQLITE_CORE) \
  31. || defined(SQLITE_ENABLE_ICU) \
  32. || defined(SQLITE_ENABLE_ICU_COLLATIONS)
  33. /* Include ICU headers */
  34. #include <unicode/utypes.h>
  35. #include <unicode/uregex.h>
  36. #include <unicode/ustring.h>
  37. #include <unicode/ucol.h>
  38. #include <assert.h>
  39. #ifndef SQLITE_CORE
  40. #include "sqlite3ext.h"
  41. SQLITE_EXTENSION_INIT1
  42. #else
  43. #include "sqlite3.h"
  44. #endif
  45. /*
  46. ** This function is called when an ICU function called from within
  47. ** the implementation of an SQL scalar function returns an error.
  48. **
  49. ** The scalar function context passed as the first argument is
  50. ** loaded with an error message based on the following two args.
  51. */
  52. static void icuFunctionError(
  53. sqlite3_context *pCtx, /* SQLite scalar function context */
  54. const char *zName, /* Name of ICU function that failed */
  55. UErrorCode e /* Error code returned by ICU function */
  56. ){
  57. char zBuf[128];
  58. sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
  59. zBuf[127] = '\0';
  60. sqlite3_result_error(pCtx, zBuf, -1);
  61. }
  62. #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
  63. /*
  64. ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
  65. ** operator.
  66. */
  67. #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
  68. # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
  69. #endif
  70. /*
  71. ** Version of sqlite3_free() that is always a function, never a macro.
  72. */
  73. static void xFree(void *p){
  74. sqlite3_free(p);
  75. }
  76. /*
  77. ** This lookup table is used to help decode the first byte of
  78. ** a multi-byte UTF8 character. It is copied here from SQLite source
  79. ** code file utf8.c.
  80. */
  81. static const unsigned char icuUtf8Trans1[] = {
  82. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  83. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  84. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  85. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  86. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  87. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  88. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  89. 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
  90. };
  91. #define SQLITE_ICU_READ_UTF8(zIn, c) \
  92. c = *(zIn++); \
  93. if( c>=0xc0 ){ \
  94. c = icuUtf8Trans1[c-0xc0]; \
  95. while( (*zIn & 0xc0)==0x80 ){ \
  96. c = (c<<6) + (0x3f & *(zIn++)); \
  97. } \
  98. }
  99. #define SQLITE_ICU_SKIP_UTF8(zIn) \
  100. assert( *zIn ); \
  101. if( *(zIn++)>=0xc0 ){ \
  102. while( (*zIn & 0xc0)==0x80 ){zIn++;} \
  103. }
  104. /*
  105. ** Compare two UTF-8 strings for equality where the first string is
  106. ** a "LIKE" expression. Return true (1) if they are the same and
  107. ** false (0) if they are different.
  108. */
  109. static int icuLikeCompare(
  110. const uint8_t *zPattern, /* LIKE pattern */
  111. const uint8_t *zString, /* The UTF-8 string to compare against */
  112. const UChar32 uEsc /* The escape character */
  113. ){
  114. static const uint32_t MATCH_ONE = (uint32_t)'_';
  115. static const uint32_t MATCH_ALL = (uint32_t)'%';
  116. int prevEscape = 0; /* True if the previous character was uEsc */
  117. while( 1 ){
  118. /* Read (and consume) the next character from the input pattern. */
  119. uint32_t uPattern;
  120. SQLITE_ICU_READ_UTF8(zPattern, uPattern);
  121. if( uPattern==0 ) break;
  122. /* There are now 4 possibilities:
  123. **
  124. ** 1. uPattern is an unescaped match-all character "%",
  125. ** 2. uPattern is an unescaped match-one character "_",
  126. ** 3. uPattern is an unescaped escape character, or
  127. ** 4. uPattern is to be handled as an ordinary character
  128. */
  129. if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){
  130. /* Case 1. */
  131. uint8_t c;
  132. /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
  133. ** MATCH_ALL. For each MATCH_ONE, skip one character in the
  134. ** test string.
  135. */
  136. while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
  137. if( c==MATCH_ONE ){
  138. if( *zString==0 ) return 0;
  139. SQLITE_ICU_SKIP_UTF8(zString);
  140. }
  141. zPattern++;
  142. }
  143. if( *zPattern==0 ) return 1;
  144. while( *zString ){
  145. if( icuLikeCompare(zPattern, zString, uEsc) ){
  146. return 1;
  147. }
  148. SQLITE_ICU_SKIP_UTF8(zString);
  149. }
  150. return 0;
  151. }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){
  152. /* Case 2. */
  153. if( *zString==0 ) return 0;
  154. SQLITE_ICU_SKIP_UTF8(zString);
  155. }else if( uPattern==(uint32_t)uEsc && !prevEscape ){
  156. /* Case 3. */
  157. prevEscape = 1;
  158. }else{
  159. /* Case 4. */
  160. uint32_t uString;
  161. SQLITE_ICU_READ_UTF8(zString, uString);
  162. uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
  163. uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
  164. if( uString!=uPattern ){
  165. return 0;
  166. }
  167. prevEscape = 0;
  168. }
  169. }
  170. return *zString==0;
  171. }
  172. /*
  173. ** Implementation of the like() SQL function. This function implements
  174. ** the build-in LIKE operator. The first argument to the function is the
  175. ** pattern and the second argument is the string. So, the SQL statements:
  176. **
  177. ** A LIKE B
  178. **
  179. ** is implemented as like(B, A). If there is an escape character E,
  180. **
  181. ** A LIKE B ESCAPE E
  182. **
  183. ** is mapped to like(B, A, E).
  184. */
  185. static void icuLikeFunc(
  186. sqlite3_context *context,
  187. int argc,
  188. sqlite3_value **argv
  189. ){
  190. const unsigned char *zA = sqlite3_value_text(argv[0]);
  191. const unsigned char *zB = sqlite3_value_text(argv[1]);
  192. UChar32 uEsc = 0;
  193. /* Limit the length of the LIKE or GLOB pattern to avoid problems
  194. ** of deep recursion and N*N behavior in patternCompare().
  195. */
  196. if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
  197. sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
  198. return;
  199. }
  200. if( argc==3 ){
  201. /* The escape character string must consist of a single UTF-8 character.
  202. ** Otherwise, return an error.
  203. */
  204. int nE= sqlite3_value_bytes(argv[2]);
  205. const unsigned char *zE = sqlite3_value_text(argv[2]);
  206. int i = 0;
  207. if( zE==0 ) return;
  208. U8_NEXT(zE, i, nE, uEsc);
  209. if( i!=nE){
  210. sqlite3_result_error(context,
  211. "ESCAPE expression must be a single character", -1);
  212. return;
  213. }
  214. }
  215. if( zA && zB ){
  216. sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
  217. }
  218. }
  219. /*
  220. ** Function to delete compiled regexp objects. Registered as
  221. ** a destructor function with sqlite3_set_auxdata().
  222. */
  223. static void icuRegexpDelete(void *p){
  224. URegularExpression *pExpr = (URegularExpression *)p;
  225. uregex_close(pExpr);
  226. }
  227. /*
  228. ** Implementation of SQLite REGEXP operator. This scalar function takes
  229. ** two arguments. The first is a regular expression pattern to compile
  230. ** the second is a string to match against that pattern. If either
  231. ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
  232. ** is 1 if the string matches the pattern, or 0 otherwise.
  233. **
  234. ** SQLite maps the regexp() function to the regexp() operator such
  235. ** that the following two are equivalent:
  236. **
  237. ** zString REGEXP zPattern
  238. ** regexp(zPattern, zString)
  239. **
  240. ** Uses the following ICU regexp APIs:
  241. **
  242. ** uregex_open()
  243. ** uregex_matches()
  244. ** uregex_close()
  245. */
  246. static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
  247. UErrorCode status = U_ZERO_ERROR;
  248. URegularExpression *pExpr;
  249. UBool res;
  250. const UChar *zString = sqlite3_value_text16(apArg[1]);
  251. (void)nArg; /* Unused parameter */
  252. /* If the left hand side of the regexp operator is NULL,
  253. ** then the result is also NULL.
  254. */
  255. if( !zString ){
  256. return;
  257. }
  258. pExpr = sqlite3_get_auxdata(p, 0);
  259. if( !pExpr ){
  260. const UChar *zPattern = sqlite3_value_text16(apArg[0]);
  261. if( !zPattern ){
  262. return;
  263. }
  264. pExpr = uregex_open(zPattern, -1, 0, 0, &status);
  265. if( U_SUCCESS(status) ){
  266. sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
  267. pExpr = sqlite3_get_auxdata(p, 0);
  268. }
  269. if( !pExpr ){
  270. icuFunctionError(p, "uregex_open", status);
  271. return;
  272. }
  273. }
  274. /* Configure the text that the regular expression operates on. */
  275. uregex_setText(pExpr, zString, -1, &status);
  276. if( !U_SUCCESS(status) ){
  277. icuFunctionError(p, "uregex_setText", status);
  278. return;
  279. }
  280. /* Attempt the match */
  281. res = uregex_matches(pExpr, 0, &status);
  282. if( !U_SUCCESS(status) ){
  283. icuFunctionError(p, "uregex_matches", status);
  284. return;
  285. }
  286. /* Set the text that the regular expression operates on to a NULL
  287. ** pointer. This is not really necessary, but it is tidier than
  288. ** leaving the regular expression object configured with an invalid
  289. ** pointer after this function returns.
  290. */
  291. uregex_setText(pExpr, 0, 0, &status);
  292. /* Return 1 or 0. */
  293. sqlite3_result_int(p, res ? 1 : 0);
  294. }
  295. /*
  296. ** Implementations of scalar functions for case mapping - upper() and
  297. ** lower(). Function upper() converts its input to upper-case (ABC).
  298. ** Function lower() converts to lower-case (abc).
  299. **
  300. ** ICU provides two types of case mapping, "general" case mapping and
  301. ** "language specific". Refer to ICU documentation for the differences
  302. ** between the two.
  303. **
  304. ** To utilise "general" case mapping, the upper() or lower() scalar
  305. ** functions are invoked with one argument:
  306. **
  307. ** upper('ABC') -> 'abc'
  308. ** lower('abc') -> 'ABC'
  309. **
  310. ** To access ICU "language specific" case mapping, upper() or lower()
  311. ** should be invoked with two arguments. The second argument is the name
  312. ** of the locale to use. Passing an empty string ("") or SQL NULL value
  313. ** as the second argument is the same as invoking the 1 argument version
  314. ** of upper() or lower().
  315. **
  316. ** lower('I', 'en_us') -> 'i'
  317. ** lower('I', 'tr_tr') -> '\u131' (small dotless i)
  318. **
  319. ** http://www.icu-project.org/userguide/posix.html#case_mappings
  320. */
  321. static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
  322. const UChar *zInput; /* Pointer to input string */
  323. UChar *zOutput = 0; /* Pointer to output buffer */
  324. int nInput; /* Size of utf-16 input string in bytes */
  325. int nOut; /* Size of output buffer in bytes */
  326. int cnt;
  327. int bToUpper; /* True for toupper(), false for tolower() */
  328. UErrorCode status;
  329. const char *zLocale = 0;
  330. assert(nArg==1 || nArg==2);
  331. bToUpper = (sqlite3_user_data(p)!=0);
  332. if( nArg==2 ){
  333. zLocale = (const char *)sqlite3_value_text(apArg[1]);
  334. }
  335. zInput = sqlite3_value_text16(apArg[0]);
  336. if( !zInput ){
  337. return;
  338. }
  339. nOut = nInput = sqlite3_value_bytes16(apArg[0]);
  340. if( nOut==0 ){
  341. sqlite3_result_text16(p, "", 0, SQLITE_STATIC);
  342. return;
  343. }
  344. for(cnt=0; cnt<2; cnt++){
  345. UChar *zNew = sqlite3_realloc(zOutput, nOut);
  346. if( zNew==0 ){
  347. sqlite3_free(zOutput);
  348. sqlite3_result_error_nomem(p);
  349. return;
  350. }
  351. zOutput = zNew;
  352. status = U_ZERO_ERROR;
  353. if( bToUpper ){
  354. nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
  355. }else{
  356. nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
  357. }
  358. if( U_SUCCESS(status) ){
  359. sqlite3_result_text16(p, zOutput, nOut, xFree);
  360. }else if( status==U_BUFFER_OVERFLOW_ERROR ){
  361. assert( cnt==0 );
  362. continue;
  363. }else{
  364. icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
  365. }
  366. return;
  367. }
  368. assert( 0 ); /* Unreachable */
  369. }
  370. #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
  371. /*
  372. ** Collation sequence destructor function. The pCtx argument points to
  373. ** a UCollator structure previously allocated using ucol_open().
  374. */
  375. static void icuCollationDel(void *pCtx){
  376. UCollator *p = (UCollator *)pCtx;
  377. ucol_close(p);
  378. }
  379. /*
  380. ** Collation sequence comparison function. The pCtx argument points to
  381. ** a UCollator structure previously allocated using ucol_open().
  382. */
  383. static int icuCollationColl(
  384. void *pCtx,
  385. int nLeft,
  386. const void *zLeft,
  387. int nRight,
  388. const void *zRight
  389. ){
  390. UCollationResult res;
  391. UCollator *p = (UCollator *)pCtx;
  392. res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
  393. switch( res ){
  394. case UCOL_LESS: return -1;
  395. case UCOL_GREATER: return +1;
  396. case UCOL_EQUAL: return 0;
  397. }
  398. assert(!"Unexpected return value from ucol_strcoll()");
  399. return 0;
  400. }
  401. /*
  402. ** Implementation of the scalar function icu_load_collation().
  403. **
  404. ** This scalar function is used to add ICU collation based collation
  405. ** types to an SQLite database connection. It is intended to be called
  406. ** as follows:
  407. **
  408. ** SELECT icu_load_collation(<locale>, <collation-name>);
  409. **
  410. ** Where <locale> is a string containing an ICU locale identifier (i.e.
  411. ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
  412. ** collation sequence to create.
  413. */
  414. static void icuLoadCollation(
  415. sqlite3_context *p,
  416. int nArg,
  417. sqlite3_value **apArg
  418. ){
  419. sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
  420. UErrorCode status = U_ZERO_ERROR;
  421. const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
  422. const char *zName; /* SQL Collation sequence name (eg. "japanese") */
  423. UCollator *pUCollator; /* ICU library collation object */
  424. int rc; /* Return code from sqlite3_create_collation_x() */
  425. assert(nArg==2 || nArg==3);
  426. (void)nArg; /* Unused parameter */
  427. zLocale = (const char *)sqlite3_value_text(apArg[0]);
  428. zName = (const char *)sqlite3_value_text(apArg[1]);
  429. if( !zLocale || !zName ){
  430. return;
  431. }
  432. pUCollator = ucol_open(zLocale, &status);
  433. if( !U_SUCCESS(status) ){
  434. icuFunctionError(p, "ucol_open", status);
  435. return;
  436. }
  437. assert(p);
  438. if(nArg==3){
  439. const char *zOption = (const char*)sqlite3_value_text(apArg[2]);
  440. static const struct {
  441. const char *zName;
  442. UColAttributeValue val;
  443. } aStrength[] = {
  444. { "PRIMARY", UCOL_PRIMARY },
  445. { "SECONDARY", UCOL_SECONDARY },
  446. { "TERTIARY", UCOL_TERTIARY },
  447. { "DEFAULT", UCOL_DEFAULT_STRENGTH },
  448. { "QUARTERNARY", UCOL_QUATERNARY },
  449. { "IDENTICAL", UCOL_IDENTICAL },
  450. };
  451. unsigned int i;
  452. for(i=0; i<sizeof(aStrength)/sizeof(aStrength[0]); i++){
  453. if( sqlite3_stricmp(zOption,aStrength[i].zName)==0 ){
  454. ucol_setStrength(pUCollator, aStrength[i].val);
  455. break;
  456. }
  457. }
  458. if( i>=sizeof(aStrength)/sizeof(aStrength[0]) ){
  459. sqlite3_str *pStr = sqlite3_str_new(sqlite3_context_db_handle(p));
  460. sqlite3_str_appendf(pStr,
  461. "unknown collation strength \"%s\" - should be one of:",
  462. zOption);
  463. for(i=0; i<sizeof(aStrength)/sizeof(aStrength[0]); i++){
  464. sqlite3_str_appendf(pStr, " %s", aStrength[i].zName);
  465. }
  466. sqlite3_result_error(p, sqlite3_str_value(pStr), -1);
  467. sqlite3_free(sqlite3_str_finish(pStr));
  468. return;
  469. }
  470. }
  471. rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
  472. icuCollationColl, icuCollationDel
  473. );
  474. if( rc!=SQLITE_OK ){
  475. ucol_close(pUCollator);
  476. sqlite3_result_error(p, "Error registering collation function", -1);
  477. }
  478. }
  479. /*
  480. ** Register the ICU extension functions with database db.
  481. */
  482. int sqlite3IcuInit(sqlite3 *db){
  483. # define SQLITEICU_EXTRAFLAGS (SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS)
  484. static const struct IcuScalar {
  485. const char *zName; /* Function name */
  486. unsigned char nArg; /* Number of arguments */
  487. unsigned int enc; /* Optimal text encoding */
  488. unsigned char iContext; /* sqlite3_user_data() context */
  489. void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  490. } scalars[] = {
  491. {"icu_load_collation",2,SQLITE_UTF8|SQLITE_DIRECTONLY,1, icuLoadCollation},
  492. {"icu_load_collation",3,SQLITE_UTF8|SQLITE_DIRECTONLY,1, icuLoadCollation},
  493. #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
  494. {"regexp", 2, SQLITE_ANY|SQLITEICU_EXTRAFLAGS, 0, icuRegexpFunc},
  495. {"lower", 1, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
  496. {"lower", 2, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
  497. {"upper", 1, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
  498. {"upper", 2, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
  499. {"lower", 1, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
  500. {"lower", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
  501. {"upper", 1, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
  502. {"upper", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
  503. {"like", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
  504. {"like", 3, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
  505. #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
  506. };
  507. int rc = SQLITE_OK;
  508. int i;
  509. for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
  510. const struct IcuScalar *p = &scalars[i];
  511. rc = sqlite3_create_function(
  512. db, p->zName, p->nArg, p->enc,
  513. p->iContext ? (void*)db : (void*)0,
  514. p->xFunc, 0, 0
  515. );
  516. }
  517. return rc;
  518. }
  519. #ifndef SQLITE_CORE
  520. #ifdef _WIN32
  521. __declspec(dllexport)
  522. #endif
  523. int sqlite3_icu_init(
  524. sqlite3 *db,
  525. char **pzErrMsg,
  526. const sqlite3_api_routines *pApi
  527. ){
  528. SQLITE_EXTENSION_INIT2(pApi)
  529. return sqlite3IcuInit(db);
  530. }
  531. #endif
  532. #endif