diff --git a/storage/public/mozIStorageConnection.idl b/storage/public/mozIStorageConnection.idl index 69e0c143ba7..8b6ec5b05c2 100644 --- a/storage/public/mozIStorageConnection.idl +++ b/storage/public/mozIStorageConnection.idl @@ -22,6 +22,7 @@ * Contributor(s): * Vladimir Vukicevic * Brett Wilson + * Shawn Wilsher * * 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 */ diff --git a/storage/src/mozStorageConnection.cpp b/storage/src/mozStorageConnection.cpp index b1b69ee4984..b40d4e498c4 100644 --- a/storage/src/mozStorageConnection.cpp +++ b/storage/src/mozStorageConnection.cpp @@ -217,6 +217,35 @@ mozStorageConnection::GetLastErrorString(nsACString& aLastErrorString) return NS_OK; } +NS_IMETHODIMP +mozStorageConnection::GetSchemaVersion(PRInt32 *version) +{ + NS_ASSERTION(mDBConn, "Connection not initialized"); + + nsCOMPtr 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 **/ diff --git a/storage/test/unit/test_storage_connection.js b/storage/test/unit/test_storage_connection.js index 0baed133a9d..52e7716ee0b 100644 --- a/storage/test/unit/test_storage_connection.js +++ b/storage/test/unit/test_storage_connection.js @@ -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() {