2009-03-12 21:30:15 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2009-04-13 20:29:41 +04:00
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-03-12 21:30:15 +03:00
|
|
|
|
|
|
|
#include "sqlite3.h"
|
|
|
|
|
2012-01-15 12:13:07 +04:00
|
|
|
#include "jsfriendapi.h"
|
2009-07-23 02:31:03 +04:00
|
|
|
|
2009-03-20 23:28:16 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2009-03-12 21:30:15 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsError.h"
|
2010-06-22 01:23:00 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/CondVar.h"
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h"
|
2009-11-09 20:58:34 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2010-12-03 11:24:17 +03:00
|
|
|
#include "nsJSUtils.h"
|
2009-03-12 21:30:15 +03:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
#include "Variant.h"
|
2009-03-12 21:30:15 +03:00
|
|
|
#include "mozStoragePrivateHelpers.h"
|
2009-07-23 02:31:03 +04:00
|
|
|
#include "mozIStorageStatement.h"
|
2009-11-09 20:58:34 +03:00
|
|
|
#include "mozIStorageCompletionCallback.h"
|
2010-03-24 10:32:40 +03:00
|
|
|
#include "mozIStorageBindingParams.h"
|
2009-03-12 21:30:15 +03:00
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2016-01-28 21:35:00 +03:00
|
|
|
extern mozilla::LazyLogModule gStorageLog;
|
2011-04-30 01:35:22 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
2009-03-12 21:30:15 +03:00
|
|
|
nsresult
|
2009-05-09 04:29:56 +04:00
|
|
|
convertResultCode(int aSQLiteResultCode)
|
2009-03-12 21:30:15 +03:00
|
|
|
{
|
2010-06-22 01:23:00 +04:00
|
|
|
// Drop off the extended result bits of the result code.
|
|
|
|
int rc = aSQLiteResultCode & 0xFF;
|
|
|
|
|
|
|
|
switch (rc) {
|
2009-03-12 21:30:15 +03:00
|
|
|
case SQLITE_OK:
|
|
|
|
case SQLITE_ROW:
|
|
|
|
case SQLITE_DONE:
|
|
|
|
return NS_OK;
|
|
|
|
case SQLITE_CORRUPT:
|
|
|
|
case SQLITE_NOTADB:
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
case SQLITE_PERM:
|
|
|
|
case SQLITE_CANTOPEN:
|
|
|
|
return NS_ERROR_FILE_ACCESS_DENIED;
|
|
|
|
case SQLITE_BUSY:
|
|
|
|
return NS_ERROR_STORAGE_BUSY;
|
|
|
|
case SQLITE_LOCKED:
|
|
|
|
return NS_ERROR_FILE_IS_LOCKED;
|
|
|
|
case SQLITE_READONLY:
|
|
|
|
return NS_ERROR_FILE_READ_ONLY;
|
|
|
|
case SQLITE_IOERR:
|
|
|
|
return NS_ERROR_STORAGE_IOERR;
|
|
|
|
case SQLITE_FULL:
|
|
|
|
case SQLITE_TOOBIG:
|
|
|
|
return NS_ERROR_FILE_NO_DEVICE_SPACE;
|
|
|
|
case SQLITE_NOMEM:
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
case SQLITE_MISUSE:
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
case SQLITE_ABORT:
|
|
|
|
case SQLITE_INTERRUPT:
|
|
|
|
return NS_ERROR_ABORT;
|
2010-06-16 20:59:24 +04:00
|
|
|
case SQLITE_CONSTRAINT:
|
|
|
|
return NS_ERROR_STORAGE_CONSTRAINT;
|
2009-03-12 21:30:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// generic error
|
2010-04-16 16:31:18 +04:00
|
|
|
#ifdef DEBUG
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString message;
|
2010-04-22 16:53:56 +04:00
|
|
|
message.AppendLiteral("SQLite returned error code ");
|
2010-06-22 01:23:00 +04:00
|
|
|
message.AppendInt(rc);
|
2010-04-22 16:53:56 +04:00
|
|
|
message.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(rc == SQLITE_ERROR, message.get());
|
2010-04-16 16:31:18 +04:00
|
|
|
#endif
|
2009-03-12 21:30:15 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-09 04:29:56 +04:00
|
|
|
checkAndLogStatementPerformance(sqlite3_stmt *aStatement)
|
2009-03-12 21:30:15 +03:00
|
|
|
{
|
|
|
|
// Check to see if the query performed sorting operations or not. If it
|
|
|
|
// did, it may need to be optimized!
|
2009-05-09 04:29:56 +04:00
|
|
|
int count = ::sqlite3_stmt_status(aStatement, SQLITE_STMTSTATUS_SORT, 1);
|
2009-03-12 21:30:15 +03:00
|
|
|
if (count <= 0)
|
|
|
|
return;
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
const char *sql = ::sqlite3_sql(aStatement);
|
2009-03-20 23:28:16 +03:00
|
|
|
|
|
|
|
// Check to see if this is marked to not warn
|
2009-05-09 04:29:56 +04:00
|
|
|
if (::strstr(sql, "/* do not warn (bug "))
|
2009-03-20 23:28:16 +03:00
|
|
|
return;
|
|
|
|
|
2015-09-26 01:27:48 +03:00
|
|
|
// CREATE INDEX always sorts (sorting is a necessary step in creating
|
|
|
|
// an index). So ignore the warning there.
|
|
|
|
if (::strstr(sql, "CREATE INDEX") || ::strstr(sql, "CREATE UNIQUE INDEX"))
|
|
|
|
return;
|
|
|
|
|
2015-04-28 23:35:48 +03:00
|
|
|
nsAutoCString message("Suboptimal indexes for the SQL statement ");
|
2014-04-29 04:15:00 +04:00
|
|
|
#ifdef MOZ_STORAGE_SORTWARNING_SQL_DUMP
|
2015-04-28 23:35:48 +03:00
|
|
|
message.Append('`');
|
2014-04-29 04:15:00 +04:00
|
|
|
message.Append(sql);
|
2015-04-28 23:35:48 +03:00
|
|
|
message.AppendLiteral("` [");
|
|
|
|
message.AppendInt(count);
|
|
|
|
message.AppendLiteral(" sort operation(s)]");
|
2014-04-29 04:15:00 +04:00
|
|
|
#else
|
2009-03-20 23:28:16 +03:00
|
|
|
nsPrintfCString address("0x%p", aStatement);
|
|
|
|
message.Append(address);
|
2014-04-29 04:15:00 +04:00
|
|
|
#endif
|
2015-04-28 23:35:48 +03:00
|
|
|
message.AppendLiteral(" (http://mzl.la/1FuID0j).");
|
2009-03-12 21:30:15 +03:00
|
|
|
NS_WARNING(message.get());
|
|
|
|
}
|
2009-05-09 04:29:56 +04:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
nsIVariant *
|
|
|
|
convertJSValToVariant(
|
|
|
|
JSContext *aCtx,
|
2016-09-11 12:21:36 +03:00
|
|
|
const JS::Value& aValue)
|
2009-07-23 02:31:03 +04:00
|
|
|
{
|
2012-05-11 19:46:26 +04:00
|
|
|
if (aValue.isInt32())
|
|
|
|
return new IntegerVariant(aValue.toInt32());
|
2009-07-23 02:31:03 +04:00
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
if (aValue.isDouble())
|
|
|
|
return new FloatVariant(aValue.toDouble());
|
2009-07-23 02:31:03 +04:00
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
if (aValue.isString()) {
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString value;
|
|
|
|
if (!value.init(aCtx, aValue.toString()))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-03-24 10:32:40 +03:00
|
|
|
return new TextVariant(value);
|
2009-07-23 02:31:03 +04:00
|
|
|
}
|
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
if (aValue.isBoolean())
|
|
|
|
return new IntegerVariant(aValue.isTrue() ? 1 : 0);
|
2009-07-23 02:31:03 +04:00
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
if (aValue.isNull())
|
2010-03-24 10:32:40 +03:00
|
|
|
return new NullVariant();
|
2009-07-23 02:31:03 +04:00
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
if (aValue.isObject()) {
|
2014-10-23 21:53:31 +04:00
|
|
|
JS::Rooted<JSObject*> obj(aCtx, &aValue.toObject());
|
2010-03-24 10:32:40 +03:00
|
|
|
// We only support Date instances, all others fail.
|
2015-08-29 07:55:40 +03:00
|
|
|
bool valid;
|
|
|
|
if (!js::DateIsValid(aCtx, obj, &valid) || !valid)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
double msecd;
|
|
|
|
if (!js::DateGetMsecSinceEpoch(aCtx, obj, &msecd))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-11-09 20:58:34 +03:00
|
|
|
|
2009-07-23 02:31:03 +04:00
|
|
|
msecd *= 1000.0;
|
2012-10-26 03:25:58 +04:00
|
|
|
int64_t msec = msecd;
|
2009-11-09 20:58:34 +03:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
return new IntegerVariant(msec);
|
2009-07-23 02:31:03 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-07-23 02:31:03 +04:00
|
|
|
}
|
|
|
|
|
2015-03-12 19:55:56 +03:00
|
|
|
Variant_base *
|
|
|
|
convertVariantToStorageVariant(nsIVariant* aVariant)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Variant_base> variant = do_QueryObject(aVariant);
|
2015-03-12 19:55:56 +03:00
|
|
|
if (variant) {
|
|
|
|
// JS helpers already convert the JS representation to a Storage Variant,
|
|
|
|
// in such a case there's nothing left to do here, so just pass-through.
|
|
|
|
return variant;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aVariant)
|
|
|
|
return new NullVariant();
|
|
|
|
|
|
|
|
uint16_t dataType;
|
|
|
|
nsresult rv = aVariant->GetDataType(&dataType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
switch (dataType) {
|
|
|
|
case nsIDataType::VTYPE_BOOL:
|
|
|
|
case nsIDataType::VTYPE_INT8:
|
|
|
|
case nsIDataType::VTYPE_INT16:
|
|
|
|
case nsIDataType::VTYPE_INT32:
|
|
|
|
case nsIDataType::VTYPE_UINT8:
|
|
|
|
case nsIDataType::VTYPE_UINT16:
|
|
|
|
case nsIDataType::VTYPE_UINT32:
|
|
|
|
case nsIDataType::VTYPE_INT64:
|
|
|
|
case nsIDataType::VTYPE_UINT64: {
|
|
|
|
int64_t v;
|
|
|
|
rv = aVariant->GetAsInt64(&v);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
return new IntegerVariant(v);
|
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_FLOAT:
|
|
|
|
case nsIDataType::VTYPE_DOUBLE: {
|
|
|
|
double v;
|
|
|
|
rv = aVariant->GetAsDouble(&v);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
return new FloatVariant(v);
|
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_CHAR:
|
|
|
|
case nsIDataType::VTYPE_CHAR_STR:
|
|
|
|
case nsIDataType::VTYPE_STRING_SIZE_IS:
|
|
|
|
case nsIDataType::VTYPE_UTF8STRING:
|
|
|
|
case nsIDataType::VTYPE_CSTRING: {
|
|
|
|
nsCString v;
|
|
|
|
rv = aVariant->GetAsAUTF8String(v);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
return new UTF8TextVariant(v);
|
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_WCHAR:
|
|
|
|
case nsIDataType::VTYPE_DOMSTRING:
|
|
|
|
case nsIDataType::VTYPE_WCHAR_STR:
|
|
|
|
case nsIDataType::VTYPE_WSTRING_SIZE_IS:
|
|
|
|
case nsIDataType::VTYPE_ASTRING: {
|
|
|
|
nsString v;
|
|
|
|
rv = aVariant->GetAsAString(v);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
return new TextVariant(v);
|
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_ARRAY: {
|
|
|
|
uint16_t type;
|
|
|
|
nsIID iid;
|
|
|
|
uint32_t len;
|
|
|
|
void *rawArray;
|
|
|
|
// Note this copies the array data.
|
|
|
|
rv = aVariant->GetAsArray(&type, &iid, &len, &rawArray);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
if (type == nsIDataType::VTYPE_UINT8) {
|
|
|
|
std::pair<uint8_t *, int> v(static_cast<uint8_t *>(rawArray), len);
|
|
|
|
// Take ownership of the data avoiding a further copy.
|
|
|
|
return new AdoptedBlobVariant(v);
|
|
|
|
}
|
2015-11-23 09:09:23 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2015-03-12 19:55:56 +03:00
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_EMPTY:
|
|
|
|
case nsIDataType::VTYPE_EMPTY_ARRAY:
|
|
|
|
case nsIDataType::VTYPE_VOID:
|
|
|
|
return new NullVariant();
|
|
|
|
case nsIDataType::VTYPE_ID:
|
|
|
|
case nsIDataType::VTYPE_INTERFACE:
|
|
|
|
case nsIDataType::VTYPE_INTERFACE_IS:
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unsupported variant type");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2009-11-09 20:58:34 +03:00
|
|
|
namespace {
|
2016-04-26 03:23:21 +03:00
|
|
|
class CallbackEvent : public Runnable
|
2009-11-09 20:58:34 +03:00
|
|
|
{
|
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
explicit CallbackEvent(mozIStorageCompletionCallback* aCallback)
|
|
|
|
: Runnable("storage::CallbackEvent")
|
|
|
|
, mCallback(aCallback)
|
2009-11-09 20:58:34 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2009-11-09 20:58:34 +03:00
|
|
|
{
|
2013-06-27 17:00:59 +04:00
|
|
|
(void)mCallback->Complete(NS_OK, nullptr);
|
2009-11-09 20:58:34 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsCOMPtr<mozIStorageCompletionCallback> mCallback;
|
|
|
|
};
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2009-11-09 20:58:34 +03:00
|
|
|
already_AddRefed<nsIRunnable>
|
|
|
|
newCompletionEvent(mozIStorageCompletionCallback *aCallback)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aCallback, "Passing a null callback is a no-no!");
|
|
|
|
nsCOMPtr<nsIRunnable> event = new CallbackEvent(aCallback);
|
|
|
|
return event.forget();
|
|
|
|
}
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|