1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
// 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 "chrome/browser/history/android/android_cache_database.h"
#include "base/file_util.h"
#include "chrome/browser/history/android/android_time.h"
#include "sql/statement.h"
using base::Time;
using base::TimeDelta;
namespace history {
AndroidCacheDatabase::AndroidCacheDatabase() {
}
AndroidCacheDatabase::~AndroidCacheDatabase() {
}
sql::InitStatus AndroidCacheDatabase::InitAndroidCacheDatabase(
const FilePath& db_name) {
if (!CreateDatabase(db_name))
return sql::INIT_FAILURE;
if (!Attach())
return sql::INIT_FAILURE;
if (!CreateBookmarkCacheTable())
return sql::INIT_FAILURE;
if (!CreateSearchTermsTable())
return sql::INIT_FAILURE;
return sql::INIT_OK;
}
bool AndroidCacheDatabase::AddBookmarkCacheRow(const Time& created_time,
const Time& last_visit_time,
URLID url_id) {
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"INSERT INTO android_cache_db.bookmark_cache (created_time, "
"last_visit_time, url_id) VALUES (?, ?, ?)"));
statement.BindInt64(0, ToDatabaseTime(created_time));
statement.BindInt64(1, ToDatabaseTime(last_visit_time));
statement.BindInt64(2, url_id);
if (!statement.Run()) {
LOG(ERROR) << GetDB().GetErrorMessage();
return false;
}
return true;
}
bool AndroidCacheDatabase::ClearAllBookmarkCache() {
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"DELETE FROM android_cache_db.bookmark_cache"));
if (!statement.Run()) {
LOG(ERROR) << GetDB().GetErrorMessage();
return false;
}
return true;
}
bool AndroidCacheDatabase::MarkURLsAsBookmarked(
const std::vector<URLID>& url_ids) {
bool has_id = false;
std::ostringstream oss;
for (std::vector<URLID>::const_iterator i = url_ids.begin();
i != url_ids.end(); ++i) {
if (has_id)
oss << ", ";
else
has_id = true;
oss << *i;
}
if (!has_id)
return true;
std::string sql("UPDATE android_cache_db.bookmark_cache "
"SET bookmark = 1 WHERE url_id in (");
sql.append(oss.str());
sql.append(")");
if (!GetDB().Execute(sql.c_str())) {
LOG(ERROR) << GetDB().GetErrorMessage();
return false;
}
return true;
}
bool AndroidCacheDatabase::SetFaviconID(URLID url_id, FaviconID favicon_id) {
sql::Statement update_statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"UPDATE android_cache_db.bookmark_cache "
"SET favicon_id = ? WHERE url_id = ? "));
update_statement.BindInt64(0, favicon_id);
update_statement.BindInt64(1, url_id);
if (!update_statement.Run()) {
LOG(ERROR) << GetDB().GetErrorMessage();
return false;
}
return true;
}
SearchTermID AndroidCacheDatabase::AddSearchTerm(
const string16& term,
const base::Time& last_visit_time) {
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"INSERT INTO android_cache_db.search_terms (search, "
"date) VALUES (?, ?)"));
statement.BindString16(0, term);
statement.BindInt64(1, ToDatabaseTime(last_visit_time));
if (!statement.Run()) {
LOG(ERROR) << GetDB().GetErrorMessage();
return 0;
}
return GetDB().GetLastInsertRowId();
}
bool AndroidCacheDatabase::UpdateSearchTerm(SearchTermID id,
const SearchTermRow& row) {
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"UPDATE android_cache_db.search_terms "
"SET search = ?, date = ? "
"WHERE _id = ?"
));
statement.BindString16(0, row.term);
statement.BindInt64(1, ToDatabaseTime(row.last_visit_time));
statement.BindInt64(2, id);
return statement.Run();
}
SearchTermID AndroidCacheDatabase::GetSearchTerm(const string16& term,
SearchTermRow* row) {
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"SELECT _id, search, date "
"FROM android_cache_db.search_terms "
"WHERE search = ?"
));
if (!statement.is_valid()) {
LOG(ERROR) << GetDB().GetErrorMessage();
return 0;
}
statement.BindString16(0, term);
if (!statement.Step())
return 0;
if (row) {
row->id = statement.ColumnInt64(0);
row->term = statement.ColumnString16(1);
row->last_visit_time = FromDatabaseTime(statement.ColumnInt64(2));
}
return statement.ColumnInt64(0);
}
bool AndroidCacheDatabase::DeleteUnusedSearchTerms() {
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"DELETE FROM android_cache_db.search_terms "
"WHERE search NOT IN (SELECT DISTINCT term FROM keyword_search_terms)"
));
if (!statement.is_valid())
return false;
return statement.Run();
}
bool AndroidCacheDatabase::CreateDatabase(const FilePath& db_name) {
db_name_ = db_name;
if (file_util::PathExists(db_name_))
file_util::Delete(db_name_, false);
// Using a new connection, otherwise we can not create the database.
sql::Connection connection;
// The db doesn't store too much data, so we don't need that big a page
// size or cache.
connection.set_page_size(2048);
connection.set_cache_size(32);
// Run the database in exclusive mode. Nobody else should be accessing the
// database while we're running, and this will give somewhat improved perf.
connection.set_exclusive_locking();
if (!connection.Open(db_name_)) {
LOG(ERROR) << connection.GetErrorMessage();
return false;
}
connection.Close();
return true;
}
bool AndroidCacheDatabase::CreateBookmarkCacheTable() {
const char* name = "android_cache_db.bookmark_cache";
DCHECK(!GetDB().DoesTableExist(name));
std::string sql;
sql.append("CREATE TABLE ");
sql.append(name);
sql.append("("
"id INTEGER PRIMARY KEY,"
"created_time INTEGER NOT NULL," // Time in millisecond.
"last_visit_time INTEGER NOT NULL," // Time in millisecond.
"url_id INTEGER NOT NULL," // url id in urls table.
"favicon_id INTEGER DEFAULT NULL," // favicon id.
"bookmark INTEGER DEFAULT 0" // whether is bookmark.
")");
if (!GetDB().Execute(sql.c_str())) {
LOG(ERROR) << GetDB().GetErrorMessage();
return false;
}
sql.assign("CREATE INDEX ");
sql.append("android_cache_db.bookmark_cache_url_id_idx ON "
"bookmark_cache(url_id)");
if (!GetDB().Execute(sql.c_str())) {
LOG(ERROR) << GetDB().GetErrorMessage();
return false;
}
return true;
}
bool AndroidCacheDatabase::CreateSearchTermsTable() {
const char* name = "android_cache_db.search_terms";
// The table's column name matchs Android's definition.
std::string sql;
sql.append("CREATE TABLE ");
sql.append(name);
sql.append("("
"_id INTEGER PRIMARY KEY,"
"date INTEGER NOT NULL," // last visit time in millisecond.
"search LONGVARCHAR NOT NULL)"); // The actual search term.
if (!GetDB().Execute(sql.c_str())) {
LOG(ERROR) << GetDB().GetErrorMessage();
return false;
}
sql.assign("CREATE INDEX "
"android_cache_db.search_terms_term_idx ON "
"search_terms(search)");
if (!GetDB().Execute(sql.c_str())) {
LOG(ERROR) << GetDB().GetErrorMessage();
return false;
}
return true;
}
bool AndroidCacheDatabase::Attach() {
// Commit all open transactions to make attach succeed.
int transaction_nesting = GetDB().transaction_nesting();
int count = transaction_nesting;
while (count--)
GetDB().CommitTransaction();
bool result = DoAttach();
// No matter whether the attach succeeded or not, we need to create the
// transaction stack again.
count = transaction_nesting;
while (count--)
GetDB().BeginTransaction();
return result;
}
bool AndroidCacheDatabase::DoAttach() {
std::string sql("ATTACH ? AS android_cache_db");
sql::Statement attach(GetDB().GetUniqueStatement(sql.c_str()));
if (!attach.is_valid())
// Keep the transaction open, even though we failed.
return false;
attach.BindString(0, db_name_.value());
if (!attach.Run()) {
LOG(ERROR) << GetDB().GetErrorMessage();
return false;
}
return true;
}
} // namespace history
|