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 "chrome/browser/history/visit_database.h" | 5 #include "chrome/browser/history/visit_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 bool VisitDatabase::InitVisitTable() { | 28 bool VisitDatabase::InitVisitTable() { |
29 if (!GetDB().DoesTableExist("visits")) { | 29 if (!GetDB().DoesTableExist("visits")) { |
30 if (!GetDB().Execute("CREATE TABLE visits(" | 30 if (!GetDB().Execute("CREATE TABLE visits(" |
31 "id INTEGER PRIMARY KEY," | 31 "id INTEGER PRIMARY KEY," |
32 "url INTEGER NOT NULL," // key of the URL this corresponds to | 32 "url INTEGER NOT NULL," // key of the URL this corresponds to |
33 "visit_time INTEGER NOT NULL," | 33 "visit_time INTEGER NOT NULL," |
34 "from_visit INTEGER," | 34 "from_visit INTEGER," |
35 "transition INTEGER DEFAULT 0 NOT NULL," | 35 "transition INTEGER DEFAULT 0 NOT NULL," |
36 "segment_id INTEGER," | 36 "segment_id INTEGER," |
37 // True when we have indexed data for this visit. | 37 // Some old DBs may have an "is_indexed" field here, but this is no |
38 "is_indexed BOOLEAN," | 38 // longer used and should NOT be read or written from any longer. |
39 "visit_duration INTEGER DEFAULT 0 NOT NULL)")) | 39 "visit_duration INTEGER DEFAULT 0 NOT NULL)")) |
40 return false; | 40 return false; |
41 } else if (!GetDB().DoesColumnExist("visits", "is_indexed")) { | |
42 // Old versions don't have the is_indexed column, we can just add that and | |
43 // not worry about different database revisions, since old ones will | |
44 // continue to work. | |
45 // | |
46 // TODO(brettw) this should be removed once we think everybody has been | |
47 // updated (added early Mar 2008). | |
48 if (!GetDB().Execute("ALTER TABLE visits ADD COLUMN is_indexed BOOLEAN")) | |
49 return false; | |
50 } | 41 } |
51 | 42 |
52 // Visit source table contains the source information for all the visits. To | 43 // Visit source table contains the source information for all the visits. To |
53 // save space, we do not record those user browsed visits which would be the | 44 // save space, we do not record those user browsed visits which would be the |
54 // majority in this table. Only other sources are recorded. | 45 // majority in this table. Only other sources are recorded. |
55 // Due to the tight relationship between visit_source and visits table, they | 46 // Due to the tight relationship between visit_source and visits table, they |
56 // should be created and dropped at the same time. | 47 // should be created and dropped at the same time. |
57 if (!GetDB().DoesTableExist("visit_source")) { | 48 if (!GetDB().DoesTableExist("visit_source")) { |
58 if (!GetDB().Execute("CREATE TABLE visit_source(" | 49 if (!GetDB().Execute("CREATE TABLE visit_source(" |
59 "id INTEGER PRIMARY KEY,source INTEGER NOT NULL)")) | 50 "id INTEGER PRIMARY KEY,source INTEGER NOT NULL)")) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 82 |
92 // Must be in sync with HISTORY_VISIT_ROW_FIELDS. | 83 // Must be in sync with HISTORY_VISIT_ROW_FIELDS. |
93 // static | 84 // static |
94 void VisitDatabase::FillVisitRow(sql::Statement& statement, VisitRow* visit) { | 85 void VisitDatabase::FillVisitRow(sql::Statement& statement, VisitRow* visit) { |
95 visit->visit_id = statement.ColumnInt64(0); | 86 visit->visit_id = statement.ColumnInt64(0); |
96 visit->url_id = statement.ColumnInt64(1); | 87 visit->url_id = statement.ColumnInt64(1); |
97 visit->visit_time = base::Time::FromInternalValue(statement.ColumnInt64(2)); | 88 visit->visit_time = base::Time::FromInternalValue(statement.ColumnInt64(2)); |
98 visit->referring_visit = statement.ColumnInt64(3); | 89 visit->referring_visit = statement.ColumnInt64(3); |
99 visit->transition = content::PageTransitionFromInt(statement.ColumnInt(4)); | 90 visit->transition = content::PageTransitionFromInt(statement.ColumnInt(4)); |
100 visit->segment_id = statement.ColumnInt64(5); | 91 visit->segment_id = statement.ColumnInt64(5); |
101 visit->is_indexed = !!statement.ColumnInt(6); | |
102 visit->visit_duration = | 92 visit->visit_duration = |
103 base::TimeDelta::FromInternalValue(statement.ColumnInt64(7)); | 93 base::TimeDelta::FromInternalValue(statement.ColumnInt64(6)); |
104 } | 94 } |
105 | 95 |
106 // static | 96 // static |
107 bool VisitDatabase::FillVisitVector(sql::Statement& statement, | 97 bool VisitDatabase::FillVisitVector(sql::Statement& statement, |
108 VisitVector* visits) { | 98 VisitVector* visits) { |
109 if (!statement.is_valid()) | 99 if (!statement.is_valid()) |
110 return false; | 100 return false; |
111 | 101 |
112 while (statement.Step()) { | 102 while (statement.Step()) { |
113 history::VisitRow visit; | 103 history::VisitRow visit; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 if (static_cast<int>(visits->size()) >= options.EffectiveMaxCount()) | 137 if (static_cast<int>(visits->size()) >= options.EffectiveMaxCount()) |
148 return true; | 138 return true; |
149 visits->push_back(visit); | 139 visits->push_back(visit); |
150 } | 140 } |
151 return false; | 141 return false; |
152 } | 142 } |
153 | 143 |
154 VisitID VisitDatabase::AddVisit(VisitRow* visit, VisitSource source) { | 144 VisitID VisitDatabase::AddVisit(VisitRow* visit, VisitSource source) { |
155 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 145 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
156 "INSERT INTO visits " | 146 "INSERT INTO visits " |
157 "(url, visit_time, from_visit, transition, segment_id, is_indexed, " | 147 "(url, visit_time, from_visit, transition, segment_id, " |
158 "visit_duration) VALUES (?,?,?,?,?,?,?)")); | 148 "visit_duration) VALUES (?,?,?,?,?,?)")); |
159 statement.BindInt64(0, visit->url_id); | 149 statement.BindInt64(0, visit->url_id); |
160 statement.BindInt64(1, visit->visit_time.ToInternalValue()); | 150 statement.BindInt64(1, visit->visit_time.ToInternalValue()); |
161 statement.BindInt64(2, visit->referring_visit); | 151 statement.BindInt64(2, visit->referring_visit); |
162 statement.BindInt64(3, visit->transition); | 152 statement.BindInt64(3, visit->transition); |
163 statement.BindInt64(4, visit->segment_id); | 153 statement.BindInt64(4, visit->segment_id); |
164 statement.BindInt64(5, visit->is_indexed); | 154 statement.BindInt64(5, visit->visit_duration.ToInternalValue()); |
165 statement.BindInt64(6, visit->visit_duration.ToInternalValue()); | |
166 | 155 |
167 if (!statement.Run()) { | 156 if (!statement.Run()) { |
168 VLOG(0) << "Failed to execute visit insert statement: " | 157 VLOG(0) << "Failed to execute visit insert statement: " |
169 << "url_id = " << visit->url_id; | 158 << "url_id = " << visit->url_id; |
170 return 0; | 159 return 0; |
171 } | 160 } |
172 | 161 |
173 visit->visit_id = GetDB().GetLastInsertRowId(); | 162 visit->visit_id = GetDB().GetLastInsertRowId(); |
174 | 163 |
175 if (source != SOURCE_BROWSED) { | 164 if (source != SOURCE_BROWSED) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 } | 223 } |
235 | 224 |
236 bool VisitDatabase::UpdateVisitRow(const VisitRow& visit) { | 225 bool VisitDatabase::UpdateVisitRow(const VisitRow& visit) { |
237 // Don't store inconsistent data to the database. | 226 // Don't store inconsistent data to the database. |
238 DCHECK_NE(visit.visit_id, visit.referring_visit); | 227 DCHECK_NE(visit.visit_id, visit.referring_visit); |
239 if (visit.visit_id == visit.referring_visit) | 228 if (visit.visit_id == visit.referring_visit) |
240 return false; | 229 return false; |
241 | 230 |
242 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 231 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
243 "UPDATE visits SET " | 232 "UPDATE visits SET " |
244 "url=?,visit_time=?,from_visit=?,transition=?,segment_id=?,is_indexed=?," | 233 "url=?,visit_time=?,from_visit=?,transition=?,segment_id=?," |
245 "visit_duration=? WHERE id=?")); | 234 "visit_duration=? WHERE id=?")); |
246 statement.BindInt64(0, visit.url_id); | 235 statement.BindInt64(0, visit.url_id); |
247 statement.BindInt64(1, visit.visit_time.ToInternalValue()); | 236 statement.BindInt64(1, visit.visit_time.ToInternalValue()); |
248 statement.BindInt64(2, visit.referring_visit); | 237 statement.BindInt64(2, visit.referring_visit); |
249 statement.BindInt64(3, visit.transition); | 238 statement.BindInt64(3, visit.transition); |
250 statement.BindInt64(4, visit.segment_id); | 239 statement.BindInt64(4, visit.segment_id); |
251 statement.BindInt64(5, visit.is_indexed); | 240 statement.BindInt64(5, visit.visit_duration.ToInternalValue()); |
252 statement.BindInt64(6, visit.visit_duration.ToInternalValue()); | 241 statement.BindInt64(6, visit.visit_id); |
253 statement.BindInt64(7, visit.visit_id); | |
254 | 242 |
255 return statement.Run(); | 243 return statement.Run(); |
256 } | 244 } |
257 | 245 |
258 bool VisitDatabase::GetVisitsForURL(URLID url_id, VisitVector* visits) { | 246 bool VisitDatabase::GetVisitsForURL(URLID url_id, VisitVector* visits) { |
259 visits->clear(); | 247 visits->clear(); |
260 | 248 |
261 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 249 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
262 "SELECT" HISTORY_VISIT_ROW_FIELDS | 250 "SELECT" HISTORY_VISIT_ROW_FIELDS |
263 "FROM visits " | 251 "FROM visits " |
264 "WHERE url=? " | 252 "WHERE url=? " |
265 "ORDER BY visit_time ASC")); | 253 "ORDER BY visit_time ASC")); |
266 statement.BindInt64(0, url_id); | 254 statement.BindInt64(0, url_id); |
267 return FillVisitVector(statement, visits); | 255 return FillVisitVector(statement, visits); |
268 } | 256 } |
269 | 257 |
270 bool VisitDatabase::GetIndexedVisitsForURL(URLID url_id, VisitVector* visits) { | |
271 visits->clear(); | |
272 | |
273 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
274 "SELECT" HISTORY_VISIT_ROW_FIELDS | |
275 "FROM visits " | |
276 "WHERE url=? AND is_indexed=1")); | |
277 statement.BindInt64(0, url_id); | |
278 return FillVisitVector(statement, visits); | |
279 } | |
280 | |
281 bool VisitDatabase::GetVisitsForURLWithOptions(URLID url_id, | 258 bool VisitDatabase::GetVisitsForURLWithOptions(URLID url_id, |
282 const QueryOptions& options, | 259 const QueryOptions& options, |
283 VisitVector* visits) { | 260 VisitVector* visits) { |
284 visits->clear(); | 261 visits->clear(); |
285 | 262 |
286 if (options.REMOVE_ALL_DUPLICATES) { | 263 if (options.REMOVE_ALL_DUPLICATES) { |
287 VisitRow visit_row; | 264 VisitRow visit_row; |
288 VisitID visit_id = GetMostRecentVisitForURL(url_id, &visit_row); | 265 VisitID visit_id = GetMostRecentVisitForURL(url_id, &visit_row); |
289 if (visit_id && options.EffectiveMaxCount() != 0) { | 266 if (visit_id && options.EffectiveMaxCount() != 0) { |
290 visits->push_back(visit_row); | 267 visits->push_back(visit_row); |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 while (statement.Step()) { | 616 while (statement.Step()) { |
640 BriefVisitInfo info; | 617 BriefVisitInfo info; |
641 info.url_id = statement.ColumnInt64(0); | 618 info.url_id = statement.ColumnInt64(0); |
642 info.time = base::Time::FromInternalValue(statement.ColumnInt64(1)); | 619 info.time = base::Time::FromInternalValue(statement.ColumnInt64(1)); |
643 info.transition = content::PageTransitionFromInt(statement.ColumnInt(2)); | 620 info.transition = content::PageTransitionFromInt(statement.ColumnInt(2)); |
644 result_vector->push_back(info); | 621 result_vector->push_back(info); |
645 } | 622 } |
646 } | 623 } |
647 | 624 |
648 } // namespace history | 625 } // namespace history |
OLD | NEW |