Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Private Member Functions
mozStorageConnection Class Reference

#include <mozStorageConnection.h>

Inheritance diagram for mozStorageConnection:
Inheritance graph
[legend]
Collaboration diagram for mozStorageConnection:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 mozStorageConnection (mozIStorageService *aService)
NS_IMETHOD Initialize (nsIFile *aDatabaseFile)
 Actually creates the connection from the DB.
NS_DECL_ISUPPORTS
NS_DECL_MOZISTORAGECONNECTION
sqlite3
GetNativeConnection ()
mozIStorageStatement createStatement (in AUTF8String aSQLStatement)
 Create a mozIStorageStatement for the given SQL expression.
void executeSimpleSQL (in AUTF8String aSQLStatement)
 Execute a SQL expression, expecting no arguments.
boolean tableExists (in AUTF8String aTableName)
 Check if the given table exists.
boolean indexExists (in AUTF8String aIndexName)
 Check if the given index exists.
void beginTransaction ()
 Begin a new transaction.
void beginTransactionAs (in PRInt32 transactionType)
void commitTransaction ()
 Commits the current transaction.
void rollbackTransaction ()
 Rolls back the current transaction.
void createTable (in string aTableName, in string aTableSchema)
 Create the table with the given name and schema.
void createFunction (in string aFunctionName, in long aNumArguments, in mozIStorageFunction aFunction)
 Create a new SQLite function.
void preload ()
 This is used to preload the database cache.

Public Attributes

readonly attribute boolean connectionReady
 whether the database is open or not
readonly attribute nsIFile databaseFile
 The current database nsIFile.
readonly attribute long long lastInsertRowID
 lastInsertRowID returns the row ID from the last INSERT operation.
readonly attribute long lastError
 The last error SQLite error code.
readonly attribute AUTF8String lastErrorString
 The last SQLite error as a string (in english, straight from the sqlite library).
readonly attribute boolean transactionInProgress
 Returns true if a transaction is active on this connection.
const PRInt32 TRANSACTION_DEFERRED = 0
 Begins a new transaction with the given type.
const PRInt32 TRANSACTION_IMMEDIATE = 1
const PRInt32 TRANSACTION_EXCLUSIVE = 2

Protected Member Functions

void HandleSqliteError (const char *aSqlStatement)
 Other bits.

Protected Attributes

sqlite3mDBConn
nsCOMPtr< nsIFilemDatabaseFile
PRBool mTransactionInProgress
nsCOMPtr< nsIMutableArraymFunctions
nsCOMPtr< mozIStorageServicemStorageService

Private Member Functions

 ~mozStorageConnection ()

Detailed Description

Definition at line 54 of file mozStorageConnection.h.


Constructor & Destructor Documentation

Definition at line 61 of file mozStorageConnection.cpp.

Definition at line 68 of file mozStorageConnection.cpp.

{
    if (mDBConn) {
        int srv = sqlite3_close (mDBConn);
        if (srv != SQLITE_OK) {
            NS_WARNING("sqlite3_close failed.  There are probably outstanding statements!");
        }

        // make sure it really got closed
        ((mozStorageService*)(mStorageService.get()))->FlushAsyncIO();
    }
}

Here is the call graph for this function:


Member Function Documentation

Begin a new transaction.

sqlite default transactions are deferred. If a transaction is active, throws an error.

void mozIStorageConnection::beginTransactionAs ( in PRInt32  transactionType) [inherited]

Commits the current transaction.

If no transaction is active,

Exceptions:
NS_ERROR_STORAGE_NO_TRANSACTION.
void mozIStorageConnection::createFunction ( in string  aFunctionName,
in long  aNumArguments,
in mozIStorageFunction  aFunction 
) [inherited]

Create a new SQLite function.

mozIStorageStatement mozIStorageConnection::createStatement ( in AUTF8String  aSQLStatement) [inherited]

Create a mozIStorageStatement for the given SQL expression.

The expression may use ? to indicate sequential numbered arguments, or :name and $var to indicate named arguments.

Parameters:
aSQLStatementThe SQL statement to execute
Returns:
a new mozIStorageStatement
void mozIStorageConnection::createTable ( in string  aTableName,
in string  aTableSchema 
) [inherited]

Create the table with the given name and schema.

If the table already exists, NS_ERROR_FAILURE is thrown. (XXX at some point in the future it will check if the schema is the same as what is specified, but that doesn't happen currently.)

Parameters:
aTableNamethe table name to be created, consisting of [A-Za-z0-9_], and beginning with a letter.
aTableSchemathe schema of the table; what would normally go between the parens in a CREATE TABLE statement: e.g., "foo INTEGER, bar STRING".
Exceptions:
NS_ERROR_FAILUREif the table already exists or could not be created for any other reason.
void mozIStorageConnection::executeSimpleSQL ( in AUTF8String  aSQLStatement) [inherited]

Execute a SQL expression, expecting no arguments.

Parameters:
aSQLStatementThe SQL statement to execute
NS_DECL_ISUPPORTS NS_DECL_MOZISTORAGECONNECTION sqlite3* mozStorageConnection::GetNativeConnection ( ) [inline]

Definition at line 67 of file mozStorageConnection.h.

{ return mDBConn; }
void mozStorageConnection::HandleSqliteError ( const char *  aSqlStatement) [protected]

Other bits.

Definition at line 509 of file mozStorageConnection.cpp.

{
    // an error just occured!
#ifdef PR_LOGGING
    PR_LOG(gStorageLog, PR_LOG_DEBUG, ("Sqlite error: %d '%s'", sqlite3_errcode(mDBConn), sqlite3_errmsg(mDBConn)));
    if (aSqlStatement)
        PR_LOG(gStorageLog, PR_LOG_DEBUG, ("Statement was: %s", aSqlStatement));
#endif
}

Here is the call graph for this function:

boolean mozIStorageConnection::indexExists ( in AUTF8String  aIndexName) [inherited]

Check if the given index exists.

Parameters:
aIndexNameThe index to check
Returns:
TRUE if the index exists, FALSE otherwise.

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.

Definition at line 114 of file mozStorageConnection.cpp.

{
    NS_ASSERTION (!mDBConn, "Initialize called on already opened database!");

    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;
        return ConvertResultCode(srv);
    }

#ifdef PR_LOGGING
    if (! gStorageLog)
        gStorageLog = PR_NewLogModule("mozStorage");

    sqlite3_trace (mDBConn, tracefunc, nsnull);
#endif

    /* Execute a dummy statement to force the db open, and to verify
     * whether it's valid or not
     */
    sqlite3_stmt *stmt = nsnull;
    nsCString query("SELECT * FROM sqlite_master");
    srv = sqlite3_prepare (mDBConn, query.get(), query.Length(), &stmt, nsnull);
 
    if (srv == SQLITE_OK) {
        srv = sqlite3_step(stmt);
 
        if (srv == SQLITE_DONE || srv == SQLITE_ROW)
            srv = SQLITE_OK;
    } else {
        stmt = nsnull;
    }

    if (stmt != nsnull)
        sqlite3_finalize (stmt);
 
    if (srv != SQLITE_OK) {
        sqlite3_close (mDBConn);
        mDBConn = nsnull;

        // make sure it really got closed
        ((mozStorageService*)(mStorageService.get()))->FlushAsyncIO();

        return ConvertResultCode(srv);
    }

    mFunctions = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
    if (NS_FAILED(rv)) return rv;

    return NS_OK;
}

Here is the call graph for this function:

This is used to preload the database cache.

It loads pages from the start of the database file until the memory cache (specified by "PRAGMA cache_size=") is full or the entire file is read.

The cache MUST be active on the database for this to work. This means that you must have a transaction open on the connection, or have a transaction open on another connection that shares the same pager cache. This cached data will go away when the transaction is closed.

This preload operation can dramatically speed up read operations because the data is loaded as one large block. Normally, pages are read in on demand, which can cause many disk seeks.

Rolls back the current transaction.

If no transaction is active,

Exceptions:
NS_ERROR_STORAGE_NO_TRANSACTION.
boolean mozIStorageConnection::tableExists ( in AUTF8String  aTableName) [inherited]

Check if the given table exists.

Parameters:
aTableNameThe table to check
Returns:
TRUE if table exists, FALSE otherwise.

Member Data Documentation

whether the database is open or not

Definition at line 62 of file mozIStorageConnection.idl.

The current database nsIFile.

Null if the database connection refers to an in-memory database.

Definition at line 68 of file mozIStorageConnection.idl.

The last error SQLite error code.

Definition at line 79 of file mozIStorageConnection.idl.

readonly attribute AUTF8String mozIStorageConnection::lastErrorString [inherited]

The last SQLite error as a string (in english, straight from the sqlite library).

Definition at line 85 of file mozIStorageConnection.idl.

lastInsertRowID returns the row ID from the last INSERT operation.

Definition at line 74 of file mozIStorageConnection.idl.

Definition at line 76 of file mozStorageConnection.h.

Definition at line 75 of file mozStorageConnection.h.

Definition at line 79 of file mozStorageConnection.h.

Definition at line 84 of file mozStorageConnection.h.

Definition at line 77 of file mozStorageConnection.h.

Begins a new transaction with the given type.

Definition at line 144 of file mozIStorageConnection.idl.

Definition at line 146 of file mozIStorageConnection.idl.

Definition at line 145 of file mozIStorageConnection.idl.

Returns true if a transaction is active on this connection.

Definition at line 133 of file mozIStorageConnection.idl.


The documentation for this class was generated from the following files: