diff options
author | Vasu Nori <vnori@google.com> | 2010-04-14 16:33:32 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2010-04-14 16:33:32 -0700 |
commit | 9d3b3e1324ad30e228ed7e0d66fdbfca7376a20a (patch) | |
tree | 974d3d7cf686f9b4644e9a8c529881b79495b4ed /core | |
parent | 8579f2114a08475e55eb5d8095ce52a4e4b2f691 (diff) | |
parent | bad5f620df13ae41374e05cad0a391ab084664c4 (diff) | |
download | frameworks_base-9d3b3e1324ad30e228ed7e0d66fdbfca7376a20a.zip frameworks_base-9d3b3e1324ad30e228ed7e0d66fdbfca7376a20a.tar.gz frameworks_base-9d3b3e1324ad30e228ed7e0d66fdbfca7376a20a.tar.bz2 |
am bad5f620: am 0bbcdc6c: Merge "verify database state before calling sqlite. Bug:2593970" into froyo
Merge commit 'bad5f620df13ae41374e05cad0a391ab084664c4' into kraken
* commit 'bad5f620df13ae41374e05cad0a391ab084664c4':
verify database state before calling sqlite. Bug:2593970
Diffstat (limited to 'core')
4 files changed, 61 insertions, 26 deletions
diff --git a/core/java/android/database/sqlite/SQLiteCompiledSql.java b/core/java/android/database/sqlite/SQLiteCompiledSql.java index 72ceb9b..047e176 100644 --- a/core/java/android/database/sqlite/SQLiteCompiledSql.java +++ b/core/java/android/database/sqlite/SQLiteCompiledSql.java @@ -54,6 +54,9 @@ import android.util.Log; private boolean mInUse = false; /* package */ SQLiteCompiledSql(SQLiteDatabase db, String sql) { + if (!db.isOpen()) { + throw new IllegalStateException("database " + db.getPath() + " already closed"); + } mDatabase = db; mSqlStmt = sql; mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace(); @@ -75,6 +78,9 @@ import android.util.Log; * existing compiled SQL program already around */ private void compile(String sql, boolean forceCompilation) { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } // Only compile if we don't have a valid statement already or the caller has // explicitly requested a recompile. if (forceCompilation) { @@ -90,6 +96,9 @@ import android.util.Log; } /* package */ void releaseSqlStatement() { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } // Note that native_finalize() checks to make sure that nStatement is // non-null before destroying it. if (nStatement != 0) { diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java index 3b7416e..70f681f 100644 --- a/core/java/android/database/sqlite/SQLiteDatabase.java +++ b/core/java/android/database/sqlite/SQLiteDatabase.java @@ -500,10 +500,10 @@ public class SQLiteDatabase extends SQLiteClosable { * {@link #yieldIfContendedSafely}. */ public void beginTransactionWithListener(SQLiteTransactionListener transactionListener) { + lockForced(); if (!isOpen()) { throw new IllegalStateException("database not open"); } - lockForced(); boolean ok = false; try { // If this thread already had the lock then get out @@ -915,11 +915,11 @@ public class SQLiteDatabase extends SQLiteClosable { * @return the database version */ public int getVersion() { + SQLiteStatement prog = null; + lock(); if (!isOpen()) { throw new IllegalStateException("database not open"); } - SQLiteStatement prog = null; - lock(); try { prog = new SQLiteStatement(this, "PRAGMA user_version;"); long version = prog.simpleQueryForLong(); @@ -936,9 +936,6 @@ public class SQLiteDatabase extends SQLiteClosable { * @param version the new database version */ public void setVersion(int version) { - if (!isOpen()) { - throw new IllegalStateException("database not open"); - } execSQL("PRAGMA user_version = " + version); } @@ -948,11 +945,11 @@ public class SQLiteDatabase extends SQLiteClosable { * @return the new maximum database size */ public long getMaximumSize() { + SQLiteStatement prog = null; + lock(); if (!isOpen()) { throw new IllegalStateException("database not open"); } - SQLiteStatement prog = null; - lock(); try { prog = new SQLiteStatement(this, "PRAGMA max_page_count;"); @@ -972,11 +969,11 @@ public class SQLiteDatabase extends SQLiteClosable { * @return the new maximum database size */ public long setMaximumSize(long numBytes) { + SQLiteStatement prog = null; + lock(); if (!isOpen()) { throw new IllegalStateException("database not open"); } - SQLiteStatement prog = null; - lock(); try { long pageSize = getPageSize(); long numPages = numBytes / pageSize; @@ -1000,11 +997,11 @@ public class SQLiteDatabase extends SQLiteClosable { * @return the database page size, in bytes */ public long getPageSize() { + SQLiteStatement prog = null; + lock(); if (!isOpen()) { throw new IllegalStateException("database not open"); } - SQLiteStatement prog = null; - lock(); try { prog = new SQLiteStatement(this, "PRAGMA page_size;"); @@ -1024,9 +1021,6 @@ public class SQLiteDatabase extends SQLiteClosable { * @param numBytes the database page size, in bytes */ public void setPageSize(long numBytes) { - if (!isOpen()) { - throw new IllegalStateException("database not open"); - } execSQL("PRAGMA page_size = " + numBytes); } @@ -1143,10 +1137,10 @@ public class SQLiteDatabase extends SQLiteClosable { * @return a pre-compiled statement object. */ public SQLiteStatement compileStatement(String sql) throws SQLException { + lock(); if (!isOpen()) { throw new IllegalStateException("database not open"); } - lock(); try { return new SQLiteStatement(this, sql); } finally { @@ -1586,10 +1580,10 @@ public class SQLiteDatabase extends SQLiteClosable { * whereClause. */ public int delete(String table, String whereClause, String[] whereArgs) { + lock(); if (!isOpen()) { throw new IllegalStateException("database not open"); } - lock(); SQLiteStatement statement = null; try { statement = compileStatement("DELETE FROM " + table @@ -1641,10 +1635,6 @@ public class SQLiteDatabase extends SQLiteClosable { */ public int updateWithOnConflict(String table, ContentValues values, String whereClause, String[] whereArgs, int conflictAlgorithm) { - if (!isOpen()) { - throw new IllegalStateException("database not open"); - } - if (values == null || values.size() == 0) { throw new IllegalArgumentException("Empty values"); } @@ -1673,6 +1663,9 @@ public class SQLiteDatabase extends SQLiteClosable { } lock(); + if (!isOpen()) { + throw new IllegalStateException("database not open"); + } SQLiteStatement statement = null; try { statement = compileStatement(sql.toString()); @@ -1724,11 +1717,11 @@ public class SQLiteDatabase extends SQLiteClosable { * @throws SQLException If the SQL string is invalid for some reason */ public void execSQL(String sql) throws SQLException { + long timeStart = SystemClock.uptimeMillis(); + lock(); if (!isOpen()) { throw new IllegalStateException("database not open"); } - long timeStart = SystemClock.uptimeMillis(); - lock(); logTimeStat(mLastSqlStatement, timeStart, GET_LOCK_LOG_PREFIX); try { native_execSQL(sql); @@ -1759,14 +1752,14 @@ public class SQLiteDatabase extends SQLiteClosable { * @throws SQLException If the SQL string is invalid for some reason */ public void execSQL(String sql, Object[] bindArgs) throws SQLException { - if (!isOpen()) { - throw new IllegalStateException("database not open"); - } if (bindArgs == null) { throw new IllegalArgumentException("Empty bindArgs"); } long timeStart = SystemClock.uptimeMillis(); lock(); + if (!isOpen()) { + throw new IllegalStateException("database not open"); + } SQLiteStatement statement = null; try { statement = compileStatement(sql); diff --git a/core/java/android/database/sqlite/SQLiteProgram.java b/core/java/android/database/sqlite/SQLiteProgram.java index 66ce3b0..5f13eb1 100644 --- a/core/java/android/database/sqlite/SQLiteProgram.java +++ b/core/java/android/database/sqlite/SQLiteProgram.java @@ -173,6 +173,9 @@ public abstract class SQLiteProgram extends SQLiteClosable { * @param index The 1-based index to the parameter to bind null to */ public void bindNull(int index) { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } acquireReference(); try { native_bind_null(index); @@ -189,6 +192,9 @@ public abstract class SQLiteProgram extends SQLiteClosable { * @param value The value to bind */ public void bindLong(int index, long value) { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } acquireReference(); try { native_bind_long(index, value); @@ -205,6 +211,9 @@ public abstract class SQLiteProgram extends SQLiteClosable { * @param value The value to bind */ public void bindDouble(int index, double value) { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } acquireReference(); try { native_bind_double(index, value); @@ -224,6 +233,9 @@ public abstract class SQLiteProgram extends SQLiteClosable { if (value == null) { throw new IllegalArgumentException("the bind value at index " + index + " is null"); } + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } acquireReference(); try { native_bind_string(index, value); @@ -243,6 +255,9 @@ public abstract class SQLiteProgram extends SQLiteClosable { if (value == null) { throw new IllegalArgumentException("the bind value at index " + index + " is null"); } + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } acquireReference(); try { native_bind_blob(index, value); @@ -255,6 +270,9 @@ public abstract class SQLiteProgram extends SQLiteClosable { * Clears all existing bindings. Unset bindings are treated as NULL. */ public void clearBindings() { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } acquireReference(); try { native_clear_bindings(); @@ -267,6 +285,9 @@ public abstract class SQLiteProgram extends SQLiteClosable { * Release this program's resources, making it invalid. */ public void close() { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } mDatabase.lock(); try { releaseReference(); diff --git a/core/java/android/database/sqlite/SQLiteStatement.java b/core/java/android/database/sqlite/SQLiteStatement.java index 7f484ff..98da414 100644 --- a/core/java/android/database/sqlite/SQLiteStatement.java +++ b/core/java/android/database/sqlite/SQLiteStatement.java @@ -44,6 +44,9 @@ public class SQLiteStatement extends SQLiteProgram * some reason */ public void execute() { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } long timeStart = SystemClock.uptimeMillis(); mDatabase.lock(); @@ -67,6 +70,9 @@ public class SQLiteStatement extends SQLiteProgram * some reason */ public long executeInsert() { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } long timeStart = SystemClock.uptimeMillis(); mDatabase.lock(); @@ -90,6 +96,9 @@ public class SQLiteStatement extends SQLiteProgram * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows */ public long simpleQueryForLong() { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } long timeStart = SystemClock.uptimeMillis(); mDatabase.lock(); @@ -113,6 +122,9 @@ public class SQLiteStatement extends SQLiteProgram * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows */ public String simpleQueryForString() { + if (!mDatabase.isOpen()) { + throw new IllegalStateException("database " + mDatabase.getPath() + " already closed"); + } long timeStart = SystemClock.uptimeMillis(); mDatabase.lock(); |