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 "js/TypeDecls.h"
20 class mozIStorageCompletionCallback
;
26 ////////////////////////////////////////////////////////////////////////////////
29 #define ENSURE_INDEX_VALUE(aIndex, aCount) \
30 NS_ENSURE_TRUE(aIndex < aCount, NS_ERROR_INVALID_ARG)
32 ////////////////////////////////////////////////////////////////////////////////
36 * Returns true if the given SQLite result is an error of come kind.
38 * @param aSQLiteResultCode
39 * The SQLite return code to check.
40 * @returns true if the result represents an error.
42 bool isErrorCode(int aSQLiteResultCode
);
45 * Converts a SQLite return code to an nsresult return code.
47 * @param aSQLiteResultCode
48 * The SQLite return code to convert.
49 * @returns the corresponding nsresult code for aSQLiteResultCode.
51 nsresult
convertResultCode(int aSQLiteResultCode
);
54 * Checks the performance of a SQLite statement and logs a warning with
55 * NS_WARNING. Currently this only checks the number of sort operations done
56 * on a statement, and if more than zero have been done, the statement can be
57 * made faster with the careful use of an index.
60 * The sqlite3_stmt object to check.
62 void checkAndLogStatementPerformance(sqlite3_stmt
* aStatement
);
65 * Convert the provided JS::Value into a variant representation if possible.
68 * The JSContext the value is from.
70 * The JavaScript value to convert. All primitive types are supported,
71 * but only Date objects are supported from the Date family. Date
72 * objects are coerced to PRTime (nanoseconds since epoch) values.
73 * @return the variant if conversion was successful, nullptr if conversion
74 * failed. The caller is responsible for addref'ing if non-null.
76 nsIVariant
* convertJSValToVariant(JSContext
* aCtx
, const JS::Value
& aValue
);
79 * Convert a provided nsIVariant implementation to our own thread-safe
80 * refcounting implementation, if needed.
83 * The original nsIVariant to be converted.
84 * @return a thread-safe refcounting nsIVariant implementation.
86 Variant_base
* convertVariantToStorageVariant(nsIVariant
* aVariant
);
89 * Obtains an event that will notify a completion callback about completion.
92 * The callback to be notified.
93 * @return an nsIRunnable that can be dispatched to the calling thread.
95 already_AddRefed
<nsIRunnable
> newCompletionEvent(
96 mozIStorageCompletionCallback
* aCallback
);
99 * Utility method to get a Blob as a string value. The string expects
100 * the interface exposed by nsAString/nsACString/etc.
102 template <class T
, class V
>
103 nsresult
DoGetBlobAsString(T
* aThis
, uint32_t aIndex
, V
& aValue
) {
104 typedef typename
V::char_type char_type
;
109 aThis
->GetBlob(aIndex
, &size
, reinterpret_cast<uint8_t**>(&blob
));
110 NS_ENSURE_SUCCESS(rv
, rv
);
112 aValue
.Assign(blob
, size
/ sizeof(char_type
));
118 * Utility method to bind a string value as a Blob. The string expects
119 * the interface exposed by nsAString/nsACString/etc.
121 template <class T
, class V
>
122 nsresult
DoBindStringAsBlobByName(T
* aThis
, const nsACString
& aName
,
124 typedef typename
V::char_type char_type
;
125 return aThis
->BindBlobByName(
126 aName
, reinterpret_cast<const uint8_t*>(aValue
.BeginReading()),
127 aValue
.Length() * sizeof(char_type
));
131 * Utility method to bind a string value as a Blob. The string expects
132 * the interface exposed by nsAString/nsACString/etc.
134 template <class T
, class V
>
135 nsresult
DoBindStringAsBlobByIndex(T
* aThis
, uint32_t aIndex
, const V
& aValue
) {
136 typedef typename
V::char_type char_type
;
137 return aThis
->BindBlobByIndex(
138 aIndex
, reinterpret_cast<const uint8_t*>(aValue
.BeginReading()),
139 aValue
.Length() * sizeof(char_type
));
142 } // namespace storage
143 } // namespace mozilla
145 #endif // mozStoragePrivateHelpers_h