1e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown/*
2e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * Copyright (C) 2011 The Android Open Source Project
3e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown *
4e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
5e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * you may not use this file except in compliance with the License.
6e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * You may obtain a copy of the License at
7e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown *
8e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
9e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown *
10e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * Unless required by applicable law or agreed to in writing, software
11e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
12e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * See the License for the specific language governing permissions and
14e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * limitations under the License.
15e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown */
16e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
17e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#define LOG_TAG "SQLiteConnection"
18e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
19e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <jni.h>
20e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <JNIHelp.h>
21e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <android_runtime/AndroidRuntime.h>
2287eac99a21772ae56018cb81db6966557b459554Ruben Brunk#include <android_runtime/Log.h>
23e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
24e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <utils/Log.h>
25e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <utils/String8.h>
26e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <utils/String16.h>
27e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <cutils/ashmem.h>
28e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <sys/mman.h>
29e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
30e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <string.h>
31e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <unistd.h>
32e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
3349d2b1864c3dfec6faff74d67cb2527a8f1af5a8Mathias Agopian#include <androidfw/CursorWindow.h>
34e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
35e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <sqlite3.h>
36e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <sqlite3_android.h>
37e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
38e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include "android_database_SQLiteCommon.h"
39e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
40987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe#include "core_jni_helpers.h"
41987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe
421d9f742e001ed8280fa93fd9ba0b1125ce6d00aeJeff Brown// Set to 1 to use UTF16 storage for localized indexes.
43e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#define UTF16_STORAGE 0
44e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
45e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brownnamespace android {
46e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
47676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown/* Busy timeout in milliseconds.
48676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown * If another connection (possibly in another process) has the database locked for
49676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown * longer than this amount of time then SQLite will generate a SQLITE_BUSY error.
50676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown * The SQLITE_BUSY error is then raised as a SQLiteDatabaseLockedException.
51676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown *
52676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown * In ordinary usage, busy timeouts are quite rare.  Most databases only ever
53676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown * have a single open connection at a time unless they are using WAL.  When using
54676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown * WAL, a timeout could occur if one connection is busy performing an auto-checkpoint
55676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown * operation.  The busy timeout needs to be long enough to tolerate slow I/O write
56676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown * operations but not so long as to cause the application to hang indefinitely if
57676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown * there is a problem acquiring a database lock.
58676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown */
59676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brownstatic const int BUSY_TIMEOUT_MS = 2500;
60676c519ec4509c799049bae5f6b20748ee5210c1Jeff Brown
61e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brownstatic struct {
62e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    jfieldID name;
63e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    jfieldID numArgs;
64e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    jmethodID dispatchCallback;
65e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown} gSQLiteCustomFunctionClassInfo;
66e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
67e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brownstatic struct {
68e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    jclass clazz;
69e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown} gStringClassInfo;
70e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
71e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brownstruct SQLiteConnection {
72e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    // Open flags.
73e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    // Must be kept in sync with the constants defined in SQLiteDatabase.java.
74e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    enum {
75e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown        OPEN_READWRITE          = 0x00000000,
76e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown        OPEN_READONLY           = 0x00000001,
77e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown        OPEN_READ_MASK          = 0x00000001,
78e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown        NO_LOCALIZED_COLLATORS  = 0x00000010,
79e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown        CREATE_IF_NECESSARY     = 0x10000000,
80e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    };
81e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
82e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    sqlite3* const db;
83e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    const int openFlags;
84e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    const String8 path;
85e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    const String8 label;
86e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
8775ea64fc54f328d37b115cfb1ded1e45c30380edJeff Brown    volatile bool canceled;
8875ea64fc54f328d37b115cfb1ded1e45c30380edJeff Brown
89e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    SQLiteConnection(sqlite3* db, int openFlags, const String8& path, const String8& label) :
9075ea64fc54f328d37b115cfb1ded1e45c30380edJeff Brown        db(db), openFlags(openFlags), path(path), label(label), canceled(false) { }
91e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown};
92e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
93e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown// Called each time a statement begins execution, when tracing is enabled.
94e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brownstatic void sqliteTraceCallback(void *data, const char *sql) {
95