Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2006 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 #undef LOG_TAG
     18 #define LOG_TAG "Cursor"
     19 
     20 #include <jni.h>
     21 #include <JNIHelp.h>
     22 #include <android_runtime/AndroidRuntime.h>
     23 
     24 #include <sqlite3.h>
     25 
     26 #include <utils/Log.h>
     27 
     28 #include <stdio.h>
     29 #include <string.h>
     30 #include <unistd.h>
     31 
     32 #include "CursorWindow.h"
     33 #include "sqlite3_exception.h"
     34 
     35 
     36 namespace android {
     37 
     38 sqlite3_stmt * compile(JNIEnv* env, jobject object,
     39                        sqlite3 * handle, jstring sqlString);
     40 
     41 // From android_database_CursorWindow.cpp
     42 CursorWindow * get_window_from_object(JNIEnv * env, jobject javaWindow);
     43 
     44 static jfieldID gHandleField;
     45 static jfieldID gStatementField;
     46 
     47 
     48 #define GET_STATEMENT(env, object) \
     49         (sqlite3_stmt *)env->GetIntField(object, gStatementField)
     50 #define GET_HANDLE(env, object) \
     51         (sqlite3 *)env->GetIntField(object, gHandleField)
     52 
     53 static int skip_rows(sqlite3_stmt *statement, int maxRows) {
     54     int retryCount = 0;
     55     for (int i = 0; i < maxRows; i++) {
     56         int err = sqlite3_step(statement);
     57         if (err == SQLITE_ROW){
     58             // do nothing
     59         } else if (err == SQLITE_DONE) {
     60             return i;
     61         } else if (err == SQLITE_LOCKED || err == SQLITE_BUSY) {
     62             // The table is locked, retry
     63             LOG_WINDOW("Database locked, retrying");
     64            if (retryCount > 50) {
     65                 LOGE("Bailing on database busy rety");
     66                 break;
     67             }
     68             // Sleep to give the thread holding the lock a chance to finish
     69             usleep(1000);
     70             retryCount++;
     71             continue;
     72         } else {
     73             return -1;
     74         }
     75     }
     76     LOGD("skip_rows row %d", maxRows);
     77     return maxRows;
     78 }
     79 
     80 static int finish_program_and_get_row_count(sqlite3_stmt *statement) {
     81     int numRows = 0;
     82     int retryCount = 0;
     83     while (true) {
     84         int err = sqlite3_step(statement);
     85         if (err == SQLITE_ROW){
     86             numRows++;
     87         } else if (err == SQLITE_LOCKED || err == SQLITE_BUSY) {
     88             // The table is locked, retry
     89             LOG_WINDOW("Database locked, retrying");
     90             if (retryCount > 50) {
     91                 LOGE("Bailing on database busy rety");
     92                 break;
     93             }
     94             // Sleep to give the thread holding the lock a chance to finish
     95             usleep(1000);
     96             retryCount++;
     97             continue;
     98         } else {
     99             // no need to throw exception
    100             break;
    101         }
    102     }
    103     sqlite3_reset(statement);
    104     LOGD("finish_program_and_get_row_count row %d", numRows);
    105     return numRows;
    106 }
    107 
    108 static jint native_fill_window(JNIEnv* env, jobject object, jobject javaWindow,
    109                                jint startPos, jint offsetParam, jint maxRead, jint lastPos)
    110 {
    111     int err;
    112     sqlite3_stmt * statement = GET_STATEMENT(env, object);
    113     int numRows = lastPos;
    114     maxRead += lastPos;
    115     int numColumns;
    116     int retryCount;
    117     int boundParams;
    118     CursorWindow * window;
    119 
    120     if (statement == NULL) {
    121         LOGE("Invalid statement in fillWindow()");
    122         jniThrowException(env, "java/lang/IllegalStateException",
    123                           "Attempting to access a deactivated, closed, or empty cursor");
    124         return 0;
    125     }
    126 
    127     // Only do the binding if there is a valid offsetParam. If no binding needs to be done
    128     // offsetParam will be set to 0, an invliad value.
    129     if(offsetParam > 0) {
    130         // Bind the offset parameter, telling the program which row to start with
    131         err = sqlite3_bind_int(statement, offsetParam, startPos);
    132         if (err != SQLITE_OK) {
    133             LOGE("Unable to bind offset position, offsetParam = %d", offsetParam);
    134             jniThrowException(env, "java/lang/IllegalArgumentException",
    135                               sqlite3_errmsg(GET_HANDLE(env, object)));
    136             return 0;
    137         }
    138         LOG_WINDOW("Bound to startPos %d", startPos);
    139     } else {
    140         LOG_WINDOW("Not binding to startPos %d", startPos);
    141     }
    142 
    143     // Get the native window
    144     window = get_window_from_object(env, javaWindow);
    145     if (!window) {
    146         LOGE("Invalid CursorWindow");
    147         jniThrowException(env, "java/lang/IllegalArgumentException",
    148                           "Bad CursorWindow");
    149         return 0;
    150     }
    151     LOG_WINDOW("Window: numRows = %d, size = %d, freeSpace = %d", window->getNumRows(), window->size(), window->freeSpace());
    152 
    153     numColumns = sqlite3_column_count(statement);
    154     if (!window->setNumColumns(numColumns)) {
    155         LOGE("Failed to change column count from %d to %d", window->getNumColumns(), numColumns);
    156         jniThrowException(env, "java/lang/IllegalStateException", "numColumns mismatch");
    157         return 0;
    158     }
    159 
    160     retryCount = 0;
    161     if (startPos > 0) {
    162         int num = skip_rows(statement, startPos);
    163         if (num < 0) {
    164             throw_sqlite3_exception(env, GET_HANDLE(env, object));
    165             return 0;
    166         } else if (num < startPos) {
    167             LOGE("startPos %d > actual rows %d", startPos, num);
    168             return num;
    169         }
    170     }
    171 
    172     while(startPos != 0 || numRows < maxRead) {
    173         err = sqlite3_step(statement);
    174         if (err == SQLITE_ROW) {
    175             LOG_WINDOW("\nStepped statement %p to row %d", statement, startPos + numRows);
    176             retryCount = 0;
    177 
    178             // Allocate a new field directory for the row. This pointer is not reused
    179             // since it mey be possible for it to be relocated on a call to alloc() when
    180             // the field data is being allocated.
    181             {
    182                 field_slot_t * fieldDir = window->allocRow();
    183                 if (!fieldDir) {
    184                     LOGE("Failed allocating fieldDir at startPos %d row %d", startPos, numRows);
    185                     return startPos + numRows + finish_program_and_get_row_count(statement) + 1;
    186                 }
    187             }
    188 
    189             // Pack the row into the window
    190             int i;
    191             for (i = 0; i < numColumns; i++) {
    192                 int type = sqlite3_column_type(statement, i);
    193                 if (type == SQLITE_TEXT) {
    194                     // TEXT data
    195 #if WINDOW_STORAGE_UTF8
    196                     uint8_t const * text = (uint8_t const *)sqlite3_column_text(statement, i);
    197                     // SQLite does not include the NULL terminator in size, but does
    198                     // ensure all strings are NULL terminated, so increase size by
    199                     // one to make sure we store the terminator.
    200                     size_t size = sqlite3_column_bytes(statement, i) + 1;
    201 #else
    202                     uint8_t const * text = (uint8_t const *)sqlite3_column_text16(statement, i);
    203                     size_t size = sqlite3_column_bytes16(statement, i);
    204 #endif
    205                     int offset = window->alloc(size);
    206                     if (!offset) {
    207                         window->freeLastRow();
    208                         LOGE("Failed allocating %u bytes for text/blob at %d,%d", size,
    209                                    startPos + numRows, i);
    210                         return startPos + numRows + finish_program_and_get_row_count(statement) + 1;
    211                     }
    212 
    213                     window->copyIn(offset, text, size);
    214 
    215                     // This must be updated after the call to alloc(), since that
    216                     // may move the field around in the window
    217                     field_slot_t * fieldSlot = window->getFieldSlot(numRows, i);
    218                     fieldSlot->type = FIELD_TYPE_STRING;
    219                     fieldSlot->data.buffer.offset = offset;
    220                     fieldSlot->data.buffer.size = size;
    221 
    222                     LOG_WINDOW("%d,%d is TEXT with %u bytes", startPos + numRows, i, size);
    223                 } else if (type == SQLITE_INTEGER) {
    224                     // INTEGER data
    225                     int64_t value = sqlite3_column_int64(statement, i);
    226                     if (!window->putLong(numRows, i, value)) {
    227                         window->freeLastRow();
    228                         LOGE("Failed allocating space for a long in column %d", i);
    229                         return startPos + numRows + finish_program_and_get_row_count(statement) + 1;
    230                     }
    231                     LOG_WINDOW("%d,%d is INTEGER 0x%016llx", startPos + numRows, i, value);
    232                 } else if (type == SQLITE_FLOAT) {
    233                     // FLOAT data
    234                     double value = sqlite3_column_double(statement, i);
    235                     if (!window->putDouble(numRows, i, value)) {
    236                         window->freeLastRow();
    237                         LOGE("Failed allocating space for a double in column %d", i);
    238                         return startPos + numRows + finish_program_and_get_row_count(statement) + 1;
    239                     }
    240                     LOG_WINDOW("%d,%d is FLOAT %lf", startPos + numRows, i, value);
    241                 } else if (type == SQLITE_BLOB) {
    242                     // BLOB data
    243                     uint8_t const * blob = (uint8_t const *)sqlite3_column_blob(statement, i);
    244                     size_t size = sqlite3_column_bytes16(statement, i);
    245                     int offset = window->alloc(size);
    246                     if (!offset) {
    247                         window->freeLastRow();
    248                         LOGE("Failed allocating %u bytes for blob at %d,%d", size,
    249                                    startPos + numRows, i);
    250                         return startPos + numRows + finish_program_and_get_row_count(statement) + 1;
    251                     }
    252 
    253                     window->copyIn(offset, blob, size);
    254 
    255                     // This must be updated after the call to alloc(), since that
    256                     // may move the field around in the window
    257                     field_slot_t * fieldSlot = window->getFieldSlot(numRows, i);
    258                     fieldSlot->type = FIELD_TYPE_BLOB;
    259                     fieldSlot->data.buffer.offset = offset;
    260                     fieldSlot->data.buffer.size = size;
    261 
    262                     LOG_WINDOW("%d,%d is Blob with %u bytes @ %d", startPos + numRows, i, size, offset);
    263                 } else if (type == SQLITE_NULL) {
    264                     // NULL field
    265                     window->putNull(numRows, i);
    266 
    267                     LOG_WINDOW("%d,%d is NULL", startPos + numRows, i);
    268                 } else {
    269                     // Unknown data
    270                     LOGE("Unknown column type when filling database window");
    271                     throw_sqlite3_exception(env, "Unknown column type when filling window");
    272                     break;
    273                 }
    274             }
    275 
    276             if (i < numColumns) {
    277                 // Not all the fields fit in the window
    278                 // Unknown data error happened
    279                 break;
    280             }
    281 
    282             // Mark the row as complete in the window
    283             numRows++;
    284         } else if (err == SQLITE_DONE) {
    285             // All rows processed, bail
    286             LOG_WINDOW("Processed all rows");
    287             break;
    288         } else if (err == SQLITE_LOCKED || err == SQLITE_BUSY) {
    289             // The table is locked, retry
    290             LOG_WINDOW("Database locked, retrying");
    291             if (retryCount > 50) {
    292                 LOGE("Bailing on database busy rety");
    293                 break;
    294             }
    295 
    296             // Sleep to give the thread holding the lock a chance to finish
    297             usleep(1000);
    298 
    299             retryCount++;
    300             continue;
    301         } else {
    302             throw_sqlite3_exception(env, GET_HANDLE(env, object));
    303             break;
    304         }
    305     }
    306 
    307     LOG_WINDOW("Resetting statement %p after fetching %d rows in %d bytes\n\n\n\n", statement,
    308             numRows, window->size() - window->freeSpace());
    309 //    LOGI("Filled window with %d rows in %d bytes", numRows, window->size() - window->freeSpace());
    310     if (err == SQLITE_ROW) {
    311         return -1;
    312     } else {
    313         sqlite3_reset(statement);
    314         return startPos + numRows;
    315     }
    316 }
    317 
    318 static jint native_column_count(JNIEnv* env, jobject object)
    319 {
    320     sqlite3_stmt * statement = GET_STATEMENT(env, object);
    321 
    322     return sqlite3_column_count(statement);
    323 }
    324 
    325 static jstring native_column_name(JNIEnv* env, jobject object, jint columnIndex)
    326 {
    327     sqlite3_stmt * statement = GET_STATEMENT(env, object);
    328     char const * name;
    329 
    330     name = sqlite3_column_name(statement, columnIndex);
    331 
    332     return env->NewStringUTF(name);
    333 }
    334 
    335 
    336 static JNINativeMethod sMethods[] =
    337 {
    338      /* name, signature, funcPtr */
    339     {"native_fill_window", "(Landroid/database/CursorWindow;IIII)I", (void *)native_fill_window},
    340     {"native_column_count", "()I", (void*)native_column_count},
    341     {"native_column_name", "(I)Ljava/lang/String;", (void *)native_column_name},
    342 };
    343 
    344 int register_android_database_SQLiteQuery(JNIEnv * env)
    345 {
    346     jclass clazz;
    347 
    348     clazz = env->FindClass("android/database/sqlite/SQLiteQuery");
    349     if (clazz == NULL) {
    350         LOGE("Can't find android/database/sqlite/SQLiteQuery");
    351         return -1;
    352     }
    353 
    354     gHandleField = env->GetFieldID(clazz, "nHandle", "I");
    355     gStatementField = env->GetFieldID(clazz, "nStatement", "I");
    356 
    357     if (gHandleField == NULL || gStatementField == NULL) {
    358         LOGE("Error locating fields");
    359         return -1;
    360     }
    361 
    362     return AndroidRuntime::registerNativeMethods(env,
    363         "android/database/sqlite/SQLiteQuery", sMethods, NELEM(sMethods));
    364 }
    365 
    366 } // namespace android
    367