Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / storage / browser / database / databases_table.cc
blob6d84ae8c54dc2ca1439c15993ad3378543eb2cc0
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
3 // found in the LICENSE file.
5 #include "storage/browser/database/databases_table.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "sql/statement.h"
11 namespace storage {
13 DatabaseDetails::DatabaseDetails() : estimated_size(0) { }
15 DatabaseDetails::~DatabaseDetails() {}
17 bool DatabasesTable::Init() {
18 // 'Databases' schema:
19 // id A unique ID assigned to each database
20 // origin The originto which the database belongs. This is a
21 // string that can be used as part of a file name
22 // (http_webkit.org_0, for example).
23 // name The database name.
24 // description A short description of the database.
25 // estimated_size The estimated size of the database.
26 return db_->DoesTableExist("Databases") ||
27 (db_->Execute(
28 "CREATE TABLE Databases ("
29 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
30 "origin TEXT NOT NULL, "
31 "name TEXT NOT NULL, "
32 "description TEXT NOT NULL, "
33 "estimated_size INTEGER NOT NULL)") &&
34 db_->Execute(
35 "CREATE INDEX origin_index ON Databases (origin)") &&
36 db_->Execute(
37 "CREATE UNIQUE INDEX unique_index ON Databases (origin, name)"));
40 int64 DatabasesTable::GetDatabaseID(const std::string& origin_identifier,
41 const base::string16& database_name) {
42 sql::Statement select_statement(db_->GetCachedStatement(
43 SQL_FROM_HERE, "SELECT id FROM Databases WHERE origin = ? AND name = ?"));
44 select_statement.BindString(0, origin_identifier);
45 select_statement.BindString16(1, database_name);
47 if (select_statement.Step()) {
48 return select_statement.ColumnInt64(0);
51 return -1;
54 bool DatabasesTable::GetDatabaseDetails(const std::string& origin_identifier,
55 const base::string16& database_name,
56 DatabaseDetails* details) {
57 DCHECK(details);
58 sql::Statement select_statement(db_->GetCachedStatement(
59 SQL_FROM_HERE, "SELECT description, estimated_size FROM Databases "
60 "WHERE origin = ? AND name = ?"));
61 select_statement.BindString(0, origin_identifier);
62 select_statement.BindString16(1, database_name);
64 if (select_statement.Step()) {
65 details->origin_identifier = origin_identifier;
66 details->database_name = database_name;
67 details->description = select_statement.ColumnString16(0);
68 details->estimated_size = select_statement.ColumnInt64(1);
69 return true;
72 return false;
75 bool DatabasesTable::InsertDatabaseDetails(const DatabaseDetails& details) {
76 sql::Statement insert_statement(db_->GetCachedStatement(
77 SQL_FROM_HERE, "INSERT INTO Databases (origin, name, description, "
78 "estimated_size) VALUES (?, ?, ?, ?)"));
79 insert_statement.BindString(0, details.origin_identifier);
80 insert_statement.BindString16(1, details.database_name);
81 insert_statement.BindString16(2, details.description);
82 insert_statement.BindInt64(3, details.estimated_size);
84 return insert_statement.Run();
87 bool DatabasesTable::UpdateDatabaseDetails(const DatabaseDetails& details) {
88 sql::Statement update_statement(db_->GetCachedStatement(
89 SQL_FROM_HERE, "UPDATE Databases SET description = ?, "
90 "estimated_size = ? WHERE origin = ? AND name = ?"));
91 update_statement.BindString16(0, details.description);
92 update_statement.BindInt64(1, details.estimated_size);
93 update_statement.BindString(2, details.origin_identifier);
94 update_statement.BindString16(3, details.database_name);
96 return (update_statement.Run() && db_->GetLastChangeCount());
99 bool DatabasesTable::DeleteDatabaseDetails(
100 const std::string& origin_identifier,
101 const base::string16& database_name) {
102 sql::Statement delete_statement(db_->GetCachedStatement(
103 SQL_FROM_HERE, "DELETE FROM Databases WHERE origin = ? AND name = ?"));
104 delete_statement.BindString(0, origin_identifier);
105 delete_statement.BindString16(1, database_name);
107 return (delete_statement.Run() && db_->GetLastChangeCount());
110 bool DatabasesTable::GetAllOriginIdentifiers(
111 std::vector<std::string>* origin_identifiers) {
112 sql::Statement statement(db_->GetCachedStatement(
113 SQL_FROM_HERE, "SELECT DISTINCT origin FROM Databases ORDER BY origin"));
115 while (statement.Step())
116 origin_identifiers->push_back(statement.ColumnString(0));
118 return statement.Succeeded();
121 bool DatabasesTable::GetAllDatabaseDetailsForOriginIdentifier(
122 const std::string& origin_identifier,
123 std::vector<DatabaseDetails>* details_vector) {
124 sql::Statement statement(db_->GetCachedStatement(
125 SQL_FROM_HERE, "SELECT name, description, estimated_size "
126 "FROM Databases WHERE origin = ? ORDER BY name"));
127 statement.BindString(0, origin_identifier);
129 while (statement.Step()) {
130 DatabaseDetails details;
131 details.origin_identifier = origin_identifier;
132 details.database_name = statement.ColumnString16(0);
133 details.description = statement.ColumnString16(1);
134 details.estimated_size = statement.ColumnInt64(2);
135 details_vector->push_back(details);
138 return statement.Succeeded();
141 bool DatabasesTable::DeleteOriginIdentifier(
142 const std::string& origin_identifier) {
143 sql::Statement delete_statement(db_->GetCachedStatement(
144 SQL_FROM_HERE, "DELETE FROM Databases WHERE origin = ?"));
145 delete_statement.BindString(0, origin_identifier);
147 return (delete_statement.Run() && db_->GetLastChangeCount());
150 } // namespace storage