2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2012-12-14 15:58:45 -08:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
#include "OpenDatabaseHelper.h"
|
|
|
|
|
|
|
|
#include "nsIFile.h"
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
#include "mozilla/dom/quota/AcquireListener.h"
|
|
|
|
#include "mozilla/dom/quota/OriginOrPatternString.h"
|
2012-12-17 11:25:10 -08:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2011-12-07 15:24:20 -08:00
|
|
|
#include "mozilla/storage.h"
|
2011-10-20 09:10:56 -07:00
|
|
|
#include "nsEscape.h"
|
2012-12-17 11:25:10 -08:00
|
|
|
#include "nsNetUtil.h"
|
2011-10-20 09:10:56 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2011-12-07 15:24:20 -08:00
|
|
|
#include "snappy/snappy.h"
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
#include "Client.h"
|
2012-07-01 16:45:59 -07:00
|
|
|
#include "nsIBFCacheEntry.h"
|
2011-12-07 15:24:20 -08:00
|
|
|
#include "IDBEvents.h"
|
|
|
|
#include "IDBFactory.h"
|
|
|
|
#include "IndexedDatabaseManager.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2012-03-12 15:53:18 -07:00
|
|
|
using namespace mozilla;
|
2011-10-20 09:10:56 -07:00
|
|
|
USING_INDEXEDDB_NAMESPACE
|
2012-12-17 11:25:10 -08:00
|
|
|
USING_QUOTA_NAMESPACE
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2011-12-07 15:24:20 -08:00
|
|
|
// If JS_STRUCTURED_CLONE_VERSION changes then we need to update our major
|
|
|
|
// schema version.
|
2012-08-07 00:06:29 -07:00
|
|
|
MOZ_STATIC_ASSERT(JS_STRUCTURED_CLONE_VERSION == 1,
|
|
|
|
"Need to update the major schema version.");
|
2011-12-07 15:24:20 -08:00
|
|
|
|
|
|
|
// Major schema version. Bump for almost everything.
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t kMajorSchemaVersion = 12;
|
2011-12-07 15:24:20 -08:00
|
|
|
|
|
|
|
// Minor schema version. Should almost always be 0 (maybe bump on release
|
|
|
|
// branches if we have to).
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t kMinorSchemaVersion = 0;
|
2011-12-07 15:24:20 -08:00
|
|
|
|
|
|
|
// The schema version we store in the SQLite database is a (signed) 32-bit
|
|
|
|
// integer. The major version is left-shifted 4 bits so the max value is
|
|
|
|
// 0xFFFFFFF. The minor version occupies the lower 4 bits and its max is 0xF.
|
2012-08-07 00:06:29 -07:00
|
|
|
MOZ_STATIC_ASSERT(kMajorSchemaVersion <= 0xFFFFFFF,
|
|
|
|
"Major version needs to fit in 28 bits.");
|
|
|
|
MOZ_STATIC_ASSERT(kMinorSchemaVersion <= 0xF,
|
|
|
|
"Minor version needs to fit in 4 bits.");
|
2011-12-07 15:24:20 -08:00
|
|
|
|
|
|
|
inline
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
|
|
|
MakeSchemaVersion(uint32_t aMajorSchemaVersion,
|
|
|
|
uint32_t aMinorSchemaVersion)
|
2011-12-07 15:24:20 -08:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
return int32_t((aMajorSchemaVersion << 4) + aMinorSchemaVersion);
|
2011-12-07 15:24:20 -08:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const int32_t kSQLiteSchemaVersion = int32_t((kMajorSchemaVersion << 4) +
|
2011-12-07 15:24:20 -08:00
|
|
|
kMinorSchemaVersion);
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
nsresult
|
2011-12-15 23:34:24 -08:00
|
|
|
GetDatabaseFilename(const nsAString& aName,
|
|
|
|
nsAString& aDatabaseFilename)
|
2011-10-20 09:10:56 -07:00
|
|
|
{
|
2011-12-15 23:34:24 -08:00
|
|
|
aDatabaseFilename.AppendInt(HashString(aName));
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
nsCString escapedName;
|
|
|
|
if (!NS_Escape(NS_ConvertUTF16toUTF8(aName), escapedName, url_XPAlphas)) {
|
|
|
|
NS_WARNING("Can't escape database name!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* forwardIter = escapedName.BeginReading();
|
|
|
|
const char* backwardIter = escapedName.EndReading() - 1;
|
|
|
|
|
|
|
|
nsCString substring;
|
|
|
|
while (forwardIter <= backwardIter && substring.Length() < 21) {
|
|
|
|
if (substring.Length() % 2) {
|
|
|
|
substring.Append(*backwardIter--);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
substring.Append(*forwardIter++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
aDatabaseFilename.Append(NS_ConvertASCIItoUTF16(substring));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CreateFileTables(mozIStorageConnection* aDBConn)
|
|
|
|
{
|
|
|
|
// Table `file`
|
|
|
|
nsresult rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE file ("
|
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"refcount INTEGER NOT NULL"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TRIGGER object_data_insert_trigger "
|
|
|
|
"AFTER INSERT ON object_data "
|
|
|
|
"FOR EACH ROW "
|
|
|
|
"WHEN NEW.file_ids IS NOT NULL "
|
|
|
|
"BEGIN "
|
|
|
|
"SELECT update_refcount(NULL, NEW.file_ids); "
|
|
|
|
"END;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TRIGGER object_data_update_trigger "
|
|
|
|
"AFTER UPDATE OF file_ids ON object_data "
|
|
|
|
"FOR EACH ROW "
|
|
|
|
"WHEN OLD.file_ids IS NOT NULL OR NEW.file_ids IS NOT NULL "
|
|
|
|
"BEGIN "
|
|
|
|
"SELECT update_refcount(OLD.file_ids, NEW.file_ids); "
|
|
|
|
"END;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TRIGGER object_data_delete_trigger "
|
|
|
|
"AFTER DELETE ON object_data "
|
|
|
|
"FOR EACH ROW WHEN OLD.file_ids IS NOT NULL "
|
|
|
|
"BEGIN "
|
|
|
|
"SELECT update_refcount(OLD.file_ids, NULL); "
|
|
|
|
"END;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TRIGGER file_update_trigger "
|
|
|
|
"AFTER UPDATE ON file "
|
|
|
|
"FOR EACH ROW WHEN NEW.refcount = 0 "
|
|
|
|
"BEGIN "
|
|
|
|
"DELETE FROM file WHERE id = OLD.id; "
|
|
|
|
"END;"
|
|
|
|
));
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CreateTables(mozIStorageConnection* aDBConn)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(!NS_IsMainThread(),
|
|
|
|
"Creating tables on the main thread!");
|
|
|
|
NS_PRECONDITION(aDBConn, "Passing a null database connection!");
|
|
|
|
|
|
|
|
// Table `database`
|
|
|
|
nsresult rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE database ("
|
|
|
|
"name TEXT NOT NULL, "
|
2011-12-07 15:24:20 -08:00
|
|
|
"version INTEGER NOT NULL DEFAULT 0"
|
2011-10-20 09:10:56 -07:00
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Table `object_store`
|
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE object_store ("
|
2011-11-21 20:18:25 -08:00
|
|
|
"id INTEGER PRIMARY KEY, "
|
2011-12-02 19:46:25 -08:00
|
|
|
"auto_increment INTEGER NOT NULL DEFAULT 0, "
|
2011-12-04 09:39:01 -08:00
|
|
|
"name TEXT NOT NULL, "
|
|
|
|
"key_path TEXT, "
|
2011-10-20 09:10:56 -07:00
|
|
|
"UNIQUE (name)"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Table `object_data`
|
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE object_data ("
|
2011-11-21 20:18:25 -08:00
|
|
|
"id INTEGER PRIMARY KEY, "
|
2011-10-20 09:10:56 -07:00
|
|
|
"object_store_id INTEGER NOT NULL, "
|
2011-12-20 02:58:44 -08:00
|
|
|
"key_value BLOB DEFAULT NULL, "
|
2011-12-15 23:34:24 -08:00
|
|
|
"file_ids TEXT, "
|
2011-12-20 02:59:07 -08:00
|
|
|
"data BLOB NOT NULL, "
|
2011-11-21 20:18:25 -08:00
|
|
|
"UNIQUE (object_store_id, key_value), "
|
2011-10-20 09:10:56 -07:00
|
|
|
"FOREIGN KEY (object_store_id) REFERENCES object_store(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Table `index`
|
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE object_store_index ("
|
2011-12-16 16:40:47 -08:00
|
|
|
"id INTEGER PRIMARY KEY, "
|
2011-10-20 09:10:56 -07:00
|
|
|
"object_store_id INTEGER NOT NULL, "
|
|
|
|
"name TEXT NOT NULL, "
|
|
|
|
"key_path TEXT NOT NULL, "
|
|
|
|
"unique_index INTEGER NOT NULL, "
|
2011-12-16 16:40:47 -08:00
|
|
|
"multientry INTEGER NOT NULL, "
|
2011-10-20 09:10:56 -07:00
|
|
|
"UNIQUE (object_store_id, name), "
|
|
|
|
"FOREIGN KEY (object_store_id) REFERENCES object_store(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Table `index_data`
|
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE index_data ("
|
|
|
|
"index_id INTEGER NOT NULL, "
|
2011-12-20 02:58:44 -08:00
|
|
|
"value BLOB NOT NULL, "
|
|
|
|
"object_data_key BLOB NOT NULL, "
|
2011-11-21 20:18:25 -08:00
|
|
|
"object_data_id INTEGER NOT NULL, "
|
|
|
|
"PRIMARY KEY (index_id, value, object_data_key), "
|
2011-10-20 09:10:56 -07:00
|
|
|
"FOREIGN KEY (index_id) REFERENCES object_store_index(id) ON DELETE "
|
|
|
|
"CASCADE, "
|
|
|
|
"FOREIGN KEY (object_data_id) REFERENCES object_data(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-11-21 20:18:25 -08:00
|
|
|
// Need this to make cascading deletes from object_data and object_store fast.
|
2011-10-20 09:10:56 -07:00
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2011-11-21 20:18:25 -08:00
|
|
|
"CREATE INDEX index_data_object_data_id_index "
|
|
|
|
"ON index_data (object_data_id);"
|
2011-10-20 09:10:56 -07:00
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Table `unique_index_data`
|
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE unique_index_data ("
|
|
|
|
"index_id INTEGER NOT NULL, "
|
2011-12-20 02:58:44 -08:00
|
|
|
"value BLOB NOT NULL, "
|
|
|
|
"object_data_key BLOB NOT NULL, "
|
2011-11-21 20:18:25 -08:00
|
|
|
"object_data_id INTEGER NOT NULL, "
|
|
|
|
"PRIMARY KEY (index_id, value, object_data_key), "
|
2011-10-20 09:10:56 -07:00
|
|
|
"UNIQUE (index_id, value), "
|
|
|
|
"FOREIGN KEY (index_id) REFERENCES object_store_index(id) ON DELETE "
|
|
|
|
"CASCADE "
|
|
|
|
"FOREIGN KEY (object_data_id) REFERENCES object_data(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-11-21 20:18:25 -08:00
|
|
|
// Need this to make cascading deletes from object_data and object_store fast.
|
|
|
|
rv = aDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE INDEX unique_index_data_object_data_id_index "
|
|
|
|
"ON unique_index_data (object_data_id);"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = CreateFileTables(aDBConn);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-07 15:24:20 -08:00
|
|
|
rv = aDBConn->SetSchemaVersion(kSQLiteSchemaVersion);
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
UpgradeSchemaFrom4To5(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// All we changed is the type of the version column, so lets try to
|
|
|
|
// convert that to an integer, and if we fail, set it to 0.
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
rv = aConnection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SELECT name, version, dataVersion "
|
|
|
|
"FROM database"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsString name;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t intVersion;
|
|
|
|
int64_t dataVersion;
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
bool hasResults;
|
|
|
|
rv = stmt->ExecuteStep(&hasResults);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(hasResults, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsString version;
|
|
|
|
rv = stmt->GetString(1, version);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
intVersion = version.ToInteger(&rv, 10);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
intVersion = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = stmt->GetString(0, name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->GetInt64(2, &dataVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE database"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE database ("
|
|
|
|
"name TEXT NOT NULL, "
|
|
|
|
"version INTEGER NOT NULL DEFAULT 0, "
|
|
|
|
"dataVersion INTEGER NOT NULL"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO database (name, version, dataVersion) "
|
|
|
|
"VALUES (:name, :version, :dataVersion)"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
{
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
rv = stmt->BindStringParameter(0, name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->BindInt32Parameter(1, intVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->BindInt64Parameter(2, dataVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2011-11-21 20:18:25 -08:00
|
|
|
rv = aConnection->SetSchemaVersion(5);
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-11-21 20:18:25 -08:00
|
|
|
UpgradeSchemaFrom5To6(mozIStorageConnection* aConnection)
|
2011-10-20 09:10:56 -07:00
|
|
|
{
|
2011-12-07 08:41:02 -08:00
|
|
|
// First, drop all the indexes we're no longer going to use.
|
2011-12-07 15:24:20 -08:00
|
|
|
nsresult rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2011-11-21 20:18:25 -08:00
|
|
|
"DROP INDEX key_index;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP INDEX ai_key_index;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP INDEX value_index;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP INDEX ai_value_index;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Now, reorder the columns of object_data to put the blob data last. We do
|
|
|
|
// this by copying into a temporary table, dropping the original, then copying
|
|
|
|
// back into a newly created table.
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"object_store_id, "
|
|
|
|
"key_value, "
|
|
|
|
"data "
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT id, object_store_id, key_value, data "
|
|
|
|
"FROM object_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE object_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE object_data ("
|
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"object_store_id INTEGER NOT NULL, "
|
|
|
|
"key_value DEFAULT NULL, "
|
|
|
|
"data BLOB NOT NULL, "
|
|
|
|
"UNIQUE (object_store_id, key_value), "
|
|
|
|
"FOREIGN KEY (object_store_id) REFERENCES object_store(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO object_data "
|
|
|
|
"SELECT id, object_store_id, key_value, data "
|
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// We need to add a unique constraint to our ai_object_data table. Copy all
|
|
|
|
// the data out of it using a temporary table as before.
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"object_store_id, "
|
|
|
|
"data "
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT id, object_store_id, data "
|
|
|
|
"FROM ai_object_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE ai_object_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE ai_object_data ("
|
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"object_store_id INTEGER NOT NULL, "
|
|
|
|
"data BLOB NOT NULL, "
|
|
|
|
"UNIQUE (object_store_id, id), "
|
|
|
|
"FOREIGN KEY (object_store_id) REFERENCES object_store(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO ai_object_data "
|
|
|
|
"SELECT id, object_store_id, data "
|
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Fix up the index_data table. We're reordering the columns as well as
|
|
|
|
// changing the primary key from being a simple id to being a composite.
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"index_id, "
|
|
|
|
"value, "
|
|
|
|
"object_data_key, "
|
|
|
|
"object_data_id "
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT index_id, value, object_data_key, object_data_id "
|
|
|
|
"FROM index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE index_data ("
|
|
|
|
"index_id INTEGER NOT NULL, "
|
|
|
|
"value NOT NULL, "
|
|
|
|
"object_data_key NOT NULL, "
|
|
|
|
"object_data_id INTEGER NOT NULL, "
|
|
|
|
"PRIMARY KEY (index_id, value, object_data_key), "
|
|
|
|
"FOREIGN KEY (index_id) REFERENCES object_store_index(id) ON DELETE "
|
|
|
|
"CASCADE, "
|
|
|
|
"FOREIGN KEY (object_data_id) REFERENCES object_data(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT OR IGNORE INTO index_data "
|
|
|
|
"SELECT index_id, value, object_data_key, object_data_id "
|
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE INDEX index_data_object_data_id_index "
|
|
|
|
"ON index_data (object_data_id);"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Fix up the unique_index_data table. We're reordering the columns as well as
|
|
|
|
// changing the primary key from being a simple id to being a composite.
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"index_id, "
|
|
|
|
"value, "
|
|
|
|
"object_data_key, "
|
|
|
|
"object_data_id "
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT index_id, value, object_data_key, object_data_id "
|
|
|
|
"FROM unique_index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE unique_index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE unique_index_data ("
|
|
|
|
"index_id INTEGER NOT NULL, "
|
|
|
|
"value NOT NULL, "
|
|
|
|
"object_data_key NOT NULL, "
|
|
|
|
"object_data_id INTEGER NOT NULL, "
|
|
|
|
"PRIMARY KEY (index_id, value, object_data_key), "
|
|
|
|
"UNIQUE (index_id, value), "
|
|
|
|
"FOREIGN KEY (index_id) REFERENCES object_store_index(id) ON DELETE "
|
|
|
|
"CASCADE "
|
|
|
|
"FOREIGN KEY (object_data_id) REFERENCES object_data(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO unique_index_data "
|
|
|
|
"SELECT index_id, value, object_data_key, object_data_id "
|
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE INDEX unique_index_data_object_data_id_index "
|
|
|
|
"ON unique_index_data (object_data_id);"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Fix up the ai_index_data table. We're reordering the columns as well as
|
|
|
|
// changing the primary key from being a simple id to being a composite.
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"index_id, "
|
|
|
|
"value, "
|
|
|
|
"ai_object_data_id "
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT index_id, value, ai_object_data_id "
|
|
|
|
"FROM ai_index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE ai_index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE ai_index_data ("
|
|
|
|
"index_id INTEGER NOT NULL, "
|
|
|
|
"value NOT NULL, "
|
|
|
|
"ai_object_data_id INTEGER NOT NULL, "
|
|
|
|
"PRIMARY KEY (index_id, value, ai_object_data_id), "
|
|
|
|
"FOREIGN KEY (index_id) REFERENCES object_store_index(id) ON DELETE "
|
|
|
|
"CASCADE, "
|
|
|
|
"FOREIGN KEY (ai_object_data_id) REFERENCES ai_object_data(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT OR IGNORE INTO ai_index_data "
|
|
|
|
"SELECT index_id, value, ai_object_data_id "
|
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE INDEX ai_index_data_ai_object_data_id_index "
|
|
|
|
"ON ai_index_data (ai_object_data_id);"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Fix up the ai_unique_index_data table. We're reordering the columns as well
|
|
|
|
// as changing the primary key from being a simple id to being a composite.
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"index_id, "
|
|
|
|
"value, "
|
|
|
|
"ai_object_data_id "
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2011-11-21 20:18:25 -08:00
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT index_id, value, ai_object_data_id "
|
|
|
|
"FROM ai_unique_index_data;"
|
|
|
|
));
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-11-21 20:18:25 -08:00
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE ai_unique_index_data;"
|
|
|
|
));
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-11-21 20:18:25 -08:00
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE ai_unique_index_data ("
|
|
|
|
"index_id INTEGER NOT NULL, "
|
|
|
|
"value NOT NULL, "
|
|
|
|
"ai_object_data_id INTEGER NOT NULL, "
|
|
|
|
"UNIQUE (index_id, value), "
|
|
|
|
"PRIMARY KEY (index_id, value, ai_object_data_id), "
|
|
|
|
"FOREIGN KEY (index_id) REFERENCES object_store_index(id) ON DELETE "
|
|
|
|
"CASCADE, "
|
|
|
|
"FOREIGN KEY (ai_object_data_id) REFERENCES ai_object_data(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO ai_unique_index_data "
|
|
|
|
"SELECT index_id, value, ai_object_data_id "
|
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE INDEX ai_unique_index_data_ai_object_data_id_index "
|
|
|
|
"ON ai_unique_index_data (ai_object_data_id);"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->SetSchemaVersion(6);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
nsresult
|
|
|
|
UpgradeSchemaFrom6To7(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
nsresult rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"id, "
|
|
|
|
"name, "
|
|
|
|
"key_path, "
|
2011-12-07 15:24:20 -08:00
|
|
|
"auto_increment"
|
2011-12-04 09:39:01 -08:00
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT id, name, key_path, auto_increment "
|
|
|
|
"FROM object_store;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE object_store;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE object_store ("
|
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"auto_increment INTEGER NOT NULL DEFAULT 0, "
|
|
|
|
"name TEXT NOT NULL, "
|
|
|
|
"key_path TEXT, "
|
|
|
|
"UNIQUE (name)"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO object_store "
|
|
|
|
"SELECT id, auto_increment, name, nullif(key_path, '') "
|
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->SetSchemaVersion(7);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
nsresult
|
|
|
|
UpgradeSchemaFrom7To8(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
nsresult rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"id, "
|
|
|
|
"object_store_id, "
|
|
|
|
"name, "
|
|
|
|
"key_path, "
|
|
|
|
"unique_index, "
|
2011-12-07 15:24:20 -08:00
|
|
|
"object_store_autoincrement"
|
2011-12-04 09:39:01 -08:00
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT id, object_store_id, name, key_path, "
|
2011-12-07 15:24:20 -08:00
|
|
|
"unique_index, object_store_autoincrement "
|
2011-12-04 09:39:01 -08:00
|
|
|
"FROM object_store_index;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE object_store_index;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE object_store_index ("
|
|
|
|
"id INTEGER, "
|
|
|
|
"object_store_id INTEGER NOT NULL, "
|
|
|
|
"name TEXT NOT NULL, "
|
|
|
|
"key_path TEXT NOT NULL, "
|
|
|
|
"unique_index INTEGER NOT NULL, "
|
|
|
|
"multientry INTEGER NOT NULL, "
|
|
|
|
"object_store_autoincrement INTERGER NOT NULL, "
|
|
|
|
"PRIMARY KEY (id), "
|
|
|
|
"UNIQUE (object_store_id, name), "
|
|
|
|
"FOREIGN KEY (object_store_id) REFERENCES object_store(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO object_store_index "
|
|
|
|
"SELECT id, object_store_id, name, key_path, "
|
2011-12-07 15:24:20 -08:00
|
|
|
"unique_index, 0, object_store_autoincrement "
|
2011-12-04 09:39:01 -08:00
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->SetSchemaVersion(8);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-07 15:24:20 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-14 21:50:29 -07:00
|
|
|
class CompressDataBlobsFunction MOZ_FINAL : public mozIStorageFunction
|
2011-12-07 15:24:20 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
OnFunctionCall(mozIStorageValueArray* aArguments,
|
|
|
|
nsIVariant** aResult)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t argc;
|
2011-12-07 15:24:20 -08:00
|
|
|
nsresult rv = aArguments->GetNumEntries(&argc);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
NS_WARNING("Don't call me with the wrong number of arguments!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t type;
|
2011-12-07 15:24:20 -08:00
|
|
|
rv = aArguments->GetTypeOfIndex(0, &type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (type != mozIStorageStatement::VALUE_TYPE_BLOB) {
|
|
|
|
NS_WARNING("Don't call me with the wrong type of arguments!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint8_t* uncompressed;
|
|
|
|
uint32_t uncompressedLength;
|
2011-12-07 15:24:20 -08:00
|
|
|
rv = aArguments->GetSharedBlob(0, &uncompressedLength, &uncompressed);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
size_t compressedLength = snappy::MaxCompressedLength(uncompressedLength);
|
|
|
|
nsAutoArrayPtr<char> compressed(new char[compressedLength]);
|
|
|
|
|
|
|
|
snappy::RawCompress(reinterpret_cast<const char*>(uncompressed),
|
|
|
|
uncompressedLength, compressed.get(),
|
|
|
|
&compressedLength);
|
|
|
|
|
|
|
|
std::pair<const void *, int> data(static_cast<void*>(compressed.get()),
|
|
|
|
int(compressedLength));
|
|
|
|
|
|
|
|
// XXX This copies the buffer again... There doesn't appear to be any way to
|
|
|
|
// preallocate space and write directly to a BlobVariant at the moment.
|
|
|
|
nsCOMPtr<nsIVariant> result = new mozilla::storage::BlobVariant(data);
|
|
|
|
|
|
|
|
result.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(CompressDataBlobsFunction, mozIStorageFunction)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
UpgradeSchemaFrom8To9_0(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
// We no longer use the dataVersion column.
|
|
|
|
nsresult rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE database SET dataVersion = 0;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageFunction> compressor = new CompressDataBlobsFunction();
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(compressorName, "compress");
|
|
|
|
|
|
|
|
rv = aConnection->CreateFunction(compressorName, 1, compressor);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Turn off foreign key constraints before we do anything here.
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE object_data SET data = compress(data);"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE ai_object_data SET data = compress(data);"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->RemoveFunction(compressorName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->SetSchemaVersion(MakeSchemaVersion(9, 0));
|
2011-12-04 09:39:01 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-21 20:18:25 -08:00
|
|
|
nsresult
|
2011-12-15 23:34:24 -08:00
|
|
|
UpgradeSchemaFrom9_0To10_0(mozIStorageConnection* aConnection)
|
2011-11-21 20:18:25 -08:00
|
|
|
{
|
2011-12-15 23:34:24 -08:00
|
|
|
nsresult rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE object_data ADD COLUMN file_ids TEXT;"
|
|
|
|
));
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE ai_object_data ADD COLUMN file_ids TEXT;"
|
|
|
|
));
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = CreateFileTables(aConnection);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = aConnection->SetSchemaVersion(MakeSchemaVersion(10, 0));
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
nsresult
|
|
|
|
UpgradeSchemaFrom10_0To11_0(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
nsresult rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"id, "
|
|
|
|
"object_store_id, "
|
|
|
|
"name, "
|
|
|
|
"key_path, "
|
|
|
|
"unique_index, "
|
|
|
|
"multientry"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT id, object_store_id, name, key_path, "
|
|
|
|
"unique_index, multientry "
|
|
|
|
"FROM object_store_index;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE object_store_index;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE object_store_index ("
|
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"object_store_id INTEGER NOT NULL, "
|
|
|
|
"name TEXT NOT NULL, "
|
|
|
|
"key_path TEXT NOT NULL, "
|
|
|
|
"unique_index INTEGER NOT NULL, "
|
|
|
|
"multientry INTEGER NOT NULL, "
|
|
|
|
"UNIQUE (object_store_id, name), "
|
|
|
|
"FOREIGN KEY (object_store_id) REFERENCES object_store(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO object_store_index "
|
|
|
|
"SELECT id, object_store_id, name, key_path, "
|
|
|
|
"unique_index, multientry "
|
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-20 03:23:27 -08:00
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TRIGGER object_data_insert_trigger;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO object_data (object_store_id, key_value, data, file_ids) "
|
|
|
|
"SELECT object_store_id, id, data, file_ids "
|
|
|
|
"FROM ai_object_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-20 03:23:27 -08:00
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TRIGGER object_data_insert_trigger "
|
|
|
|
"AFTER INSERT ON object_data "
|
|
|
|
"FOR EACH ROW "
|
|
|
|
"WHEN NEW.file_ids IS NOT NULL "
|
|
|
|
"BEGIN "
|
|
|
|
"SELECT update_refcount(NULL, NEW.file_ids); "
|
|
|
|
"END;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO index_data (index_id, value, object_data_key, object_data_id) "
|
|
|
|
"SELECT ai_index_data.index_id, ai_index_data.value, ai_index_data.ai_object_data_id, object_data.id "
|
|
|
|
"FROM ai_index_data "
|
|
|
|
"INNER JOIN object_store_index ON "
|
|
|
|
"object_store_index.id = ai_index_data.index_id "
|
|
|
|
"INNER JOIN object_data ON "
|
|
|
|
"object_data.object_store_id = object_store_index.object_store_id AND "
|
|
|
|
"object_data.key_value = ai_index_data.ai_object_data_id;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO unique_index_data (index_id, value, object_data_key, object_data_id) "
|
|
|
|
"SELECT ai_unique_index_data.index_id, ai_unique_index_data.value, ai_unique_index_data.ai_object_data_id, object_data.id "
|
|
|
|
"FROM ai_unique_index_data "
|
|
|
|
"INNER JOIN object_store_index ON "
|
|
|
|
"object_store_index.id = ai_unique_index_data.index_id "
|
|
|
|
"INNER JOIN object_data ON "
|
|
|
|
"object_data.object_store_id = object_store_index.object_store_id AND "
|
|
|
|
"object_data.key_value = ai_unique_index_data.ai_object_data_id;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE object_store "
|
|
|
|
"SET auto_increment = (SELECT max(id) FROM ai_object_data) + 1 "
|
|
|
|
"WHERE auto_increment;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE ai_unique_index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE ai_index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE ai_object_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->SetSchemaVersion(MakeSchemaVersion(11, 0));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-14 21:50:29 -07:00
|
|
|
class EncodeKeysFunction MOZ_FINAL : public mozIStorageFunction
|
2011-12-20 02:58:44 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
OnFunctionCall(mozIStorageValueArray* aArguments,
|
|
|
|
nsIVariant** aResult)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t argc;
|
2011-12-20 02:58:44 -08:00
|
|
|
nsresult rv = aArguments->GetNumEntries(&argc);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
NS_WARNING("Don't call me with the wrong number of arguments!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t type;
|
2011-12-20 02:58:44 -08:00
|
|
|
rv = aArguments->GetTypeOfIndex(0, &type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
Key key;
|
|
|
|
if (type == mozIStorageStatement::VALUE_TYPE_INTEGER) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t intKey;
|
2011-12-20 02:58:44 -08:00
|
|
|
aArguments->GetInt64(0, &intKey);
|
|
|
|
key.SetFromInteger(intKey);
|
|
|
|
}
|
|
|
|
else if (type == mozIStorageStatement::VALUE_TYPE_TEXT) {
|
|
|
|
nsString stringKey;
|
|
|
|
aArguments->GetString(0, stringKey);
|
|
|
|
key.SetFromString(stringKey);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("Don't call me with the wrong type of arguments!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCString& buffer = key.GetBuffer();
|
|
|
|
|
|
|
|
std::pair<const void *, int> data(static_cast<const void*>(buffer.get()),
|
|
|
|
int(buffer.Length()));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIVariant> result = new mozilla::storage::BlobVariant(data);
|
|
|
|
|
|
|
|
result.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(EncodeKeysFunction, mozIStorageFunction)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
UpgradeSchemaFrom11_0To12_0(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_NAMED_LITERAL_CSTRING(encoderName, "encode");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageFunction> encoder = new EncodeKeysFunction();
|
|
|
|
|
|
|
|
nsresult rv = aConnection->CreateFunction(encoderName, 1, encoder);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"object_store_id, "
|
|
|
|
"key_value, "
|
|
|
|
"data, "
|
|
|
|
"file_ids "
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT id, object_store_id, encode(key_value), data, file_ids "
|
|
|
|
"FROM object_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE object_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE object_data ("
|
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"object_store_id INTEGER NOT NULL, "
|
|
|
|
"key_value BLOB DEFAULT NULL, "
|
|
|
|
"file_ids TEXT, "
|
2011-12-20 02:59:07 -08:00
|
|
|
"data BLOB NOT NULL, "
|
2011-12-20 02:58:44 -08:00
|
|
|
"UNIQUE (object_store_id, key_value), "
|
|
|
|
"FOREIGN KEY (object_store_id) REFERENCES object_store(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO object_data "
|
2011-12-20 02:59:07 -08:00
|
|
|
"SELECT id, object_store_id, key_value, file_ids, data "
|
2011-12-20 02:58:44 -08:00
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TRIGGER object_data_insert_trigger "
|
|
|
|
"AFTER INSERT ON object_data "
|
|
|
|
"FOR EACH ROW "
|
|
|
|
"WHEN NEW.file_ids IS NOT NULL "
|
|
|
|
"BEGIN "
|
|
|
|
"SELECT update_refcount(NULL, NEW.file_ids); "
|
|
|
|
"END;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TRIGGER object_data_update_trigger "
|
|
|
|
"AFTER UPDATE OF file_ids ON object_data "
|
|
|
|
"FOR EACH ROW "
|
|
|
|
"WHEN OLD.file_ids IS NOT NULL OR NEW.file_ids IS NOT NULL "
|
|
|
|
"BEGIN "
|
|
|
|
"SELECT update_refcount(OLD.file_ids, NEW.file_ids); "
|
|
|
|
"END;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TRIGGER object_data_delete_trigger "
|
|
|
|
"AFTER DELETE ON object_data "
|
|
|
|
"FOR EACH ROW WHEN OLD.file_ids IS NOT NULL "
|
|
|
|
"BEGIN "
|
|
|
|
"SELECT update_refcount(OLD.file_ids, NULL); "
|
|
|
|
"END;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"index_id, "
|
|
|
|
"value, "
|
|
|
|
"object_data_key, "
|
|
|
|
"object_data_id "
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT index_id, encode(value), encode(object_data_key), object_data_id "
|
|
|
|
"FROM index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE index_data ("
|
|
|
|
"index_id INTEGER NOT NULL, "
|
|
|
|
"value BLOB NOT NULL, "
|
|
|
|
"object_data_key BLOB NOT NULL, "
|
|
|
|
"object_data_id INTEGER NOT NULL, "
|
|
|
|
"PRIMARY KEY (index_id, value, object_data_key), "
|
|
|
|
"FOREIGN KEY (index_id) REFERENCES object_store_index(id) ON DELETE "
|
|
|
|
"CASCADE, "
|
|
|
|
"FOREIGN KEY (object_data_id) REFERENCES object_data(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2011-12-20 02:59:07 -08:00
|
|
|
"INSERT INTO index_data "
|
2011-12-20 02:58:44 -08:00
|
|
|
"SELECT index_id, value, object_data_key, object_data_id "
|
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE INDEX index_data_object_data_id_index "
|
|
|
|
"ON index_data (object_data_id);"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE temp_upgrade ("
|
|
|
|
"index_id, "
|
|
|
|
"value, "
|
|
|
|
"object_data_key, "
|
|
|
|
"object_data_id "
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO temp_upgrade "
|
|
|
|
"SELECT index_id, encode(value), encode(object_data_key), object_data_id "
|
|
|
|
"FROM unique_index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE unique_index_data;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE unique_index_data ("
|
|
|
|
"index_id INTEGER NOT NULL, "
|
|
|
|
"value BLOB NOT NULL, "
|
|
|
|
"object_data_key BLOB NOT NULL, "
|
|
|
|
"object_data_id INTEGER NOT NULL, "
|
|
|
|
"PRIMARY KEY (index_id, value, object_data_key), "
|
|
|
|
"UNIQUE (index_id, value), "
|
|
|
|
"FOREIGN KEY (index_id) REFERENCES object_store_index(id) ON DELETE "
|
|
|
|
"CASCADE "
|
|
|
|
"FOREIGN KEY (object_data_id) REFERENCES object_data(id) ON DELETE "
|
|
|
|
"CASCADE"
|
|
|
|
");"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO unique_index_data "
|
|
|
|
"SELECT index_id, value, object_data_key, object_data_id "
|
|
|
|
"FROM temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE temp_upgrade;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE INDEX unique_index_data_object_data_id_index "
|
|
|
|
"ON unique_index_data (object_data_id);"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->RemoveFunction(encoderName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aConnection->SetSchemaVersion(MakeSchemaVersion(12, 0));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
class VersionChangeEventsRunnable;
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
class SetVersionHelper : public AsyncConnectionHelper,
|
2013-03-26 04:13:17 -07:00
|
|
|
public IDBTransactionListener,
|
|
|
|
public AcquireListener
|
2011-10-20 09:10:56 -07:00
|
|
|
{
|
2011-11-07 16:15:45 -08:00
|
|
|
friend class VersionChangeEventsRunnable;
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
public:
|
|
|
|
SetVersionHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBOpenDBRequest* aRequest,
|
|
|
|
OpenDatabaseHelper* aHelper,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t aRequestedVersion,
|
|
|
|
uint64_t aCurrentVersion)
|
2011-10-20 09:10:56 -07:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest),
|
|
|
|
mOpenRequest(aRequest), mOpenHelper(aHelper),
|
|
|
|
mRequestedVersion(aRequestedVersion),
|
|
|
|
mCurrentVersion(aCurrentVersion)
|
|
|
|
{
|
|
|
|
mTransaction->SetTransactionListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
virtual nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal) MOZ_OVERRIDE;
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
virtual nsresult
|
|
|
|
OnExclusiveAccessAcquired() MOZ_OVERRIDE;
|
|
|
|
|
2011-10-25 05:49:31 -07:00
|
|
|
protected:
|
2012-06-01 10:21:12 -07:00
|
|
|
virtual nsresult Init() MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
MOZ_OVERRIDE;
|
2011-10-25 05:49:31 -07:00
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
// SetVersionHelper never fires an error event at the request. It hands that
|
|
|
|
// responsibility back to the OpenDatabaseHelper
|
2012-06-01 10:21:12 -07:00
|
|
|
virtual void OnError() MOZ_OVERRIDE
|
|
|
|
{ }
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
// Need an upgradeneeded event here.
|
2013-03-09 03:34:29 -08:00
|
|
|
virtual already_AddRefed<nsIDOMEvent> CreateSuccessEvent(
|
|
|
|
mozilla::dom::EventTarget* aOwner) MOZ_OVERRIDE;
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2012-06-26 08:09:25 -07:00
|
|
|
virtual nsresult NotifyTransactionPreComplete(IDBTransaction* aTransaction)
|
|
|
|
MOZ_OVERRIDE;
|
|
|
|
virtual nsresult NotifyTransactionPostComplete(IDBTransaction* aTransaction)
|
|
|
|
MOZ_OVERRIDE;
|
2012-06-01 10:21:12 -07:00
|
|
|
|
|
|
|
virtual ChildProcessSendResult
|
2012-10-05 12:09:33 -07:00
|
|
|
SendResponseToChildProcess(nsresult aResultCode) MOZ_OVERRIDE
|
2012-06-01 10:21:12 -07:00
|
|
|
{
|
|
|
|
return Success_NotSent;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual nsresult UnpackResponseFromParentProcess(
|
|
|
|
const ResponseValue& aResponseValue)
|
|
|
|
MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_NOT_REACHED("Should never get here!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t RequestedVersion() const
|
2011-11-07 16:15:45 -08:00
|
|
|
{
|
|
|
|
return mRequestedVersion;
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
private:
|
|
|
|
// In-params
|
|
|
|
nsRefPtr<IDBOpenDBRequest> mOpenRequest;
|
2011-12-15 23:34:24 -08:00
|
|
|
nsRefPtr<OpenDatabaseHelper> mOpenHelper;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t mRequestedVersion;
|
|
|
|
uint64_t mCurrentVersion;
|
2011-10-20 09:10:56 -07:00
|
|
|
};
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
class DeleteDatabaseHelper : public AsyncConnectionHelper,
|
|
|
|
public AcquireListener
|
2011-11-07 16:15:45 -08:00
|
|
|
{
|
|
|
|
friend class VersionChangeEventsRunnable;
|
|
|
|
public:
|
|
|
|
DeleteDatabaseHelper(IDBOpenDBRequest* aRequest,
|
|
|
|
OpenDatabaseHelper* aHelper,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t aCurrentVersion,
|
2011-11-07 16:15:45 -08:00
|
|
|
const nsAString& aName,
|
|
|
|
const nsACString& aASCIIOrigin)
|
2012-07-30 07:20:58 -07:00
|
|
|
: AsyncConnectionHelper(static_cast<IDBDatabase*>(nullptr), aRequest),
|
2011-11-08 09:51:37 -08:00
|
|
|
mOpenHelper(aHelper), mOpenRequest(aRequest),
|
2011-11-07 16:15:45 -08:00
|
|
|
mCurrentVersion(aCurrentVersion), mName(aName),
|
|
|
|
mASCIIOrigin(aASCIIOrigin)
|
|
|
|
{ }
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
|
|
|
|
2011-11-08 09:51:37 -08:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mOpenHelper = nullptr;
|
|
|
|
mOpenRequest = nullptr;
|
2011-11-08 09:51:37 -08:00
|
|
|
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
virtual nsresult
|
|
|
|
OnExclusiveAccessAcquired() MOZ_OVERRIDE;
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
protected:
|
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
// DeleteDatabaseHelper never fires events at the request. It hands that
|
|
|
|
// responsibility back to the OpenDatabaseHelper
|
|
|
|
void OnError()
|
|
|
|
{
|
|
|
|
mOpenHelper->NotifyDeleteFinished();
|
|
|
|
}
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
nsresult OnSuccess()
|
|
|
|
{
|
|
|
|
return mOpenHelper->NotifyDeleteFinished();
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t RequestedVersion() const
|
2011-11-07 16:15:45 -08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-01 10:21:12 -07:00
|
|
|
|
|
|
|
virtual ChildProcessSendResult
|
2012-10-05 12:09:33 -07:00
|
|
|
SendResponseToChildProcess(nsresult aResultCode) MOZ_OVERRIDE
|
2012-06-01 10:21:12 -07:00
|
|
|
{
|
|
|
|
return Success_NotSent;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual nsresult UnpackResponseFromParentProcess(
|
|
|
|
const ResponseValue& aResponseValue)
|
|
|
|
MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_NOT_REACHED("Should never get here!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
private:
|
|
|
|
// In-params
|
|
|
|
nsRefPtr<OpenDatabaseHelper> mOpenHelper;
|
|
|
|
nsRefPtr<IDBOpenDBRequest> mOpenRequest;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t mCurrentVersion;
|
2011-11-07 16:15:45 -08:00
|
|
|
nsString mName;
|
|
|
|
nsCString mASCIIOrigin;
|
|
|
|
};
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// Responsible for firing "versionchange" events at all live and non-closed
|
|
|
|
// databases, and for firing a "blocked" event at the requesting database if any
|
|
|
|
// databases fail to close.
|
|
|
|
class VersionChangeEventsRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
VersionChangeEventsRunnable(
|
2013-03-26 04:13:17 -07:00
|
|
|
IDBDatabase* aRequestingDatabase,
|
|
|
|
IDBOpenDBRequest* aRequest,
|
|
|
|
nsTArray<nsCOMPtr<nsIOfflineStorage> >& aWaitingDatabases,
|
|
|
|
int64_t aOldVersion,
|
|
|
|
int64_t aNewVersion)
|
2011-11-02 05:53:12 -07:00
|
|
|
: mRequestingDatabase(aRequestingDatabase),
|
|
|
|
mRequest(aRequest),
|
|
|
|
mOldVersion(aOldVersion),
|
|
|
|
mNewVersion(aNewVersion)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aRequestingDatabase, "Null pointer!");
|
|
|
|
NS_ASSERTION(aRequest, "Null pointer!");
|
|
|
|
|
2013-04-01 10:35:13 -07:00
|
|
|
mWaitingDatabases.SwapElements(aWaitingDatabases);
|
2011-11-02 05:53:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
// Fire version change events at all of the databases that are not already
|
|
|
|
// closed. Also kick bfcached documents out of bfcache.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mWaitingDatabases.Length();
|
|
|
|
for (uint32_t index = 0; index < count; index++) {
|
2013-03-26 04:13:17 -07:00
|
|
|
IDBDatabase* database =
|
|
|
|
IDBDatabase::FromStorage(mWaitingDatabases[index]);
|
|
|
|
NS_ASSERTION(database, "This shouldn't be null!");
|
2011-11-02 05:53:12 -07:00
|
|
|
|
|
|
|
if (database->IsClosed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First check if the document the IDBDatabase is part of is bfcached.
|
|
|
|
nsCOMPtr<nsIDocument> ownerDoc = database->GetOwnerDocument();
|
|
|
|
nsIBFCacheEntry* bfCacheEntry;
|
|
|
|
if (ownerDoc && (bfCacheEntry = ownerDoc->GetBFCacheEntry())) {
|
|
|
|
bfCacheEntry->RemoveFromBFCacheSync();
|
|
|
|
NS_ASSERTION(database->IsClosed(),
|
|
|
|
"Kicking doc out of bfcache should have closed database");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-12-20 06:18:03 -08:00
|
|
|
// Next check if it's in the process of being bfcached.
|
|
|
|
nsPIDOMWindow* owner = database->GetOwner();
|
|
|
|
if (owner && owner->IsFrozen()) {
|
|
|
|
// We can't kick the document out of the bfcache because it's not yet
|
|
|
|
// fully in the bfcache. Instead we'll abort everything for the window
|
|
|
|
// and mark it as not-bfcacheable.
|
2013-03-26 04:13:17 -07:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Huh?");
|
|
|
|
quotaManager->AbortCloseStoragesForWindow(owner);
|
2012-12-20 06:18:03 -08:00
|
|
|
|
|
|
|
NS_ASSERTION(database->IsClosed(),
|
2013-03-26 04:13:17 -07:00
|
|
|
"AbortCloseStoragesForWindow should have closed database");
|
2012-12-20 06:18:03 -08:00
|
|
|
ownerDoc->DisallowBFCaching();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// Otherwise fire a versionchange event.
|
|
|
|
nsRefPtr<nsDOMEvent> event =
|
2013-03-09 03:34:29 -08:00
|
|
|
IDBVersionChangeEvent::Create(database, mOldVersion, mNewVersion);
|
2011-11-02 05:53:12 -07:00
|
|
|
NS_ENSURE_TRUE(event, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
database->DispatchEvent(event, &dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check to see if any didn't close. If there are some running still
|
|
|
|
// then fire the blocked event.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < count; index++) {
|
2011-11-02 05:53:12 -07:00
|
|
|
if (!mWaitingDatabases[index]->IsClosed()) {
|
|
|
|
nsRefPtr<nsDOMEvent> event =
|
2013-03-09 03:34:29 -08:00
|
|
|
IDBVersionChangeEvent::CreateBlocked(mRequest,
|
|
|
|
mOldVersion, mNewVersion);
|
2011-11-02 05:53:12 -07:00
|
|
|
NS_ENSURE_TRUE(event, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
mRequest->DispatchEvent(event, &dummy);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
template <class T>
|
|
|
|
static
|
2013-03-26 04:13:17 -07:00
|
|
|
void QueueVersionChange(nsTArray<nsCOMPtr<nsIOfflineStorage> >& aDatabases,
|
2011-11-07 16:15:45 -08:00
|
|
|
void* aClosure);
|
2011-11-02 05:53:12 -07:00
|
|
|
private:
|
|
|
|
nsRefPtr<IDBDatabase> mRequestingDatabase;
|
|
|
|
nsRefPtr<IDBOpenDBRequest> mRequest;
|
2013-03-26 04:13:17 -07:00
|
|
|
nsTArray<nsCOMPtr<nsIOfflineStorage> > mWaitingDatabases;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mOldVersion;
|
|
|
|
int64_t mNewVersion;
|
2011-11-02 05:53:12 -07:00
|
|
|
};
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-01-11 02:14:04 -08:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(OpenDatabaseHelper, nsIRunnable)
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::Init()
|
|
|
|
{
|
2013-03-26 04:13:17 -07:00
|
|
|
mDatabaseId = QuotaManager::GetStorageId(mASCIIOrigin, mName);
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ENSURE_TRUE(mDatabaseId, NS_ERROR_FAILURE);
|
2011-11-02 05:53:12 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::Dispatch(nsIEventTarget* aTarget)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mState == eCreated, "We've already been dispatched?");
|
|
|
|
mState = eDBWork;
|
|
|
|
|
|
|
|
return aTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::RunImmediately()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mState == eCreated, "We've already been dispatched?");
|
|
|
|
NS_ASSERTION(NS_FAILED(mResultCode),
|
|
|
|
"Should only be short-circuiting if we failed!");
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "All hell is about to break lose!");
|
|
|
|
|
|
|
|
mState = eFiringEvents;
|
|
|
|
return this->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::DoDatabaseWork()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
bool correctThread;
|
2013-03-26 04:13:17 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(QuotaManager::Get()->IOThread()->
|
2011-10-20 09:10:56 -07:00
|
|
|
IsOnCurrentThread(&correctThread)) &&
|
|
|
|
correctThread,
|
|
|
|
"Running on the wrong thread!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-25 05:49:31 -07:00
|
|
|
mState = eFiringEvents; // In case we fail somewhere along the line.
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
if (QuotaManager::IsShuttingDown()) {
|
2011-10-20 09:10:56 -07:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2011-12-03 09:18:38 -08:00
|
|
|
NS_ASSERTION(mOpenDBRequest, "This should never be null!");
|
|
|
|
|
2012-01-24 02:03:37 -08:00
|
|
|
// This will be null for non-window contexts.
|
|
|
|
nsPIDOMWindow* window = mOpenDBRequest->GetOwner();
|
|
|
|
|
2011-12-03 09:18:38 -08:00
|
|
|
AutoEnterWindow autoWindow(window);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsCOMPtr<nsIFile> dbDirectory;
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "This should never be null!");
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
nsresult rv =
|
|
|
|
quotaManager->EnsureOriginIsInitialized(mASCIIOrigin,
|
2013-03-31 17:10:27 -07:00
|
|
|
mTrackingQuota,
|
2013-03-26 04:13:17 -07:00
|
|
|
getter_AddRefs(dbDirectory));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
rv = dbDirectory->Append(NS_LITERAL_STRING(IDB_DIRECTORY_NAME));
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
bool exists;
|
|
|
|
rv = dbDirectory->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
rv = dbDirectory->Create(nsIFile::DIRECTORY_TYPE, 0755);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else {
|
|
|
|
bool isDirectory;
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(dbDirectory->IsDirectory(&isDirectory)) &&
|
|
|
|
isDirectory, "Should have caught this earlier!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsAutoString filename;
|
|
|
|
rv = GetDatabaseFilename(mName, filename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2012-08-24 11:51:33 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsCOMPtr<nsIFile> dbFile;
|
|
|
|
rv = dbDirectory->Clone(getter_AddRefs(dbFile));
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = dbFile->Append(filename + NS_LITERAL_STRING(".sqlite"));
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = dbFile->GetPath(mDatabaseFilePath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
nsCOMPtr<nsIFile> fmDirectory;
|
|
|
|
rv = dbDirectory->Clone(getter_AddRefs(fmDirectory));
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = fmDirectory->Append(filename);
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageConnection> connection;
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = CreateDatabaseConnection(dbFile, fmDirectory, mName, mASCIIOrigin,
|
2011-12-15 23:34:24 -08:00
|
|
|
getter_AddRefs(connection));
|
2012-06-29 09:48:34 -07:00
|
|
|
if (NS_FAILED(rv) &&
|
|
|
|
NS_ERROR_GET_MODULE(rv) != NS_ERROR_MODULE_DOM_INDEXEDDB) {
|
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2011-12-07 15:24:20 -08:00
|
|
|
rv = IDBFactory::LoadDatabaseInformation(connection, mDatabaseId,
|
|
|
|
&mCurrentVersion, mObjectStores);
|
2011-12-07 08:41:02 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-12-07 15:24:20 -08:00
|
|
|
if (mForDeletion) {
|
|
|
|
mState = eDeletePending;
|
|
|
|
return NS_OK;
|
2011-12-07 08:41:02 -08:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mObjectStores.Length(); i++) {
|
2011-12-16 16:40:47 -08:00
|
|
|
nsRefPtr<ObjectStoreInfo>& objectStoreInfo = mObjectStores[i];
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < objectStoreInfo->indexes.Length(); j++) {
|
2011-10-20 09:10:56 -07:00
|
|
|
IndexInfo& indexInfo = objectStoreInfo->indexes[j];
|
2013-01-15 04:22:03 -08:00
|
|
|
mLastIndexId = std::max(indexInfo.id, mLastIndexId);
|
2011-10-20 09:10:56 -07:00
|
|
|
}
|
2013-01-15 04:22:03 -08:00
|
|
|
mLastObjectStoreId = std::max(objectStoreInfo->id, mLastObjectStoreId);
|
2011-10-20 09:10:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// See if we need to do a VERSION_CHANGE transaction
|
2011-11-02 05:53:12 -07:00
|
|
|
|
|
|
|
// Optional version semantics.
|
|
|
|
if (!mRequestedVersion) {
|
|
|
|
// If the requested version was not specified and the database was created,
|
|
|
|
// treat it as if version 1 were requested.
|
|
|
|
if (mCurrentVersion == 0) {
|
|
|
|
mRequestedVersion = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Otherwise, treat it as if the current version were requested.
|
|
|
|
mRequestedVersion = mCurrentVersion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
if (mCurrentVersion > mRequestedVersion) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_VERSION_ERR;
|
|
|
|
}
|
|
|
|
|
2011-10-25 05:49:31 -07:00
|
|
|
if (mCurrentVersion != mRequestedVersion) {
|
|
|
|
mState = eSetVersionPending;
|
|
|
|
}
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(mgr, "This should never be null!");
|
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
nsRefPtr<FileManager> fileManager = mgr->GetFileManager(mASCIIOrigin, mName);
|
|
|
|
if (!fileManager) {
|
2012-12-17 11:25:10 -08:00
|
|
|
fileManager = new FileManager(mASCIIOrigin, mPrivilege, mName);
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = fileManager->Init(fmDirectory, connection);
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
mgr->AddFileManager(fileManager);
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
|
|
|
|
2012-08-24 11:51:33 -07:00
|
|
|
mFileManager = fileManager.forget();
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::CreateDatabaseConnection(
|
|
|
|
nsIFile* aDBFile,
|
2012-12-17 11:25:10 -08:00
|
|
|
nsIFile* aFMDirectory,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsACString& aOrigin,
|
2011-12-15 23:34:24 -08:00
|
|
|
mozIStorageConnection** aConnection)
|
|
|
|
{
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
nsCOMPtr<nsIFileURL> dbFileUrl =
|
|
|
|
IDBFactory::GetDatabaseFileURL(aDBFile, aOrigin);
|
|
|
|
NS_ENSURE_TRUE(dbFileUrl, NS_ERROR_FAILURE);
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
nsCOMPtr<mozIStorageService> ss =
|
2011-12-15 23:34:24 -08:00
|
|
|
do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(ss, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageConnection> connection;
|
2012-12-17 11:25:10 -08:00
|
|
|
nsresult rv =
|
|
|
|
ss->OpenDatabaseWithFileURL(dbFileUrl, getter_AddRefs(connection));
|
2011-12-15 23:34:24 -08:00
|
|
|
if (rv == NS_ERROR_FILE_CORRUPTED) {
|
|
|
|
// If we're just opening the database during origin initialization, then
|
|
|
|
// we don't want to erase any files. The failure here will fail origin
|
|
|
|
// initialization too.
|
|
|
|
if (aName.IsVoid()) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nuke the database file. The web services can recreate their data.
|
|
|
|
rv = aDBFile->Remove(false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool exists;
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = aFMDirectory->Exists(&exists);
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
bool isDirectory;
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = aFMDirectory->IsDirectory(&isDirectory);
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(isDirectory, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = aFMDirectory->Remove(true);
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = ss->OpenDatabaseWithFileURL(dbFileUrl, getter_AddRefs(connection));
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = connection->EnableModule(NS_LITERAL_CSTRING("filesystem"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Check to make sure that the database schema is correct.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t schemaVersion;
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = connection->GetSchemaVersion(&schemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Unknown schema will fail origin initialization too
|
|
|
|
if (!schemaVersion && aName.IsVoid()) {
|
|
|
|
NS_WARNING("Unable to open IndexedDB database, schema is not set!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schemaVersion > kSQLiteSchemaVersion) {
|
|
|
|
NS_WARNING("Unable to open IndexedDB database, schema is too high!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool vacuumNeeded = false;
|
|
|
|
|
|
|
|
if (schemaVersion != kSQLiteSchemaVersion) {
|
|
|
|
mozStorageTransaction transaction(connection, false,
|
|
|
|
mozIStorageConnection::TRANSACTION_IMMEDIATE);
|
|
|
|
|
|
|
|
if (!schemaVersion) {
|
|
|
|
// Brand new file, initialize our tables.
|
|
|
|
rv = CreateTables(connection);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(connection->GetSchemaVersion(&schemaVersion)) &&
|
|
|
|
schemaVersion == kSQLiteSchemaVersion,
|
|
|
|
"CreateTables set a bad schema version!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = connection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO database (name) "
|
|
|
|
"VALUES (:name)"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("name"), aName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// This logic needs to change next time we change the schema!
|
2012-08-22 08:56:38 -07:00
|
|
|
MOZ_STATIC_ASSERT(kSQLiteSchemaVersion == int32_t((12 << 4) + 0),
|
2012-08-07 00:06:29 -07:00
|
|
|
"Need upgrade code from schema version increase.");
|
2011-12-15 23:34:24 -08:00
|
|
|
|
|
|
|
while (schemaVersion != kSQLiteSchemaVersion) {
|
|
|
|
if (schemaVersion == 4) {
|
|
|
|
rv = UpgradeSchemaFrom4To5(connection);
|
|
|
|
}
|
|
|
|
else if (schemaVersion == 5) {
|
|
|
|
rv = UpgradeSchemaFrom5To6(connection);
|
|
|
|
}
|
|
|
|
else if (schemaVersion == 6) {
|
|
|
|
rv = UpgradeSchemaFrom6To7(connection);
|
|
|
|
}
|
|
|
|
else if (schemaVersion == 7) {
|
|
|
|
rv = UpgradeSchemaFrom7To8(connection);
|
|
|
|
}
|
|
|
|
else if (schemaVersion == 8) {
|
|
|
|
rv = UpgradeSchemaFrom8To9_0(connection);
|
|
|
|
vacuumNeeded = true;
|
|
|
|
}
|
|
|
|
else if (schemaVersion == MakeSchemaVersion(9, 0)) {
|
|
|
|
rv = UpgradeSchemaFrom9_0To10_0(connection);
|
|
|
|
}
|
2011-12-16 16:40:47 -08:00
|
|
|
else if (schemaVersion == MakeSchemaVersion(10, 0)) {
|
|
|
|
rv = UpgradeSchemaFrom10_0To11_0(connection);
|
|
|
|
}
|
2011-12-20 02:58:44 -08:00
|
|
|
else if (schemaVersion == MakeSchemaVersion(11, 0)) {
|
|
|
|
rv = UpgradeSchemaFrom11_0To12_0(connection);
|
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
else {
|
|
|
|
NS_WARNING("Unable to open IndexedDB database, no upgrade path is "
|
|
|
|
"available!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = connection->GetSchemaVersion(&schemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(schemaVersion == kSQLiteSchemaVersion, "Huh?!");
|
|
|
|
}
|
|
|
|
|
2012-06-29 09:48:34 -07:00
|
|
|
rv = transaction.Commit();
|
|
|
|
if (rv == NS_ERROR_FILE_NO_DEVICE_SPACE) {
|
|
|
|
// mozstorage translates SQLITE_FULL to NS_ERROR_FILE_NO_DEVICE_SPACE,
|
|
|
|
// which we know better as NS_ERROR_DOM_INDEXEDDB_QUOTA_ERR.
|
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_QUOTA_ERR;
|
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vacuumNeeded) {
|
|
|
|
rv = connection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"VACUUM;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn on foreign key constraints.
|
|
|
|
rv = connection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"PRAGMA foreign_keys = ON;"
|
|
|
|
));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
connection.forget(aConnection);
|
2011-10-20 09:10:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::StartSetVersion()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mState == eSetVersionPending, "Why are we here?");
|
|
|
|
|
|
|
|
// In case we fail, fire error events
|
|
|
|
mState = eFiringEvents;
|
|
|
|
|
|
|
|
nsresult rv = EnsureSuccessResult();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsTArray<nsString> storesToOpen;
|
|
|
|
nsRefPtr<IDBTransaction> transaction =
|
|
|
|
IDBTransaction::Create(mDatabase, storesToOpen,
|
2011-11-08 09:43:00 -08:00
|
|
|
IDBTransaction::VERSION_CHANGE, true);
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_TRUE(transaction, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsRefPtr<SetVersionHelper> helper =
|
|
|
|
new SetVersionHelper(transaction, mOpenDBRequest, this, mRequestedVersion,
|
|
|
|
mCurrentVersion);
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "This should never be null!");
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
rv = quotaManager->AcquireExclusiveAccess(
|
|
|
|
mDatabase, mDatabase->Origin(), helper,
|
2012-06-19 18:50:39 -07:00
|
|
|
&VersionChangeEventsRunnable::QueueVersionChange<SetVersionHelper>,
|
2013-03-26 04:13:17 -07:00
|
|
|
helper);
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
// The SetVersionHelper is responsible for dispatching us back to the
|
|
|
|
// main thread again and changing the state to eSetVersionCompleted.
|
|
|
|
mState = eSetVersionPending;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::StartDelete()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mState == eDeletePending, "Why are we here?");
|
|
|
|
|
|
|
|
// In case we fail, fire error events
|
|
|
|
mState = eFiringEvents;
|
|
|
|
|
|
|
|
nsresult rv = EnsureSuccessResult();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<DeleteDatabaseHelper> helper =
|
|
|
|
new DeleteDatabaseHelper(mOpenDBRequest, this, mCurrentVersion, mName,
|
|
|
|
mASCIIOrigin);
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "This should never be null!");
|
2011-11-07 16:15:45 -08:00
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
rv = quotaManager->AcquireExclusiveAccess(
|
|
|
|
mDatabase, mDatabase->Origin(), helper,
|
2012-06-19 18:50:39 -07:00
|
|
|
&VersionChangeEventsRunnable::QueueVersionChange<DeleteDatabaseHelper>,
|
2013-03-26 04:13:17 -07:00
|
|
|
helper);
|
2011-11-07 16:15:45 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
// The DeleteDatabaseHelper is responsible for dispatching us back to the
|
|
|
|
// main thread again and changing the state to eDeleteCompleted.
|
|
|
|
mState = eDeletePending;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
OpenDatabaseHelper::Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mState != eCreated, "Dispatch was not called?!?");
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
// If we need to queue up a SetVersionHelper, do that here.
|
|
|
|
if (mState == eSetVersionPending) {
|
|
|
|
nsresult rv = StartSetVersion();
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetError(rv);
|
|
|
|
// fall through and run the default error processing
|
|
|
|
}
|
2011-11-07 16:15:45 -08:00
|
|
|
else if (mState == eDeletePending) {
|
|
|
|
nsresult rv = StartDelete();
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetError(rv);
|
|
|
|
// fall through and run the default error processing
|
|
|
|
}
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
// We've done whatever work we need to do on the DB thread, and any
|
2011-11-07 16:15:45 -08:00
|
|
|
// SetVersion/DeleteDatabase stuff is done by now.
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ASSERTION(mState == eFiringEvents ||
|
2011-11-07 16:15:45 -08:00
|
|
|
mState == eSetVersionCompleted ||
|
|
|
|
mState == eDeleteCompleted, "Why are we here?");
|
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case eSetVersionCompleted: {
|
|
|
|
mState = eFiringEvents;
|
|
|
|
break;
|
|
|
|
}
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
case eDeleteCompleted: {
|
|
|
|
// Destroy the database now (we should have the only ref).
|
2012-07-30 07:20:58 -07:00
|
|
|
mDatabase = nullptr;
|
2011-10-25 05:49:31 -07:00
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
DatabaseInfo::Remove(mDatabaseId);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(mgr, "This should never fail!");
|
|
|
|
|
|
|
|
mgr->InvalidateFileManager(mASCIIOrigin, mName);
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
mState = eFiringEvents;
|
|
|
|
break;
|
2011-10-20 09:10:56 -07:00
|
|
|
}
|
2011-11-07 16:15:45 -08:00
|
|
|
|
|
|
|
case eFiringEvents: {
|
|
|
|
// Notify the request that we're done, but only if we didn't just
|
|
|
|
// finish a [SetVersion/DeleteDatabase]Helper. In that case, the
|
|
|
|
// helper tells the request that it is done, and we avoid calling
|
|
|
|
// NotifyHelperCompleted twice.
|
|
|
|
|
|
|
|
nsresult rv = mOpenDBRequest->NotifyHelperCompleted(this);
|
|
|
|
if (NS_SUCCEEDED(mResultCode) && NS_FAILED(rv)) {
|
|
|
|
mResultCode = rv;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Shouldn't get here!");
|
2011-10-20 09:10:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mState == eFiringEvents, "Why are we here?");
|
|
|
|
|
|
|
|
if (NS_FAILED(mResultCode)) {
|
|
|
|
DispatchErrorEvent();
|
|
|
|
} else {
|
|
|
|
DispatchSuccessEvent();
|
|
|
|
}
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "This should never be null!");
|
2011-11-02 05:53:12 -07:00
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
quotaManager->AllowNextSynchronizedOp(
|
2012-10-23 09:31:19 -07:00
|
|
|
OriginOrPatternString::FromOrigin(mASCIIOrigin),
|
|
|
|
mDatabaseId);
|
2011-11-02 05:53:12 -07:00
|
|
|
|
2011-10-22 04:56:19 -07:00
|
|
|
ReleaseMainThreadObjects();
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
// If we're on the DB thread, do that
|
|
|
|
NS_ASSERTION(mState == eDBWork, "Why are we here?");
|
|
|
|
mResultCode = DoDatabaseWork();
|
|
|
|
NS_ASSERTION(mState != eDBWork, "We should be doing something else now.");
|
|
|
|
|
|
|
|
return NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::EnsureSuccessResult()
|
|
|
|
{
|
2011-11-07 16:15:45 -08:00
|
|
|
nsRefPtr<DatabaseInfo> dbInfo;
|
|
|
|
if (DatabaseInfo::Get(mDatabaseId, getter_AddRefs(dbInfo))) {
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
NS_ASSERTION(dbInfo->name == mName &&
|
|
|
|
dbInfo->version == mCurrentVersion &&
|
|
|
|
dbInfo->id == mDatabaseId &&
|
|
|
|
dbInfo->filePath == mDatabaseFilePath,
|
|
|
|
"Metadata mismatch!");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t objectStoreCount = mObjectStores.Length();
|
|
|
|
for (uint32_t index = 0; index < objectStoreCount; index++) {
|
2011-12-16 16:40:47 -08:00
|
|
|
nsRefPtr<ObjectStoreInfo>& info = mObjectStores[index];
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
ObjectStoreInfo* otherInfo = dbInfo->GetObjectStore(info->name);
|
|
|
|
NS_ASSERTION(otherInfo, "ObjectStore not known!");
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(info->name == otherInfo->name &&
|
|
|
|
info->id == otherInfo->id &&
|
2011-12-16 16:40:47 -08:00
|
|
|
info->keyPath == otherInfo->keyPath,
|
2011-10-20 09:10:56 -07:00
|
|
|
"Metadata mismatch!");
|
|
|
|
NS_ASSERTION(dbInfo->ContainsStoreName(info->name),
|
|
|
|
"Object store names out of date!");
|
|
|
|
NS_ASSERTION(info->indexes.Length() == otherInfo->indexes.Length(),
|
|
|
|
"Bad index length!");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t indexCount = info->indexes.Length();
|
|
|
|
for (uint32_t indexIndex = 0; indexIndex < indexCount; indexIndex++) {
|
2011-10-20 09:10:56 -07:00
|
|
|
const IndexInfo& indexInfo = info->indexes[indexIndex];
|
|
|
|
const IndexInfo& otherIndexInfo = otherInfo->indexes[indexIndex];
|
|
|
|
NS_ASSERTION(indexInfo.id == otherIndexInfo.id,
|
|
|
|
"Bad index id!");
|
|
|
|
NS_ASSERTION(indexInfo.name == otherIndexInfo.name,
|
|
|
|
"Bad index name!");
|
|
|
|
NS_ASSERTION(indexInfo.keyPath == otherIndexInfo.keyPath,
|
|
|
|
"Bad index keyPath!");
|
|
|
|
NS_ASSERTION(indexInfo.unique == otherIndexInfo.unique,
|
|
|
|
"Bad index unique value!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2011-11-07 16:06:13 -08:00
|
|
|
nsRefPtr<DatabaseInfo> newInfo(new DatabaseInfo());
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
newInfo->name = mName;
|
2011-12-16 16:40:47 -08:00
|
|
|
newInfo->origin = mASCIIOrigin;
|
2011-10-20 09:10:56 -07:00
|
|
|
newInfo->id = mDatabaseId;
|
|
|
|
newInfo->filePath = mDatabaseFilePath;
|
|
|
|
|
|
|
|
if (!DatabaseInfo::Put(newInfo)) {
|
|
|
|
NS_ERROR("Failed to add to hash!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
newInfo.swap(dbInfo);
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2011-12-16 16:40:47 -08:00
|
|
|
nsresult rv = IDBFactory::SetDatabaseMetadata(dbInfo, mCurrentVersion,
|
|
|
|
mObjectStores);
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
NS_ASSERTION(mObjectStores.IsEmpty(), "Should have swapped!");
|
|
|
|
}
|
|
|
|
|
|
|
|
dbInfo->nextObjectStoreId = mLastObjectStoreId + 1;
|
|
|
|
dbInfo->nextIndexId = mLastIndexId + 1;
|
|
|
|
|
|
|
|
nsRefPtr<IDBDatabase> database =
|
2012-08-29 17:50:28 -07:00
|
|
|
IDBDatabase::Create(mOpenDBRequest, mOpenDBRequest->Factory(),
|
|
|
|
dbInfo.forget(), mASCIIOrigin, mFileManager,
|
|
|
|
mContentParent);
|
2011-10-20 09:10:56 -07:00
|
|
|
if (!database) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mDatabase, "Shouldn't have a database yet!");
|
|
|
|
mDatabase.swap(database);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
|
|
|
{
|
|
|
|
// Be careful not to load the database twice.
|
|
|
|
if (!mDatabase) {
|
|
|
|
nsresult rv = EnsureSuccessResult();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return WrapNative(aCx, NS_ISUPPORTS_CAST(nsIDOMEventTarget*, mDatabase),
|
|
|
|
aVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::NotifySetVersionFinished()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread");
|
|
|
|
NS_ASSERTION(mState = eSetVersionPending, "How did we get here?");
|
|
|
|
|
2012-06-26 08:09:25 -07:00
|
|
|
// Allow transaction creation to proceed.
|
|
|
|
mDatabase->ExitSetVersionTransaction();
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
mState = eSetVersionCompleted;
|
2012-06-19 18:50:39 -07:00
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
// Dispatch ourself back to the main thread
|
|
|
|
return NS_DispatchToCurrentThread(this);
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
nsresult
|
|
|
|
OpenDatabaseHelper::NotifyDeleteFinished()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread");
|
|
|
|
NS_ASSERTION(mState == eDeletePending, "How did we get here?");
|
|
|
|
|
|
|
|
mState = eDeleteCompleted;
|
|
|
|
|
|
|
|
// Dispatch ourself back to the main thread
|
|
|
|
return NS_DispatchToCurrentThread(this);
|
|
|
|
}
|
|
|
|
|
2011-10-25 05:49:31 -07:00
|
|
|
void
|
|
|
|
OpenDatabaseHelper::BlockDatabase()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(mDatabase, "This is going bad fast.");
|
|
|
|
|
|
|
|
mDatabase->EnterSetVersionTransaction();
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
void
|
|
|
|
OpenDatabaseHelper::DispatchSuccessEvent()
|
|
|
|
{
|
2013-03-09 03:34:29 -08:00
|
|
|
nsRefPtr<nsIDOMEvent> event =
|
|
|
|
CreateGenericEvent(mOpenDBRequest, NS_LITERAL_STRING(SUCCESS_EVT_STR),
|
2011-11-23 06:15:15 -08:00
|
|
|
eDoesNotBubble, eNotCancelable);
|
2011-10-20 09:10:56 -07:00
|
|
|
if (!event) {
|
|
|
|
NS_ERROR("Failed to create event!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
mOpenDBRequest->DispatchEvent(event, &dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OpenDatabaseHelper::DispatchErrorEvent()
|
|
|
|
{
|
2013-03-09 03:34:29 -08:00
|
|
|
nsRefPtr<nsIDOMEvent> event =
|
|
|
|
CreateGenericEvent(mOpenDBRequest, NS_LITERAL_STRING(ERROR_EVT_STR),
|
2011-11-23 06:15:15 -08:00
|
|
|
eDoesBubble, eCancelable);
|
2011-10-20 09:10:56 -07:00
|
|
|
if (!event) {
|
|
|
|
NS_ERROR("Failed to create event!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-11 14:55:21 -07:00
|
|
|
nsCOMPtr<nsIDOMDOMError> error;
|
2011-10-20 09:10:56 -07:00
|
|
|
DebugOnly<nsresult> rv =
|
2012-04-11 14:55:21 -07:00
|
|
|
mOpenDBRequest->GetError(getter_AddRefs(error));
|
2011-10-20 09:10:56 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "This shouldn't be failing at this point!");
|
2012-04-11 14:55:21 -07:00
|
|
|
if (!error) {
|
2011-10-20 09:10:56 -07:00
|
|
|
mOpenDBRequest->SetError(mResultCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
mOpenDBRequest->DispatchEvent(event, &dummy);
|
|
|
|
}
|
|
|
|
|
2011-10-22 04:56:19 -07:00
|
|
|
void
|
|
|
|
OpenDatabaseHelper::ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mOpenDBRequest = nullptr;
|
|
|
|
mDatabase = nullptr;
|
|
|
|
mDatabaseId = nullptr;
|
2011-10-22 04:56:19 -07:00
|
|
|
|
|
|
|
HelperBase::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2013-01-11 02:14:04 -08:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(SetVersionHelper, AsyncConnectionHelper)
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2011-10-25 05:49:31 -07:00
|
|
|
nsresult
|
|
|
|
SetVersionHelper::Init()
|
|
|
|
{
|
|
|
|
// Block transaction creation until we are done.
|
|
|
|
mOpenHelper->BlockDatabase();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
nsresult
|
|
|
|
SetVersionHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aConnection, "Passing a null connection!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = aConnection->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE database "
|
|
|
|
"SET version = :version"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("version"),
|
|
|
|
mRequestedVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
if (NS_FAILED(stmt->Execute())) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SetVersionHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
|
|
|
{
|
2011-11-07 16:06:13 -08:00
|
|
|
DatabaseInfo* info = mDatabase->Info();
|
2011-10-20 09:10:56 -07:00
|
|
|
info->version = mRequestedVersion;
|
|
|
|
|
|
|
|
NS_ASSERTION(mTransaction, "Better have a transaction!");
|
|
|
|
|
|
|
|
mOpenRequest->SetTransaction(mTransaction);
|
|
|
|
|
|
|
|
return WrapNative(aCx, NS_ISUPPORTS_CAST(nsIDOMEventTarget*, mDatabase),
|
|
|
|
aVal);
|
|
|
|
}
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
nsresult
|
|
|
|
SetVersionHelper::OnExclusiveAccessAcquired()
|
|
|
|
{
|
|
|
|
nsresult rv = DispatchToTransactionPool();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
// static
|
2011-11-07 16:15:45 -08:00
|
|
|
template <class T>
|
2011-11-02 05:53:12 -07:00
|
|
|
void
|
2011-11-07 16:15:45 -08:00
|
|
|
VersionChangeEventsRunnable::QueueVersionChange(
|
2013-03-26 04:13:17 -07:00
|
|
|
nsTArray<nsCOMPtr<nsIOfflineStorage> >& aDatabases,
|
|
|
|
void* aClosure)
|
2011-11-02 05:53:12 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!aDatabases.IsEmpty(), "Why are we here?");
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
T* closure = static_cast<T*>(aClosure);
|
2011-11-02 05:53:12 -07:00
|
|
|
|
|
|
|
nsRefPtr<VersionChangeEventsRunnable> eventsRunnable =
|
2011-11-07 16:15:45 -08:00
|
|
|
new VersionChangeEventsRunnable(closure->mOpenHelper->Database(),
|
|
|
|
closure->mOpenRequest,
|
2011-11-02 05:53:12 -07:00
|
|
|
aDatabases,
|
2011-11-07 16:15:45 -08:00
|
|
|
closure->mCurrentVersion,
|
|
|
|
closure->RequestedVersion());
|
2011-11-02 05:53:12 -07:00
|
|
|
|
|
|
|
NS_DispatchToCurrentThread(eventsRunnable);
|
|
|
|
}
|
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
already_AddRefed<nsIDOMEvent>
|
|
|
|
SetVersionHelper::CreateSuccessEvent(mozilla::dom::EventTarget* aOwner)
|
2011-10-20 09:10:56 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mCurrentVersion < mRequestedVersion, "Huh?");
|
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
return IDBVersionChangeEvent::CreateUpgradeNeeded(aOwner,
|
|
|
|
mCurrentVersion,
|
2011-10-20 09:10:56 -07:00
|
|
|
mRequestedVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-06-26 08:09:25 -07:00
|
|
|
SetVersionHelper::NotifyTransactionPreComplete(IDBTransaction* aTransaction)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aTransaction, "This is unexpected.");
|
|
|
|
NS_ASSERTION(mOpenRequest, "Why don't we have a request?");
|
|
|
|
|
|
|
|
return mOpenHelper->NotifySetVersionFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SetVersionHelper::NotifyTransactionPostComplete(IDBTransaction* aTransaction)
|
2011-10-20 09:10:56 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aTransaction, "This is unexpected.");
|
|
|
|
NS_ASSERTION(mOpenRequest, "Why don't we have a request?");
|
|
|
|
|
|
|
|
// If we hit an error, the OpenDatabaseHelper needs to get that error too.
|
|
|
|
nsresult rv = GetResultCode();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mOpenHelper->SetError(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the transaction was aborted, we should throw an error message.
|
|
|
|
if (aTransaction->IsAborted()) {
|
2012-06-01 10:21:12 -07:00
|
|
|
mOpenHelper->SetError(aTransaction->GetAbortCode());
|
2011-10-20 09:10:56 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mOpenRequest->SetTransaction(nullptr);
|
|
|
|
mOpenRequest = nullptr;
|
2011-10-20 09:10:56 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mOpenHelper = nullptr;
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2011-11-07 16:15:45 -08:00
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(DeleteDatabaseHelper, AsyncConnectionHelper);
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
nsresult
|
|
|
|
DeleteDatabaseHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!aConnection, "How did we get a connection here?");
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
const StoragePrivilege& privilege = mOpenHelper->Privilege();
|
2012-12-17 11:25:10 -08:00
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "This should never fail!");
|
2012-04-30 16:52:27 -07:00
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsCOMPtr<nsIFile> directory;
|
2013-03-26 04:13:17 -07:00
|
|
|
nsresult rv = quotaManager->GetDirectoryForOrigin(mASCIIOrigin,
|
|
|
|
getter_AddRefs(directory));
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
NS_ASSERTION(directory, "What?");
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
rv = directory->Append(NS_LITERAL_STRING(IDB_DIRECTORY_NAME));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
nsAutoString filename;
|
|
|
|
rv = GetDatabaseFilename(mName, filename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
nsCOMPtr<nsIFile> dbFile;
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = directory->Clone(getter_AddRefs(dbFile));
|
2011-11-07 16:15:45 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
rv = dbFile->Append(filename + NS_LITERAL_STRING(".sqlite"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-11-07 16:15:45 -08:00
|
|
|
|
|
|
|
bool exists = false;
|
|
|
|
rv = dbFile->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-12-15 23:34:24 -08:00
|
|
|
if (exists) {
|
2012-12-17 11:25:10 -08:00
|
|
|
int64_t fileSize;
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
if (privilege != Chrome) {
|
|
|
|
rv = dbFile->GetFileSize(&fileSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
2012-06-11 14:59:27 -07:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = dbFile->Remove(false);
|
2012-06-11 14:59:27 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
if (privilege != Chrome) {
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Shouldn't be null!");
|
|
|
|
|
|
|
|
quotaManager->DecreaseUsageForOrigin(mASCIIOrigin, fileSize);
|
2012-06-11 14:59:27 -07:00
|
|
|
}
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
nsCOMPtr<nsIFile> fmDirectory;
|
|
|
|
rv = directory->Clone(getter_AddRefs(fmDirectory));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = fmDirectory->Append(filename);
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = fmDirectory->Exists(&exists);
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
if (exists) {
|
2011-12-15 23:34:24 -08:00
|
|
|
bool isDirectory;
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = fmDirectory->IsDirectory(&isDirectory);
|
2011-12-15 23:34:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(isDirectory, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
uint64_t usage = 0;
|
2011-12-15 23:34:24 -08:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
if (privilege != Chrome) {
|
|
|
|
rv = FileManager::GetUsage(fmDirectory, &usage);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-12-15 23:34:24 -08:00
|
|
|
}
|
2012-01-04 08:25:26 -08:00
|
|
|
|
2012-12-17 11:25:10 -08:00
|
|
|
rv = fmDirectory->Remove(true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
if (privilege != Chrome) {
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "Shouldn't be null!");
|
|
|
|
|
|
|
|
quotaManager->DecreaseUsageForOrigin(mASCIIOrigin, usage);
|
|
|
|
}
|
2011-11-07 16:15:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DeleteDatabaseHelper::GetSuccessResult(JSContext* aCx, jsval* aVal)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-26 04:13:17 -07:00
|
|
|
nsresult
|
|
|
|
DeleteDatabaseHelper::OnExclusiveAccessAcquired()
|
|
|
|
{
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
NS_ASSERTION(quotaManager, "We should definitely have a manager here");
|
|
|
|
|
|
|
|
nsresult rv = Dispatch(quotaManager->IOThread());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:15:45 -08:00
|
|
|
nsresult
|
|
|
|
DeleteDatabaseHelper::Init()
|
|
|
|
{
|
|
|
|
// Note that there's no need to block the database here, since the page
|
|
|
|
// never gets to touch it, and all other databases must be closed.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|