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 "mozIStorageStatement.h"
23 #include "mozIStorageCompletionCallback.h"
24 #include "mozIStorageBindingParams.h"
26 #include "mozilla/Logging.h"
27 extern mozilla::LazyLogModule gStorageLog
;
33 convertResultCode(int aSQLiteResultCode
)
35 // Drop off the extended result bits of the result code.
36 int rc
= aSQLiteResultCode
& 0xFF;
45 return NS_ERROR_FILE_CORRUPTED
;
48 return NS_ERROR_FILE_ACCESS_DENIED
;
50 return NS_ERROR_STORAGE_BUSY
;
52 return NS_ERROR_FILE_IS_LOCKED
;
54 return NS_ERROR_FILE_READ_ONLY
;
56 return NS_ERROR_STORAGE_IOERR
;
59 return NS_ERROR_FILE_NO_DEVICE_SPACE
;
61 return NS_ERROR_OUT_OF_MEMORY
;
63 return NS_ERROR_UNEXPECTED
;
65 case SQLITE_INTERRUPT
:
66 return NS_ERROR_ABORT
;
67 case SQLITE_CONSTRAINT
:
68 return NS_ERROR_STORAGE_CONSTRAINT
;
73 nsAutoCString message
;
74 message
.AppendLiteral("SQLite returned error code ");
75 message
.AppendInt(rc
);
76 message
.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
77 NS_WARNING_ASSERTION(rc
== SQLITE_ERROR
, message
.get());
79 return NS_ERROR_FAILURE
;
83 checkAndLogStatementPerformance(sqlite3_stmt
*aStatement
)
85 // Check to see if the query performed sorting operations or not. If it
86 // did, it may need to be optimized!
87 int count
= ::sqlite3_stmt_status(aStatement
, SQLITE_STMTSTATUS_SORT
, 1);
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 "))
97 // CREATE INDEX always sorts (sorting is a necessary step in creating
98 // an index). So ignore the warning there.
99 if (::strstr(sql
, "CREATE INDEX") || ::strstr(sql
, "CREATE UNIQUE INDEX"))
102 nsAutoCString
message("Suboptimal indexes for the SQL statement ");
103 #ifdef MOZ_STORAGE_SORTWARNING_SQL_DUMP
106 message
.AppendLiteral("` [");
107 message
.AppendInt(count
);
108 message
.AppendLiteral(" sort operation(s)]");
110 nsPrintfCString
address("0x%p", aStatement
);
111 message
.Append(address
);
113 message
.AppendLiteral(" (http://mzl.la/1FuID0j).");
114 NS_WARNING(message
.get());
118 convertJSValToVariant(
120 const JS::Value
& aValue
)
122 if (aValue
.isInt32())
123 return new IntegerVariant(aValue
.toInt32());
125 if (aValue
.isDouble())
126 return new FloatVariant(aValue
.toDouble());
128 if (aValue
.isString()) {
129 nsAutoJSString value
;
130 if (!value
.init(aCtx
, aValue
.toString()))
132 return new TextVariant(value
);
135 if (aValue
.isBoolean())
136 return new IntegerVariant(aValue
.isTrue() ? 1 : 0);
139 return new NullVariant();
141 if (aValue
.isObject()) {
142 JS::Rooted
<JSObject
*> obj(aCtx
, &aValue
.toObject());
143 // We only support Date instances, all others fail.
145 if (!js::DateIsValid(aCtx
, obj
, &valid
) || !valid
)
149 if (!js::DateGetMsecSinceEpoch(aCtx
, obj
, &msecd
))
153 int64_t msec
= msecd
;
155 return new IntegerVariant(msec
);
162 convertVariantToStorageVariant(nsIVariant
* aVariant
)
164 RefPtr
<Variant_base
> variant
= do_QueryObject(aVariant
);
166 // JS helpers already convert the JS representation to a Storage Variant,
167 // in such a case there's nothing left to do here, so just pass-through.
172 return new NullVariant();
175 nsresult rv
= aVariant
->GetDataType(&dataType
);
176 NS_ENSURE_SUCCESS(rv
, nullptr);
179 case nsIDataType::VTYPE_BOOL
:
180 case nsIDataType::VTYPE_INT8
:
181 case nsIDataType::VTYPE_INT16
:
182 case nsIDataType::VTYPE_INT32
:
183 case nsIDataType::VTYPE_UINT8
:
184 case nsIDataType::VTYPE_UINT16
:
185 case nsIDataType::VTYPE_UINT32
:
186 case nsIDataType::VTYPE_INT64
:
187 case nsIDataType::VTYPE_UINT64
: {
189 rv
= aVariant
->GetAsInt64(&v
);
190 NS_ENSURE_SUCCESS(rv
, nullptr);
191 return new IntegerVariant(v
);
193 case nsIDataType::VTYPE_FLOAT
:
194 case nsIDataType::VTYPE_DOUBLE
: {
196 rv
= aVariant
->GetAsDouble(&v
);
197 NS_ENSURE_SUCCESS(rv
, nullptr);
198 return new FloatVariant(v
);
200 case nsIDataType::VTYPE_CHAR
:
201 case nsIDataType::VTYPE_CHAR_STR
:
202 case nsIDataType::VTYPE_STRING_SIZE_IS
:
203 case nsIDataType::VTYPE_UTF8STRING
:
204 case nsIDataType::VTYPE_CSTRING
: {
206 rv
= aVariant
->GetAsAUTF8String(v
);
207 NS_ENSURE_SUCCESS(rv
, nullptr);
208 return new UTF8TextVariant(v
);
210 case nsIDataType::VTYPE_WCHAR
:
211 case nsIDataType::VTYPE_DOMSTRING
:
212 case nsIDataType::VTYPE_WCHAR_STR
:
213 case nsIDataType::VTYPE_WSTRING_SIZE_IS
:
214 case nsIDataType::VTYPE_ASTRING
: {
216 rv
= aVariant
->GetAsAString(v
);
217 NS_ENSURE_SUCCESS(rv
, nullptr);
218 return new TextVariant(v
);
220 case nsIDataType::VTYPE_ARRAY
: {
225 // Note this copies the array data.
226 rv
= aVariant
->GetAsArray(&type
, &iid
, &len
, &rawArray
);
227 NS_ENSURE_SUCCESS(rv
, nullptr);
228 if (type
== nsIDataType::VTYPE_UINT8
) {
229 std::pair
<uint8_t *, int> v(static_cast<uint8_t *>(rawArray
), len
);
230 // Take ownership of the data avoiding a further copy.
231 return new AdoptedBlobVariant(v
);
235 case nsIDataType::VTYPE_EMPTY
:
236 case nsIDataType::VTYPE_EMPTY_ARRAY
:
237 case nsIDataType::VTYPE_VOID
:
238 return new NullVariant();
239 case nsIDataType::VTYPE_ID
:
240 case nsIDataType::VTYPE_INTERFACE
:
241 case nsIDataType::VTYPE_INTERFACE_IS
:
243 NS_WARNING("Unsupported variant type");
251 class CallbackEvent
: public Runnable
254 explicit CallbackEvent(mozIStorageCompletionCallback
* aCallback
)
255 : Runnable("storage::CallbackEvent")
256 , mCallback(aCallback
)
260 NS_IMETHOD
Run() override
262 (void)mCallback
->Complete(NS_OK
, nullptr);
266 nsCOMPtr
<mozIStorageCompletionCallback
> mCallback
;
269 already_AddRefed
<nsIRunnable
>
270 newCompletionEvent(mozIStorageCompletionCallback
*aCallback
)
272 NS_ASSERTION(aCallback
, "Passing a null callback is a no-no!");
273 nsCOMPtr
<nsIRunnable
> event
= new CallbackEvent(aCallback
);
274 return event
.forget();
277 } // namespace storage
278 } // namespace mozilla