Bug 384526 - Use sqlite3_prepare_v2 instead of sqlite3_prepare. r=sspitzer
authorsdwilsh@shawnwilsher.com
Wed, 11 Jul 2007 15:20:39 -0700
changeset 3356 a19cae5461252e62cff2cbe93960bd7ae73b795c
parent 3355 14572917fdfe26bfd8556f4fc1e4d6ae0e020095
child 3357 2504e782ed9fd0d580e782c18f1e893da53d8e52
push id1
push userbsmedberg@mozilla.com
push dateThu, 20 Mar 2008 16:49:24 +0000
treeherderautoland@61007906a1f8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssspitzer
bugs384526
milestone1.9a7pre
Bug 384526 - Use sqlite3_prepare_v2 instead of sqlite3_prepare. r=sspitzer
storage/src/mozStorage.h
storage/src/mozStorageConnection.cpp
storage/src/mozStorageStatement.cpp
storage/test/unit/test_storage_progresshandler.js
--- a/storage/src/mozStorage.h
+++ b/storage/src/mozStorage.h
@@ -72,16 +72,18 @@ ConvertResultCode(int srv)
             return NS_ERROR_FILE_READ_ONLY;
         case SQLITE_FULL:
         case SQLITE_TOOBIG:
             return NS_ERROR_FILE_NO_DEVICE_SPACE;
         case SQLITE_NOMEM:
             return NS_ERROR_OUT_OF_MEMORY;
         case SQLITE_MISUSE:
             return NS_ERROR_UNEXPECTED;
+        case SQLITE_ABORT:
+            return NS_ERROR_ABORT;
     }
 
     // generic error
     return NS_ERROR_FAILURE;
 }
 
 #endif // _MOZSTORAGE_H_
 
--- a/storage/src/mozStorageConnection.cpp
+++ b/storage/src/mozStorageConnection.cpp
@@ -135,17 +135,17 @@ mozStorageConnection::Initialize(nsIFile
     sqlite3_trace (mDBConn, tracefunc, nsnull);
 #endif
 
     /* Execute a dummy statement to force the db open, and to verify
      * whether it's valid or not
      */
     sqlite3_stmt *stmt = nsnull;
     nsCString query("SELECT * FROM sqlite_master");
-    srv = sqlite3_prepare (mDBConn, query.get(), query.Length(), &stmt, nsnull);
+    srv = sqlite3_prepare_v2(mDBConn, query.get(), query.Length(), &stmt, NULL);
 
     if (srv == SQLITE_OK) {
         srv = sqlite3_step(stmt);
 
         if (srv == SQLITE_DONE || srv == SQLITE_ROW)
             srv = SQLITE_OK;
     } else {
         stmt = nsnull;
@@ -299,68 +299,66 @@ mozStorageConnection::TableExists(const 
 {
     if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
 
     nsCString query("SELECT name FROM sqlite_master WHERE type = 'table' AND name ='");
     query.Append(aSQLStatement);
     query.AppendLiteral("'");
 
     sqlite3_stmt *stmt = nsnull;
-    int srv = sqlite3_prepare (mDBConn, query.get(), query.Length(), &stmt, nsnull);
+    int srv = sqlite3_prepare_v2(mDBConn, query.get(), query.Length(), &stmt,
+                                 NULL);
     if (srv != SQLITE_OK) {
         HandleSqliteError(query.get());
         return ConvertResultCode(srv);
     }
 
     PRBool exists = PR_FALSE;
 
     srv = sqlite3_step(stmt);
     // we just care about the return value from step
     sqlite3_finalize(stmt);
 
     if (srv == SQLITE_ROW) {
         exists = PR_TRUE;
     } else if (srv == SQLITE_DONE) {
         exists = PR_FALSE;
-    } else if (srv == SQLITE_ERROR) {
+    } else {
         HandleSqliteError("TableExists finalize");
-        return NS_ERROR_FAILURE;
+        return ConvertResultCode(srv);
     }
 
     *_retval = exists;
     return NS_OK;
 }
 
 NS_IMETHODIMP
 mozStorageConnection::IndexExists(const nsACString& aIndexName, PRBool* _retval)
 {
     if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
 
     nsCString query("SELECT name FROM sqlite_master WHERE type = 'index' AND name ='");
     query.Append(aIndexName);
     query.AppendLiteral("'");
 
     sqlite3_stmt *stmt = nsnull;
-    int srv = sqlite3_prepare(mDBConn, query.get(), query.Length(), &stmt, nsnull);
+    int srv = sqlite3_prepare_v2(mDBConn, query.get(), query.Length(), &stmt,
+                                 NULL);
     if (srv != SQLITE_OK) {
         HandleSqliteError(query.get());
         return ConvertResultCode(srv);
     }
 
     *_retval = PR_FALSE;
 
     srv = sqlite3_step(stmt);
-    // we just care about the return value from step
-    sqlite3_finalize(stmt);
+    (void)sqlite3_finalize(stmt);
 
     if (srv == SQLITE_ROW) {
         *_retval = PR_TRUE;
-    } else if (srv == SQLITE_ERROR) {
-        HandleSqliteError("IndexExists finalize");
-        return NS_ERROR_FAILURE;
     }
 
     return ConvertResultCode(srv);
 }
 
 
 /**
  ** Transactions
--- a/storage/src/mozStorageStatement.cpp
+++ b/storage/src/mozStorageStatement.cpp
@@ -112,17 +112,18 @@ mozStorageStatement::Initialize(mozIStor
     if (mDBStatement) {
         sqlite3_finalize(mDBStatement);
         mDBStatement = nsnull;
     }
 
     int nRetries = 0;
 
     while (nRetries < 2) {
-        srv = sqlite3_prepare (db, nsPromiseFlatCString(aSQLStatement).get(), aSQLStatement.Length(), &mDBStatement, NULL);
+        srv = sqlite3_prepare_v2(db, nsPromiseFlatCString(aSQLStatement).get(),
+                                 aSQLStatement.Length(), &mDBStatement, NULL);
         if ((srv == SQLITE_SCHEMA && nRetries != 0) ||
             (srv != SQLITE_SCHEMA && srv != SQLITE_OK))
         {
 #ifdef PR_LOGGING
             PR_LOG(gStorageLog, PR_LOG_ERROR, ("Sqlite statement prepare error: %d '%s'", srv, sqlite3_errmsg(db)));
             PR_LOG(gStorageLog, PR_LOG_ERROR, ("Statement was: '%s'", nsPromiseFlatCString(aSQLStatement).get()));
 #endif
             return NS_ERROR_FAILURE;
@@ -407,78 +408,49 @@ mozStorageStatement::ExecuteStep(PRBool 
         // check if we need to recreate this statement before executing
         if (sqlite3_expired(mDBStatement)) {
             PR_LOG(gStorageLog, PR_LOG_DEBUG, ("Statement expired, recreating before step"));
             rv = Recreate();
             NS_ENSURE_SUCCESS(rv, rv);
         }
     }
 
-    int nRetries = 0;
-
-    while (nRetries < 2) {
-        int srv = sqlite3_step (mDBStatement);
+    int srv = sqlite3_step (mDBStatement);
 
 #ifdef PR_LOGGING
-        if (srv != SQLITE_ROW && srv != SQLITE_DONE)
-        {
-            nsCAutoString errStr;
-            mDBConnection->GetLastErrorString(errStr);
-            PR_LOG(gStorageLog, PR_LOG_DEBUG, ("mozStorageStatement::ExecuteStep error: %s", errStr.get()));
-        }
+    if (srv != SQLITE_ROW && srv != SQLITE_DONE)
+    {
+        nsCAutoString errStr;
+        mDBConnection->GetLastErrorString(errStr);
+        PR_LOG(gStorageLog, PR_LOG_DEBUG, ("mozStorageStatement::ExecuteStep error: %s", errStr.get()));
+    }
 #endif
 
-        // SQLITE_ROW and SQLITE_DONE are non-errors
-        if (srv == SQLITE_ROW) {
-            // we got a row back
-            mExecuting = PR_TRUE;
-            *_retval = PR_TRUE;
-            return NS_OK;
-        } else if (srv == SQLITE_DONE) {
-            // statement is done (no row returned)
-            mExecuting = PR_FALSE;
-            *_retval = PR_FALSE;
-            return NS_OK;
-        } else if (srv == SQLITE_BUSY ||
-                   srv == SQLITE_MISUSE)
-        {
-            mExecuting = PR_FALSE;
-            return NS_ERROR_FAILURE;
-        } else if (srv == SQLITE_SCHEMA) {
-            // step should never return SQLITE_SCHEMA
-            NS_NOTREACHED("sqlite3_step returned SQLITE_SCHEMA!");
-            return NS_ERROR_FAILURE;
-        } else if (srv == SQLITE_ERROR) {
-            // so we may end up with a SQLITE_ERROR/SQLITE_SCHEMA only after
-            // we reset, because SQLite's error reporting story
-            // sucks.
-            if (mExecuting == PR_TRUE) {
-                PR_LOG(gStorageLog, PR_LOG_ERROR, ("SQLITE_ERROR after mExecuting was true!"));
-
-                mExecuting = PR_FALSE;
-                return NS_ERROR_FAILURE;
-            }
-
-            srv = sqlite3_reset(mDBStatement);
-            if (srv == SQLITE_SCHEMA) {
-                rv = Recreate();
-                NS_ENSURE_SUCCESS(rv, rv);
-
-                nRetries++;
-            } else {
-                return NS_ERROR_FAILURE;
-            }
-        } else {
-            // something that shouldn't happen happened
-            NS_ERROR ("sqlite3_step returned an error code we don't know about!");
-        }
+    // SQLITE_ROW and SQLITE_DONE are non-errors
+    if (srv == SQLITE_ROW) {
+        // we got a row back
+        mExecuting = PR_TRUE;
+        *_retval = PR_TRUE;
+        return NS_OK;
+    } else if (srv == SQLITE_DONE) {
+        // statement is done (no row returned)
+        mExecuting = PR_FALSE;
+        *_retval = PR_FALSE;
+        return NS_OK;
+    } else if (srv == SQLITE_BUSY || srv == SQLITE_MISUSE) {
+        mExecuting = PR_FALSE;
+        return NS_ERROR_FAILURE;
+    } else if (mExecuting == PR_TRUE) {
+#ifdef PR_LOGGING
+        PR_LOG(gStorageLog, PR_LOG_ERROR, ("SQLite error after mExecuting was true!"));
+#endif
+        mExecuting = PR_FALSE;
     }
 
-    // shouldn't get here
-    return NS_ERROR_FAILURE;
+    return ConvertResultCode(srv);
 }
 
 /* [noscript,notxpcom] sqlite3stmtptr getNativeStatementPointer(); */
 sqlite3_stmt*
 mozStorageStatement::GetNativeStatementPointer()
 {
     return mDBStatement;
 }
--- a/storage/test/unit/test_storage_progresshandler.js
+++ b/storage/test/unit/test_storage_progresshandler.js
@@ -101,19 +101,19 @@ function test_handler_abort()
   msc.setProgressHandler(50, testProgressHandler);
   // Some long-executing request
   var stmt = createStatement(
     "SELECT SUM(t1.num * t2.num) FROM handler_tests AS t1, handler_tests AS t2");
   try {
     while(stmt.executeStep());
     do_throw("We shouldn't get here!");
   } catch (e) {
-    do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
+    do_check_eq(Cr.NS_ERROR_ABORT, e.result);
     // Magic value: callback abort
-    do_check_eq(msc.lastError, 4);
+    //do_check_eq(msc.lastError, 4); XXX see Bug 387609
   }
 }
 
 var tests = [test_handler_registration, test_handler_return,
              test_handler_removal, test_handler_call,
              test_handler_abort];
 
 function run_test()