blob: 252a0c5b2749ec0d7e749f4ea2ac2a012d3103b7 [file] [log] [blame]
The Android Open Source Project7790ef52009-03-03 19:30:40 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "sqlite3_android"
18
19#include <ctype.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#include <unicode/ucol.h>
25#include <unicode/ustring.h>
26#include <cutils/log.h>
27
28#include "sqlite3_android.h"
29#include "PhoneNumberUtils.h"
The Android Open Source Project455ed292009-03-13 13:04:22 -070030#include "PhoneticStringUtils.h"
The Android Open Source Project7790ef52009-03-03 19:30:40 -080031
32#define ENABLE_ANDROID_LOG 0
33
34static int collate16(void *p, int n1, const void *v1, int n2, const void *v2)
35{
36 UCollator *coll = (UCollator *) p;
37 UCollationResult result = ucol_strcoll(coll, (const UChar *) v1, n1,
38 (const UChar *) v2, n2);
39
40 if (result == UCOL_LESS) {
41 return -1;
42 } else if (result == UCOL_GREATER) {
43 return 1;
44 } else {
45 return 0;
46 }
47}
48
49static int collate8(void *p, int n1, const void *v1, int n2, const void *v2)
50{
51 UCollator *coll = (UCollator *) p;
52 UCharIterator i1, i2;
53 UErrorCode status = U_ZERO_ERROR;
54
55 uiter_setUTF8(&i1, (const char *) v1, n1);
56 uiter_setUTF8(&i2, (const char *) v2, n2);
57
58 UCollationResult result = ucol_strcollIter(coll, &i1, &i2, &status);
59
60 if (U_FAILURE(status)) {
61// LOGE("Collation iterator error: %d\n", status);
62 }
63
64 if (result == UCOL_LESS) {
65 return -1;
66 } else if (result == UCOL_GREATER) {
67 return 1;
68 } else {
69 return 0;
70 }
71}
72
The Android Open Source Project455ed292009-03-13 13:04:22 -070073static void get_phonetically_sortable_string(
74 sqlite3_context * context, int argc, sqlite3_value ** argv)
75{
76 if (argc != 1) {
77 sqlite3_result_null(context);
78 return;
79 }
80 char const * src = (char const *)sqlite3_value_text(argv[0]);
81 char * ret;
82 size_t len;
83
84 if (!android::GetPhoneticallySortableString(src, &ret, &len)) {
Daisuke Miyakawa0c45e822009-03-27 19:41:52 -070085 // Put this text at the end of a list.
86 sqlite3_result_text(context, "\xF0\x9F\xBF\xBD", -1, SQLITE_STATIC);
87 // sqlite3_result_null(context);
The Android Open Source Project455ed292009-03-13 13:04:22 -070088 } else {
89 sqlite3_result_text(context, ret, len, free);
90 }
91}
92
Daisuke Miyakawad28cdc42009-05-18 14:51:52 +090093static void get_normalized_string(
94 sqlite3_context * context, int argc, sqlite3_value ** argv)
95{
96 if (argc != 1) {
97 sqlite3_result_null(context);
98 return;
99 }
100 char const * src = (char const *)sqlite3_value_text(argv[0]);
101 char * ret;
102 size_t len;
103
104 if (!android::GetNormalizedString(src, &ret, &len)) {
105 // Probably broken string. Return 0 length string.
106 sqlite3_result_text(context, "", -1, SQLITE_STATIC);
107 } else {
108 sqlite3_result_text(context, ret, len, free);
109 }
110}
111
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800112static void phone_numbers_equal(sqlite3_context * context, int argc, sqlite3_value ** argv)
113{
114 if (argc != 2) {
115 sqlite3_result_int(context, 0);
116 return;
117 }
118
119 char const * num1 = (char const *)sqlite3_value_text(argv[0]);
120 char const * num2 = (char const *)sqlite3_value_text(argv[1]);
121
122 if (num1 == NULL || num2 == NULL) {
123 sqlite3_result_null(context);
124 return;
125 }
126
127 bool equal = android::phone_number_compare(num1, num2);
128
129 if (equal) {
130 sqlite3_result_int(context, 1);
131 } else {
132 sqlite3_result_int(context, 0);
133 }
134}
135
136#if ENABLE_ANDROID_LOG
137static void android_log(sqlite3_context * context, int argc, sqlite3_value ** argv)
138{
139 char const * tag = "sqlite_trigger";
140 char const * msg = "";
141 int msgIndex = 0;
142
143 switch (argc) {
144 case 2:
145 tag = (char const *)sqlite3_value_text(argv[0]);
146 if (tag == NULL) {
147 tag = "sqlite_trigger";
148 }
149 msgIndex = 1;
150 case 1:
151 msg = (char const *)sqlite3_value_text(argv[msgIndex]);
152 if (msg == NULL) {
153 msg = "";
154 }
155 LOG(LOG_INFO, tag, msg);
156 sqlite3_result_int(context, 1);
157 return;
158
159 default:
160 sqlite3_result_int(context, 0);
161 return;
162 }
163}
164#endif
165
166static void delete_file(sqlite3_context * context, int argc, sqlite3_value ** argv)
167{
168 if (argc != 1) {
169 sqlite3_result_int(context, 0);
170 return;
171 }
172
173 char const * path = (char const *)sqlite3_value_text(argv[0]);
174 if (path == NULL) {
175 sqlite3_result_null(context);
176 return;
177 }
178
179 if (strncmp("/sdcard/", path, 8) != 0) {
180 sqlite3_result_null(context);
181 return;
182 }
Marco Nelissen2da78c02009-05-06 11:08:08 -0700183 if (strstr(path, "/../") != NULL) {
184 sqlite3_result_null(context);
185 return;
186 }
187
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800188 int err = unlink(path);
189 if (err != -1) {
190 // No error occured, return true
191 sqlite3_result_int(context, 1);
192 } else {
193 // An error occured, return false
194 sqlite3_result_int(context, 0);
195 }
196}
197
198static void tokenize_auxdata_delete(void * data)
199{
200 sqlite3_stmt * statement = (sqlite3_stmt *)data;
201 sqlite3_finalize(statement);
202}
203
204static void base16Encode(char* dest, const char* src, uint32_t size)
205{
206 static const char * BASE16_TABLE = "0123456789abcdef";
207 for (uint32_t i = 0; i < size; i++) {
208 char ch = *src++;
209 *dest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
210 *dest++ = BASE16_TABLE[ (ch & 0x0f) ];
211 }
212}
213
214struct SqliteUserData {
215 sqlite3 * handle;
216 UCollator* collator;
217};
218
219/**
220 * This function is invoked as:
221 *
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100222 * _TOKENIZE('<token_table>', <data_row_id>, <data>, <delimiter>,
223 * <use_token_index>, <data_tag>)
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800224 *
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100225 * If <use_token_index> is omitted, it is treated as 0.
226 * If <data_tag> is omitted, it is treated as NULL.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100227 *
228 * It will split <data> on each instance of <delimiter> and insert each token
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100229 * into <token_table>. The following columns in <token_table> are used:
230 * token TEXT, source INTEGER, token_index INTEGER, tag (any type)
231 * The token_index column is not required if <use_token_index> is 0.
232 * The tag column is not required if <data_tag> is NULL.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100233 *
234 * One row is inserted for each token in <data>.
235 * In each inserted row, 'source' is <data_row_id>.
236 * In the first inserted row, 'token' is the hex collation key of
237 * the entire <data> string, and 'token_index' is 0.
238 * In each row I (where 1 <= I < N, and N is the number of tokens in <data>)
239 * 'token' will be set to the hex collation key of the I:th token (0-based).
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100240 * If <use_token_index> != 0, 'token_index' is set to I.
241 * If <data_tag> is not NULL, 'tag' is set to <data_tag>.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100242 *
243 * In other words, there will be one row for the entire string,
244 * and one row for each token except the first one.
245 *
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800246 * The function returns the number of tokens generated.
247 */
248static void tokenize(sqlite3_context * context, int argc, sqlite3_value ** argv)
249{
250 //LOGD("enter tokenize");
251 int err;
Bjorn Bringert687f1162009-05-13 22:13:09 +0100252 int useTokenIndex = 0;
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100253 int useDataTag = 0;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800254
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100255 if (!(argc >= 4 || argc <= 6)) {
256 LOGE("Tokenize requires 4 to 6 arguments");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800257 sqlite3_result_null(context);
258 return;
259 }
260
Bjorn Bringert687f1162009-05-13 22:13:09 +0100261 if (argc > 4) {
262 useTokenIndex = sqlite3_value_int(argv[4]);
263 }
264
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100265 if (argc > 5) {
266 useDataTag = (sqlite3_value_type(argv[5]) != SQLITE_NULL);
267 }
268
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800269 sqlite3 * handle = sqlite3_context_db_handle(context);
270 UCollator* collator = (UCollator*)sqlite3_user_data(context);
271 char const * tokenTable = (char const *)sqlite3_value_text(argv[0]);
272 if (tokenTable == NULL) {
273 LOGE("tokenTable null");
274 sqlite3_result_null(context);
275 return;
276 }
277
278 // Get or create the prepared statement for the insertions
279 sqlite3_stmt * statement = (sqlite3_stmt *)sqlite3_get_auxdata(context, 0);
280 if (!statement) {
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100281 char const * tokenIndexCol = useTokenIndex ? ", token_index" : "";
282 char const * tokenIndexParam = useTokenIndex ? ", ?" : "";
283 char const * dataTagCol = useDataTag ? ", tag" : "";
284 char const * dataTagParam = useDataTag ? ", ?" : "";
285 char * sql = sqlite3_mprintf("INSERT INTO %s (token, source%s%s) VALUES (?, ?%s%s);",
286 tokenTable, tokenIndexCol, dataTagCol, tokenIndexParam, dataTagParam);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800287 err = sqlite3_prepare_v2(handle, sql, -1, &statement, NULL);
288 sqlite3_free(sql);
289 if (err) {
290 LOGE("prepare failed");
291 sqlite3_result_null(context);
292 return;
293 }
294 // This binds the statement to the table it was compiled against, which is argv[0].
295 // If this function is ever called with a different table the finalizer will be called
296 // and sqlite3_get_auxdata() will return null above, forcing a recompile for the new table.
297 sqlite3_set_auxdata(context, 0, statement, tokenize_auxdata_delete);
298 } else {
299 // Reset the cached statement so that binding the row ID will work properly
300 sqlite3_reset(statement);
301 }
302
303 // Bind the row ID of the source row
304 int64_t rowID = sqlite3_value_int64(argv[1]);
305 err = sqlite3_bind_int64(statement, 2, rowID);
306 if (err != SQLITE_OK) {
307 LOGE("bind failed");
308 sqlite3_result_null(context);
309 return;
310 }
311
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100312 // Bind <data_tag> to the tag column
313 if (useDataTag) {
314 int dataTagParamIndex = useTokenIndex ? 4 : 3;
315 err = sqlite3_bind_value(statement, dataTagParamIndex, argv[5]);
316 if (err != SQLITE_OK) {
317 LOGE("bind failed");
318 sqlite3_result_null(context);
319 return;
320 }
321 }
322
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800323 // Get the raw bytes for the string to tokenize
324 // the string will be modified by following code
325 // however, sqlite did not reuse the string, so it is safe to not dup it
326 UChar * origData = (UChar *)sqlite3_value_text16(argv[2]);
327 if (origData == NULL) {
328 sqlite3_result_null(context);
329 return;
330 }
331
332 // Get the raw bytes for the delimiter
333 const UChar * delim = (const UChar *)sqlite3_value_text16(argv[3]);
334 if (delim == NULL) {
335 LOGE("can't get delimiter");
336 sqlite3_result_null(context);
337 return;
338 }
339
340 UChar * token = NULL;
341 UChar *state;
342 int numTokens = 0;
343
344 do {
345 if (numTokens == 0) {
346 token = origData;
347 }
348
349 // Reset the program so we can use it to perform the insert
350 sqlite3_reset(statement);
351 UErrorCode status = U_ZERO_ERROR;
352 char keybuf[1024];
353 uint32_t result = ucol_getSortKey(collator, token, -1, (uint8_t*)keybuf, sizeof(keybuf)-1);
354 if (result > sizeof(keybuf)) {
355 // TODO allocate memory for this super big string
356 LOGE("ucol_getSortKey needs bigger buffer %d", result);
357 break;
358 }
359 uint32_t keysize = result-1;
360 uint32_t base16Size = keysize*2;
361 char *base16buf = (char*)malloc(base16Size);
362 base16Encode(base16buf, keybuf, keysize);
363 err = sqlite3_bind_text(statement, 1, base16buf, base16Size, SQLITE_STATIC);
364
365 if (err != SQLITE_OK) {
366 LOGE(" sqlite3_bind_text16 error %d", err);
367 free(base16buf);
368 break;
369 }
370
Bjorn Bringert687f1162009-05-13 22:13:09 +0100371 if (useTokenIndex) {
372 err = sqlite3_bind_int(statement, 3, numTokens);
373 if (err != SQLITE_OK) {
374 LOGE(" sqlite3_bind_int error %d", err);
375 free(base16buf);
376 break;
377 }
378 }
379
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800380 err = sqlite3_step(statement);
381 free(base16buf);
382
383 if (err != SQLITE_DONE) {
384 LOGE(" sqlite3_step error %d", err);
385 break;
386 }
387 numTokens++;
388 if (numTokens == 1) {
389 // first call
390 u_strtok_r(origData, delim, &state);
391 }
392 } while ((token = u_strtok_r(NULL, delim, &state)) != NULL);
393 sqlite3_result_int(context, numTokens);
394}
395
396static void localized_collator_dtor(UCollator* collator)
397{
398 ucol_close(collator);
399}
400
401#define LOCALIZED_COLLATOR_NAME "LOCALIZED"
402
403extern "C" int register_localized_collators(sqlite3* handle, const char* systemLocale, int utf16Storage)
404{
405 int err;
406 UErrorCode status = U_ZERO_ERROR;
407 void* icudata;
408
409 UCollator* collator = ucol_open(systemLocale, &status);
410 if (U_FAILURE(status)) {
411 return -1;
412 }
413
414 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
415 if (U_FAILURE(status)) {
416 return -1;
417 }
418
419 status = U_ZERO_ERROR;
420 char buf[1024];
421 int n = ucol_getShortDefinitionString(collator, NULL, buf, 1024, &status);
422
423 if (utf16Storage) {
424 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF16, collator,
425 collate16, (void(*)(void*))localized_collator_dtor);
426 } else {
427 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF8, collator,
428 collate8, (void(*)(void*))localized_collator_dtor);
429 }
430 if (err != SQLITE_OK) {
431 return err;
432 }
433
434 // Register the _TOKENIZE function
435 err = sqlite3_create_function(handle, "_TOKENIZE", 4, SQLITE_UTF16, collator, tokenize, NULL, NULL);
436 if (err != SQLITE_OK) {
437 return err;
Bjorn Bringert687f1162009-05-13 22:13:09 +0100438 }
439 err = sqlite3_create_function(handle, "_TOKENIZE", 5, SQLITE_UTF16, collator, tokenize, NULL, NULL);
440 if (err != SQLITE_OK) {
441 return err;
442 }
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100443 err = sqlite3_create_function(handle, "_TOKENIZE", 6, SQLITE_UTF16, collator, tokenize, NULL, NULL);
444 if (err != SQLITE_OK) {
445 return err;
446 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800447
448 return SQLITE_OK;
449}
450
451
452extern "C" int register_android_functions(sqlite3 * handle, int utf16Storage)
453{
454 int err;
455 UErrorCode status = U_ZERO_ERROR;
456
457 UCollator * collator = ucol_open(NULL, &status);
458 if (U_FAILURE(status)) {
459 return -1;
460 }
461
462 if (utf16Storage) {
463 // Note that text should be stored as UTF-16
464 err = sqlite3_exec(handle, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
465 if (err != SQLITE_OK) {
466 return err;
467 }
468
469 // Register the UNICODE collation
470 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF16, collator, collate16,
471 (void(*)(void*))localized_collator_dtor);
472 } else {
473 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF8, collator, collate8,
474 (void(*)(void*))localized_collator_dtor);
475 }
476
477 if (err != SQLITE_OK) {
478 return err;
479 }
480
481 // Register the PHONE_NUM_EQUALS function
482 err = sqlite3_create_function(handle, "PHONE_NUMBERS_EQUAL", 2, SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
483 if (err != SQLITE_OK) {
484 return err;
485 }
486
487 // Register the _DELETE_FILE function
488 err = sqlite3_create_function(handle, "_DELETE_FILE", 1, SQLITE_UTF8, NULL, delete_file, NULL, NULL);
489 if (err != SQLITE_OK) {
490 return err;
491 }
492
493#if ENABLE_ANDROID_LOG
494 // Register the _LOG function
495 err = sqlite3_create_function(handle, "_LOG", 1, SQLITE_UTF8, NULL, android_log, NULL, NULL);
496 if (err != SQLITE_OK) {
497 return err;
498 }
499#endif
500
The Android Open Source Project455ed292009-03-13 13:04:22 -0700501 // Register the GET_PHONETICALLY_SORTABLE_STRING function
502 err = sqlite3_create_function(handle,
503 "GET_PHONETICALLY_SORTABLE_STRING",
504 1, SQLITE_UTF8, NULL,
505 get_phonetically_sortable_string,
506 NULL, NULL);
507 if (err != SQLITE_OK) {
508 return err;
509 }
510
Daisuke Miyakawad28cdc42009-05-18 14:51:52 +0900511 // Register the GET_NORMALIZED_STRING function
512 err = sqlite3_create_function(handle,
513 "GET_NORMALIZED_STRING",
514 1, SQLITE_UTF8, NULL,
515 get_normalized_string,
516 NULL, NULL);
517 if (err != SQLITE_OK) {
518 return err;
519 }
520
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800521 return SQLITE_OK;
522}