2009-05-09 04:29:56 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-06-13 02:01:05 +04:00
|
|
|
|
2010-06-01 16:38:00 +04:00
|
|
|
#ifndef mozStoragePrivateHelpers_h
|
|
|
|
#define mozStoragePrivateHelpers_h
|
2008-10-31 01:50:00 +03:00
|
|
|
|
2007-06-13 02:01:05 +04:00
|
|
|
/**
|
|
|
|
* This file contains convenience methods for mozStorage.
|
|
|
|
*/
|
|
|
|
|
2009-05-09 04:29:57 +04:00
|
|
|
#include "sqlite3.h"
|
|
|
|
#include "nsIVariant.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2013-08-28 06:59:14 +04:00
|
|
|
#include "js/TypeDecls.h"
|
2015-03-12 19:55:56 +03:00
|
|
|
#include "Variant.h"
|
2009-07-23 02:31:03 +04:00
|
|
|
|
2009-11-09 20:58:34 +03:00
|
|
|
class mozIStorageCompletionCallback;
|
|
|
|
class nsIRunnable;
|
2009-05-09 04:29:56 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Macros
|
|
|
|
|
|
|
|
#define ENSURE_INDEX_VALUE(aIndex, aCount) \
|
|
|
|
NS_ENSURE_TRUE(aIndex < aCount, NS_ERROR_INVALID_ARG)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Functions
|
|
|
|
|
2007-06-13 02:01:05 +04:00
|
|
|
/**
|
|
|
|
* Converts a SQLite return code to an nsresult return code.
|
|
|
|
*
|
2009-03-12 21:30:15 +03:00
|
|
|
* @param aSQLiteResultCode
|
|
|
|
* The SQLite return code to convert.
|
|
|
|
* @returns the corresponding nsresult code for aSQLiteResultCode.
|
2007-06-13 02:01:05 +04:00
|
|
|
*/
|
2009-05-09 04:29:56 +04:00
|
|
|
nsresult convertResultCode(int aSQLiteResultCode);
|
2007-06-13 02:01:05 +04:00
|
|
|
|
2009-03-12 21:30:15 +03:00
|
|
|
/**
|
|
|
|
* Checks the performance of a SQLite statement and logs a warning with
|
|
|
|
* NS_WARNING. Currently this only checks the number of sort operations done
|
|
|
|
* on a statement, and if more than zero have been done, the statement can be
|
|
|
|
* made faster with the careful use of an index.
|
|
|
|
*
|
|
|
|
* @param aStatement
|
|
|
|
* The sqlite3_stmt object to check.
|
|
|
|
*/
|
2009-05-09 04:29:56 +04:00
|
|
|
void checkAndLogStatementPerformance(sqlite3_stmt* aStatement);
|
2007-06-13 02:01:05 +04:00
|
|
|
|
2009-07-23 02:31:03 +04:00
|
|
|
/**
|
2013-08-18 02:50:18 +04:00
|
|
|
* Convert the provided JS::Value into a variant representation if possible.
|
2009-07-23 02:31:03 +04:00
|
|
|
*
|
2009-11-09 20:58:34 +03:00
|
|
|
* @param aCtx
|
2010-03-24 10:32:40 +03:00
|
|
|
* The JSContext the value is from.
|
2009-11-09 20:58:34 +03:00
|
|
|
* @param aValue
|
2010-03-24 10:32:40 +03:00
|
|
|
* The JavaScript value to convert. All primitive types are supported,
|
|
|
|
* but only Date objects are supported from the Date family. Date
|
|
|
|
* objects are coerced to PRTime (nanoseconds since epoch) values.
|
2012-07-30 18:20:58 +04:00
|
|
|
* @return the variant if conversion was successful, nullptr if conversion
|
2010-03-24 10:32:40 +03:00
|
|
|
* failed. The caller is responsible for addref'ing if non-null.
|
2009-11-09 20:58:34 +03:00
|
|
|
*/
|
2016-09-11 12:21:36 +03:00
|
|
|
nsIVariant* convertJSValToVariant(JSContext* aCtx, const JS::Value& aValue);
|
2009-11-09 20:58:34 +03:00
|
|
|
|
2015-03-12 19:55:56 +03:00
|
|
|
/**
|
|
|
|
* Convert a provided nsIVariant implementation to our own thread-safe
|
|
|
|
* refcounting implementation, if needed.
|
|
|
|
*
|
|
|
|
* @param aValue
|
|
|
|
* The original nsIVariant to be converted.
|
|
|
|
* @return a thread-safe refcounting nsIVariant implementation.
|
|
|
|
*/
|
|
|
|
Variant_base* convertVariantToStorageVariant(nsIVariant* aVariant);
|
|
|
|
|
2009-11-09 20:58:34 +03:00
|
|
|
/**
|
|
|
|
* Obtains an event that will notify a completion callback about completion.
|
|
|
|
*
|
|
|
|
* @param aCallback
|
|
|
|
* The callback to be notified.
|
|
|
|
* @return an nsIRunnable that can be dispatched to the calling thread.
|
2009-07-23 02:31:03 +04:00
|
|
|
*/
|
2010-03-24 10:32:40 +03:00
|
|
|
already_AddRefed<nsIRunnable> newCompletionEvent(
|
|
|
|
mozIStorageCompletionCallback* aCallback);
|
2009-07-23 02:31:03 +04:00
|
|
|
|
2015-04-29 19:03:15 +03:00
|
|
|
/**
|
|
|
|
* Utility method to get a Blob as a string value. The string expects
|
|
|
|
* the interface exposed by nsAString/nsACString/etc.
|
|
|
|
*/
|
|
|
|
template <class T, class V>
|
|
|
|
nsresult DoGetBlobAsString(T* aThis, uint32_t aIndex, V& aValue) {
|
|
|
|
typedef typename V::char_type char_type;
|
|
|
|
|
|
|
|
uint32_t size;
|
|
|
|
char_type* blob;
|
|
|
|
nsresult rv =
|
|
|
|
aThis->GetBlob(aIndex, &size, reinterpret_cast<uint8_t**>(&blob));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-02-17 01:32:00 +03:00
|
|
|
aValue.Assign(blob, size / sizeof(char_type));
|
2016-02-17 06:57:59 +03:00
|
|
|
delete[] blob;
|
2015-04-29 19:03:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility method to bind a string value as a Blob. The string expects
|
|
|
|
* the interface exposed by nsAString/nsACString/etc.
|
|
|
|
*/
|
|
|
|
template <class T, class V>
|
|
|
|
nsresult DoBindStringAsBlobByName(T* aThis, const nsACString& aName,
|
|
|
|
const V& aValue) {
|
|
|
|
typedef typename V::char_type char_type;
|
|
|
|
return aThis->BindBlobByName(
|
|
|
|
aName, reinterpret_cast<const uint8_t*>(aValue.BeginReading()),
|
|
|
|
aValue.Length() * sizeof(char_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility method to bind a string value as a Blob. The string expects
|
|
|
|
* the interface exposed by nsAString/nsACString/etc.
|
|
|
|
*/
|
|
|
|
template <class T, class V>
|
|
|
|
nsresult DoBindStringAsBlobByIndex(T* aThis, uint32_t aIndex, const V& aValue) {
|
|
|
|
typedef typename V::char_type char_type;
|
|
|
|
return aThis->BindBlobByIndex(
|
|
|
|
aIndex, reinterpret_cast<const uint8_t*>(aValue.BeginReading()),
|
|
|
|
aValue.Length() * sizeof(char_type));
|
|
|
|
}
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|
2007-06-13 02:01:05 +04:00
|
|
|
|
2010-06-01 16:38:00 +04:00
|
|
|
#endif // mozStoragePrivateHelpers_h
|