Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fb16cbd9 authored by Vasu Nori's avatar Vasu Nori
Browse files

add new API in SQLiteStatement to deprecate another.

1. SQLIteStatement.executeUpdateDelete() replaces execute() - and returns the
number of rows changed.
2. let SQLiteDatabase.execSQL() call the above new API - which
means all CRUD statements by execSQL() are stored in prepared statement cache.
3. remove the following from SQLiteDatabase
     lastrowId
     lastchangecount()
     native_execSQL()

Change-Id: I4e93a09dc381f425c3ae6ccc331a7bf227491e22
parent 037644fb
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -63391,6 +63391,17 @@
 visibility="public"
>
</method>
<method name="executeUpdateDelete"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="simpleQueryForLong"
 return="long"
 abstract="false"
+12 −47
Original line number Diff line number Diff line
@@ -67,7 +67,7 @@ import java.util.regex.Pattern;
 * is the Unicode Collation Algorithm and not tailored to the current locale.
 */
public class SQLiteDatabase extends SQLiteClosable {
    private static final String TAG = "Database";
    private static final String TAG = "SQLiteDatabase";
    private static final int EVENT_DB_OPERATION = 52000;
    private static final int EVENT_DB_CORRUPT = 75004;

@@ -1641,18 +1641,7 @@ public class SQLiteDatabase extends SQLiteClosable {
            }

            // Run the program and then cleanup
            statement.execute();

            long insertedRowId = lastInsertRow();
            if (insertedRowId == -1) {
                Log.e(TAG, "Error inserting " + initialValues + " using " + sql);
            } else {
                if (Config.LOGD && Log.isLoggable(TAG, Log.VERBOSE)) {
                    Log.v(TAG, "Inserting row " + insertedRowId + " from "
                            + initialValues + " using " + sql);
                }
            }
            return insertedRowId;
            return statement.executeInsert();
        } catch (SQLiteDatabaseCorruptException e) {
            onCorruption();
            throw e;
@@ -1689,8 +1678,7 @@ public class SQLiteDatabase extends SQLiteClosable {
                    DatabaseUtils.bindObjectToProgram(statement, i + 1, whereArgs[i]);
                }
            }
            statement.execute();
            return lastChangeCount();
            return statement.executeUpdateDelete();
        } catch (SQLiteDatabaseCorruptException e) {
            onCorruption();
            throw e;
@@ -1782,12 +1770,7 @@ public class SQLiteDatabase extends SQLiteClosable {
            }

            // Run the program and then cleanup
            statement.execute();
            int numChangedRows = lastChangeCount();
            if (Config.LOGD && Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG, "Updated " + numChangedRows + " using " + values + " and " + sql);
            }
            return numChangedRows;
            return statement.executeUpdateDelete();
        } catch (SQLiteDatabaseCorruptException e) {
            onCorruption();
            throw e;
@@ -1834,13 +1817,18 @@ public class SQLiteDatabase extends SQLiteClosable {
        long timeStart = SystemClock.uptimeMillis();
        lock();
        logTimeStat(mLastSqlStatement, timeStart, GET_LOCK_LOG_PREFIX);
        SQLiteStatement stmt = null;
        try {
            closePendingStatements();
            native_execSQL(sql);
            stmt = compileStatement(sql);
            stmt.execute();
        } catch (SQLiteDatabaseCorruptException e) {
            onCorruption();
            throw e;
        } finally {
            if (stmt != null) {
                stmt.close();
            }
            unlock();
        }

@@ -1914,7 +1902,7 @@ public class SQLiteDatabase extends SQLiteClosable {
                    DatabaseUtils.bindObjectToProgram(statement, i + 1, bindArgs[i]);
                }
            }
            statement.execute();
            statement.executeUpdateDelete();
        } catch (SQLiteDatabaseCorruptException e) {
            onCorruption();
            throw e;
@@ -2025,7 +2013,7 @@ public class SQLiteDatabase extends SQLiteClosable {
        }
        if (durationMillis >= sQueryLogTimeInMillis) {
            samplePercent = 100;
        } else {;
        } else {
            samplePercent = (int) (100 * durationMillis / sQueryLogTimeInMillis) + 1;
            if (mRandom.nextInt(100) >= samplePercent) return;
        }
@@ -2627,15 +2615,6 @@ public class SQLiteDatabase extends SQLiteClosable {
     */
    private native void enableSqlProfiling(String path, short connectionNum);

    /**
     * Native call to execute a raw SQL statement. {@link #lock} must be held
     * when calling this method.
     *
     * @param sql The raw SQL string
     * @throws SQLException
     */
    /* package */ native void native_execSQL(String sql) throws SQLException;

    /**
     * Native call to set the locale.  {@link #lock} must be held when calling
     * this method.
@@ -2643,20 +2622,6 @@ public class SQLiteDatabase extends SQLiteClosable {
     */
    /* package */ native void native_setLocale(String loc, int flags);

    /**
     * Returns the row ID of the last row inserted into the database.
     *
     * @return the row ID of the last row inserted into the database.
     */
    /* package */ native long lastInsertRow();

    /**
     * Returns the number of changes made in the last statement executed.
     *
     * @return the number of changes made in the last statement executed.
     */
    /* package */ native int lastChangeCount();

    /**
     * return the SQLITE_DBSTATUS_LOOKASIDE_USED documented here
     * http://www.sqlite.org/c3ref/c_dbstatus_lookaside_used.html
+20 −6
Original line number Diff line number Diff line
@@ -48,18 +48,31 @@ public class SQLiteStatement extends SQLiteProgram
    }

    /**
     * Execute this SQL statement, if it is not a query. For example,
     * CREATE TABLE, DELTE, INSERT, etc.
     * Execute this SQL statement, if it is not a SELECT / INSERT / DELETE / UPDATE, for example
     * CREATE / DROP table, view, trigger, index etc.
     *
     * @throws android.database.SQLException If the SQL string is invalid for
     *         some reason
     */
    public void execute() {
        executeUpdateDelete();
    }

    /**
     * Execute this SQL statement, if the the number of rows affected by exection of this SQL
     * statement is of any importance to the caller - for example, UPDATE / DELETE SQL statements.
     *
     * @return the number of rows affected by this SQL statement execution.
     * @throws android.database.SQLException If the SQL string is invalid for
     *         some reason
     */
    public int executeUpdateDelete() {
        synchronized(this) {
            long timeStart = acquireAndLock(WRITE);
            try {
                native_execute();
                int numChanges = native_execute();
                mDatabase.logTimeStat(mSql, timeStart);
                return numChanges;
            } finally {
                releaseAndUnlock();
            }
@@ -79,9 +92,9 @@ public class SQLiteStatement extends SQLiteProgram
        synchronized(this) {
            long timeStart = acquireAndLock(WRITE);
            try {
                native_execute();
                long lastInsertedRowId = native_executeInsert();
                mDatabase.logTimeStat(mSql, timeStart);
                return (mDatabase.lastChangeCount() > 0) ? mDatabase.lastInsertRow() : -1;
                return lastInsertedRowId;
            } finally {
                releaseAndUnlock();
            }
@@ -182,7 +195,8 @@ public class SQLiteStatement extends SQLiteProgram
        nHandle = mDatabase.mNativeHandle;
    }

    private final native void native_execute();
    private final native int native_execute();
    private final native long native_executeInsert();
    private final native long native_1x1_long();
    private final native String native_1x1_string();
}
+1 −75
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
 */

#undef LOG_TAG
#define LOG_TAG "Database"
#define LOG_TAG "SqliteDatabaseCpp"

#include <utils/Log.h>
#include <utils/String8.h>
@@ -245,77 +245,6 @@ static void dbclose(JNIEnv* env, jobject object)
    }
}

/* public native void native_execSQL(String sql); */
static void native_execSQL(JNIEnv* env, jobject object, jstring sqlString)
{
    int err;
    int stepErr;
    sqlite3_stmt * statement = NULL;
    sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);
    jchar const * sql = env->GetStringChars(sqlString, NULL);
    jsize sqlLen = env->GetStringLength(sqlString);

    if (sql == NULL || sqlLen == 0) {
        jniThrowException(env, "java/lang/IllegalArgumentException",
                "You must supply an SQL string");
        return;
    }

    err = sqlite3_prepare16_v2(handle, sql, sqlLen * 2, &statement, NULL);

    env->ReleaseStringChars(sqlString, sql);

    if (err != SQLITE_OK) {
        char const * sql8 = env->GetStringUTFChars(sqlString, NULL);
        LOGE("Failure %d (%s) on %p when preparing '%s'.\n", err, sqlite3_errmsg(handle),
                handle, sql8);
        throw_sqlite3_exception(env, handle, sql8);
        env->ReleaseStringUTFChars(sqlString, sql8);
        return;
    }

    stepErr = sqlite3_step(statement);
    err = sqlite3_finalize(statement);

    if (stepErr != SQLITE_DONE) {
        if (stepErr == SQLITE_ROW) {
            throw_sqlite3_exception(env,
                    "Queries cannot be performed using execSQL(), use query() instead.");
        } else {
            char const * sql8 = env->GetStringUTFChars(sqlString, NULL);
            LOGE("Failure %d (%s) on %p when executing '%s'\n", err, sqlite3_errmsg(handle),
                    handle, sql8);
            throw_sqlite3_exception(env, handle, sql8);
            env->ReleaseStringUTFChars(sqlString, sql8);

        }
    } else
#ifndef DB_LOG_STATEMENTS
    IF_LOGV()
#endif
    {
        char const * sql8 = env->GetStringUTFChars(sqlString, NULL);
        LOGV("Success on %p when executing '%s'\n", handle, sql8);
        env->ReleaseStringUTFChars(sqlString, sql8);
    }
}

/* native long lastInsertRow(); */
static jlong lastInsertRow(JNIEnv* env, jobject object)
{
    sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);

    return sqlite3_last_insert_rowid(handle);
}

/* native int lastChangeCount(); */
static jint lastChangeCount(JNIEnv* env, jobject object)
{
    sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);

    return sqlite3_changes(handle);
}

/* native int native_getDbLookaside(); */
static jint native_getDbLookaside(JNIEnv* env, jobject object)
{
@@ -522,9 +451,6 @@ static JNINativeMethod sMethods[] =
    {"dbclose", "()V", (void *)dbclose},
    {"enableSqlTracing", "(Ljava/lang/String;S)V", (void *)enableSqlTracing},
    {"enableSqlProfiling", "(Ljava/lang/String;S)V", (void *)enableSqlProfiling},
    {"native_execSQL", "(Ljava/lang/String;)V", (void *)native_execSQL},
    {"lastInsertRow", "()J", (void *)lastInsertRow},
    {"lastChangeCount", "()I", (void *)lastChangeCount},
    {"native_setLocale", "(Ljava/lang/String;I)V", (void *)native_setLocale},
    {"native_getDbLookaside", "()I", (void *)native_getDbLookaside},
    {"releaseMemory", "()I", (void *)native_releaseMemory},
+23 −7
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@
*/

#undef LOG_TAG
#define LOG_TAG "Cursor"
#define LOG_TAG "SQLiteStatementCpp"

#include <jni.h>
#include <JNIHelp.h>
@@ -48,24 +48,40 @@ static jfieldID gStatementField;
        (sqlite3 *)env->GetIntField(object, gHandleField)


static void native_execute(JNIEnv* env, jobject object)
static jint native_execute(JNIEnv* env, jobject object)
{
    int err;
    sqlite3 * handle = GET_HANDLE(env, object);
    sqlite3_stmt * statement = GET_STATEMENT(env, object);
    int numChanges = -1;

    // Execute the statement
    err = sqlite3_step(statement);

    // Throw an exception if an error occured
    // Throw an exception if an error occurred
    if (err == SQLITE_ROW) {
        LOGV("Queries cannot be performed using execute(). use SQLiteDatabase.query() instead.");
        throw_sqlite3_exception(env,
                "Queries can be performed using SQLiteDatabase query or rawQuery methods only.");
    } else if (err != SQLITE_DONE) {
        throw_sqlite3_exception_errcode(env, err, sqlite3_errmsg(handle));
    } else {
        numChanges = sqlite3_changes(handle);
    }

    // Reset the statment so it's ready to use again
    // Reset the statement so it's ready to use again
    sqlite3_reset(statement);
    return numChanges;
}

static jlong native_executeInsert(JNIEnv* env, jobject object)
{
    sqlite3 * handle = GET_HANDLE(env, object);
    jint numChanges = native_execute(env, object);
    if (numChanges > 0) {
        return sqlite3_last_insert_rowid(handle);
    } else {
        return -1;
    }
}

static jlong native_1x1_long(JNIEnv* env, jobject object)
@@ -117,11 +133,11 @@ static jstring native_1x1_string(JNIEnv* env, jobject object)
    return value;
}


static JNINativeMethod sMethods[] =
{
     /* name, signature, funcPtr */
    {"native_execute", "()V", (void *)native_execute},
    {"native_execute", "()I", (void *)native_execute},
    {"native_executeInsert", "()J", (void *)native_executeInsert},
    {"native_1x1_long", "()J", (void *)native_1x1_long},
    {"native_1x1_string", "()Ljava/lang/String;", (void *)native_1x1_string},
};
Loading