The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 1 | /* |
| 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 Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 25 | #include <unicode/uiter.h> |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 26 | #include <unicode/ustring.h> |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 27 | #include <unicode/utypes.h> |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 28 | #include <cutils/log.h> |
| 29 | |
| 30 | #include "sqlite3_android.h" |
| 31 | #include "PhoneNumberUtils.h" |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 32 | #include "PhonebookIndex.h" |
The Android Open Source Project | 455ed29 | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 33 | #include "PhoneticStringUtils.h" |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 34 | |
| 35 | #define ENABLE_ANDROID_LOG 0 |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 36 | #define SMALL_BUFFER_SIZE 10 |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 37 | |
| 38 | static int collate16(void *p, int n1, const void *v1, int n2, const void *v2) |
| 39 | { |
| 40 | UCollator *coll = (UCollator *) p; |
| 41 | UCollationResult result = ucol_strcoll(coll, (const UChar *) v1, n1, |
| 42 | (const UChar *) v2, n2); |
| 43 | |
| 44 | if (result == UCOL_LESS) { |
| 45 | return -1; |
| 46 | } else if (result == UCOL_GREATER) { |
| 47 | return 1; |
| 48 | } else { |
| 49 | return 0; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static int collate8(void *p, int n1, const void *v1, int n2, const void *v2) |
| 54 | { |
| 55 | UCollator *coll = (UCollator *) p; |
| 56 | UCharIterator i1, i2; |
| 57 | UErrorCode status = U_ZERO_ERROR; |
| 58 | |
| 59 | uiter_setUTF8(&i1, (const char *) v1, n1); |
| 60 | uiter_setUTF8(&i2, (const char *) v2, n2); |
| 61 | |
| 62 | UCollationResult result = ucol_strcollIter(coll, &i1, &i2, &status); |
| 63 | |
| 64 | if (U_FAILURE(status)) { |
| 65 | // LOGE("Collation iterator error: %d\n", status); |
| 66 | } |
| 67 | |
| 68 | if (result == UCOL_LESS) { |
| 69 | return -1; |
| 70 | } else if (result == UCOL_GREATER) { |
| 71 | return 1; |
| 72 | } else { |
| 73 | return 0; |
| 74 | } |
| 75 | } |
| 76 | |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 77 | /** |
| 78 | * Obtains the first UNICODE letter from the supplied string, normalizes and returns it. |
| 79 | */ |
| 80 | static void get_phonebook_index( |
| 81 | sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 82 | { |
| 83 | if (argc != 2) { |
| 84 | sqlite3_result_null(context); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | char const * src = (char const *)sqlite3_value_text(argv[0]); |
| 89 | char const * locale = (char const *)sqlite3_value_text(argv[1]); |
| 90 | if (src == NULL || src[0] == 0 || locale == NULL) { |
| 91 | sqlite3_result_null(context); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | UCharIterator iter; |
| 96 | uiter_setUTF8(&iter, src, -1); |
| 97 | |
| 98 | UChar index = android::GetPhonebookIndex(&iter, locale); |
| 99 | if (index == 0) { |
| 100 | sqlite3_result_null(context); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | uint32_t outlen = 0; |
| 105 | uint8_t out[SMALL_BUFFER_SIZE]; |
| 106 | UBool isError = FALSE; |
| 107 | U8_APPEND(out, outlen, SMALL_BUFFER_SIZE * sizeof(uint8_t), index, isError); |
| 108 | if (isError || outlen == 0) { |
| 109 | sqlite3_result_null(context); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | sqlite3_result_text(context, (const char*)out, outlen, SQLITE_TRANSIENT); |
| 114 | } |
| 115 | |
The Android Open Source Project | 455ed29 | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 116 | static void get_phonetically_sortable_string( |
| 117 | sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 118 | { |
| 119 | if (argc != 1) { |
| 120 | sqlite3_result_null(context); |
| 121 | return; |
| 122 | } |
| 123 | char const * src = (char const *)sqlite3_value_text(argv[0]); |
| 124 | char * ret; |
| 125 | size_t len; |
| 126 | |
| 127 | if (!android::GetPhoneticallySortableString(src, &ret, &len)) { |
Daisuke Miyakawa | 0c45e82 | 2009-03-27 19:41:52 -0700 | [diff] [blame] | 128 | // Put this text at the end of a list. |
| 129 | sqlite3_result_text(context, "\xF0\x9F\xBF\xBD", -1, SQLITE_STATIC); |
| 130 | // sqlite3_result_null(context); |
The Android Open Source Project | 455ed29 | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 131 | } else { |
| 132 | sqlite3_result_text(context, ret, len, free); |
| 133 | } |
| 134 | } |
| 135 | |
Daisuke Miyakawa | d28cdc4 | 2009-05-18 14:51:52 +0900 | [diff] [blame] | 136 | static void get_normalized_string( |
| 137 | sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 138 | { |
| 139 | if (argc != 1) { |
| 140 | sqlite3_result_null(context); |
| 141 | return; |
| 142 | } |
| 143 | char const * src = (char const *)sqlite3_value_text(argv[0]); |
| 144 | char * ret; |
| 145 | size_t len; |
| 146 | |
| 147 | if (!android::GetNormalizedString(src, &ret, &len)) { |
| 148 | // Probably broken string. Return 0 length string. |
| 149 | sqlite3_result_text(context, "", -1, SQLITE_STATIC); |
| 150 | } else { |
| 151 | sqlite3_result_text(context, ret, len, free); |
| 152 | } |
| 153 | } |
| 154 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 155 | static void phone_numbers_equal(sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 156 | { |
Daisuke Miyakawa | 948a119 | 2009-09-19 19:19:53 -0700 | [diff] [blame] | 157 | if (argc != 2 && argc != 3) { |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 158 | sqlite3_result_int(context, 0); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | char const * num1 = (char const *)sqlite3_value_text(argv[0]); |
| 163 | char const * num2 = (char const *)sqlite3_value_text(argv[1]); |
| 164 | |
Daisuke Miyakawa | 948a119 | 2009-09-19 19:19:53 -0700 | [diff] [blame] | 165 | bool use_strict = false; |
| 166 | if (argc == 3) { |
| 167 | use_strict = (sqlite3_value_int(argv[2]) != 0); |
| 168 | } |
| 169 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 170 | if (num1 == NULL || num2 == NULL) { |
| 171 | sqlite3_result_null(context); |
| 172 | return; |
| 173 | } |
| 174 | |
Daisuke Miyakawa | 948a119 | 2009-09-19 19:19:53 -0700 | [diff] [blame] | 175 | bool equal = |
| 176 | (use_strict ? |
| 177 | android::phone_number_compare_strict(num1, num2) : |
| 178 | android::phone_number_compare_loose(num1, num2)); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 179 | |
| 180 | if (equal) { |
| 181 | sqlite3_result_int(context, 1); |
| 182 | } else { |
| 183 | sqlite3_result_int(context, 0); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #if ENABLE_ANDROID_LOG |
| 188 | static void android_log(sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 189 | { |
| 190 | char const * tag = "sqlite_trigger"; |
| 191 | char const * msg = ""; |
| 192 | int msgIndex = 0; |
| 193 | |
| 194 | switch (argc) { |
| 195 | case 2: |
| 196 | tag = (char const *)sqlite3_value_text(argv[0]); |
| 197 | if (tag == NULL) { |
| 198 | tag = "sqlite_trigger"; |
| 199 | } |
| 200 | msgIndex = 1; |
| 201 | case 1: |
| 202 | msg = (char const *)sqlite3_value_text(argv[msgIndex]); |
| 203 | if (msg == NULL) { |
| 204 | msg = ""; |
| 205 | } |
| 206 | LOG(LOG_INFO, tag, msg); |
| 207 | sqlite3_result_int(context, 1); |
| 208 | return; |
| 209 | |
| 210 | default: |
| 211 | sqlite3_result_int(context, 0); |
| 212 | return; |
| 213 | } |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | static void delete_file(sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 218 | { |
| 219 | if (argc != 1) { |
| 220 | sqlite3_result_int(context, 0); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | char const * path = (char const *)sqlite3_value_text(argv[0]); |
| 225 | if (path == NULL) { |
| 226 | sqlite3_result_null(context); |
| 227 | return; |
| 228 | } |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 229 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 230 | if (strncmp("/sdcard/", path, 8) != 0) { |
| 231 | sqlite3_result_null(context); |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 232 | return; |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 233 | } |
Marco Nelissen | 2da78c0 | 2009-05-06 11:08:08 -0700 | [diff] [blame] | 234 | if (strstr(path, "/../") != NULL) { |
| 235 | sqlite3_result_null(context); |
| 236 | return; |
| 237 | } |
| 238 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 239 | int err = unlink(path); |
| 240 | if (err != -1) { |
| 241 | // No error occured, return true |
| 242 | sqlite3_result_int(context, 1); |
| 243 | } else { |
| 244 | // An error occured, return false |
| 245 | sqlite3_result_int(context, 0); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static void tokenize_auxdata_delete(void * data) |
| 250 | { |
| 251 | sqlite3_stmt * statement = (sqlite3_stmt *)data; |
| 252 | sqlite3_finalize(statement); |
| 253 | } |
| 254 | |
| 255 | static void base16Encode(char* dest, const char* src, uint32_t size) |
| 256 | { |
| 257 | static const char * BASE16_TABLE = "0123456789abcdef"; |
| 258 | for (uint32_t i = 0; i < size; i++) { |
| 259 | char ch = *src++; |
| 260 | *dest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ]; |
| 261 | *dest++ = BASE16_TABLE[ (ch & 0x0f) ]; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | struct SqliteUserData { |
| 266 | sqlite3 * handle; |
| 267 | UCollator* collator; |
| 268 | }; |
| 269 | |
| 270 | /** |
| 271 | * This function is invoked as: |
| 272 | * |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 273 | * _TOKENIZE('<token_table>', <data_row_id>, <data>, <delimiter>, |
| 274 | * <use_token_index>, <data_tag>) |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 275 | * |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 276 | * If <use_token_index> is omitted, it is treated as 0. |
| 277 | * If <data_tag> is omitted, it is treated as NULL. |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 278 | * |
| 279 | * It will split <data> on each instance of <delimiter> and insert each token |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 280 | * into <token_table>. The following columns in <token_table> are used: |
| 281 | * token TEXT, source INTEGER, token_index INTEGER, tag (any type) |
| 282 | * The token_index column is not required if <use_token_index> is 0. |
| 283 | * The tag column is not required if <data_tag> is NULL. |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 284 | * |
| 285 | * One row is inserted for each token in <data>. |
| 286 | * In each inserted row, 'source' is <data_row_id>. |
| 287 | * In the first inserted row, 'token' is the hex collation key of |
| 288 | * the entire <data> string, and 'token_index' is 0. |
| 289 | * In each row I (where 1 <= I < N, and N is the number of tokens in <data>) |
| 290 | * 'token' will be set to the hex collation key of the I:th token (0-based). |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 291 | * If <use_token_index> != 0, 'token_index' is set to I. |
| 292 | * If <data_tag> is not NULL, 'tag' is set to <data_tag>. |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 293 | * |
| 294 | * In other words, there will be one row for the entire string, |
| 295 | * and one row for each token except the first one. |
| 296 | * |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 297 | * The function returns the number of tokens generated. |
| 298 | */ |
| 299 | static void tokenize(sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 300 | { |
| 301 | //LOGD("enter tokenize"); |
| 302 | int err; |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 303 | int useTokenIndex = 0; |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 304 | int useDataTag = 0; |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 305 | |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 306 | if (!(argc >= 4 || argc <= 6)) { |
| 307 | LOGE("Tokenize requires 4 to 6 arguments"); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 308 | sqlite3_result_null(context); |
| 309 | return; |
| 310 | } |
| 311 | |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 312 | if (argc > 4) { |
| 313 | useTokenIndex = sqlite3_value_int(argv[4]); |
| 314 | } |
| 315 | |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 316 | if (argc > 5) { |
| 317 | useDataTag = (sqlite3_value_type(argv[5]) != SQLITE_NULL); |
| 318 | } |
| 319 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 320 | sqlite3 * handle = sqlite3_context_db_handle(context); |
| 321 | UCollator* collator = (UCollator*)sqlite3_user_data(context); |
| 322 | char const * tokenTable = (char const *)sqlite3_value_text(argv[0]); |
| 323 | if (tokenTable == NULL) { |
| 324 | LOGE("tokenTable null"); |
| 325 | sqlite3_result_null(context); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | // Get or create the prepared statement for the insertions |
| 330 | sqlite3_stmt * statement = (sqlite3_stmt *)sqlite3_get_auxdata(context, 0); |
| 331 | if (!statement) { |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 332 | char const * tokenIndexCol = useTokenIndex ? ", token_index" : ""; |
| 333 | char const * tokenIndexParam = useTokenIndex ? ", ?" : ""; |
| 334 | char const * dataTagCol = useDataTag ? ", tag" : ""; |
| 335 | char const * dataTagParam = useDataTag ? ", ?" : ""; |
| 336 | char * sql = sqlite3_mprintf("INSERT INTO %s (token, source%s%s) VALUES (?, ?%s%s);", |
| 337 | tokenTable, tokenIndexCol, dataTagCol, tokenIndexParam, dataTagParam); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 338 | err = sqlite3_prepare_v2(handle, sql, -1, &statement, NULL); |
| 339 | sqlite3_free(sql); |
| 340 | if (err) { |
| 341 | LOGE("prepare failed"); |
| 342 | sqlite3_result_null(context); |
| 343 | return; |
| 344 | } |
| 345 | // This binds the statement to the table it was compiled against, which is argv[0]. |
| 346 | // If this function is ever called with a different table the finalizer will be called |
| 347 | // and sqlite3_get_auxdata() will return null above, forcing a recompile for the new table. |
| 348 | sqlite3_set_auxdata(context, 0, statement, tokenize_auxdata_delete); |
| 349 | } else { |
| 350 | // Reset the cached statement so that binding the row ID will work properly |
| 351 | sqlite3_reset(statement); |
| 352 | } |
| 353 | |
| 354 | // Bind the row ID of the source row |
| 355 | int64_t rowID = sqlite3_value_int64(argv[1]); |
| 356 | err = sqlite3_bind_int64(statement, 2, rowID); |
| 357 | if (err != SQLITE_OK) { |
| 358 | LOGE("bind failed"); |
| 359 | sqlite3_result_null(context); |
| 360 | return; |
| 361 | } |
| 362 | |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 363 | // Bind <data_tag> to the tag column |
| 364 | if (useDataTag) { |
| 365 | int dataTagParamIndex = useTokenIndex ? 4 : 3; |
| 366 | err = sqlite3_bind_value(statement, dataTagParamIndex, argv[5]); |
| 367 | if (err != SQLITE_OK) { |
| 368 | LOGE("bind failed"); |
| 369 | sqlite3_result_null(context); |
| 370 | return; |
| 371 | } |
| 372 | } |
| 373 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 374 | // Get the raw bytes for the string to tokenize |
| 375 | // the string will be modified by following code |
| 376 | // however, sqlite did not reuse the string, so it is safe to not dup it |
| 377 | UChar * origData = (UChar *)sqlite3_value_text16(argv[2]); |
| 378 | if (origData == NULL) { |
| 379 | sqlite3_result_null(context); |
| 380 | return; |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 381 | } |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 382 | |
| 383 | // Get the raw bytes for the delimiter |
| 384 | const UChar * delim = (const UChar *)sqlite3_value_text16(argv[3]); |
| 385 | if (delim == NULL) { |
| 386 | LOGE("can't get delimiter"); |
| 387 | sqlite3_result_null(context); |
| 388 | return; |
| 389 | } |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 390 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 391 | UChar * token = NULL; |
| 392 | UChar *state; |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 393 | int numTokens = 0; |
| 394 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 395 | do { |
| 396 | if (numTokens == 0) { |
| 397 | token = origData; |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 398 | } |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 399 | |
| 400 | // Reset the program so we can use it to perform the insert |
| 401 | sqlite3_reset(statement); |
| 402 | UErrorCode status = U_ZERO_ERROR; |
| 403 | char keybuf[1024]; |
| 404 | uint32_t result = ucol_getSortKey(collator, token, -1, (uint8_t*)keybuf, sizeof(keybuf)-1); |
| 405 | if (result > sizeof(keybuf)) { |
| 406 | // TODO allocate memory for this super big string |
| 407 | LOGE("ucol_getSortKey needs bigger buffer %d", result); |
| 408 | break; |
| 409 | } |
| 410 | uint32_t keysize = result-1; |
| 411 | uint32_t base16Size = keysize*2; |
| 412 | char *base16buf = (char*)malloc(base16Size); |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 413 | base16Encode(base16buf, keybuf, keysize); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 414 | err = sqlite3_bind_text(statement, 1, base16buf, base16Size, SQLITE_STATIC); |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 415 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 416 | if (err != SQLITE_OK) { |
| 417 | LOGE(" sqlite3_bind_text16 error %d", err); |
| 418 | free(base16buf); |
| 419 | break; |
| 420 | } |
| 421 | |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 422 | if (useTokenIndex) { |
| 423 | err = sqlite3_bind_int(statement, 3, numTokens); |
| 424 | if (err != SQLITE_OK) { |
| 425 | LOGE(" sqlite3_bind_int error %d", err); |
| 426 | free(base16buf); |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 431 | err = sqlite3_step(statement); |
| 432 | free(base16buf); |
| 433 | |
| 434 | if (err != SQLITE_DONE) { |
| 435 | LOGE(" sqlite3_step error %d", err); |
| 436 | break; |
| 437 | } |
| 438 | numTokens++; |
| 439 | if (numTokens == 1) { |
| 440 | // first call |
| 441 | u_strtok_r(origData, delim, &state); |
| 442 | } |
| 443 | } while ((token = u_strtok_r(NULL, delim, &state)) != NULL); |
| 444 | sqlite3_result_int(context, numTokens); |
| 445 | } |
| 446 | |
| 447 | static void localized_collator_dtor(UCollator* collator) |
| 448 | { |
| 449 | ucol_close(collator); |
| 450 | } |
| 451 | |
| 452 | #define LOCALIZED_COLLATOR_NAME "LOCALIZED" |
| 453 | |
| 454 | extern "C" int register_localized_collators(sqlite3* handle, const char* systemLocale, int utf16Storage) |
| 455 | { |
| 456 | int err; |
| 457 | UErrorCode status = U_ZERO_ERROR; |
| 458 | void* icudata; |
| 459 | |
| 460 | UCollator* collator = ucol_open(systemLocale, &status); |
| 461 | if (U_FAILURE(status)) { |
| 462 | return -1; |
| 463 | } |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 464 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 465 | ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status); |
| 466 | if (U_FAILURE(status)) { |
| 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | status = U_ZERO_ERROR; |
| 471 | char buf[1024]; |
| 472 | int n = ucol_getShortDefinitionString(collator, NULL, buf, 1024, &status); |
| 473 | |
| 474 | if (utf16Storage) { |
| 475 | err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF16, collator, |
| 476 | collate16, (void(*)(void*))localized_collator_dtor); |
| 477 | } else { |
| 478 | err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF8, collator, |
| 479 | collate8, (void(*)(void*))localized_collator_dtor); |
| 480 | } |
| 481 | if (err != SQLITE_OK) { |
| 482 | return err; |
| 483 | } |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 484 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 485 | // Register the _TOKENIZE function |
| 486 | err = sqlite3_create_function(handle, "_TOKENIZE", 4, SQLITE_UTF16, collator, tokenize, NULL, NULL); |
| 487 | if (err != SQLITE_OK) { |
| 488 | return err; |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 489 | } |
| 490 | err = sqlite3_create_function(handle, "_TOKENIZE", 5, SQLITE_UTF16, collator, tokenize, NULL, NULL); |
| 491 | if (err != SQLITE_OK) { |
| 492 | return err; |
| 493 | } |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 494 | err = sqlite3_create_function(handle, "_TOKENIZE", 6, SQLITE_UTF16, collator, tokenize, NULL, NULL); |
| 495 | if (err != SQLITE_OK) { |
| 496 | return err; |
| 497 | } |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 498 | |
| 499 | return SQLITE_OK; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | extern "C" int register_android_functions(sqlite3 * handle, int utf16Storage) |
| 504 | { |
| 505 | int err; |
| 506 | UErrorCode status = U_ZERO_ERROR; |
| 507 | |
| 508 | UCollator * collator = ucol_open(NULL, &status); |
| 509 | if (U_FAILURE(status)) { |
| 510 | return -1; |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 511 | } |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 512 | |
| 513 | if (utf16Storage) { |
| 514 | // Note that text should be stored as UTF-16 |
| 515 | err = sqlite3_exec(handle, "PRAGMA encoding = 'UTF-16'", 0, 0, 0); |
| 516 | if (err != SQLITE_OK) { |
| 517 | return err; |
| 518 | } |
| 519 | |
| 520 | // Register the UNICODE collation |
| 521 | err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF16, collator, collate16, |
| 522 | (void(*)(void*))localized_collator_dtor); |
| 523 | } else { |
| 524 | err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF8, collator, collate8, |
| 525 | (void(*)(void*))localized_collator_dtor); |
| 526 | } |
| 527 | |
| 528 | if (err != SQLITE_OK) { |
| 529 | return err; |
| 530 | } |
| 531 | |
| 532 | // Register the PHONE_NUM_EQUALS function |
Daisuke Miyakawa | 948a119 | 2009-09-19 19:19:53 -0700 | [diff] [blame] | 533 | err = sqlite3_create_function( |
| 534 | handle, "PHONE_NUMBERS_EQUAL", 2, |
| 535 | SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 536 | if (err != SQLITE_OK) { |
| 537 | return err; |
| 538 | } |
Daisuke Miyakawa | 948a119 | 2009-09-19 19:19:53 -0700 | [diff] [blame] | 539 | |
| 540 | // Register the PHONE_NUM_EQUALS function with an additional argument "use_strict" |
| 541 | err = sqlite3_create_function( |
| 542 | handle, "PHONE_NUMBERS_EQUAL", 3, |
| 543 | SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL); |
| 544 | if (err != SQLITE_OK) { |
| 545 | return err; |
| 546 | } |
| 547 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 548 | // Register the _DELETE_FILE function |
| 549 | err = sqlite3_create_function(handle, "_DELETE_FILE", 1, SQLITE_UTF8, NULL, delete_file, NULL, NULL); |
| 550 | if (err != SQLITE_OK) { |
| 551 | return err; |
| 552 | } |
| 553 | |
| 554 | #if ENABLE_ANDROID_LOG |
| 555 | // Register the _LOG function |
| 556 | err = sqlite3_create_function(handle, "_LOG", 1, SQLITE_UTF8, NULL, android_log, NULL, NULL); |
| 557 | if (err != SQLITE_OK) { |
| 558 | return err; |
| 559 | } |
| 560 | #endif |
| 561 | |
The Android Open Source Project | 455ed29 | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 562 | // Register the GET_PHONETICALLY_SORTABLE_STRING function |
| 563 | err = sqlite3_create_function(handle, |
| 564 | "GET_PHONETICALLY_SORTABLE_STRING", |
| 565 | 1, SQLITE_UTF8, NULL, |
| 566 | get_phonetically_sortable_string, |
| 567 | NULL, NULL); |
| 568 | if (err != SQLITE_OK) { |
| 569 | return err; |
| 570 | } |
| 571 | |
Daisuke Miyakawa | d28cdc4 | 2009-05-18 14:51:52 +0900 | [diff] [blame] | 572 | // Register the GET_NORMALIZED_STRING function |
| 573 | err = sqlite3_create_function(handle, |
| 574 | "GET_NORMALIZED_STRING", |
| 575 | 1, SQLITE_UTF8, NULL, |
| 576 | get_normalized_string, |
| 577 | NULL, NULL); |
| 578 | if (err != SQLITE_OK) { |
| 579 | return err; |
| 580 | } |
| 581 | |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame^] | 582 | // Register the GET_PHONEBOOK_INDEX function |
| 583 | err = sqlite3_create_function(handle, |
| 584 | "GET_PHONEBOOK_INDEX", |
| 585 | 2, SQLITE_UTF8, NULL, |
| 586 | get_phonebook_index, |
| 587 | NULL, NULL); |
| 588 | if (err != SQLITE_OK) { |
| 589 | return err; |
| 590 | } |
| 591 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 592 | return SQLITE_OK; |
| 593 | } |