Bug 1380606 - Add an `IS_VALID_GUID()` SQL function to Places. r=mak
authorKit Cambridge <kit@yakshaving.ninja>
Thu, 05 Oct 2017 11:32:49 -0700
changeset 428334 455609e45fe7b0e59bcb874e86a3a88c002c0968
parent 428333 f1fb2969c2b3f1f4a0f8a4be8dbe06fd0b6ec01f
child 428335 479c9304b70343d8ede784bc8079513a6fab04ca
push id97
push userfmarier@mozilla.com
push dateSat, 14 Oct 2017 01:12:59 +0000
reviewersmak
bugs1380606
milestone58.0a1
Bug 1380606 - Add an `IS_VALID_GUID()` SQL function to Places. r=mak This exposes `IsValidGUID` to SQL, matching `GENERATE_GUID()` and making it easier to SELECT rows with invalid GUIDs. MozReview-Commit-ID: Dspm8A59P5L
toolkit/components/places/Database.cpp
toolkit/components/places/SQLFunctions.cpp
toolkit/components/places/SQLFunctions.h
--- a/toolkit/components/places/Database.cpp
+++ b/toolkit/components/places/Database.cpp
@@ -1325,16 +1325,18 @@ Database::InitFunctions()
   nsresult rv = GetUnreversedHostFunction::create(mMainConn);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = MatchAutoCompleteFunction::create(mMainConn);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = CalculateFrecencyFunction::create(mMainConn);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = GenerateGUIDFunction::create(mMainConn);
   NS_ENSURE_SUCCESS(rv, rv);
+  rv = IsValidGUIDFunction::create(mMainConn);
+  NS_ENSURE_SUCCESS(rv, rv);
   rv = FixupURLFunction::create(mMainConn);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = FrecencyNotificationFunction::create(mMainConn);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = StoreLastInsertedIdFunction::create(mMainConn);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = HashFunction::create(mMainConn);
   NS_ENSURE_SUCCESS(rv, rv);
--- a/toolkit/components/places/SQLFunctions.cpp
+++ b/toolkit/components/places/SQLFunctions.cpp
@@ -767,16 +767,50 @@ namespace places {
     nsresult rv = GenerateGUID(guid);
     NS_ENSURE_SUCCESS(rv, rv);
 
     NS_ADDREF(*_result = new UTF8TextVariant(guid));
     return NS_OK;
   }
 
 ////////////////////////////////////////////////////////////////////////////////
+//// GUID Validation Function
+
+  /* static */
+  nsresult
+  IsValidGUIDFunction::create(mozIStorageConnection *aDBConn)
+  {
+    RefPtr<IsValidGUIDFunction> function = new IsValidGUIDFunction();
+    return aDBConn->CreateFunction(
+      NS_LITERAL_CSTRING("is_valid_guid"), 1, function
+    );
+  }
+
+  NS_IMPL_ISUPPORTS(
+    IsValidGUIDFunction,
+    mozIStorageFunction
+  )
+
+  NS_IMETHODIMP
+  IsValidGUIDFunction::OnFunctionCall(mozIStorageValueArray *aArguments,
+                                      nsIVariant **_result)
+  {
+    // Must have non-null function arguments.
+    MOZ_ASSERT(aArguments);
+
+    nsAutoCString guid;
+    aArguments->GetUTF8String(0, guid);
+
+    RefPtr<nsVariant> result = new nsVariant();
+    result->SetAsBool(IsValidGUID(guid));
+    result.forget(_result);
+    return NS_OK;
+  }
+
+////////////////////////////////////////////////////////////////////////////////
 //// Get Unreversed Host Function
 
   /* static */
   nsresult
   GetUnreversedHostFunction::create(mozIStorageConnection *aDBConn)
   {
     RefPtr<GetUnreversedHostFunction> function = new GetUnreversedHostFunction();
     nsresult rv = aDBConn->CreateFunction(
--- a/toolkit/components/places/SQLFunctions.h
+++ b/toolkit/components/places/SQLFunctions.h
@@ -237,16 +237,39 @@ public:
    *        The database connection to register with.
    */
   static nsresult create(mozIStorageConnection *aDBConn);
 private:
   ~GenerateGUIDFunction() {}
 };
 
 /**
+ * SQL function to check if a GUID is valid.  This is just a wrapper around
+ * IsValidGUID in Helpers.h.
+ *
+ * @return true if valid, false otherwise.
+ */
+class IsValidGUIDFunction final : public mozIStorageFunction
+{
+public:
+  NS_DECL_THREADSAFE_ISUPPORTS
+  NS_DECL_MOZISTORAGEFUNCTION
+
+  /**
+   * Registers the function with the specified database connection.
+   *
+   * @param aDBConn
+   *        The database connection to register with.
+   */
+  static nsresult create(mozIStorageConnection *aDBConn);
+private:
+  ~IsValidGUIDFunction() {}
+};
+
+/**
  * SQL function to unreverse the rev_host of a page.
  *
  * @param rev_host
  *        The rev_host value of the page.
  *
  * @return the unreversed host of the page.
  */
 class GetUnreversedHostFunction final : public mozIStorageFunction