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\:>~ :
|
2007-06-12 16:19:34 -07:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
2007-03-22 10:30:00 -07:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Oracle Corporation code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Oracle Corporation
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2004
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
|
|
|
|
* Brett Wilson <brettw@gmail.com>
|
2007-06-12 16:19:34 -07:00
|
|
|
* Shawn Wilsher <me@shawnwilsher.com>
|
2007-07-07 14:14:51 -07:00
|
|
|
* Lev Serebryakov <lev@serebryakov.spb.ru>
|
2009-07-15 10:49:05 -07:00
|
|
|
* Drew Willcoxon <adw@mozilla.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIMutableArray.h"
|
2007-07-07 14:14:51 -07:00
|
|
|
#include "nsHashSets.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIFile.h"
|
2008-05-23 07:56:10 -07:00
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
2008-10-28 20:53:19 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2009-06-17 12:12:43 -07:00
|
|
|
#include "nsAutoLock.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-07 14:14:51 -07:00
|
|
|
#include "mozIStorageAggregateFunction.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"
|
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"
|
2009-07-15 10:49:05 -07:00
|
|
|
#include "SQLCollations.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* gStorageLog = nsnull;
|
|
|
|
#endif
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
2008-05-23 07:56:10 -07:00
|
|
|
#define PREF_TS_SYNCHRONOUS "toolkit.storage.synchronous"
|
|
|
|
|
2009-06-28 15:59:40 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// sqlite3_context Specialization Functions
|
|
|
|
|
|
|
|
template < >
|
|
|
|
int
|
|
|
|
sqlite3_T_int(sqlite3_context *aCtx,
|
|
|
|
int aValue)
|
|
|
|
{
|
|
|
|
::sqlite3_result_int(aCtx, aValue);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < >
|
|
|
|
int
|
|
|
|
sqlite3_T_int64(sqlite3_context *aCtx,
|
|
|
|
sqlite3_int64 aValue)
|
|
|
|
{
|
|
|
|
::sqlite3_result_int64(aCtx, aValue);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < >
|
|
|
|
int
|
|
|
|
sqlite3_T_double(sqlite3_context *aCtx,
|
|
|
|
double aValue)
|
|
|
|
{
|
|
|
|
::sqlite3_result_double(aCtx, aValue);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < >
|
|
|
|
int
|
|
|
|
sqlite3_T_text16(sqlite3_context *aCtx,
|
|
|
|
nsString aValue)
|
|
|
|
{
|
|
|
|
::sqlite3_result_text16(aCtx,
|
|
|
|
PromiseFlatString(aValue).get(),
|
|
|
|
aValue.Length() * 2, // Number of bytes.
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < >
|
|
|
|
int
|
|
|
|
sqlite3_T_null(sqlite3_context *aCtx)
|
|
|
|
{
|
|
|
|
::sqlite3_result_null(aCtx);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < >
|
|
|
|
int
|
|
|
|
sqlite3_T_blob(sqlite3_context *aCtx,
|
|
|
|
const void *aData,
|
|
|
|
int aSize)
|
|
|
|
{
|
|
|
|
::sqlite3_result_blob(aCtx, aData, aSize, NS_Free);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
namespace {
|
|
|
|
#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,
|
|
|
|
nsISupports *aData,
|
|
|
|
void *aUserArg)
|
|
|
|
{
|
|
|
|
FFEArguments *args = static_cast<FFEArguments *>(aUserArg);
|
|
|
|
if (aData == args->target) {
|
|
|
|
args->found = PR_TRUE;
|
|
|
|
return PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
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 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
|
|
|
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Connection
|
|
|
|
|
2009-07-15 10:49:05 -07:00
|
|
|
Connection::Connection(Service *aService)
|
2009-07-29 17:24:50 -07:00
|
|
|
: mDBConn(nsnull)
|
2009-04-17 14:19:31 -07:00
|
|
|
, mAsyncExecutionMutex(nsAutoLock::NewLock("AsyncExecutionMutex"))
|
|
|
|
, mAsyncExecutionThreadShuttingDown(PR_FALSE)
|
2009-07-23 10:34:54 -07:00
|
|
|
, mTransactionMutex(nsAutoLock::NewLock("TransactionMutex"))
|
2009-04-17 14:19:31 -07:00
|
|
|
, mTransactionInProgress(PR_FALSE)
|
2009-07-23 10:34:54 -07:00
|
|
|
, mFunctionsMutex(nsAutoLock::NewLock("FunctionsMutex"))
|
|
|
|
, mProgressHandlerMutex(nsAutoLock::NewLock("ProgressHandlerMutex"))
|
2009-04-17 14:19:31 -07:00
|
|
|
, mProgressHandler(nsnull)
|
|
|
|
, mStorageService(aService)
|
|
|
|
{
|
|
|
|
mFunctions.Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection::~Connection()
|
|
|
|
{
|
|
|
|
(void)Close();
|
|
|
|
nsAutoLock::DestroyLock(mAsyncExecutionMutex);
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock::DestroyLock(mTransactionMutex);
|
|
|
|
nsAutoLock::DestroyLock(mFunctionsMutex);
|
|
|
|
nsAutoLock::DestroyLock(mProgressHandlerMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(
|
|
|
|
Connection,
|
|
|
|
mozIStorageConnection
|
|
|
|
)
|
|
|
|
|
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
Connection::getAsyncExecutionTarget()
|
|
|
|
{
|
|
|
|
nsAutoLock mutex(mAsyncExecutionMutex);
|
|
|
|
|
|
|
|
// If we are shutting down the asynchronous thread, don't hand out any more
|
|
|
|
// references to the thread.
|
|
|
|
if (mAsyncExecutionThreadShuttingDown)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
if (!mAsyncExecutionThread) {
|
|
|
|
nsresult rv = ::NS_NewThread(getter_AddRefs(mAsyncExecutionThread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create async thread.");
|
|
|
|
return nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-04-17 14:19:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIEventTarget *eventTarget;
|
|
|
|
NS_ADDREF(eventTarget = mAsyncExecutionThread);
|
|
|
|
return eventTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Connection::initialize(nsIFile *aDatabaseFile)
|
|
|
|
{
|
|
|
|
NS_ASSERTION (!mDBConn, "Initialize called on already opened database!");
|
|
|
|
NS_ENSURE_TRUE(mAsyncExecutionMutex, NS_ERROR_OUT_OF_MEMORY);
|
2009-07-23 10:34:54 -07:00
|
|
|
NS_ENSURE_TRUE(mTransactionMutex, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
NS_ENSURE_TRUE(mFunctionsMutex, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
NS_ENSURE_TRUE(mProgressHandlerMutex, NS_ERROR_OUT_OF_MEMORY);
|
2009-04-17 14:19:31 -07:00
|
|
|
|
|
|
|
int srv;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
mDatabaseFile = aDatabaseFile;
|
|
|
|
|
|
|
|
if (aDatabaseFile) {
|
|
|
|
nsAutoString path;
|
|
|
|
rv = aDatabaseFile->GetPath(path);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
srv = ::sqlite3_open(NS_ConvertUTF16toUTF8(path).get(), &mDBConn);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// in memory database requested, sqlite uses a magic file name
|
|
|
|
srv = ::sqlite3_open(":memory:", &mDBConn);
|
|
|
|
}
|
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
mDBConn = nsnull;
|
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
|
|
|
|
|
|
|
#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
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
nsCAutoString leafName(":memory");
|
|
|
|
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
|
|
|
|
|
2009-04-20 07:59:52 -07:00
|
|
|
// Register our built-in SQL functions.
|
2009-07-15 10:49:05 -07:00
|
|
|
srv = registerFunctions(mDBConn);
|
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
::sqlite3_close(mDBConn);
|
|
|
|
mDBConn = nsnull;
|
|
|
|
return convertResultCode(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register our built-in SQL collating sequences.
|
|
|
|
srv = registerCollations(mDBConn, mStorageService);
|
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
::sqlite3_close(mDBConn);
|
2009-04-17 14:19:31 -07:00
|
|
|
mDBConn = nsnull;
|
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
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
// Execute a dummy statement to force the db open, and to verify if it is
|
|
|
|
// valid or not.
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
srv = ::sqlite3_prepare_v2(mDBConn, "SELECT * FROM sqlite_master", -1, &stmt,
|
2008-09-03 09:20:10 -07:00
|
|
|
NULL);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (srv == SQLITE_OK) {
|
|
|
|
srv = ::sqlite3_step(stmt);
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
if (srv == SQLITE_DONE || srv == SQLITE_ROW)
|
|
|
|
srv = SQLITE_OK;
|
|
|
|
::sqlite3_finalize(stmt);
|
|
|
|
}
|
2007-07-07 14:14:51 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
if (srv != SQLITE_OK) {
|
|
|
|
::sqlite3_close(mDBConn);
|
|
|
|
mDBConn = nsnull;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Set the synchronous PRAGMA, according to the pref
|
|
|
|
nsCOMPtr<nsIPrefBranch> pref(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
|
|
|
PRInt32 synchronous = 1; // Default to NORMAL if pref not set
|
|
|
|
if (pref)
|
|
|
|
(void)pref->GetIntPref(PREF_TS_SYNCHRONOUS, &synchronous);
|
|
|
|
|
|
|
|
switch (synchronous) {
|
|
|
|
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,
|
|
|
|
PRBool *_exists)
|
|
|
|
{
|
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsCAutoString query("SELECT name FROM sqlite_master WHERE type = '");
|
|
|
|
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;
|
|
|
|
int srv = ::sqlite3_prepare_v2(mDBConn, query.get(), -1, &stmt, NULL);
|
|
|
|
if (srv != SQLITE_OK)
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2009-04-17 14:19:31 -07:00
|
|
|
|
|
|
|
srv = ::sqlite3_step(stmt);
|
|
|
|
// we just care about the return value from step
|
|
|
|
(void)::sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
if (srv == SQLITE_ROW) {
|
|
|
|
*_exists = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (srv == SQLITE_DONE) {
|
|
|
|
*_exists = PR_FALSE;
|
|
|
|
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)
|
|
|
|
{
|
2009-07-23 10:34:54 -07:00
|
|
|
PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mFunctionsMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
FFEArguments args = { aInstance, false };
|
|
|
|
mFunctions.EnumerateRead(findFunctionEnumerator, &args);
|
|
|
|
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()
|
|
|
|
{
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock mutex(mProgressHandlerMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (mProgressHandler) {
|
|
|
|
PRBool result;
|
|
|
|
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-04-17 14:19:31 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageConnection
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-30 06:43:17 -07:00
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::Close()
|
2007-08-30 06:43:17 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mDBConn)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-08-30 06:43:17 -07:00
|
|
|
|
2008-07-11 12:00:58 -07:00
|
|
|
#ifdef PR_LOGGING
|
2009-04-17 14:19:31 -07:00
|
|
|
nsCAutoString leafName(":memory");
|
|
|
|
if (mDatabaseFile)
|
|
|
|
(void)mDatabaseFile->GetNativeLeafName(leafName);
|
|
|
|
PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Opening connection to '%s'",
|
|
|
|
leafName.get()));
|
2008-07-11 12:00:58 -07:00
|
|
|
#endif
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
// Flag that we are shutting down the async thread, so that
|
|
|
|
// getAsyncExecutionTarget knows not to expose/create the async thread.
|
|
|
|
{
|
|
|
|
nsAutoLock mutex(mAsyncExecutionMutex);
|
|
|
|
mAsyncExecutionThreadShuttingDown = PR_TRUE;
|
|
|
|
}
|
|
|
|
// Shutdown the async thread if it exists. (Because we just set the flag,
|
|
|
|
// we are the only code that is going to be touching this variable from here
|
|
|
|
// on out.)
|
|
|
|
if (mAsyncExecutionThread) {
|
|
|
|
mAsyncExecutionThread->Shutdown();
|
|
|
|
mAsyncExecutionThread = nsnull;
|
|
|
|
}
|
2008-10-28 20:53:19 -07:00
|
|
|
|
2008-12-08 14:14:14 -08:00
|
|
|
#ifdef DEBUG
|
2009-04-17 14:19:31 -07:00
|
|
|
// Notify about any non-finalized statements.
|
|
|
|
sqlite3_stmt *stmt = NULL;
|
|
|
|
while ((stmt = ::sqlite3_next_stmt(mDBConn, stmt))) {
|
|
|
|
char *msg = ::PR_smprintf("SQL statement '%s' was not finalized",
|
|
|
|
::sqlite3_sql(stmt));
|
|
|
|
NS_WARNING(msg);
|
|
|
|
::PR_smprintf_free(msg);
|
|
|
|
}
|
2008-12-08 14:14:14 -08:00
|
|
|
#endif
|
|
|
|
|
2009-07-23 10:34:54 -07:00
|
|
|
{
|
|
|
|
nsAutoLock mutex(mProgressHandlerMutex);
|
|
|
|
if (mProgressHandler)
|
|
|
|
::sqlite3_progress_handler(mDBConn, 0, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
int srv = ::sqlite3_close(mDBConn);
|
2009-06-26 15:03:53 -07:00
|
|
|
NS_ASSERTION(srv == SQLITE_OK,
|
|
|
|
"sqlite3_close failed. There are probably outstanding statements that are listed above!");
|
2007-08-30 06:43:17 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
mDBConn = NULL;
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-08-30 06:43:17 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::GetConnectionReady(PRBool *_ready)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-17 14:19:31 -07:00
|
|
|
*_ready = (mDBConn != nsnull);
|
|
|
|
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
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::GetLastInsertRowID(PRInt64 *_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
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::GetLastError(PRInt32 *_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
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::GetSchemaVersion(PRInt32 *_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;
|
|
|
|
PRBool hasResult;
|
|
|
|
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
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::SetSchemaVersion(PRInt32 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
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
nsCAutoString stmt(NS_LITERAL_CSTRING("PRAGMA user_version = "));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
int srv = ::sqlite3_exec(mDBConn, PromiseFlatCString(aSQLStatement).get(),
|
|
|
|
NULL, NULL, NULL);
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-13 15:45:40 -07:00
|
|
|
nsresult
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::ExecuteAsync(mozIStorageStatement **aStatements,
|
|
|
|
PRUint32 aNumStatements,
|
|
|
|
mozIStorageStatementCallback *aCallback,
|
|
|
|
mozIStoragePendingStatement **_handle)
|
|
|
|
{
|
2009-06-17 12:12:51 -07:00
|
|
|
nsTArray<StatementData> stmts(aNumStatements);
|
2009-07-28 10:21:03 -07:00
|
|
|
for (PRUint32 i = 0; i < aNumStatements; i++) {
|
|
|
|
Statement *stmt = static_cast<Statement *>(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
|
|
|
|
2009-07-28 10:21:03 -07:00
|
|
|
NS_ASSERTION(::sqlite3_db_handle(stmt->nativeStatement()) == mDBConn,
|
|
|
|
"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,
|
|
|
|
PRBool *_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,
|
|
|
|
PRBool* _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
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::GetTransactionInProgress(PRBool *_inProgress)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock mutex(mTransactionMutex);
|
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
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::BeginTransactionAs(PRInt32 aTransactionType)
|
|
|
|
{
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock mutex(mTransactionMutex);
|
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))
|
|
|
|
mTransactionInProgress = PR_TRUE;
|
|
|
|
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
|
|
|
{
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock mutex(mTransactionMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mTransactionInProgress)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsresult rv = ExecuteSimpleSQL(NS_LITERAL_CSTRING("COMMIT TRANSACTION"));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mTransactionInProgress = PR_FALSE;
|
|
|
|
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
|
|
|
{
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock mutex(mTransactionMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
if (!mTransactionInProgress)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsresult rv = ExecuteSimpleSQL(NS_LITERAL_CSTRING("ROLLBACK TRANSACTION"));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mTransactionInProgress = PR_FALSE;
|
|
|
|
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;
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
int srv = ::sqlite3_exec(mDBConn, buf, NULL, NULL, NULL);
|
|
|
|
::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,
|
|
|
|
PRInt32 aNumArguments,
|
|
|
|
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.
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock mutex(mFunctionsMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_ENSURE_FALSE(mFunctions.Get(aFunctionName, NULL), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
int srv = ::sqlite3_create_function(mDBConn,
|
|
|
|
nsPromiseFlatCString(aFunctionName).get(),
|
|
|
|
aNumArguments,
|
|
|
|
SQLITE_ANY,
|
|
|
|
aFunction,
|
|
|
|
basicFunctionHelper,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
if (srv != SQLITE_OK)
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_ENSURE_TRUE(mFunctions.Put(aFunctionName, aFunction),
|
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
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,
|
|
|
|
PRInt32 aNumArguments,
|
|
|
|
mozIStorageAggregateFunction *aFunction)
|
|
|
|
{
|
|
|
|
if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
// Check to see if this function name is already defined.
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock mutex(mFunctionsMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_ENSURE_FALSE(mFunctions.Get(aFunctionName, NULL), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
NULL,
|
|
|
|
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
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_ENSURE_TRUE(mFunctions.Put(aFunctionName, aFunction),
|
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
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;
|
|
|
|
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock mutex(mFunctionsMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, NULL), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
int srv = ::sqlite3_create_function(mDBConn,
|
|
|
|
nsPromiseFlatCString(aFunctionName).get(),
|
|
|
|
0,
|
|
|
|
SQLITE_ANY,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
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
|
2009-04-17 14:19:31 -07:00
|
|
|
Connection::SetProgressHandler(PRInt32 aGranularity,
|
|
|
|
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
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock mutex(mProgressHandlerMutex);
|
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) {
|
|
|
|
aHandler = nsnull;
|
|
|
|
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
|
2009-07-23 10:34:54 -07:00
|
|
|
nsAutoLock mutex(mProgressHandlerMutex);
|
2009-04-17 14:19:31 -07:00
|
|
|
NS_IF_ADDREF(*_oldHandler = mProgressHandler);
|
2008-10-28 20:53:19 -07:00
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
mProgressHandler = nsnull;
|
|
|
|
::sqlite3_progress_handler(mDBConn, 0, NULL, NULL);
|
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
|
|
|
}
|
|
|
|
|
2009-04-17 14:19:31 -07:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|