blob: da406476f27cb75381eaedda937f72c8383553ad [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
Jiyong Parkb4076df2017-08-09 20:15:42 +090024// ICU is turned off when sqlite is built for VNDK
25#ifndef __ANDROID_VNDK__
26#define SQLITE_ENABLE_ICU
27#else
28#undef SQLITE_ENABLE_ICU
29#endif
30
31#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -080032#include <unicode/ucol.h>
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080033#include <unicode/uiter.h>
The Android Open Source Project7790ef52009-03-03 19:30:40 -080034#include <unicode/ustring.h>
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080035#include <unicode/utypes.h>
Jiyong Parkb4076df2017-08-09 20:15:42 +090036#endif //SQLITE_ENABLE_ICU
Colin Crossea170612017-04-19 17:36:28 -070037#include <log/log.h>
The Android Open Source Project7790ef52009-03-03 19:30:40 -080038
39#include "sqlite3_android.h"
40#include "PhoneNumberUtils.h"
41
42#define ENABLE_ANDROID_LOG 0
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080043#define SMALL_BUFFER_SIZE 10
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -080044#define PHONE_NUMBER_BUFFER_SIZE 40
The Android Open Source Project7790ef52009-03-03 19:30:40 -080045
Jiyong Parkb4076df2017-08-09 20:15:42 +090046#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -080047static int collate16(void *p, int n1, const void *v1, int n2, const void *v2)
48{
49 UCollator *coll = (UCollator *) p;
50 UCollationResult result = ucol_strcoll(coll, (const UChar *) v1, n1,
51 (const UChar *) v2, n2);
52
53 if (result == UCOL_LESS) {
54 return -1;
55 } else if (result == UCOL_GREATER) {
56 return 1;
57 } else {
58 return 0;
59 }
60}
61
62static int collate8(void *p, int n1, const void *v1, int n2, const void *v2)
63{
64 UCollator *coll = (UCollator *) p;
65 UCharIterator i1, i2;
66 UErrorCode status = U_ZERO_ERROR;
67
68 uiter_setUTF8(&i1, (const char *) v1, n1);
69 uiter_setUTF8(&i2, (const char *) v2, n2);
70
71 UCollationResult result = ucol_strcollIter(coll, &i1, &i2, &status);
72
73 if (U_FAILURE(status)) {
Steve Blockcbfefda2012-01-06 19:14:58 +000074// ALOGE("Collation iterator error: %d\n", status);
The Android Open Source Project7790ef52009-03-03 19:30:40 -080075 }
76
77 if (result == UCOL_LESS) {
78 return -1;
79 } else if (result == UCOL_GREATER) {
80 return 1;
81 } else {
82 return 0;
83 }
84}
Jiyong Parkb4076df2017-08-09 20:15:42 +090085#endif // SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -080086
87static void phone_numbers_equal(sqlite3_context * context, int argc, sqlite3_value ** argv)
88{
Daisuke Miyakawa948a1192009-09-19 19:19:53 -070089 if (argc != 2 && argc != 3) {
The Android Open Source Project7790ef52009-03-03 19:30:40 -080090 sqlite3_result_int(context, 0);
91 return;
92 }
93
94 char const * num1 = (char const *)sqlite3_value_text(argv[0]);
95 char const * num2 = (char const *)sqlite3_value_text(argv[1]);
96
Daisuke Miyakawa948a1192009-09-19 19:19:53 -070097 bool use_strict = false;
98 if (argc == 3) {
99 use_strict = (sqlite3_value_int(argv[2]) != 0);
100 }
101
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800102 if (num1 == NULL || num2 == NULL) {
103 sqlite3_result_null(context);
104 return;
105 }
106
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700107 bool equal =
108 (use_strict ?
109 android::phone_number_compare_strict(num1, num2) :
110 android::phone_number_compare_loose(num1, num2));
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800111
112 if (equal) {
113 sqlite3_result_int(context, 1);
114 } else {
115 sqlite3_result_int(context, 0);
116 }
117}
118
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800119static void phone_number_stripped_reversed(sqlite3_context * context, int argc,
120 sqlite3_value ** argv)
121{
122 if (argc != 1) {
123 sqlite3_result_int(context, 0);
124 return;
125 }
126
127 char const * number = (char const *)sqlite3_value_text(argv[0]);
128 if (number == NULL) {
129 sqlite3_result_null(context);
130 return;
131 }
132
133 char out[PHONE_NUMBER_BUFFER_SIZE];
134 int outlen = 0;
135 android::phone_number_stripped_reversed_inter(number, out, PHONE_NUMBER_BUFFER_SIZE, &outlen);
136 sqlite3_result_text(context, (const char*)out, outlen, SQLITE_TRANSIENT);
137}
138
139
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800140#if ENABLE_ANDROID_LOG
141static void android_log(sqlite3_context * context, int argc, sqlite3_value ** argv)
142{
143 char const * tag = "sqlite_trigger";
144 char const * msg = "";
145 int msgIndex = 0;
146
147 switch (argc) {
148 case 2:
149 tag = (char const *)sqlite3_value_text(argv[0]);
150 if (tag == NULL) {
151 tag = "sqlite_trigger";
152 }
153 msgIndex = 1;
154 case 1:
155 msg = (char const *)sqlite3_value_text(argv[msgIndex]);
156 if (msg == NULL) {
157 msg = "";
158 }
Hyejin Kim165c6872013-02-28 16:21:20 +0900159 ALOG(LOG_INFO, tag, "%s", msg);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800160 sqlite3_result_int(context, 1);
161 return;
162
163 default:
164 sqlite3_result_int(context, 0);
165 return;
166 }
167}
168#endif
169
170static void delete_file(sqlite3_context * context, int argc, sqlite3_value ** argv)
171{
172 if (argc != 1) {
173 sqlite3_result_int(context, 0);
174 return;
175 }
176
177 char const * path = (char const *)sqlite3_value_text(argv[0]);
Mike Lockwood5a345992011-07-07 12:22:34 -0400178 // Don't allow ".." in paths
179 if (path == NULL || strstr(path, "/../") != NULL) {
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800180 sqlite3_result_null(context);
181 return;
182 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800183
Mike Lockwood5a345992011-07-07 12:22:34 -0400184 // We only allow deleting files in the EXTERNAL_STORAGE path, or one of the
185 // SECONDARY_STORAGE paths
186 bool good_path = false;
187 char const * external_storage = getenv("EXTERNAL_STORAGE");
188 if (external_storage && strncmp(external_storage, path, strlen(external_storage)) == 0) {
189 good_path = true;
190 } else {
191 // check SECONDARY_STORAGE, which should be a colon separated list of paths
192 char const * secondary_paths = getenv("SECONDARY_STORAGE");
193 while (secondary_paths && secondary_paths[0]) {
194 const char* colon = strchr(secondary_paths, ':');
195 int length = (colon ? colon - secondary_paths : strlen(secondary_paths));
196 if (strncmp(secondary_paths, path, length) == 0) {
197 good_path = true;
198 }
199 secondary_paths += length;
200 while (*secondary_paths == ':') secondary_paths++;
201 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800202 }
Mike Lockwood5a345992011-07-07 12:22:34 -0400203
204 if (!good_path) {
Marco Nelissen2da78c02009-05-06 11:08:08 -0700205 sqlite3_result_null(context);
206 return;
207 }
208
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800209 int err = unlink(path);
210 if (err != -1) {
211 // No error occured, return true
212 sqlite3_result_int(context, 1);
213 } else {
214 // An error occured, return false
215 sqlite3_result_int(context, 0);
216 }
217}
218
Jiyong Parkb4076df2017-08-09 20:15:42 +0900219#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800220static void tokenize_auxdata_delete(void * data)
221{
222 sqlite3_stmt * statement = (sqlite3_stmt *)data;
223 sqlite3_finalize(statement);
224}
225
226static void base16Encode(char* dest, const char* src, uint32_t size)
227{
228 static const char * BASE16_TABLE = "0123456789abcdef";
229 for (uint32_t i = 0; i < size; i++) {
230 char ch = *src++;
231 *dest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
232 *dest++ = BASE16_TABLE[ (ch & 0x0f) ];
233 }
234}
235
236struct SqliteUserData {
237 sqlite3 * handle;
238 UCollator* collator;
239};
240
241/**
242 * This function is invoked as:
243 *
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100244 * _TOKENIZE('<token_table>', <data_row_id>, <data>, <delimiter>,
245 * <use_token_index>, <data_tag>)
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800246 *
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100247 * If <use_token_index> is omitted, it is treated as 0.
248 * If <data_tag> is omitted, it is treated as NULL.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100249 *
250 * It will split <data> on each instance of <delimiter> and insert each token
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100251 * into <token_table>. The following columns in <token_table> are used:
252 * token TEXT, source INTEGER, token_index INTEGER, tag (any type)
253 * The token_index column is not required if <use_token_index> is 0.
254 * The tag column is not required if <data_tag> is NULL.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100255 *
256 * One row is inserted for each token in <data>.
257 * In each inserted row, 'source' is <data_row_id>.
258 * In the first inserted row, 'token' is the hex collation key of
259 * the entire <data> string, and 'token_index' is 0.
260 * In each row I (where 1 <= I < N, and N is the number of tokens in <data>)
261 * 'token' will be set to the hex collation key of the I:th token (0-based).
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100262 * If <use_token_index> != 0, 'token_index' is set to I.
263 * If <data_tag> is not NULL, 'tag' is set to <data_tag>.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100264 *
265 * In other words, there will be one row for the entire string,
266 * and one row for each token except the first one.
267 *
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800268 * The function returns the number of tokens generated.
269 */
270static void tokenize(sqlite3_context * context, int argc, sqlite3_value ** argv)
271{
Steve Blockbcf3ccc2011-12-20 16:21:48 +0000272 //ALOGD("enter tokenize");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800273 int err;
Bjorn Bringert687f1162009-05-13 22:13:09 +0100274 int useTokenIndex = 0;
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100275 int useDataTag = 0;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800276
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100277 if (!(argc >= 4 || argc <= 6)) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000278 ALOGE("Tokenize requires 4 to 6 arguments");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800279 sqlite3_result_null(context);
280 return;
281 }
282
Bjorn Bringert687f1162009-05-13 22:13:09 +0100283 if (argc > 4) {
284 useTokenIndex = sqlite3_value_int(argv[4]);
285 }
286
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100287 if (argc > 5) {
288 useDataTag = (sqlite3_value_type(argv[5]) != SQLITE_NULL);
289 }
290
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800291 sqlite3 * handle = sqlite3_context_db_handle(context);
292 UCollator* collator = (UCollator*)sqlite3_user_data(context);
293 char const * tokenTable = (char const *)sqlite3_value_text(argv[0]);
294 if (tokenTable == NULL) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000295 ALOGE("tokenTable null");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800296 sqlite3_result_null(context);
297 return;
298 }
299
300 // Get or create the prepared statement for the insertions
301 sqlite3_stmt * statement = (sqlite3_stmt *)sqlite3_get_auxdata(context, 0);
302 if (!statement) {
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100303 char const * tokenIndexCol = useTokenIndex ? ", token_index" : "";
304 char const * tokenIndexParam = useTokenIndex ? ", ?" : "";
305 char const * dataTagCol = useDataTag ? ", tag" : "";
306 char const * dataTagParam = useDataTag ? ", ?" : "";
307 char * sql = sqlite3_mprintf("INSERT INTO %s (token, source%s%s) VALUES (?, ?%s%s);",
308 tokenTable, tokenIndexCol, dataTagCol, tokenIndexParam, dataTagParam);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800309 err = sqlite3_prepare_v2(handle, sql, -1, &statement, NULL);
310 sqlite3_free(sql);
311 if (err) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000312 ALOGE("prepare failed");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800313 sqlite3_result_null(context);
314 return;
315 }
316 // This binds the statement to the table it was compiled against, which is argv[0].
317 // If this function is ever called with a different table the finalizer will be called
318 // and sqlite3_get_auxdata() will return null above, forcing a recompile for the new table.
319 sqlite3_set_auxdata(context, 0, statement, tokenize_auxdata_delete);
320 } else {
321 // Reset the cached statement so that binding the row ID will work properly
322 sqlite3_reset(statement);
323 }
324
325 // Bind the row ID of the source row
326 int64_t rowID = sqlite3_value_int64(argv[1]);
327 err = sqlite3_bind_int64(statement, 2, rowID);
328 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000329 ALOGE("bind failed");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800330 sqlite3_result_null(context);
331 return;
332 }
333
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100334 // Bind <data_tag> to the tag column
335 if (useDataTag) {
336 int dataTagParamIndex = useTokenIndex ? 4 : 3;
337 err = sqlite3_bind_value(statement, dataTagParamIndex, argv[5]);
338 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000339 ALOGE("bind failed");
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100340 sqlite3_result_null(context);
341 return;
342 }
343 }
344
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800345 // Get the raw bytes for the string to tokenize
346 // the string will be modified by following code
347 // however, sqlite did not reuse the string, so it is safe to not dup it
348 UChar * origData = (UChar *)sqlite3_value_text16(argv[2]);
349 if (origData == NULL) {
350 sqlite3_result_null(context);
351 return;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800352 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800353
354 // Get the raw bytes for the delimiter
355 const UChar * delim = (const UChar *)sqlite3_value_text16(argv[3]);
356 if (delim == NULL) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000357 ALOGE("can't get delimiter");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800358 sqlite3_result_null(context);
359 return;
360 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800361
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800362 UChar * token = NULL;
363 UChar *state;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800364 int numTokens = 0;
365
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800366 do {
367 if (numTokens == 0) {
368 token = origData;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800369 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800370
371 // Reset the program so we can use it to perform the insert
372 sqlite3_reset(statement);
373 UErrorCode status = U_ZERO_ERROR;
374 char keybuf[1024];
375 uint32_t result = ucol_getSortKey(collator, token, -1, (uint8_t*)keybuf, sizeof(keybuf)-1);
376 if (result > sizeof(keybuf)) {
377 // TODO allocate memory for this super big string
Steve Blockcbfefda2012-01-06 19:14:58 +0000378 ALOGE("ucol_getSortKey needs bigger buffer %d", result);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800379 break;
380 }
381 uint32_t keysize = result-1;
382 uint32_t base16Size = keysize*2;
383 char *base16buf = (char*)malloc(base16Size);
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800384 base16Encode(base16buf, keybuf, keysize);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800385 err = sqlite3_bind_text(statement, 1, base16buf, base16Size, SQLITE_STATIC);
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800386
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800387 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000388 ALOGE(" sqlite3_bind_text16 error %d", err);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800389 free(base16buf);
390 break;
391 }
392
Bjorn Bringert687f1162009-05-13 22:13:09 +0100393 if (useTokenIndex) {
394 err = sqlite3_bind_int(statement, 3, numTokens);
395 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000396 ALOGE(" sqlite3_bind_int error %d", err);
Bjorn Bringert687f1162009-05-13 22:13:09 +0100397 free(base16buf);
398 break;
399 }
400 }
401
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800402 err = sqlite3_step(statement);
403 free(base16buf);
404
405 if (err != SQLITE_DONE) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000406 ALOGE(" sqlite3_step error %d", err);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800407 break;
408 }
409 numTokens++;
410 if (numTokens == 1) {
411 // first call
412 u_strtok_r(origData, delim, &state);
413 }
414 } while ((token = u_strtok_r(NULL, delim, &state)) != NULL);
415 sqlite3_result_int(context, numTokens);
416}
417
418static void localized_collator_dtor(UCollator* collator)
419{
420 ucol_close(collator);
421}
422
423#define LOCALIZED_COLLATOR_NAME "LOCALIZED"
424
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900425// This collator may be removed in the near future, so you MUST not use now.
426#define PHONEBOOK_COLLATOR_NAME "PHONEBOOK"
427
Jiyong Parkb4076df2017-08-09 20:15:42 +0900428#endif // SQLITE_ENABLE_ICU
429
430extern "C" int register_localized_collators(sqlite3* handle __attribute((unused)),
431 const char* systemLocale __attribute((unused)),
432 int utf16Storage __attribute((unused)))
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800433{
Jiyong Parkb4076df2017-08-09 20:15:42 +0900434// This function is no-op for the VNDK, but should exist in case when some vendor
435// module has a reference to this function.
436#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800437 UErrorCode status = U_ZERO_ERROR;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800438 UCollator* collator = ucol_open(systemLocale, &status);
439 if (U_FAILURE(status)) {
440 return -1;
441 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800442
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800443 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
444 if (U_FAILURE(status)) {
445 return -1;
446 }
447
Elliott Hughes153c1022016-09-13 17:13:29 -0700448 int err;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800449 if (utf16Storage) {
450 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF16, collator,
451 collate16, (void(*)(void*))localized_collator_dtor);
452 } else {
453 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF8, collator,
454 collate8, (void(*)(void*))localized_collator_dtor);
455 }
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900456
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800457 if (err != SQLITE_OK) {
458 return err;
459 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800460
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800461 // Register the _TOKENIZE function
462 err = sqlite3_create_function(handle, "_TOKENIZE", 4, SQLITE_UTF16, collator, tokenize, NULL, NULL);
463 if (err != SQLITE_OK) {
464 return err;
Bjorn Bringert687f1162009-05-13 22:13:09 +0100465 }
466 err = sqlite3_create_function(handle, "_TOKENIZE", 5, SQLITE_UTF16, collator, tokenize, NULL, NULL);
467 if (err != SQLITE_OK) {
468 return err;
469 }
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100470 err = sqlite3_create_function(handle, "_TOKENIZE", 6, SQLITE_UTF16, collator, tokenize, NULL, NULL);
471 if (err != SQLITE_OK) {
472 return err;
473 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800474
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900475 //// PHONEBOOK_COLLATOR
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900476 status = U_ZERO_ERROR;
Jay Shraunerdb8a3862012-12-17 11:12:30 -0800477 collator = ucol_open(systemLocale, &status);
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900478 if (U_FAILURE(status)) {
479 return -1;
480 }
481
482 status = U_ZERO_ERROR;
483 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
484 if (U_FAILURE(status)) {
485 return -1;
486 }
487
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900488 if (utf16Storage) {
489 err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF16, collator,
490 collate16, (void(*)(void*))localized_collator_dtor);
491 } else {
492 err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF8, collator,
493 collate8, (void(*)(void*))localized_collator_dtor);
494 }
495
496 if (err != SQLITE_OK) {
497 return err;
498 }
499 //// PHONEBOOK_COLLATOR
Jiyong Parkb4076df2017-08-09 20:15:42 +0900500#endif //SQLITE_ENABLE_ICU
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900501
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800502 return SQLITE_OK;
503}
504
505
Jiyong Parkb4076df2017-08-09 20:15:42 +0900506extern "C" int register_android_functions(sqlite3 * handle, int utf16Storage __attribute((unused)))
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800507{
508 int err;
Jiyong Parkb4076df2017-08-09 20:15:42 +0900509#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800510 UErrorCode status = U_ZERO_ERROR;
511
512 UCollator * collator = ucol_open(NULL, &status);
513 if (U_FAILURE(status)) {
514 return -1;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800515 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800516
517 if (utf16Storage) {
518 // Note that text should be stored as UTF-16
519 err = sqlite3_exec(handle, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
520 if (err != SQLITE_OK) {
521 return err;
522 }
523
524 // Register the UNICODE collation
525 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF16, collator, collate16,
526 (void(*)(void*))localized_collator_dtor);
527 } else {
528 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF8, collator, collate8,
529 (void(*)(void*))localized_collator_dtor);
530 }
531
532 if (err != SQLITE_OK) {
533 return err;
534 }
Jiyong Parkb4076df2017-08-09 20:15:42 +0900535#endif // SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800536
537 // Register the PHONE_NUM_EQUALS function
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700538 err = sqlite3_create_function(
539 handle, "PHONE_NUMBERS_EQUAL", 2,
540 SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800541 if (err != SQLITE_OK) {
542 return err;
543 }
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700544
545 // Register the PHONE_NUM_EQUALS function with an additional argument "use_strict"
546 err = sqlite3_create_function(
547 handle, "PHONE_NUMBERS_EQUAL", 3,
548 SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
549 if (err != SQLITE_OK) {
550 return err;
551 }
552
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800553 // Register the _DELETE_FILE function
554 err = sqlite3_create_function(handle, "_DELETE_FILE", 1, SQLITE_UTF8, NULL, delete_file, NULL, NULL);
555 if (err != SQLITE_OK) {
556 return err;
557 }
558
559#if ENABLE_ANDROID_LOG
560 // Register the _LOG function
561 err = sqlite3_create_function(handle, "_LOG", 1, SQLITE_UTF8, NULL, android_log, NULL, NULL);
562 if (err != SQLITE_OK) {
563 return err;
564 }
565#endif
566
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800567 // Register the _PHONE_NUMBER_STRIPPED_REVERSED function, which imitates
Elliott Hughes153c1022016-09-13 17:13:29 -0700568 // PhoneNumberUtils.getStrippedReversed. This function is used by
569 // packages/providers/ContactsProvider/src/com/android/providers/contacts/LegacyApiSupport.java
570 // to provide compatibility with Android 1.6 and earlier.
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800571 err = sqlite3_create_function(handle,
572 "_PHONE_NUMBER_STRIPPED_REVERSED",
573 1, SQLITE_UTF8, NULL,
574 phone_number_stripped_reversed,
575 NULL, NULL);
576 if (err != SQLITE_OK) {
577 return err;
578 }
579
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800580 return SQLITE_OK;
581}