Bug 482159 - Add support for nsCString in mozStorageVariant
authorShawn Wilsher <sdwilsh@shawnwilsher.com>
Fri, 20 Mar 2009 16:28:16 -0400
changeset 26422 56722f4fc2f64e8cfb5116f664e12a6a4376b11e
parent 26421 a809eae393418f7051156f6c8fb33e9bbf81a1e6
child 26423 762b098aed655532d406d052d78ef693d99832c1
push idunknown
push userunknown
push dateunknown
bugs482159
milestone1.9.2a1pre
Bug 482159 - Add support for nsCString in mozStorageVariant r=asuth r=bent
storage/src/mozStorageRow.cpp
storage/src/mozStorageVariant.h
--- a/storage/src/mozStorageRow.cpp
+++ b/storage/src/mozStorageRow.cpp
@@ -74,17 +74,17 @@ mozStorageRow::initialize(sqlite3_stmt *
       case SQLITE_INTEGER:
         variant = new mozStorageInteger(sqlite3_column_int64(aStatement, i));
         break;
       case SQLITE_FLOAT:
         variant = new mozStorageFloat(sqlite3_column_double(aStatement, i));
         break;
       case SQLITE_TEXT:
       {
-        nsAutoString str(
+        nsDependentString str(
           static_cast<const PRUnichar *>(sqlite3_column_text16(aStatement, i))
         );
         variant = new mozStorageText(str);
         break;
       }
       case SQLITE_NULL:
         variant = new mozStorageNull();
         break;
--- a/storage/src/mozStorageVariant.h
+++ b/storage/src/mozStorageVariant.h
@@ -46,16 +46,17 @@
 
 /**
  * This class is used by the storage module whenever an nsIVariant needs to be
  * returned.  We provide traits for the basic sqlite types to make use easier.
  * The following types map to the indicated sqlite type:
  * PRInt64   -> INTEGER (use mozStorageInteger)
  * double    -> FLOAT (use mozStorageFloat)
  * nsString  -> TEXT (use mozStorageText)
+ * nsCString -> TEXT (use mozStorageUTF8Text)
  * PRUint8[] -> BLOB (use mozStorageBlob)
  * nsnull    -> NULL (use mozStorageNull)
  */
 
 #define NO_CONVERSION return NS_ERROR_CANNOT_CONVERT_DATA;
 
 ////////////////////////////////////////////////////////////////////////////////
 //// Base Class
@@ -244,16 +245,48 @@ struct variant_text_traits<nsString>
   static inline nsresult asString(const nsString &aValue,
                                   nsAString &_result)
   {
     _result = aValue;
     return NS_OK;
   }
 };
 
+template < >
+struct variant_traits<nsCString>
+{
+  static inline PRUint16 type() { return nsIDataType::VTYPE_UTF8STRING; }
+};
+template < >
+struct variant_storage_traits<nsCString>
+{
+  typedef const nsACString & ConstructorType;
+  typedef nsCString StorageType;
+  static inline StorageType storage_conversion(ConstructorType aText)
+  {
+    return StorageType(aText);
+  }
+};
+template < >
+struct variant_text_traits<nsCString>
+{
+  static inline nsresult asUTF8String(const nsCString &aValue,
+                                      nsACString &_result)
+  {
+    _result = aValue;
+    return NS_OK;
+  }
+  static inline nsresult asString(const nsCString &aValue,
+                                  nsAString &_result)
+  {
+    CopyUTF8toUTF16(aValue, _result);
+    return NS_OK;
+  }
+};
+
 /**
  * BLOB types
  */
 
 template < >
 struct variant_traits<PRUint8[]>
 {
   static inline PRUint16 type() { return nsIDataType::VTYPE_ARRAY; }
@@ -343,12 +376,13 @@ private:
 
 
 ////////////////////////////////////////////////////////////////////////////////
 //// Handy typedefs!  Use these for the right mapping.
 
 typedef mozStorageVariant<PRInt64> mozStorageInteger;
 typedef mozStorageVariant<double> mozStorageFloat;
 typedef mozStorageVariant<nsString> mozStorageText;
+typedef mozStorageVariant<nsCString> mozStorageUTF8Text;
 typedef mozStorageVariant<PRUint8[]> mozStorageBlob;
 typedef mozStorageVariant_base mozStorageNull;
 
 #endif // __mozStorageVariant_h__