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
92 * Utility method to get a Blob as a string value. The string expects
93 * the interface exposed by nsAString/nsACString/etc.
95 template<class T
, class V
>
97 DoGetBlobAsString(T
* aThis
, uint32_t aIndex
, V
& aValue
)
99 typedef typename
V::char_type char_type
;
104 aThis
->GetBlob(aIndex
, &size
, reinterpret_cast<uint8_t**>(&blob
));
105 NS_ENSURE_SUCCESS(rv
, rv
);
107 aValue
.Assign(blob
, size
/ sizeof(char_type
));
113 * Utility method to bind a string value as a Blob. The string expects
114 * the interface exposed by nsAString/nsACString/etc.
116 template<class T
, class V
>
118 DoBindStringAsBlobByName(T
* aThis
, const nsACString
& aName
, const V
& aValue
)
120 typedef typename
V::char_type char_type
;
121 return aThis
->BindBlobByName(aName
,
122 reinterpret_cast<const uint8_t*>(aValue
.BeginReading()),
123 aValue
.Length() * sizeof(char_type
));
127 * Utility method to bind a string value as a Blob. The string expects
128 * the interface exposed by nsAString/nsACString/etc.
130 template<class T
, class V
>
132 DoBindStringAsBlobByIndex(T
* aThis
, uint32_t aIndex
, const V
& aValue
)
134 typedef typename
V::char_type char_type
;
135 return aThis
->BindBlobByIndex(aIndex
,
136 reinterpret_cast<const uint8_t*>(aValue
.BeginReading()),
137 aValue
.Length() * sizeof(char_type
));
140 } // namespace storage
141 } // namespace mozilla
143 #endif // mozStoragePrivateHelpers_h