1 // Copyright (c) 2011 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 if (select_statement
.is_valid() &&
45 select_statement
.BindString(0, UTF16ToUTF8(origin_identifier
)) &&
46 select_statement
.BindString(1, UTF16ToUTF8(database_name
)) &&
47 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 if (select_statement
.is_valid() &&
62 select_statement
.BindString(0, UTF16ToUTF8(origin_identifier
)) &&
63 select_statement
.BindString(1, UTF16ToUTF8(database_name
)) &&
64 select_statement
.Step()) {
65 details
->origin_identifier
= origin_identifier
;
66 details
->database_name
= database_name
;
67 details
->description
= UTF8ToUTF16(select_statement
.ColumnString(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 if (insert_statement
.is_valid() &&
80 insert_statement
.BindString(0, UTF16ToUTF8(details
.origin_identifier
)) &&
81 insert_statement
.BindString(1, UTF16ToUTF8(details
.database_name
)) &&
82 insert_statement
.BindString(2, UTF16ToUTF8(details
.description
)) &&
83 insert_statement
.BindInt64(3, details
.estimated_size
)) {
84 return insert_statement
.Run();
90 bool DatabasesTable::UpdateDatabaseDetails(const DatabaseDetails
& details
) {
91 sql::Statement
update_statement(db_
->GetCachedStatement(
92 SQL_FROM_HERE
, "UPDATE Databases SET description = ?, "
93 "estimated_size = ? WHERE origin = ? AND name = ?"));
94 if (update_statement
.is_valid() &&
95 update_statement
.BindString(0, UTF16ToUTF8(details
.description
)) &&
96 update_statement
.BindInt64(1, details
.estimated_size
) &&
97 update_statement
.BindString(2, UTF16ToUTF8(details
.origin_identifier
)) &&
98 update_statement
.BindString(3, UTF16ToUTF8(details
.database_name
))) {
99 return (update_statement
.Run() && db_
->GetLastChangeCount());
105 bool DatabasesTable::DeleteDatabaseDetails(const string16
& origin_identifier
,
106 const string16
& database_name
) {
107 sql::Statement
delete_statement(db_
->GetCachedStatement(
108 SQL_FROM_HERE
, "DELETE FROM Databases WHERE origin = ? AND name = ?"));
109 if (delete_statement
.is_valid() &&
110 delete_statement
.BindString(0, UTF16ToUTF8(origin_identifier
)) &&
111 delete_statement
.BindString(1, UTF16ToUTF8(database_name
))) {
112 return (delete_statement
.Run() && db_
->GetLastChangeCount());
118 bool DatabasesTable::GetAllOrigins(std::vector
<string16
>* origins
) {
119 sql::Statement
statement(db_
->GetCachedStatement(
120 SQL_FROM_HERE
, "SELECT DISTINCT origin FROM Databases ORDER BY origin"));
121 if (statement
.is_valid()) {
122 while (statement
.Step())
123 origins
->push_back(UTF8ToUTF16(statement
.ColumnString(0)));
124 return statement
.Succeeded();
130 bool DatabasesTable::GetAllDatabaseDetailsForOrigin(
131 const string16
& origin_identifier
,
132 std::vector
<DatabaseDetails
>* details_vector
) {
133 sql::Statement
statement(db_
->GetCachedStatement(
134 SQL_FROM_HERE
, "SELECT name, description, estimated_size "
135 "FROM Databases WHERE origin = ? ORDER BY name"));
136 if (statement
.is_valid() &&
137 statement
.BindString(0, UTF16ToUTF8(origin_identifier
))) {
138 while (statement
.Step()) {
139 DatabaseDetails details
;
140 details
.origin_identifier
= origin_identifier
;
141 details
.database_name
= UTF8ToUTF16(statement
.ColumnString(0));
142 details
.description
= UTF8ToUTF16(statement
.ColumnString(1));
143 details
.estimated_size
= statement
.ColumnInt64(2);
144 details_vector
->push_back(details
);
146 return statement
.Succeeded();
152 bool DatabasesTable::DeleteOrigin(const string16
& origin_identifier
) {
153 sql::Statement
delete_statement(db_
->GetCachedStatement(
154 SQL_FROM_HERE
, "DELETE FROM Databases WHERE origin = ?"));
155 if (delete_statement
.is_valid() &&
156 delete_statement
.BindString(0, UTF16ToUTF8(origin_identifier
))) {
157 return (delete_statement
.Run() && db_
->GetLastChangeCount());
163 } // namespace webkit_database