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
3// found in the LICENSE file.
4
5#include "chrome/browser/history/android/android_urls_sql_handler.h"
6
7#include "base/logging.h"
8#include "chrome/browser/history/history_database.h"
9
10namespace history {
11
12namespace {
13
14// The interesting columns of this handler.
15const HistoryAndBookmarkRow::ColumnID kInterestingColumns[] = {
16    HistoryAndBookmarkRow::RAW_URL, HistoryAndBookmarkRow::URL_ID };
17
18} // namespace
19
20AndroidURLsSQLHandler::AndroidURLsSQLHandler(HistoryDatabase* history_db)
21    : SQLHandler(kInterestingColumns, arraysize(kInterestingColumns)),
22      history_db_(history_db) {
23}
24
25AndroidURLsSQLHandler::~AndroidURLsSQLHandler() {
26}
27
28bool AndroidURLsSQLHandler::Update(const HistoryAndBookmarkRow& row,
29                                   const TableIDRows& ids_set) {
30  DCHECK(row.is_value_set_explicitly(HistoryAndBookmarkRow::URL_ID));
31  DCHECK(row.is_value_set_explicitly(HistoryAndBookmarkRow::RAW_URL));
32  if (ids_set.size() != 1)
33    return false;
34
35  AndroidURLRow android_url_row;
36  if (!history_db_->GetAndroidURLRow(ids_set[0].url_id, &android_url_row))
37    return false;
38
39  return history_db_->UpdateAndroidURLRow(android_url_row.id, row.raw_url(),
40                                          row.url_id());
41}
42
43bool AndroidURLsSQLHandler::Insert(HistoryAndBookmarkRow* row) {
44  AndroidURLID new_id = history_db_->AddAndroidURLRow(row->raw_url(),
45                                                      row->url_id());
46  row->set_id(new_id);
47  return new_id;
48}
49
50bool AndroidURLsSQLHandler::Delete(const TableIDRows& ids_set) {
51  std::vector<URLID> ids;
52  for (TableIDRows::const_iterator id = ids_set.begin();
53       id != ids_set.end(); ++id)
54    ids.push_back(id->url_id);
55
56  if (!ids.size())
57    return true;
58
59  return history_db_->DeleteAndroidURLRows(ids);
60}
61
62}  // namespace history.
63