sqlite3_android.cpp revision 6005ac625aa553fe461b363385a8ed4a3c217a1f
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>
25#include <unicode/ustring.h>
26#include <cutils/log.h>
27
28#include "sqlite3_android.h"
29#include "PhoneNumberUtils.h"
30
31#define ENABLE_ANDROID_LOG 0
32
33static int collate16(void *p, int n1, const void *v1, int n2, const void *v2)
34{
35    UCollator *coll = (UCollator *) p;
36    UCollationResult result = ucol_strcoll(coll, (const UChar *) v1, n1,
37                                                 (const UChar *) v2, n2);
38
39    if (result == UCOL_LESS) {
40        return -1;
41    } else if (result == UCOL_GREATER) {
42        return 1;
43    } else {
44        return 0;
45    }
46}
47
48static int collate8(void *p, int n1, const void *v1, int n2, const void *v2)
49{
50    UCollator *coll = (UCollator *) p;
51    UCharIterator i1, i2;
52    UErrorCode status = U_ZERO_ERROR;
53
54    uiter_setUTF8(&i1, (const char *) v1, n1);
55    uiter_setUTF8(&i2, (const char *) v2, n2);
56
57    UCollationResult result = ucol_strcollIter(coll, &i1, &i2, &status);
58
59    if (U_FAILURE(status)) {
60//        LOGE("Collation iterator error: %d\n", status);
61    }
62
63    if (result == UCOL_LESS) {
64        return -1;
65    } else if (result == UCOL_GREATER) {
66        return 1;
67    } else {
68        return 0;
69    }
70}
71
72static void phone_numbers_equal(sqlite3_context * context, int argc, sqlite3_value ** argv)
73{
74    if (argc != 2) {
75        sqlite3_result_int(context, 0);
76        return;
77    }
78
79    char const * num1 = (char const *)sqlite3_value_text(argv[0]);
80    char const * num2 = (char const *)sqlite3_value_text(argv[1]);
81
82    if (num1 == NULL || num2 == NULL) {
83        sqlite3_result_null(context);
84        return;
85    }
86
87    bool equal = android::phone_number_compare(num1, num2);
88
89    if (equal) {
90        sqlite3_result_int(context, 1);
91    } else {
92        sqlite3_result_int(context, 0);
93    }
94}
95
96#if ENABLE_ANDROID_LOG
97static void android_log(sqlite3_context * context, int argc, sqlite3_value ** argv)
98{
99    char const * tag = "sqlite_trigger";
100    char const * msg = "";
101    int msgIndex = 0;
102
103    switch (argc) {
104        case 2:
105            tag = (char const *)sqlite3_value_text(argv[0]);
106            if (tag == NULL) {
107                tag = "sqlite_trigger";
108            }
109            msgIndex = 1;
110        case 1:
111            msg = (char const *)sqlite3_value_text(argv[msgIndex]);
112            if (msg == NULL) {
113                msg = "";
114            }
115            LOG(LOG_INFO, tag, msg);
116            sqlite3_result_int(context, 1);
117            return;
118
119        default:
120            sqlite3_result_int(context, 0);
121            return;
122    }
123}
124#endif
125
126static void delete_file(sqlite3_context * context, int argc, sqlite3_value ** argv)
127{
128    if (argc != 1) {
129        sqlite3_result_int(context, 0);
130        return;
131    }
132
133    char const * path = (char const *)sqlite3_value_text(argv[0]);
134    if (path == NULL) {
135        sqlite3_result_null(context);
136        return;
137    }
138
139    if (strncmp("/sdcard/", path, 8) != 0) {
140        sqlite3_result_null(context);
141        return;
142    }
143
144    int err = unlink(path);
145    if (err != -1) {
146        // No error occured, return true
147        sqlite3_result_int(context, 1);
148    } else {
149        // An error occured, return false
150        sqlite3_result_int(context, 0);
151    }
152}
153
154static void tokenize_auxdata_delete(void * data)
155{
156    sqlite3_stmt * statement = (sqlite3_stmt *)data;
157    sqlite3_finalize(statement);
158}
159
160static void base16Encode(char* dest, const char* src, uint32_t size)
161{
162    static const char * BASE16_TABLE = "0123456789abcdef";
163    for (uint32_t i = 0; i < size; i++) {
164        char ch = *src++;
165        *dest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
166        *dest++ = BASE16_TABLE[ (ch & 0x0f)      ];
167    }
168}
169
170struct SqliteUserData {
171    sqlite3 * handle;
172    UCollator* collator;
173};
174
175/**
176 * This function is invoked as:
177 *
178 *  _TOKENIZE('<token_table>', <data_row_id>, <data>, <delimiter>)
179 *
180 * It will then split data on each instance of delimiter and insert each token
181 * into token_table's 'token' column with data_row_id in the 'source' column.
182 * The function returns the number of tokens generated.
183 */
184static void tokenize(sqlite3_context * context, int argc, sqlite3_value ** argv)
185{
186    //LOGD("enter tokenize");
187    int err;
188
189    if (argc != 4) {
190        LOGE("Tokenize requires 4 arguments");
191        sqlite3_result_null(context);
192        return;
193    }
194
195    sqlite3 * handle = sqlite3_context_db_handle(context);
196    UCollator* collator = (UCollator*)sqlite3_user_data(context);
197    char const * tokenTable = (char const *)sqlite3_value_text(argv[0]);
198    if (tokenTable == NULL) {
199        LOGE("tokenTable null");
200        sqlite3_result_null(context);
201        return;
202    }
203
204    // Get or create the prepared statement for the insertions
205    sqlite3_stmt * statement = (sqlite3_stmt *)sqlite3_get_auxdata(context, 0);
206    if (!statement) {
207        char * sql = sqlite3_mprintf("INSERT INTO %s (token, source) VALUES (?, ?);", tokenTable);
208        err = sqlite3_prepare_v2(handle, sql, -1, &statement, NULL);
209        sqlite3_free(sql);
210        if (err) {
211            LOGE("prepare failed");
212            sqlite3_result_null(context);
213            return;
214        }
215        // This binds the statement to the table it was compiled against, which is argv[0].
216        // If this function is ever called with a different table the finalizer will be called
217        // and sqlite3_get_auxdata() will return null above, forcing a recompile for the new table.
218        sqlite3_set_auxdata(context, 0, statement, tokenize_auxdata_delete);
219    } else {
220        // Reset the cached statement so that binding the row ID will work properly
221        sqlite3_reset(statement);
222    }
223
224    // Bind the row ID of the source row
225    int64_t rowID = sqlite3_value_int64(argv[1]);
226    err = sqlite3_bind_int64(statement, 2, rowID);
227    if (err != SQLITE_OK) {
228        LOGE("bind failed");
229        sqlite3_result_null(context);
230        return;
231    }
232
233    // Get the raw bytes for the string to tokenize
234    // the string will be modified by following code
235    // however, sqlite did not reuse the string, so it is safe to not dup it
236    UChar * origData = (UChar *)sqlite3_value_text16(argv[2]);
237    if (origData == NULL) {
238        sqlite3_result_null(context);
239        return;
240    }
241
242    // Get the raw bytes for the delimiter
243    const UChar * delim = (const UChar *)sqlite3_value_text16(argv[3]);
244    if (delim == NULL) {
245        LOGE("can't get delimiter");
246        sqlite3_result_null(context);
247        return;
248    }
249
250    UChar * token = NULL;
251    UChar *state;
252    int numTokens = 0;
253
254    do {
255        if (numTokens == 0) {
256            token = origData;
257        }
258
259        // Reset the program so we can use it to perform the insert
260        sqlite3_reset(statement);
261        UErrorCode status = U_ZERO_ERROR;
262        char keybuf[1024];
263        uint32_t result = ucol_getSortKey(collator, token, -1, (uint8_t*)keybuf, sizeof(keybuf)-1);
264        if (result > sizeof(keybuf)) {
265            // TODO allocate memory for this super big string
266            LOGE("ucol_getSortKey needs bigger buffer %d", result);
267            break;
268        }
269        uint32_t keysize = result-1;
270        uint32_t base16Size = keysize*2;
271        char *base16buf = (char*)malloc(base16Size);
272        base16Encode(base16buf, keybuf, keysize);
273        err = sqlite3_bind_text(statement, 1, base16buf, base16Size, SQLITE_STATIC);
274
275        if (err != SQLITE_OK) {
276            LOGE(" sqlite3_bind_text16 error %d", err);
277            free(base16buf);
278            break;
279        }
280
281        err = sqlite3_step(statement);
282        free(base16buf);
283
284        if (err != SQLITE_DONE) {
285            LOGE(" sqlite3_step error %d", err);
286            break;
287        }
288        numTokens++;
289        if (numTokens == 1) {
290            // first call
291            u_strtok_r(origData, delim, &state);
292        }
293    } while ((token = u_strtok_r(NULL, delim, &state)) != NULL);
294    sqlite3_result_int(context, numTokens);
295}
296
297static void localized_collator_dtor(UCollator* collator)
298{
299    ucol_close(collator);
300}
301
302#define LOCALIZED_COLLATOR_NAME "LOCALIZED"
303
304extern "C" int register_localized_collators(sqlite3* handle, const char* systemLocale, int utf16Storage)
305{
306    int err;
307    UErrorCode status = U_ZERO_ERROR;
308    void* icudata;
309
310    UCollator* collator = ucol_open(systemLocale, &status);
311    if (U_FAILURE(status)) {
312        return -1;
313    }
314
315    ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
316    if (U_FAILURE(status)) {
317        return -1;
318    }
319
320    status = U_ZERO_ERROR;
321    char buf[1024];
322    int n = ucol_getShortDefinitionString(collator, NULL, buf, 1024, &status);
323
324    if (utf16Storage) {
325        err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF16, collator,
326                collate16, (void(*)(void*))localized_collator_dtor);
327    } else {
328        err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF8, collator,
329                collate8, (void(*)(void*))localized_collator_dtor);
330    }
331    if (err != SQLITE_OK) {
332        return err;
333    }
334
335    // Register the _TOKENIZE function
336    err = sqlite3_create_function(handle, "_TOKENIZE", 4, SQLITE_UTF16, collator, tokenize, NULL, NULL);
337    if (err != SQLITE_OK) {
338        return err;
339    }
340
341    return SQLITE_OK;
342}
343
344
345extern "C" int register_android_functions(sqlite3 * handle, int utf16Storage)
346{
347    int err;
348    UErrorCode status = U_ZERO_ERROR;
349
350    UCollator * collator = ucol_open(NULL, &status);
351    if (U_FAILURE(status)) {
352        return -1;
353    }
354
355    if (utf16Storage) {
356        // Note that text should be stored as UTF-16
357        err = sqlite3_exec(handle, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
358        if (err != SQLITE_OK) {
359            return err;
360        }
361
362        // Register the UNICODE collation
363        err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF16, collator, collate16,
364                (void(*)(void*))localized_collator_dtor);
365    } else {
366        err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF8, collator, collate8,
367                (void(*)(void*))localized_collator_dtor);
368    }
369
370    if (err != SQLITE_OK) {
371        return err;
372    }
373
374    // Register the PHONE_NUM_EQUALS function
375    err = sqlite3_create_function(handle, "PHONE_NUMBERS_EQUAL", 2, SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
376    if (err != SQLITE_OK) {
377        return err;
378    }
379
380    // Register the _DELETE_FILE function
381    err = sqlite3_create_function(handle, "_DELETE_FILE", 1, SQLITE_UTF8, NULL, delete_file, NULL, NULL);
382    if (err != SQLITE_OK) {
383        return err;
384    }
385
386#if ENABLE_ANDROID_LOG
387    // Register the _LOG function
388    err = sqlite3_create_function(handle, "_LOG", 1, SQLITE_UTF8, NULL, android_log, NULL, NULL);
389    if (err != SQLITE_OK) {
390        return err;
391    }
392#endif
393
394    return SQLITE_OK;
395}
396
397