From b8e3915bf5feee6d5016d958c31c102c0094c3df Mon Sep 17 00:00:00 2001 From: Kit Cambridge Date: Thu, 5 Oct 2017 11:32:49 -0700 Subject: [PATCH] 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 --HG-- extra : rebase_source : 9bbc853ae3892314cc3bab7469c0f9c25706aea5 --- toolkit/components/places/Database.cpp | 2 ++ toolkit/components/places/SQLFunctions.cpp | 34 ++++++++++++++++++++++ toolkit/components/places/SQLFunctions.h | 23 +++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/toolkit/components/places/Database.cpp b/toolkit/components/places/Database.cpp index dd60277fc8d6..b5dac3a0e46f 100644 --- a/toolkit/components/places/Database.cpp +++ b/toolkit/components/places/Database.cpp @@ -1330,6 +1330,8 @@ Database::InitFunctions() 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); diff --git a/toolkit/components/places/SQLFunctions.cpp b/toolkit/components/places/SQLFunctions.cpp index 893f17915f86..bdc9e5fdb5ab 100644 --- a/toolkit/components/places/SQLFunctions.cpp +++ b/toolkit/components/places/SQLFunctions.cpp @@ -771,6 +771,40 @@ namespace places { return NS_OK; } +//////////////////////////////////////////////////////////////////////////////// +//// GUID Validation Function + + /* static */ + nsresult + IsValidGUIDFunction::create(mozIStorageConnection *aDBConn) + { + RefPtr 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 result = new nsVariant(); + result->SetAsBool(IsValidGUID(guid)); + result.forget(_result); + return NS_OK; + } + //////////////////////////////////////////////////////////////////////////////// //// Get Unreversed Host Function diff --git a/toolkit/components/places/SQLFunctions.h b/toolkit/components/places/SQLFunctions.h index 5886606ce8e9..12277dd61014 100644 --- a/toolkit/components/places/SQLFunctions.h +++ b/toolkit/components/places/SQLFunctions.h @@ -241,6 +241,29 @@ 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. *