Chromium Code Reviews| 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 // TODO(rmcilroy): remove unused is_indexed field. |
|
Scott Hess - ex-Googler
2013/06/14 19:39:56
Removing involves copying the entire table over.
rmcilroy
2013/06/17 14:11:49
Ack.
| |
| 38 "is_indexed BOOLEAN," | 38 "is_indexed BOOLEAN," |
| 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")) { | 41 } else if (!GetDB().DoesColumnExist("visits", "is_indexed")) { |
| 42 // Old versions don't have the is_indexed column, we can just add that and | 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 | 43 // not worry about different database revisions, since old ones will |
| 44 // continue to work. | 44 // continue to work. |
| 45 // | 45 // |
| 46 // TODO(brettw) this should be removed once we think everybody has been | 46 // TODO(brettw) this should be removed once we think everybody has been |
| 47 // updated (added early Mar 2008). | 47 // updated (added early Mar 2008). |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 | 91 |
| 92 // Must be in sync with HISTORY_VISIT_ROW_FIELDS. | 92 // Must be in sync with HISTORY_VISIT_ROW_FIELDS. |
| 93 // static | 93 // static |
| 94 void VisitDatabase::FillVisitRow(sql::Statement& statement, VisitRow* visit) { | 94 void VisitDatabase::FillVisitRow(sql::Statement& statement, VisitRow* visit) { |
| 95 visit->visit_id = statement.ColumnInt64(0); | 95 visit->visit_id = statement.ColumnInt64(0); |
| 96 visit->url_id = statement.ColumnInt64(1); | 96 visit->url_id = statement.ColumnInt64(1); |
| 97 visit->visit_time = base::Time::FromInternalValue(statement.ColumnInt64(2)); | 97 visit->visit_time = base::Time::FromInternalValue(statement.ColumnInt64(2)); |
| 98 visit->referring_visit = statement.ColumnInt64(3); | 98 visit->referring_visit = statement.ColumnInt64(3); |
| 99 visit->transition = content::PageTransitionFromInt(statement.ColumnInt(4)); | 99 visit->transition = content::PageTransitionFromInt(statement.ColumnInt(4)); |
| 100 visit->segment_id = statement.ColumnInt64(5); | 100 visit->segment_id = statement.ColumnInt64(5); |
| 101 visit->is_indexed = !!statement.ColumnInt(6); | |
| 102 visit->visit_duration = | 101 visit->visit_duration = |
| 103 base::TimeDelta::FromInternalValue(statement.ColumnInt64(7)); | 102 base::TimeDelta::FromInternalValue(statement.ColumnInt64(7)); |
| 104 } | 103 } |
| 105 | 104 |
| 106 // static | 105 // static |
| 107 bool VisitDatabase::FillVisitVector(sql::Statement& statement, | 106 bool VisitDatabase::FillVisitVector(sql::Statement& statement, |
| 108 VisitVector* visits) { | 107 VisitVector* visits) { |
| 109 if (!statement.is_valid()) | 108 if (!statement.is_valid()) |
| 110 return false; | 109 return false; |
| 111 | 110 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 if (static_cast<int>(visits->size()) >= options.EffectiveMaxCount()) | 146 if (static_cast<int>(visits->size()) >= options.EffectiveMaxCount()) |
| 148 return true; | 147 return true; |
| 149 visits->push_back(visit); | 148 visits->push_back(visit); |
| 150 } | 149 } |
| 151 return false; | 150 return false; |
| 152 } | 151 } |
| 153 | 152 |
| 154 VisitID VisitDatabase::AddVisit(VisitRow* visit, VisitSource source) { | 153 VisitID VisitDatabase::AddVisit(VisitRow* visit, VisitSource source) { |
| 155 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 154 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 156 "INSERT INTO visits " | 155 "INSERT INTO visits " |
| 157 "(url, visit_time, from_visit, transition, segment_id, is_indexed, " | 156 "(url, visit_time, from_visit, transition, segment_id, " |
| 158 "visit_duration) VALUES (?,?,?,?,?,?,?)")); | 157 "visit_duration) VALUES (?,?,?,?,?,?)")); |
| 159 statement.BindInt64(0, visit->url_id); | 158 statement.BindInt64(0, visit->url_id); |
| 160 statement.BindInt64(1, visit->visit_time.ToInternalValue()); | 159 statement.BindInt64(1, visit->visit_time.ToInternalValue()); |
| 161 statement.BindInt64(2, visit->referring_visit); | 160 statement.BindInt64(2, visit->referring_visit); |
| 162 statement.BindInt64(3, visit->transition); | 161 statement.BindInt64(3, visit->transition); |
| 163 statement.BindInt64(4, visit->segment_id); | 162 statement.BindInt64(4, visit->segment_id); |
| 164 statement.BindInt64(5, visit->is_indexed); | 163 statement.BindInt64(5, visit->visit_duration.ToInternalValue()); |
| 165 statement.BindInt64(6, visit->visit_duration.ToInternalValue()); | |
| 166 | 164 |
| 167 if (!statement.Run()) { | 165 if (!statement.Run()) { |
| 168 VLOG(0) << "Failed to execute visit insert statement: " | 166 VLOG(0) << "Failed to execute visit insert statement: " |
| 169 << "url_id = " << visit->url_id; | 167 << "url_id = " << visit->url_id; |
| 170 return 0; | 168 return 0; |
| 171 } | 169 } |
| 172 | 170 |
| 173 visit->visit_id = GetDB().GetLastInsertRowId(); | 171 visit->visit_id = GetDB().GetLastInsertRowId(); |
| 174 | 172 |
| 175 if (source != SOURCE_BROWSED) { | 173 if (source != SOURCE_BROWSED) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 } | 232 } |
| 235 | 233 |
| 236 bool VisitDatabase::UpdateVisitRow(const VisitRow& visit) { | 234 bool VisitDatabase::UpdateVisitRow(const VisitRow& visit) { |
| 237 // Don't store inconsistent data to the database. | 235 // Don't store inconsistent data to the database. |
| 238 DCHECK_NE(visit.visit_id, visit.referring_visit); | 236 DCHECK_NE(visit.visit_id, visit.referring_visit); |
| 239 if (visit.visit_id == visit.referring_visit) | 237 if (visit.visit_id == visit.referring_visit) |
| 240 return false; | 238 return false; |
| 241 | 239 |
| 242 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 240 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 243 "UPDATE visits SET " | 241 "UPDATE visits SET " |
| 244 "url=?,visit_time=?,from_visit=?,transition=?,segment_id=?,is_indexed=?," | 242 "url=?,visit_time=?,from_visit=?,transition=?,segment_id=?," |
| 245 "visit_duration=? WHERE id=?")); | 243 "visit_duration=? WHERE id=?")); |
| 246 statement.BindInt64(0, visit.url_id); | 244 statement.BindInt64(0, visit.url_id); |
| 247 statement.BindInt64(1, visit.visit_time.ToInternalValue()); | 245 statement.BindInt64(1, visit.visit_time.ToInternalValue()); |
| 248 statement.BindInt64(2, visit.referring_visit); | 246 statement.BindInt64(2, visit.referring_visit); |
| 249 statement.BindInt64(3, visit.transition); | 247 statement.BindInt64(3, visit.transition); |
| 250 statement.BindInt64(4, visit.segment_id); | 248 statement.BindInt64(4, visit.segment_id); |
| 251 statement.BindInt64(5, visit.is_indexed); | 249 statement.BindInt64(5, visit.visit_duration.ToInternalValue()); |
| 252 statement.BindInt64(6, visit.visit_duration.ToInternalValue()); | 250 statement.BindInt64(6, visit.visit_id); |
| 253 statement.BindInt64(7, visit.visit_id); | |
| 254 | 251 |
| 255 return statement.Run(); | 252 return statement.Run(); |
| 256 } | 253 } |
| 257 | 254 |
| 258 bool VisitDatabase::GetVisitsForURL(URLID url_id, VisitVector* visits) { | 255 bool VisitDatabase::GetVisitsForURL(URLID url_id, VisitVector* visits) { |
| 259 visits->clear(); | 256 visits->clear(); |
| 260 | 257 |
| 261 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 258 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 262 "SELECT" HISTORY_VISIT_ROW_FIELDS | 259 "SELECT" HISTORY_VISIT_ROW_FIELDS |
| 263 "FROM visits " | 260 "FROM visits " |
| 264 "WHERE url=? " | 261 "WHERE url=? " |
| 265 "ORDER BY visit_time ASC")); | 262 "ORDER BY visit_time ASC")); |
| 266 statement.BindInt64(0, url_id); | 263 statement.BindInt64(0, url_id); |
| 267 return FillVisitVector(statement, visits); | 264 return FillVisitVector(statement, visits); |
| 268 } | 265 } |
| 269 | 266 |
| 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, | 267 bool VisitDatabase::GetVisitsForURLWithOptions(URLID url_id, |
| 282 const QueryOptions& options, | 268 const QueryOptions& options, |
| 283 VisitVector* visits) { | 269 VisitVector* visits) { |
| 284 visits->clear(); | 270 visits->clear(); |
| 285 | 271 |
| 286 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 272 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 287 "SELECT" HISTORY_VISIT_ROW_FIELDS | 273 "SELECT" HISTORY_VISIT_ROW_FIELDS |
| 288 "FROM visits " | 274 "FROM visits " |
| 289 "WHERE url=? AND visit_time >= ? AND visit_time < ? " | 275 "WHERE url=? AND visit_time >= ? AND visit_time < ? " |
| 290 "ORDER BY visit_time ASC")); | 276 "ORDER BY visit_time ASC")); |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 630 while (statement.Step()) { | 616 while (statement.Step()) { |
| 631 BriefVisitInfo info; | 617 BriefVisitInfo info; |
| 632 info.url_id = statement.ColumnInt64(0); | 618 info.url_id = statement.ColumnInt64(0); |
| 633 info.time = base::Time::FromInternalValue(statement.ColumnInt64(1)); | 619 info.time = base::Time::FromInternalValue(statement.ColumnInt64(1)); |
| 634 info.transition = content::PageTransitionFromInt(statement.ColumnInt(2)); | 620 info.transition = content::PageTransitionFromInt(statement.ColumnInt(2)); |
| 635 result_vector->push_back(info); | 621 result_vector->push_back(info); |
| 636 } | 622 } |
| 637 } | 623 } |
| 638 | 624 |
| 639 } // namespace history | 625 } // namespace history |
| OLD | NEW |