Bug 483157 - mozStorageStatementRow needs to use mozIStorageStatement API
authorShawn Wilsher <sdwilsh@shawnwilsher.com>
Fri, 20 Mar 2009 18:41:01 -0400
changeset 26429 514e4089924d889c7a2dcecd0e081ab55bd4e2e2
parent 26428 61440f2eb1f58093a9ae4814f2547f7d92c895f4
child 26430 dee8dab15435086e3afdf1146a19cebd1bf23d6d
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs483157
milestone1.9.2a1pre
Bug 483157 - mozStorageStatementRow needs to use mozIStorageStatement API This makes our language helper use public API instead of calling SQLite directly, which centralizes some of our logic. r=asuth r=bent
storage/src/mozStorageStatementRow.cpp
--- a/storage/src/mozStorageStatementRow.cpp
+++ b/storage/src/mozStorageStatementRow.cpp
@@ -41,18 +41,16 @@
 #include "nsMemory.h"
 #include "nsString.h"
 
 #include "mozStorageStatementRow.h"
 
 #include "jsapi.h"
 #include "jsdate.h"
 
-#include "sqlite3.h"
-
 /*************************************************************************
  ****
  **** mozStorageStatementRow
  ****
  *************************************************************************/
 
 NS_IMPL_ISUPPORTS2(mozStorageStatementRow, mozIStorageStatementRow, nsIXPCScriptable)
 
@@ -95,44 +93,65 @@ mozStorageStatementRow::GetProperty(nsIX
     NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
 
     if (JSVAL_IS_STRING(id)) {
         nsDependentCString jsid(::JS_GetStringBytes(JSVAL_TO_STRING(id)));
 
         PRUint32 idx;
         nsresult rv = mStatement->GetColumnIndex(jsid, &idx);
         NS_ENSURE_SUCCESS(rv, rv);
-        int ctype = sqlite3_column_type(NativeStatement(), idx);
+        PRInt32 type;
+        rv = mStatement->GetTypeOfIndex(idx, &type);
+        NS_ENSURE_SUCCESS(rv, rv);
 
-        if (ctype == SQLITE_INTEGER || ctype == SQLITE_FLOAT) {
-            double dval = sqlite3_column_double(NativeStatement(), idx);
+        if (type == mozIStorageValueArray::VALUE_TYPE_INTEGER ||
+            type == mozIStorageValueArray::VALUE_TYPE_FLOAT) {
+            double dval;
+            rv = mStatement->GetDouble(idx, &dval);
+            NS_ENSURE_SUCCESS(rv, rv);
             if (!JS_NewNumberValue(cx, dval, vp)) {
                 *_retval = PR_FALSE;
                 return NS_OK;
             }
-        } else if (ctype == SQLITE_TEXT) {
-            JSString *str = JS_NewUCStringCopyN(cx,
-                                                (jschar*) sqlite3_column_text16(NativeStatement(), idx),
-                                                sqlite3_column_bytes16(NativeStatement(), idx)/2);
+        }
+        else if (type == mozIStorageValueArray::VALUE_TYPE_TEXT) {
+            PRUint32 bytes;
+            const jschar *sval = reinterpret_cast<const jschar *>(
+                mStatement->AsSharedWString(idx, &bytes)
+            );
+            JSString *str = JS_NewUCStringCopyN(cx, sval, bytes / 2);
             if (!str) {
                 *_retval = PR_FALSE;
                 return NS_OK;
             }
             *vp = STRING_TO_JSVAL(str);
-        } else if (ctype == SQLITE_BLOB) {
-            JSString *str = JS_NewStringCopyN(cx,
-                                              (char*) sqlite3_column_blob(NativeStatement(), idx),
-                                              sqlite3_column_bytes(NativeStatement(), idx));
-            if (!str) {
+        }
+        else if (type == mozIStorageValueArray::VALUE_TYPE_BLOB) {
+            PRUint32 length;
+            const PRUint8 *blob = mStatement->AsSharedBlob(idx, &length);
+            JSObject *obj = JS_NewArrayObject(cx, length, nsnull);
+            if (!obj) {
                 *_retval = PR_FALSE;
                 return NS_OK;
             }
-        } else if (ctype == SQLITE_NULL) {
+            *vp = OBJECT_TO_JSVAL(obj);
+
+            // Copy the blob over to the JS array.
+            for (PRUint32 i = 0; i < length; i++) {
+                jsval val = INT_TO_JSVAL(blob[i]);
+                if (!JS_SetElement(cx, obj, i, &val)) {
+                    *_retval = PR_FALSE;
+                    return NS_OK;
+                }
+            }
+        }
+        else if (type == mozIStorageValueArray::VALUE_TYPE_NULL) {
             *vp = JSVAL_NULL;
-        } else {
+        }
+        else {
             NS_ERROR("sqlite3_column_type returned unknown column type, what's going on?");
         }
     }
 
     return NS_OK;
 }