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 "is_indexed BOOLEAN," // unused field - kept to avoid recreating DB. | 
| 
 
brettw
2013/07/17 17:39:56
We should be able to remove this line and tolerate
 
rmcilroy
2013/07/18 11:42:52
Done.
 
 | |
| 38 "is_indexed BOOLEAN," | |
| 39 "visit_duration INTEGER DEFAULT 0 NOT NULL)")) | 38 "visit_duration INTEGER DEFAULT 0 NOT NULL)")) | 
| 40 return false; | 39 return false; | 
| 41 } else if (!GetDB().DoesColumnExist("visits", "is_indexed")) { | 40 } else if (!GetDB().DoesColumnExist("visits", "is_indexed")) { | 
| 
 
brettw
2013/07/17 17:39:56
They we can also remove this block.
 
rmcilroy
2013/07/18 11:42:52
Done.
 
 | |
| 42 // Old versions don't have the is_indexed column, we can just add that and | 41 // 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 | 42 // not worry about different database revisions, since old ones will | 
| 44 // continue to work. | 43 // continue to work. | 
| 45 // | 44 // | 
| 46 // TODO(brettw) this should be removed once we think everybody has been | 45 // TODO(brettw) this should be removed once we think everybody has been | 
| 47 // updated (added early Mar 2008). | 46 // updated (added early Mar 2008). | 
| 48 if (!GetDB().Execute("ALTER TABLE visits ADD COLUMN is_indexed BOOLEAN")) | 47 if (!GetDB().Execute("ALTER TABLE visits ADD COLUMN is_indexed BOOLEAN")) | 
| 49 return false; | 48 return false; | 
| 50 } | 49 } | 
| 51 | 50 | 
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 | 90 | 
| 92 // Must be in sync with HISTORY_VISIT_ROW_FIELDS. | 91 // Must be in sync with HISTORY_VISIT_ROW_FIELDS. | 
| 93 // static | 92 // static | 
| 94 void VisitDatabase::FillVisitRow(sql::Statement& statement, VisitRow* visit) { | 93 void VisitDatabase::FillVisitRow(sql::Statement& statement, VisitRow* visit) { | 
| 95 visit->visit_id = statement.ColumnInt64(0); | 94 visit->visit_id = statement.ColumnInt64(0); | 
| 96 visit->url_id = statement.ColumnInt64(1); | 95 visit->url_id = statement.ColumnInt64(1); | 
| 97 visit->visit_time = base::Time::FromInternalValue(statement.ColumnInt64(2)); | 96 visit->visit_time = base::Time::FromInternalValue(statement.ColumnInt64(2)); | 
| 98 visit->referring_visit = statement.ColumnInt64(3); | 97 visit->referring_visit = statement.ColumnInt64(3); | 
| 99 visit->transition = content::PageTransitionFromInt(statement.ColumnInt(4)); | 98 visit->transition = content::PageTransitionFromInt(statement.ColumnInt(4)); | 
| 100 visit->segment_id = statement.ColumnInt64(5); | 99 visit->segment_id = statement.ColumnInt64(5); | 
| 101 visit->is_indexed = !!statement.ColumnInt(6); | |
| 102 visit->visit_duration = | 100 visit->visit_duration = | 
| 103 base::TimeDelta::FromInternalValue(statement.ColumnInt64(7)); | 101 base::TimeDelta::FromInternalValue(statement.ColumnInt64(7)); | 
| 104 } | 102 } | 
| 105 | 103 | 
| 106 // static | 104 // static | 
| 107 bool VisitDatabase::FillVisitVector(sql::Statement& statement, | 105 bool VisitDatabase::FillVisitVector(sql::Statement& statement, | 
| 108 VisitVector* visits) { | 106 VisitVector* visits) { | 
| 109 if (!statement.is_valid()) | 107 if (!statement.is_valid()) | 
| 110 return false; | 108 return false; | 
| 111 | 109 | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 if (static_cast<int>(visits->size()) >= options.EffectiveMaxCount()) | 145 if (static_cast<int>(visits->size()) >= options.EffectiveMaxCount()) | 
| 148 return true; | 146 return true; | 
| 149 visits->push_back(visit); | 147 visits->push_back(visit); | 
| 150 } | 148 } | 
| 151 return false; | 149 return false; | 
| 152 } | 150 } | 
| 153 | 151 | 
| 154 VisitID VisitDatabase::AddVisit(VisitRow* visit, VisitSource source) { | 152 VisitID VisitDatabase::AddVisit(VisitRow* visit, VisitSource source) { | 
| 155 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 153 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 
| 156 "INSERT INTO visits " | 154 "INSERT INTO visits " | 
| 157 "(url, visit_time, from_visit, transition, segment_id, is_indexed, " | 155 "(url, visit_time, from_visit, transition, segment_id, " | 
| 158 "visit_duration) VALUES (?,?,?,?,?,?,?)")); | 156 "visit_duration) VALUES (?,?,?,?,?,?)")); | 
| 159 statement.BindInt64(0, visit->url_id); | 157 statement.BindInt64(0, visit->url_id); | 
| 160 statement.BindInt64(1, visit->visit_time.ToInternalValue()); | 158 statement.BindInt64(1, visit->visit_time.ToInternalValue()); | 
| 161 statement.BindInt64(2, visit->referring_visit); | 159 statement.BindInt64(2, visit->referring_visit); | 
| 162 statement.BindInt64(3, visit->transition); | 160 statement.BindInt64(3, visit->transition); | 
| 163 statement.BindInt64(4, visit->segment_id); | 161 statement.BindInt64(4, visit->segment_id); | 
| 164 statement.BindInt64(5, visit->is_indexed); | 162 statement.BindInt64(5, visit->visit_duration.ToInternalValue()); | 
| 165 statement.BindInt64(6, visit->visit_duration.ToInternalValue()); | |
| 166 | 163 | 
| 167 if (!statement.Run()) { | 164 if (!statement.Run()) { | 
| 168 VLOG(0) << "Failed to execute visit insert statement: " | 165 VLOG(0) << "Failed to execute visit insert statement: " | 
| 169 << "url_id = " << visit->url_id; | 166 << "url_id = " << visit->url_id; | 
| 170 return 0; | 167 return 0; | 
| 171 } | 168 } | 
| 172 | 169 | 
| 173 visit->visit_id = GetDB().GetLastInsertRowId(); | 170 visit->visit_id = GetDB().GetLastInsertRowId(); | 
| 174 | 171 | 
| 175 if (source != SOURCE_BROWSED) { | 172 if (source != SOURCE_BROWSED) { | 
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 } | 231 } | 
| 235 | 232 | 
| 236 bool VisitDatabase::UpdateVisitRow(const VisitRow& visit) { | 233 bool VisitDatabase::UpdateVisitRow(const VisitRow& visit) { | 
| 237 // Don't store inconsistent data to the database. | 234 // Don't store inconsistent data to the database. | 
| 238 DCHECK_NE(visit.visit_id, visit.referring_visit); | 235 DCHECK_NE(visit.visit_id, visit.referring_visit); | 
| 239 if (visit.visit_id == visit.referring_visit) | 236 if (visit.visit_id == visit.referring_visit) | 
| 240 return false; | 237 return false; | 
| 241 | 238 | 
| 242 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 239 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 
| 243 "UPDATE visits SET " | 240 "UPDATE visits SET " | 
| 244 "url=?,visit_time=?,from_visit=?,transition=?,segment_id=?,is_indexed=?," | 241 "url=?,visit_time=?,from_visit=?,transition=?,segment_id=?," | 
| 245 "visit_duration=? WHERE id=?")); | 242 "visit_duration=? WHERE id=?")); | 
| 246 statement.BindInt64(0, visit.url_id); | 243 statement.BindInt64(0, visit.url_id); | 
| 247 statement.BindInt64(1, visit.visit_time.ToInternalValue()); | 244 statement.BindInt64(1, visit.visit_time.ToInternalValue()); | 
| 248 statement.BindInt64(2, visit.referring_visit); | 245 statement.BindInt64(2, visit.referring_visit); | 
| 249 statement.BindInt64(3, visit.transition); | 246 statement.BindInt64(3, visit.transition); | 
| 250 statement.BindInt64(4, visit.segment_id); | 247 statement.BindInt64(4, visit.segment_id); | 
| 251 statement.BindInt64(5, visit.is_indexed); | 248 statement.BindInt64(5, visit.visit_duration.ToInternalValue()); | 
| 252 statement.BindInt64(6, visit.visit_duration.ToInternalValue()); | 249 statement.BindInt64(6, visit.visit_id); | 
| 253 statement.BindInt64(7, visit.visit_id); | |
| 254 | 250 | 
| 255 return statement.Run(); | 251 return statement.Run(); | 
| 256 } | 252 } | 
| 257 | 253 | 
| 258 bool VisitDatabase::GetVisitsForURL(URLID url_id, VisitVector* visits) { | 254 bool VisitDatabase::GetVisitsForURL(URLID url_id, VisitVector* visits) { | 
| 259 visits->clear(); | 255 visits->clear(); | 
| 260 | 256 | 
| 261 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 257 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 
| 262 "SELECT" HISTORY_VISIT_ROW_FIELDS | 258 "SELECT" HISTORY_VISIT_ROW_FIELDS | 
| 263 "FROM visits " | 259 "FROM visits " | 
| 264 "WHERE url=? " | 260 "WHERE url=? " | 
| 265 "ORDER BY visit_time ASC")); | 261 "ORDER BY visit_time ASC")); | 
| 266 statement.BindInt64(0, url_id); | 262 statement.BindInt64(0, url_id); | 
| 267 return FillVisitVector(statement, visits); | 263 return FillVisitVector(statement, visits); | 
| 268 } | 264 } | 
| 269 | 265 | 
| 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, | 266 bool VisitDatabase::GetVisitsForURLWithOptions(URLID url_id, | 
| 282 const QueryOptions& options, | 267 const QueryOptions& options, | 
| 283 VisitVector* visits) { | 268 VisitVector* visits) { | 
| 284 visits->clear(); | 269 visits->clear(); | 
| 285 | 270 | 
| 286 if (options.REMOVE_ALL_DUPLICATES) { | 271 if (options.REMOVE_ALL_DUPLICATES) { | 
| 287 VisitRow visit_row; | 272 VisitRow visit_row; | 
| 288 VisitID visit_id = GetMostRecentVisitForURL(url_id, &visit_row); | 273 VisitID visit_id = GetMostRecentVisitForURL(url_id, &visit_row); | 
| 289 if (visit_id && options.EffectiveMaxCount() != 0) { | 274 if (visit_id && options.EffectiveMaxCount() != 0) { | 
| 290 visits->push_back(visit_row); | 275 visits->push_back(visit_row); | 
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 639 while (statement.Step()) { | 624 while (statement.Step()) { | 
| 640 BriefVisitInfo info; | 625 BriefVisitInfo info; | 
| 641 info.url_id = statement.ColumnInt64(0); | 626 info.url_id = statement.ColumnInt64(0); | 
| 642 info.time = base::Time::FromInternalValue(statement.ColumnInt64(1)); | 627 info.time = base::Time::FromInternalValue(statement.ColumnInt64(1)); | 
| 643 info.transition = content::PageTransitionFromInt(statement.ColumnInt(2)); | 628 info.transition = content::PageTransitionFromInt(statement.ColumnInt(2)); | 
| 644 result_vector->push_back(info); | 629 result_vector->push_back(info); | 
| 645 } | 630 } | 
| 646 } | 631 } | 
| 647 | 632 | 
| 648 } // namespace history | 633 } // namespace history | 
| OLD | NEW |