2009-05-09 00:29:56 +00: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 11:12:37 +00: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-12 22:01:05 +00:00
|
|
|
|
2010-06-01 12:38:00 +00:00
|
|
|
#ifndef mozStoragePrivateHelpers_h
|
|
|
|
#define mozStoragePrivateHelpers_h
|
2008-10-30 22:50:00 +00:00
|
|
|
|
2007-06-12 22:01:05 +00:00
|
|
|
/**
|
|
|
|
* This file contains convenience methods for mozStorage.
|
|
|
|
*/
|
|
|
|
|
2009-05-09 00:29:57 +00:00
|
|
|
#include "sqlite3.h"
|
2022-09-21 15:02:14 +00:00
|
|
|
#include "nsISerialEventTarget.h"
|
2009-05-09 00:29:57 +00:00
|
|
|
#include "nsIVariant.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2013-08-28 02:59:14 +00:00
|
|
|
#include "js/TypeDecls.h"
|
2015-03-12 16:55:56 +00:00
|
|
|
#include "Variant.h"
|
2009-07-22 22:31:03 +00:00
|
|
|
|
2009-11-09 17:58:34 +00:00
|
|
|
class mozIStorageCompletionCallback;
|
|
|
|
class nsIRunnable;
|
2009-05-09 00:29:56 +00:00
|
|
|
|
2009-05-09 00:29:56 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
2009-05-09 00:29:56 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Macros
|
|
|
|
|
|
|
|
#define ENSURE_INDEX_VALUE(aIndex, aCount) \
|
|
|
|
NS_ENSURE_TRUE(aIndex < aCount, NS_ERROR_INVALID_ARG)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Functions
|
|
|
|
|
2020-08-11 20:55:33 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the given SQLite result is an error of come kind.
|
|
|
|
*
|
|
|
|
* @param aSQLiteResultCode
|
|
|
|
* The SQLite return code to check.
|
|
|
|
* @returns true if the result represents an error.
|
|
|
|
*/
|
|
|
|
bool isErrorCode(int aSQLiteResultCode);
|
|
|
|
|
2007-06-12 22:01:05 +00:00
|
|
|
/**
|
|
|
|
* Converts a SQLite return code to an nsresult return code.
|
|
|
|
*
|
2009-03-12 18:30:15 +00:00
|
|
|
* @param aSQLiteResultCode
|
|
|
|
* The SQLite return code to convert.
|
|
|
|
* @returns the corresponding nsresult code for aSQLiteResultCode.
|
2007-06-12 22:01:05 +00:00
|
|
|
*/
|
2009-05-09 00:29:56 +00:00
|
|
|
nsresult convertResultCode(int aSQLiteResultCode);
|
2007-06-12 22:01:05 +00:00
|
|
|
|
2009-03-12 18:30:15 +00: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 00:29:56 +00:00
|
|
|
void checkAndLogStatementPerformance(sqlite3_stmt* aStatement);
|
2007-06-12 22:01:05 +00:00
|
|
|
|
2009-07-22 22:31:03 +00:00
|
|
|
/**
|
2013-08-17 22:50:18 +00:00
|
|
|
* Convert the provided JS::Value into a variant representation if possible.
|
2009-07-22 22:31:03 +00:00
|
|
|
*
|
2009-11-09 17:58:34 +00:00
|
|
|
* @param aCtx
|
2010-03-24 07:32:40 +00:00
|
|
|
* The JSContext the value is from.
|
2009-11-09 17:58:34 +00:00
|
|
|
* @param aValue
|
2010-03-24 07:32:40 +00: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 14:20:58 +00:00
|
|
|
* @return the variant if conversion was successful, nullptr if conversion
|
2010-03-24 07:32:40 +00:00
|
|
|
* failed. The caller is responsible for addref'ing if non-null.
|
2009-11-09 17:58:34 +00:00
|
|
|
*/
|
2016-09-11 09:21:36 +00:00
|
|
|
nsIVariant* convertJSValToVariant(JSContext* aCtx, const JS::Value& aValue);
|
2009-11-09 17:58:34 +00:00
|
|
|
|
2015-03-12 16:55:56 +00: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 17:58:34 +00: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-22 22:31:03 +00:00
|
|
|
*/
|
2010-03-24 07:32:40 +00:00
|
|
|
already_AddRefed<nsIRunnable> newCompletionEvent(
|
|
|
|
mozIStorageCompletionCallback* aCallback);
|
2009-07-22 22:31:03 +00:00
|
|
|
|
2015-04-29 16:03:15 +00: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-16 22:32:00 +00:00
|
|
|
aValue.Assign(blob, size / sizeof(char_type));
|
2016-02-17 03:57:59 +00:00
|
|
|
delete[] blob;
|
2015-04-29 16:03:15 +00: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));
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:02:14 +00:00
|
|
|
/**
|
|
|
|
* Utility function to check if a serial event target may run runnables
|
|
|
|
* on the current thread.
|
|
|
|
*/
|
|
|
|
inline bool IsOnCurrentSerialEventTarget(nsISerialEventTarget* aTarget) {
|
|
|
|
return aTarget->IsOnCurrentThread();
|
|
|
|
}
|
|
|
|
|
2009-05-09 00:29:56 +00:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|
2007-06-12 22:01:05 +00:00
|
|
|
|
2010-06-01 12:38:00 +00:00
|
|
|
#endif // mozStoragePrivateHelpers_h
|