Merge "verify database state before calling sqlite. Bug:2593970" into froyo

This commit is contained in:
Vasu Nori
2010-04-14 15:53:43 -07:00
committed by Android (Google) Code Review
4 changed files with 61 additions and 26 deletions

View File

@@ -54,6 +54,9 @@ import android.util.Log;
private boolean mInUse = false; private boolean mInUse = false;
/* package */ SQLiteCompiledSql(SQLiteDatabase db, String sql) { /* package */ SQLiteCompiledSql(SQLiteDatabase db, String sql) {
if (!db.isOpen()) {
throw new IllegalStateException("database " + db.getPath() + " already closed");
}
mDatabase = db; mDatabase = db;
mSqlStmt = sql; mSqlStmt = sql;
mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace(); mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace();
@@ -75,6 +78,9 @@ import android.util.Log;
* existing compiled SQL program already around * existing compiled SQL program already around
*/ */
private void compile(String sql, boolean forceCompilation) { 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 // Only compile if we don't have a valid statement already or the caller has
// explicitly requested a recompile. // explicitly requested a recompile.
if (forceCompilation) { if (forceCompilation) {
@@ -90,6 +96,9 @@ import android.util.Log;
} }
/* package */ void releaseSqlStatement() { /* 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 // Note that native_finalize() checks to make sure that nStatement is
// non-null before destroying it. // non-null before destroying it.
if (nStatement != 0) { if (nStatement != 0) {

View File

@@ -500,10 +500,10 @@ public class SQLiteDatabase extends SQLiteClosable {
* {@link #yieldIfContendedSafely}. * {@link #yieldIfContendedSafely}.
*/ */
public void beginTransactionWithListener(SQLiteTransactionListener transactionListener) { public void beginTransactionWithListener(SQLiteTransactionListener transactionListener) {
lockForced();
if (!isOpen()) { if (!isOpen()) {
throw new IllegalStateException("database not open"); throw new IllegalStateException("database not open");
} }
lockForced();
boolean ok = false; boolean ok = false;
try { try {
// If this thread already had the lock then get out // If this thread already had the lock then get out
@@ -915,11 +915,11 @@ public class SQLiteDatabase extends SQLiteClosable {
* @return the database version * @return the database version
*/ */
public int getVersion() { public int getVersion() {
SQLiteStatement prog = null;
lock();
if (!isOpen()) { if (!isOpen()) {
throw new IllegalStateException("database not open"); throw new IllegalStateException("database not open");
} }
SQLiteStatement prog = null;
lock();
try { try {
prog = new SQLiteStatement(this, "PRAGMA user_version;"); prog = new SQLiteStatement(this, "PRAGMA user_version;");
long version = prog.simpleQueryForLong(); long version = prog.simpleQueryForLong();
@@ -936,9 +936,6 @@ public class SQLiteDatabase extends SQLiteClosable {
* @param version the new database version * @param version the new database version
*/ */
public void setVersion(int version) { public void setVersion(int version) {
if (!isOpen()) {
throw new IllegalStateException("database not open");
}
execSQL("PRAGMA user_version = " + version); execSQL("PRAGMA user_version = " + version);
} }
@@ -948,11 +945,11 @@ public class SQLiteDatabase extends SQLiteClosable {
* @return the new maximum database size * @return the new maximum database size
*/ */
public long getMaximumSize() { public long getMaximumSize() {
SQLiteStatement prog = null;
lock();
if (!isOpen()) { if (!isOpen()) {
throw new IllegalStateException("database not open"); throw new IllegalStateException("database not open");
} }
SQLiteStatement prog = null;
lock();
try { try {
prog = new SQLiteStatement(this, prog = new SQLiteStatement(this,
"PRAGMA max_page_count;"); "PRAGMA max_page_count;");
@@ -972,11 +969,11 @@ public class SQLiteDatabase extends SQLiteClosable {
* @return the new maximum database size * @return the new maximum database size
*/ */
public long setMaximumSize(long numBytes) { public long setMaximumSize(long numBytes) {
SQLiteStatement prog = null;
lock();
if (!isOpen()) { if (!isOpen()) {
throw new IllegalStateException("database not open"); throw new IllegalStateException("database not open");
} }
SQLiteStatement prog = null;
lock();
try { try {
long pageSize = getPageSize(); long pageSize = getPageSize();
long numPages = numBytes / pageSize; long numPages = numBytes / pageSize;
@@ -1000,11 +997,11 @@ public class SQLiteDatabase extends SQLiteClosable {
* @return the database page size, in bytes * @return the database page size, in bytes
*/ */
public long getPageSize() { public long getPageSize() {
SQLiteStatement prog = null;
lock();
if (!isOpen()) { if (!isOpen()) {
throw new IllegalStateException("database not open"); throw new IllegalStateException("database not open");
} }
SQLiteStatement prog = null;
lock();
try { try {
prog = new SQLiteStatement(this, prog = new SQLiteStatement(this,
"PRAGMA page_size;"); "PRAGMA page_size;");
@@ -1024,9 +1021,6 @@ public class SQLiteDatabase extends SQLiteClosable {
* @param numBytes the database page size, in bytes * @param numBytes the database page size, in bytes
*/ */
public void setPageSize(long numBytes) { public void setPageSize(long numBytes) {
if (!isOpen()) {
throw new IllegalStateException("database not open");
}
execSQL("PRAGMA page_size = " + numBytes); execSQL("PRAGMA page_size = " + numBytes);
} }
@@ -1143,10 +1137,10 @@ public class SQLiteDatabase extends SQLiteClosable {
* @return a pre-compiled statement object. * @return a pre-compiled statement object.
*/ */
public SQLiteStatement compileStatement(String sql) throws SQLException { public SQLiteStatement compileStatement(String sql) throws SQLException {
lock();
if (!isOpen()) { if (!isOpen()) {
throw new IllegalStateException("database not open"); throw new IllegalStateException("database not open");
} }
lock();
try { try {
return new SQLiteStatement(this, sql); return new SQLiteStatement(this, sql);
} finally { } finally {
@@ -1586,10 +1580,10 @@ public class SQLiteDatabase extends SQLiteClosable {
* whereClause. * whereClause.
*/ */
public int delete(String table, String whereClause, String[] whereArgs) { public int delete(String table, String whereClause, String[] whereArgs) {
lock();
if (!isOpen()) { if (!isOpen()) {
throw new IllegalStateException("database not open"); throw new IllegalStateException("database not open");
} }
lock();
SQLiteStatement statement = null; SQLiteStatement statement = null;
try { try {
statement = compileStatement("DELETE FROM " + table statement = compileStatement("DELETE FROM " + table
@@ -1641,10 +1635,6 @@ public class SQLiteDatabase extends SQLiteClosable {
*/ */
public int updateWithOnConflict(String table, ContentValues values, public int updateWithOnConflict(String table, ContentValues values,
String whereClause, String[] whereArgs, int conflictAlgorithm) { String whereClause, String[] whereArgs, int conflictAlgorithm) {
if (!isOpen()) {
throw new IllegalStateException("database not open");
}
if (values == null || values.size() == 0) { if (values == null || values.size() == 0) {
throw new IllegalArgumentException("Empty values"); throw new IllegalArgumentException("Empty values");
} }
@@ -1673,6 +1663,9 @@ public class SQLiteDatabase extends SQLiteClosable {
} }
lock(); lock();
if (!isOpen()) {
throw new IllegalStateException("database not open");
}
SQLiteStatement statement = null; SQLiteStatement statement = null;
try { try {
statement = compileStatement(sql.toString()); statement = compileStatement(sql.toString());
@@ -1724,11 +1717,11 @@ public class SQLiteDatabase extends SQLiteClosable {
* @throws SQLException If the SQL string is invalid for some reason * @throws SQLException If the SQL string is invalid for some reason
*/ */
public void execSQL(String sql) throws SQLException { public void execSQL(String sql) throws SQLException {
long timeStart = SystemClock.uptimeMillis();
lock();
if (!isOpen()) { if (!isOpen()) {
throw new IllegalStateException("database not open"); throw new IllegalStateException("database not open");
} }
long timeStart = SystemClock.uptimeMillis();
lock();
logTimeStat(mLastSqlStatement, timeStart, GET_LOCK_LOG_PREFIX); logTimeStat(mLastSqlStatement, timeStart, GET_LOCK_LOG_PREFIX);
try { try {
native_execSQL(sql); native_execSQL(sql);
@@ -1759,14 +1752,14 @@ public class SQLiteDatabase extends SQLiteClosable {
* @throws SQLException If the SQL string is invalid for some reason * @throws SQLException If the SQL string is invalid for some reason
*/ */
public void execSQL(String sql, Object[] bindArgs) throws SQLException { public void execSQL(String sql, Object[] bindArgs) throws SQLException {
if (!isOpen()) {
throw new IllegalStateException("database not open");
}
if (bindArgs == null) { if (bindArgs == null) {
throw new IllegalArgumentException("Empty bindArgs"); throw new IllegalArgumentException("Empty bindArgs");
} }
long timeStart = SystemClock.uptimeMillis(); long timeStart = SystemClock.uptimeMillis();
lock(); lock();
if (!isOpen()) {
throw new IllegalStateException("database not open");
}
SQLiteStatement statement = null; SQLiteStatement statement = null;
try { try {
statement = compileStatement(sql); statement = compileStatement(sql);

View File

@@ -173,6 +173,9 @@ public abstract class SQLiteProgram extends SQLiteClosable {
* @param index The 1-based index to the parameter to bind null to * @param index The 1-based index to the parameter to bind null to
*/ */
public void bindNull(int index) { public void bindNull(int index) {
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
acquireReference(); acquireReference();
try { try {
native_bind_null(index); native_bind_null(index);
@@ -189,6 +192,9 @@ public abstract class SQLiteProgram extends SQLiteClosable {
* @param value The value to bind * @param value The value to bind
*/ */
public void bindLong(int index, long value) { public void bindLong(int index, long value) {
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
acquireReference(); acquireReference();
try { try {
native_bind_long(index, value); native_bind_long(index, value);
@@ -205,6 +211,9 @@ public abstract class SQLiteProgram extends SQLiteClosable {
* @param value The value to bind * @param value The value to bind
*/ */
public void bindDouble(int index, double value) { public void bindDouble(int index, double value) {
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
acquireReference(); acquireReference();
try { try {
native_bind_double(index, value); native_bind_double(index, value);
@@ -224,6 +233,9 @@ public abstract class SQLiteProgram extends SQLiteClosable {
if (value == null) { if (value == null) {
throw new IllegalArgumentException("the bind value at index " + index + " is null"); throw new IllegalArgumentException("the bind value at index " + index + " is null");
} }
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
acquireReference(); acquireReference();
try { try {
native_bind_string(index, value); native_bind_string(index, value);
@@ -243,6 +255,9 @@ public abstract class SQLiteProgram extends SQLiteClosable {
if (value == null) { if (value == null) {
throw new IllegalArgumentException("the bind value at index " + index + " is null"); throw new IllegalArgumentException("the bind value at index " + index + " is null");
} }
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
acquireReference(); acquireReference();
try { try {
native_bind_blob(index, value); 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. * Clears all existing bindings. Unset bindings are treated as NULL.
*/ */
public void clearBindings() { public void clearBindings() {
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
acquireReference(); acquireReference();
try { try {
native_clear_bindings(); native_clear_bindings();
@@ -267,6 +285,9 @@ public abstract class SQLiteProgram extends SQLiteClosable {
* Release this program's resources, making it invalid. * Release this program's resources, making it invalid.
*/ */
public void close() { public void close() {
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
mDatabase.lock(); mDatabase.lock();
try { try {
releaseReference(); releaseReference();

View File

@@ -44,6 +44,9 @@ public class SQLiteStatement extends SQLiteProgram
* some reason * some reason
*/ */
public void execute() { public void execute() {
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
long timeStart = SystemClock.uptimeMillis(); long timeStart = SystemClock.uptimeMillis();
mDatabase.lock(); mDatabase.lock();
@@ -67,6 +70,9 @@ public class SQLiteStatement extends SQLiteProgram
* some reason * some reason
*/ */
public long executeInsert() { public long executeInsert() {
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
long timeStart = SystemClock.uptimeMillis(); long timeStart = SystemClock.uptimeMillis();
mDatabase.lock(); mDatabase.lock();
@@ -90,6 +96,9 @@ public class SQLiteStatement extends SQLiteProgram
* @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
*/ */
public long simpleQueryForLong() { public long simpleQueryForLong() {
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
long timeStart = SystemClock.uptimeMillis(); long timeStart = SystemClock.uptimeMillis();
mDatabase.lock(); mDatabase.lock();
@@ -113,6 +122,9 @@ public class SQLiteStatement extends SQLiteProgram
* @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
*/ */
public String simpleQueryForString() { public String simpleQueryForString() {
if (!mDatabase.isOpen()) {
throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
}
long timeStart = SystemClock.uptimeMillis(); long timeStart = SystemClock.uptimeMillis();
mDatabase.lock(); mDatabase.lock();