android_database_SQLiteConnection.cpp revision 0f0b4919667f418b249c497f5ad3e83fdf4437e5
1/* 2 * Copyright (C) 2011 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 "SQLiteConnection" 18 19#include <jni.h> 20#include <JNIHelp.h> 21#include <android_runtime/AndroidRuntime.h> 22#include <android_runtime/Log.h> 23 24#include <utils/Log.h> 25#include <utils/String8.h> 26#include <utils/String16.h> 27#include <cutils/ashmem.h> 28#include <sys/mman.h> 29 30#include <string.h> 31#include <unistd.h> 32 33#include <androidfw/CursorWindow.h> 34 35#include <sqlite3.h> 36#include <sqlite3_android.h> 37 38#include "android_database_SQLiteCommon.h" 39 40// Set to 1 to use UTF16 storage for localized indexes. 41#define UTF16_STORAGE 0 42 43namespace android { 44 45/* Busy timeout in milliseconds. 46 * If another connection (possibly in another process) has the database locked for 47 * longer than this amount of time then SQLite will generate a SQLITE_BUSY error. 48 * The SQLITE_BUSY error is then raised as a SQLiteDatabaseLockedException. 49 * 50 * In ordinary usage, busy timeouts are quite rare. Most databases only ever 51 * have a single open connection at a time unless they are using WAL. When using 52 * WAL, a timeout could occur if one connection is busy performing an auto-checkpoint 53 * operation. The busy timeout needs to be long enough to tolerate slow I/O write 54 * operations but not so long as to cause the application to hang indefinitely if 55 * there is a problem acquiring a database lock. 56 */ 57static const int BUSY_TIMEOUT_MS = 2500; 58 59static struct { 60 jfieldID name; 61 jfieldID numArgs; 62 jmethodID dispatchCallback; 63} gSQLiteCustomFunctionClassInfo; 64 65static struct { 66 jclass clazz; 67} gStringClassInfo; 68 69struct SQLiteConnection { 70 // Open flags. 71 // Must be kept in sync with the constants defined in SQLiteDatabase.java. 72 enum { 73 OPEN_READWRITE = 0x00000000, 74 OPEN_READONLY = 0x00000001, 75 OPEN_READ_MASK = 0x00000001, 76 NO_LOCALIZED_COLLATORS = 0x00000010, 77 CREATE_IF_NECESSARY = 0x10000000, 78 }; 79 80 sqlite3* const db; 81 const int openFlags; 82 const String8 path; 83 const String8 label; 84 85 volatile bool canceled; 86 87 SQLiteConnection(sqlite3* db, int openFlags, const String8& path, const String8& label) : 88 db(db), openFlags(openFlags), path(path), label(label), canceled(false) { } 89}; 90 91// Called each time a statement begins execution, when tracing is enabled. 92static void sqliteTraceCallback(void *data, const char *sql) { 93 SQLiteConnection* connection = static_cast<SQLiteConnection*>(data); 94 ALOG(LOG_VERBOSE, SQLITE_TRACE_TAG, "%s: \"%s\"\n", 95 connection->label.string(), sql); 96} 97 98// Called each time a statement finishes execution, when profiling is enabled. 99static void sqliteProfileCallback(void *data, const char *sql, sqlite3_uint64 tm) { 100 SQLiteConnection* connection = static_cast<SQLiteConnection*>(data); 101 ALOG(LOG_VERBOSE, SQLITE_PROFILE_TAG, "%s: \"%s\" took %0.3f ms\n", 102 connection->label.string(), sql, tm * 0.000001f); 103} 104 105// Called after each SQLite VM instruction when cancelation is enabled. 106static int sqliteProgressHandlerCallback(void* data) { 107 SQLiteConnection* connection = static_cast<SQLiteConnection*>(data); 108 return connection->canceled; 109} 110 111 112static jlong nativeOpen(JNIEnv* env, jclass clazz, jstring pathStr, jint openFlags, 113 jstring labelStr, jboolean enableTrace, jboolean enableProfile) { 114 int sqliteFlags; 115 if (openFlags & SQLiteConnection::CREATE_IF_NECESSARY) { 116 sqliteFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; 117 } else if (openFlags & SQLiteConnection::OPEN_READONLY) { 118 sqliteFlags = SQLITE_OPEN_READONLY; 119 } else { 120 sqliteFlags = SQLITE_OPEN_READWRITE; 121 } 122 123 const char* pathChars = env->GetStringUTFChars(pathStr, NULL); 124 String8 path(pathChars); 125 env->ReleaseStringUTFChars(pathStr, pathChars); 126 127 const char* labelChars = env->GetStringUTFChars(labelStr, NULL); 128 String8 label(labelChars); 129 env->ReleaseStringUTFChars(labelStr, labelChars); 130 131 sqlite3* db; 132 int err = sqlite3_open_v2(path.string(), &db, sqliteFlags, NULL); 133 if (err != SQLITE_OK) { 134 throw_sqlite3_exception_errcode(env, err, "Could not open database"); 135 return 0; 136 } 137 138 // Check that the database is really read/write when that is what we asked for. 139 if ((sqliteFlags & SQLITE_OPEN_READWRITE) && sqlite3_db_readonly(db, NULL)) { 140 throw_sqlite3_exception(env, db, "Could not open the database in read/write mode."); 141 sqlite3_close(db); 142 return 0; 143 } 144 145 // Set the default busy handler to retry automatically before returning SQLITE_BUSY. 146 err = sqlite3_busy_timeout(db, BUSY_TIMEOUT_MS); 147 if (err != SQLITE_OK) { 148 throw_sqlite3_exception(env, db, "Could not set busy timeout"); 149 sqlite3_close(db); 150 return 0; 151 } 152 153 // Register custom Android functions. 154 err = register_android_functions(db, UTF16_STORAGE); 155 if (err) { 156 throw_sqlite3_exception(env, db, "Could not register Android SQL functions."); 157 sqlite3_close(db); 158 return 0; 159 } 160 161 // Create wrapper object. 162 SQLiteConnection* connection = new SQLiteConnection(db, openFlags, path, label); 163 164 // Enable tracing and profiling if requested. 165 if (enableTrace) { 166 sqlite3_trace(db, &sqliteTraceCallback, connection); 167 } 168 if (enableProfile) { 169 sqlite3_profile(db, &sqliteProfileCallback, connection); 170 } 171 172 ALOGV("Opened connection %p with label '%s'", db, label.string()); 173 return reinterpret_cast<jlong>(connection); 174} 175 176static void nativeClose(JNIEnv* env, jclass clazz, jlong connectionPtr) { 177 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 178 179 if (connection) { 180 ALOGV("Closing connection %p", connection->db); 181 int err = sqlite3_close(connection->db); 182 if (err != SQLITE_OK) { 183 // This can happen if sub-objects aren't closed first. Make sure the caller knows. 184 ALOGE("sqlite3_close(%p) failed: %d", connection->db, err); 185 throw_sqlite3_exception(env, connection->db, "Count not close db."); 186 return; 187 } 188 189 delete connection; 190 } 191} 192 193// Called each time a custom function is evaluated. 194static void sqliteCustomFunctionCallback(sqlite3_context *context, 195 int argc, sqlite3_value **argv) { 196 JNIEnv* env = AndroidRuntime::getJNIEnv(); 197 198 // Get the callback function object. 199 // Create a new local reference to it in case the callback tries to do something 200 // dumb like unregister the function (thereby destroying the global ref) while it is running. 201 jobject functionObjGlobal = reinterpret_cast<jobject>(sqlite3_user_data(context)); 202 jobject functionObj = env->NewLocalRef(functionObjGlobal); 203 204 jobjectArray argsArray = env->NewObjectArray(argc, gStringClassInfo.clazz, NULL); 205 if (argsArray) { 206 for (int i = 0; i < argc; i++) { 207 const jchar* arg = static_cast<const jchar*>(sqlite3_value_text16(argv[i])); 208 if (!arg) { 209 ALOGW("NULL argument in custom_function_callback. This should not happen."); 210 } else { 211 size_t argLen = sqlite3_value_bytes16(argv[i]) / sizeof(jchar); 212 jstring argStr = env->NewString(arg, argLen); 213 if (!argStr) { 214 goto error; // out of memory error 215 } 216 env->SetObjectArrayElement(argsArray, i, argStr); 217 env->DeleteLocalRef(argStr); 218 } 219 } 220 221 // TODO: Support functions that return values. 222 env->CallVoidMethod(functionObj, 223 gSQLiteCustomFunctionClassInfo.dispatchCallback, argsArray); 224 225error: 226 env->DeleteLocalRef(argsArray); 227 } 228 229 env->DeleteLocalRef(functionObj); 230 231 if (env->ExceptionCheck()) { 232 ALOGE("An exception was thrown by custom SQLite function."); 233 LOGE_EX(env); 234 env->ExceptionClear(); 235 } 236} 237 238// Called when a custom function is destroyed. 239static void sqliteCustomFunctionDestructor(void* data) { 240 jobject functionObjGlobal = reinterpret_cast<jobject>(data); 241 242 JNIEnv* env = AndroidRuntime::getJNIEnv(); 243 env->DeleteGlobalRef(functionObjGlobal); 244} 245 246static void nativeRegisterCustomFunction(JNIEnv* env, jclass clazz, jlong connectionPtr, 247 jobject functionObj) { 248 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 249 250 jstring nameStr = jstring(env->GetObjectField( 251 functionObj, gSQLiteCustomFunctionClassInfo.name)); 252 jint numArgs = env->GetIntField(functionObj, gSQLiteCustomFunctionClassInfo.numArgs); 253 254 jobject functionObjGlobal = env->NewGlobalRef(functionObj); 255 256 const char* name = env->GetStringUTFChars(nameStr, NULL); 257 int err = sqlite3_create_function_v2(connection->db, name, numArgs, SQLITE_UTF16, 258 reinterpret_cast<void*>(functionObjGlobal), 259 &sqliteCustomFunctionCallback, NULL, NULL, &sqliteCustomFunctionDestructor); 260 env->ReleaseStringUTFChars(nameStr, name); 261 262 if (err != SQLITE_OK) { 263 ALOGE("sqlite3_create_function returned %d", err); 264 env->DeleteGlobalRef(functionObjGlobal); 265 throw_sqlite3_exception(env, connection->db); 266 return; 267 } 268} 269 270static void nativeRegisterLocalizedCollators(JNIEnv* env, jclass clazz, jlong connectionPtr, 271 jstring localeStr) { 272 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 273 274 const char* locale = env->GetStringUTFChars(localeStr, NULL); 275 int err = register_localized_collators(connection->db, locale, UTF16_STORAGE); 276 env->ReleaseStringUTFChars(localeStr, locale); 277 278 if (err != SQLITE_OK) { 279 throw_sqlite3_exception(env, connection->db); 280 } 281} 282 283static jlong nativePrepareStatement(JNIEnv* env, jclass clazz, jlong connectionPtr, 284 jstring sqlString) { 285 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 286 287 jsize sqlLength = env->GetStringLength(sqlString); 288 const jchar* sql = env->GetStringCritical(sqlString, NULL); 289 sqlite3_stmt* statement; 290 int err = sqlite3_prepare16_v2(connection->db, 291 sql, sqlLength * sizeof(jchar), &statement, NULL); 292 env->ReleaseStringCritical(sqlString, sql); 293 294 if (err != SQLITE_OK) { 295 // Error messages like 'near ")": syntax error' are not 296 // always helpful enough, so construct an error string that 297 // includes the query itself. 298 const char *query = env->GetStringUTFChars(sqlString, NULL); 299 char *message = (char*) malloc(strlen(query) + 50); 300 if (message) { 301 strcpy(message, ", while compiling: "); // less than 50 chars 302 strcat(message, query); 303 } 304 env->ReleaseStringUTFChars(sqlString, query); 305 throw_sqlite3_exception(env, connection->db, message); 306 free(message); 307 return 0; 308 } 309 310 ALOGV("Prepared statement %p on connection %p", statement, connection->db); 311 return reinterpret_cast<jlong>(statement); 312} 313 314static void nativeFinalizeStatement(JNIEnv* env, jclass clazz, jlong connectionPtr, 315 jlong statementPtr) { 316 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 317 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 318 319 // We ignore the result of sqlite3_finalize because it is really telling us about 320 // whether any errors occurred while executing the statement. The statement itself 321 // is always finalized regardless. 322 ALOGV("Finalized statement %p on connection %p", statement, connection->db); 323 sqlite3_finalize(statement); 324} 325 326static jint nativeGetParameterCount(JNIEnv* env, jclass clazz, jlong connectionPtr, 327 jlong statementPtr) { 328 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 329 330 return sqlite3_bind_parameter_count(statement); 331} 332 333static jboolean nativeIsReadOnly(JNIEnv* env, jclass clazz, jlong connectionPtr, 334 jlong statementPtr) { 335 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 336 337 return sqlite3_stmt_readonly(statement) != 0; 338} 339 340static jint nativeGetColumnCount(JNIEnv* env, jclass clazz, jlong connectionPtr, 341 jlong statementPtr) { 342 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 343 344 return sqlite3_column_count(statement); 345} 346 347static jstring nativeGetColumnName(JNIEnv* env, jclass clazz, jlong connectionPtr, 348 jlong statementPtr, jint index) { 349 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 350 351 const jchar* name = static_cast<const jchar*>(sqlite3_column_name16(statement, index)); 352 if (name) { 353 size_t length = 0; 354 while (name[length]) { 355 length += 1; 356 } 357 return env->NewString(name, length); 358 } 359 return NULL; 360} 361 362static void nativeBindNull(JNIEnv* env, jclass clazz, jlong connectionPtr, 363 jlong statementPtr, jint index) { 364 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 365 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 366 367 int err = sqlite3_bind_null(statement, index); 368 if (err != SQLITE_OK) { 369 throw_sqlite3_exception(env, connection->db, NULL); 370 } 371} 372 373static void nativeBindLong(JNIEnv* env, jclass clazz, jlong connectionPtr, 374 jlong statementPtr, jint index, jlong value) { 375 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 376 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 377 378 int err = sqlite3_bind_int64(statement, index, value); 379 if (err != SQLITE_OK) { 380 throw_sqlite3_exception(env, connection->db, NULL); 381 } 382} 383 384static void nativeBindDouble(JNIEnv* env, jclass clazz, jlong connectionPtr, 385 jlong statementPtr, jint index, jdouble value) { 386 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 387 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 388 389 int err = sqlite3_bind_double(statement, index, value); 390 if (err != SQLITE_OK) { 391 throw_sqlite3_exception(env, connection->db, NULL); 392 } 393} 394 395static void nativeBindString(JNIEnv* env, jclass clazz, jlong connectionPtr, 396 jlong statementPtr, jint index, jstring valueString) { 397 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 398 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 399 400 jsize valueLength = env->GetStringLength(valueString); 401 const jchar* value = env->GetStringCritical(valueString, NULL); 402 int err = sqlite3_bind_text16(statement, index, value, valueLength * sizeof(jchar), 403 SQLITE_TRANSIENT); 404 env->ReleaseStringCritical(valueString, value); 405 if (err != SQLITE_OK) { 406 throw_sqlite3_exception(env, connection->db, NULL); 407 } 408} 409 410static void nativeBindBlob(JNIEnv* env, jclass clazz, jlong connectionPtr, 411 jlong statementPtr, jint index, jbyteArray valueArray) { 412 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 413 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 414 415 jsize valueLength = env->GetArrayLength(valueArray); 416 jbyte* value = static_cast<jbyte*>(env->GetPrimitiveArrayCritical(valueArray, NULL)); 417 int err = sqlite3_bind_blob(statement, index, value, valueLength, SQLITE_TRANSIENT); 418 env->ReleasePrimitiveArrayCritical(valueArray, value, JNI_ABORT); 419 if (err != SQLITE_OK) { 420 throw_sqlite3_exception(env, connection->db, NULL); 421 } 422} 423 424static void nativeResetStatementAndClearBindings(JNIEnv* env, jclass clazz, jlong connectionPtr, 425 jlong statementPtr) { 426 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 427 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 428 429 int err = sqlite3_reset(statement); 430 if (err == SQLITE_OK) { 431 err = sqlite3_clear_bindings(statement); 432 } 433 if (err != SQLITE_OK) { 434 throw_sqlite3_exception(env, connection->db, NULL); 435 } 436} 437 438static int executeNonQuery(JNIEnv* env, SQLiteConnection* connection, sqlite3_stmt* statement) { 439 int err = sqlite3_step(statement); 440 if (err == SQLITE_ROW) { 441 throw_sqlite3_exception(env, 442 "Queries can be performed using SQLiteDatabase query or rawQuery methods only."); 443 } else if (err != SQLITE_DONE) { 444 throw_sqlite3_exception(env, connection->db); 445 } 446 return err; 447} 448 449static void nativeExecute(JNIEnv* env, jclass clazz, jlong connectionPtr, 450 jlong statementPtr) { 451 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 452 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 453 454 executeNonQuery(env, connection, statement); 455} 456 457static jint nativeExecuteForChangedRowCount(JNIEnv* env, jclass clazz, 458 jlong connectionPtr, jlong statementPtr) { 459 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 460 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 461 462 int err = executeNonQuery(env, connection, statement); 463 return err == SQLITE_DONE ? sqlite3_changes(connection->db) : -1; 464} 465 466static jlong nativeExecuteForLastInsertedRowId(JNIEnv* env, jclass clazz, 467 jlong connectionPtr, jlong statementPtr) { 468 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 469 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 470 471 int err = executeNonQuery(env, connection, statement); 472 return err == SQLITE_DONE && sqlite3_changes(connection->db) > 0 473 ? sqlite3_last_insert_rowid(connection->db) : -1; 474} 475 476static int executeOneRowQuery(JNIEnv* env, SQLiteConnection* connection, sqlite3_stmt* statement) { 477 int err = sqlite3_step(statement); 478 if (err != SQLITE_ROW) { 479 throw_sqlite3_exception(env, connection->db); 480 } 481 return err; 482} 483 484static jlong nativeExecuteForLong(JNIEnv* env, jclass clazz, 485 jlong connectionPtr, jlong statementPtr) { 486 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 487 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 488 489 int err = executeOneRowQuery(env, connection, statement); 490 if (err == SQLITE_ROW && sqlite3_column_count(statement) >= 1) { 491 return sqlite3_column_int64(statement, 0); 492 } 493 return -1; 494} 495 496static jstring nativeExecuteForString(JNIEnv* env, jclass clazz, 497 jlong connectionPtr, jlong statementPtr) { 498 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 499 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 500 501 int err = executeOneRowQuery(env, connection, statement); 502 if (err == SQLITE_ROW && sqlite3_column_count(statement) >= 1) { 503 const jchar* text = static_cast<const jchar*>(sqlite3_column_text16(statement, 0)); 504 if (text) { 505 size_t length = sqlite3_column_bytes16(statement, 0) / sizeof(jchar); 506 return env->NewString(text, length); 507 } 508 } 509 return NULL; 510} 511 512static int createAshmemRegionWithData(JNIEnv* env, const void* data, size_t length) { 513 int error = 0; 514 int fd = ashmem_create_region(NULL, length); 515 if (fd < 0) { 516 error = errno; 517 ALOGE("ashmem_create_region failed: %s", strerror(error)); 518 } else { 519 if (length > 0) { 520 void* ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 521 if (ptr == MAP_FAILED) { 522 error = errno; 523 ALOGE("mmap failed: %s", strerror(error)); 524 } else { 525 memcpy(ptr, data, length); 526 munmap(ptr, length); 527 } 528 } 529 530 if (!error) { 531 if (ashmem_set_prot_region(fd, PROT_READ) < 0) { 532 error = errno; 533 ALOGE("ashmem_set_prot_region failed: %s", strerror(errno)); 534 } else { 535 return fd; 536 } 537 } 538 539 close(fd); 540 } 541 542 jniThrowIOException(env, error); 543 return -1; 544} 545 546static jint nativeExecuteForBlobFileDescriptor(JNIEnv* env, jclass clazz, 547 jlong connectionPtr, jlong statementPtr) { 548 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 549 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 550 551 int err = executeOneRowQuery(env, connection, statement); 552 if (err == SQLITE_ROW && sqlite3_column_count(statement) >= 1) { 553 const void* blob = sqlite3_column_blob(statement, 0); 554 if (blob) { 555 int length = sqlite3_column_bytes(statement, 0); 556 if (length >= 0) { 557 return createAshmemRegionWithData(env, blob, length); 558 } 559 } 560 } 561 return -1; 562} 563 564enum CopyRowResult { 565 CPR_OK, 566 CPR_FULL, 567 CPR_ERROR, 568}; 569 570static CopyRowResult copyRow(JNIEnv* env, CursorWindow* window, 571 sqlite3_stmt* statement, int numColumns, int startPos, int addedRows) { 572 // Allocate a new field directory for the row. 573 status_t status = window->allocRow(); 574 if (status) { 575 LOG_WINDOW("Failed allocating fieldDir at startPos %d row %d, error=%d", 576 startPos, addedRows, status); 577 return CPR_FULL; 578 } 579 580 // Pack the row into the window. 581 CopyRowResult result = CPR_OK; 582 for (int i = 0; i < numColumns; i++) { 583 int type = sqlite3_column_type(statement, i); 584 if (type == SQLITE_TEXT) { 585 // TEXT data 586 const char* text = reinterpret_cast<const char*>( 587 sqlite3_column_text(statement, i)); 588 // SQLite does not include the NULL terminator in size, but does 589 // ensure all strings are NULL terminated, so increase size by 590 // one to make sure we store the terminator. 591 size_t sizeIncludingNull = sqlite3_column_bytes(statement, i) + 1; 592 status = window->putString(addedRows, i, text, sizeIncludingNull); 593 if (status) { 594 LOG_WINDOW("Failed allocating %u bytes for text at %d,%d, error=%d", 595 sizeIncludingNull, startPos + addedRows, i, status); 596 result = CPR_FULL; 597 break; 598 } 599 LOG_WINDOW("%d,%d is TEXT with %u bytes", 600 startPos + addedRows, i, sizeIncludingNull); 601 } else if (type == SQLITE_INTEGER) { 602 // INTEGER data 603 int64_t value = sqlite3_column_int64(statement, i); 604 status = window->putLong(addedRows, i, value); 605 if (status) { 606 LOG_WINDOW("Failed allocating space for a long in column %d, error=%d", 607 i, status); 608 result = CPR_FULL; 609 break; 610 } 611 LOG_WINDOW("%d,%d is INTEGER 0x%016llx", startPos + addedRows, i, value); 612 } else if (type == SQLITE_FLOAT) { 613 // FLOAT data 614 double value = sqlite3_column_double(statement, i); 615 status = window->putDouble(addedRows, i, value); 616 if (status) { 617 LOG_WINDOW("Failed allocating space for a double in column %d, error=%d", 618 i, status); 619 result = CPR_FULL; 620 break; 621 } 622 LOG_WINDOW("%d,%d is FLOAT %lf", startPos + addedRows, i, value); 623 } else if (type == SQLITE_BLOB) { 624 // BLOB data 625 const void* blob = sqlite3_column_blob(statement, i); 626 size_t size = sqlite3_column_bytes(statement, i); 627 status = window->putBlob(addedRows, i, blob, size); 628 if (status) { 629 LOG_WINDOW("Failed allocating %u bytes for blob at %d,%d, error=%d", 630 size, startPos + addedRows, i, status); 631 result = CPR_FULL; 632 break; 633 } 634 LOG_WINDOW("%d,%d is Blob with %u bytes", 635 startPos + addedRows, i, size); 636 } else if (type == SQLITE_NULL) { 637 // NULL field 638 status = window->putNull(addedRows, i); 639 if (status) { 640 LOG_WINDOW("Failed allocating space for a null in column %d, error=%d", 641 i, status); 642 result = CPR_FULL; 643 break; 644 } 645 646 LOG_WINDOW("%d,%d is NULL", startPos + addedRows, i); 647 } else { 648 // Unknown data 649 ALOGE("Unknown column type when filling database window"); 650 throw_sqlite3_exception(env, "Unknown column type when filling window"); 651 result = CPR_ERROR; 652 break; 653 } 654 } 655 656 // Free the last row if if was not successfully copied. 657 if (result != CPR_OK) { 658 window->freeLastRow(); 659 } 660 return result; 661} 662 663static jlong nativeExecuteForCursorWindow(JNIEnv* env, jclass clazz, 664 jlong connectionPtr, jlong statementPtr, jlong windowPtr, 665 jint startPos, jint requiredPos, jboolean countAllRows) { 666 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 667 sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); 668 CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); 669 670 status_t status = window->clear(); 671 if (status) { 672 String8 msg; 673 msg.appendFormat("Failed to clear the cursor window, status=%d", status); 674 throw_sqlite3_exception(env, connection->db, msg.string()); 675 return 0; 676 } 677 678 int numColumns = sqlite3_column_count(statement); 679 status = window->setNumColumns(numColumns); 680 if (status) { 681 String8 msg; 682 msg.appendFormat("Failed to set the cursor window column count to %d, status=%d", 683 numColumns, status); 684 throw_sqlite3_exception(env, connection->db, msg.string()); 685 return 0; 686 } 687 688 int retryCount = 0; 689 int totalRows = 0; 690 int addedRows = 0; 691 bool windowFull = false; 692 bool gotException = false; 693 while (!gotException && (!windowFull || countAllRows)) { 694 int err = sqlite3_step(statement); 695 if (err == SQLITE_ROW) { 696 LOG_WINDOW("Stepped statement %p to row %d", statement, totalRows); 697 retryCount = 0; 698 totalRows += 1; 699 700 // Skip the row if the window is full or we haven't reached the start position yet. 701 if (startPos >= totalRows || windowFull) { 702 continue; 703 } 704 705 CopyRowResult cpr = copyRow(env, window, statement, numColumns, startPos, addedRows); 706 if (cpr == CPR_FULL && addedRows && startPos + addedRows <= requiredPos) { 707 // We filled the window before we got to the one row that we really wanted. 708 // Clear the window and start filling it again from here. 709 // TODO: Would be nicer if we could progressively replace earlier rows. 710 window->clear(); 711 window->setNumColumns(numColumns); 712 startPos += addedRows; 713 addedRows = 0; 714 cpr = copyRow(env, window, statement, numColumns, startPos, addedRows); 715 } 716 717 if (cpr == CPR_OK) { 718 addedRows += 1; 719 } else if (cpr == CPR_FULL) { 720 windowFull = true; 721 } else { 722 gotException = true; 723 } 724 } else if (err == SQLITE_DONE) { 725 // All rows processed, bail 726 LOG_WINDOW("Processed all rows"); 727 break; 728 } else if (err == SQLITE_LOCKED || err == SQLITE_BUSY) { 729 // The table is locked, retry 730 LOG_WINDOW("Database locked, retrying"); 731 if (retryCount > 50) { 732 ALOGE("Bailing on database busy retry"); 733 throw_sqlite3_exception(env, connection->db, "retrycount exceeded"); 734 gotException = true; 735 } else { 736 // Sleep to give the thread holding the lock a chance to finish 737 usleep(1000); 738 retryCount++; 739 } 740 } else { 741 throw_sqlite3_exception(env, connection->db); 742 gotException = true; 743 } 744 } 745 746 LOG_WINDOW("Resetting statement %p after fetching %d rows and adding %d rows" 747 "to the window in %d bytes", 748 statement, totalRows, addedRows, window->size() - window->freeSpace()); 749 sqlite3_reset(statement); 750 751 // Report the total number of rows on request. 752 if (startPos > totalRows) { 753 ALOGE("startPos %d > actual rows %d", startPos, totalRows); 754 } 755 jlong result = jlong(startPos) << 32 | jlong(totalRows); 756 return result; 757} 758 759static jint nativeGetDbLookaside(JNIEnv* env, jobject clazz, jlong connectionPtr) { 760 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 761 762 int cur = -1; 763 int unused; 764 sqlite3_db_status(connection->db, SQLITE_DBSTATUS_LOOKASIDE_USED, &cur, &unused, 0); 765 return cur; 766} 767 768static void nativeCancel(JNIEnv* env, jobject clazz, jlong connectionPtr) { 769 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 770 connection->canceled = true; 771} 772 773static void nativeResetCancel(JNIEnv* env, jobject clazz, jlong connectionPtr, 774 jboolean cancelable) { 775 SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); 776 connection->canceled = false; 777 778 if (cancelable) { 779 sqlite3_progress_handler(connection->db, 4, sqliteProgressHandlerCallback, 780 connection); 781 } else { 782 sqlite3_progress_handler(connection->db, 0, NULL, NULL); 783 } 784} 785 786 787static JNINativeMethod sMethods[] = 788{ 789 /* name, signature, funcPtr */ 790 { "nativeOpen", "(Ljava/lang/String;ILjava/lang/String;ZZ)J", 791 (void*)nativeOpen }, 792 { "nativeClose", "(J)V", 793 (void*)nativeClose }, 794 { "nativeRegisterCustomFunction", "(JLandroid/database/sqlite/SQLiteCustomFunction;)V", 795 (void*)nativeRegisterCustomFunction }, 796 { "nativeRegisterLocalizedCollators", "(JLjava/lang/String;)V", 797 (void*)nativeRegisterLocalizedCollators }, 798 { "nativePrepareStatement", "(JLjava/lang/String;)J", 799 (void*)nativePrepareStatement }, 800 { "nativeFinalizeStatement", "(JJ)V", 801 (void*)nativeFinalizeStatement }, 802 { "nativeGetParameterCount", "(JJ)I", 803 (void*)nativeGetParameterCount }, 804 { "nativeIsReadOnly", "(JJ)Z", 805 (void*)nativeIsReadOnly }, 806 { "nativeGetColumnCount", "(JJ)I", 807 (void*)nativeGetColumnCount }, 808 { "nativeGetColumnName", "(JJI)Ljava/lang/String;", 809 (void*)nativeGetColumnName }, 810 { "nativeBindNull", "(JJI)V", 811 (void*)nativeBindNull }, 812 { "nativeBindLong", "(JJIJ)V", 813 (void*)nativeBindLong }, 814 { "nativeBindDouble", "(JJID)V", 815 (void*)nativeBindDouble }, 816 { "nativeBindString", "(JJILjava/lang/String;)V", 817 (void*)nativeBindString }, 818 { "nativeBindBlob", "(JJI[B)V", 819 (void*)nativeBindBlob }, 820 { "nativeResetStatementAndClearBindings", "(JJ)V", 821 (void*)nativeResetStatementAndClearBindings }, 822 { "nativeExecute", "(JJ)V", 823 (void*)nativeExecute }, 824 { "nativeExecuteForLong", "(JJ)J", 825 (void*)nativeExecuteForLong }, 826 { "nativeExecuteForString", "(JJ)Ljava/lang/String;", 827 (void*)nativeExecuteForString }, 828 { "nativeExecuteForBlobFileDescriptor", "(JJ)I", 829 (void*)nativeExecuteForBlobFileDescriptor }, 830 { "nativeExecuteForChangedRowCount", "(JJ)I", 831 (void*)nativeExecuteForChangedRowCount }, 832 { "nativeExecuteForLastInsertedRowId", "(JJ)J", 833 (void*)nativeExecuteForLastInsertedRowId }, 834 { "nativeExecuteForCursorWindow", "(JJJIIZ)J", 835 (void*)nativeExecuteForCursorWindow }, 836 { "nativeGetDbLookaside", "(J)I", 837 (void*)nativeGetDbLookaside }, 838 { "nativeCancel", "(J)V", 839 (void*)nativeCancel }, 840 { "nativeResetCancel", "(JZ)V", 841 (void*)nativeResetCancel }, 842}; 843 844#define FIND_CLASS(var, className) \ 845 var = env->FindClass(className); \ 846 LOG_FATAL_IF(! var, "Unable to find class " className); 847 848#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \ 849 var = env->GetMethodID(clazz, methodName, fieldDescriptor); \ 850 LOG_FATAL_IF(! var, "Unable to find method" methodName); 851 852#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ 853 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ 854 LOG_FATAL_IF(! var, "Unable to find field " fieldName); 855 856int register_android_database_SQLiteConnection(JNIEnv *env) 857{ 858 jclass clazz; 859 FIND_CLASS(clazz, "android/database/sqlite/SQLiteCustomFunction"); 860 861 GET_FIELD_ID(gSQLiteCustomFunctionClassInfo.name, clazz, 862 "name", "Ljava/lang/String;"); 863 GET_FIELD_ID(gSQLiteCustomFunctionClassInfo.numArgs, clazz, 864 "numArgs", "I"); 865 GET_METHOD_ID(gSQLiteCustomFunctionClassInfo.dispatchCallback, 866 clazz, "dispatchCallback", "([Ljava/lang/String;)V"); 867 868 FIND_CLASS(clazz, "java/lang/String"); 869 gStringClassInfo.clazz = jclass(env->NewGlobalRef(clazz)); 870 871 return AndroidRuntime::registerNativeMethods(env, "android/database/sqlite/SQLiteConnection", 872 sMethods, NELEM(sMethods)); 873} 874 875} // namespace android 876