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 "webkit/database/databases_table.h"
7 #include "base/logging.h"
8 #include "base/utf_string_conversions.h"
9 #include "sql/statement.h"
11 namespace webkit_database
{
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") ||
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)") &&
35 "CREATE INDEX origin_index ON Databases (origin)") &&
37 "CREATE UNIQUE INDEX unique_index ON Databases (origin, name)"));
40 int64
DatabasesTable::GetDatabaseID(const string16
& origin_identifier
,
41 const 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
.BindString16(0, origin_identifier
);
45 select_statement
.BindString16(1, database_name
);
47 if (select_statement
.Step()) {
48 return select_statement
.ColumnInt64(0);
54 bool DatabasesTable::GetDatabaseDetails(const string16
& origin_identifier
,
55 const string16
& database_name
,
56 DatabaseDetails
* 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
.BindString16(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);
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
.BindString16(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
.BindString16(2, details
.origin_identifier
);
94 update_statement
.BindString16(3, details
.database_name
);
96 return (update_statement
.Run() && db_
->GetLastChangeCount());
99 bool DatabasesTable::DeleteDatabaseDetails(const string16
& origin_identifier
,
100 const string16
& database_name
) {
101 sql::Statement
delete_statement(db_
->GetCachedStatement(
102 SQL_FROM_HERE
, "DELETE FROM Databases WHERE origin = ? AND name = ?"));
103 delete_statement
.BindString16(0, origin_identifier
);
104 delete_statement
.BindString16(1, database_name
);
106 return (delete_statement
.Run() && db_
->GetLastChangeCount());
109 bool DatabasesTable::GetAllOrigins(std::vector
<string16
>* origins
) {
110 sql::Statement
statement(db_
->GetCachedStatement(
111 SQL_FROM_HERE
, "SELECT DISTINCT origin FROM Databases ORDER BY origin"));
113 while (statement
.Step())
114 origins
->push_back(statement
.ColumnString16(0));
116 return statement
.Succeeded();
119 bool DatabasesTable::GetAllDatabaseDetailsForOrigin(
120 const string16
& origin_identifier
,
121 std::vector
<DatabaseDetails
>* details_vector
) {
122 sql::Statement
statement(db_
->GetCachedStatement(
123 SQL_FROM_HERE
, "SELECT name, description, estimated_size "
124 "FROM Databases WHERE origin = ? ORDER BY name"));
125 statement
.BindString16(0, origin_identifier
);
127 while (statement
.Step()) {
128 DatabaseDetails details
;
129 details
.origin_identifier
= origin_identifier
;
130 details
.database_name
= statement
.ColumnString16(0);
131 details
.description
= statement
.ColumnString16(1);
132 details
.estimated_size
= statement
.ColumnInt64(2);
133 details_vector
->push_back(details
);
136 return statement
.Succeeded();
139 bool DatabasesTable::DeleteOrigin(const string16
& origin_identifier
) {
140 sql::Statement
delete_statement(db_
->GetCachedStatement(
141 SQL_FROM_HERE
, "DELETE FROM Databases WHERE origin = ?"));
142 delete_statement
.BindString16(0, origin_identifier
);
144 return (delete_statement
.Run() && db_
->GetLastChangeCount());
147 } // namespace webkit_database