blob: 3e11808cefe7b35d3748b044a39c40237d8e8e1c [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
The Android Open Source Project7790ef52009-03-03 19:30:40 -080093static void phone_numbers_equal(sqlite3_context * context, int argc, sqlite3_value ** argv)
94{
95 if (argc != 2) {
96 sqlite3_result_int(context, 0);
97 return;
98 }
99
100 char const * num1 = (char const *)sqlite3_value_text(argv[0]);
101 char const * num2 = (char const *)sqlite3_value_text(argv[1]);
102
103 if (num1 == NULL || num2 == NULL) {
104 sqlite3_result_null(context);
105 return;
106 }
107
108 bool equal = android::phone_number_compare(num1, num2);
109
110 if (equal) {
111 sqlite3_result_int(context, 1);
112 } else {
113 sqlite3_result_int(context, 0);
114 }
115}
116
117#if ENABLE_ANDROID_LOG
118static void android_log(sqlite3_context * context, int argc, sqlite3_value ** argv)
119{
120 char const * tag = "sqlite_trigger";
121 char const * msg = "";
122 int msgIndex = 0;
123
124 switch (argc) {
125 case 2:
126 tag = (char const *)sqlite3_value_text(argv[0]);
127 if (tag == NULL) {
128 tag = "sqlite_trigger";
129 }
130 msgIndex = 1;
131 case 1:
132 msg = (char const *)sqlite3_value_text(argv[msgIndex]);
133 if (msg == NULL) {
134 msg = "";
135 }
136 LOG(LOG_INFO, tag, msg);
137 sqlite3_result_int(context, 1);
138 return;
139
140 default:
141 sqlite3_result_int(context, 0);
142 return;
143 }
144}
145#endif
146
147static void delete_file(sqlite3_context * context, int argc, sqlite3_value ** argv)
148{
149 if (argc != 1) {
150 sqlite3_result_int(context, 0);
151 return;
152 }
153
154 char const * path = (char const *)sqlite3_value_text(argv[0]);
155 if (path == NULL) {
156 sqlite3_result_null(context);
157 return;
158 }
159
160 if (strncmp("/sdcard/", path, 8) != 0) {
161 sqlite3_result_null(context);
162 return;
163 }
Marco Nelissen2da78c02009-05-06 11:08:08 -0700164 if (strstr(path, "/../") != NULL) {
165 sqlite3_result_null(context);
166 return;
167 }
168
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800169 int err = unlink(path);
170 if (err != -1) {
171 // No error occured, return true
172 sqlite3_result_int(context, 1);
173 } else {
174 // An error occured, return false
175 sqlite3_result_int(context, 0);
176 }
177}
178
179static void tokenize_auxdata_delete(void * data)
180{
181 sqlite3_stmt * statement = (sqlite3_stmt *)data;
182 sqlite3_finalize(statement);
183}
184
185static void base16Encode(char* dest, const char* src, uint32_t size)
186{
187 static const char * BASE16_TABLE = "0123456789abcdef";
188 for (uint32_t i = 0; i < size; i++) {
189 char ch = *src++;
190 *dest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
191 *dest++ = BASE16_TABLE[ (ch & 0x0f) ];
192 }
193}
194
195struct SqliteUserData {
196 sqlite3 * handle;
197 UCollator* collator;
198};
199
200/**
201 * This function is invoked as:
202 *
203 * _TOKENIZE('<token_table>', <data_row_id>, <data>, <delimiter>)
204 *
205 * It will then split data on each instance of delimiter and insert each token
206 * into token_table's 'token' column with data_row_id in the 'source' column.
207 * The function returns the number of tokens generated.
208 */
209static void tokenize(sqlite3_context * context, int argc, sqlite3_value ** argv)
210{
211 //LOGD("enter tokenize");
212 int err;
213
214 if (argc != 4) {
215 LOGE("Tokenize requires 4 arguments");
216 sqlite3_result_null(context);
217 return;
218 }
219
220 sqlite3 * handle = sqlite3_context_db_handle(context);
221 UCollator* collator = (UCollator*)sqlite3_user_data(context);
222 char const * tokenTable = (char const *)sqlite3_value_text(argv[0]);
223 if (tokenTable == NULL) {
224 LOGE("tokenTable null");
225 sqlite3_result_null(context);
226 return;
227 }
228
229 // Get or create the prepared statement for the insertions
230 sqlite3_stmt * statement = (sqlite3_stmt *)sqlite3_get_auxdata(context, 0);
231 if (!statement) {
232 char * sql = sqlite3_mprintf("INSERT INTO %s (token, source) VALUES (?, ?);", tokenTable);
233 err = sqlite3_prepare_v2(handle, sql, -1, &statement, NULL);
234 sqlite3_free(sql);
235 if (err) {
236 LOGE("prepare failed");
237 sqlite3_result_null(context);
238 return;
239 }
240 // This binds the statement to the table it was compiled against, which is argv[0].
241 // If this function is ever called with a different table the finalizer will be called
242 // and sqlite3_get_auxdata() will return null above, forcing a recompile for the new table.
243 sqlite3_set_auxdata(context, 0, statement, tokenize_auxdata_delete);
244 } else {
245 // Reset the cached statement so that binding the row ID will work properly
246 sqlite3_reset(statement);
247 }
248
249 // Bind the row ID of the source row
250 int64_t rowID = sqlite3_value_int64(argv[1]);
251 err = sqlite3_bind_int64(statement, 2, rowID);
252 if (err != SQLITE_OK) {
253 LOGE("bind failed");
254 sqlite3_result_null(context);
255 return;
256 }
257
258 // Get the raw bytes for the string to tokenize
259 // the string will be modified by following code
260 // however, sqlite did not reuse the string, so it is safe to not dup it
261 UChar * origData = (UChar *)sqlite3_value_text16(argv[2]);
262 if (origData == NULL) {
263 sqlite3_result_null(context);
264 return;
265 }
266
267 // Get the raw bytes for the delimiter
268 const UChar * delim = (const UChar *)sqlite3_value_text16(argv[3]);
269 if (delim == NULL) {
270 LOGE("can't get delimiter");
271 sqlite3_result_null(context);
272 return;
273 }
274
275 UChar * token = NULL;
276 UChar *state;
277 int numTokens = 0;
278
279 do {
280 if (numTokens == 0) {
281 token = origData;
282 }
283
284 // Reset the program so we can use it to perform the insert
285 sqlite3_reset(statement);
286 UErrorCode status = U_ZERO_ERROR;
287 char keybuf[1024];
288 uint32_t result = ucol_getSortKey(collator, token, -1, (uint8_t*)keybuf, sizeof(keybuf)-1);
289 if (result > sizeof(keybuf)) {
290 // TODO allocate memory for this super big string
291 LOGE("ucol_getSortKey needs bigger buffer %d", result);
292 break;
293 }
294 uint32_t keysize = result-1;
295 uint32_t base16Size = keysize*2;
296 char *base16buf = (char*)malloc(base16Size);
297 base16Encode(base16buf, keybuf, keysize);
298 err = sqlite3_bind_text(statement, 1, base16buf, base16Size, SQLITE_STATIC);
299
300 if (err != SQLITE_OK) {
301 LOGE(" sqlite3_bind_text16 error %d", err);
302 free(base16buf);
303 break;
304 }
305
306 err = sqlite3_step(statement);
307 free(base16buf);
308
309 if (err != SQLITE_DONE) {
310 LOGE(" sqlite3_step error %d", err);
311 break;
312 }
313 numTokens++;
314 if (numTokens == 1) {
315 // first call
316 u_strtok_r(origData, delim, &state);
317 }
318 } while ((token = u_strtok_r(NULL, delim, &state)) != NULL);
319 sqlite3_result_int(context, numTokens);
320}
321
322static void localized_collator_dtor(UCollator* collator)
323{
324 ucol_close(collator);
325}
326
327#define LOCALIZED_COLLATOR_NAME "LOCALIZED"
328
329extern "C" int register_localized_collators(sqlite3* handle, const char* systemLocale, int utf16Storage)
330{
331 int err;
332 UErrorCode status = U_ZERO_ERROR;
333 void* icudata;
334
335 UCollator* collator = ucol_open(systemLocale, &status);
336 if (U_FAILURE(status)) {
337 return -1;
338 }
339
340 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
341 if (U_FAILURE(status)) {
342 return -1;
343 }
344
345 status = U_ZERO_ERROR;
346 char buf[1024];
347 int n = ucol_getShortDefinitionString(collator, NULL, buf, 1024, &status);
348
349 if (utf16Storage) {
350 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF16, collator,
351 collate16, (void(*)(void*))localized_collator_dtor);
352 } else {
353 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF8, collator,
354 collate8, (void(*)(void*))localized_collator_dtor);
355 }
356 if (err != SQLITE_OK) {
357 return err;
358 }
359
360 // Register the _TOKENIZE function
361 err = sqlite3_create_function(handle, "_TOKENIZE", 4, SQLITE_UTF16, collator, tokenize, NULL, NULL);
362 if (err != SQLITE_OK) {
363 return err;
364 }
365
366 return SQLITE_OK;
367}
368
369
370extern "C" int register_android_functions(sqlite3 * handle, int utf16Storage)
371{
372 int err;
373 UErrorCode status = U_ZERO_ERROR;
374
375 UCollator * collator = ucol_open(NULL, &status);
376 if (U_FAILURE(status)) {
377 return -1;
378 }
379
380 if (utf16Storage) {
381 // Note that text should be stored as UTF-16
382 err = sqlite3_exec(handle, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
383 if (err != SQLITE_OK) {
384 return err;
385 }
386
387 // Register the UNICODE collation
388 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF16, collator, collate16,
389 (void(*)(void*))localized_collator_dtor);
390 } else {
391 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF8, collator, collate8,
392 (void(*)(void*))localized_collator_dtor);
393 }
394
395 if (err != SQLITE_OK) {
396 return err;
397 }
398
399 // Register the PHONE_NUM_EQUALS function
400 err = sqlite3_create_function(handle, "PHONE_NUMBERS_EQUAL", 2, SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
401 if (err != SQLITE_OK) {
402 return err;
403 }
404
405 // Register the _DELETE_FILE function
406 err = sqlite3_create_function(handle, "_DELETE_FILE", 1, SQLITE_UTF8, NULL, delete_file, NULL, NULL);
407 if (err != SQLITE_OK) {
408 return err;
409 }
410
411#if ENABLE_ANDROID_LOG
412 // Register the _LOG function
413 err = sqlite3_create_function(handle, "_LOG", 1, SQLITE_UTF8, NULL, android_log, NULL, NULL);
414 if (err != SQLITE_OK) {
415 return err;
416 }
417#endif
418
The Android Open Source Project455ed292009-03-13 13:04:22 -0700419 // Register the GET_PHONETICALLY_SORTABLE_STRING function
420 err = sqlite3_create_function(handle,
421 "GET_PHONETICALLY_SORTABLE_STRING",
422 1, SQLITE_UTF8, NULL,
423 get_phonetically_sortable_string,
424 NULL, NULL);
425 if (err != SQLITE_OK) {
426 return err;
427 }
428
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800429 return SQLITE_OK;
430}