blob: 659ee6c9b4adfede0d5045a7b1bf140ed55e9492 [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#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -080025#include <unicode/ucol.h>
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080026#include <unicode/uiter.h>
The Android Open Source Project7790ef52009-03-03 19:30:40 -080027#include <unicode/ustring.h>
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080028#include <unicode/utypes.h>
Jiyong Parkb4076df2017-08-09 20:15:42 +090029#endif //SQLITE_ENABLE_ICU
Colin Crossea170612017-04-19 17:36:28 -070030#include <log/log.h>
The Android Open Source Project7790ef52009-03-03 19:30:40 -080031
32#include "sqlite3_android.h"
33#include "PhoneNumberUtils.h"
34
35#define ENABLE_ANDROID_LOG 0
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080036#define SMALL_BUFFER_SIZE 10
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -080037#define PHONE_NUMBER_BUFFER_SIZE 40
The Android Open Source Project7790ef52009-03-03 19:30:40 -080038
Jiyong Parkb4076df2017-08-09 20:15:42 +090039#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -080040static int collate16(void *p, int n1, const void *v1, int n2, const void *v2)
41{
42 UCollator *coll = (UCollator *) p;
43 UCollationResult result = ucol_strcoll(coll, (const UChar *) v1, n1,
44 (const UChar *) v2, n2);
45
46 if (result == UCOL_LESS) {
47 return -1;
48 } else if (result == UCOL_GREATER) {
49 return 1;
50 } else {
51 return 0;
52 }
53}
54
55static int collate8(void *p, int n1, const void *v1, int n2, const void *v2)
56{
57 UCollator *coll = (UCollator *) p;
58 UCharIterator i1, i2;
59 UErrorCode status = U_ZERO_ERROR;
60
61 uiter_setUTF8(&i1, (const char *) v1, n1);
62 uiter_setUTF8(&i2, (const char *) v2, n2);
63
64 UCollationResult result = ucol_strcollIter(coll, &i1, &i2, &status);
65
66 if (U_FAILURE(status)) {
Steve Blockcbfefda2012-01-06 19:14:58 +000067// ALOGE("Collation iterator error: %d\n", status);
The Android Open Source Project7790ef52009-03-03 19:30:40 -080068 }
69
70 if (result == UCOL_LESS) {
71 return -1;
72 } else if (result == UCOL_GREATER) {
73 return 1;
74 } else {
75 return 0;
76 }
77}
Jiyong Parkb4076df2017-08-09 20:15:42 +090078#endif // SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -080079
80static void phone_numbers_equal(sqlite3_context * context, int argc, sqlite3_value ** argv)
81{
Daisuke Miyakawa948a1192009-09-19 19:19:53 -070082 if (argc != 2 && argc != 3) {
The Android Open Source Project7790ef52009-03-03 19:30:40 -080083 sqlite3_result_int(context, 0);
84 return;
85 }
86
87 char const * num1 = (char const *)sqlite3_value_text(argv[0]);
88 char const * num2 = (char const *)sqlite3_value_text(argv[1]);
89
Daisuke Miyakawa948a1192009-09-19 19:19:53 -070090 bool use_strict = false;
91 if (argc == 3) {
92 use_strict = (sqlite3_value_int(argv[2]) != 0);
93 }
94
The Android Open Source Project7790ef52009-03-03 19:30:40 -080095 if (num1 == NULL || num2 == NULL) {
96 sqlite3_result_null(context);
97 return;
98 }
99
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700100 bool equal =
101 (use_strict ?
102 android::phone_number_compare_strict(num1, num2) :
103 android::phone_number_compare_loose(num1, num2));
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800104
105 if (equal) {
106 sqlite3_result_int(context, 1);
107 } else {
108 sqlite3_result_int(context, 0);
109 }
110}
111
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800112static void phone_number_stripped_reversed(sqlite3_context * context, int argc,
113 sqlite3_value ** argv)
114{
115 if (argc != 1) {
116 sqlite3_result_int(context, 0);
117 return;
118 }
119
120 char const * number = (char const *)sqlite3_value_text(argv[0]);
121 if (number == NULL) {
122 sqlite3_result_null(context);
123 return;
124 }
125
126 char out[PHONE_NUMBER_BUFFER_SIZE];
127 int outlen = 0;
128 android::phone_number_stripped_reversed_inter(number, out, PHONE_NUMBER_BUFFER_SIZE, &outlen);
129 sqlite3_result_text(context, (const char*)out, outlen, SQLITE_TRANSIENT);
130}
131
132
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800133#if ENABLE_ANDROID_LOG
134static void android_log(sqlite3_context * context, int argc, sqlite3_value ** argv)
135{
136 char const * tag = "sqlite_trigger";
137 char const * msg = "";
138 int msgIndex = 0;
139
140 switch (argc) {
141 case 2:
142 tag = (char const *)sqlite3_value_text(argv[0]);
143 if (tag == NULL) {
144 tag = "sqlite_trigger";
145 }
146 msgIndex = 1;
147 case 1:
148 msg = (char const *)sqlite3_value_text(argv[msgIndex]);
149 if (msg == NULL) {
150 msg = "";
151 }
Hyejin Kim165c6872013-02-28 16:21:20 +0900152 ALOG(LOG_INFO, tag, "%s", msg);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800153 sqlite3_result_int(context, 1);
154 return;
155
156 default:
157 sqlite3_result_int(context, 0);
158 return;
159 }
160}
161#endif
162
163static void delete_file(sqlite3_context * context, int argc, sqlite3_value ** argv)
164{
165 if (argc != 1) {
166 sqlite3_result_int(context, 0);
167 return;
168 }
169
170 char const * path = (char const *)sqlite3_value_text(argv[0]);
Mike Lockwood5a345992011-07-07 12:22:34 -0400171 // Don't allow ".." in paths
172 if (path == NULL || strstr(path, "/../") != NULL) {
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800173 sqlite3_result_null(context);
174 return;
175 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800176
Mike Lockwood5a345992011-07-07 12:22:34 -0400177 // We only allow deleting files in the EXTERNAL_STORAGE path, or one of the
178 // SECONDARY_STORAGE paths
179 bool good_path = false;
180 char const * external_storage = getenv("EXTERNAL_STORAGE");
181 if (external_storage && strncmp(external_storage, path, strlen(external_storage)) == 0) {
182 good_path = true;
183 } else {
184 // check SECONDARY_STORAGE, which should be a colon separated list of paths
185 char const * secondary_paths = getenv("SECONDARY_STORAGE");
186 while (secondary_paths && secondary_paths[0]) {
187 const char* colon = strchr(secondary_paths, ':');
188 int length = (colon ? colon - secondary_paths : strlen(secondary_paths));
189 if (strncmp(secondary_paths, path, length) == 0) {
190 good_path = true;
191 }
192 secondary_paths += length;
193 while (*secondary_paths == ':') secondary_paths++;
194 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800195 }
Mike Lockwood5a345992011-07-07 12:22:34 -0400196
197 if (!good_path) {
Marco Nelissen2da78c02009-05-06 11:08:08 -0700198 sqlite3_result_null(context);
199 return;
200 }
201
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800202 int err = unlink(path);
203 if (err != -1) {
204 // No error occured, return true
205 sqlite3_result_int(context, 1);
206 } else {
207 // An error occured, return false
208 sqlite3_result_int(context, 0);
209 }
210}
211
Jiyong Parkb4076df2017-08-09 20:15:42 +0900212#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800213static void tokenize_auxdata_delete(void * data)
214{
215 sqlite3_stmt * statement = (sqlite3_stmt *)data;
216 sqlite3_finalize(statement);
217}
218
219static void base16Encode(char* dest, const char* src, uint32_t size)
220{
221 static const char * BASE16_TABLE = "0123456789abcdef";
222 for (uint32_t i = 0; i < size; i++) {
223 char ch = *src++;
224 *dest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
225 *dest++ = BASE16_TABLE[ (ch & 0x0f) ];
226 }
227}
228
229struct SqliteUserData {
230 sqlite3 * handle;
231 UCollator* collator;
232};
233
234/**
235 * This function is invoked as:
236 *
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100237 * _TOKENIZE('<token_table>', <data_row_id>, <data>, <delimiter>,
238 * <use_token_index>, <data_tag>)
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800239 *
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100240 * If <use_token_index> is omitted, it is treated as 0.
241 * If <data_tag> is omitted, it is treated as NULL.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100242 *
243 * It will split <data> on each instance of <delimiter> and insert each token
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100244 * into <token_table>. The following columns in <token_table> are used:
245 * token TEXT, source INTEGER, token_index INTEGER, tag (any type)
246 * The token_index column is not required if <use_token_index> is 0.
247 * The tag column is not required if <data_tag> is NULL.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100248 *
249 * One row is inserted for each token in <data>.
250 * In each inserted row, 'source' is <data_row_id>.
251 * In the first inserted row, 'token' is the hex collation key of
252 * the entire <data> string, and 'token_index' is 0.
253 * In each row I (where 1 <= I < N, and N is the number of tokens in <data>)
254 * 'token' will be set to the hex collation key of the I:th token (0-based).
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100255 * If <use_token_index> != 0, 'token_index' is set to I.
256 * If <data_tag> is not NULL, 'tag' is set to <data_tag>.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100257 *
258 * In other words, there will be one row for the entire string,
259 * and one row for each token except the first one.
260 *
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800261 * The function returns the number of tokens generated.
262 */
263static void tokenize(sqlite3_context * context, int argc, sqlite3_value ** argv)
264{
Steve Blockbcf3ccc2011-12-20 16:21:48 +0000265 //ALOGD("enter tokenize");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800266 int err;
Bjorn Bringert687f1162009-05-13 22:13:09 +0100267 int useTokenIndex = 0;
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100268 int useDataTag = 0;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800269
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100270 if (!(argc >= 4 || argc <= 6)) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000271 ALOGE("Tokenize requires 4 to 6 arguments");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800272 sqlite3_result_null(context);
273 return;
274 }
275
Bjorn Bringert687f1162009-05-13 22:13:09 +0100276 if (argc > 4) {
277 useTokenIndex = sqlite3_value_int(argv[4]);
278 }
279
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100280 if (argc > 5) {
281 useDataTag = (sqlite3_value_type(argv[5]) != SQLITE_NULL);
282 }
283
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800284 sqlite3 * handle = sqlite3_context_db_handle(context);
285 UCollator* collator = (UCollator*)sqlite3_user_data(context);
286 char const * tokenTable = (char const *)sqlite3_value_text(argv[0]);
287 if (tokenTable == NULL) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000288 ALOGE("tokenTable null");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800289 sqlite3_result_null(context);
290 return;
291 }
292
293 // Get or create the prepared statement for the insertions
294 sqlite3_stmt * statement = (sqlite3_stmt *)sqlite3_get_auxdata(context, 0);
295 if (!statement) {
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100296 char const * tokenIndexCol = useTokenIndex ? ", token_index" : "";
297 char const * tokenIndexParam = useTokenIndex ? ", ?" : "";
298 char const * dataTagCol = useDataTag ? ", tag" : "";
299 char const * dataTagParam = useDataTag ? ", ?" : "";
300 char * sql = sqlite3_mprintf("INSERT INTO %s (token, source%s%s) VALUES (?, ?%s%s);",
301 tokenTable, tokenIndexCol, dataTagCol, tokenIndexParam, dataTagParam);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800302 err = sqlite3_prepare_v2(handle, sql, -1, &statement, NULL);
303 sqlite3_free(sql);
304 if (err) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000305 ALOGE("prepare failed");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800306 sqlite3_result_null(context);
307 return;
308 }
309 // This binds the statement to the table it was compiled against, which is argv[0].
310 // If this function is ever called with a different table the finalizer will be called
311 // and sqlite3_get_auxdata() will return null above, forcing a recompile for the new table.
312 sqlite3_set_auxdata(context, 0, statement, tokenize_auxdata_delete);
313 } else {
314 // Reset the cached statement so that binding the row ID will work properly
315 sqlite3_reset(statement);
316 }
317
318 // Bind the row ID of the source row
319 int64_t rowID = sqlite3_value_int64(argv[1]);
320 err = sqlite3_bind_int64(statement, 2, rowID);
321 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000322 ALOGE("bind failed");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800323 sqlite3_result_null(context);
324 return;
325 }
326
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100327 // Bind <data_tag> to the tag column
328 if (useDataTag) {
329 int dataTagParamIndex = useTokenIndex ? 4 : 3;
330 err = sqlite3_bind_value(statement, dataTagParamIndex, argv[5]);
331 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000332 ALOGE("bind failed");
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100333 sqlite3_result_null(context);
334 return;
335 }
336 }
337
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800338 // Get the raw bytes for the string to tokenize
339 // the string will be modified by following code
340 // however, sqlite did not reuse the string, so it is safe to not dup it
341 UChar * origData = (UChar *)sqlite3_value_text16(argv[2]);
342 if (origData == NULL) {
343 sqlite3_result_null(context);
344 return;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800345 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800346
347 // Get the raw bytes for the delimiter
348 const UChar * delim = (const UChar *)sqlite3_value_text16(argv[3]);
349 if (delim == NULL) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000350 ALOGE("can't get delimiter");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800351 sqlite3_result_null(context);
352 return;
353 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800354
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800355 UChar * token = NULL;
356 UChar *state;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800357 int numTokens = 0;
358
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800359 do {
360 if (numTokens == 0) {
361 token = origData;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800362 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800363
364 // Reset the program so we can use it to perform the insert
365 sqlite3_reset(statement);
366 UErrorCode status = U_ZERO_ERROR;
367 char keybuf[1024];
368 uint32_t result = ucol_getSortKey(collator, token, -1, (uint8_t*)keybuf, sizeof(keybuf)-1);
369 if (result > sizeof(keybuf)) {
370 // TODO allocate memory for this super big string
Steve Blockcbfefda2012-01-06 19:14:58 +0000371 ALOGE("ucol_getSortKey needs bigger buffer %d", result);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800372 break;
373 }
374 uint32_t keysize = result-1;
375 uint32_t base16Size = keysize*2;
376 char *base16buf = (char*)malloc(base16Size);
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800377 base16Encode(base16buf, keybuf, keysize);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800378 err = sqlite3_bind_text(statement, 1, base16buf, base16Size, SQLITE_STATIC);
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800379
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800380 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000381 ALOGE(" sqlite3_bind_text16 error %d", err);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800382 free(base16buf);
383 break;
384 }
385
Bjorn Bringert687f1162009-05-13 22:13:09 +0100386 if (useTokenIndex) {
387 err = sqlite3_bind_int(statement, 3, numTokens);
388 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000389 ALOGE(" sqlite3_bind_int error %d", err);
Bjorn Bringert687f1162009-05-13 22:13:09 +0100390 free(base16buf);
391 break;
392 }
393 }
394
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800395 err = sqlite3_step(statement);
396 free(base16buf);
397
398 if (err != SQLITE_DONE) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000399 ALOGE(" sqlite3_step error %d", err);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800400 break;
401 }
402 numTokens++;
403 if (numTokens == 1) {
404 // first call
405 u_strtok_r(origData, delim, &state);
406 }
407 } while ((token = u_strtok_r(NULL, delim, &state)) != NULL);
408 sqlite3_result_int(context, numTokens);
409}
410
411static void localized_collator_dtor(UCollator* collator)
412{
413 ucol_close(collator);
414}
415
416#define LOCALIZED_COLLATOR_NAME "LOCALIZED"
417
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900418// This collator may be removed in the near future, so you MUST not use now.
419#define PHONEBOOK_COLLATOR_NAME "PHONEBOOK"
420
Jiyong Parkb4076df2017-08-09 20:15:42 +0900421#endif // SQLITE_ENABLE_ICU
422
423extern "C" int register_localized_collators(sqlite3* handle __attribute((unused)),
424 const char* systemLocale __attribute((unused)),
425 int utf16Storage __attribute((unused)))
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800426{
Jiyong Parkb4076df2017-08-09 20:15:42 +0900427// This function is no-op for the VNDK, but should exist in case when some vendor
428// module has a reference to this function.
429#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800430 UErrorCode status = U_ZERO_ERROR;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800431 UCollator* collator = ucol_open(systemLocale, &status);
432 if (U_FAILURE(status)) {
433 return -1;
434 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800435
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800436 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
437 if (U_FAILURE(status)) {
438 return -1;
439 }
440
Elliott Hughes153c1022016-09-13 17:13:29 -0700441 int err;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800442 if (utf16Storage) {
443 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF16, collator,
444 collate16, (void(*)(void*))localized_collator_dtor);
445 } else {
446 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF8, collator,
447 collate8, (void(*)(void*))localized_collator_dtor);
448 }
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900449
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800450 if (err != SQLITE_OK) {
451 return err;
452 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800453
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800454 // Register the _TOKENIZE function
455 err = sqlite3_create_function(handle, "_TOKENIZE", 4, SQLITE_UTF16, collator, tokenize, NULL, NULL);
456 if (err != SQLITE_OK) {
457 return err;
Bjorn Bringert687f1162009-05-13 22:13:09 +0100458 }
459 err = sqlite3_create_function(handle, "_TOKENIZE", 5, SQLITE_UTF16, collator, tokenize, NULL, NULL);
460 if (err != SQLITE_OK) {
461 return err;
462 }
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100463 err = sqlite3_create_function(handle, "_TOKENIZE", 6, SQLITE_UTF16, collator, tokenize, NULL, NULL);
464 if (err != SQLITE_OK) {
465 return err;
466 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800467
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900468 //// PHONEBOOK_COLLATOR
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900469 status = U_ZERO_ERROR;
Jay Shraunerdb8a3862012-12-17 11:12:30 -0800470 collator = ucol_open(systemLocale, &status);
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900471 if (U_FAILURE(status)) {
472 return -1;
473 }
474
475 status = U_ZERO_ERROR;
476 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
477 if (U_FAILURE(status)) {
478 return -1;
479 }
480
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900481 if (utf16Storage) {
482 err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF16, collator,
483 collate16, (void(*)(void*))localized_collator_dtor);
484 } else {
485 err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF8, collator,
486 collate8, (void(*)(void*))localized_collator_dtor);
487 }
488
489 if (err != SQLITE_OK) {
490 return err;
491 }
492 //// PHONEBOOK_COLLATOR
Jiyong Parkb4076df2017-08-09 20:15:42 +0900493#endif //SQLITE_ENABLE_ICU
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900494
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800495 return SQLITE_OK;
496}
497
498
Jiyong Parkb4076df2017-08-09 20:15:42 +0900499extern "C" int register_android_functions(sqlite3 * handle, int utf16Storage __attribute((unused)))
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800500{
501 int err;
Jiyong Parkb4076df2017-08-09 20:15:42 +0900502#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800503 UErrorCode status = U_ZERO_ERROR;
504
505 UCollator * collator = ucol_open(NULL, &status);
506 if (U_FAILURE(status)) {
507 return -1;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800508 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800509
510 if (utf16Storage) {
511 // Note that text should be stored as UTF-16
512 err = sqlite3_exec(handle, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
513 if (err != SQLITE_OK) {
514 return err;
515 }
516
517 // Register the UNICODE collation
518 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF16, collator, collate16,
519 (void(*)(void*))localized_collator_dtor);
520 } else {
521 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF8, collator, collate8,
522 (void(*)(void*))localized_collator_dtor);
523 }
524
525 if (err != SQLITE_OK) {
526 return err;
527 }
Jiyong Parkb4076df2017-08-09 20:15:42 +0900528#endif // SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800529
530 // Register the PHONE_NUM_EQUALS function
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700531 err = sqlite3_create_function(
532 handle, "PHONE_NUMBERS_EQUAL", 2,
533 SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800534 if (err != SQLITE_OK) {
535 return err;
536 }
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700537
538 // Register the PHONE_NUM_EQUALS function with an additional argument "use_strict"
539 err = sqlite3_create_function(
540 handle, "PHONE_NUMBERS_EQUAL", 3,
541 SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
542 if (err != SQLITE_OK) {
543 return err;
544 }
545
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800546 // Register the _DELETE_FILE function
547 err = sqlite3_create_function(handle, "_DELETE_FILE", 1, SQLITE_UTF8, NULL, delete_file, NULL, NULL);
548 if (err != SQLITE_OK) {
549 return err;
550 }
551
552#if ENABLE_ANDROID_LOG
553 // Register the _LOG function
554 err = sqlite3_create_function(handle, "_LOG", 1, SQLITE_UTF8, NULL, android_log, NULL, NULL);
555 if (err != SQLITE_OK) {
556 return err;
557 }
558#endif
559
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800560 // Register the _PHONE_NUMBER_STRIPPED_REVERSED function, which imitates
Elliott Hughes153c1022016-09-13 17:13:29 -0700561 // PhoneNumberUtils.getStrippedReversed. This function is used by
562 // packages/providers/ContactsProvider/src/com/android/providers/contacts/LegacyApiSupport.java
563 // to provide compatibility with Android 1.6 and earlier.
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800564 err = sqlite3_create_function(handle,
565 "_PHONE_NUMBER_STRIPPED_REVERSED",
566 1, SQLITE_UTF8, NULL,
567 phone_number_stripped_reversed,
568 NULL, NULL);
569 if (err != SQLITE_OK) {
570 return err;
571 }
572
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800573 return SQLITE_OK;
574}