Bug 489018 - Rename mozStorageUnicodeFunctions.* to mozStorageSQLFunctions.*
authorShawn Wilsher <me@shawnwilsher.com>
Mon, 20 Apr 2009 07:59:52 -0700
changeset 27529 84df98a671f2f58a436e7c9d5252114ac1707678
parent 27528 2b21f8f4a8ce69795f9679b74a931eedf03e4849
child 27530 25386c8ca67744ca4410c0fa8ed63fa5ed829b4a
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)
bugs489018
milestone1.9.2a1pre
Bug 489018 - Rename mozStorageUnicodeFunctions.* to mozStorageSQLFunctions.* New filename better reflects current (and future) contents. Also updates the files to follow the storage style guidelines. r=asuth
storage/src/Makefile.in
storage/src/mozStorageConnection.cpp
storage/src/mozStorageSQLFunctions.cpp
storage/src/mozStorageSQLFunctions.h
storage/src/mozStorageUnicodeFunctions.cpp
storage/src/mozStorageUnicodeFunctions.h
--- a/storage/src/Makefile.in
+++ b/storage/src/Makefile.in
@@ -64,17 +64,17 @@ REQUIRES = xpcom \
 CPPSRCS = \
   mozStorageService.cpp \
   mozStorageConnection.cpp \
   mozStorageStatement.cpp \
   mozStorageStatementWrapper.cpp \
   mozStorageStatementParams.cpp \
   mozStorageStatementRow.cpp \
   mozStorageValueArray.cpp \
-  mozStorageUnicodeFunctions.cpp \
+  mozStorageSQLFunctions.cpp \
   mozStorageRow.cpp \
   mozStorageResultSet.cpp \
   mozStorageError.cpp \
   mozStorageEvents.cpp \
   mozStorageStatementJSHelper.cpp \
   mozStoragePrivateHelpers.cpp \
   mozStorageVariant.cpp \
   $(NULL)
--- a/storage/src/mozStorageConnection.cpp
+++ b/storage/src/mozStorageConnection.cpp
@@ -51,17 +51,17 @@
 #include "nsIPrefService.h"
 #include "nsIPrefBranch.h"
 #include "nsThreadUtils.h"
 
 #include "mozIStorageAggregateFunction.h"
 #include "mozIStorageFunction.h"
 
 #include "mozStorageEvents.h"
-#include "mozStorageUnicodeFunctions.h"
+#include "mozStorageSQLFunctions.h"
 #include "mozStorageConnection.h"
 #include "mozStorageService.h"
 #include "mozStorageStatement.h"
 #include "mozStorageValueArray.h"
 #include "mozStoragePrivateHelpers.h"
 
 #include "prlog.h"
 #include "prprf.h"
@@ -364,19 +364,18 @@ Connection::initialize(nsIFile *aDatabas
 
   nsCAutoString leafName(":memory");
   if (aDatabaseFile)
     (void)aDatabaseFile->GetNativeLeafName(leafName);
   PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Opening connection to '%s' (%p)",
                                       leafName.get(), this));
 #endif
 
-  // Hook up i18n functions
-  srv = StorageUnicodeFunctions::RegisterFunctions(mDBConn);
-  if (srv != SQLITE_OK) {
+  // Register our built-in SQL functions.
+  if (registerFunctions(mDBConn) != SQLITE_OK) {
     mDBConn = nsnull;
     return ConvertResultCode(srv);
   }
 
   // Execute a dummy statement to force the db open, and to verify if it is
   // valid or not.
   sqlite3_stmt *stmt;
   srv = ::sqlite3_prepare_v2(mDBConn, "SELECT * FROM sqlite_master", -1, &stmt,
rename from storage/src/mozStorageUnicodeFunctions.cpp
rename to storage/src/mozStorageSQLFunctions.cpp
--- a/storage/src/mozStorageUnicodeFunctions.cpp
+++ b/storage/src/mozStorageSQLFunctions.cpp
@@ -11,17 +11,17 @@
  * Software distributed under the License is distributed on an "AS IS" basis,
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  * for the specific language governing rights and limitations under the
  * License.
  *
  * The Original Code is unicode functions code.
  *
  * The Initial Developer of the Original Code is
- * Mozilla Corporation. 
+ * Mozilla Corporation.
  * Portions created by the Initial Developer are Copyright (C) 2007
  * the Initial Developer. All Rights Reserved.
  *
  * This code is based off of icu.c from the sqlite code
  * whose original author is danielk1977
  *
  * Contributor(s):
  *   Shawn Wilsher <me@shawnwilsher.com> (Original Author)
@@ -35,75 +35,49 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
-#include "mozStorageUnicodeFunctions.h"
+#include "mozStorageSQLFunctions.h"
 #include "nsUnicharUtils.h"
 
-int
-StorageUnicodeFunctions::RegisterFunctions(sqlite3 *aDB)
-{
-  struct Functions {
-    const char *zName;
-    int nArg;
-    int enc;
-    void *pContext;
-    void (*xFunc)(sqlite3_context*, int, sqlite3_value**);
-  } functions[] = {
-    {"lower", 1, SQLITE_UTF16, 0,        caseFunction},
-    {"lower", 1, SQLITE_UTF8,  0,        caseFunction},
-    {"upper", 1, SQLITE_UTF16, (void*)1, caseFunction},
-    {"upper", 1, SQLITE_UTF8,  (void*)1, caseFunction},
+namespace mozilla {
+namespace storage {
 
-    {"like",  2, SQLITE_UTF16, 0,        likeFunction},
-    {"like",  2, SQLITE_UTF8,  0,        likeFunction},
-    {"like",  3, SQLITE_UTF16, 0,        likeFunction},
-    {"like",  3, SQLITE_UTF8,  0,        likeFunction},
-  };
+////////////////////////////////////////////////////////////////////////////////
+//// Local Helper Functions
+
+namespace {
 
-  int rv = SQLITE_OK;
-  for (unsigned i = 0; SQLITE_OK == rv && i < NS_ARRAY_LENGTH(functions); ++i) {
-    struct Functions *p = &functions[i];
-    rv = sqlite3_create_function(aDB, p->zName, p->nArg, p->enc, p->pContext,
-                                 p->xFunc, NULL, NULL);
-  }
-
-  return rv;
-}
-
-void
-StorageUnicodeFunctions::caseFunction(sqlite3_context *p,
-                                      int aArgc,
-                                      sqlite3_value **aArgv)
-{
-  NS_ASSERTION(1 == aArgc, "Invalid number of arguments!");
-
-  nsAutoString data(static_cast<const PRUnichar *>(sqlite3_value_text16(aArgv[0])));
-  PRBool toUpper = sqlite3_user_data(p) ? PR_TRUE : PR_FALSE;
-
-  if (toUpper)
-    ToUpperCase(data);
-  else 
-    ToLowerCase(data);
-
-  // Give sqlite our result
-  sqlite3_result_text16(p, data.get(), -1, SQLITE_TRANSIENT);
-}
-
-static int
+/**
+ * Performs the LIKE comparison of a string against a pattern.  For more detail
+ * see http://www.sqlite.org/lang_expr.html#like.
+ *
+ * @param aPatternItr
+ *        An iterator at the start of the pattern to check for.
+ * @param aPatternEnd
+ *        An iterator at the end of the pattern to check for.
+ * @param aStringItr
+ *        An iterator at the start of the string to check for the pattern.
+ * @param aStringEnd
+ *        An iterator at the end of the string to check for the pattern.
+ * @param aEscapeChar
+ *        The character to use for escaping symbols in the pattern.
+ * @returns 1 if the pattern is found, 0 otherwise.
+ */
+int
 likeCompare(nsAString::const_iterator aPatternItr,
             nsAString::const_iterator aPatternEnd,
             nsAString::const_iterator aStringItr,
             nsAString::const_iterator aStringEnd,
-            PRUnichar aEscape)
+            PRUnichar aEscapeChar)
 {
   const PRUnichar MATCH_ALL('%');
   const PRUnichar MATCH_ONE('_');
 
   PRBool lastWasEscape = PR_FALSE;
   while (aPatternItr != aPatternEnd) {
     /**
      * What we do in here is take a look at each character from the input
@@ -130,82 +104,147 @@ likeCompare(nsAString::const_iterator aP
         aPatternItr++;
       }
 
       // If we've hit the end of the pattern string, match
       if (aPatternItr == aPatternEnd)
         return 1;
 
       while (aStringItr != aStringEnd) {
-        if (likeCompare(aPatternItr, aPatternEnd, aStringItr, aStringEnd, aEscape)) {
+        if (likeCompare(aPatternItr, aPatternEnd, aStringItr, aStringEnd,
+                        aEscapeChar)) {
           // we've hit a match, so indicate this
           return 1;
         }
         aStringItr++;
       }
 
       // No match
       return 0;
-    } else if (!lastWasEscape && *aPatternItr == MATCH_ONE) {
+    }
+    else if (!lastWasEscape && *aPatternItr == MATCH_ONE) {
       // CASE 2
       if (aStringItr == aStringEnd) {
         // If we've hit the end of the string we are testing, no match
         return 0;
       }
       aStringItr++;
       lastWasEscape = PR_FALSE;
-    } else if (!lastWasEscape && *aPatternItr == aEscape) {
+    }
+    else if (!lastWasEscape && *aPatternItr == aEscapeChar) {
       // CASE 3
       lastWasEscape = PR_TRUE;
-    } else {
+    }
+    else {
       // CASE 4
-      if (ToUpperCase(*aStringItr) != ToUpperCase(*aPatternItr)) {
+      if (::ToUpperCase(*aStringItr) != ::ToUpperCase(*aPatternItr)) {
         // If we've hit a point where the strings don't match, there is no match
         return 0;
       }
       aStringItr++;
       lastWasEscape = PR_FALSE;
     }
-    
+
     aPatternItr++;
   }
 
   return aStringItr == aStringEnd;
 }
 
+} // anonymous namespace
+
+////////////////////////////////////////////////////////////////////////////////
+//// Exposed Functions
+
+int
+registerFunctions(sqlite3 *aDB)
+{
+  struct Functions {
+    const char *zName;
+    int nArg;
+    int enc;
+    void *pContext;
+    void (*xFunc)(::sqlite3_context*, int, sqlite3_value**);
+  } functions[] = {
+    {"lower", 1, SQLITE_UTF16, 0,        caseFunction},
+    {"lower", 1, SQLITE_UTF8,  0,        caseFunction},
+    {"upper", 1, SQLITE_UTF16, (void*)1, caseFunction},
+    {"upper", 1, SQLITE_UTF8,  (void*)1, caseFunction},
+
+    {"like",  2, SQLITE_UTF16, 0,        likeFunction},
+    {"like",  2, SQLITE_UTF8,  0,        likeFunction},
+    {"like",  3, SQLITE_UTF16, 0,        likeFunction},
+    {"like",  3, SQLITE_UTF8,  0,        likeFunction},
+  };
+
+  int rv = SQLITE_OK;
+  for (unsigned i = 0; SQLITE_OK == rv && i < NS_ARRAY_LENGTH(functions); ++i) {
+    struct Functions *p = &functions[i];
+    rv = ::sqlite3_create_function(aDB, p->zName, p->nArg, p->enc, p->pContext,
+                                   p->xFunc, NULL, NULL);
+  }
+
+  return rv;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+//// SQL Functions
+
+void
+caseFunction(sqlite3_context *aCtx,
+             int aArgc,
+             sqlite3_value **aArgv)
+{
+  NS_ASSERTION(1 == aArgc, "Invalid number of arguments!");
+
+  nsAutoString data(static_cast<const PRUnichar *>(::sqlite3_value_text16(aArgv[0])));
+  PRBool toUpper = ::sqlite3_user_data(aCtx) ? PR_TRUE : PR_FALSE;
+
+  if (toUpper)
+    ::ToUpperCase(data);
+  else
+    ::ToLowerCase(data);
+
+  // Set the result.
+  ::sqlite3_result_text16(aCtx, data.get(), -1, SQLITE_TRANSIENT);
+}
+
 /**
  * This implements the like() SQL function.  This is used by the LIKE operator.
  * The SQL statement 'A LIKE B' is implemented as 'like(B, A)', and if there is
  * an escape character, say E, it is implemented as 'like(B, A, E)'.
  */
 void
-StorageUnicodeFunctions::likeFunction(sqlite3_context *p,
-                                      int aArgc,
-                                      sqlite3_value **aArgv)
+likeFunction(sqlite3_context *aCtx,
+             int aArgc,
+             sqlite3_value **aArgv)
 {
   NS_ASSERTION(2 == aArgc || 3 == aArgc, "Invalid number of arguments!");
 
-  if (sqlite3_value_bytes(aArgv[0]) > SQLITE_MAX_LIKE_PATTERN_LENGTH) {
-    sqlite3_result_error(p, "LIKE or GLOB pattern too complex", SQLITE_TOOBIG);
+  if (::sqlite3_value_bytes(aArgv[0]) > SQLITE_MAX_LIKE_PATTERN_LENGTH) {
+    ::sqlite3_result_error(aCtx, "LIKE or GLOB pattern too complex",
+                           SQLITE_TOOBIG);
     return;
   }
 
-  if (!sqlite3_value_text16(aArgv[0]) || !sqlite3_value_text16(aArgv[1]))
+  if (!::sqlite3_value_text16(aArgv[0]) || !::sqlite3_value_text16(aArgv[1]))
     return;
 
-  nsDependentString A(static_cast<const PRUnichar *>(sqlite3_value_text16(aArgv[1])));
-  nsDependentString B(static_cast<const PRUnichar *>(sqlite3_value_text16(aArgv[0])));
+  nsDependentString A(static_cast<const PRUnichar *>(::sqlite3_value_text16(aArgv[1])));
+  nsDependentString B(static_cast<const PRUnichar *>(::sqlite3_value_text16(aArgv[0])));
   NS_ASSERTION(!B.IsEmpty(), "LIKE string must not be null!");
 
   PRUnichar E = 0;
   if (3 == aArgc)
-    E = static_cast<const PRUnichar *>(sqlite3_value_text16(aArgv[2]))[0];
+    E = static_cast<const PRUnichar *>(::sqlite3_value_text16(aArgv[2]))[0];
 
   nsAString::const_iterator itrString, endString;
   A.BeginReading(itrString);
   A.EndReading(endString);
   nsAString::const_iterator itrPattern, endPattern;
   B.BeginReading(itrPattern);
   B.EndReading(endPattern);
-  sqlite3_result_int(p, likeCompare(itrPattern, endPattern,
-                                    itrString, endString, E));
+  ::sqlite3_result_int(aCtx, likeCompare(itrPattern, endPattern, itrString,
+                                         endString, E));
 }
 
+} // namespace storage
+} // namespace mozilla
rename from storage/src/mozStorageUnicodeFunctions.h
rename to storage/src/mozStorageSQLFunctions.h
--- a/storage/src/mozStorageUnicodeFunctions.h
+++ b/storage/src/mozStorageSQLFunctions.h
@@ -11,17 +11,17 @@
  * Software distributed under the License is distributed on an "AS IS" basis,
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  * for the specific language governing rights and limitations under the
  * License.
  *
  * The Original Code is unicode functions code.
  *
  * The Initial Developer of the Original Code is
- * Mozilla Corporation. 
+ * Mozilla Corporation.
  * Portions created by the Initial Developer are Copyright (C) 2007
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Shawn Wilsher <me@shawnwilsher.com> (Original Author)
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -32,56 +32,63 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
-#ifndef _mozStorageUnicodeFunctions_h_
+#ifndef _mozStorageSQLFunctions_h_
 #define _mozStorageUnicodeFunctions_h_
 
 #include "sqlite3.h"
 #include "nscore.h"
 
-namespace StorageUnicodeFunctions {
+namespace mozilla {
+namespace storage {
 
-  /**
-   * Registers the functions with sqlite.
-   *
-   * @param aDB
-   *        The database we'll be registering the functions with.
-   * @return the sqlite status code.
-   */
-  NS_HIDDEN_(int) RegisterFunctions(sqlite3 *aDB);
+/**
+ * Registers the functions declared here with the specified database.
+ *
+ * @param aDB
+ *        The database we'll be registering the functions with.
+ * @return the SQLite status code indicating success or failure.
+ */
+NS_HIDDEN_(int) registerFunctions(sqlite3 *aDB);
+
+////////////////////////////////////////////////////////////////////////////////
+//// Predefined Functions
 
-  /**
-   * Does the upper and lowercase conversions for the SQL functions upper() and
-   * lower().
-   *
-   * @param p
-   *        The sqlite_context that this function is being called on.
-   * @param aArgc
-   *        The number of arguments the function is being called with.
-   * @param aArgv
-   *        An array of the arguments the functions is being called with.
-   */
-  NS_HIDDEN_(void) caseFunction(sqlite3_context *p,
-                                int aArgc,
-                                sqlite3_value **aArgv);
+/**
+ * Overridden function to perform the SQL functions UPPER and LOWER.  These
+ * support unicode, which the default implementations do not do.
+ *
+ * @param aCtx
+ *        The sqlite_context that this function is being called on.
+ * @param aArgc
+ *        The number of arguments the function is being called with.
+ * @param aArgv
+ *        An array of the arguments the functions is being called with.
+ */
+NS_HIDDEN_(void) caseFunction(sqlite3_context *aCtx,
+                              int aArgc,
+                              sqlite3_value **aArgv);
 
-  /**
-   * Performs the LIKE comparison in sqlite.
-   *
-   * @param p
-   *        The sqlite_context that this function is being called on.
-   * @param aArgc
-   *        The number of arguments the function is being called with.
-   * @param aArgv
-   *        An array of the arguments the functions is being called with.
-   */
-  NS_HIDDEN_(void) likeFunction(sqlite3_context *p,
-                                int aArgc,
-                                sqlite3_value **aArgv);
-}
+/**
+ * Overridden function to perform the SQL function LIKE.  This supports unicode,
+ * which the default implementation does not do.
+ *
+ * @param aCtx
+ *        The sqlite_context that this function is being called on.
+ * @param aArgc
+ *        The number of arguments the function is being called with.
+ * @param aArgv
+ *        An array of the arguments the functions is being called with.
+ */
+NS_HIDDEN_(void) likeFunction(sqlite3_context *aCtx,
+                              int aArgc,
+                              sqlite3_value **aArgv);
 
-#endif // _mozStorageUnicodeFunctions_h_
+} // namespace storage
+} // namespace mozilla
+
+#endif // _mozStorageSQLFunctions_h_