blob: 4174a1392d8cfa5a672794216d9231714d52e636 [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>
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080025#include <unicode/uiter.h>
The Android Open Source Project7790ef52009-03-03 19:30:40 -080026#include <unicode/ustring.h>
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080027#include <unicode/utypes.h>
Colin Crossea170612017-04-19 17:36:28 -070028#include <log/log.h>
The Android Open Source Project7790ef52009-03-03 19:30:40 -080029
30#include "sqlite3_android.h"
31#include "PhoneNumberUtils.h"
32
33#define ENABLE_ANDROID_LOG 0
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080034#define SMALL_BUFFER_SIZE 10
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -080035#define PHONE_NUMBER_BUFFER_SIZE 40
The Android Open Source Project7790ef52009-03-03 19:30:40 -080036
37static int collate16(void *p, int n1, const void *v1, int n2, const void *v2)
38{
39 UCollator *coll = (UCollator *) p;
40 UCollationResult result = ucol_strcoll(coll, (const UChar *) v1, n1,
41 (const UChar *) v2, n2);
42
43 if (result == UCOL_LESS) {
44 return -1;
45 } else if (result == UCOL_GREATER) {
46 return 1;
47 } else {
48 return 0;
49 }
50}
51
52static int collate8(void *p, int n1, const void *v1, int n2, const void *v2)
53{
54 UCollator *coll = (UCollator *) p;
55 UCharIterator i1, i2;
56 UErrorCode status = U_ZERO_ERROR;
57
58 uiter_setUTF8(&i1, (const char *) v1, n1);
59 uiter_setUTF8(&i2, (const char *) v2, n2);
60
61 UCollationResult result = ucol_strcollIter(coll, &i1, &i2, &status);
62
63 if (U_FAILURE(status)) {
Steve Blockcbfefda2012-01-06 19:14:58 +000064// ALOGE("Collation iterator error: %d\n", status);
The Android Open Source Project7790ef52009-03-03 19:30:40 -080065 }
66
67 if (result == UCOL_LESS) {
68 return -1;
69 } else if (result == UCOL_GREATER) {
70 return 1;
71 } else {
72 return 0;
73 }
74}
75
76static void phone_numbers_equal(sqlite3_context * context, int argc, sqlite3_value ** argv)
77{
Daisuke Miyakawa948a1192009-09-19 19:19:53 -070078 if (argc != 2 && argc != 3) {
The Android Open Source Project7790ef52009-03-03 19:30:40 -080079 sqlite3_result_int(context, 0);
80 return;
81 }
82
83 char const * num1 = (char const *)sqlite3_value_text(argv[0]);
84 char const * num2 = (char const *)sqlite3_value_text(argv[1]);
85
Daisuke Miyakawa948a1192009-09-19 19:19:53 -070086 bool use_strict = false;
87 if (argc == 3) {
88 use_strict = (sqlite3_value_int(argv[2]) != 0);
89 }
90
The Android Open Source Project7790ef52009-03-03 19:30:40 -080091 if (num1 == NULL || num2 == NULL) {
92 sqlite3_result_null(context);
93 return;
94 }
95
Daisuke Miyakawa948a1192009-09-19 19:19:53 -070096 bool equal =
97 (use_strict ?
98 android::phone_number_compare_strict(num1, num2) :
99 android::phone_number_compare_loose(num1, num2));
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800100
101 if (equal) {
102 sqlite3_result_int(context, 1);
103 } else {
104 sqlite3_result_int(context, 0);
105 }
106}
107
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800108static void phone_number_stripped_reversed(sqlite3_context * context, int argc,
109 sqlite3_value ** argv)
110{
111 if (argc != 1) {
112 sqlite3_result_int(context, 0);
113 return;
114 }
115
116 char const * number = (char const *)sqlite3_value_text(argv[0]);
117 if (number == NULL) {
118 sqlite3_result_null(context);
119 return;
120 }
121
122 char out[PHONE_NUMBER_BUFFER_SIZE];
123 int outlen = 0;
124 android::phone_number_stripped_reversed_inter(number, out, PHONE_NUMBER_BUFFER_SIZE, &outlen);
125 sqlite3_result_text(context, (const char*)out, outlen, SQLITE_TRANSIENT);
126}
127
128
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800129#if ENABLE_ANDROID_LOG
130static void android_log(sqlite3_context * context, int argc, sqlite3_value ** argv)
131{
132 char const * tag = "sqlite_trigger";
133 char const * msg = "";
134 int msgIndex = 0;
135
136 switch (argc) {
137 case 2:
138 tag = (char const *)sqlite3_value_text(argv[0]);
139 if (tag == NULL) {
140 tag = "sqlite_trigger";
141 }
142 msgIndex = 1;
143 case 1:
144 msg = (char const *)sqlite3_value_text(argv[msgIndex]);
145 if (msg == NULL) {
146 msg = "";
147 }
Hyejin Kim165c6872013-02-28 16:21:20 +0900148 ALOG(LOG_INFO, tag, "%s", msg);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800149 sqlite3_result_int(context, 1);
150 return;
151
152 default:
153 sqlite3_result_int(context, 0);
154 return;
155 }
156}
157#endif
158
159static void delete_file(sqlite3_context * context, int argc, sqlite3_value ** argv)
160{
161 if (argc != 1) {
162 sqlite3_result_int(context, 0);
163 return;
164 }
165
166 char const * path = (char const *)sqlite3_value_text(argv[0]);
Mike Lockwood5a345992011-07-07 12:22:34 -0400167 // Don't allow ".." in paths
168 if (path == NULL || strstr(path, "/../") != NULL) {
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800169 sqlite3_result_null(context);
170 return;
171 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800172
Mike Lockwood5a345992011-07-07 12:22:34 -0400173 // We only allow deleting files in the EXTERNAL_STORAGE path, or one of the
174 // SECONDARY_STORAGE paths
175 bool good_path = false;
176 char const * external_storage = getenv("EXTERNAL_STORAGE");
177 if (external_storage && strncmp(external_storage, path, strlen(external_storage)) == 0) {
178 good_path = true;
179 } else {
180 // check SECONDARY_STORAGE, which should be a colon separated list of paths
181 char const * secondary_paths = getenv("SECONDARY_STORAGE");
182 while (secondary_paths && secondary_paths[0]) {
183 const char* colon = strchr(secondary_paths, ':');
184 int length = (colon ? colon - secondary_paths : strlen(secondary_paths));
185 if (strncmp(secondary_paths, path, length) == 0) {
186 good_path = true;
187 }
188 secondary_paths += length;
189 while (*secondary_paths == ':') secondary_paths++;
190 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800191 }
Mike Lockwood5a345992011-07-07 12:22:34 -0400192
193 if (!good_path) {
Marco Nelissen2da78c02009-05-06 11:08:08 -0700194 sqlite3_result_null(context);
195 return;
196 }
197
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800198 int err = unlink(path);
199 if (err != -1) {
200 // No error occured, return true
201 sqlite3_result_int(context, 1);
202 } else {
203 // An error occured, return false
204 sqlite3_result_int(context, 0);
205 }
206}
207
208static void tokenize_auxdata_delete(void * data)
209{
210 sqlite3_stmt * statement = (sqlite3_stmt *)data;
211 sqlite3_finalize(statement);
212}
213
214static void base16Encode(char* dest, const char* src, uint32_t size)
215{
216 static const char * BASE16_TABLE = "0123456789abcdef";
217 for (uint32_t i = 0; i < size; i++) {
218 char ch = *src++;
219 *dest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
220 *dest++ = BASE16_TABLE[ (ch & 0x0f) ];
221 }
222}
223
224struct SqliteUserData {
225 sqlite3 * handle;
226 UCollator* collator;
227};
228
229/**
230 * This function is invoked as:
231 *
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100232 * _TOKENIZE('<token_table>', <data_row_id>, <data>, <delimiter>,
233 * <use_token_index>, <data_tag>)
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800234 *
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100235 * If <use_token_index> is omitted, it is treated as 0.
236 * If <data_tag> is omitted, it is treated as NULL.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100237 *
238 * It will split <data> on each instance of <delimiter> and insert each token
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100239 * into <token_table>. The following columns in <token_table> are used:
240 * token TEXT, source INTEGER, token_index INTEGER, tag (any type)
241 * The token_index column is not required if <use_token_index> is 0.
242 * The tag column is not required if <data_tag> is NULL.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100243 *
244 * One row is inserted for each token in <data>.
245 * In each inserted row, 'source' is <data_row_id>.
246 * In the first inserted row, 'token' is the hex collation key of
247 * the entire <data> string, and 'token_index' is 0.
248 * In each row I (where 1 <= I < N, and N is the number of tokens in <data>)
249 * 'token' will be set to the hex collation key of the I:th token (0-based).
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100250 * If <use_token_index> != 0, 'token_index' is set to I.
251 * If <data_tag> is not NULL, 'tag' is set to <data_tag>.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100252 *
253 * In other words, there will be one row for the entire string,
254 * and one row for each token except the first one.
255 *
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800256 * The function returns the number of tokens generated.
257 */
258static void tokenize(sqlite3_context * context, int argc, sqlite3_value ** argv)
259{
Steve Blockbcf3ccc2011-12-20 16:21:48 +0000260 //ALOGD("enter tokenize");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800261 int err;
Bjorn Bringert687f1162009-05-13 22:13:09 +0100262 int useTokenIndex = 0;
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100263 int useDataTag = 0;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800264
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100265 if (!(argc >= 4 || argc <= 6)) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000266 ALOGE("Tokenize requires 4 to 6 arguments");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800267 sqlite3_result_null(context);
268 return;
269 }
270
Bjorn Bringert687f1162009-05-13 22:13:09 +0100271 if (argc > 4) {
272 useTokenIndex = sqlite3_value_int(argv[4]);
273 }
274
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100275 if (argc > 5) {
276 useDataTag = (sqlite3_value_type(argv[5]) != SQLITE_NULL);
277 }
278
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800279 sqlite3 * handle = sqlite3_context_db_handle(context);
280 UCollator* collator = (UCollator*)sqlite3_user_data(context);
281 char const * tokenTable = (char const *)sqlite3_value_text(argv[0]);
282 if (tokenTable == NULL) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000283 ALOGE("tokenTable null");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800284 sqlite3_result_null(context);
285 return;
286 }
287
288 // Get or create the prepared statement for the insertions
289 sqlite3_stmt * statement = (sqlite3_stmt *)sqlite3_get_auxdata(context, 0);
290 if (!statement) {
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100291 char const * tokenIndexCol = useTokenIndex ? ", token_index" : "";
292 char const * tokenIndexParam = useTokenIndex ? ", ?" : "";
293 char const * dataTagCol = useDataTag ? ", tag" : "";
294 char const * dataTagParam = useDataTag ? ", ?" : "";
295 char * sql = sqlite3_mprintf("INSERT INTO %s (token, source%s%s) VALUES (?, ?%s%s);",
296 tokenTable, tokenIndexCol, dataTagCol, tokenIndexParam, dataTagParam);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800297 err = sqlite3_prepare_v2(handle, sql, -1, &statement, NULL);
298 sqlite3_free(sql);
299 if (err) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000300 ALOGE("prepare failed");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800301 sqlite3_result_null(context);
302 return;
303 }
304 // This binds the statement to the table it was compiled against, which is argv[0].
305 // If this function is ever called with a different table the finalizer will be called
306 // and sqlite3_get_auxdata() will return null above, forcing a recompile for the new table.
307 sqlite3_set_auxdata(context, 0, statement, tokenize_auxdata_delete);
308 } else {
309 // Reset the cached statement so that binding the row ID will work properly
310 sqlite3_reset(statement);
311 }
312
313 // Bind the row ID of the source row
314 int64_t rowID = sqlite3_value_int64(argv[1]);
315 err = sqlite3_bind_int64(statement, 2, rowID);
316 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000317 ALOGE("bind failed");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800318 sqlite3_result_null(context);
319 return;
320 }
321
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100322 // Bind <data_tag> to the tag column
323 if (useDataTag) {
324 int dataTagParamIndex = useTokenIndex ? 4 : 3;
325 err = sqlite3_bind_value(statement, dataTagParamIndex, argv[5]);
326 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000327 ALOGE("bind failed");
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100328 sqlite3_result_null(context);
329 return;
330 }
331 }
332
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800333 // Get the raw bytes for the string to tokenize
334 // the string will be modified by following code
335 // however, sqlite did not reuse the string, so it is safe to not dup it
336 UChar * origData = (UChar *)sqlite3_value_text16(argv[2]);
337 if (origData == NULL) {
338 sqlite3_result_null(context);
339 return;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800340 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800341
342 // Get the raw bytes for the delimiter
343 const UChar * delim = (const UChar *)sqlite3_value_text16(argv[3]);
344 if (delim == NULL) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000345 ALOGE("can't get delimiter");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800346 sqlite3_result_null(context);
347 return;
348 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800349
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800350 UChar * token = NULL;
351 UChar *state;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800352 int numTokens = 0;
353
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800354 do {
355 if (numTokens == 0) {
356 token = origData;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800357 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800358
359 // Reset the program so we can use it to perform the insert
360 sqlite3_reset(statement);
361 UErrorCode status = U_ZERO_ERROR;
362 char keybuf[1024];
363 uint32_t result = ucol_getSortKey(collator, token, -1, (uint8_t*)keybuf, sizeof(keybuf)-1);
364 if (result > sizeof(keybuf)) {
365 // TODO allocate memory for this super big string
Steve Blockcbfefda2012-01-06 19:14:58 +0000366 ALOGE("ucol_getSortKey needs bigger buffer %d", result);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800367 break;
368 }
369 uint32_t keysize = result-1;
370 uint32_t base16Size = keysize*2;
371 char *base16buf = (char*)malloc(base16Size);
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800372 base16Encode(base16buf, keybuf, keysize);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800373 err = sqlite3_bind_text(statement, 1, base16buf, base16Size, SQLITE_STATIC);
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800374
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800375 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000376 ALOGE(" sqlite3_bind_text16 error %d", err);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800377 free(base16buf);
378 break;
379 }
380
Bjorn Bringert687f1162009-05-13 22:13:09 +0100381 if (useTokenIndex) {
382 err = sqlite3_bind_int(statement, 3, numTokens);
383 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000384 ALOGE(" sqlite3_bind_int error %d", err);
Bjorn Bringert687f1162009-05-13 22:13:09 +0100385 free(base16buf);
386 break;
387 }
388 }
389
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800390 err = sqlite3_step(statement);
391 free(base16buf);
392
393 if (err != SQLITE_DONE) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000394 ALOGE(" sqlite3_step error %d", err);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800395 break;
396 }
397 numTokens++;
398 if (numTokens == 1) {
399 // first call
400 u_strtok_r(origData, delim, &state);
401 }
402 } while ((token = u_strtok_r(NULL, delim, &state)) != NULL);
403 sqlite3_result_int(context, numTokens);
404}
405
406static void localized_collator_dtor(UCollator* collator)
407{
408 ucol_close(collator);
409}
410
411#define LOCALIZED_COLLATOR_NAME "LOCALIZED"
412
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900413// This collator may be removed in the near future, so you MUST not use now.
414#define PHONEBOOK_COLLATOR_NAME "PHONEBOOK"
415
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800416extern "C" int register_localized_collators(sqlite3* handle, const char* systemLocale, int utf16Storage)
417{
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800418 UErrorCode status = U_ZERO_ERROR;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800419 UCollator* collator = ucol_open(systemLocale, &status);
420 if (U_FAILURE(status)) {
421 return -1;
422 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800423
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800424 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
425 if (U_FAILURE(status)) {
426 return -1;
427 }
428
Elliott Hughes153c1022016-09-13 17:13:29 -0700429 int err;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800430 if (utf16Storage) {
431 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF16, collator,
432 collate16, (void(*)(void*))localized_collator_dtor);
433 } else {
434 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF8, collator,
435 collate8, (void(*)(void*))localized_collator_dtor);
436 }
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900437
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800438 if (err != SQLITE_OK) {
439 return err;
440 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800441
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800442 // Register the _TOKENIZE function
443 err = sqlite3_create_function(handle, "_TOKENIZE", 4, SQLITE_UTF16, collator, tokenize, NULL, NULL);
444 if (err != SQLITE_OK) {
445 return err;
Bjorn Bringert687f1162009-05-13 22:13:09 +0100446 }
447 err = sqlite3_create_function(handle, "_TOKENIZE", 5, SQLITE_UTF16, collator, tokenize, NULL, NULL);
448 if (err != SQLITE_OK) {
449 return err;
450 }
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100451 err = sqlite3_create_function(handle, "_TOKENIZE", 6, SQLITE_UTF16, collator, tokenize, NULL, NULL);
452 if (err != SQLITE_OK) {
453 return err;
454 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800455
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900456 //// PHONEBOOK_COLLATOR
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900457 status = U_ZERO_ERROR;
Jay Shraunerdb8a3862012-12-17 11:12:30 -0800458 collator = ucol_open(systemLocale, &status);
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900459 if (U_FAILURE(status)) {
460 return -1;
461 }
462
463 status = U_ZERO_ERROR;
464 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
465 if (U_FAILURE(status)) {
466 return -1;
467 }
468
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900469 if (utf16Storage) {
470 err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF16, collator,
471 collate16, (void(*)(void*))localized_collator_dtor);
472 } else {
473 err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF8, collator,
474 collate8, (void(*)(void*))localized_collator_dtor);
475 }
476
477 if (err != SQLITE_OK) {
478 return err;
479 }
480 //// PHONEBOOK_COLLATOR
481
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800482 return SQLITE_OK;
483}
484
485
486extern "C" int register_android_functions(sqlite3 * handle, int utf16Storage)
487{
488 int err;
489 UErrorCode status = U_ZERO_ERROR;
490
491 UCollator * collator = ucol_open(NULL, &status);
492 if (U_FAILURE(status)) {
493 return -1;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800494 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800495
496 if (utf16Storage) {
497 // Note that text should be stored as UTF-16
498 err = sqlite3_exec(handle, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
499 if (err != SQLITE_OK) {
500 return err;
501 }
502
503 // Register the UNICODE collation
504 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF16, collator, collate16,
505 (void(*)(void*))localized_collator_dtor);
506 } else {
507 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF8, collator, collate8,
508 (void(*)(void*))localized_collator_dtor);
509 }
510
511 if (err != SQLITE_OK) {
512 return err;
513 }
514
515 // Register the PHONE_NUM_EQUALS function
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700516 err = sqlite3_create_function(
517 handle, "PHONE_NUMBERS_EQUAL", 2,
518 SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800519 if (err != SQLITE_OK) {
520 return err;
521 }
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700522
523 // Register the PHONE_NUM_EQUALS function with an additional argument "use_strict"
524 err = sqlite3_create_function(
525 handle, "PHONE_NUMBERS_EQUAL", 3,
526 SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
527 if (err != SQLITE_OK) {
528 return err;
529 }
530
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800531 // Register the _DELETE_FILE function
532 err = sqlite3_create_function(handle, "_DELETE_FILE", 1, SQLITE_UTF8, NULL, delete_file, NULL, NULL);
533 if (err != SQLITE_OK) {
534 return err;
535 }
536
537#if ENABLE_ANDROID_LOG
538 // Register the _LOG function
539 err = sqlite3_create_function(handle, "_LOG", 1, SQLITE_UTF8, NULL, android_log, NULL, NULL);
540 if (err != SQLITE_OK) {
541 return err;
542 }
543#endif
544
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800545 // Register the _PHONE_NUMBER_STRIPPED_REVERSED function, which imitates
Elliott Hughes153c1022016-09-13 17:13:29 -0700546 // PhoneNumberUtils.getStrippedReversed. This function is used by
547 // packages/providers/ContactsProvider/src/com/android/providers/contacts/LegacyApiSupport.java
548 // to provide compatibility with Android 1.6 and earlier.
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800549 err = sqlite3_create_function(handle,
550 "_PHONE_NUMBER_STRIPPED_REVERSED",
551 1, SQLITE_UTF8, NULL,
552 phone_number_stripped_reversed,
553 NULL, NULL);
554 if (err != SQLITE_OK) {
555 return err;
556 }
557
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800558 return SQLITE_OK;
559}