diff options
Diffstat (limited to 'chrome/browser/history/url_database.cc')
-rw-r--r-- | chrome/browser/history/url_database.cc | 282 |
1 files changed, 137 insertions, 145 deletions
diff --git a/chrome/browser/history/url_database.cc b/chrome/browser/history/url_database.cc index b15af5b..0a9115f 100644 --- a/chrome/browser/history/url_database.cc +++ b/chrome/browser/history/url_database.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -8,20 +8,19 @@ #include <limits> #include "app/l10n_util.h" +#include "app/sql/connection.h" +#include "app/sql/statement.h" #include "base/string_util.h" -#include "chrome/common/sqlite_utils.h" #include "chrome/common/url_constants.h" #include "googleurl/src/gurl.h" -using base::Time; - namespace history { const char URLDatabase::kURLRowFields[] = HISTORY_URL_ROW_FIELDS; const int URLDatabase::kNumURLRowFields = 9; bool URLDatabase::URLEnumerator::GetNextURL(URLRow* r) { - if (statement_.step() == SQLITE_ROW) { + if (statement_.Step()) { FillURLRow(statement_, r); return true; } @@ -42,16 +41,16 @@ std::string URLDatabase::GURLToDatabaseURL(const GURL& gurl) { // Convenience to fill a history::URLRow. Must be in sync with the fields in // kURLRowFields. -void URLDatabase::FillURLRow(SQLStatement& s, history::URLRow* i) { +void URLDatabase::FillURLRow(sql::Statement& s, history::URLRow* i) { DCHECK(i); - i->id_ = s.column_int64(0); - i->url_ = GURL(s.column_string(1)); - i->title_ = s.column_wstring(2); - i->visit_count_ = s.column_int(3); - i->typed_count_ = s.column_int(4); - i->last_visit_ = Time::FromInternalValue(s.column_int64(5)); - i->hidden_ = s.column_int(6) != 0; - i->favicon_id_ = s.column_int64(7); + i->id_ = s.ColumnInt64(0); + i->url_ = GURL(s.ColumnString(1)); + i->title_ = UTF8ToWide(s.ColumnString(2)); + i->visit_count_ = s.ColumnInt(3); + i->typed_count_ = s.ColumnInt(4); + i->last_visit_ = base::Time::FromInternalValue(s.ColumnInt64(5)); + i->hidden_ = s.ColumnInt(6) != 0; + i->favicon_id_ = s.ColumnInt64(7); } bool URLDatabase::GetURLRow(URLID url_id, URLRow* info) { @@ -59,52 +58,52 @@ bool URLDatabase::GetURLRow(URLID url_id, URLRow* info) { // there are old URLs in the database that are empty that got in before // we added any checks. We should eventually be able to remove it // when all inputs are using GURL (which prohibit empty input). - SQLITE_UNIQUE_STATEMENT(statement, GetStatementCache(), - "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls WHERE id=?"); - if (!statement.is_valid()) + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, + "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls WHERE id=?")); + if (!statement) return false; - statement->bind_int64(0, url_id); - if (statement->step() == SQLITE_ROW) { - FillURLRow(*statement, info); + statement.BindInt64(0, url_id); + if (statement.Step()) { + FillURLRow(statement, info); return true; } return false; } URLID URLDatabase::GetRowForURL(const GURL& url, history::URLRow* info) { - SQLITE_UNIQUE_STATEMENT(statement, GetStatementCache(), - "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls WHERE url=?"); - if (!statement.is_valid()) + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, + "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls WHERE url=?")); + if (!statement) return 0; std::string url_string = GURLToDatabaseURL(url); - statement->bind_string(0, url_string); - if (statement->step() != SQLITE_ROW) + statement.BindString(0, url_string); + if (!statement.Step()) return 0; // no data if (info) - FillURLRow(*statement, info); - return statement->column_int64(0); + FillURLRow(statement, info); + return statement.ColumnInt64(0); } bool URLDatabase::UpdateURLRow(URLID url_id, const history::URLRow& info) { - SQLITE_UNIQUE_STATEMENT(statement, GetStatementCache(), + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, "UPDATE urls SET title=?,visit_count=?,typed_count=?,last_visit_time=?," "hidden=?,favicon_id=?" - "WHERE id=?"); - if (!statement.is_valid()) + "WHERE id=?")); + if (!statement) return false; - statement->bind_wstring(0, info.title()); - statement->bind_int(1, info.visit_count()); - statement->bind_int(2, info.typed_count()); - statement->bind_int64(3, info.last_visit().ToInternalValue()); - statement->bind_int(4, info.hidden() ? 1 : 0); - statement->bind_int64(5, info.favicon_id()); - statement->bind_int64(6, url_id); - return statement->step() == SQLITE_DONE; + statement.BindString(0, WideToUTF8(info.title())); + statement.BindInt(1, info.visit_count()); + statement.BindInt(2, info.typed_count()); + statement.BindInt64(3, info.last_visit().ToInternalValue()); + statement.BindInt(4, info.hidden() ? 1 : 0); + statement.BindInt64(5, info.favicon_id()); + statement.BindInt64(6, url_id); + return statement.Run(); } URLID URLDatabase::AddURLInternal(const history::URLRow& info, @@ -128,44 +127,46 @@ URLID URLDatabase::AddURLInternal(const history::URLRow& info, } #undef ADDURL_COMMON_SUFFIX - SqliteCompiledStatement statement(statement_name, 0, GetStatementCache(), - statement_sql); - if (!statement.is_valid()) + sql::Statement statement(GetDB().GetCachedStatement( + sql::StatementID(statement_name), statement_sql)); + if (!statement) { + NOTREACHED() << GetDB().GetErrorMessage(); return 0; + } - statement->bind_string(0, GURLToDatabaseURL(info.url())); - statement->bind_wstring(1, info.title()); - statement->bind_int(2, info.visit_count()); - statement->bind_int(3, info.typed_count()); - statement->bind_int64(4, info.last_visit().ToInternalValue()); - statement->bind_int(5, info.hidden() ? 1 : 0); - statement->bind_int64(6, info.favicon_id()); + statement.BindString(0, GURLToDatabaseURL(info.url())); + statement.BindString(1, WideToUTF8(info.title())); + statement.BindInt(2, info.visit_count()); + statement.BindInt(3, info.typed_count()); + statement.BindInt64(4, info.last_visit().ToInternalValue()); + statement.BindInt(5, info.hidden() ? 1 : 0); + statement.BindInt64(6, info.favicon_id()); - if (statement->step() != SQLITE_DONE) + if (!statement.Run()) return 0; - return sqlite3_last_insert_rowid(GetDB()); + return GetDB().GetLastInsertRowId(); } bool URLDatabase::DeleteURLRow(URLID id) { - SQLITE_UNIQUE_STATEMENT(statement, GetStatementCache(), - "DELETE FROM urls WHERE id = ?"); - if (!statement.is_valid()) + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, + "DELETE FROM urls WHERE id = ?")); + if (!statement) return false; - statement->bind_int64(0, id); - if (statement->step() != SQLITE_DONE) + statement.BindInt64(0, id); + if (!statement.Run()) return false; - // And delete any keyword visits. + // And delete any keyword visits. if (!has_keyword_search_terms_) return true; - SQLITE_UNIQUE_STATEMENT(del_keyword_visit, GetStatementCache(), - "DELETE FROM keyword_search_terms WHERE url_id=?"); - if (!del_keyword_visit.is_valid()) + sql::Statement del_keyword_visit(GetDB().GetCachedStatement(SQL_FROM_HERE, + "DELETE FROM keyword_search_terms WHERE url_id=?")); + if (!del_keyword_visit) return false; - del_keyword_visit->bind_int64(0, id); - return (del_keyword_visit->step() == SQLITE_DONE); + del_keyword_visit.BindInt64(0, id); + return del_keyword_visit.Run(); } bool URLDatabase::CreateTemporaryURLTable() { @@ -181,14 +182,12 @@ bool URLDatabase::CommitTemporaryURLTable() { // supplimentary indices that the archived database doesn't need. // Swap the url table out and replace it with the temporary one. - if (sqlite3_exec(GetDB(), "DROP TABLE urls", - NULL, NULL, NULL) != SQLITE_OK) { - NOTREACHED(); + if (!GetDB().Execute("DROP TABLE urls")) { + NOTREACHED() << GetDB().GetErrorMessage(); return false; } - if (sqlite3_exec(GetDB(), "ALTER TABLE temp_urls RENAME TO urls", - NULL, NULL, NULL) != SQLITE_OK) { - NOTREACHED(); + if (!GetDB().Execute("ALTER TABLE temp_urls RENAME TO urls")) { + NOTREACHED() << GetDB().GetErrorMessage(); return false; } @@ -206,8 +205,9 @@ bool URLDatabase::InitURLEnumeratorForEverything(URLEnumerator* enumerator) { std::string sql("SELECT "); sql.append(kURLRowFields); sql.append(" FROM urls"); - if (enumerator->statement_.prepare(GetDB(), sql.c_str()) != SQLITE_OK) { - NOTREACHED() << "Query statement prep failed"; + enumerator->statement_.Assign(GetDB().GetUniqueStatement(sql.c_str())); + if (!enumerator->statement_) { + NOTREACHED() << GetDB().GetErrorMessage(); return false; } enumerator->initialized_ = true; @@ -215,13 +215,13 @@ bool URLDatabase::InitURLEnumeratorForEverything(URLEnumerator* enumerator) { } bool URLDatabase::IsFavIconUsed(FavIconID favicon_id) { - SQLITE_UNIQUE_STATEMENT(statement, GetStatementCache(), - "SELECT id FROM urls WHERE favicon_id=? LIMIT 1"); - if (!statement.is_valid()) + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, + "SELECT id FROM urls WHERE favicon_id=? LIMIT 1")); + if (!statement) return false; - statement->bind_int64(0, favicon_id); - return statement->step() == SQLITE_ROW; + statement.BindInt64(0, favicon_id); + return statement.Step(); } void URLDatabase::AutocompleteForPrefix(const std::wstring& prefix, @@ -231,12 +231,12 @@ void URLDatabase::AutocompleteForPrefix(const std::wstring& prefix, // as bookmarks is no longer part of the db we no longer include the order // by clause. results->clear(); - SQLITE_UNIQUE_STATEMENT(statement, GetStatementCache(), + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls " "WHERE url >= ? AND url < ? AND hidden = 0 " "ORDER BY typed_count DESC, visit_count DESC, last_visit_time DESC " - "LIMIT ?"); - if (!statement.is_valid()) + "LIMIT ?")); + if (!statement) return; // We will find all strings between "prefix" and this string, which is prefix @@ -247,13 +247,13 @@ void URLDatabase::AutocompleteForPrefix(const std::wstring& prefix, std::string end_query(prefix_utf8); end_query.push_back(std::numeric_limits<unsigned char>::max()); - statement->bind_string(0, prefix_utf8); - statement->bind_string(1, end_query); - statement->bind_int(2, static_cast<int>(max_results)); + statement.BindString(0, prefix_utf8); + statement.BindString(1, end_query); + statement.BindInt(2, static_cast<int>(max_results)); - while (statement->step() == SQLITE_ROW) { + while (statement.Step()) { history::URLRow info; - FillURLRow(*statement, &info); + FillURLRow(statement, &info); if (info.url().is_valid()) results->push_back(info); } @@ -277,18 +277,18 @@ bool URLDatabase::FindShortestURLFromBase(const std::string& base, sql.append(" ? AND url < :end AND url = substr(:end, 1, length(url)) " "AND hidden = 0 AND visit_count >= ? AND typed_count >= ? " "ORDER BY url LIMIT 1"); - SQLStatement statement; - if (statement.prepare(GetDB(), sql.c_str()) != SQLITE_OK) { - NOTREACHED() << "select statement prep failed"; + sql::Statement statement(GetDB().GetUniqueStatement(sql.c_str())); + if (!statement) { + NOTREACHED() << GetDB().GetErrorMessage(); return false; } - statement.bind_string(0, base); - statement.bind_string(1, url); // :end - statement.bind_int(2, min_visits); - statement.bind_int(3, min_typed); + statement.BindString(0, base); + statement.BindString(1, url); // :end + statement.BindInt(2, min_visits); + statement.BindInt(3, min_typed); - if (statement.step() != SQLITE_ROW) + if (!statement.Step()) return false; DCHECK(info); @@ -298,33 +298,29 @@ bool URLDatabase::FindShortestURLFromBase(const std::string& base, bool URLDatabase::InitKeywordSearchTermsTable() { has_keyword_search_terms_ = true; - if (!DoesSqliteTableExist(GetDB(), "keyword_search_terms")) { - if (sqlite3_exec(GetDB(), "CREATE TABLE keyword_search_terms (" + if (!GetDB().DoesTableExist("keyword_search_terms")) { + if (!GetDB().Execute("CREATE TABLE keyword_search_terms (" "keyword_id INTEGER NOT NULL," // ID of the TemplateURL. "url_id INTEGER NOT NULL," // ID of the url. "lower_term LONGVARCHAR NOT NULL," // The search term, in lower case. - "term LONGVARCHAR NOT NULL)", // The actual search term. - NULL, NULL, NULL) != SQLITE_OK) + "term LONGVARCHAR NOT NULL)")) // The actual search term. return false; } // For searching. - sqlite3_exec(GetDB(), "CREATE INDEX keyword_search_terms_index1 ON " - "keyword_search_terms (keyword_id, lower_term)", - NULL, NULL, NULL); + GetDB().Execute("CREATE INDEX keyword_search_terms_index1 ON " + "keyword_search_terms (keyword_id, lower_term)"); // For deletion. - sqlite3_exec(GetDB(), "CREATE INDEX keyword_search_terms_index2 ON " - "keyword_search_terms (url_id)", - NULL, NULL, NULL); + GetDB().Execute("CREATE INDEX keyword_search_terms_index2 ON " + "keyword_search_terms (url_id)"); return true; } bool URLDatabase::DropKeywordSearchTermsTable() { // This will implicitly delete the indices over the table. - return sqlite3_exec(GetDB(), "DROP TABLE keyword_search_terms", - NULL, NULL, NULL) == SQLITE_OK; + return GetDB().Execute("DROP TABLE keyword_search_terms"); } bool URLDatabase::SetKeywordSearchTermsForURL(URLID url_id, @@ -332,39 +328,39 @@ bool URLDatabase::SetKeywordSearchTermsForURL(URLID url_id, const std::wstring& term) { DCHECK(url_id && keyword_id && !term.empty()); - SQLITE_UNIQUE_STATEMENT(exist_statement, GetStatementCache(), + sql::Statement exist_statement(GetDB().GetCachedStatement(SQL_FROM_HERE, "SELECT term FROM keyword_search_terms " - "WHERE keyword_id = ? AND url_id = ?"); - if (!exist_statement.is_valid()) + "WHERE keyword_id = ? AND url_id = ?")); + if (!exist_statement) return false; - exist_statement->bind_int64(0, keyword_id); - exist_statement->bind_int64(1, url_id); - if (exist_statement->step() == SQLITE_ROW) + exist_statement.BindInt64(0, keyword_id); + exist_statement.BindInt64(1, url_id); + if (exist_statement.Step()) return true; // Term already exists, no need to add it. - SQLITE_UNIQUE_STATEMENT(statement, GetStatementCache(), + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, "INSERT INTO keyword_search_terms (keyword_id, url_id, lower_term, term) " - "VALUES (?,?,?,?)"); - if (!statement.is_valid()) + "VALUES (?,?,?,?)")); + if (!statement) return false; - statement->bind_int64(0, keyword_id); - statement->bind_int64(1, url_id); - statement->bind_wstring(2, l10n_util::ToLower(term)); - statement->bind_wstring(3, term); - return (statement->step() == SQLITE_DONE); + statement.BindInt64(0, keyword_id); + statement.BindInt64(1, url_id); + statement.BindString(2, UTF16ToUTF8(l10n_util::ToLower(WideToUTF16(term)))); + statement.BindString(3, WideToUTF8(term)); + return statement.Run(); } void URLDatabase::DeleteAllSearchTermsForKeyword( TemplateURL::IDType keyword_id) { DCHECK(keyword_id); - SQLITE_UNIQUE_STATEMENT(statement, GetStatementCache(), - "DELETE FROM keyword_search_terms WHERE keyword_id=?"); - if (!statement.is_valid()) + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, + "DELETE FROM keyword_search_terms WHERE keyword_id=?")); + if (!statement) return; - statement->bind_int64(0, keyword_id); - statement->step(); + statement.BindInt64(0, keyword_id); + statement.Run(); } void URLDatabase::GetMostRecentKeywordSearchTerms( @@ -379,30 +375,30 @@ void URLDatabase::GetMostRecentKeywordSearchTerms( return; DCHECK(!prefix.empty()); - SQLITE_UNIQUE_STATEMENT(statement, GetStatementCache(), + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, "SELECT DISTINCT kv.term, u.last_visit_time " "FROM keyword_search_terms kv " "JOIN urls u ON kv.url_id = u.id " "WHERE kv.keyword_id = ? AND kv.lower_term >= ? AND kv.lower_term < ? " - "ORDER BY u.last_visit_time DESC LIMIT ?"); - if (!statement.is_valid()) + "ORDER BY u.last_visit_time DESC LIMIT ?")); + if (!statement) return; // NOTE: Keep this ToLower() call in sync with search_provider.cc. - const std::wstring lower_prefix = l10n_util::ToLower(prefix); + string16 lower_prefix = l10n_util::ToLower(WideToUTF16(prefix)); // This magic gives us a prefix search. - std::wstring next_prefix = lower_prefix; + string16 next_prefix = lower_prefix; next_prefix[next_prefix.size() - 1] = next_prefix[next_prefix.size() - 1] + 1; - statement->bind_int64(0, keyword_id); - statement->bind_wstring(1, lower_prefix); - statement->bind_wstring(2, next_prefix); - statement->bind_int(3, max_count); + statement.BindInt64(0, keyword_id); + statement.BindString(1, UTF16ToUTF8(lower_prefix)); + statement.BindString(2, UTF16ToUTF8(next_prefix)); + statement.BindInt(3, max_count); KeywordSearchTermVisit visit; - while (statement->step() == SQLITE_ROW) { - visit.term = statement->column_wstring(0); - visit.time = Time::FromInternalValue(statement->column_int64(1)); + while (statement.Step()) { + visit.term = UTF8ToWide(statement.ColumnString(0)); + visit.time = base::Time::FromInternalValue(statement.ColumnInt64(1)); matches->push_back(visit); } } @@ -417,7 +413,7 @@ bool URLDatabase::MigrateFromVersion11ToVersion12() { } bool URLDatabase::DropStarredIDFromURLs() { - if (!DoesSqliteColumnExist(GetDB(), "urls", "starred_id", NULL)) + if (!GetDB().DoesColumnExist("urls", "starred_id")) return true; // urls is already updated, no need to continue. // Create a temporary table to contain the new URLs table. @@ -427,13 +423,12 @@ bool URLDatabase::DropStarredIDFromURLs() { } // Copy the contents. - const char* insert_statement = + if (!GetDB().Execute( "INSERT INTO temp_urls (id, url, title, visit_count, typed_count, " "last_visit_time, hidden, favicon_id) " "SELECT id, url, title, visit_count, typed_count, last_visit_time, " - "hidden, favicon_id FROM urls"; - if (sqlite3_exec(GetDB(), insert_statement, NULL, NULL, NULL) != SQLITE_OK) { - NOTREACHED(); + "hidden, favicon_id FROM urls")) { + NOTREACHED() << GetDB().GetErrorMessage(); return false; } @@ -448,7 +443,7 @@ bool URLDatabase::DropStarredIDFromURLs() { bool URLDatabase::CreateURLTable(bool is_temporary) { const char* name = is_temporary ? "temp_urls" : "urls"; - if (DoesSqliteTableExist(GetDB(), name)) + if (GetDB().DoesTableExist(name)) return true; std::string sql; @@ -464,21 +459,18 @@ bool URLDatabase::CreateURLTable(bool is_temporary) { "hidden INTEGER DEFAULT 0 NOT NULL," "favicon_id INTEGER DEFAULT 0 NOT NULL)"); - return sqlite3_exec(GetDB(), sql.c_str(), NULL, NULL, NULL) == SQLITE_OK; + return GetDB().Execute(sql.c_str()); } void URLDatabase::CreateMainURLIndex() { // Index over URLs so we can quickly look up based on URL. Ignore errors as // this likely already exists (and the same below). - sqlite3_exec(GetDB(), "CREATE INDEX urls_url_index ON urls (url)", NULL, NULL, - NULL); + GetDB().Execute("CREATE INDEX urls_url_index ON urls (url)"); } void URLDatabase::CreateSupplimentaryURLIndices() { // Add a favicon index. This is useful when we delete urls. - sqlite3_exec(GetDB(), - "CREATE INDEX urls_favicon_id_INDEX ON urls (favicon_id)", - NULL, NULL, NULL); + GetDB().Execute("CREATE INDEX urls_favicon_id_INDEX ON urls (favicon_id)"); } } // namespace history |