Line data Source code
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/. */
6 :
7 : #ifndef mozStoragePrivateHelpers_h
8 : #define mozStoragePrivateHelpers_h
9 :
10 : /**
11 : * This file contains convenience methods for mozStorage.
12 : */
13 :
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"
20 :
21 : class mozIStorageCompletionCallback;
22 : class nsIRunnable;
23 :
24 : namespace mozilla {
25 : namespace storage {
26 :
27 : ////////////////////////////////////////////////////////////////////////////////
28 : //// Macros
29 :
30 : #define ENSURE_INDEX_VALUE(aIndex, aCount) \
31 : NS_ENSURE_TRUE(aIndex < aCount, NS_ERROR_INVALID_ARG)
32 :
33 : ////////////////////////////////////////////////////////////////////////////////
34 : //// Functions
35 :
36 : /**
37 : * Converts a SQLite return code to an nsresult return code.
38 : *
39 : * @param aSQLiteResultCode
40 : * The SQLite return code to convert.
41 : * @returns the corresponding nsresult code for aSQLiteResultCode.
42 : */
43 : nsresult convertResultCode(int aSQLiteResultCode);
44 :
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.
50 : *
51 : * @param aStatement
52 : * The sqlite3_stmt object to check.
53 : */
54 : void checkAndLogStatementPerformance(sqlite3_stmt *aStatement);
55 :
56 : /**
57 : * Convert the provided JS::Value into a variant representation if possible.
58 : *
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.
67 : */
68 : nsIVariant *convertJSValToVariant(JSContext *aCtx, const JS::Value& aValue);
69 :
70 : /**
71 : * Convert a provided nsIVariant implementation to our own thread-safe
72 : * refcounting implementation, if needed.
73 : *
74 : * @param aValue
75 : * The original nsIVariant to be converted.
76 : * @return a thread-safe refcounting nsIVariant implementation.
77 : */
78 : Variant_base *convertVariantToStorageVariant(nsIVariant *aVariant);
79 :
80 : /**
81 : * Obtains an event that will notify a completion callback about completion.
82 : *
83 : * @param aCallback
84 : * The callback to be notified.
85 : * @return an nsIRunnable that can be dispatched to the calling thread.
86 : */
87 : already_AddRefed<nsIRunnable> newCompletionEvent(
88 : mozIStorageCompletionCallback *aCallback
89 : );
90 :
91 : /**
92 : * Utility method to get a Blob as a string value. The string expects
93 : * the interface exposed by nsAString/nsACString/etc.
94 : */
95 : template<class T, class V>
96 : nsresult
97 0 : DoGetBlobAsString(T* aThis, uint32_t aIndex, V& aValue)
98 : {
99 : typedef typename V::char_type char_type;
100 :
101 : uint32_t size;
102 : char_type* blob;
103 : nsresult rv =
104 0 : aThis->GetBlob(aIndex, &size, reinterpret_cast<uint8_t**>(&blob));
105 0 : NS_ENSURE_SUCCESS(rv, rv);
106 :
107 0 : aValue.Assign(blob, size / sizeof(char_type));
108 0 : delete[] blob;
109 0 : return NS_OK;
110 : }
111 :
112 : /**
113 : * Utility method to bind a string value as a Blob. The string expects
114 : * the interface exposed by nsAString/nsACString/etc.
115 : */
116 : template<class T, class V>
117 : nsresult
118 0 : DoBindStringAsBlobByName(T* aThis, const nsACString& aName, const V& aValue)
119 : {
120 : typedef typename V::char_type char_type;
121 : return aThis->BindBlobByName(aName,
122 0 : reinterpret_cast<const uint8_t*>(aValue.BeginReading()),
123 0 : aValue.Length() * sizeof(char_type));
124 : }
125 :
126 : /**
127 : * Utility method to bind a string value as a Blob. The string expects
128 : * the interface exposed by nsAString/nsACString/etc.
129 : */
130 : template<class T, class V>
131 : nsresult
132 0 : DoBindStringAsBlobByIndex(T* aThis, uint32_t aIndex, const V& aValue)
133 : {
134 : typedef typename V::char_type char_type;
135 : return aThis->BindBlobByIndex(aIndex,
136 0 : reinterpret_cast<const uint8_t*>(aValue.BeginReading()),
137 0 : aValue.Length() * sizeof(char_type));
138 : }
139 :
140 : } // namespace storage
141 : } // namespace mozilla
142 :
143 : #endif // mozStoragePrivateHelpers_h
|