2009-03-12 18:30:15 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2009-04-13 16:29:41 +00:00
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 11:12:37 +00: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 18:30:15 +00:00
|
|
|
|
|
|
|
#include "sqlite3.h"
|
|
|
|
|
2012-01-15 08:13:07 +00:00
|
|
|
#include "jsfriendapi.h"
|
2009-07-22 22:31:03 +00:00
|
|
|
|
2009-03-20 20:28:16 +00:00
|
|
|
#include "nsPrintfCString.h"
|
2009-03-12 18:30:15 +00:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsError.h"
|
2010-06-21 21:23:00 +00:00
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/CondVar.h"
|
2015-04-15 16:47:03 +00:00
|
|
|
#include "nsQueryObject.h"
|
2009-11-09 17:58:34 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2010-12-03 08:24:17 +00:00
|
|
|
#include "nsJSUtils.h"
|
2009-03-12 18:30:15 +00:00
|
|
|
|
2010-03-24 07:32:40 +00:00
|
|
|
#include "Variant.h"
|
2009-03-12 18:30:15 +00:00
|
|
|
#include "mozStoragePrivateHelpers.h"
|
2009-07-22 22:31:03 +00:00
|
|
|
#include "mozIStorageStatement.h"
|
2009-11-09 17:58:34 +00:00
|
|
|
#include "mozIStorageCompletionCallback.h"
|
2010-03-24 07:32:40 +00:00
|
|
|
#include "mozIStorageBindingParams.h"
|
2009-03-12 18:30:15 +00:00
|
|
|
|
2015-05-19 18:15:34 +00:00
|
|
|
#include "mozilla/Logging.h"
|
2011-04-29 21:35:22 +00:00
|
|
|
extern PRLogModuleInfo* gStorageLog;
|
|
|
|
|
2009-05-09 00:29:56 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
2009-03-12 18:30:15 +00:00
|
|
|
nsresult
|
2009-05-09 00:29:56 +00:00
|
|
|
convertResultCode(int aSQLiteResultCode)
|
2009-03-12 18:30:15 +00:00
|
|
|
{
|
2010-06-21 21:23:00 +00:00
|
|
|
// Drop off the extended result bits of the result code.
|
|
|
|
int rc = aSQLiteResultCode & 0xFF;
|
|
|
|
|
|
|
|
switch (rc) {
|
2009-03-12 18:30:15 +00: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 16:59:24 +00:00
|
|
|
case SQLITE_CONSTRAINT:
|
|
|
|
return NS_ERROR_STORAGE_CONSTRAINT;
|
2009-03-12 18:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generic error
|
2010-04-16 12:31:18 +00:00
|
|
|
#ifdef DEBUG
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString message;
|
2010-04-22 12:53:56 +00:00
|
|
|
message.AppendLiteral("SQLite returned error code ");
|
2010-06-21 21:23:00 +00:00
|
|
|
message.AppendInt(rc);
|
2010-04-22 12:53:56 +00:00
|
|
|
message.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
|
2015-04-10 11:16:00 +00:00
|
|
|
NS_WARN_IF_FALSE(rc == SQLITE_ERROR, message.get());
|
2010-04-16 12:31:18 +00:00
|
|
|
#endif
|
2009-03-12 18:30:15 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-09 00:29:56 +00:00
|
|
|
checkAndLogStatementPerformance(sqlite3_stmt *aStatement)
|
2009-03-12 18:30:15 +00:00
|
|
|
{
|
|
|
|
// Check to see if the query performed sorting operations or not. If it
|
|
|
|
// did, it may need to be optimized!
|
2009-05-09 00:29:56 +00:00
|
|
|
int count = ::sqlite3_stmt_status(aStatement, SQLITE_STMTSTATUS_SORT, 1);
|
2009-03-12 18:30:15 +00:00
|
|
|
if (count <= 0)
|
|
|
|
return;
|
|
|
|
|
2009-05-09 00:29:56 +00:00
|
|
|
const char *sql = ::sqlite3_sql(aStatement);
|
2009-03-20 20:28:16 +00:00
|
|
|
|
|
|
|
// Check to see if this is marked to not warn
|
2009-05-09 00:29:56 +00:00
|
|
|
if (::strstr(sql, "/* do not warn (bug "))
|
2009-03-20 20:28:16 +00:00
|
|
|
return;
|
|
|
|
|
2015-04-28 20:35:48 +00:00
|
|
|
nsAutoCString message("Suboptimal indexes for the SQL statement ");
|
2014-04-29 00:15:00 +00:00
|
|
|
#ifdef MOZ_STORAGE_SORTWARNING_SQL_DUMP
|
2015-04-28 20:35:48 +00:00
|
|
|
message.Append('`');
|
2014-04-29 00:15:00 +00:00
|
|
|
message.Append(sql);
|
2015-04-28 20:35:48 +00:00
|
|
|
message.AppendLiteral("` [");
|
|
|
|
message.AppendInt(count);
|
|
|
|
message.AppendLiteral(" sort operation(s)]");
|
2014-04-29 00:15:00 +00:00
|
|
|
#else
|
2009-03-20 20:28:16 +00:00
|
|
|
nsPrintfCString address("0x%p", aStatement);
|
|
|
|
message.Append(address);
|
2014-04-29 00:15:00 +00:00
|
|
|
#endif
|
2015-04-28 20:35:48 +00:00
|
|
|
message.AppendLiteral(" (http://mzl.la/1FuID0j).");
|
2009-03-12 18:30:15 +00:00
|
|
|
NS_WARNING(message.get());
|
|
|
|
}
|
2009-05-09 00:29:56 +00:00
|
|
|
|
2010-03-24 07:32:40 +00:00
|
|
|
nsIVariant *
|
|
|
|
convertJSValToVariant(
|
|
|
|
JSContext *aCtx,
|
2012-05-11 15:46:26 +00:00
|
|
|
JS::Value aValue)
|
2009-07-22 22:31:03 +00:00
|
|
|
{
|
2012-05-11 15:46:26 +00:00
|
|
|
if (aValue.isInt32())
|
|
|
|
return new IntegerVariant(aValue.toInt32());
|
2009-07-22 22:31:03 +00:00
|
|
|
|
2012-05-11 15:46:26 +00:00
|
|
|
if (aValue.isDouble())
|
|
|
|
return new FloatVariant(aValue.toDouble());
|
2009-07-22 22:31:03 +00:00
|
|
|
|
2012-05-11 15:46:26 +00:00
|
|
|
if (aValue.isString()) {
|
2014-07-05 15:30:54 +00:00
|
|
|
nsAutoJSString value;
|
|
|
|
if (!value.init(aCtx, aValue.toString()))
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-03-24 07:32:40 +00:00
|
|
|
return new TextVariant(value);
|
2009-07-22 22:31:03 +00:00
|
|
|
}
|
|
|
|
|
2012-05-11 15:46:26 +00:00
|
|
|
if (aValue.isBoolean())
|
|
|
|
return new IntegerVariant(aValue.isTrue() ? 1 : 0);
|
2009-07-22 22:31:03 +00:00
|
|
|
|
2012-05-11 15:46:26 +00:00
|
|
|
if (aValue.isNull())
|
2010-03-24 07:32:40 +00:00
|
|
|
return new NullVariant();
|
2009-07-22 22:31:03 +00:00
|
|
|
|
2012-05-11 15:46:26 +00:00
|
|
|
if (aValue.isObject()) {
|
2014-10-23 17:53:31 +00:00
|
|
|
JS::Rooted<JSObject*> obj(aCtx, &aValue.toObject());
|
2010-03-24 07:32:40 +00:00
|
|
|
// We only support Date instances, all others fail.
|
2014-10-23 17:53:31 +00:00
|
|
|
if (!js::DateIsValid(aCtx, obj))
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2009-11-09 17:58:34 +00:00
|
|
|
|
2014-10-23 17:53:31 +00:00
|
|
|
double msecd = js::DateGetMsecSinceEpoch(aCtx, obj);
|
2009-07-22 22:31:03 +00:00
|
|
|
msecd *= 1000.0;
|
2012-10-25 23:25:58 +00:00
|
|
|
int64_t msec = msecd;
|
2009-11-09 17:58:34 +00:00
|
|
|
|
2010-03-24 07:32:40 +00:00
|
|
|
return new IntegerVariant(msec);
|
2009-07-22 22:31:03 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2009-07-22 22:31:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 16:55:56 +00:00
|
|
|
Variant_base *
|
|
|
|
convertVariantToStorageVariant(nsIVariant* aVariant)
|
|
|
|
{
|
|
|
|
nsRefPtr<Variant_base> variant = do_QueryObject(aVariant);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 17:58:34 +00:00
|
|
|
namespace {
|
|
|
|
class CallbackEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-08-07 23:47:28 +00:00
|
|
|
explicit CallbackEvent(mozIStorageCompletionCallback *aCallback)
|
2009-11-09 17:58:34 +00:00
|
|
|
: mCallback(aCallback)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-06-27 13:00:59 +00:00
|
|
|
(void)mCallback->Complete(NS_OK, nullptr);
|
2009-11-09 17:58:34 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsCOMPtr<mozIStorageCompletionCallback> mCallback;
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-06-21 21:23:00 +00:00
|
|
|
|
|
|
|
|
2009-05-09 00:29:56 +00:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|