| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/browser/database/databases_table.h" | 5 #include "storage/browser/database/databases_table.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "sql/statement.h" | 9 #include "sql/statement.h" |
| 10 | 10 |
| 11 namespace webkit_database { | 11 namespace webkit_database { |
| 12 | 12 |
| 13 DatabaseDetails::DatabaseDetails() : estimated_size(0) { } | 13 DatabaseDetails::DatabaseDetails() : estimated_size(0) { |
| 14 } |
| 14 | 15 |
| 15 DatabaseDetails::~DatabaseDetails() {} | 16 DatabaseDetails::~DatabaseDetails() { |
| 17 } |
| 16 | 18 |
| 17 bool DatabasesTable::Init() { | 19 bool DatabasesTable::Init() { |
| 18 // 'Databases' schema: | 20 // 'Databases' schema: |
| 19 // id A unique ID assigned to each database | 21 // id A unique ID assigned to each database |
| 20 // origin The originto which the database belongs. This is a | 22 // origin The originto which the database belongs. This is a |
| 21 // string that can be used as part of a file name | 23 // string that can be used as part of a file name |
| 22 // (http_webkit.org_0, for example). | 24 // (http_webkit.org_0, for example). |
| 23 // name The database name. | 25 // name The database name. |
| 24 // description A short description of the database. | 26 // description A short description of the database. |
| 25 // estimated_size The estimated size of the database. | 27 // estimated_size The estimated size of the database. |
| 26 return db_->DoesTableExist("Databases") || | 28 return db_->DoesTableExist("Databases") || |
| 27 (db_->Execute( | 29 (db_->Execute( |
| 28 "CREATE TABLE Databases (" | 30 "CREATE TABLE Databases (" |
| 29 "id INTEGER PRIMARY KEY AUTOINCREMENT, " | 31 "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 30 "origin TEXT NOT NULL, " | 32 "origin TEXT NOT NULL, " |
| 31 "name TEXT NOT NULL, " | 33 "name TEXT NOT NULL, " |
| 32 "description TEXT NOT NULL, " | 34 "description TEXT NOT NULL, " |
| 33 "estimated_size INTEGER NOT NULL)") && | 35 "estimated_size INTEGER NOT NULL)") && |
| 34 db_->Execute( | 36 db_->Execute("CREATE INDEX origin_index ON Databases (origin)") && |
| 35 "CREATE INDEX origin_index ON Databases (origin)") && | 37 db_->Execute( |
| 36 db_->Execute( | 38 "CREATE UNIQUE INDEX unique_index ON Databases (origin, name)")); |
| 37 "CREATE UNIQUE INDEX unique_index ON Databases (origin, name)")); | |
| 38 } | 39 } |
| 39 | 40 |
| 40 int64 DatabasesTable::GetDatabaseID(const std::string& origin_identifier, | 41 int64 DatabasesTable::GetDatabaseID(const std::string& origin_identifier, |
| 41 const base::string16& database_name) { | 42 const base::string16& database_name) { |
| 42 sql::Statement select_statement(db_->GetCachedStatement( | 43 sql::Statement select_statement(db_->GetCachedStatement( |
| 43 SQL_FROM_HERE, "SELECT id FROM Databases WHERE origin = ? AND name = ?")); | 44 SQL_FROM_HERE, "SELECT id FROM Databases WHERE origin = ? AND name = ?")); |
| 44 select_statement.BindString(0, origin_identifier); | 45 select_statement.BindString(0, origin_identifier); |
| 45 select_statement.BindString16(1, database_name); | 46 select_statement.BindString16(1, database_name); |
| 46 | 47 |
| 47 if (select_statement.Step()) { | 48 if (select_statement.Step()) { |
| 48 return select_statement.ColumnInt64(0); | 49 return select_statement.ColumnInt64(0); |
| 49 } | 50 } |
| 50 | 51 |
| 51 return -1; | 52 return -1; |
| 52 } | 53 } |
| 53 | 54 |
| 54 bool DatabasesTable::GetDatabaseDetails(const std::string& origin_identifier, | 55 bool DatabasesTable::GetDatabaseDetails(const std::string& origin_identifier, |
| 55 const base::string16& database_name, | 56 const base::string16& database_name, |
| 56 DatabaseDetails* details) { | 57 DatabaseDetails* details) { |
| 57 DCHECK(details); | 58 DCHECK(details); |
| 58 sql::Statement select_statement(db_->GetCachedStatement( | 59 sql::Statement select_statement(db_->GetCachedStatement( |
| 59 SQL_FROM_HERE, "SELECT description, estimated_size FROM Databases " | 60 SQL_FROM_HERE, |
| 60 "WHERE origin = ? AND name = ?")); | 61 "SELECT description, estimated_size FROM Databases " |
| 62 "WHERE origin = ? AND name = ?")); |
| 61 select_statement.BindString(0, origin_identifier); | 63 select_statement.BindString(0, origin_identifier); |
| 62 select_statement.BindString16(1, database_name); | 64 select_statement.BindString16(1, database_name); |
| 63 | 65 |
| 64 if (select_statement.Step()) { | 66 if (select_statement.Step()) { |
| 65 details->origin_identifier = origin_identifier; | 67 details->origin_identifier = origin_identifier; |
| 66 details->database_name = database_name; | 68 details->database_name = database_name; |
| 67 details->description = select_statement.ColumnString16(0); | 69 details->description = select_statement.ColumnString16(0); |
| 68 details->estimated_size = select_statement.ColumnInt64(1); | 70 details->estimated_size = select_statement.ColumnInt64(1); |
| 69 return true; | 71 return true; |
| 70 } | 72 } |
| 71 | 73 |
| 72 return false; | 74 return false; |
| 73 } | 75 } |
| 74 | 76 |
| 75 bool DatabasesTable::InsertDatabaseDetails(const DatabaseDetails& details) { | 77 bool DatabasesTable::InsertDatabaseDetails(const DatabaseDetails& details) { |
| 76 sql::Statement insert_statement(db_->GetCachedStatement( | 78 sql::Statement insert_statement(db_->GetCachedStatement( |
| 77 SQL_FROM_HERE, "INSERT INTO Databases (origin, name, description, " | 79 SQL_FROM_HERE, |
| 78 "estimated_size) VALUES (?, ?, ?, ?)")); | 80 "INSERT INTO Databases (origin, name, description, " |
| 81 "estimated_size) VALUES (?, ?, ?, ?)")); |
| 79 insert_statement.BindString(0, details.origin_identifier); | 82 insert_statement.BindString(0, details.origin_identifier); |
| 80 insert_statement.BindString16(1, details.database_name); | 83 insert_statement.BindString16(1, details.database_name); |
| 81 insert_statement.BindString16(2, details.description); | 84 insert_statement.BindString16(2, details.description); |
| 82 insert_statement.BindInt64(3, details.estimated_size); | 85 insert_statement.BindInt64(3, details.estimated_size); |
| 83 | 86 |
| 84 return insert_statement.Run(); | 87 return insert_statement.Run(); |
| 85 } | 88 } |
| 86 | 89 |
| 87 bool DatabasesTable::UpdateDatabaseDetails(const DatabaseDetails& details) { | 90 bool DatabasesTable::UpdateDatabaseDetails(const DatabaseDetails& details) { |
| 88 sql::Statement update_statement(db_->GetCachedStatement( | 91 sql::Statement update_statement(db_->GetCachedStatement( |
| 89 SQL_FROM_HERE, "UPDATE Databases SET description = ?, " | 92 SQL_FROM_HERE, |
| 90 "estimated_size = ? WHERE origin = ? AND name = ?")); | 93 "UPDATE Databases SET description = ?, " |
| 94 "estimated_size = ? WHERE origin = ? AND name = ?")); |
| 91 update_statement.BindString16(0, details.description); | 95 update_statement.BindString16(0, details.description); |
| 92 update_statement.BindInt64(1, details.estimated_size); | 96 update_statement.BindInt64(1, details.estimated_size); |
| 93 update_statement.BindString(2, details.origin_identifier); | 97 update_statement.BindString(2, details.origin_identifier); |
| 94 update_statement.BindString16(3, details.database_name); | 98 update_statement.BindString16(3, details.database_name); |
| 95 | 99 |
| 96 return (update_statement.Run() && db_->GetLastChangeCount()); | 100 return (update_statement.Run() && db_->GetLastChangeCount()); |
| 97 } | 101 } |
| 98 | 102 |
| 99 bool DatabasesTable::DeleteDatabaseDetails( | 103 bool DatabasesTable::DeleteDatabaseDetails( |
| 100 const std::string& origin_identifier, | 104 const std::string& origin_identifier, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 114 | 118 |
| 115 while (statement.Step()) | 119 while (statement.Step()) |
| 116 origin_identifiers->push_back(statement.ColumnString(0)); | 120 origin_identifiers->push_back(statement.ColumnString(0)); |
| 117 | 121 |
| 118 return statement.Succeeded(); | 122 return statement.Succeeded(); |
| 119 } | 123 } |
| 120 | 124 |
| 121 bool DatabasesTable::GetAllDatabaseDetailsForOriginIdentifier( | 125 bool DatabasesTable::GetAllDatabaseDetailsForOriginIdentifier( |
| 122 const std::string& origin_identifier, | 126 const std::string& origin_identifier, |
| 123 std::vector<DatabaseDetails>* details_vector) { | 127 std::vector<DatabaseDetails>* details_vector) { |
| 124 sql::Statement statement(db_->GetCachedStatement( | 128 sql::Statement statement( |
| 125 SQL_FROM_HERE, "SELECT name, description, estimated_size " | 129 db_->GetCachedStatement(SQL_FROM_HERE, |
| 126 "FROM Databases WHERE origin = ? ORDER BY name")); | 130 "SELECT name, description, estimated_size " |
| 131 "FROM Databases WHERE origin = ? ORDER BY name")); |
| 127 statement.BindString(0, origin_identifier); | 132 statement.BindString(0, origin_identifier); |
| 128 | 133 |
| 129 while (statement.Step()) { | 134 while (statement.Step()) { |
| 130 DatabaseDetails details; | 135 DatabaseDetails details; |
| 131 details.origin_identifier = origin_identifier; | 136 details.origin_identifier = origin_identifier; |
| 132 details.database_name = statement.ColumnString16(0); | 137 details.database_name = statement.ColumnString16(0); |
| 133 details.description = statement.ColumnString16(1); | 138 details.description = statement.ColumnString16(1); |
| 134 details.estimated_size = statement.ColumnInt64(2); | 139 details.estimated_size = statement.ColumnInt64(2); |
| 135 details_vector->push_back(details); | 140 details_vector->push_back(details); |
| 136 } | 141 } |
| 137 | 142 |
| 138 return statement.Succeeded(); | 143 return statement.Succeeded(); |
| 139 } | 144 } |
| 140 | 145 |
| 141 bool DatabasesTable::DeleteOriginIdentifier( | 146 bool DatabasesTable::DeleteOriginIdentifier( |
| 142 const std::string& origin_identifier) { | 147 const std::string& origin_identifier) { |
| 143 sql::Statement delete_statement(db_->GetCachedStatement( | 148 sql::Statement delete_statement(db_->GetCachedStatement( |
| 144 SQL_FROM_HERE, "DELETE FROM Databases WHERE origin = ?")); | 149 SQL_FROM_HERE, "DELETE FROM Databases WHERE origin = ?")); |
| 145 delete_statement.BindString(0, origin_identifier); | 150 delete_statement.BindString(0, origin_identifier); |
| 146 | 151 |
| 147 return (delete_statement.Run() && db_->GetLastChangeCount()); | 152 return (delete_statement.Run() && db_->GetLastChangeCount()); |
| 148 } | 153 } |
| 149 | 154 |
| 150 } // namespace webkit_database | 155 } // namespace webkit_database |
| OLD | NEW |