Bug 1733615 [wpt PR 31058] - Update wpt metadata, a=testonly
[gecko.git] / storage / mozStoragePrivateHelpers.h
blob457ee1c1a3cfeaeac1e31e4f2300b1f3990d7131
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
10 /**
11 * This file contains convenience methods for mozStorage.
14 #include "sqlite3.h"
15 #include "nsIVariant.h"
16 #include "nsError.h"
17 #include "js/TypeDecls.h"
18 #include "Variant.h"
20 class mozIStorageCompletionCallback;
21 class nsIRunnable;
23 namespace mozilla {
24 namespace storage {
26 ////////////////////////////////////////////////////////////////////////////////
27 //// Macros
29 #define ENSURE_INDEX_VALUE(aIndex, aCount) \
30 NS_ENSURE_TRUE(aIndex < aCount, NS_ERROR_INVALID_ARG)
32 ////////////////////////////////////////////////////////////////////////////////
33 //// Functions
35 /**
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);
44 /**
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);
53 /**
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.
59 * @param aStatement
60 * The sqlite3_stmt object to check.
62 void checkAndLogStatementPerformance(sqlite3_stmt* aStatement);
64 /**
65 * Convert the provided JS::Value into a variant representation if possible.
67 * @param aCtx
68 * The JSContext the value is from.
69 * @param aValue
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);
78 /**
79 * Convert a provided nsIVariant implementation to our own thread-safe
80 * refcounting implementation, if needed.
82 * @param aValue
83 * The original nsIVariant to be converted.
84 * @return a thread-safe refcounting nsIVariant implementation.
86 Variant_base* convertVariantToStorageVariant(nsIVariant* aVariant);
88 /**
89 * Obtains an event that will notify a completion callback about completion.
91 * @param aCallback
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);
98 /**
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;
106 uint32_t size;
107 char_type* blob;
108 nsresult rv =
109 aThis->GetBlob(aIndex, &size, reinterpret_cast<uint8_t**>(&blob));
110 NS_ENSURE_SUCCESS(rv, rv);
112 aValue.Assign(blob, size / sizeof(char_type));
113 delete[] blob;
114 return NS_OK;
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,
123 const V& aValue) {
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