diff --git a/storage/public/Makefile.in b/storage/public/Makefile.in index b669d956721e..6dfcfc69aeeb 100644 --- a/storage/public/Makefile.in +++ b/storage/public/Makefile.in @@ -57,4 +57,8 @@ XPIDLSRCS = \ mozIStorageValueArray.idl \ $(NULL) +EXPORTS = \ + mozStorageHelper.h \ + $(NULL) + include $(topsrcdir)/config/rules.mk diff --git a/storage/public/mozIStorageConnection.idl b/storage/public/mozIStorageConnection.idl index 2957e95be2d3..15c55fa83f6f 100644 --- a/storage/public/mozIStorageConnection.idl +++ b/storage/public/mozIStorageConnection.idl @@ -54,7 +54,7 @@ interface mozIStorageConnection : nsISupports { readonly attribute boolean connectionReady; /** - * the current database filename + * the current database filename. Empty if the database is in-memory */ readonly attribute AUTF8String databaseName; @@ -113,11 +113,19 @@ interface mozIStorageConnection : nsISupports { readonly attribute boolean transactionInProgress; /** - * Begin a new transaction. If a transaction is active, - * @throws NS_ERROR_STORAGE_INVALID_OPERATION. + * Begin a new transaction. sqlite default transactions are deferred. + * If a transaction is active, throws an error. */ 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, * @throws NS_ERROR_STORAGE_NO_TRANSACTION. diff --git a/storage/src/mozStorageConnection.cpp b/storage/src/mozStorageConnection.cpp index 9a11ab5505ae..2a1518f0d2d5 100644 --- a/storage/src/mozStorageConnection.cpp +++ b/storage/src/mozStorageConnection.cpp @@ -80,24 +80,31 @@ void tracefunc (void *closure, const char *stmt) } #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 mozStorageConnection::Initialize(nsIFile *aDatabaseFile) { - NS_ENSURE_ARG_POINTER(aDatabaseFile); - NS_ASSERTION (!mDBConn, "Initialize called on already opened database!"); int srv; nsresult rv; - rv = aDatabaseFile->GetNativeLeafName(mDatabaseName); - NS_ENSURE_SUCCESS(rv, rv); + if (aDatabaseFile) { + rv = aDatabaseFile->GetNativeLeafName(mDatabaseName); + NS_ENSURE_SUCCESS(rv, rv); - nsCAutoString nativePath; - rv = aDatabaseFile->GetNativePath(nativePath); - NS_ENSURE_SUCCESS(rv, rv); + nsCAutoString nativePath; + rv = aDatabaseFile->GetNativePath(nativePath); + 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) { mDBConn = nsnull; return NS_ERROR_FAILURE; // XXX error code @@ -252,7 +259,7 @@ mozStorageConnection::TableExists(const nsACString& aSQLStatement, PRBool *_retv **/ NS_IMETHODIMP -mozStorageConnection::GetTransactionInProgress(PRInt32 *_retval) +mozStorageConnection::GetTransactionInProgress(PRBool *_retval) { *_retval = mTransactionInProgress; return NS_OK; @@ -270,6 +277,30 @@ mozStorageConnection::BeginTransaction() 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 mozStorageConnection::CommitTransaction() { diff --git a/storage/src/mozStorageService.cpp b/storage/src/mozStorageService.cpp index 24e6ef2100e6..77cb5912ed52 100644 --- a/storage/src/mozStorageService.cpp +++ b/storage/src/mozStorageService.cpp @@ -38,6 +38,7 @@ #include "mozStorageService.h" #include "mozStorageConnection.h" +#include "plstr.h" #include "sqlite3.h" @@ -67,10 +68,23 @@ mozStorageService::GetProfileStorage(const char *aStorageKey, mozIStorageConnect nsresult rv; nsCOMPtr storageFile; - rv = NS_GetSpecialDirectory(NS_APP_STORAGE_50_FILE, getter_AddRefs(storageFile)); - if (NS_FAILED(rv)) { - // teh wtf? - return rv; + if (PL_strcmp(aStorageKey, "memory") == 0) { + // just fall through with NULL storageFile, this will cause the storage + // connection to use a memory DB. + } 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();