Bug 1380606 - Add an `IS_VALID_GUID()` SQL function to Places. r=mak draft
authorKit Cambridge <kit@yakshaving.ninja>
Thu, 05 Oct 2017 11:32:49 -0700
changeset 678314 f75895c475a993a8ea4d46e4d4989aad37a01d73
parent 678275 e897e367d3bd489422d86fbdfac54925c18329d2
child 678315 a2c96d56f6628f9734e8a0e0d25e7f9f02b6f118
child 678612 86f48ca6d0628dbe602b4d8cc7883627a757913f
push id83880
push userbmo:kit@mozilla.com
push dateWed, 11 Oct 2017 03:52:48 +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