1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozStoragePrivateHelpers_h
8 #define mozStoragePrivateHelpers_h
11 * This file contains convenience methods for mozStorage.
15 #include "nsIVariant.h"
17 #include "nsAutoPtr.h"
18 #include "js/TypeDecls.h"
21 class mozIStorageCompletionCallback
;
27 ////////////////////////////////////////////////////////////////////////////////
30 #define ENSURE_INDEX_VALUE(aIndex, aCount) \
31 NS_ENSURE_TRUE(aIndex < aCount, NS_ERROR_INVALID_ARG)
33 ////////////////////////////////////////////////////////////////////////////////
37 * Converts a SQLite return code to an nsresult return code.
39 * @param aSQLiteResultCode
40 * The SQLite return code to convert.
41 * @returns the corresponding nsresult code for aSQLiteResultCode.
43 nsresult
convertResultCode(int aSQLiteResultCode
);
46 * Checks the performance of a SQLite statement and logs a warning with
47 * NS_WARNING. Currently this only checks the number of sort operations done
48 * on a statement, and if more than zero have been done, the statement can be
49 * made faster with the careful use of an index.
52 * The sqlite3_stmt object to check.
54 void checkAndLogStatementPerformance(sqlite3_stmt
* aStatement
);
57 * Convert the provided JS::Value into a variant representation if possible.
60 * The JSContext the value is from.
62 * The JavaScript value to convert. All primitive types are supported,
63 * but only Date objects are supported from the Date family. Date
64 * objects are coerced to PRTime (nanoseconds since epoch) values.
65 * @return the variant if conversion was successful, nullptr if conversion
66 * failed. The caller is responsible for addref'ing if non-null.
68 nsIVariant
* convertJSValToVariant(JSContext
* aCtx
, const JS::Value
& aValue
);
71 * Convert a provided nsIVariant implementation to our own thread-safe
72 * refcounting implementation, if needed.
75 * The original nsIVariant to be converted.
76 * @return a thread-safe refcounting nsIVariant implementation.
78 Variant_base
* convertVariantToStorageVariant(nsIVariant
* aVariant
);
81 * Obtains an event that will notify a completion callback about completion.
84 * The callback to be notified.
85 * @return an nsIRunnable that can be dispatched to the calling thread.
87 already_AddRefed
<nsIRunnable
> newCompletionEvent(
88 mozIStorageCompletionCallback
* aCallback
);
91 * Utility method to get a Blob as a string value. The string expects
92 * the interface exposed by nsAString/nsACString/etc.
94 template <class T
, class V
>
95 nsresult
DoGetBlobAsString(T
* aThis
, uint32_t aIndex
, V
& aValue
) {
96 typedef typename
V::char_type char_type
;
101 aThis
->GetBlob(aIndex
, &size
, reinterpret_cast<uint8_t**>(&blob
));
102 NS_ENSURE_SUCCESS(rv
, rv
);
104 aValue
.Assign(blob
, size
/ sizeof(char_type
));
110 * Utility method to bind a string value as a Blob. The string expects
111 * the interface exposed by nsAString/nsACString/etc.
113 template <class T
, class V
>
114 nsresult
DoBindStringAsBlobByName(T
* aThis
, const nsACString
& aName
,
116 typedef typename
V::char_type char_type
;
117 return aThis
->BindBlobByName(
118 aName
, reinterpret_cast<const uint8_t*>(aValue
.BeginReading()),
119 aValue
.Length() * sizeof(char_type
));
123 * Utility method to bind a string value as a Blob. The string expects
124 * the interface exposed by nsAString/nsACString/etc.
126 template <class T
, class V
>
127 nsresult
DoBindStringAsBlobByIndex(T
* aThis
, uint32_t aIndex
, const V
& aValue
) {
128 typedef typename
V::char_type char_type
;
129 return aThis
->BindBlobByIndex(
130 aIndex
, reinterpret_cast<const uint8_t*>(aValue
.BeginReading()),
131 aValue
.Length() * sizeof(char_type
));
134 } // namespace storage
135 } // namespace mozilla
137 #endif // mozStoragePrivateHelpers_h