Backed out changeset 2284c3e8c336 (bug 1215092)
[gecko.git] / storage / mozStoragePrivateHelpers.h
blobe29188ce58e374f67575e6415bf312aefe686b10
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 "nsAutoPtr.h"
18 #include "js/TypeDecls.h"
19 #include "Variant.h"
21 class mozIStorageCompletionCallback;
22 class nsIRunnable;
24 namespace mozilla {
25 namespace storage {
27 ////////////////////////////////////////////////////////////////////////////////
28 //// Macros
30 #define ENSURE_INDEX_VALUE(aIndex, aCount) \
31 NS_ENSURE_TRUE(aIndex < aCount, NS_ERROR_INVALID_ARG)
33 ////////////////////////////////////////////////////////////////////////////////
34 //// Functions
36 /**
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);
45 /**
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.
51 * @param aStatement
52 * The sqlite3_stmt object to check.
54 void checkAndLogStatementPerformance(sqlite3_stmt *aStatement);
56 /**
57 * Convert the provided JS::Value into a variant representation if possible.
59 * @param aCtx
60 * The JSContext the value is from.
61 * @param aValue
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, JS::Value aValue);
70 /**
71 * Convert a provided nsIVariant implementation to our own thread-safe
72 * refcounting implementation, if needed.
74 * @param aValue
75 * The original nsIVariant to be converted.
76 * @return a thread-safe refcounting nsIVariant implementation.
78 Variant_base *convertVariantToStorageVariant(nsIVariant *aVariant);
80 /**
81 * Obtains an event that will notify a completion callback about completion.
83 * @param aCallback
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 /**
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>
96 nsresult
97 DoGetBlobAsString(T* aThis, uint32_t aIndex, V& aValue)
99 typedef typename V::char_type char_type;
101 uint32_t size;
102 char_type* blob;
103 nsresult rv =
104 aThis->GetBlob(aIndex, &size, reinterpret_cast<uint8_t**>(&blob));
105 NS_ENSURE_SUCCESS(rv, rv);
107 aValue.Adopt(blob, size / sizeof(char_type));
108 return NS_OK;
112 * Utility method to bind a string value as a Blob. The string expects
113 * the interface exposed by nsAString/nsACString/etc.
115 template<class T, class V>
116 nsresult
117 DoBindStringAsBlobByName(T* aThis, const nsACString& aName, const V& aValue)
119 typedef typename V::char_type char_type;
120 return aThis->BindBlobByName(aName,
121 reinterpret_cast<const uint8_t*>(aValue.BeginReading()),
122 aValue.Length() * sizeof(char_type));
126 * Utility method to bind a string value as a Blob. The string expects
127 * the interface exposed by nsAString/nsACString/etc.
129 template<class T, class V>
130 nsresult
131 DoBindStringAsBlobByIndex(T* aThis, uint32_t aIndex, const V& aValue)
133 typedef typename V::char_type char_type;
134 return aThis->BindBlobByIndex(aIndex,
135 reinterpret_cast<const uint8_t*>(aValue.BeginReading()),
136 aValue.Length() * sizeof(char_type));
139 } // namespace storage
140 } // namespace mozilla
142 #endif // mozStoragePrivateHelpers_h