| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <limits> | 5 #include <limits> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "chrome/browser/history/text_database.h" | 9 #include "chrome/browser/history/text_database.h" |
| 10 | 10 |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 const std::string& url, | 209 const std::string& url, |
| 210 const std::string& title, | 210 const std::string& title, |
| 211 const std::string& contents) { | 211 const std::string& contents) { |
| 212 sql::Transaction committer(&db_); | 212 sql::Transaction committer(&db_); |
| 213 if (!committer.Begin()) | 213 if (!committer.Begin()) |
| 214 return false; | 214 return false; |
| 215 | 215 |
| 216 // Add to the pages table. | 216 // Add to the pages table. |
| 217 sql::Statement add_to_pages(db_.GetCachedStatement(SQL_FROM_HERE, | 217 sql::Statement add_to_pages(db_.GetCachedStatement(SQL_FROM_HERE, |
| 218 "INSERT INTO pages (url, title, body) VALUES (?,?,?)")); | 218 "INSERT INTO pages (url, title, body) VALUES (?,?,?)")); |
| 219 if (!add_to_pages) { | |
| 220 NOTREACHED() << db_.GetErrorMessage(); | |
| 221 return false; | |
| 222 } | |
| 223 add_to_pages.BindString(0, url); | 219 add_to_pages.BindString(0, url); |
| 224 add_to_pages.BindString(1, title); | 220 add_to_pages.BindString(1, title); |
| 225 add_to_pages.BindString(2, contents); | 221 add_to_pages.BindString(2, contents); |
| 226 if (!add_to_pages.Run()) { | 222 if (!add_to_pages.Run()) |
| 227 NOTREACHED() << db_.GetErrorMessage(); | |
| 228 return false; | 223 return false; |
| 229 } | |
| 230 | 224 |
| 231 int64 rowid = db_.GetLastInsertRowId(); | 225 int64 rowid = db_.GetLastInsertRowId(); |
| 232 | 226 |
| 233 // Add to the info table with the same rowid. | 227 // Add to the info table with the same rowid. |
| 234 sql::Statement add_to_info(db_.GetCachedStatement(SQL_FROM_HERE, | 228 sql::Statement add_to_info(db_.GetCachedStatement(SQL_FROM_HERE, |
| 235 "INSERT INTO info (rowid, time) VALUES (?,?)")); | 229 "INSERT INTO info (rowid, time) VALUES (?,?)")); |
| 236 if (!add_to_info) { | |
| 237 NOTREACHED() << db_.GetErrorMessage(); | |
| 238 return false; | |
| 239 } | |
| 240 add_to_info.BindInt64(0, rowid); | 230 add_to_info.BindInt64(0, rowid); |
| 241 add_to_info.BindInt64(1, time.ToInternalValue()); | 231 add_to_info.BindInt64(1, time.ToInternalValue()); |
| 242 if (!add_to_info.Run()) { | 232 |
| 243 NOTREACHED() << db_.GetErrorMessage(); | 233 if (!add_to_info.Run()) |
| 244 return false; | 234 return false; |
| 245 } | |
| 246 | 235 |
| 247 return committer.Commit(); | 236 return committer.Commit(); |
| 248 } | 237 } |
| 249 | 238 |
| 250 void TextDatabase::DeletePageData(base::Time time, const std::string& url) { | 239 void TextDatabase::DeletePageData(base::Time time, const std::string& url) { |
| 251 // First get all rows that match. Selecing on time (which has an index) allows | 240 // First get all rows that match. Selecing on time (which has an index) allows |
| 252 // us to avoid brute-force searches on the full-text-index table (there will | 241 // us to avoid brute-force searches on the full-text-index table (there will |
| 253 // generally be only one match per time). | 242 // generally be only one match per time). |
| 254 sql::Statement select_ids(db_.GetCachedStatement(SQL_FROM_HERE, | 243 sql::Statement select_ids(db_.GetCachedStatement(SQL_FROM_HERE, |
| 255 "SELECT info.rowid " | 244 "SELECT info.rowid " |
| 256 "FROM info JOIN pages ON info.rowid = pages.rowid " | 245 "FROM info JOIN pages ON info.rowid = pages.rowid " |
| 257 "WHERE info.time=? AND pages.url=?")); | 246 "WHERE info.time=? AND pages.url=?")); |
| 258 if (!select_ids) | |
| 259 return; | |
| 260 select_ids.BindInt64(0, time.ToInternalValue()); | 247 select_ids.BindInt64(0, time.ToInternalValue()); |
| 261 select_ids.BindString(1, url); | 248 select_ids.BindString(1, url); |
| 249 |
| 262 std::set<int64> rows_to_delete; | 250 std::set<int64> rows_to_delete; |
| 263 while (select_ids.Step()) | 251 while (select_ids.Step()) |
| 264 rows_to_delete.insert(select_ids.ColumnInt64(0)); | 252 rows_to_delete.insert(select_ids.ColumnInt64(0)); |
| 265 | 253 |
| 266 // Delete from the pages table. | 254 // Delete from the pages table. |
| 267 sql::Statement delete_page(db_.GetCachedStatement(SQL_FROM_HERE, | 255 sql::Statement delete_page(db_.GetCachedStatement(SQL_FROM_HERE, |
| 268 "DELETE FROM pages WHERE rowid=?")); | 256 "DELETE FROM pages WHERE rowid=?")); |
| 269 if (!delete_page) | 257 |
| 270 return; | |
| 271 for (std::set<int64>::const_iterator i = rows_to_delete.begin(); | 258 for (std::set<int64>::const_iterator i = rows_to_delete.begin(); |
| 272 i != rows_to_delete.end(); ++i) { | 259 i != rows_to_delete.end(); ++i) { |
| 273 delete_page.BindInt64(0, *i); | 260 delete_page.BindInt64(0, *i); |
| 274 if (!delete_page.Run()) { | 261 if (!delete_page.Run()) |
| 275 NOTREACHED(); | |
| 276 return; | 262 return; |
| 277 } | |
| 278 delete_page.Reset(); | 263 delete_page.Reset(); |
| 279 } | 264 } |
| 280 | 265 |
| 281 // Delete from the info table. | 266 // Delete from the info table. |
| 282 sql::Statement delete_info(db_.GetCachedStatement(SQL_FROM_HERE, | 267 sql::Statement delete_info(db_.GetCachedStatement(SQL_FROM_HERE, |
| 283 "DELETE FROM info WHERE rowid=?")); | 268 "DELETE FROM info WHERE rowid=?")); |
| 284 if (!delete_info) | 269 |
| 285 return; | |
| 286 for (std::set<int64>::const_iterator i = rows_to_delete.begin(); | 270 for (std::set<int64>::const_iterator i = rows_to_delete.begin(); |
| 287 i != rows_to_delete.end(); ++i) { | 271 i != rows_to_delete.end(); ++i) { |
| 288 delete_info.BindInt64(0, *i); | 272 delete_info.BindInt64(0, *i); |
| 289 if (!delete_info.Run()) { | 273 if (!delete_info.Run()) |
| 290 NOTREACHED(); | |
| 291 return; | 274 return; |
| 292 } | |
| 293 delete_info.Reset(); | 275 delete_info.Reset(); |
| 294 } | 276 } |
| 295 } | 277 } |
| 296 | 278 |
| 297 void TextDatabase::Optimize() { | 279 void TextDatabase::Optimize() { |
| 298 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 280 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
| 299 "SELECT OPTIMIZE(pages) FROM pages LIMIT 1")); | 281 "SELECT OPTIMIZE(pages) FROM pages LIMIT 1")); |
| 300 if (!statement) | |
| 301 return; | |
| 302 statement.Run(); | 282 statement.Run(); |
| 303 } | 283 } |
| 304 | 284 |
| 305 void TextDatabase::GetTextMatches(const std::string& query, | 285 void TextDatabase::GetTextMatches(const std::string& query, |
| 306 const QueryOptions& options, | 286 const QueryOptions& options, |
| 307 std::vector<Match>* results, | 287 std::vector<Match>* results, |
| 308 URLSet* found_urls, | 288 URLSet* found_urls, |
| 309 base::Time* first_time_searched) { | 289 base::Time* first_time_searched) { |
| 310 *first_time_searched = options.begin_time; | 290 *first_time_searched = options.begin_time; |
| 311 | 291 |
| 312 // TODO(mrossetti): Remove the non-body_only alternative and move the string | 292 // TODO(mrossetti): Remove the non-body_only alternative and move the string |
| 313 // into the statement construction when we switch to body_only permanently. | 293 // into the statement construction when we switch to body_only permanently. |
| 314 std::string sql = "SELECT url, title, time, offsets(pages), body FROM pages " | 294 std::string sql = "SELECT url, title, time, offsets(pages), body FROM pages " |
| 315 " LEFT OUTER JOIN info ON pages.rowid = info.rowid WHERE "; | 295 " LEFT OUTER JOIN info ON pages.rowid = info.rowid WHERE "; |
| 316 sql += options.body_only ? "body " : "pages "; | 296 sql += options.body_only ? "body " : "pages "; |
| 317 sql += "MATCH ? AND time >= ? AND time < ? ORDER BY time DESC LIMIT ?"; | 297 sql += "MATCH ? AND time >= ? AND time < ? ORDER BY time DESC LIMIT ?"; |
| 318 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, sql.c_str())); | 298 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, sql.c_str())); |
| 319 if (!statement) | |
| 320 return; | |
| 321 | 299 |
| 322 // When their values indicate "unspecified", saturate the numbers to the max | 300 // When their values indicate "unspecified", saturate the numbers to the max |
| 323 // or min to get the correct result. | 301 // or min to get the correct result. |
| 324 int64 effective_begin_time = options.begin_time.is_null() ? | 302 int64 effective_begin_time = options.begin_time.is_null() ? |
| 325 0 : options.begin_time.ToInternalValue(); | 303 0 : options.begin_time.ToInternalValue(); |
| 326 int64 effective_end_time = options.end_time.is_null() ? | 304 int64 effective_end_time = options.end_time.is_null() ? |
| 327 std::numeric_limits<int64>::max() : options.end_time.ToInternalValue(); | 305 std::numeric_limits<int64>::max() : options.end_time.ToInternalValue(); |
| 328 int effective_max_count = options.max_count ? | 306 int effective_max_count = options.max_count ? |
| 329 options.max_count : std::numeric_limits<int>::max(); | 307 options.max_count : std::numeric_limits<int>::max(); |
| 330 | 308 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 } else { | 356 } else { |
| 379 // Since we got the results in order, we know the last item is the last | 357 // Since we got the results in order, we know the last item is the last |
| 380 // time we considered. | 358 // time we considered. |
| 381 *first_time_searched = results->back().time; | 359 *first_time_searched = results->back().time; |
| 382 } | 360 } |
| 383 | 361 |
| 384 statement.Reset(); | 362 statement.Reset(); |
| 385 } | 363 } |
| 386 | 364 |
| 387 } // namespace history | 365 } // namespace history |
| OLD | NEW |