| 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/common/url_database/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/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 | 18 |
| 19 namespace history { | 19 namespace history { |
| 20 | 20 |
| 21 const char URLDatabase::kURLRowFields[] = HISTORY_URL_ROW_FIELDS; | 21 const char URLDatabase::kURLRowFields[] = URL_DATABASE_ROW_FIELDS; |
| 22 const int URLDatabase::kNumURLRowFields = 9; | 22 const int URLDatabase::kNumURLRowFields = 9; |
| 23 | 23 |
| 24 URLDatabase::URLEnumeratorBase::URLEnumeratorBase() | 24 URLDatabase::URLEnumeratorBase::URLEnumeratorBase() |
| 25 : initialized_(false) { | 25 : initialized_(false) { |
| 26 } | 26 } |
| 27 | 27 |
| 28 URLDatabase::URLEnumeratorBase::~URLEnumeratorBase() { | 28 URLDatabase::URLEnumeratorBase::~URLEnumeratorBase() { |
| 29 } | 29 } |
| 30 | 30 |
| 31 URLDatabase::URLEnumerator::URLEnumerator() { | 31 URLDatabase::URLEnumerator::URLEnumerator() { |
| 32 } | 32 } |
| 33 | 33 |
| 34 URLDatabase::IconMappingEnumerator::IconMappingEnumerator() { | 34 URLDatabase::IconMappingEnumerator::IconMappingEnumerator() { |
| 35 } | 35 } |
| 36 | 36 |
| 37 bool URLDatabase::URLEnumerator::GetNextURL(URLRow* r) { | 37 bool URLDatabase::URLEnumerator::GetNextURL(URLRow* r) { |
| 38 if (statement_.Step()) { | 38 if (statement_.Step()) { |
| 39 FillURLRow(statement_, r); | 39 FillURLRow(statement_, r); |
| 40 return true; | 40 return true; |
| 41 } | 41 } |
| 42 return false; | 42 return false; |
| 43 } | 43 } |
| 44 | 44 |
| 45 bool URLDatabase::IconMappingEnumerator::GetNextIconMapping(IconMapping* r) { | 45 bool URLDatabase::IconMappingEnumerator::GetNextIconMapping(IconMapping* r) { |
| 46 if (!statement_.Step()) | 46 if (!statement_.Step()) |
| 47 return false; | 47 return false; |
| 48 | 48 |
| 49 r->page_url = GURL(statement_.ColumnString(0)); | 49 r->page_url = GURL(statement_.ColumnString(0)); |
| 50 r->icon_id = statement_.ColumnInt64(1); | 50 r->icon_id = statement_.ColumnInt64(1); |
| 51 return true; | 51 return true; |
| 52 } | 52 } |
| 53 | 53 |
| 54 URLDatabase::URLDatabase() | 54 URLDatabase::URLDatabase() |
| 55 : has_keyword_search_terms_(false) { | 55 : has_keyword_search_terms_(false) { |
| 56 } | 56 } |
| 57 | 57 |
| 58 URLDatabase::~URLDatabase() { | 58 URLDatabase::~URLDatabase() { |
| 59 } | 59 } |
| 60 | 60 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 82 i->last_visit_ = base::Time::FromInternalValue(s.ColumnInt64(5)); | 82 i->last_visit_ = base::Time::FromInternalValue(s.ColumnInt64(5)); |
| 83 i->hidden_ = s.ColumnInt(6) != 0; | 83 i->hidden_ = s.ColumnInt(6) != 0; |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool URLDatabase::GetURLRow(URLID url_id, URLRow* info) { | 86 bool URLDatabase::GetURLRow(URLID url_id, URLRow* info) { |
| 87 // TODO(brettw) We need check for empty URLs to handle the case where | 87 // TODO(brettw) We need check for empty URLs to handle the case where |
| 88 // there are old URLs in the database that are empty that got in before | 88 // there are old URLs in the database that are empty that got in before |
| 89 // we added any checks. We should eventually be able to remove it | 89 // we added any checks. We should eventually be able to remove it |
| 90 // when all inputs are using GURL (which prohibit empty input). | 90 // when all inputs are using GURL (which prohibit empty input). |
| 91 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 91 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 92 "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls WHERE id=?")); | 92 "SELECT" URL_DATABASE_ROW_FIELDS "FROM urls WHERE id=?")); |
| 93 statement.BindInt64(0, url_id); | 93 statement.BindInt64(0, url_id); |
| 94 | 94 |
| 95 if (statement.Step()) { | 95 if (statement.Step()) { |
| 96 FillURLRow(statement, info); | 96 FillURLRow(statement, info); |
| 97 return true; | 97 return true; |
| 98 } | 98 } |
| 99 return false; | 99 return false; |
| 100 } | 100 } |
| 101 | 101 |
| 102 bool URLDatabase::GetAllTypedUrls(URLRows* urls) { | 102 bool URLDatabase::GetAllTypedUrls(URLRows* urls) { |
| 103 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 103 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 104 "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls WHERE typed_count > 0")); | 104 "SELECT" URL_DATABASE_ROW_FIELDS "FROM urls WHERE typed_count > 0")); |
| 105 | 105 |
| 106 while (statement.Step()) { | 106 while (statement.Step()) { |
| 107 URLRow info; | 107 URLRow info; |
| 108 FillURLRow(statement, &info); | 108 FillURLRow(statement, &info); |
| 109 urls->push_back(info); | 109 urls->push_back(info); |
| 110 } | 110 } |
| 111 return true; | 111 return true; |
| 112 } | 112 } |
| 113 | 113 |
| 114 URLID URLDatabase::GetRowForURL(const GURL& url, history::URLRow* info) { | 114 URLID URLDatabase::GetRowForURL(const GURL& url, history::URLRow* info) { |
| 115 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 115 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 116 "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls WHERE url=?")); | 116 "SELECT" URL_DATABASE_ROW_FIELDS "FROM urls WHERE url=?")); |
| 117 std::string url_string = GURLToDatabaseURL(url); | 117 std::string url_string = GURLToDatabaseURL(url); |
| 118 statement.BindString(0, url_string); | 118 statement.BindString(0, url_string); |
| 119 | 119 |
| 120 if (!statement.Step()) | 120 if (!statement.Step()) |
| 121 return 0; // no data | 121 return 0; // no data |
| 122 | 122 |
| 123 if (info) | 123 if (info) |
| 124 FillURLRow(statement, info); | 124 FillURLRow(statement, info); |
| 125 return statement.ColumnInt64(0); | 125 return statement.ColumnInt64(0); |
| 126 } | 126 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 138 statement.BindInt(4, info.hidden() ? 1 : 0); | 138 statement.BindInt(4, info.hidden() ? 1 : 0); |
| 139 statement.BindInt64(5, url_id); | 139 statement.BindInt64(5, url_id); |
| 140 | 140 |
| 141 return statement.Run(); | 141 return statement.Run(); |
| 142 } | 142 } |
| 143 | 143 |
| 144 URLID URLDatabase::AddURLInternal(const history::URLRow& info, | 144 URLID URLDatabase::AddURLInternal(const history::URLRow& info, |
| 145 bool is_temporary) { | 145 bool is_temporary) { |
| 146 // This function is used to insert into two different tables, so we have to | 146 // 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 | 147 // do some shuffling. Unfortinately, we can't use the macro |
| 148 // HISTORY_URL_ROW_FIELDS because that specifies the table name which is | 148 // URL_DATABASE_ROW_FIELDS because that specifies the table name which is |
| 149 // invalid in the insert syntax. | 149 // invalid in the insert syntax. |
| 150 #define ADDURL_COMMON_SUFFIX \ | 150 #define ADDURL_COMMON_SUFFIX \ |
| 151 " (url, title, visit_count, typed_count, "\ | 151 " (url, title, visit_count, typed_count, "\ |
| 152 "last_visit_time, hidden) "\ | 152 "last_visit_time, hidden) "\ |
| 153 "VALUES (?,?,?,?,?,?)" | 153 "VALUES (?,?,?,?,?,?)" |
| 154 const char* statement_name; | 154 const char* statement_name; |
| 155 const char* statement_sql; | 155 const char* statement_sql; |
| 156 if (is_temporary) { | 156 if (is_temporary) { |
| 157 statement_name = "AddURLTemporary"; | 157 statement_name = "AddURLTemporary"; |
| 158 statement_sql = "INSERT INTO temp_urls" ADDURL_COMMON_SUFFIX; | 158 statement_sql = "INSERT INTO temp_urls" ADDURL_COMMON_SUFFIX; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 size_t max_results, | 267 size_t max_results, |
| 268 bool typed_only, | 268 bool typed_only, |
| 269 URLRows* results) { | 269 URLRows* results) { |
| 270 // NOTE: this query originally sorted by starred as the second parameter. But | 270 // NOTE: this query originally sorted by starred as the second parameter. But |
| 271 // as bookmarks is no longer part of the db we no longer include the order | 271 // as bookmarks is no longer part of the db we no longer include the order |
| 272 // by clause. | 272 // by clause. |
| 273 results->clear(); | 273 results->clear(); |
| 274 const char* sql; | 274 const char* sql; |
| 275 int line; | 275 int line; |
| 276 if (typed_only) { | 276 if (typed_only) { |
| 277 sql = "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls " | 277 sql = "SELECT" URL_DATABASE_ROW_FIELDS "FROM urls " |
| 278 "WHERE url >= ? AND url < ? AND hidden = 0 AND typed_count > 0 " | 278 "WHERE url >= ? AND url < ? AND hidden = 0 AND typed_count > 0 " |
| 279 "ORDER BY typed_count DESC, visit_count DESC, last_visit_time DESC " | 279 "ORDER BY typed_count DESC, visit_count DESC, last_visit_time DESC " |
| 280 "LIMIT ?"; | 280 "LIMIT ?"; |
| 281 line = __LINE__; | 281 line = __LINE__; |
| 282 } else { | 282 } else { |
| 283 sql = "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls " | 283 sql = "SELECT" URL_DATABASE_ROW_FIELDS "FROM urls " |
| 284 "WHERE url >= ? AND url < ? AND hidden = 0 " | 284 "WHERE url >= ? AND url < ? AND hidden = 0 " |
| 285 "ORDER BY typed_count DESC, visit_count DESC, last_visit_time DESC " | 285 "ORDER BY typed_count DESC, visit_count DESC, last_visit_time DESC " |
| 286 "LIMIT ?"; | 286 "LIMIT ?"; |
| 287 line = __LINE__; | 287 line = __LINE__; |
| 288 } | 288 } |
| 289 sql::Statement statement( | 289 sql::Statement statement( |
| 290 GetDB().GetCachedStatement(sql::StatementID(__FILE__, line), sql)); | 290 GetDB().GetCachedStatement(sql::StatementID(__FILE__, line), sql)); |
| 291 | 291 |
| 292 // We will find all strings between "prefix" and this string, which is prefix | 292 // We will find all strings between "prefix" and this string, which is prefix |
| 293 // followed by the maximum character size. Use 8-bit strings for everything | 293 // followed by the maximum character size. Use 8-bit strings for everything |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 return GetDB().Execute(sql.c_str()); | 569 return GetDB().Execute(sql.c_str()); |
| 570 } | 570 } |
| 571 | 571 |
| 572 bool URLDatabase::CreateMainURLIndex() { | 572 bool URLDatabase::CreateMainURLIndex() { |
| 573 // Index over URLs so we can quickly look up based on URL. | 573 // Index over URLs so we can quickly look up based on URL. |
| 574 return GetDB().Execute( | 574 return GetDB().Execute( |
| 575 "CREATE INDEX IF NOT EXISTS urls_url_index ON urls (url)"); | 575 "CREATE INDEX IF NOT EXISTS urls_url_index ON urls (url)"); |
| 576 } | 576 } |
| 577 | 577 |
| 578 } // namespace history | 578 } // namespace history |
| OLD | NEW |