4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
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:
19 ** * An implementation of the SQL regexp() function (and hence REGEXP
20 ** operator) using the ICU uregex_XX() APIs.
22 ** * Implementations of the SQL scalar upper() and lower() functions
25 ** * Integration of ICU and SQLite collation sequences.
27 ** * An implementation of the LIKE operator that uses ICU to
28 ** provide case-independent matching.
31 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
33 /* Include ICU headers */
34 #include <unicode/utypes.h>
35 #include <unicode/uregex.h>
36 #include <unicode/ustring.h>
37 #include <unicode/ucol.h>
42 #include "sqlite3ext.h"
43 SQLITE_EXTENSION_INIT1
49 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
52 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
53 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
57 ** Version of sqlite3_free() that is always a function, never a macro.
59 static void xFree(void *p
){
64 ** Compare two UTF-8 strings for equality where the first string is
65 ** a "LIKE" expression. Return true (1) if they are the same and
66 ** false (0) if they are different.
68 static int icuLikeCompare(
69 const uint8_t *zPattern
, /* LIKE pattern */
70 const uint8_t *zString
, /* The UTF-8 string to compare against */
71 const UChar32 uEsc
/* The escape character */
73 static const int MATCH_ONE
= (UChar32
)'_';
74 static const int MATCH_ALL
= (UChar32
)'%';
76 int iPattern
= 0; /* Current byte index in zPattern */
77 int iString
= 0; /* Current byte index in zString */
79 int prevEscape
= 0; /* True if the previous character was uEsc */
81 while( zPattern
[iPattern
]!=0 ){
83 /* Read (and consume) the next character from the input pattern. */
85 U8_NEXT_UNSAFE(zPattern
, iPattern
, uPattern
);
88 /* There are now 4 possibilities:
90 ** 1. uPattern is an unescaped match-all character "%",
91 ** 2. uPattern is an unescaped match-one character "_",
92 ** 3. uPattern is an unescaped escape character, or
93 ** 4. uPattern is to be handled as an ordinary character
95 if( !prevEscape
&& uPattern
==MATCH_ALL
){
99 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
100 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
103 while( (c
=zPattern
[iPattern
]) == MATCH_ALL
|| c
== MATCH_ONE
){
105 if( zString
[iString
]==0 ) return 0;
106 U8_FWD_1_UNSAFE(zString
, iString
);
111 if( zPattern
[iPattern
]==0 ) return 1;
113 while( zString
[iString
] ){
114 if( icuLikeCompare(&zPattern
[iPattern
], &zString
[iString
], uEsc
) ){
117 U8_FWD_1_UNSAFE(zString
, iString
);
121 }else if( !prevEscape
&& uPattern
==MATCH_ONE
){
123 if( zString
[iString
]==0 ) return 0;
124 U8_FWD_1_UNSAFE(zString
, iString
);
126 }else if( !prevEscape
&& uPattern
==uEsc
){
133 U8_NEXT_UNSAFE(zString
, iString
, uString
);
134 uString
= u_foldCase(uString
, U_FOLD_CASE_DEFAULT
);
135 uPattern
= u_foldCase(uPattern
, U_FOLD_CASE_DEFAULT
);
136 if( uString
!=uPattern
){
143 return zString
[iString
]==0;
147 ** Implementation of the like() SQL function. This function implements
148 ** the build-in LIKE operator. The first argument to the function is the
149 ** pattern and the second argument is the string. So, the SQL statements:
153 ** is implemented as like(B, A). If there is an escape character E,
157 ** is mapped to like(B, A, E).
159 static void icuLikeFunc(
160 sqlite3_context
*context
,
164 const unsigned char *zA
= sqlite3_value_text(argv
[0]);
165 const unsigned char *zB
= sqlite3_value_text(argv
[1]);
168 /* Limit the length of the LIKE or GLOB pattern to avoid problems
169 ** of deep recursion and N*N behavior in patternCompare().
171 if( sqlite3_value_bytes(argv
[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH
){
172 sqlite3_result_error(context
, "LIKE or GLOB pattern too complex", -1);
178 /* The escape character string must consist of a single UTF-8 character.
179 ** Otherwise, return an error.
181 int nE
= sqlite3_value_bytes(argv
[2]);
182 const unsigned char *zE
= sqlite3_value_text(argv
[2]);
185 U8_NEXT(zE
, i
, nE
, uEsc
);
187 sqlite3_result_error(context
,
188 "ESCAPE expression must be a single character", -1);
194 sqlite3_result_int(context
, icuLikeCompare(zA
, zB
, uEsc
));
199 ** This function is called when an ICU function called from within
200 ** the implementation of an SQL scalar function returns an error.
202 ** The scalar function context passed as the first argument is
203 ** loaded with an error message based on the following two args.
205 static void icuFunctionError(
206 sqlite3_context
*pCtx
, /* SQLite scalar function context */
207 const char *zName
, /* Name of ICU function that failed */
208 UErrorCode e
/* Error code returned by ICU function */
211 sqlite3_snprintf(128, zBuf
, "ICU error: %s(): %s", zName
, u_errorName(e
));
213 sqlite3_result_error(pCtx
, zBuf
, -1);
217 ** Function to delete compiled regexp objects. Registered as
218 ** a destructor function with sqlite3_set_auxdata().
220 static void icuRegexpDelete(void *p
){
221 URegularExpression
*pExpr
= (URegularExpression
*)p
;
226 ** Implementation of SQLite REGEXP operator. This scalar function takes
227 ** two arguments. The first is a regular expression pattern to compile
228 ** the second is a string to match against that pattern. If either
229 ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
230 ** is 1 if the string matches the pattern, or 0 otherwise.
232 ** SQLite maps the regexp() function to the regexp() operator such
233 ** that the following two are equivalent:
235 ** zString REGEXP zPattern
236 ** regexp(zPattern, zString)
238 ** Uses the following ICU regexp APIs:
244 static void icuRegexpFunc(sqlite3_context
*p
, int nArg
, sqlite3_value
**apArg
){
245 UErrorCode status
= U_ZERO_ERROR
;
246 URegularExpression
*pExpr
;
248 const UChar
*zString
= sqlite3_value_text16(apArg
[1]);
250 (void)nArg
; /* Unused parameter */
252 /* If the left hand side of the regexp operator is NULL,
253 ** then the result is also NULL.
259 pExpr
= sqlite3_get_auxdata(p
, 0);
261 const UChar
*zPattern
= sqlite3_value_text16(apArg
[0]);
265 pExpr
= uregex_open(zPattern
, -1, 0, 0, &status
);
267 if( U_SUCCESS(status
) ){
268 sqlite3_set_auxdata(p
, 0, pExpr
, icuRegexpDelete
);
271 icuFunctionError(p
, "uregex_open", status
);
276 /* Configure the text that the regular expression operates on. */
277 uregex_setText(pExpr
, zString
, -1, &status
);
278 if( !U_SUCCESS(status
) ){
279 icuFunctionError(p
, "uregex_setText", status
);
283 /* Attempt the match */
284 res
= uregex_matches(pExpr
, 0, &status
);
285 if( !U_SUCCESS(status
) ){
286 icuFunctionError(p
, "uregex_matches", status
);
290 /* Set the text that the regular expression operates on to a NULL
291 ** pointer. This is not really necessary, but it is tidier than
292 ** leaving the regular expression object configured with an invalid
293 ** pointer after this function returns.
295 uregex_setText(pExpr
, 0, 0, &status
);
298 sqlite3_result_int(p
, res
? 1 : 0);
302 ** Implementations of scalar functions for case mapping - upper() and
303 ** lower(). Function upper() converts its input to upper-case (ABC).
304 ** Function lower() converts to lower-case (abc).
306 ** ICU provides two types of case mapping, "general" case mapping and
307 ** "language specific". Refer to ICU documentation for the differences
310 ** To utilise "general" case mapping, the upper() or lower() scalar
311 ** functions are invoked with one argument:
313 ** upper('ABC') -> 'abc'
314 ** lower('abc') -> 'ABC'
316 ** To access ICU "language specific" case mapping, upper() or lower()
317 ** should be invoked with two arguments. The second argument is the name
318 ** of the locale to use. Passing an empty string ("") or SQL NULL value
319 ** as the second argument is the same as invoking the 1 argument version
320 ** of upper() or lower().
322 ** lower('I', 'en_us') -> 'i'
323 ** lower('I', 'tr_tr') -> 'ı' (small dotless i)
325 ** http://www.icu-project.org/userguide/posix.html#case_mappings
327 static void icuCaseFunc16(sqlite3_context
*p
, int nArg
, sqlite3_value
**apArg
){
333 UErrorCode status
= U_ZERO_ERROR
;
334 const char *zLocale
= 0;
336 assert(nArg
==1 || nArg
==2);
338 zLocale
= (const char *)sqlite3_value_text(apArg
[1]);
341 zInput
= sqlite3_value_text16(apArg
[0]);
345 nInput
= sqlite3_value_bytes16(apArg
[0]);
347 nOutput
= nInput
* 2 + 2;
348 zOutput
= sqlite3_malloc(nOutput
);
353 if( sqlite3_user_data(p
) ){
354 u_strToUpper(zOutput
, nOutput
/2, zInput
, nInput
/2, zLocale
, &status
);
356 u_strToLower(zOutput
, nOutput
/2, zInput
, nInput
/2, zLocale
, &status
);
359 if( !U_SUCCESS(status
) ){
360 icuFunctionError(p
, "u_strToLower()/u_strToUpper", status
);
364 sqlite3_result_text16(p
, zOutput
, -1, xFree
);
368 ** Collation sequence destructor function. The pCtx argument points to
369 ** a UCollator structure previously allocated using ucol_open().
371 static void icuCollationDel(void *pCtx
){
372 UCollator
*p
= (UCollator
*)pCtx
;
377 ** Collation sequence comparison function. The pCtx argument points to
378 ** a UCollator structure previously allocated using ucol_open().
380 static int icuCollationColl(
387 UCollationResult res
;
388 UCollator
*p
= (UCollator
*)pCtx
;
389 res
= ucol_strcoll(p
, (UChar
*)zLeft
, nLeft
/2, (UChar
*)zRight
, nRight
/2);
391 case UCOL_LESS
: return -1;
392 case UCOL_GREATER
: return +1;
393 case UCOL_EQUAL
: return 0;
395 assert(!"Unexpected return value from ucol_strcoll()");
400 ** Implementation of the scalar function icu_load_collation().
402 ** This scalar function is used to add ICU collation based collation
403 ** types to an SQLite database connection. It is intended to be called
406 ** SELECT icu_load_collation(<locale>, <collation-name>);
408 ** Where <locale> is a string containing an ICU locale identifier (i.e.
409 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
410 ** collation sequence to create.
412 static void icuLoadCollation(
415 sqlite3_value
**apArg
417 sqlite3
*db
= (sqlite3
*)sqlite3_user_data(p
);
418 UErrorCode status
= U_ZERO_ERROR
;
419 const char *zLocale
; /* Locale identifier - (eg. "jp_JP") */
420 const char *zName
; /* SQL Collation sequence name (eg. "japanese") */
421 UCollator
*pUCollator
; /* ICU library collation object */
422 int rc
; /* Return code from sqlite3_create_collation_x() */
425 zLocale
= (const char *)sqlite3_value_text(apArg
[0]);
426 zName
= (const char *)sqlite3_value_text(apArg
[1]);
428 if( !zLocale
|| !zName
){
432 pUCollator
= ucol_open(zLocale
, &status
);
433 if( !U_SUCCESS(status
) ){
434 icuFunctionError(p
, "ucol_open", status
);
439 rc
= sqlite3_create_collation_v2(db
, zName
, SQLITE_UTF16
, (void *)pUCollator
,
440 icuCollationColl
, icuCollationDel
443 ucol_close(pUCollator
);
444 sqlite3_result_error(p
, "Error registering collation function", -1);
449 ** Register the ICU extension functions with database db.
451 int sqlite3IcuInit(sqlite3
*db
){
453 const char *zName
; /* Function name */
454 int nArg
; /* Number of arguments */
455 int enc
; /* Optimal text encoding */
456 void *pContext
; /* sqlite3_user_data() context */
457 void (*xFunc
)(sqlite3_context
*,int,sqlite3_value
**);
459 {"regexp", 2, SQLITE_ANY
, 0, icuRegexpFunc
},
461 {"lower", 1, SQLITE_UTF16
, 0, icuCaseFunc16
},
462 {"lower", 2, SQLITE_UTF16
, 0, icuCaseFunc16
},
463 {"upper", 1, SQLITE_UTF16
, (void*)1, icuCaseFunc16
},
464 {"upper", 2, SQLITE_UTF16
, (void*)1, icuCaseFunc16
},
466 {"lower", 1, SQLITE_UTF8
, 0, icuCaseFunc16
},
467 {"lower", 2, SQLITE_UTF8
, 0, icuCaseFunc16
},
468 {"upper", 1, SQLITE_UTF8
, (void*)1, icuCaseFunc16
},
469 {"upper", 2, SQLITE_UTF8
, (void*)1, icuCaseFunc16
},
471 {"like", 2, SQLITE_UTF8
, 0, icuLikeFunc
},
472 {"like", 3, SQLITE_UTF8
, 0, icuLikeFunc
},
474 {"icu_load_collation", 2, SQLITE_UTF8
, (void*)db
, icuLoadCollation
},
480 for(i
=0; rc
==SQLITE_OK
&& i
<(int)(sizeof(scalars
)/sizeof(scalars
[0])); i
++){
481 struct IcuScalar
*p
= &scalars
[i
];
482 rc
= sqlite3_create_function(
483 db
, p
->zName
, p
->nArg
, p
->enc
, p
->pContext
, p
->xFunc
, 0, 0
492 __declspec(dllexport
)
494 int sqlite3_icu_init(
497 const sqlite3_api_routines
*pApi
499 SQLITE_EXTENSION_INIT2(pApi
)
500 return sqlite3IcuInit(db
);