| 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/url_database.h" | 5 #include "chrome/browser/history/url_database.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/history/url_database_observer.h" |
| 14 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 15 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 18 | 19 |
| 19 namespace history { | 20 namespace history { |
| 20 | 21 |
| 21 const char URLDatabase::kURLRowFields[] = HISTORY_URL_ROW_FIELDS; | 22 const char URLDatabase::kURLRowFields[] = HISTORY_URL_ROW_FIELDS; |
| 22 const int URLDatabase::kNumURLRowFields = 9; | 23 const int URLDatabase::kNumURLRowFields = 9; |
| 23 | 24 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 if (!statement.Step()) | 121 if (!statement.Step()) |
| 121 return 0; // no data | 122 return 0; // no data |
| 122 | 123 |
| 123 if (info) | 124 if (info) |
| 124 FillURLRow(statement, info); | 125 FillURLRow(statement, info); |
| 125 return statement.ColumnInt64(0); | 126 return statement.ColumnInt64(0); |
| 126 } | 127 } |
| 127 | 128 |
| 128 bool URLDatabase::UpdateURLRow(URLID url_id, | 129 bool URLDatabase::UpdateURLRow(URLID url_id, |
| 129 const history::URLRow& info) { | 130 const history::URLRow& info) { |
| 131 URLRow old; |
| 132 if (!GetURLRow(url_id, &old)) |
| 133 return false; |
| 134 |
| 130 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 135 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 131 "UPDATE urls SET title=?,visit_count=?,typed_count=?,last_visit_time=?," | 136 "UPDATE urls SET title=?,visit_count=?,typed_count=?,last_visit_time=?," |
| 132 "hidden=?" | 137 "hidden=?" |
| 133 "WHERE id=?")); | 138 "WHERE id=?")); |
| 134 statement.BindString16(0, info.title()); | 139 statement.BindString16(0, info.title()); |
| 135 statement.BindInt(1, info.visit_count()); | 140 statement.BindInt(1, info.visit_count()); |
| 136 statement.BindInt(2, info.typed_count()); | 141 statement.BindInt(2, info.typed_count()); |
| 137 statement.BindInt64(3, info.last_visit().ToInternalValue()); | 142 statement.BindInt64(3, info.last_visit().ToInternalValue()); |
| 138 statement.BindInt(4, info.hidden() ? 1 : 0); | 143 statement.BindInt(4, info.hidden() ? 1 : 0); |
| 139 statement.BindInt64(5, url_id); | 144 statement.BindInt64(5, url_id); |
| 140 | 145 |
| 141 return statement.Run(); | 146 if(!statement.Run()) |
| 147 return false; |
| 148 |
| 149 URLRow cur; |
| 150 if (!GetURLRow(url_id, &cur)) |
| 151 return false; |
| 152 |
| 153 FOR_EACH_OBSERVER(URLDatabaseObserver, observers_, URLChanged(old, cur)); |
| 154 |
| 155 return true; |
| 142 } | 156 } |
| 143 | 157 |
| 144 URLID URLDatabase::AddURLInternal(const history::URLRow& info, | 158 URLID URLDatabase::AddURLInternal(const history::URLRow& info, |
| 145 bool is_temporary) { | 159 bool is_temporary) { |
| 146 // This function is used to insert into two different tables, so we have to | 160 // This function is used to insert into two different tables, so we have to |
| 147 // do some shuffling. Unfortinately, we can't use the macro | 161 // do some shuffling. Unfortinately, we can't use the macro |
| 148 // HISTORY_URL_ROW_FIELDS because that specifies the table name which is | 162 // HISTORY_URL_ROW_FIELDS because that specifies the table name which is |
| 149 // invalid in the insert syntax. | 163 // invalid in the insert syntax. |
| 150 #define ADDURL_COMMON_SUFFIX \ | 164 #define ADDURL_COMMON_SUFFIX \ |
| 151 " (url, title, visit_count, typed_count, "\ | 165 " (url, title, visit_count, typed_count, "\ |
| (...skipping 17 matching lines...) Expand all Loading... |
| 169 statement.BindInt(2, info.visit_count()); | 183 statement.BindInt(2, info.visit_count()); |
| 170 statement.BindInt(3, info.typed_count()); | 184 statement.BindInt(3, info.typed_count()); |
| 171 statement.BindInt64(4, info.last_visit().ToInternalValue()); | 185 statement.BindInt64(4, info.last_visit().ToInternalValue()); |
| 172 statement.BindInt(5, info.hidden() ? 1 : 0); | 186 statement.BindInt(5, info.hidden() ? 1 : 0); |
| 173 | 187 |
| 174 if (!statement.Run()) { | 188 if (!statement.Run()) { |
| 175 VLOG(0) << "Failed to add url " << info.url().possibly_invalid_spec() | 189 VLOG(0) << "Failed to add url " << info.url().possibly_invalid_spec() |
| 176 << " to table history.urls."; | 190 << " to table history.urls."; |
| 177 return 0; | 191 return 0; |
| 178 } | 192 } |
| 193 |
| 194 if (!is_temporary) |
| 195 FOR_EACH_OBSERVER(URLDatabaseObserver, observers_, URLAdded(info)); |
| 196 |
| 179 return GetDB().GetLastInsertRowId(); | 197 return GetDB().GetLastInsertRowId(); |
| 180 } | 198 } |
| 181 | 199 |
| 182 bool URLDatabase::DeleteURLRow(URLID id) { | 200 bool URLDatabase::DeleteURLRow(URLID id) { |
| 201 URLRow info; |
| 202 if (!GetURLRow(id, &info)) |
| 203 return false; |
| 204 |
| 183 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 205 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 184 "DELETE FROM urls WHERE id = ?")); | 206 "DELETE FROM urls WHERE id = ?")); |
| 185 statement.BindInt64(0, id); | 207 statement.BindInt64(0, id); |
| 186 | 208 |
| 187 if (!statement.Run()) | 209 if (!statement.Run()) |
| 188 return false; | 210 return false; |
| 189 | 211 |
| 212 FOR_EACH_OBSERVER(URLDatabaseObserver, observers_, URLDeleted(info)); |
| 213 |
| 190 // And delete any keyword visits. | 214 // And delete any keyword visits. |
| 191 if (!has_keyword_search_terms_) | 215 if (!has_keyword_search_terms_) |
| 192 return true; | 216 return true; |
| 193 | 217 |
| 194 sql::Statement del_keyword_visit(GetDB().GetCachedStatement(SQL_FROM_HERE, | 218 sql::Statement del_keyword_visit(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 195 "DELETE FROM keyword_search_terms WHERE url_id=?")); | 219 "DELETE FROM keyword_search_terms WHERE url_id=?")); |
| 196 del_keyword_visit.BindInt64(0, id); | 220 del_keyword_visit.BindInt64(0, id); |
| 197 | 221 |
| 198 return del_keyword_visit.Run(); | 222 return del_keyword_visit.Run(); |
| 199 } | 223 } |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 | 618 |
| 595 return GetDB().Execute(sql.c_str()); | 619 return GetDB().Execute(sql.c_str()); |
| 596 } | 620 } |
| 597 | 621 |
| 598 bool URLDatabase::CreateMainURLIndex() { | 622 bool URLDatabase::CreateMainURLIndex() { |
| 599 // Index over URLs so we can quickly look up based on URL. | 623 // Index over URLs so we can quickly look up based on URL. |
| 600 return GetDB().Execute( | 624 return GetDB().Execute( |
| 601 "CREATE INDEX IF NOT EXISTS urls_url_index ON urls (url)"); | 625 "CREATE INDEX IF NOT EXISTS urls_url_index ON urls (url)"); |
| 602 } | 626 } |
| 603 | 627 |
| 628 void URLDatabase::AddObserver(URLDatabaseObserver* observer) { |
| 629 observers_.AddObserver(observer); |
| 630 } |
| 631 |
| 632 void URLDatabase::RemoveObserver(URLDatabaseObserver* observer) { |
| 633 observers_.RemoveObserver(observer); |
| 634 } |
| 635 |
| 604 } // namespace history | 636 } // namespace history |
| OLD | NEW |