Chromium Code Reviews| Index: webkit/dom_storage/dom_storage_database.cc |
| diff --git a/webkit/dom_storage/dom_storage_database.cc b/webkit/dom_storage/dom_storage_database.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7638a02517f5a80b17a969f8d0f15815ae87fcdb |
| --- /dev/null |
| +++ b/webkit/dom_storage/dom_storage_database.cc |
| @@ -0,0 +1,198 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "webkit/dom_storage/dom_storage_database.h" |
| + |
| +#include <string> |
| + |
| +#include "base/logging.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "sql/diagnostic_error_delegate.h" |
| +#include "sql/statement.h" |
| +#include "sql/transaction.h" |
| + |
| +namespace { |
| + |
| +class HistogramUniquifier { |
| + public: |
| + static const char* name() { return "Sqlite.DomStorageDatabase.Error"; } |
| +}; |
| + |
| +sql::ErrorDelegate* GetErrorHandlerForDomStorageDatabase() { |
| + return new sql::DiagnosticErrorDelegate<HistogramUniquifier>(); |
| +} |
| + |
| +} // anon namespace |
| + |
| +namespace dom_storage { |
| + |
| +DomStorageDatabase::DomStorageDatabase(const FilePath& file_path) |
| + : file_path_(file_path), |
| + db_(NULL) { |
| + DCHECK(!file_path_.empty()); |
| +} |
| + |
| +DomStorageDatabase::~DomStorageDatabase() { |
| + if (IsOpen()) |
| + Close(); |
| +} |
| + |
| +bool DomStorageDatabase::Open() { |
| + if (IsOpen()) |
| + return true; |
| + |
| + db_.reset(new sql::Connection()); |
| + bool is_open = db_->Open(file_path_); |
| + db_->set_error_delegate(GetErrorHandlerForDomStorageDatabase()); |
| + |
| + if (!is_open) { |
| + LOG(WARNING) << "Unable to open Database at " << file_path_.value(); |
| + return false; |
| + } |
| + |
| + // sql::Connection uses UTF-8 encoding, but WebCore style databases use |
| + // UTF-16, so ensure we match. |
| + ignore_result(db_->Execute("PRAGMA encoding=\"UTF-16\"")); |
| + |
| + return is_open; |
| +} |
| + |
| +bool DomStorageDatabase::Init() { |
| + if (!IsOpen()) |
| + return false; |
| + |
| + if (db_->DoesTableExist("ItemTable")) { |
| + if (UpgradeVersion1To2IfNeeded()) |
| + return true; |
| + else { |
| + // Upgrade failed, drop table and start fresh. |
| + if (db_->Execute("DROP TABLE ItemTable")) |
| + return CreateTable(); |
| + else |
| + return false; |
| + } |
| + } else |
| + return CreateTable(); |
| +} |
| + |
| +void DomStorageDatabase::Close() { |
| + if (!IsOpen()) |
| + return; |
| + |
| + db_->Close(); |
| + db_.reset(NULL); |
| +} |
| + |
| +bool DomStorageDatabase::CreateTable() { |
| + // Current version is 2. |
| + return CreateTableV2(); |
| +} |
| + |
| +bool DomStorageDatabase::CreateTableV2() { |
| + if (!IsOpen()) |
| + return false; |
| + |
| + return db_->Execute( |
| + "CREATE TABLE IF NOT EXISTS ItemTable (" |
| + "key TEXT UNIQUE ON CONFLICT REPLACE, " |
| + "value BLOB NOT NULL ON CONFLICT FAIL)"); |
| +} |
| + |
| +DomStorageDatabase::ValuesMap DomStorageDatabase::ReadAllValues() const { |
| + ValuesMap values; |
| + if (!IsOpen()) |
| + return values; |
| + |
| + sql::Statement stmt(db_->GetCachedStatement(SQL_FROM_HERE, |
| + "SELECT * from ItemTable")); |
| + DCHECK(stmt.is_valid()); |
| + if (stmt) { |
| + while (stmt.Step()) { |
| + string16 key = stmt.ColumnString16(0); |
| + string16 value(reinterpret_cast<const char16*>(stmt.ColumnBlob(1))); |
|
michaeln
2012/01/31 03:11:16
Can these values have embedded NULLs in them and i
benm (inactive)
2012/01/31 15:10:53
I'm writing out using c_str(), so I think it shoul
|
| + values[key] = value; |
| + |
| + } |
| + } |
| + |
| + return values; |
| +} |
| + |
| +bool DomStorageDatabase::WriteValues(const ValuesMap& values) { |
| + if (!IsOpen()) |
| + return false; |
| + |
| + ValuesMap::const_iterator it = values.begin(); |
| + for(; it != values.end(); ++it) { |
| + sql::Statement stmt(db_->GetCachedStatement(SQL_FROM_HERE, |
| + "INSERT INTO ItemTable VALUES (?,?)")); |
| + string16 key = it->first; |
| + string16 value = it->second; |
| + stmt.BindString16(0, key); |
| + stmt.BindBlob(1, value.c_str(), -1); |
|
michaeln
2012/01/31 03:11:16
do we have to be explicit about the length here?
benm (inactive)
2012/01/31 15:10:53
-1 says to write up to the first null, so not as l
|
| + DCHECK(stmt.is_valid()); |
| + stmt.Run(); |
| + } |
| + return true; |
| +} |
| + |
| +bool DomStorageDatabase::RemoveValues(const ValuesSet& keys) { |
| + if (!IsOpen()) |
| + return false; |
| + |
| + ValuesSet::const_iterator it = keys.begin(); |
| + for ( ; it != keys.end(); ++it) { |
|
michaeln
2012/01/31 03:11:16
We'll want to put multiple write statements in a t
benm (inactive)
2012/01/31 15:10:53
Done (and for Write above).
|
| + sql::Statement stmt(db_->GetCachedStatement(SQL_FROM_HERE, |
| + "DELETE FROM ItemTable WHERE key=?")); |
| + stmt.BindString16(0, *it); |
| + DCHECK(stmt.is_valid()); |
| + stmt.Run(); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool DomStorageDatabase::UpgradeVersion1To2IfNeeded() { |
| + sql::Statement stmt(db_->GetCachedStatement(SQL_FROM_HERE, |
| + "SELECT * FROM ItemTable")); |
| + DCHECK(stmt.is_valid()); |
| + if (!stmt) |
| + return false; |
| + |
| + // Quick check to see if we need to upgrade or not. The single |
| + // effect of V1 -> V2 is to change the value column from type |
| + // TEXT to type BLOB. |
| + sql::ColType valueColumnType = stmt.DeclaredColumnType(1); |
| + if (valueColumnType == sql::COLUMN_TYPE_BLOB) |
| + return true; |
| + |
| + if (valueColumnType != sql::COLUMN_TYPE_TEXT) { |
| + // Something is messed up. This is not a V1 database. |
| + return false; |
| + } |
| + |
| + // Need to migrate from TEXT value column to BLOB. |
| + ValuesMap values; |
| + while (stmt.Step()) { |
| + string16 key = stmt.ColumnString16(0); |
| + string16 value = stmt.ColumnString16(1); |
| + values[key] = value; |
| + } |
| + |
| + { |
| + sql::Transaction migration(db_.get()); |
| + if (migration.Begin()) { |
| + if (db_->Execute("DROP TABLE ItemTable")) { |
| + CreateTableV2(); |
| + if (WriteValues(values)) { |
| + migration.Commit(); |
| + return true; |
| + } |
| + } |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +} // namespace dom_storage |