SQLite Android Bindings
Check-in [dec1c9374f]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Update some C++ files to match recent changes to the Android core.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: dec1c9374f66c4f062721a7c410a4caebe0e3731
User & Date: dan 2017-11-27 20:59:39
Context
2017-11-28
08:22
Update a couple of java source files to more closely match their Android counterparts. check-in: 20bdb663b0 user: dan tags: trunk
2017-11-27
20:59
Update some C++ files to match recent changes to the Android core. check-in: dec1c9374f user: dan tags: trunk
19:22
Remove out of date "package.html" files. check-in: e9352bdf96 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to sqlite3/src/main/jni/sqlite/android_database_SQLiteConnection.cpp.

    26     26   
    27     27   
    28     28   #include <sys/mman.h>
    29     29   #include <string.h>
    30     30   #include <unistd.h>
    31     31   #include <assert.h>
    32     32   
    33         -#if 0
    34         -#include <androidfw/CursorWindow.h>
    35         -#endif
    36         -
    37     33   #include <sqlite3.h>
    38         -#if 0
    39         -#include <sqlite3_android.h>
    40         -#endif
    41     34   
    42     35   #include "android_database_SQLiteCommon.h"
    43     36   
    44     37   #include <string>
    45     38   
    46     39   // Set to 1 to use UTF16 storage for localized indexes.
    47     40   #define UTF16_STORAGE 0
................................................................................
    58     51    * WAL, a timeout could occur if one connection is busy performing an auto-checkpoint
    59     52    * operation.  The busy timeout needs to be long enough to tolerate slow I/O write
    60     53    * operations but not so long as to cause the application to hang indefinitely if
    61     54    * there is a problem acquiring a database lock.
    62     55    */
    63     56   static const int BUSY_TIMEOUT_MS = 2500;
    64     57   
           58  +/* The original code uses AndroidRuntime::getJNIEnv() to obtain a 
           59  +** pointer to the VM. This is not available in the NDK, so instead
           60  +** the following global variable is set as part of this module's
           61  +** JNI_OnLoad method.  */
    65     62   static JavaVM *gpJavaVM = 0;
    66     63   
    67     64   static struct {
    68     65       jfieldID name;
    69     66       jfieldID numArgs;
    70     67       jmethodID dispatchCallback;
    71     68   } gSQLiteCustomFunctionClassInfo;
................................................................................
   184    181       err = sqlite3_busy_timeout(db, BUSY_TIMEOUT_MS);
   185    182       if (err != SQLITE_OK) {
   186    183           throw_sqlite3_exception(env, db, "Could not set busy timeout");
   187    184           sqlite3_close(db);
   188    185           return 0;
   189    186       }
   190    187   
   191         -    // Register custom Android functions.
   192         -#if 0
   193         -    err = register_android_functions(db, UTF16_STORAGE);
   194         -    if (err) {
   195         -        throw_sqlite3_exception(env, db, "Could not register Android SQL functions.");
   196         -        sqlite3_close(db);
   197         -        return 0;
   198         -    }
   199         -#endif
   200         -
   201    188       // Create wrapper object.
   202    189       SQLiteConnection* connection = new SQLiteConnection(db, openFlags, path, label);
   203    190   
   204    191       // Enable tracing and profiling if requested.
   205    192       if (enableTrace) {
   206    193           sqlite3_trace(db, &sqliteTraceCallback, connection);
   207    194       }
................................................................................
   307    294           throw_sqlite3_exception(env, connection->db);
   308    295           return;
   309    296       }
   310    297   }
   311    298   
   312    299   static void nativeRegisterLocalizedCollators(JNIEnv* env, jclass clazz, jlong connectionPtr,
   313    300           jstring localeStr) {
   314         -    SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);
   315         -
   316         -    const char* locale = env->GetStringUTFChars(localeStr, NULL);
   317         -#if 0
   318         -    int err = register_localized_collators(connection->db, locale, UTF16_STORAGE);
   319         -    env->ReleaseStringUTFChars(localeStr, locale);
   320         -
   321         -    if (err != SQLITE_OK) {
   322         -        throw_sqlite3_exception(env, connection->db);
   323         -    }
   324         -#endif
          301  +  /* Localized collators are not supported. */
   325    302   }
   326    303   
   327    304   static jlong nativePrepareStatement(JNIEnv* env, jclass clazz, jlong connectionPtr,
   328    305           jstring sqlString) {
   329    306       SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);
   330    307   
   331    308       jsize sqlLength = env->GetStringLength(sqlString);
................................................................................
   365    342       // is always finalized regardless.
   366    343       ALOGV("Finalized statement %p on connection %p", statement, connection->db);
   367    344       sqlite3_finalize(statement);
   368    345   }
   369    346   
   370    347   static jint nativeGetParameterCount(JNIEnv* env, jclass clazz, jlong connectionPtr,
   371    348           jlong statementPtr) {
   372         -    SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);
   373    349       sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
   374    350   
   375    351       return sqlite3_bind_parameter_count(statement);
   376    352   }
   377    353   
   378    354   static jboolean nativeIsReadOnly(JNIEnv* env, jclass clazz, jlong connectionPtr,
   379    355           jlong statementPtr) {
   380         -    SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);
   381    356       sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
   382    357   
   383    358       return sqlite3_stmt_readonly(statement) != 0;
   384    359   }
   385    360   
   386    361   static jint nativeGetColumnCount(JNIEnv* env, jclass clazz, jlong connectionPtr,
   387    362           jlong statementPtr) {
   388         -    SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);
   389    363       sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
   390    364   
   391    365       return sqlite3_column_count(statement);
   392    366   }
   393    367   
   394    368   static jstring nativeGetColumnName(JNIEnv* env, jclass clazz, jlong connectionPtr,
   395    369           jlong statementPtr, jint index) {
   396         -    SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);
   397    370       sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);
   398    371   
   399    372       const jchar* name = static_cast<const jchar*>(sqlite3_column_name16(statement, index));
   400    373       if (name) {
   401    374           size_t length = 0;
   402    375           while (name[length]) {
   403    376               length += 1;
................................................................................
   554    527               return env->NewString(text, length);
   555    528           }
   556    529       }
   557    530       return NULL;
   558    531   }
   559    532   
   560    533   static int createAshmemRegionWithData(JNIEnv* env, const void* data, size_t length) {
   561         -#if 0
   562         -    int error = 0;
   563         -    int fd = ashmem_create_region(NULL, length);
   564         -    if (fd < 0) {
   565         -        error = errno;
   566         -        ALOGE("ashmem_create_region failed: %s", strerror(error));
   567         -    } else {
   568         -        if (length > 0) {
   569         -            void* ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
   570         -            if (ptr == MAP_FAILED) {
   571         -                error = errno;
   572         -                ALOGE("mmap failed: %s", strerror(error));
   573         -            } else {
   574         -                memcpy(ptr, data, length);
   575         -                munmap(ptr, length);
   576         -            }
   577         -        }
   578         -
   579         -        if (!error) {
   580         -            if (ashmem_set_prot_region(fd, PROT_READ) < 0) {
   581         -                error = errno;
   582         -                ALOGE("ashmem_set_prot_region failed: %s", strerror(errno));
   583         -            } else {
   584         -                return fd;
   585         -            }
   586         -        }
   587         -
   588         -        close(fd);
   589         -    }
   590         -
   591         -#endif
   592    534       jniThrowIOException(env, -1);
   593    535       return -1;
   594    536   }
   595    537   
   596    538   static jint nativeExecuteForBlobFileDescriptor(JNIEnv* env, jclass clazz,
   597    539           jlong connectionPtr, jlong statementPtr) {
   598    540       SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);

Changes to sqlite3/src/main/jni/sqlite/android_database_SQLiteGlobal.cpp.

    18     18   ** sqlite-dev@sqlite.org.
    19     19   */
    20     20   
    21     21   #define LOG_TAG "SQLiteGlobal"
    22     22   
    23     23   #include <jni.h>
    24     24   #include <JNIHelp.h>
    25         -#include "ALog-priv.h"
    26         -
    27     25   
    28     26   #include <sqlite3.h>
    29         -#if 0
    30         -#include <sqlite3_android.h>
    31         -#endif
    32         -
    33     27   
    34     28   #include "android_database_SQLiteCommon.h"
           29  +#include "ALog-priv.h"
    35     30   
    36     31   namespace android {
    37     32   
    38     33   // Limit heap to 8MB for now.  This is 4 times the maximum cursor window
    39     34   // size, as has been used by the original code in SQLiteDatabase for
    40     35   // a long time.
    41     36   static const int SOFT_HEAP_LIMIT = 8 * 1024 * 1024;
    42     37   
    43     38   
    44     39   // Called each time a message is logged.
    45         -static void sqliteLogCallback(void* data, int iErrCode, const char* zMsg) {
           40  +static void sqliteLogCallback(void* data, int err, const char* msg) {
    46     41       bool verboseLog = !!data;
    47         -    if (iErrCode == 0 || iErrCode == SQLITE_CONSTRAINT || iErrCode == SQLITE_SCHEMA) {
           42  +    int errType = err & 255;
           43  +    if (errType == 0 || errType == SQLITE_CONSTRAINT || errType == SQLITE_SCHEMA
           44  +            || errType == SQLITE_NOTICE || err == SQLITE_WARNING_AUTOINDEX) {
    48     45           if (verboseLog) {
    49         -            ALOG(LOG_VERBOSE, SQLITE_LOG_TAG, "(%d) %s\n", iErrCode, zMsg);
           46  +            ALOG(LOG_VERBOSE, SQLITE_LOG_TAG, "(%d) %s\n", err, msg);
    50     47           }
           48  +    } else if (errType == SQLITE_WARNING) {
           49  +        ALOG(LOG_WARN, SQLITE_LOG_TAG, "(%d) %s\n", err, msg);
    51     50       } else {
    52         -        ALOG(LOG_ERROR, SQLITE_LOG_TAG, "(%d) %s\n", iErrCode, zMsg);
           51  +        ALOG(LOG_ERROR, SQLITE_LOG_TAG, "(%d) %s\n", err, msg);
    53     52       }
    54     53   }
    55     54   
    56     55   // Sets the global SQLite configuration.
    57     56   // This must be called before any other SQLite functions are called.
    58     57   static void sqliteInitialize() {
    59     58       // Enable multi-threaded mode.  In this mode, SQLite is safe to use by multiple
    60     59       // threads as long as no two threads use the same database connection at the same
    61     60       // time (which we guarantee in the SQLite database wrappers).
    62     61       sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
    63     62   
    64     63       // Redirect SQLite log messages to the Android log.
    65         -#if 0
    66         -    bool verboseLog = android_util_Log_isVerboseLogEnabled(SQLITE_LOG_TAG);
    67         -#endif
    68     64       bool verboseLog = false;
    69     65       sqlite3_config(SQLITE_CONFIG_LOG, &sqliteLogCallback, verboseLog ? (void*)1 : NULL);
    70     66   
    71     67       // The soft heap limit prevents the page cache allocations from growing
    72     68       // beyond the given limit, no matter what the max page cache sizes are
    73     69       // set to. The limit does not, as of 3.5.0, affect any other allocations.
    74     70       sqlite3_soft_heap_limit(SOFT_HEAP_LIMIT);
................................................................................
    77     73       sqlite3_initialize();
    78     74   }
    79     75   
    80     76   static jint nativeReleaseMemory(JNIEnv* env, jclass clazz) {
    81     77       return sqlite3_release_memory(SOFT_HEAP_LIMIT);
    82     78   }
    83     79   
    84         -static JNINativeMethod sMethods[] =
           80  +static const JNINativeMethod sMethods[] =
    85     81   {
    86     82       /* name, signature, funcPtr */
    87         -    { "nativeReleaseMemory", "()I",
    88         -            (void*)nativeReleaseMemory },
           83  +    { "nativeReleaseMemory", "()I", (void*)nativeReleaseMemory },
    89     84   };
    90     85   
    91     86   int register_android_database_SQLiteGlobal(JNIEnv *env)
    92     87   {
    93     88       sqliteInitialize();
    94     89   
    95     90       return jniRegisterNativeMethods(env, "org/sqlite/database/sqlite/SQLiteGlobal",
    96     91               sMethods, NELEM(sMethods));
    97     92   }
    98     93   
    99     94   } // namespace android