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 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
24 * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25 * Shawn Wilsher <me@shawnwilsher.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
46 #include "nsPrintfCString.h"
49 #include "mozilla/Mutex.h"
50 #include "mozilla/CondVar.h"
51 #include "nsThreadUtils.h"
52 #include "nsJSUtils.h"
55 #include "mozStoragePrivateHelpers.h"
56 #include "mozIStorageStatement.h"
57 #include "mozIStorageCompletionCallback.h"
58 #include "mozIStorageBindingParams.h"
64 convertResultCode(int aSQLiteResultCode
)
66 // Drop off the extended result bits of the result code.
67 int rc
= aSQLiteResultCode
& 0xFF;
76 return NS_ERROR_FILE_CORRUPTED
;
79 return NS_ERROR_FILE_ACCESS_DENIED
;
81 return NS_ERROR_STORAGE_BUSY
;
83 return NS_ERROR_FILE_IS_LOCKED
;
85 return NS_ERROR_FILE_READ_ONLY
;
87 return NS_ERROR_STORAGE_IOERR
;
90 return NS_ERROR_FILE_NO_DEVICE_SPACE
;
92 return NS_ERROR_OUT_OF_MEMORY
;
94 return NS_ERROR_UNEXPECTED
;
96 case SQLITE_INTERRUPT
:
97 return NS_ERROR_ABORT
;
98 case SQLITE_CONSTRAINT
:
99 return NS_ERROR_STORAGE_CONSTRAINT
;
104 nsCAutoString message
;
105 message
.AppendLiteral("SQLite returned error code ");
106 message
.AppendInt(rc
);
107 message
.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
108 NS_WARNING(message
.get());
110 return NS_ERROR_FAILURE
;
114 checkAndLogStatementPerformance(sqlite3_stmt
*aStatement
)
116 // Check to see if the query performed sorting operations or not. If it
117 // did, it may need to be optimized!
118 int count
= ::sqlite3_stmt_status(aStatement
, SQLITE_STMTSTATUS_SORT
, 1);
122 const char *sql
= ::sqlite3_sql(aStatement
);
124 // Check to see if this is marked to not warn
125 if (::strstr(sql
, "/* do not warn (bug "))
128 nsCAutoString message
;
129 message
.AppendInt(count
);
131 message
.Append(" sort operation has ");
133 message
.Append(" sort operations have ");
134 message
.Append("occurred for the SQL statement '");
135 nsPrintfCString
address("0x%p", aStatement
);
136 message
.Append(address
);
137 message
.Append("'. See https://developer.mozilla.org/En/Storage/Warnings "
139 NS_WARNING(message
.get());
143 convertJSValToVariant(
147 if (JSVAL_IS_INT(aValue
))
148 return new IntegerVariant(JSVAL_TO_INT(aValue
));
150 if (JSVAL_IS_DOUBLE(aValue
))
151 return new FloatVariant(JSVAL_TO_DOUBLE(aValue
));
153 if (JSVAL_IS_STRING(aValue
)) {
154 JSString
*str
= JSVAL_TO_STRING(aValue
);
155 nsDependentJSString value
;
156 if (!value
.init(aCtx
, str
))
158 return new TextVariant(value
);
161 if (JSVAL_IS_BOOLEAN(aValue
))
162 return new IntegerVariant((aValue
== JSVAL_TRUE
) ? 1 : 0);
164 if (JSVAL_IS_NULL(aValue
))
165 return new NullVariant();
167 if (JSVAL_IS_OBJECT(aValue
)) {
168 JSObject
*obj
= JSVAL_TO_OBJECT(aValue
);
169 // We only support Date instances, all others fail.
170 if (!::js_DateIsValid(aCtx
, obj
))
173 double msecd
= ::js_DateGetMsecSinceEpoch(aCtx
, obj
);
178 return new IntegerVariant(msec
);
186 class CallbackEvent
: public nsRunnable
189 CallbackEvent(mozIStorageCompletionCallback
*aCallback
)
190 : mCallback(aCallback
)
196 (void)mCallback
->Complete();
200 nsCOMPtr
<mozIStorageCompletionCallback
> mCallback
;
202 } // anonymous namespace
203 already_AddRefed
<nsIRunnable
>
204 newCompletionEvent(mozIStorageCompletionCallback
*aCallback
)
206 NS_ASSERTION(aCallback
, "Passing a null callback is a no-no!");
207 nsCOMPtr
<nsIRunnable
> event
= new CallbackEvent(aCallback
);
208 return event
.forget();
212 * This code is heavily based on the sample at:
213 * http://www.sqlite.org/unlock_notify.html
217 class UnlockNotification
221 : mMutex("UnlockNotification mMutex")
222 , mCondVar(mMutex
, "UnlockNotification condVar")
229 mozilla::MutexAutoLock
lock(mMutex
);
231 (void)mCondVar
.Wait();
237 mozilla::MutexAutoLock
lock(mMutex
);
239 (void)mCondVar
.Notify();
243 mozilla::Mutex mMutex
;
244 mozilla::CondVar mCondVar
;
249 UnlockNotifyCallback(void **aArgs
,
252 for (int i
= 0; i
< aArgsSize
; i
++) {
253 UnlockNotification
*notification
=
254 static_cast<UnlockNotification
*>(aArgs
[i
]);
255 notification
->Signal();
260 WaitForUnlockNotify(sqlite3
* aDatabase
)
262 UnlockNotification notification
;
263 int srv
= ::sqlite3_unlock_notify(aDatabase
, UnlockNotifyCallback
,
265 NS_ASSERTION(srv
== SQLITE_LOCKED
|| srv
== SQLITE_OK
, "Bad result!");
266 if (srv
== SQLITE_OK
)
272 } // anonymous namespace
275 stepStmt(sqlite3_stmt
* aStatement
)
277 bool checkedMainThread
= false;
279 sqlite3
* db
= ::sqlite3_db_handle(aStatement
);
280 (void)::sqlite3_extended_result_codes(db
, 1);
283 while ((srv
= ::sqlite3_step(aStatement
)) == SQLITE_LOCKED_SHAREDCACHE
) {
284 if (!checkedMainThread
) {
285 checkedMainThread
= true;
286 if (NS_IsMainThread()) {
287 NS_WARNING("We won't allow blocking on the main thread!");
292 srv
= WaitForUnlockNotify(sqlite3_db_handle(aStatement
));
293 if (srv
!= SQLITE_OK
)
296 ::sqlite3_reset(aStatement
);
299 (void)::sqlite3_extended_result_codes(db
, 0);
300 // Drop off the extended result bits of the result code.
305 prepareStmt(sqlite3
* aDatabase
,
306 const nsCString
&aSQL
,
307 sqlite3_stmt
**_stmt
)
309 bool checkedMainThread
= false;
311 (void)::sqlite3_extended_result_codes(aDatabase
, 1);
314 while((srv
= ::sqlite3_prepare_v2(aDatabase
, aSQL
.get(), -1, _stmt
, NULL
)) ==
315 SQLITE_LOCKED_SHAREDCACHE
) {
316 if (!checkedMainThread
) {
317 checkedMainThread
= true;
318 if (NS_IsMainThread()) {
319 NS_WARNING("We won't allow blocking on the main thread!");
324 srv
= WaitForUnlockNotify(aDatabase
);
325 if (srv
!= SQLITE_OK
)
329 (void)::sqlite3_extended_result_codes(aDatabase
, 0);
330 // Drop off the extended result bits of the result code.
334 } // namespace storage
335 } // namespace mozilla