Added helper classes for C++ users to deal with transaction and statement

scope. Exposes sqlite's in-memory database capability.

bug 310636
r=bryner
This commit is contained in:
brettw%gmail.com 2005-11-08 02:15:01 +00:00
parent f9275712b2
commit c9d2ed39ae
4 changed files with 73 additions and 16 deletions

View File

@ -57,4 +57,8 @@ XPIDLSRCS = \
mozIStorageValueArray.idl \ mozIStorageValueArray.idl \
$(NULL) $(NULL)
EXPORTS = \
mozStorageHelper.h \
$(NULL)
include $(topsrcdir)/config/rules.mk include $(topsrcdir)/config/rules.mk

View File

@ -54,7 +54,7 @@ interface mozIStorageConnection : nsISupports {
readonly attribute boolean connectionReady; readonly attribute boolean connectionReady;
/** /**
* the current database filename * the current database filename. Empty if the database is in-memory
*/ */
readonly attribute AUTF8String databaseName; readonly attribute AUTF8String databaseName;
@ -113,11 +113,19 @@ interface mozIStorageConnection : nsISupports {
readonly attribute boolean transactionInProgress; readonly attribute boolean transactionInProgress;
/** /**
* Begin a new transaction. If a transaction is active, * Begin a new transaction. sqlite default transactions are deferred.
* @throws NS_ERROR_STORAGE_INVALID_OPERATION. * If a transaction is active, throws an error.
*/ */
void beginTransaction (); void beginTransaction ();
/**
* Begins a new transaction with the given type.
*/
const PRInt32 TRANSACTION_DEFERRED = 0;
const PRInt32 TRANSACTION_IMMEDIATE = 1;
const PRInt32 TRANSACTION_EXCLUSIVE = 2;
void beginTransactionAs (in PRInt32 transactionType);
/** /**
* Commits the current transaction. If no transaction is active, * Commits the current transaction. If no transaction is active,
* @throws NS_ERROR_STORAGE_NO_TRANSACTION. * @throws NS_ERROR_STORAGE_NO_TRANSACTION.

View File

@ -80,24 +80,31 @@ void tracefunc (void *closure, const char *stmt)
} }
#endif #endif
/**
* Actually creates the connection from the DB. Called by mozStorageService.
* You can pass a NULL database file in to get an sqlite in-memory database.
*/
NS_IMETHODIMP NS_IMETHODIMP
mozStorageConnection::Initialize(nsIFile *aDatabaseFile) mozStorageConnection::Initialize(nsIFile *aDatabaseFile)
{ {
NS_ENSURE_ARG_POINTER(aDatabaseFile);
NS_ASSERTION (!mDBConn, "Initialize called on already opened database!"); NS_ASSERTION (!mDBConn, "Initialize called on already opened database!");
int srv; int srv;
nsresult rv; nsresult rv;
rv = aDatabaseFile->GetNativeLeafName(mDatabaseName); if (aDatabaseFile) {
NS_ENSURE_SUCCESS(rv, rv); rv = aDatabaseFile->GetNativeLeafName(mDatabaseName);
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString nativePath; nsCAutoString nativePath;
rv = aDatabaseFile->GetNativePath(nativePath); rv = aDatabaseFile->GetNativePath(nativePath);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
srv = sqlite3_open (nativePath.get(), &mDBConn); srv = sqlite3_open (nativePath.get(), &mDBConn);
} else {
// in memory database requested, sqlite uses a magic file name
srv = sqlite3_open (":memory:", &mDBConn);
}
if (srv != SQLITE_OK) { if (srv != SQLITE_OK) {
mDBConn = nsnull; mDBConn = nsnull;
return NS_ERROR_FAILURE; // XXX error code return NS_ERROR_FAILURE; // XXX error code
@ -252,7 +259,7 @@ mozStorageConnection::TableExists(const nsACString& aSQLStatement, PRBool *_retv
**/ **/
NS_IMETHODIMP NS_IMETHODIMP
mozStorageConnection::GetTransactionInProgress(PRInt32 *_retval) mozStorageConnection::GetTransactionInProgress(PRBool *_retval)
{ {
*_retval = mTransactionInProgress; *_retval = mTransactionInProgress;
return NS_OK; return NS_OK;
@ -270,6 +277,30 @@ mozStorageConnection::BeginTransaction()
return rv; return rv;
} }
NS_IMETHODIMP
mozStorageConnection::BeginTransactionAs(PRInt32 aTransactionType)
{
if (mTransactionInProgress)
return NS_ERROR_FAILURE; // XXX error code
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 NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
mozStorageConnection::CommitTransaction() mozStorageConnection::CommitTransaction()
{ {

View File

@ -38,6 +38,7 @@
#include "mozStorageService.h" #include "mozStorageService.h"
#include "mozStorageConnection.h" #include "mozStorageConnection.h"
#include "plstr.h"
#include "sqlite3.h" #include "sqlite3.h"
@ -67,10 +68,23 @@ mozStorageService::GetProfileStorage(const char *aStorageKey, mozIStorageConnect
nsresult rv; nsresult rv;
nsCOMPtr<nsIFile> storageFile; nsCOMPtr<nsIFile> storageFile;
rv = NS_GetSpecialDirectory(NS_APP_STORAGE_50_FILE, getter_AddRefs(storageFile)); if (PL_strcmp(aStorageKey, "memory") == 0) {
if (NS_FAILED(rv)) { // just fall through with NULL storageFile, this will cause the storage
// teh wtf? // connection to use a memory DB.
return rv; } else if (PL_strcmp(aStorageKey, "profile") == 0) {
rv = NS_GetSpecialDirectory(NS_APP_STORAGE_50_FILE, getter_AddRefs(storageFile));
if (NS_FAILED(rv)) {
// teh wtf?
return rv;
}
nsString filename;
storageFile->GetPath(filename);
nsCString filename8 = NS_ConvertUTF16toUTF8(filename.get());
// fall through to DB initialization
} else {
return NS_ERROR_INVALID_ARG;
} }
mozStorageConnection *msc = new mozStorageConnection(); mozStorageConnection *msc = new mozStorageConnection();