2009-04-17 14:19:31 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIMutableArray.h"
|
2007-07-07 14:14:51 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2011-03-31 10:19:30 -07:00
|
|
|
#include "nsIMemoryReporter.h"
|
2008-10-28 20:53:19 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2012-12-17 11:25:10 -08:00
|
|
|
#include "nsIFileURL.h"
|
2011-12-06 12:12:55 -08:00
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/CondVar.h"
|
2012-06-12 20:35:10 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-07 14:14:51 -07:00
|
|
|
#include "mozIStorageAggregateFunction.h"
|
2009-11-09 09:58:34 -08:00
|
|
|
#include "mozIStorageCompletionCallback.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "mozIStorageFunction.h"
|
|
|
|
|
2009-04-21 08:27:03 -07:00
|
|
|
#include "mozStorageAsyncStatementExecution.h"
|
2009-04-20 07:59:52 -07:00
|
|
|
#include "mozStorageSQLFunctions.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "mozStorageConnection.h"
|
|
|
|
#include "mozStorageService.h"
|
|
|
|
#include "mozStorageStatement.h"
|
2010-03-24 00:32:40 -07:00
|
|
|
#include "mozStorageAsyncStatement.h"
|
2009-04-20 08:01:51 -07:00
|
|
|
#include "mozStorageArgValueArray.h"
|
2008-10-30 15:50:00 -07:00
|
|
|
#include "mozStoragePrivateHelpers.h"
|
2009-06-17 12:12:51 -07:00
|
|
|
#include "mozStorageStatementData.h"
|
2010-03-24 00:32:40 -07:00
|
|
|
#include "StorageBaseStatementInternal.h"
|
2009-07-15 10:49:05 -07:00
|
|
|
#include "SQLCollations.h"
|
2011-12-15 23:34:24 -08:00
|
|
|
#include "FileSystemModule.h"
|
2012-01-05 15:46:35 -08:00
|
|
|
#include "mozStorageHelper.h"
|
2013-03-18 07:25:50 -07:00
|
|
|
#include "GeckoProfiler.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prprf.h"
|
2013-06-27 06:00:59 -07:00
|
|
|
#include "nsProxyRelease.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-05 02:29:06 -07:00
|
|
|
#define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
|
|
|
|
|
2013-02-21 02:12:48 -08:00
|
|
|
// Maximum size of the pages cache per connection.
|
|
|
|
#define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB
|
2011-11-09 06:06:40 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef PR_LOGGING
|
2012-07-30 07:20:58 -07:00
|
|
|
PRLogModuleInfo* gStorageLog = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
2011-01-17 09:25:17 -08:00
|
|
|
namespace {
|
|
|
|
|
2009-06-28 15:59:40 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2010-02-22 13:57:45 -08:00
|
|
|
//// Variant Specialization Functions (variantToSQLiteT)
|
2009-06-28 15:59:40 -07:00
|
|
|
|
2011-01-17 09:25:17 -08:00
|
|
|
int
|
2009-06-28 15:59:40 -07:00
|
|
|
sqlite3_T_int(sqlite3_context *aCtx,
|
|
|
|
int aValue)
|
|
|
|
{
|
|
|
|
::sqlite3_result_int(aCtx, aValue);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-17 09:25:17 -08:00
|
|
|
int
|
2009-06-28 15:59:40 -07:00
|
|
|
sqlite3_T_int64(sqlite3_context *aCtx,
|
|
|
|
sqlite3_int64 aValue)
|
|
|
|
{
|
|
|
|
::sqlite3_result_int64(aCtx, aValue);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-17 09:25:17 -08:00
|
|
|
int
|
2009-06-28 15:59:40 -07:00
|
|
|
sqlite3_T_double(sqlite3_context *aCtx,
|
|
|
|
double aValue)
|
|
|
|
{
|
|
|
|
::sqlite3_result_double(aCtx, aValue);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-17 09:25:17 -08:00
|
|
|
int
|
2010-02-22 13:57:45 -08:00
|
|
|
sqlite3_T_text(sqlite3_context *aCtx,
|
|
|
|
const nsCString &aValue)
|
|
|
|
{
|
|
|
|
::sqlite3_result_text(aCtx,
|
|
|
|
aValue.get(),
|
|
|
|
aValue.Length(),
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-17 09:25:17 -08:00
|
|
|
int
|
2009-06-28 15:59:40 -07:00
|
|
|
sqlite3_T_text16(sqlite3_context *aCtx,
|
2010-02-22 13:57:45 -08:00
|
|
|
const nsString &aValue)
|
2009-06-28 15:59:40 -07:00
|
|
|
{
|
|
|
|
::sqlite3_result_text16(aCtx,
|
2010-02-22 13:57:45 -08:00
|
|
|
aValue.get(),
|
2009-06-28 15:59:40 -07:00
|
|
|
aValue.Length() * 2, // Number of bytes.
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-17 09:25:17 -08:00
|
|
|
int
|
2009-06-28 15:59:40 -07:00
|
|
|
sqlite3_T_null(sqlite3_context *aCtx)
|
|
|
|
{
|
|
|
|
::sqlite3_result_null(aCtx);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-17 09:25:17 -08:00
|
|
|
int
|
2009-06-28 15:59:40 -07:00
|
|
|
sqlite3_T_blob(sqlite3_context *aCtx,
|
|
|
|
const void *aData,
|
|
|
|
int aSize)
|
|
|
|
{
|
|
|
|
::sqlite3_result_blob(aCtx, aData, aSize, NS_Free);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2010-02-22 13:57:45 -08:00
|
|
|
#include "variantToSQLiteT_impl.h"
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Modules
|
|
|
|
|
|
|
|
struct Module
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
int (*registerFunc)(sqlite3*, const char*);
|
|
|
|
};
|
|
|
|
|
|
|
|
Module gModules[] = {
|
|
|
|
{ "filesystem", RegisterFileSystemModule }
|
|
|
|
};
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Local Functions
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
void tracefunc (void *aClosure, const char *aStmt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
PR_LOG(gStorageLog, PR_LOG_DEBUG, ("sqlite3_trace on %p for '%s'", aClosure,
|
|
|
|
aStmt));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-04-17 14:19:31 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
struct FFEArguments
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
nsISupports *target;
|
|
|
|
bool found;
|
|
|
|
};
|
|
|
|
PLDHashOperator
|
|
|
|
findFunctionEnumerator(const nsACString &aKey,
|
2010-08-27 12:42:58 -07:00
|
|
|
Connection::FunctionInfo aData,
|
2009-04-17 14:19:31 -07:00
|
|
|
void *aUserArg)
|
|
|
|
{
|
|
|
|
FFEArguments *args = static_cast<FFEArguments *>(aUserArg);
|
2010-08-27 12:42:58 -07:00
|
|
|
if (aData.function == args->target) {
|
|
|
|
args->found = true;
|
2009-04-17 14:19:31 -07:00
|
|
|
return PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-27 12:42:58 -07:00
|
|
|
PLDHashOperator
|
|
|
|
copyFunctionEnumerator(const nsACString &aKey,
|
|
|
|
Connection::FunctionInfo aData,
|
|
|
|
void *aUserArg)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData.type == Connection::FunctionInfo::SIMPLE ||
|
|
|
|
aData.type == Connection::FunctionInfo::AGGREGATE,
|
|
|
|
"Invalid function type!");
|
|
|
|
|
|
|
|
Connection *connection = static_cast<Connection *>(aUserArg);
|
|
|
|
if (aData.type == Connection::FunctionInfo::SIMPLE) {
|
|
|
|
mozIStorageFunction *function =
|
|
|
|
static_cast<mozIStorageFunction *>(aData.function.get());
|
|
|
|
(void)connection->CreateFunction(aKey, aData.numArgs, function);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mozIStorageAggregateFunction *function =
|
|
|
|
static_cast<mozIStorageAggregateFunction *>(aData.function.get());
|
|
|
|
(void)connection->CreateAggregateFunction(aKey, aData.numArgs, function);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
void
|
|
|
|
basicFunctionHelper(sqlite3_context *aCtx,
|
|
|
|
int aArgc,
|
|
|
|
sqlite3_value **aArgv)
|
|
|
|
{
|
|
|
|
void *userData = ::sqlite3_user_data(aCtx);
|
|
|
|
|
|
|
|
mozIStorageFunction *func = static_cast<mozIStorageFunction *>(userData);
|
|
|
|
|
2009-04-20 08:01:51 -07:00
|
|
|
nsRefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!arguments)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIVariant> result;
|
|
|
|
if (NS_FAILED(func->OnFunctionCall(arguments, getter_AddRefs(result)))) {
|
|
|
|
NS_WARNING("User function returned error code!");
|
|
|
|
::sqlite3_result_error(aCtx,
|
|
|
|
"User function returned error code",
|
|
|
|
-1);
|
|
|
|
return;
|
|
|
|
}
|
2012-06-28 15:27:03 -07:00
|
|
|
int retcode = variantToSQLiteT(aCtx, result);
|
|
|
|
if (retcode == SQLITE_IGNORE) {
|
|
|
|
::sqlite3_result_int(aCtx, SQLITE_IGNORE);
|
|
|
|
} else if (retcode != SQLITE_OK) {
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_WARNING("User function returned invalid data type!");
|
|
|
|
::sqlite3_result_error(aCtx,
|
|
|
|
"User function returned invalid data type",
|
|
|
|
-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
aggregateFunctionStepHelper(sqlite3_context *aCtx,
|
|
|
|
int aArgc,
|
|
|
|
sqlite3_value **aArgv)
|
|
|
|
{
|
|
|
|
void *userData = ::sqlite3_user_data(aCtx);
|
|
|
|
mozIStorageAggregateFunction *func =
|
|
|
|
static_cast<mozIStorageAggregateFunction *>(userData);
|
|
|
|
|
2009-04-20 08:01:51 -07:00
|
|
|
nsRefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!arguments)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (NS_FAILED(func->OnStep(arguments)))
|
|
|
|
NS_WARNING("User aggregate step function returned error code!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
aggregateFunctionFinalHelper(sqlite3_context *aCtx)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
void *userData = ::sqlite3_user_data(aCtx);
|
|
|
|
mozIStorageAggregateFunction *func =
|
|
|
|
static_cast<mozIStorageAggregateFunction *>(userData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
nsRefPtr<nsIVariant> result;
|
|
|
|
if (NS_FAILED(func->OnFinal(getter_AddRefs(result)))) {
|
|
|
|
NS_WARNING("User aggregate final function returned error code!");
|
|
|
|
::sqlite3_result_error(aCtx,
|
|
|
|
"User aggregate final function returned error code",
|
|
|
|
-1);
|
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:57 -07:00
|
|
|
if (variantToSQLiteT(aCtx, result) != SQLITE_OK) {
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_WARNING("User aggregate final function returned invalid data type!");
|
|
|
|
::sqlite3_result_error(aCtx,
|
|
|
|
"User aggregate final function returned invalid data type",
|
|
|
|
-1);
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-06 12:12:55 -08:00
|
|
|
/**
|
|
|
|
* This code is heavily based on the sample at:
|
|
|
|
* http://www.sqlite.org/unlock_notify.html
|
|
|
|
*/
|
|
|
|
class UnlockNotification
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UnlockNotification()
|
|
|
|
: mMutex("UnlockNotification mMutex")
|
|
|
|
, mCondVar(mMutex, "UnlockNotification condVar")
|
|
|
|
, mSignaled(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Wait()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
while (!mSignaled) {
|
|
|
|
(void)mCondVar.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Signal()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mSignaled = true;
|
|
|
|
(void)mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex mMutex;
|
|
|
|
CondVar mCondVar;
|
|
|
|
bool mSignaled;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
UnlockNotifyCallback(void **aArgs,
|
|
|
|
int aArgsSize)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < aArgsSize; i++) {
|
|
|
|
UnlockNotification *notification =
|
|
|
|
static_cast<UnlockNotification *>(aArgs[i]);
|
|
|
|
notification->Signal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
WaitForUnlockNotify(sqlite3* aDatabase)
|
|
|
|
{
|
|
|
|
UnlockNotification notification;
|
|
|
|
int srv = ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback,
|
|
|
|
¬ification);
|
|
|
|
MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK);
|
|
|
|
if (srv == SQLITE_OK) {
|
|
|
|
notification.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv;
|
|
|
|
}
|
|
|
|
|
2009-11-09 09:58:34 -08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Local Classes
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-08-27 14:07:04 -07:00
|
|
|
class AsyncCloseConnection MOZ_FINAL: public nsRunnable
|
2009-11-09 09:58:34 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
AsyncCloseConnection(Connection *aConnection,
|
2012-10-18 11:50:39 -07:00
|
|
|
nsIRunnable *aCallbackEvent,
|
|
|
|
already_AddRefed<nsIThread> aAsyncExecutionThread)
|
2009-11-09 09:58:34 -08:00
|
|
|
: mConnection(aConnection)
|
|
|
|
, mCallbackEvent(aCallbackEvent)
|
2012-10-18 11:50:39 -07:00
|
|
|
, mAsyncExecutionThread(aAsyncExecutionThread)
|
2009-11-09 09:58:34 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD Run()
|
|
|
|
{
|
2012-10-18 11:50:39 -07:00
|
|
|
#ifdef DEBUG
|
2013-08-27 14:07:04 -07:00
|
|
|
// This code is executed on the background thread
|
|
|
|
bool onAsyncThread = false;
|
|
|
|
(void)mAsyncExecutionThread->IsOnCurrentThread(&onAsyncThread);
|
|
|
|
MOZ_ASSERT(onAsyncThread);
|
|
|
|
#endif // DEBUG
|
2009-11-09 09:58:34 -08:00
|
|
|
|
2013-08-27 14:07:04 -07:00
|
|
|
// Internal close.
|
2009-11-09 09:58:34 -08:00
|
|
|
(void)mConnection->internalClose();
|
2013-08-27 14:07:04 -07:00
|
|
|
|
|
|
|
// Callback
|
|
|
|
if (mCallbackEvent) {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
(void)NS_GetMainThread(getter_AddRefs(thread));
|
|
|
|
(void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2010-03-29 10:54:41 -07:00
|
|
|
|
2009-11-09 09:58:34 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-08-27 14:07:04 -07:00
|
|
|
|
|
|
|
~AsyncCloseConnection() {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
(void)NS_GetMainThread(getter_AddRefs(thread));
|
|
|
|
// Handle ambiguous nsISupports inheritance.
|
|
|
|
Connection *rawConnection = nullptr;
|
|
|
|
mConnection.swap(rawConnection);
|
|
|
|
(void)NS_ProxyRelease(thread,
|
|
|
|
NS_ISUPPORTS_CAST(mozIStorageConnection *,
|
|
|
|
rawConnection));
|
|
|
|
(void)NS_ProxyRelease(thread, mCallbackEvent);
|
|
|
|
}
|
2009-11-09 09:58:34 -08:00
|
|
|
private:
|
2010-10-05 15:01:22 -07:00
|
|
|
nsRefPtr<Connection> mConnection;
|
2009-11-09 09:58:34 -08:00
|
|
|
nsCOMPtr<nsIRunnable> mCallbackEvent;
|
2012-10-18 11:50:39 -07:00
|
|
|
nsCOMPtr<nsIThread> mAsyncExecutionThread;
|
2009-11-09 09:58:34 -08:00
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-27 06:00:59 -07:00
|
|
|
/**
|
|
|
|
* An event used to initialize the clone of a connection.
|
|
|
|
*
|
|
|
|
* Must be executed on the clone's async execution thread.
|
|
|
|
*/
|
|
|
|
class AsyncInitializeClone MOZ_FINAL: public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aConnection The connection being cloned.
|
|
|
|
* @param aClone The clone.
|
|
|
|
* @param aReadOnly If |true|, the clone is read only.
|
|
|
|
* @param aCallback A callback to trigger once initialization
|
|
|
|
* is complete. This event will be called on
|
|
|
|
* aClone->threadOpenedOn.
|
|
|
|
*/
|
|
|
|
AsyncInitializeClone(Connection* aConnection,
|
|
|
|
Connection* aClone,
|
|
|
|
const bool aReadOnly,
|
|
|
|
mozIStorageCompletionCallback* aCallback)
|
|
|
|
: mConnection(aConnection)
|
|
|
|
, mClone(aClone)
|
|
|
|
, mReadOnly(aReadOnly)
|
|
|
|
, mCallback(aCallback)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
MOZ_ASSERT (NS_GetCurrentThread() == mClone->getAsyncExecutionTarget());
|
|
|
|
|
|
|
|
nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return Dispatch(rv, nullptr);
|
|
|
|
}
|
|
|
|
return Dispatch(NS_OK,
|
|
|
|
NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
|
|
|
|
nsRefPtr<CallbackComplete> event = new CallbackComplete(aResult,
|
|
|
|
aValue,
|
|
|
|
mCallback.forget());
|
|
|
|
return mClone->threadOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AsyncInitializeClone() {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
// Handle ambiguous nsISupports inheritance.
|
|
|
|
Connection *rawConnection = nullptr;
|
|
|
|
mConnection.swap(rawConnection);
|
|
|
|
(void)NS_ProxyRelease(thread, NS_ISUPPORTS_CAST(mozIStorageConnection *,
|
|
|
|
rawConnection));
|
|
|
|
|
|
|
|
Connection *rawClone = nullptr;
|
|
|
|
mClone.swap(rawClone);
|
|
|
|
(void)NS_ProxyRelease(thread, NS_ISUPPORTS_CAST(mozIStorageConnection *,
|
|
|
|
rawClone));
|
|
|
|
|
|
|
|
// Generally, the callback will be released by CallbackComplete.
|
|
|
|
// However, if for some reason Run() is not executed, we still
|
|
|
|
// need to ensure that it is released here.
|
|
|
|
mozIStorageCompletionCallback *rawCallback = nullptr;
|
|
|
|
mCallback.swap(rawCallback);
|
|
|
|
(void)NS_ProxyRelease(thread, rawCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<Connection> mConnection;
|
|
|
|
nsRefPtr<Connection> mClone;
|
|
|
|
const bool mReadOnly;
|
|
|
|
nsCOMPtr<mozIStorageCompletionCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Connection
|
|
|
|
|
2010-08-27 12:42:58 -07:00
|
|
|
Connection::Connection(Service *aService,
|
2013-06-27 06:00:59 -07:00
|
|
|
int aFlags,
|
|
|
|
bool aAsyncOnly)
|
2009-09-02 13:30:24 -07:00
|
|
|
: sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex")
|
2010-03-24 00:32:40 -07:00
|
|
|
, sharedDBMutex("Connection::sharedDBMutex")
|
2009-12-02 10:53:46 -08:00
|
|
|
, threadOpenedOn(do_GetCurrentThread())
|
2012-07-30 07:20:58 -07:00
|
|
|
, mDBConn(nullptr)
|
2009-11-09 09:58:34 -08:00
|
|
|
, mAsyncExecutionThreadShuttingDown(false)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mTransactionInProgress(false)
|
2012-07-30 07:20:58 -07:00
|
|
|
, mProgressHandler(nullptr)
|
2010-08-27 12:42:58 -07:00
|
|
|
, mFlags(aFlags)
|
2009-04-17 14:19:31 -07:00
|
|
|
, mStorageService(aService)
|
2013-06-27 06:00:59 -07:00
|
|
|
, mAsyncOnly(aAsyncOnly)
|
2009-04-17 14:19:31 -07:00
|
|
|
{
|
|
|
|
mFunctions.Init();
|
2011-12-05 15:02:59 -08:00
|
|
|
mStorageService->registerConnection(this);
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Connection::~Connection()
|
|
|
|
{
|
|
|
|
(void)Close();
|
2012-10-18 11:50:39 -07:00
|
|
|
|
2013-06-27 06:00:59 -07:00
|
|
|
MOZ_ASSERT(!mAsyncExecutionThread,
|
|
|
|
"AsyncClose has not been invoked on this connection!");
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
|
|
|
|
2013-07-18 19:24:15 -07:00
|
|
|
NS_IMPL_ADDREF(Connection)
|
2013-06-27 06:00:59 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(Connection)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(mozIStorageConnection, !mAsyncOnly)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
|
|
|
|
NS_INTERFACE_MAP_END
|
2009-04-17 14:19:31 -07:00
|
|
|
|
2013-07-18 19:24:15 -07:00
|
|
|
// This is identical to what NS_IMPL_RELEASE provides, but with the
|
2011-12-05 15:02:59 -08:00
|
|
|
// extra |1 == count| case.
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) Connection::Release(void)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
2013-07-18 19:24:15 -07:00
|
|
|
nsrefcnt count = --mRefCnt;
|
2011-12-05 15:02:59 -08:00
|
|
|
NS_LOG_RELEASE(this, count, "Connection");
|
|
|
|
if (1 == count) {
|
|
|
|
// If the refcount is 1, the single reference must be from
|
|
|
|
// gService->mConnections (in class |Service|). Which means we can
|
|
|
|
// unregister it safely.
|
|
|
|
mStorageService->unregisterConnection(this);
|
|
|
|
} else if (0 == count) {
|
|
|
|
mRefCnt = 1; /* stabilize */
|
2013-06-27 06:00:59 -07:00
|
|
|
#if 0 /* enable this to find non-threadsafe destructors: */
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Connection);
|
|
|
|
#endif
|
2011-12-05 15:02:59 -08:00
|
|
|
delete (this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
nsIEventTarget *
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::getAsyncExecutionTarget()
|
|
|
|
{
|
2009-11-09 09:58:34 -08:00
|
|
|
MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
|
|
|
|
// If we are shutting down the asynchronous thread, don't hand out any more
|
|
|
|
// references to the thread.
|
|
|
|
if (mAsyncExecutionThreadShuttingDown)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2009-04-17 14:19:31 -07:00
|
|
|
|
|
|
|
if (!mAsyncExecutionThread) {
|
|
|
|
nsresult rv = ::NS_NewThread(getter_AddRefs(mAsyncExecutionThread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create async thread.");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-12 10:06:20 -07:00
|
|
|
static nsThreadPoolNaming naming;
|
|
|
|
naming.SetThreadPoolName(NS_LITERAL_CSTRING("mozStorage"),
|
|
|
|
mAsyncExecutionThread);
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
return mAsyncExecutionThread;
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-17 11:25:10 -08:00
|
|
|
Connection::initialize()
|
2009-04-17 14:19:31 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION (!mDBConn, "Initialize called on already opened database!");
|
2013-03-15 21:47:02 -07:00
|
|
|
PROFILER_LABEL("storage", "Connection::initialize");
|
2009-04-17 14:19:31 -07:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
// in memory database requested, sqlite uses a magic file name
|
2013-07-31 08:44:58 -07:00
|
|
|
int srv = ::sqlite3_open_v2(":memory:", &mDBConn, mFlags, nullptr);
|
2012-12-17 11:25:10 -08:00
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
mDBConn = nullptr;
|
|
|
|
return convertResultCode(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return initializeInternal(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Connection::initialize(nsIFile *aDatabaseFile)
|
|
|
|
{
|
|
|
|
NS_ASSERTION (aDatabaseFile, "Passed null file!");
|
|
|
|
NS_ASSERTION (!mDBConn, "Initialize called on already opened database!");
|
2013-03-15 21:47:02 -07:00
|
|
|
PROFILER_LABEL("storage", "Connection::initialize");
|
2009-04-17 14:19:31 -07:00
|
|
|
|
|
|
|
mDatabaseFile = aDatabaseFile;
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
nsAutoString path;
|
|
|
|
nsresult rv = aDatabaseFile->GetPath(path);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-04-17 14:19:31 -07:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
int srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
|
2013-07-31 08:44:58 -07:00
|
|
|
mFlags, nullptr);
|
2012-12-17 11:25:10 -08:00
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
mDBConn = nullptr;
|
|
|
|
return convertResultCode(srv);
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
2012-12-17 11:25:10 -08:00
|
|
|
|
|
|
|
rv = initializeInternal(aDatabaseFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mDatabaseFile = aDatabaseFile;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Connection::initialize(nsIFileURL *aFileURL)
|
|
|
|
{
|
|
|
|
NS_ASSERTION (aFileURL, "Passed null file URL!");
|
|
|
|
NS_ASSERTION (!mDBConn, "Initialize called on already opened database!");
|
2013-03-15 21:47:02 -07:00
|
|
|
PROFILER_LABEL("storage", "Connection::initialize");
|
2012-12-17 11:25:10 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> databaseFile;
|
|
|
|
nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
rv = aFileURL->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-07-31 08:44:58 -07:00
|
|
|
int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, nullptr);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (srv != SQLITE_OK) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mDBConn = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = initializeInternal(databaseFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mFileURL = aFileURL;
|
|
|
|
mDatabaseFile = databaseFile;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Connection::initializeInternal(nsIFile* aDatabaseFile)
|
|
|
|
{
|
2009-07-22 15:18:33 -07:00
|
|
|
// Properly wrap the database handle's mutex.
|
2010-03-24 00:32:40 -07:00
|
|
|
sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
|
2009-07-22 15:18:33 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef PR_LOGGING
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!gStorageLog)
|
|
|
|
gStorageLog = ::PR_NewLogModule("mozStorage");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
::sqlite3_trace(mDBConn, tracefunc, this);
|
2008-07-11 12:00:58 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString leafName(":memory");
|
2009-04-17 14:19:31 -07:00
|
|
|
if (aDatabaseFile)
|
|
|
|
(void)aDatabaseFile->GetNativeLeafName(leafName);
|
|
|
|
PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Opening connection to '%s' (%p)",
|
|
|
|
leafName.get(), this));
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2011-11-09 06:06:40 -08:00
|
|
|
|
2013-04-04 17:14:46 -07:00
|
|
|
int64_t pageSize = Service::getDefaultPageSize();
|
|
|
|
|
2011-11-09 06:06:40 -08:00
|
|
|
// Set page_size to the preferred default value. This is effective only if
|
|
|
|
// the database has just been created, otherwise, if the database does not
|
|
|
|
// use WAL journal mode, a VACUUM operation will updated its page_size.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
|
2012-01-05 15:46:35 -08:00
|
|
|
"PRAGMA page_size = ");
|
2011-11-09 06:06:40 -08:00
|
|
|
pageSizeQuery.AppendInt(pageSize);
|
2012-12-17 11:25:10 -08:00
|
|
|
nsresult rv = ExecuteSimpleSQL(pageSizeQuery);
|
2011-11-09 06:06:40 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Setting the cache_size forces the database open, verifying if it is valid
|
|
|
|
// or corrupt. So this is executed regardless it being actually needed.
|
|
|
|
// The cache_size is calculated from the actual page_size, to save memory.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
|
2012-01-05 15:46:35 -08:00
|
|
|
"PRAGMA cache_size = ");
|
2013-02-21 02:12:48 -08:00
|
|
|
cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
|
|
|
|
int srv = executeSql(cacheSizeQuery.get());
|
2009-07-15 10:49:05 -07:00
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
::sqlite3_close(mDBConn);
|
2012-07-30 07:20:58 -07:00
|
|
|
mDBConn = nullptr;
|
2009-07-15 10:49:05 -07:00
|
|
|
return convertResultCode(srv);
|
|
|
|
}
|
|
|
|
|
2011-11-09 06:06:40 -08:00
|
|
|
// Register our built-in SQL functions.
|
|
|
|
srv = registerFunctions(mDBConn);
|
2009-07-15 10:49:05 -07:00
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
::sqlite3_close(mDBConn);
|
2012-07-30 07:20:58 -07:00
|
|
|
mDBConn = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
2007-08-10 09:19:57 -07:00
|
|
|
|
2011-11-09 06:06:40 -08:00
|
|
|
// Register our built-in SQL collating sequences.
|
|
|
|
srv = registerCollations(mDBConn, mStorageService);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
::sqlite3_close(mDBConn);
|
2012-07-30 07:20:58 -07:00
|
|
|
mDBConn = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
|
|
|
|
2010-08-06 10:28:46 -07:00
|
|
|
// Set the synchronous PRAGMA, according to the preference.
|
|
|
|
switch (Service::getSynchronousPref()) {
|
2009-04-17 14:19:31 -07:00
|
|
|
case 2:
|
|
|
|
(void)ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"PRAGMA synchronous = FULL;"));
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
(void)ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"PRAGMA synchronous = OFF;"));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
default:
|
|
|
|
(void)ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"PRAGMA synchronous = NORMAL;"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
nsresult
|
|
|
|
Connection::databaseElementExists(enum DatabaseElementType aElementType,
|
|
|
|
const nsACString &aElementName,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *_exists)
|
2009-04-17 14:19:31 -07:00
|
|
|
{
|
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString query("SELECT name FROM sqlite_master WHERE type = '");
|
2009-04-17 14:19:31 -07:00
|
|
|
switch (aElementType) {
|
|
|
|
case INDEX:
|
|
|
|
query.Append("index");
|
|
|
|
break;
|
|
|
|
case TABLE:
|
|
|
|
query.Append("table");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
query.Append("' AND name ='");
|
|
|
|
query.Append(aElementName);
|
|
|
|
query.Append("'");
|
|
|
|
|
|
|
|
sqlite3_stmt *stmt;
|
2011-12-06 12:12:55 -08:00
|
|
|
int srv = prepareStatement(query, &stmt);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (srv != SQLITE_OK)
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2009-04-17 14:19:31 -07:00
|
|
|
|
2011-12-06 12:12:55 -08:00
|
|
|
srv = stepStatement(stmt);
|
2009-04-17 14:19:31 -07:00
|
|
|
// we just care about the return value from step
|
|
|
|
(void)::sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
if (srv == SQLITE_ROW) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_exists = true;
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (srv == SQLITE_DONE) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_exists = false;
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
bool
|
|
|
|
Connection::findFunctionByInstance(nsISupports *aInstance)
|
|
|
|
{
|
2010-03-24 00:32:40 -07:00
|
|
|
sharedDBMutex.assertCurrentThreadOwns();
|
2009-04-17 14:19:31 -07:00
|
|
|
FFEArguments args = { aInstance, false };
|
2010-08-27 12:42:58 -07:00
|
|
|
(void)mFunctions.EnumerateRead(findFunctionEnumerator, &args);
|
2009-04-17 14:19:31 -07:00
|
|
|
return args.found;
|
|
|
|
}
|
2008-05-23 07:56:10 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
/* static */ int
|
|
|
|
Connection::sProgressHelper(void *aArg)
|
|
|
|
{
|
|
|
|
Connection *_this = static_cast<Connection *>(aArg);
|
|
|
|
return _this->progressHandler();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
int
|
|
|
|
Connection::progressHandler()
|
|
|
|
{
|
2010-03-24 00:32:40 -07:00
|
|
|
sharedDBMutex.assertCurrentThreadOwns();
|
2009-04-17 14:19:31 -07:00
|
|
|
if (mProgressHandler) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool result;
|
2009-04-17 14:19:31 -07:00
|
|
|
nsresult rv = mProgressHandler->OnProgress(this, &result);
|
|
|
|
if (NS_FAILED(rv)) return 0; // Don't break request
|
|
|
|
return result ? 1 : 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-11-09 09:58:34 -08:00
|
|
|
nsresult
|
|
|
|
Connection::setClosedState()
|
|
|
|
{
|
|
|
|
// Ensure that we are on the correct thread to close the database.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool onOpenedThread;
|
2009-11-09 09:58:34 -08:00
|
|
|
nsresult rv = threadOpenedOn->IsOnCurrentThread(&onOpenedThread);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!onOpenedThread) {
|
|
|
|
NS_ERROR("Must close the database on the thread that you opened it with!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2009-11-09 09:58:34 -08:00
|
|
|
|
2009-11-09 09:58:34 -08:00
|
|
|
// Flag that we are shutting down the async thread, so that
|
|
|
|
// getAsyncExecutionTarget knows not to expose/create the async thread.
|
|
|
|
{
|
|
|
|
MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
|
|
|
|
NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
|
|
|
|
mAsyncExecutionThreadShuttingDown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:58:56 -07:00
|
|
|
bool
|
|
|
|
Connection::isAsyncClosing() {
|
|
|
|
MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
|
2012-10-18 11:50:39 -07:00
|
|
|
return mAsyncExecutionThreadShuttingDown && ConnectionReady();
|
2012-05-08 11:58:56 -07:00
|
|
|
}
|
|
|
|
|
2009-11-09 09:58:34 -08:00
|
|
|
nsresult
|
|
|
|
Connection::internalClose()
|
2007-08-30 06:43:17 -07:00
|
|
|
{
|
2009-11-09 09:58:34 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Sanity checks to make sure we are in the proper state before calling this.
|
|
|
|
NS_ASSERTION(mDBConn, "Database connection is already null!");
|
|
|
|
|
|
|
|
{ // Make sure we have marked our async thread as shutting down.
|
|
|
|
MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
|
|
|
|
NS_ASSERTION(mAsyncExecutionThreadShuttingDown,
|
|
|
|
"Did not call setClosedState!");
|
|
|
|
}
|
|
|
|
|
2013-08-27 14:07:04 -07:00
|
|
|
bool onOpeningThread = false;
|
|
|
|
(void)threadOpenedOn->IsOnCurrentThread(&onOpeningThread);
|
|
|
|
#endif // DEBUG
|
2007-08-30 06:43:17 -07:00
|
|
|
|
2008-07-11 12:00:58 -07:00
|
|
|
#ifdef PR_LOGGING
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString leafName(":memory");
|
2009-04-17 14:19:31 -07:00
|
|
|
if (mDatabaseFile)
|
|
|
|
(void)mDatabaseFile->GetNativeLeafName(leafName);
|
2009-11-05 05:52:55 -08:00
|
|
|
PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Closing connection to '%s'",
|
2009-04-17 14:19:31 -07:00
|
|
|
leafName.get()));
|
2008-07-11 12:00:58 -07:00
|
|
|
#endif
|
|
|
|
|
2013-08-27 14:07:04 -07:00
|
|
|
// At this stage, we may still have statements that need to be
|
|
|
|
// finalized. Attempt to close the database connection. This will
|
|
|
|
// always disconnect any virtual tables and cleanly finalize their
|
|
|
|
// internal statements. Once this is done, closing may fail due to
|
|
|
|
// unfinalized client statements, in which case we need to finalize
|
|
|
|
// these statements and close again.
|
|
|
|
|
|
|
|
int srv = sqlite3_close(mDBConn);
|
|
|
|
|
|
|
|
if (srv == SQLITE_BUSY) {
|
|
|
|
// We still have non-finalized statements. Finalize them.
|
|
|
|
|
|
|
|
sqlite3_stmt *stmt = NULL;
|
|
|
|
while ((stmt = ::sqlite3_next_stmt(mDBConn, stmt))) {
|
|
|
|
PR_LOG(gStorageLog, PR_LOG_NOTICE,
|
|
|
|
("Auto-finalizing SQL statement '%s' (%x)",
|
|
|
|
::sqlite3_sql(stmt),
|
|
|
|
stmt));
|
|
|
|
|
2008-12-08 14:14:14 -08:00
|
|
|
#ifdef DEBUG
|
2013-08-27 14:07:04 -07:00
|
|
|
char *msg = ::PR_smprintf("SQL statement '%s' (%x) should have been finalized",
|
|
|
|
::sqlite3_sql(stmt),
|
|
|
|
stmt);
|
|
|
|
NS_WARNING(msg);
|
|
|
|
::PR_smprintf_free(msg);
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
srv = ::sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
char *msg = ::PR_smprintf("Could not finalize SQL statement '%s' (%x)",
|
|
|
|
::sqlite3_sql(stmt),
|
|
|
|
stmt);
|
|
|
|
NS_WARNING(msg);
|
|
|
|
::PR_smprintf_free(msg);
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
// Ensure that the loop continues properly, whether closing has succeeded
|
|
|
|
// or not.
|
|
|
|
if (srv == SQLITE_OK) {
|
|
|
|
stmt = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that all statements have been finalized, we
|
|
|
|
// shoudl be able to close.
|
|
|
|
srv = ::sqlite3_close(mDBConn);
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
2008-12-08 14:14:14 -08:00
|
|
|
|
2013-08-27 14:07:04 -07:00
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
MOZ_ASSERT(srv == SQLITE_OK,
|
2009-06-26 15:03:53 -07:00
|
|
|
"sqlite3_close failed. There are probably outstanding statements that are listed above!");
|
2013-08-27 14:07:04 -07:00
|
|
|
}
|
2009-11-09 09:58:34 -08:00
|
|
|
|
2013-07-31 08:44:58 -07:00
|
|
|
mDBConn = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-08-30 06:43:17 -07:00
|
|
|
}
|
|
|
|
|
2010-05-19 16:22:19 -07:00
|
|
|
nsCString
|
|
|
|
Connection::getFilename()
|
|
|
|
{
|
2010-05-20 17:21:09 -07:00
|
|
|
nsCString leafname(":memory:");
|
|
|
|
if (mDatabaseFile) {
|
|
|
|
(void)mDatabaseFile->GetNativeLeafName(leafname);
|
|
|
|
}
|
2010-05-19 16:22:19 -07:00
|
|
|
return leafname;
|
|
|
|
}
|
|
|
|
|
2011-12-06 12:12:55 -08:00
|
|
|
int
|
|
|
|
Connection::stepStatement(sqlite3_stmt *aStatement)
|
|
|
|
{
|
2012-06-07 05:33:13 -07:00
|
|
|
MOZ_ASSERT(aStatement);
|
2011-12-06 12:12:55 -08:00
|
|
|
bool checkedMainThread = false;
|
|
|
|
TimeStamp startTime = TimeStamp::Now();
|
|
|
|
|
2012-03-01 05:13:45 -08:00
|
|
|
// mDBConn may be null if the executing statement has been created and cached
|
|
|
|
// after a call to asyncClose() but before the connection has been nullified
|
|
|
|
// by internalClose(). In such a case closing the connection fails due to
|
|
|
|
// the existence of prepared statements, but mDBConn is set to null
|
|
|
|
// regardless. This usually happens when other tasks using cached statements
|
|
|
|
// are asynchronously scheduled for execution and any of them ends up after
|
|
|
|
// asyncClose. See bug 728653 for details.
|
|
|
|
if (!mDBConn)
|
|
|
|
return SQLITE_MISUSE;
|
|
|
|
|
2011-12-06 12:12:55 -08:00
|
|
|
(void)::sqlite3_extended_result_codes(mDBConn, 1);
|
|
|
|
|
|
|
|
int srv;
|
|
|
|
while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
|
|
|
|
if (!checkedMainThread) {
|
|
|
|
checkedMainThread = true;
|
|
|
|
if (::NS_IsMainThread()) {
|
|
|
|
NS_WARNING("We won't allow blocking on the main thread!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
srv = WaitForUnlockNotify(mDBConn);
|
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
::sqlite3_reset(aStatement);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report very slow SQL statements to Telemetry
|
|
|
|
TimeDuration duration = TimeStamp::Now() - startTime;
|
2013-01-22 11:12:17 -08:00
|
|
|
const uint32_t threshold =
|
|
|
|
NS_IsMainThread() ? Telemetry::kSlowSQLThresholdForMainThread
|
|
|
|
: Telemetry::kSlowSQLThresholdForHelperThreads;
|
|
|
|
if (duration.ToMilliseconds() >= threshold) {
|
2012-06-07 05:33:13 -07:00
|
|
|
nsDependentCString statementString(::sqlite3_sql(aStatement));
|
|
|
|
Telemetry::RecordSlowSQLStatement(statementString, getFilename(),
|
2012-08-21 12:29:28 -07:00
|
|
|
duration.ToMilliseconds());
|
2011-12-06 12:12:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)::sqlite3_extended_result_codes(mDBConn, 0);
|
|
|
|
// Drop off the extended result bits of the result code.
|
|
|
|
return srv & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Connection::prepareStatement(const nsCString &aSQL,
|
|
|
|
sqlite3_stmt **_stmt)
|
|
|
|
{
|
|
|
|
bool checkedMainThread = false;
|
|
|
|
|
|
|
|
(void)::sqlite3_extended_result_codes(mDBConn, 1);
|
|
|
|
|
|
|
|
int srv;
|
2013-07-31 08:44:58 -07:00
|
|
|
while((srv = ::sqlite3_prepare_v2(mDBConn,
|
|
|
|
aSQL.get(),
|
|
|
|
-1,
|
|
|
|
_stmt,
|
|
|
|
nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
|
2011-12-06 12:12:55 -08:00
|
|
|
if (!checkedMainThread) {
|
|
|
|
checkedMainThread = true;
|
|
|
|
if (::NS_IsMainThread()) {
|
|
|
|
NS_WARNING("We won't allow blocking on the main thread!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
srv = WaitForUnlockNotify(mDBConn);
|
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
nsCString warnMsg;
|
|
|
|
warnMsg.AppendLiteral("The SQL statement '");
|
|
|
|
warnMsg.Append(aSQL);
|
|
|
|
warnMsg.AppendLiteral("' could not be compiled due to an error: ");
|
|
|
|
warnMsg.Append(::sqlite3_errmsg(mDBConn));
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_WARNING(warnMsg.get());
|
|
|
|
#endif
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PR_LOG(gStorageLog, PR_LOG_ERROR, ("%s", warnMsg.get()));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)::sqlite3_extended_result_codes(mDBConn, 0);
|
|
|
|
// Drop off the extended result bits of the result code.
|
2012-06-07 05:33:13 -07:00
|
|
|
int rc = srv & 0xFF;
|
2013-07-31 08:44:58 -07:00
|
|
|
// sqlite will return OK on a comment only string and set _stmt to nullptr.
|
2012-06-07 05:33:13 -07:00
|
|
|
// The callers of this function are used to only checking the return value,
|
|
|
|
// so it is safer to return an error code.
|
2013-07-31 08:44:58 -07:00
|
|
|
if (rc == SQLITE_OK && *_stmt == nullptr) {
|
2012-06-07 05:33:13 -07:00
|
|
|
return SQLITE_MISUSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2011-12-06 12:12:55 -08:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:26:48 -07:00
|
|
|
|
|
|
|
int
|
|
|
|
Connection::executeSql(const char *aSqlString)
|
|
|
|
{
|
|
|
|
if (!mDBConn)
|
|
|
|
return SQLITE_MISUSE;
|
|
|
|
|
|
|
|
TimeStamp startTime = TimeStamp::Now();
|
2013-07-31 08:44:58 -07:00
|
|
|
int srv = ::sqlite3_exec(mDBConn, aSqlString, nullptr, nullptr, nullptr);
|
2012-03-21 10:26:48 -07:00
|
|
|
|
|
|
|
// Report very slow SQL statements to Telemetry
|
|
|
|
TimeDuration duration = TimeStamp::Now() - startTime;
|
2013-01-22 11:12:17 -08:00
|
|
|
const uint32_t threshold =
|
|
|
|
NS_IsMainThread() ? Telemetry::kSlowSQLThresholdForMainThread
|
|
|
|
: Telemetry::kSlowSQLThresholdForHelperThreads;
|
|
|
|
if (duration.ToMilliseconds() >= threshold) {
|
2012-03-21 10:26:48 -07:00
|
|
|
nsDependentCString statementString(aSqlString);
|
|
|
|
Telemetry::RecordSlowSQLStatement(statementString, getFilename(),
|
2012-08-21 12:29:28 -07:00
|
|
|
duration.ToMilliseconds());
|
2012-03-21 10:26:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return srv;
|
|
|
|
}
|
|
|
|
|
2010-10-05 15:01:22 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIInterfaceRequestor
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Connection::GetInterface(const nsIID &aIID,
|
|
|
|
void **_result)
|
|
|
|
{
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
|
|
|
|
nsIEventTarget *background = getAsyncExecutionTarget();
|
|
|
|
NS_IF_ADDREF(background);
|
|
|
|
*_result = background;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
2009-11-09 09:58:34 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageConnection
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Connection::Close()
|
|
|
|
{
|
|
|
|
if (!mDBConn)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
{ // Make sure we have not executed any asynchronous statements.
|
2012-01-03 00:27:05 -08:00
|
|
|
// If this fails, the mDBConn will be left open, resulting in a leak.
|
|
|
|
// Ideally we'd schedule some code to destroy the mDBConn once all its
|
|
|
|
// async statements have finished executing; see bug 704030.
|
2009-11-09 09:58:34 -08:00
|
|
|
MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
|
2011-08-04 16:15:03 -07:00
|
|
|
bool asyncCloseWasCalled = !mAsyncExecutionThread;
|
|
|
|
NS_ENSURE_TRUE(asyncCloseWasCalled, NS_ERROR_UNEXPECTED);
|
2009-11-09 09:58:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = setClosedState();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return internalClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Connection::AsyncClose(mozIStorageCompletionCallback *aCallback)
|
|
|
|
{
|
2013-06-27 06:00:59 -07:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
2009-11-09 09:58:34 -08:00
|
|
|
if (!mDBConn)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
nsIEventTarget *asyncThread = getAsyncExecutionTarget();
|
2013-08-27 14:07:04 -07:00
|
|
|
NS_ENSURE_TRUE(asyncThread, NS_ERROR_NOT_INITIALIZED);
|
2009-11-09 09:58:34 -08:00
|
|
|
|
|
|
|
nsresult rv = setClosedState();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Create our callback event if we were given a callback.
|
|
|
|
nsCOMPtr<nsIRunnable> completeEvent;
|
|
|
|
if (aCallback) {
|
|
|
|
completeEvent = newCompletionEvent(aCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and dispatch our close event to the background thread.
|
2012-10-18 11:50:39 -07:00
|
|
|
nsCOMPtr<nsIRunnable> closeEvent;
|
|
|
|
{
|
|
|
|
// We need to lock because we're modifying mAsyncExecutionThread
|
|
|
|
MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
|
2013-08-27 14:07:04 -07:00
|
|
|
closeEvent = new AsyncCloseConnection(this,
|
2012-10-18 11:50:39 -07:00
|
|
|
completeEvent,
|
|
|
|
mAsyncExecutionThread.forget());
|
|
|
|
}
|
2009-11-09 09:58:34 -08:00
|
|
|
|
|
|
|
rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-27 12:42:58 -07:00
|
|
|
NS_IMETHODIMP
|
2013-06-27 06:00:59 -07:00
|
|
|
Connection::AsyncClone(bool aReadOnly,
|
|
|
|
mozIStorageCompletionCallback *aCallback)
|
2010-08-27 12:42:58 -07:00
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
PROFILER_LABEL("storage", "Connection::Clone");
|
2013-06-27 06:00:59 -07:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
2010-08-27 12:42:58 -07:00
|
|
|
if (!mDBConn)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (!mDatabaseFile)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
int flags = mFlags;
|
|
|
|
if (aReadOnly) {
|
|
|
|
// Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
|
|
|
|
flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
|
|
|
|
// Turn off SQLITE_OPEN_CREATE.
|
|
|
|
flags = (~SQLITE_OPEN_CREATE & flags);
|
|
|
|
}
|
|
|
|
|
2013-06-27 06:00:59 -07:00
|
|
|
nsRefPtr<Connection> clone = new Connection(mStorageService, flags,
|
|
|
|
mAsyncOnly);
|
|
|
|
|
|
|
|
nsRefPtr<AsyncInitializeClone> initEvent =
|
|
|
|
new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
|
|
|
|
nsCOMPtr<nsIEventTarget> target = clone->getAsyncExecutionTarget();
|
|
|
|
if (!target) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Connection::initializeClone(Connection* aClone, bool aReadOnly)
|
|
|
|
{
|
|
|
|
nsresult rv = mFileURL ? aClone->initialize(mFileURL)
|
|
|
|
: aClone->initialize(mDatabaseFile);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-08-27 12:42:58 -07:00
|
|
|
|
2011-10-03 12:55:03 -07:00
|
|
|
// Copy over pragmas from the original connection.
|
|
|
|
static const char * pragmas[] = {
|
|
|
|
"cache_size",
|
|
|
|
"temp_store",
|
|
|
|
"foreign_keys",
|
|
|
|
"journal_size_limit",
|
|
|
|
"synchronous",
|
|
|
|
"wal_autocheckpoint",
|
|
|
|
};
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < ArrayLength(pragmas); ++i) {
|
2011-10-03 12:55:03 -07:00
|
|
|
// Read-only connections just need cache_size and temp_store pragmas.
|
|
|
|
if (aReadOnly && ::strcmp(pragmas[i], "cache_size") != 0 &&
|
|
|
|
::strcmp(pragmas[i], "temp_store") != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString pragmaQuery("PRAGMA ");
|
2011-10-03 12:55:03 -07:00
|
|
|
pragmaQuery.Append(pragmas[i]);
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
bool hasResult = false;
|
|
|
|
if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
|
|
|
|
pragmaQuery.AppendLiteral(" = ");
|
|
|
|
pragmaQuery.AppendInt(stmt->AsInt32(0));
|
2013-06-27 06:00:59 -07:00
|
|
|
rv = aClone->ExecuteSimpleSQL(pragmaQuery);
|
2011-10-03 12:55:03 -07:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-27 12:42:58 -07:00
|
|
|
// Copy any functions that have been added to this connection.
|
2013-01-26 09:18:16 -08:00
|
|
|
SQLiteMutexAutoLock lockedScope(sharedDBMutex);
|
2013-06-27 06:00:59 -07:00
|
|
|
(void)mFunctions.EnumerateRead(copyFunctionEnumerator, aClone);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Connection::Clone(bool aReadOnly,
|
|
|
|
mozIStorageConnection **_connection)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
|
|
|
|
|
|
|
|
PROFILER_LABEL("storage", "Connection::Clone");
|
|
|
|
if (!mDBConn)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (!mDatabaseFile)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
int flags = mFlags;
|
|
|
|
if (aReadOnly) {
|
|
|
|
// Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
|
|
|
|
flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
|
|
|
|
// Turn off SQLITE_OPEN_CREATE.
|
|
|
|
flags = (~SQLITE_OPEN_CREATE & flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<Connection> clone = new Connection(mStorageService, flags,
|
|
|
|
mAsyncOnly);
|
|
|
|
|
|
|
|
nsresult rv = initializeClone(clone, aReadOnly);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-08-27 12:42:58 -07:00
|
|
|
|
2013-06-27 06:00:59 -07:00
|
|
|
NS_IF_ADDREF(*_connection = clone);
|
2010-08-27 12:42:58 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-11-09 09:58:34 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2013-04-04 17:14:46 -07:00
|
|
|
Connection::GetDefaultPageSize(int32_t *_defaultPageSize)
|
|
|
|
{
|
|
|
|
*_defaultPageSize = Service::getDefaultPageSize();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
Connection::GetConnectionReady(bool *_ready)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-09 07:32:14 -07:00
|
|
|
*_ready = ConnectionReady();
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::GetDatabaseFile(nsIFile **_dbFile)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_IF_ADDREF(*_dbFile = mDatabaseFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Connection::GetLastInsertRowID(int64_t *_id)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
|
|
|
|
*_id = id;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Connection::GetAffectedRows(int32_t *_rows)
|
2011-12-15 23:34:24 -08:00
|
|
|
{
|
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
*_rows = ::sqlite3_changes(mDBConn);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Connection::GetLastError(int32_t *_error)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
*_error = ::sqlite3_errcode(mDBConn);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::GetLastErrorString(nsACString &_errorString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
const char *serr = ::sqlite3_errmsg(mDBConn);
|
|
|
|
_errorString.Assign(serr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-06-29 20:32:09 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Connection::GetSchemaVersion(int32_t *_version)
|
2007-06-29 20:32:09 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-06-29 20:32:09 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
(void)CreateStatement(NS_LITERAL_CSTRING("PRAGMA user_version"),
|
|
|
|
getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
|
2007-06-29 20:32:09 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
*_version = 0;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasResult;
|
2009-04-17 14:19:31 -07:00
|
|
|
if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult)
|
|
|
|
*_version = stmt->AsInt32(0);
|
2007-06-29 20:32:09 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
2007-06-29 20:32:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Connection::SetSchemaVersion(int32_t aVersion)
|
2007-06-29 20:32:09 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString stmt(NS_LITERAL_CSTRING("PRAGMA user_version = "));
|
2009-04-17 14:19:31 -07:00
|
|
|
stmt.AppendInt(aVersion);
|
2007-06-29 20:32:09 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
return ExecuteSimpleSQL(stmt);
|
2007-06-29 20:32:09 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::CreateStatement(const nsACString &aSQLStatement,
|
|
|
|
mozIStorageStatement **_stmt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(_stmt);
|
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
nsRefPtr<Statement> statement(new Statement());
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
nsresult rv = statement->initialize(this, aSQLStatement);
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
Statement *rawPtr;
|
2009-04-17 14:19:31 -07:00
|
|
|
statement.forget(&rawPtr);
|
|
|
|
*_stmt = rawPtr;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Connection::CreateAsyncStatement(const nsACString &aSQLStatement,
|
|
|
|
mozIStorageAsyncStatement **_stmt)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_stmt);
|
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsRefPtr<AsyncStatement> statement(new AsyncStatement());
|
|
|
|
NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
nsresult rv = statement->initialize(this, aSQLStatement);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
AsyncStatement *rawPtr;
|
|
|
|
statement.forget(&rawPtr);
|
|
|
|
*_stmt = rawPtr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::ExecuteSimpleSQL(const nsACString &aSQLStatement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-21 10:26:48 -07:00
|
|
|
int srv = executeSql(PromiseFlatCString(aSQLStatement).get());
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Connection::ExecuteAsync(mozIStorageBaseStatement **aStatements,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aNumStatements,
|
2009-04-17 14:19:31 -07:00
|
|
|
mozIStorageStatementCallback *aCallback,
|
|
|
|
mozIStoragePendingStatement **_handle)
|
|
|
|
{
|
2009-06-17 12:12:51 -07:00
|
|
|
nsTArray<StatementData> stmts(aNumStatements);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aNumStatements; i++) {
|
2010-03-24 00:32:40 -07:00
|
|
|
nsCOMPtr<StorageBaseStatementInternal> stmt =
|
|
|
|
do_QueryInterface(aStatements[i]);
|
2008-10-13 15:45:40 -07:00
|
|
|
|
2009-07-28 10:21:03 -07:00
|
|
|
// Obtain our StatementData.
|
|
|
|
StatementData data;
|
|
|
|
nsresult rv = stmt->getAsynchronousStatementData(data);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-03-20 13:28:16 -07:00
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
NS_ASSERTION(stmt->getOwner() == this,
|
2009-07-28 10:21:03 -07:00
|
|
|
"Statement must be from this database connection!");
|
2008-10-13 15:45:40 -07:00
|
|
|
|
2009-07-28 10:21:03 -07:00
|
|
|
// Now append it to our array.
|
|
|
|
NS_ENSURE_TRUE(stmts.AppendElement(data), NS_ERROR_OUT_OF_MEMORY);
|
2009-07-23 10:34:54 -07:00
|
|
|
}
|
2008-10-13 15:45:40 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
// Dispatch to the background
|
2009-07-28 10:21:03 -07:00
|
|
|
return AsyncExecuteStatements::execute(stmts, this, aCallback, _handle);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::TableExists(const nsACString &aTableName,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *_exists)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
return databaseElementExists(TABLE, aTableName, _exists);
|
2009-02-23 10:05:24 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-02-23 10:05:24 -08:00
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::IndexExists(const nsACString &aIndexName,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* _exists)
|
2009-02-23 10:05:24 -08:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
return databaseElementExists(INDEX, aIndexName, _exists);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
Connection::GetTransactionInProgress(bool *_inProgress)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-22 15:18:33 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
SQLiteMutexAutoLock lockedScope(sharedDBMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
*_inProgress = mTransactionInProgress;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::BeginTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
return BeginTransactionAs(mozIStorageConnection::TRANSACTION_DEFERRED);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Connection::BeginTransactionAs(int32_t aTransactionType)
|
2009-04-17 14:19:31 -07:00
|
|
|
{
|
2009-07-22 15:18:33 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
SQLiteMutexAutoLock lockedScope(sharedDBMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (mTransactionInProgress)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsresult rv;
|
|
|
|
switch(aTransactionType) {
|
|
|
|
case TRANSACTION_DEFERRED:
|
|
|
|
rv = ExecuteSimpleSQL(NS_LITERAL_CSTRING("BEGIN DEFERRED"));
|
|
|
|
break;
|
|
|
|
case TRANSACTION_IMMEDIATE:
|
|
|
|
rv = ExecuteSimpleSQL(NS_LITERAL_CSTRING("BEGIN IMMEDIATE"));
|
|
|
|
break;
|
|
|
|
case TRANSACTION_EXCLUSIVE:
|
|
|
|
rv = ExecuteSimpleSQL(NS_LITERAL_CSTRING("BEGIN EXCLUSIVE"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2011-10-17 07:59:28 -07:00
|
|
|
mTransactionInProgress = true;
|
2009-04-17 14:19:31 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::CommitTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-16 05:31:18 -07:00
|
|
|
if (!mDBConn)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2009-07-22 15:18:33 -07:00
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
SQLiteMutexAutoLock lockedScope(sharedDBMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mTransactionInProgress)
|
2010-04-16 05:31:18 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
nsresult rv = ExecuteSimpleSQL(NS_LITERAL_CSTRING("COMMIT TRANSACTION"));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2011-10-17 07:59:28 -07:00
|
|
|
mTransactionInProgress = false;
|
2009-04-17 14:19:31 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::RollbackTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-16 05:31:18 -07:00
|
|
|
if (!mDBConn)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2009-07-22 15:18:33 -07:00
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
SQLiteMutexAutoLock lockedScope(sharedDBMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mTransactionInProgress)
|
2010-04-16 05:31:18 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
nsresult rv = ExecuteSimpleSQL(NS_LITERAL_CSTRING("ROLLBACK TRANSACTION"));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2011-10-17 07:59:28 -07:00
|
|
|
mTransactionInProgress = false;
|
2009-04-17 14:19:31 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::CreateTable(const char *aTableName,
|
|
|
|
const char *aTableSchema)
|
2007-07-07 14:14:51 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
char *buf = ::PR_smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
|
|
|
|
if (!buf)
|
2007-07-07 14:14:51 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-03-21 10:26:48 -07:00
|
|
|
int srv = executeSql(buf);
|
2009-04-17 14:19:31 -07:00
|
|
|
::PR_smprintf_free(buf);
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-07-07 14:14:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::CreateFunction(const nsACString &aFunctionName,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNumArguments,
|
2009-04-17 14:19:31 -07:00
|
|
|
mozIStorageFunction *aFunction)
|
|
|
|
{
|
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
// Check to see if this function is already defined. We only check the name
|
|
|
|
// because a function can be defined with the same body but different names.
|
2010-03-24 00:32:40 -07:00
|
|
|
SQLiteMutexAutoLock lockedScope(sharedDBMutex);
|
2013-07-31 08:44:58 -07:00
|
|
|
NS_ENSURE_FALSE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
|
2009-04-17 14:19:31 -07:00
|
|
|
|
|
|
|
int srv = ::sqlite3_create_function(mDBConn,
|
|
|
|
nsPromiseFlatCString(aFunctionName).get(),
|
|
|
|
aNumArguments,
|
|
|
|
SQLITE_ANY,
|
|
|
|
aFunction,
|
|
|
|
basicFunctionHelper,
|
2013-07-31 08:44:58 -07:00
|
|
|
nullptr,
|
|
|
|
nullptr);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (srv != SQLITE_OK)
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-27 12:42:58 -07:00
|
|
|
FunctionInfo info = { aFunction,
|
|
|
|
Connection::FunctionInfo::SIMPLE,
|
|
|
|
aNumArguments };
|
2012-05-18 10:30:49 -07:00
|
|
|
mFunctions.Put(aFunctionName, info);
|
2009-04-17 14:19:31 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
2007-07-07 14:14:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::CreateAggregateFunction(const nsACString &aFunctionName,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNumArguments,
|
2009-04-17 14:19:31 -07:00
|
|
|
mozIStorageAggregateFunction *aFunction)
|
|
|
|
{
|
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
// Check to see if this function name is already defined.
|
2010-03-24 00:32:40 -07:00
|
|
|
SQLiteMutexAutoLock lockedScope(sharedDBMutex);
|
2013-07-31 08:44:58 -07:00
|
|
|
NS_ENSURE_FALSE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
|
2009-04-17 14:19:31 -07:00
|
|
|
|
|
|
|
// Because aggregate functions depend on state across calls, you cannot have
|
|
|
|
// the same instance use the same name. We want to enumerate all functions
|
|
|
|
// and make sure this instance is not already registered.
|
|
|
|
NS_ENSURE_FALSE(findFunctionByInstance(aFunction), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
int srv = ::sqlite3_create_function(mDBConn,
|
|
|
|
nsPromiseFlatCString(aFunctionName).get(),
|
|
|
|
aNumArguments,
|
|
|
|
SQLITE_ANY,
|
|
|
|
aFunction,
|
2013-07-31 08:44:58 -07:00
|
|
|
nullptr,
|
2009-04-17 14:19:31 -07:00
|
|
|
aggregateFunctionStepHelper,
|
|
|
|
aggregateFunctionFinalHelper);
|
|
|
|
if (srv != SQLITE_OK)
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2010-08-27 12:42:58 -07:00
|
|
|
FunctionInfo info = { aFunction,
|
|
|
|
Connection::FunctionInfo::AGGREGATE,
|
|
|
|
aNumArguments };
|
2012-05-18 10:30:49 -07:00
|
|
|
mFunctions.Put(aFunctionName, info);
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
2007-07-07 14:14:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::RemoveFunction(const nsACString &aFunctionName)
|
|
|
|
{
|
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
SQLiteMutexAutoLock lockedScope(sharedDBMutex);
|
2013-07-31 08:44:58 -07:00
|
|
|
NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
|
2009-04-17 14:19:31 -07:00
|
|
|
|
|
|
|
int srv = ::sqlite3_create_function(mDBConn,
|
|
|
|
nsPromiseFlatCString(aFunctionName).get(),
|
|
|
|
0,
|
|
|
|
SQLITE_ANY,
|
2013-07-31 08:44:58 -07:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (srv != SQLITE_OK)
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
mFunctions.Remove(aFunctionName);
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
2007-07-07 14:14:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Connection::SetProgressHandler(int32_t aGranularity,
|
2009-04-17 14:19:31 -07:00
|
|
|
mozIStorageProgressHandler *aHandler,
|
|
|
|
mozIStorageProgressHandler **_oldHandler)
|
2007-07-07 14:14:51 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
// Return previous one
|
2010-03-24 00:32:40 -07:00
|
|
|
SQLiteMutexAutoLock lockedScope(sharedDBMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_IF_ADDREF(*_oldHandler = mProgressHandler);
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!aHandler || aGranularity <= 0) {
|
2012-07-30 07:20:58 -07:00
|
|
|
aHandler = nullptr;
|
2009-04-17 14:19:31 -07:00
|
|
|
aGranularity = 0;
|
|
|
|
}
|
|
|
|
mProgressHandler = aHandler;
|
|
|
|
::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
2007-07-07 14:14:51 -07:00
|
|
|
}
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Connection::RemoveProgressHandler(mozIStorageProgressHandler **_oldHandler)
|
2007-07-07 14:14:51 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
// Return previous one
|
2010-03-24 00:32:40 -07:00
|
|
|
SQLiteMutexAutoLock lockedScope(sharedDBMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_IF_ADDREF(*_oldHandler = mProgressHandler);
|
2008-10-28 20:53:19 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mProgressHandler = nullptr;
|
2013-07-31 08:44:58 -07:00
|
|
|
::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
|
2008-10-28 20:53:19 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
return NS_OK;
|
2008-10-28 20:53:19 -07:00
|
|
|
}
|
|
|
|
|
2010-09-01 18:35:46 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Connection::SetGrowthIncrement(int32_t aChunkSize, const nsACString &aDatabaseName)
|
2010-09-01 18:35:46 -07:00
|
|
|
{
|
2010-10-12 16:20:51 -07:00
|
|
|
// Bug 597215: Disk space is extremely limited on Android
|
|
|
|
// so don't preallocate space. This is also not effective
|
|
|
|
// on log structured file systems used by Android devices
|
2010-12-09 10:14:16 -08:00
|
|
|
#if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
|
2011-09-05 02:29:06 -07:00
|
|
|
// Don't preallocate if less than 500MiB is available.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t bytesAvailable;
|
2012-06-05 19:08:30 -07:00
|
|
|
nsresult rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
|
2011-09-05 02:29:06 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
|
|
|
|
return NS_ERROR_FILE_TOO_BIG;
|
|
|
|
}
|
|
|
|
|
2010-09-01 18:35:46 -07:00
|
|
|
(void)::sqlite3_file_control(mDBConn,
|
2013-07-31 08:44:58 -07:00
|
|
|
aDatabaseName.Length() ? nsPromiseFlatCString(aDatabaseName).get()
|
|
|
|
: nullptr,
|
2010-09-01 18:35:46 -07:00
|
|
|
SQLITE_FCNTL_CHUNK_SIZE,
|
|
|
|
&aChunkSize);
|
2010-10-12 16:20:51 -07:00
|
|
|
#endif
|
2010-09-01 18:35:46 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Connection::EnableModule(const nsACString& aModuleName)
|
|
|
|
{
|
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ArrayLength(gModules); i++) {
|
|
|
|
struct Module* m = &gModules[i];
|
|
|
|
if (aModuleName.Equals(m->name)) {
|
|
|
|
int srv = m->registerFunc(mDBConn, m->name);
|
|
|
|
if (srv != SQLITE_OK)
|
|
|
|
return convertResultCode(srv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|