Bug 386366 - Add convenience method for getting the database schema version, patch by Shawn Wilsher <sdwilsh@mozilla.com>, r=sspitzer

This commit is contained in:
philringnalda@gmail.com 2007-06-29 20:32:09 -07:00
parent 0e6b712112
commit c20c3615b6
3 changed files with 69 additions and 2 deletions

View File

@ -22,6 +22,7 @@
* Contributor(s):
* Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
* Brett Wilson <brettw@gmail.com>
* Shawn Wilsher <me@shawnwilsher.com>
*
* 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
@ -50,7 +51,7 @@ interface nsIFile;
* creating prepared statements, executing SQL, and examining database
* errors.
*/
[scriptable, uuid(77015f88-bfc2-4669-b1c3-cc19fb07cd4e)]
[scriptable, uuid(0b3d9b17-6c07-4c39-9ee6-314f9fbc56a6)]
interface mozIStorageConnection : nsISupports {
/*
* Initialization and status
@ -84,6 +85,12 @@ interface mozIStorageConnection : nsISupports {
*/
readonly attribute AUTF8String lastErrorString;
/**
* The schema version of the database. This should not be used until the
* database is ready. The schema will be reported as zero if it is not set.
*/
attribute long schemaVersion;
/*
* Statement creation
*/

View File

@ -217,6 +217,35 @@ mozStorageConnection::GetLastErrorString(nsACString& aLastErrorString)
return NS_OK;
}
NS_IMETHODIMP
mozStorageConnection::GetSchemaVersion(PRInt32 *version)
{
NS_ASSERTION(mDBConn, "Connection not initialized");
nsCOMPtr<mozIStorageStatement> stmt;
nsresult rv = CreateStatement(NS_LITERAL_CSTRING(
"PRAGMA user_version"), getter_AddRefs(stmt));
if (NS_FAILED(rv)) return rv;
*version = 0;
PRBool hasResult;
if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult)
*version = stmt->AsInt32(0);
return NS_OK;
}
NS_IMETHODIMP
mozStorageConnection::SetSchemaVersion(PRInt32 aVersion)
{
NS_ASSERTION(mDBConn, "Connection not initialized");
nsCAutoString stmt(NS_LITERAL_CSTRING("PRAGMA user_version = "));
stmt.AppendInt(aVersion);
return ExecuteSimpleSQL(stmt);
}
/**
** Statements & Queries
**/

View File

@ -142,13 +142,44 @@ function test_rollbackTransaction_no_transaction()
}
}
function test_get_schemaVersion_not_set()
{
do_check_eq(0, getOpenedDatabase().schemaVersion);
}
function test_set_schemaVersion()
{
var msc = getOpenedDatabase();
const version = 1;
msc.schemaVersion = version;
do_check_eq(version, msc.schemaVersion);
}
function test_set_schemaVersion_same()
{
var msc = getOpenedDatabase();
const version = 1;
msc.schemaVersion = version; // should still work ok
do_check_eq(version, msc.schemaVersion);
}
function test_set_schemaVersion_negative()
{
var msc = getOpenedDatabase();
const version = -1;
msc.schemaVersion = version;
do_check_eq(version, msc.schemaVersion);
}
var tests = [test_connectionReady, test_databaseFile,
test_tableExists_not_created, test_indexExists_not_created,
test_createTable_not_created, test_indexExists_created,
test_createTable_already_created, test_lastInsertRowID,
test_transactionInProgress_no, test_transactionInProgress_yes,
test_commitTransaction_no_transaction,
test_rollbackTransaction_no_transaction];
test_rollbackTransaction_no_transaction,
test_get_schemaVersion_not_set, test_set_schemaVersion,
test_set_schemaVersion_same, test_set_schemaVersion_negative];
function run_test()
{