LCOV - code coverage report
Current view: directory - storage/src - mozStoragePrivateHelpers.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 73 67 91.8 %
Date: 2012-06-02 Functions: 8 8 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
       2                 :  * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
       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                 :  * Mozilla Corporation
      20                 :  * Portions created by the Initial Developer are Copyright (C) 2009
      21                 :  * the Initial Developer. All Rights Reserved.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
      25                 :  *   Shawn Wilsher <me@shawnwilsher.com>
      26                 :  *
      27                 :  * Alternatively, the contents of this file may be used under the terms of
      28                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      29                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      30                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      31                 :  * of those above. If you wish to allow use of your version of this file only
      32                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      33                 :  * use your version of this file under the terms of the MPL, indicate your
      34                 :  * decision by deleting the provisions above and replace them with the notice
      35                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      36                 :  * the provisions above, a recipient may use your version of this file under
      37                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      38                 :  *
      39                 :  * ***** END LICENSE BLOCK ***** */
      40                 : 
      41                 : #include "sqlite3.h"
      42                 : 
      43                 : #include "jsapi.h"
      44                 : #include "jsfriendapi.h"
      45                 : 
      46                 : #include "nsPrintfCString.h"
      47                 : #include "nsString.h"
      48                 : #include "nsError.h"
      49                 : #include "mozilla/Mutex.h"
      50                 : #include "mozilla/CondVar.h"
      51                 : #include "nsThreadUtils.h"
      52                 : #include "nsJSUtils.h"
      53                 : 
      54                 : #include "Variant.h"
      55                 : #include "mozStoragePrivateHelpers.h"
      56                 : #include "mozIStorageStatement.h"
      57                 : #include "mozIStorageCompletionCallback.h"
      58                 : #include "mozIStorageBindingParams.h"
      59                 : 
      60                 : #include "prlog.h"
      61                 : #ifdef PR_LOGGING
      62                 : extern PRLogModuleInfo* gStorageLog;
      63                 : #endif
      64                 : 
      65                 : namespace mozilla {
      66                 : namespace storage {
      67                 : 
      68                 : nsresult
      69           85979 : convertResultCode(int aSQLiteResultCode)
      70                 : {
      71                 :   // Drop off the extended result bits of the result code.
      72           85979 :   int rc = aSQLiteResultCode & 0xFF;
      73                 : 
      74           85979 :   switch (rc) {
      75                 :     case SQLITE_OK:
      76                 :     case SQLITE_ROW:
      77                 :     case SQLITE_DONE:
      78           85886 :       return NS_OK;
      79                 :     case SQLITE_CORRUPT:
      80                 :     case SQLITE_NOTADB:
      81              13 :       return NS_ERROR_FILE_CORRUPTED;
      82                 :     case SQLITE_PERM:
      83                 :     case SQLITE_CANTOPEN:
      84               4 :       return NS_ERROR_FILE_ACCESS_DENIED;
      85                 :     case SQLITE_BUSY:
      86               1 :       return NS_ERROR_STORAGE_BUSY;
      87                 :     case SQLITE_LOCKED:
      88               6 :       return NS_ERROR_FILE_IS_LOCKED;
      89                 :     case SQLITE_READONLY:
      90               2 :       return NS_ERROR_FILE_READ_ONLY;
      91                 :     case SQLITE_IOERR:
      92               0 :       return NS_ERROR_STORAGE_IOERR;
      93                 :     case SQLITE_FULL:
      94                 :     case SQLITE_TOOBIG:
      95               0 :       return NS_ERROR_FILE_NO_DEVICE_SPACE;
      96                 :     case SQLITE_NOMEM:
      97               0 :       return NS_ERROR_OUT_OF_MEMORY;
      98                 :     case SQLITE_MISUSE:
      99               0 :       return NS_ERROR_UNEXPECTED;
     100                 :     case SQLITE_ABORT:
     101                 :     case SQLITE_INTERRUPT:
     102               2 :       return NS_ERROR_ABORT;
     103                 :     case SQLITE_CONSTRAINT:
     104              43 :       return NS_ERROR_STORAGE_CONSTRAINT;
     105                 :   }
     106                 : 
     107                 :   // generic error
     108                 : #ifdef DEBUG
     109              44 :   nsCAutoString message;
     110              22 :   message.AppendLiteral("SQLite returned error code ");
     111              22 :   message.AppendInt(rc);
     112              22 :   message.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
     113              22 :   NS_WARNING(message.get());
     114                 : #endif
     115              22 :   return NS_ERROR_FAILURE;
     116                 : }
     117                 : 
     118                 : void
     119          178590 : checkAndLogStatementPerformance(sqlite3_stmt *aStatement)
     120                 : {
     121                 :   // Check to see if the query performed sorting operations or not.  If it
     122                 :   // did, it may need to be optimized!
     123          178590 :   int count = ::sqlite3_stmt_status(aStatement, SQLITE_STMTSTATUS_SORT, 1);
     124          178589 :   if (count <= 0)
     125          177356 :     return;
     126                 : 
     127            1233 :   const char *sql = ::sqlite3_sql(aStatement);
     128                 : 
     129                 :   // Check to see if this is marked to not warn
     130            1233 :   if (::strstr(sql, "/* do not warn (bug "))
     131             700 :     return;
     132                 : 
     133            1066 :   nsCAutoString message;
     134             533 :   message.AppendInt(count);
     135             533 :   if (count == 1)
     136             440 :     message.Append(" sort operation has ");
     137                 :   else
     138              93 :     message.Append(" sort operations have ");
     139             533 :   message.Append("occurred for the SQL statement '");
     140            1066 :   nsPrintfCString address("0x%p", aStatement);
     141             533 :   message.Append(address);
     142                 :   message.Append("'.  See https://developer.mozilla.org/En/Storage/Warnings "
     143             533 :                  "details.");
     144             533 :   NS_WARNING(message.get());
     145                 : }
     146                 : 
     147                 : nsIVariant *
     148          115514 : convertJSValToVariant(
     149                 :   JSContext *aCtx,
     150                 :   jsval aValue)
     151                 : {
     152          115514 :   if (JSVAL_IS_INT(aValue))
     153           98670 :     return new IntegerVariant(JSVAL_TO_INT(aValue));
     154                 : 
     155           66179 :   if (JSVAL_IS_DOUBLE(aValue))
     156           24434 :     return new FloatVariant(JSVAL_TO_DOUBLE(aValue));
     157                 : 
     158           53962 :   if (JSVAL_IS_STRING(aValue)) {
     159           35820 :     JSString *str = JSVAL_TO_STRING(aValue);
     160           71640 :     nsDependentJSString value;
     161           35820 :     if (!value.init(aCtx, str))
     162               0 :         return nsnull;
     163           35820 :     return new TextVariant(value);
     164                 :   }
     165                 : 
     166           18142 :   if (JSVAL_IS_BOOLEAN(aValue))
     167              18 :     return new IntegerVariant((aValue == JSVAL_TRUE) ? 1 : 0);
     168                 : 
     169           18133 :   if (JSVAL_IS_NULL(aValue))
     170           18128 :     return new NullVariant();
     171                 : 
     172               5 :   if (JSVAL_IS_OBJECT(aValue)) {
     173               5 :     JSObject *obj = JSVAL_TO_OBJECT(aValue);
     174                 :     // We only support Date instances, all others fail.
     175               5 :     if (!::js_DateIsValid(aCtx, obj))
     176               1 :       return nsnull;
     177                 : 
     178               4 :     double msecd = ::js_DateGetMsecSinceEpoch(aCtx, obj);
     179               4 :     msecd *= 1000.0;
     180                 :     PRInt64 msec;
     181               4 :     LL_D2L(msec, msecd);
     182                 : 
     183               4 :     return new IntegerVariant(msec);
     184                 :   }
     185                 : 
     186               0 :   return nsnull;
     187                 : }
     188                 : 
     189                 : namespace {
     190                 : class CallbackEvent : public nsRunnable
     191            6396 : {
     192                 : public:
     193            1599 :   CallbackEvent(mozIStorageCompletionCallback *aCallback)
     194            1599 :   : mCallback(aCallback)
     195                 :   {
     196            1599 :   }
     197                 : 
     198            1599 :   NS_IMETHOD Run()
     199                 :   {
     200            1599 :     (void)mCallback->Complete();
     201            1599 :     return NS_OK;
     202                 :   }
     203                 : private:
     204                 :   nsCOMPtr<mozIStorageCompletionCallback> mCallback;
     205                 : };
     206                 : } // anonymous namespace
     207                 : already_AddRefed<nsIRunnable>
     208            1599 : newCompletionEvent(mozIStorageCompletionCallback *aCallback)
     209                 : {
     210            1599 :   NS_ASSERTION(aCallback, "Passing a null callback is a no-no!");
     211            3198 :   nsCOMPtr<nsIRunnable> event = new CallbackEvent(aCallback);
     212            1599 :   return event.forget();
     213                 : }
     214                 : 
     215                 : 
     216                 : 
     217                 : } // namespace storage
     218                 : } // namespace mozilla

Generated by: LCOV version 1.7