зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1293445 - 1 - Add a get_query_param SQL function. r=mak
MozReview-Commit-ID: 12r5GyhoYgi --HG-- extra : rebase_source : deea2bce87803d493fa9e18bb7af0ddcbe89ed7f
This commit is contained in:
Родитель
5ec37b60e1
Коммит
f01db1b2d6
|
@ -1445,6 +1445,8 @@ Database::InitFunctions()
|
|||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
rv = HashFunction::create(mMainConn);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
rv = GetQueryParamFunction::create(mMainConn);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "mozilla/storage.h"
|
||||
#include "mozilla/dom/URLSearchParams.h"
|
||||
#include "nsString.h"
|
||||
#include "nsUnicharUtils.h"
|
||||
#include "nsWhitespaceTokenizer.h"
|
||||
|
@ -246,6 +247,31 @@ namespace {
|
|||
return nsDependentCString(str, len);
|
||||
}
|
||||
|
||||
class MOZ_STACK_CLASS GetQueryParamIterator final :
|
||||
public URLParams::ForEachIterator
|
||||
{
|
||||
public:
|
||||
explicit GetQueryParamIterator(const nsCString& aParamName,
|
||||
nsVariant* aResult)
|
||||
: mParamName(aParamName)
|
||||
, mResult(aResult)
|
||||
{}
|
||||
|
||||
bool URLParamsIterator(const nsAString& aName,
|
||||
const nsAString& aValue) override
|
||||
{
|
||||
NS_ConvertUTF16toUTF8 name(aName);
|
||||
if (!mParamName.Equals(name)) {
|
||||
return true;
|
||||
}
|
||||
mResult->SetAsAString(aValue);
|
||||
return false;
|
||||
}
|
||||
private:
|
||||
const nsCString& mParamName;
|
||||
nsVariant* mResult;
|
||||
};
|
||||
|
||||
} // End anonymous namespace
|
||||
|
||||
namespace mozilla {
|
||||
|
@ -1006,6 +1032,44 @@ namespace places {
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//// Get Query Param Function
|
||||
|
||||
/* static */
|
||||
nsresult
|
||||
GetQueryParamFunction::create(mozIStorageConnection *aDBConn)
|
||||
{
|
||||
RefPtr<GetQueryParamFunction> function = new GetQueryParamFunction();
|
||||
return aDBConn->CreateFunction(
|
||||
NS_LITERAL_CSTRING("get_query_param"), 2, function
|
||||
);
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS(
|
||||
GetQueryParamFunction,
|
||||
mozIStorageFunction
|
||||
)
|
||||
|
||||
NS_IMETHODIMP
|
||||
GetQueryParamFunction::OnFunctionCall(mozIStorageValueArray *aArguments,
|
||||
nsIVariant **_result)
|
||||
{
|
||||
// Must have non-null function arguments.
|
||||
MOZ_ASSERT(aArguments);
|
||||
|
||||
nsDependentCString queryString = getSharedUTF8String(aArguments, 0);
|
||||
nsDependentCString paramName = getSharedUTF8String(aArguments, 1);
|
||||
|
||||
RefPtr<nsVariant> result = new nsVariant();
|
||||
if (!queryString.IsEmpty() && !paramName.IsEmpty()) {
|
||||
GetQueryParamIterator iterator(paramName, result);
|
||||
URLParams::Parse(queryString, iterator);
|
||||
}
|
||||
|
||||
result.forget(_result);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//// Hash Function
|
||||
|
||||
|
|
|
@ -416,6 +416,35 @@ private:
|
|||
~HashFunction() {}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//// Get Query Param Function
|
||||
|
||||
/**
|
||||
* Extracts and returns the value of a parameter from a query string.
|
||||
*
|
||||
* @param string
|
||||
* A string.
|
||||
* @return
|
||||
* The value of the query parameter as a string, or NULL if not set.
|
||||
*/
|
||||
class GetQueryParamFunction 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:
|
||||
~GetQueryParamFunction() {}
|
||||
};
|
||||
|
||||
|
||||
} // namespace places
|
||||
} // namespace mozilla
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
add_task(async function test_get_query_param_sql_function() {
|
||||
let db = await PlacesUtils.promiseDBConnection();
|
||||
await Assert.rejects(db.execute(`SELECT get_query_param()`),
|
||||
/wrong number of arguments/);
|
||||
let rows = await db.execute(`SELECT
|
||||
get_query_param('a=b&c=d', 'a'),
|
||||
get_query_param('a=b&c=d', 'c'),
|
||||
get_query_param('a=b&a=c', 'a'),
|
||||
get_query_param('a=b&c=d', 'e'),
|
||||
get_query_param('a', 'a'),
|
||||
get_query_param(NULL, NULL),
|
||||
get_query_param('a=b&c=d', NULL),
|
||||
get_query_param(NULL, 'a')`);
|
||||
let results = ["b", "d", "b", null, "", null, null, null];
|
||||
equal(rows[0].numEntries, results.length);
|
||||
for (let i = 0; i < results.length; ++i) {
|
||||
equal(rows[0].getResultByIndex(i), results[i]);
|
||||
}
|
||||
});
|
|
@ -112,3 +112,4 @@ skip-if = (os == "win" && os_version == "5.1") # Bug 1158887
|
|||
[test_utils_getURLsForContainerNode.js]
|
||||
[test_utils_setAnnotationsForItem.js]
|
||||
[test_visitsInDB.js]
|
||||
[test_get_query_param_sql_function.js]
|
||||
|
|
Загрузка…
Ссылка в новой задаче