LCOV - code coverage report
Current view: directory - storage/src - mozStorageStatementData.h (source / functions) Found Hit Coverage
Test: app.info Lines: 36 36 100.0 %
Date: 2012-06-02 Functions: 10 10 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
       2                 :  * vim: sw=2 ts=2 sts=2 et
       3                 :  * ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is mozilla.org code.
      17                 :  *
      18                 :  * The Initial Developer of the Original Code is
      19                 :  * the Mozilla Foundation.
      20                 :  * Portions created by the Initial Developer are Copyright (C) 2009
      21                 :  * the Initial Developer. All Rights Reserved.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *   Shawn Wilsher <me@shawnwilsher.com> (Original Author)
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #ifndef mozStorageStatementData_h
      41                 : #define mozStorageStatementData_h
      42                 : 
      43                 : #include "sqlite3.h"
      44                 : 
      45                 : #include "nsAutoPtr.h"
      46                 : #include "nsTArray.h"
      47                 : #include "nsIEventTarget.h"
      48                 : #include "mozilla/Util.h"
      49                 : #include "nsThreadUtils.h"
      50                 : 
      51                 : #include "mozStorageBindingParamsArray.h"
      52                 : #include "mozIStorageBaseStatement.h"
      53                 : #include "mozStorageConnection.h"
      54                 : #include "StorageBaseStatementInternal.h"
      55                 : 
      56                 : struct sqlite3_stmt;
      57                 : 
      58                 : namespace mozilla {
      59                 : namespace storage {
      60                 : 
      61                 : class StatementData
      62          216325 : {
      63                 : public:
      64           54081 :   StatementData(sqlite3_stmt *aStatement,
      65                 :                 already_AddRefed<BindingParamsArray> aParamsArray,
      66                 :                 StorageBaseStatementInternal *aStatementOwner)
      67                 :   : mStatement(aStatement)
      68                 :   , mParamsArray(aParamsArray)
      69           54081 :   , mStatementOwner(aStatementOwner)
      70                 :   {
      71           54081 :     NS_PRECONDITION(mStatementOwner, "Must have a statement owner!");
      72           54081 :   }
      73           54081 :   StatementData(const StatementData &aSource)
      74                 :   : mStatement(aSource.mStatement)
      75                 :   , mParamsArray(aSource.mParamsArray)
      76           54081 :   , mStatementOwner(aSource.mStatementOwner)
      77                 :   {
      78           54081 :     NS_PRECONDITION(mStatementOwner, "Must have a statement owner!");
      79           54081 :   }
      80           54082 :   StatementData()
      81           54082 :   {
      82           54082 :   }
      83                 : 
      84                 :   /**
      85                 :    * Return the sqlite statement, fetching it from the storage statement.  In
      86                 :    * the case of AsyncStatements this may actually create the statement 
      87                 :    */
      88          151819 :   inline int getSqliteStatement(sqlite3_stmt **_stmt)
      89                 :   {
      90          151819 :     if (!mStatement) {
      91           37831 :       int rc = mStatementOwner->getAsyncStatement(&mStatement);
      92           37831 :       NS_ENSURE_TRUE(rc == SQLITE_OK, rc);
      93                 :     }
      94          151811 :     *_stmt = mStatement;
      95          151811 :     return SQLITE_OK;
      96                 :   }
      97                 : 
      98           49122 :   operator BindingParamsArray *() const { return mParamsArray; }
      99                 : 
     100                 :   /**
     101                 :    * Provide the ability to coerce back to a sqlite3 * connection for purposes 
     102                 :    * of getting an error message out of it.
     103                 :    */
     104                 :   operator sqlite3 *() const
     105                 :   {
     106                 :     return mStatementOwner->getOwner()->GetNativeConnection();
     107                 :   }
     108                 : 
     109                 :   /**
     110                 :    * NULLs out our sqlite3_stmt (it is held by the owner) after reseting it and
     111                 :    * clear all bindings to it.  This is expected to occur on the async thread.
     112                 :    */
     113           54081 :   inline void finalize()
     114                 :   {
     115           54081 :     NS_PRECONDITION(mStatementOwner, "Must have a statement owner!");
     116                 : #ifdef DEBUG
     117                 :     {
     118                 :       nsCOMPtr<nsIEventTarget> asyncThread =
     119          108162 :         mStatementOwner->getOwner()->getAsyncExecutionTarget();
     120                 :       // It's possible that we are shutting down the async thread, and this
     121                 :       // method would return NULL as a result.
     122           54081 :       if (asyncThread) {
     123                 :         bool onAsyncThread;
     124           53921 :         NS_ASSERTION(NS_SUCCEEDED(asyncThread->IsOnCurrentThread(&onAsyncThread)) && onAsyncThread,
     125                 :                      "This should only be running on the async thread!");
     126                 :       }
     127                 :     }
     128                 : #endif
     129                 :     // In the AsyncStatement case we may never have populated mStatement if the
     130                 :     // AsyncExecuteStatements got canceled or a failure occurred in constructing
     131                 :     // the statement.
     132           54081 :     if (mStatement) {
     133           54061 :       (void)::sqlite3_reset(mStatement);
     134           54061 :       (void)::sqlite3_clear_bindings(mStatement);
     135           54061 :       mStatement = NULL;
     136                 :     }
     137           54081 :   }
     138                 : 
     139                 :   /**
     140                 :    * Indicates if this statement has parameters to be bound before it is
     141                 :    * executed.
     142                 :    *
     143                 :    * @return true if the statement has parameters to bind against, false
     144                 :    *         otherwise.
     145                 :    */
     146           54061 :   inline bool hasParametersToBeBound() const { return !!mParamsArray; }
     147                 :   /**
     148                 :    * Indicates the number of implicit statements generated by this statement
     149                 :    * requiring a transaction for execution.  For example a single statement
     150                 :    * with N BindingParams will execute N implicit staments.
     151                 :    *
     152                 :    * @return number of statements requiring a transaction for execution.
     153                 :    *
     154                 :    * @note In the case of AsyncStatements this may actually create the
     155                 :    *       statement.
     156                 :    */
     157           48632 :   inline PRUint32 needsTransaction()
     158                 :   {
     159           48632 :     MOZ_ASSERT(!NS_IsMainThread());
     160                 :     // Be sure to use the getSqliteStatement helper, since sqlite3_stmt_readonly
     161                 :     // can only analyze prepared statements and AsyncStatements are prepared
     162                 :     // lazily.
     163                 :     sqlite3_stmt *stmt;
     164           48632 :     int rc = getSqliteStatement(&stmt);
     165           48632 :     if (SQLITE_OK != rc || ::sqlite3_stmt_readonly(stmt)) {
     166           21155 :       return 0;
     167                 :     }
     168           27477 :     return mParamsArray ? mParamsArray->length() : 1;
     169                 :   }
     170                 : 
     171                 : private:
     172                 :   sqlite3_stmt *mStatement;
     173                 :   nsRefPtr<BindingParamsArray> mParamsArray;
     174                 : 
     175                 :   /**
     176                 :    * We hold onto a reference of the statement's owner so it doesn't get
     177                 :    * destroyed out from under us.
     178                 :    */
     179                 :   nsCOMPtr<StorageBaseStatementInternal> mStatementOwner;
     180                 : };
     181                 : 
     182                 : } // namespace storage
     183                 : } // namespace mozilla
     184                 : 
     185                 : #endif // mozStorageStatementData_h

Generated by: LCOV version 1.7