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 #ifndef COMPONENTS_HISTORY_CORE_BROWSER_ANDROID_SQL_HANDLER_H_
6 #define COMPONENTS_HISTORY_CORE_BROWSER_ANDROID_SQL_HANDLER_H_
7 
8 #include "base/macros.h"
9 #include "components/history/core/browser/android/android_history_types.h"
10 
11 namespace history {
12 
13 // This is a wrapper of information needed for Insert/Update/Delete
14 // method in SQLHandler. Refer to SQLHandler's comment below for how
15 // it is used.
16 struct TableIDRow {
17   TableIDRow();
18   ~TableIDRow();
19 
20   URLID url_id;
21   GURL url;
22   // Whether the URL was bookmarked.
23   bool bookmarked;
24 };
25 
26 typedef std::vector<TableIDRow> TableIDRows;
27 
28 // This base class is used by AndroidProviderBackend to manipulate the indvidual
29 // table or BookmarkModel in its Insert/Update/Delete method.
30 //
31 // The implementation needs to provides an array of columns. Once the columns
32 // are inserted or updated, the corresponding Insert() or Update() method will
33 // be invoked. The Delete() method is called to delete rows.
34 //
35 // The HistoryAndBookmarkRow given in Insert() or Update() provide the data for
36 // insert or update. No all the data in HistoryAndBookmarkRow maybe valid, using
37 // HistoryAndBookmarkRow::is_value_set_explicitly() method to see if the data
38 // need be inserted or updated.
39 class SQLHandler {
40  public:
41   // |columns| is the implementation's columns.
42   // |column_count| is the number of column in |columns|.
43   SQLHandler(const HistoryAndBookmarkRow::ColumnID columns[], int column_count);
44   virtual ~SQLHandler();
45 
46   // Updates the rows whose URLID or URL is in the given |ids_set| with new
47   // value stored in |row|. Return true if the update succeeds.
48   virtual bool Update(const HistoryAndBookmarkRow& row,
49                       const TableIDRows& ids_set) = 0;
50 
51   // Inserts the given |row|, return true on success; The id of insertted row
52   // should be set in |row|, so other implemnetations could use it to complete
53   // the insert.
54   virtual bool Insert(HistoryAndBookmarkRow* row) = 0;
55 
56   // Deletes the rows whose id is in |ids_set|, returns false if any deletion
57   // failed, otherwise return true even all/some of rows are not found.
58   virtual bool Delete(const TableIDRows& ids_set) = 0;
59 
60   // Return true if |row| has a value explicitly set for at least one of the
61   // columns in |row| that are known to this class.
62   bool HasColumnIn(const HistoryAndBookmarkRow& row);
63 
64   // Returns true if |id| is one of the columns known to this class.
65   bool HasColumn(HistoryAndBookmarkRow::ColumnID id);
66 
67  private:
68   // The columns of this handler.
69   const std::set<HistoryAndBookmarkRow::ColumnID> columns_;
70 
71   DISALLOW_COPY_AND_ASSIGN(SQLHandler);
72 };
73 
74 }  // namespace history.
75 
76 #endif  // COMPONENTS_HISTORY_CORE_BROWSER_ANDROID_SQL_HANDLER_H_
77