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/. */
9 #include "jsfriendapi.h"
11 #include "nsPrintfCString.h"
14 #include "mozilla/Mutex.h"
15 #include "mozilla/CondVar.h"
16 #include "nsQueryObject.h"
17 #include "nsThreadUtils.h"
18 #include "nsJSUtils.h"
21 #include "mozStoragePrivateHelpers.h"
22 #include "mozIStorageCompletionCallback.h"
24 #include "mozilla/Logging.h"
25 extern mozilla::LazyLogModule gStorageLog
;
30 bool isErrorCode(int aSQLiteResultCode
) {
31 // Drop off the extended result bits of the result code.
32 int rc
= aSQLiteResultCode
& 0xFF;
34 return rc
!= SQLITE_OK
&& rc
!= SQLITE_ROW
&& rc
!= SQLITE_DONE
;
37 nsresult
convertResultCode(int aSQLiteResultCode
) {
38 // Drop off the extended result bits of the result code.
39 int rc
= aSQLiteResultCode
& 0xFF;
48 return NS_ERROR_FILE_CORRUPTED
;
51 return NS_ERROR_FILE_ACCESS_DENIED
;
53 return NS_ERROR_STORAGE_BUSY
;
55 return NS_ERROR_FILE_IS_LOCKED
;
57 return NS_ERROR_FILE_READ_ONLY
;
59 return NS_ERROR_STORAGE_IOERR
;
62 return NS_ERROR_FILE_NO_DEVICE_SPACE
;
64 return NS_ERROR_OUT_OF_MEMORY
;
66 return NS_ERROR_UNEXPECTED
;
68 case SQLITE_INTERRUPT
:
69 return NS_ERROR_ABORT
;
70 case SQLITE_CONSTRAINT
:
71 return NS_ERROR_STORAGE_CONSTRAINT
;
76 nsAutoCString message
;
77 message
.AppendLiteral("SQLite returned error code ");
78 message
.AppendInt(rc
);
79 message
.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
80 NS_WARNING_ASSERTION(rc
== SQLITE_ERROR
, message
.get());
82 return NS_ERROR_FAILURE
;
85 void checkAndLogStatementPerformance(sqlite3_stmt
* aStatement
) {
86 // Check to see if the query performed sorting operations or not. If it
87 // did, it may need to be optimized!
88 int count
= ::sqlite3_stmt_status(aStatement
, SQLITE_STMTSTATUS_SORT
, 1);
89 if (count
<= 0) return;
91 const char* sql
= ::sqlite3_sql(aStatement
);
93 // Check to see if this is marked to not warn
94 if (::strstr(sql
, "/* do not warn (bug ")) return;
96 // CREATE INDEX always sorts (sorting is a necessary step in creating
97 // an index). So ignore the warning there.
98 if (::strstr(sql
, "CREATE INDEX") || ::strstr(sql
, "CREATE UNIQUE INDEX"))
101 nsAutoCString
message("Suboptimal indexes for the SQL statement ");
102 #ifdef MOZ_STORAGE_SORTWARNING_SQL_DUMP
105 message
.AppendLiteral("` [");
106 message
.AppendInt(count
);
107 message
.AppendLiteral(" sort operation(s)]");
109 nsPrintfCString
address("0x%p", aStatement
);
110 message
.Append(address
);
112 message
.AppendLiteral(" (http://mzl.la/1FuID0j).");
113 NS_WARNING(message
.get());
116 nsIVariant
* convertJSValToVariant(JSContext
* aCtx
, const JS::Value
& aValue
) {
117 if (aValue
.isInt32()) return new IntegerVariant(aValue
.toInt32());
119 if (aValue
.isDouble()) return new FloatVariant(aValue
.toDouble());
121 if (aValue
.isString()) {
122 nsAutoJSString value
;
123 if (!value
.init(aCtx
, aValue
.toString())) return nullptr;
124 return new TextVariant(value
);
127 if (aValue
.isBoolean()) return new IntegerVariant(aValue
.isTrue() ? 1 : 0);
129 if (aValue
.isNull()) return new NullVariant();
131 if (aValue
.isObject()) {
132 JS::Rooted
<JSObject
*> obj(aCtx
, &aValue
.toObject());
133 // We only support Date instances, all others fail.
135 if (!js::DateIsValid(aCtx
, obj
, &valid
) || !valid
) return nullptr;
138 if (!js::DateGetMsecSinceEpoch(aCtx
, obj
, &msecd
)) return nullptr;
141 int64_t msec
= msecd
;
143 return new IntegerVariant(msec
);
149 Variant_base
* convertVariantToStorageVariant(nsIVariant
* aVariant
) {
150 RefPtr
<Variant_base
> variant
= do_QueryObject(aVariant
);
152 // JS helpers already convert the JS representation to a Storage Variant,
153 // in such a case there's nothing left to do here, so just pass-through.
157 if (!aVariant
) return new NullVariant();
159 uint16_t dataType
= aVariant
->GetDataType();
162 case nsIDataType::VTYPE_BOOL
:
163 case nsIDataType::VTYPE_INT8
:
164 case nsIDataType::VTYPE_INT16
:
165 case nsIDataType::VTYPE_INT32
:
166 case nsIDataType::VTYPE_UINT8
:
167 case nsIDataType::VTYPE_UINT16
:
168 case nsIDataType::VTYPE_UINT32
:
169 case nsIDataType::VTYPE_INT64
:
170 case nsIDataType::VTYPE_UINT64
: {
172 nsresult rv
= aVariant
->GetAsInt64(&v
);
173 NS_ENSURE_SUCCESS(rv
, nullptr);
174 return new IntegerVariant(v
);
176 case nsIDataType::VTYPE_FLOAT
:
177 case nsIDataType::VTYPE_DOUBLE
: {
179 nsresult rv
= aVariant
->GetAsDouble(&v
);
180 NS_ENSURE_SUCCESS(rv
, nullptr);
181 return new FloatVariant(v
);
183 case nsIDataType::VTYPE_CHAR
:
184 case nsIDataType::VTYPE_CHAR_STR
:
185 case nsIDataType::VTYPE_STRING_SIZE_IS
:
186 case nsIDataType::VTYPE_UTF8STRING
:
187 case nsIDataType::VTYPE_CSTRING
: {
189 nsresult rv
= aVariant
->GetAsAUTF8String(v
);
190 NS_ENSURE_SUCCESS(rv
, nullptr);
191 return new UTF8TextVariant(v
);
193 case nsIDataType::VTYPE_WCHAR
:
194 case nsIDataType::VTYPE_WCHAR_STR
:
195 case nsIDataType::VTYPE_WSTRING_SIZE_IS
:
196 case nsIDataType::VTYPE_ASTRING
: {
198 nsresult rv
= aVariant
->GetAsAString(v
);
199 NS_ENSURE_SUCCESS(rv
, nullptr);
200 return new TextVariant(v
);
202 case nsIDataType::VTYPE_ARRAY
: {
207 // Note this copies the array data.
208 nsresult rv
= aVariant
->GetAsArray(&type
, &iid
, &len
, &rawArray
);
209 NS_ENSURE_SUCCESS(rv
, nullptr);
210 if (type
== nsIDataType::VTYPE_UINT8
) {
211 std::pair
<uint8_t*, int> v(static_cast<uint8_t*>(rawArray
), len
);
212 // Take ownership of the data avoiding a further copy.
213 return new AdoptedBlobVariant(v
);
217 case nsIDataType::VTYPE_EMPTY
:
218 case nsIDataType::VTYPE_EMPTY_ARRAY
:
219 case nsIDataType::VTYPE_VOID
:
220 return new NullVariant();
221 case nsIDataType::VTYPE_ID
:
222 case nsIDataType::VTYPE_INTERFACE
:
223 case nsIDataType::VTYPE_INTERFACE_IS
:
225 NS_WARNING("Unsupported variant type");
231 class CallbackEvent
: public Runnable
{
233 explicit CallbackEvent(mozIStorageCompletionCallback
* aCallback
)
234 : Runnable("storage::CallbackEvent"), mCallback(aCallback
) {}
236 NS_IMETHOD
Run() override
{
237 (void)mCallback
->Complete(NS_OK
, nullptr);
242 nsCOMPtr
<mozIStorageCompletionCallback
> mCallback
;
245 already_AddRefed
<nsIRunnable
> newCompletionEvent(
246 mozIStorageCompletionCallback
* aCallback
) {
247 NS_ASSERTION(aCallback
, "Passing a null callback is a no-no!");
248 nsCOMPtr
<nsIRunnable
> event
= new CallbackEvent(aCallback
);
249 return event
.forget();
252 } // namespace storage
253 } // namespace mozilla