OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "components/history/core/browser/typed_url_sync_metadata_database.h" | |
6 | |
7 #include "sql/statement.h" | |
8 | |
9 namespace history { | |
10 | |
11 // Description of database table: | |
12 // | |
13 // typed_url_sync_metadata | |
14 // storage_key the rowid of an entry in urls table, used by service to | |
15 // look up native data with sync metadata records. | |
16 // value Serialize sync EntityMetadata, which is for tracking sync | |
17 // state of each typed url. | |
18 // | |
19 // typed_url_model_type_state | |
20 // id should only one record in the table and record's id should | |
21 // always be 1. | |
22 // value Serialization of sync ModelTypeState, which is for | |
23 // tracking sync state of typed url datatype. | |
24 | |
25 TypedURLSyncMetadataDatabase::TypedURLSyncMetadataDatabase() {} | |
26 | |
27 TypedURLSyncMetadataDatabase::~TypedURLSyncMetadataDatabase() {} | |
28 | |
29 bool TypedURLSyncMetadataDatabase::GetAllSyncMetadata( | |
30 syncer::MetadataBatch* metadata_batch) { | |
31 syncer::EntityMetadataMap metadata_records_map; | |
32 if (GetAllSyncEntityMetadata(&metadata_records_map)) { | |
33 for (const auto& storayge_key_to_metadata : metadata_records_map) | |
brettw
2017/03/14 19:24:43
It's a little bit hard for me to give good suggest
Gang Wu
2017/03/15 08:02:43
This is really a good suggestion!
But since add t
brettw
2017/03/20 17:27:06
Following up with another patch for this is OK to
Gang Wu
2017/03/20 21:34:47
yes, I talked with team, I think I will fix this i
| |
34 metadata_batch->AddMetadata(storayge_key_to_metadata.first, | |
35 storayge_key_to_metadata.second); | |
36 } else { | |
37 return false; | |
38 } | |
39 | |
40 sync_pb::ModelTypeState model_type_state; | |
41 if (GetModelTypeState(&model_type_state)) { | |
42 metadata_batch->SetModelTypeState(model_type_state); | |
43 } else { | |
44 return false; | |
45 } | |
46 | |
47 return true; | |
48 } | |
49 | |
50 bool TypedURLSyncMetadataDatabase::UpdateSyncMetadata( | |
51 const std::string& storage_key, | |
52 const sync_pb::EntityMetadata& metadata) { | |
53 sql::Statement s(GetDB().GetUniqueStatement( | |
54 "INSERT OR REPLACE INTO typed_url_sync_metadata " | |
55 "(storage_key, value) VALUES(?, ?)")); | |
56 s.BindString(0, storage_key); | |
57 s.BindString(1, metadata.SerializeAsString()); | |
58 | |
59 return s.Run(); | |
60 } | |
61 | |
62 bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( | |
63 const std::string& storage_key) { | |
64 sql::Statement s(GetDB().GetUniqueStatement( | |
65 "DELETE FROM typed_url_sync_metadata WHERE storage_key=?")); | |
66 s.BindString(0, storage_key); | |
67 | |
68 return s.Run(); | |
69 } | |
70 | |
71 bool TypedURLSyncMetadataDatabase::UpdateModelTypeState( | |
72 const sync_pb::ModelTypeState& model_type_state) { | |
73 // Hardcode the id to force a collision, ensuring that there remains only a | |
74 // single entry. | |
75 sql::Statement s(GetDB().GetUniqueStatement( | |
76 "INSERT OR REPLACE INTO typed_url_model_type_state (id, value) " | |
77 "VALUES(1,?)")); | |
78 s.BindString(0, model_type_state.SerializeAsString()); | |
79 | |
80 return s.Run(); | |
81 } | |
82 | |
83 bool TypedURLSyncMetadataDatabase::ClearModelTypeState() { | |
84 sql::Statement s(GetDB().GetUniqueStatement( | |
85 "DELETE FROM typed_url_model_type_state WHERE id=1")); | |
86 | |
87 return s.Run(); | |
88 } | |
89 | |
90 bool TypedURLSyncMetadataDatabase::InitSyncTable() { | |
91 return InitTypedURLSyncMetadataTable() && InitModelTypeStateTable(); | |
92 } | |
93 | |
94 bool TypedURLSyncMetadataDatabase::InitTypedURLSyncMetadataTable() { | |
95 if (!GetDB().DoesTableExist("typed_url_sync_metadata")) { | |
96 if (!GetDB().Execute("CREATE TABLE typed_url_sync_metadata (" | |
97 "storage_key VARCHAR PRIMARY KEY NOT NULL," | |
98 "value BLOB)")) { | |
99 NOTREACHED(); | |
100 return false; | |
101 } | |
102 } | |
103 return true; | |
104 } | |
105 | |
106 bool TypedURLSyncMetadataDatabase::InitModelTypeStateTable() { | |
107 if (!GetDB().DoesTableExist("typed_url_model_type_state")) { | |
108 if (!GetDB().Execute("CREATE TABLE typed_url_model_type_state (id INTEGER " | |
109 "PRIMARY KEY, value BLOB)")) { | |
110 NOTREACHED(); | |
111 return false; | |
112 } | |
113 } | |
114 return true; | |
115 } | |
116 | |
117 bool TypedURLSyncMetadataDatabase::GetAllSyncEntityMetadata( | |
118 syncer::EntityMetadataMap* metadata_records) { | |
119 sql::Statement s(GetDB().GetUniqueStatement( | |
120 "SELECT storage_key, value FROM typed_url_sync_metadata")); | |
121 | |
122 while (s.Step()) { | |
123 std::string storage_key = s.ColumnString(0); | |
124 std::string serialized_metadata = s.ColumnString(1); | |
125 sync_pb::EntityMetadata metadata_record; | |
126 if (metadata_record.ParseFromString(serialized_metadata)) { | |
127 metadata_records->insert(std::make_pair(storage_key, metadata_record)); | |
128 } else { | |
129 return false; | |
130 } | |
131 } | |
132 return true; | |
133 } | |
134 | |
135 bool TypedURLSyncMetadataDatabase::GetModelTypeState( | |
136 sync_pb::ModelTypeState* state) { | |
137 sql::Statement s(GetDB().GetUniqueStatement( | |
138 "SELECT value FROM typed_url_model_type_state WHERE id=1")); | |
139 | |
140 if (!s.Step()) { | |
141 return true; | |
142 } | |
143 | |
144 std::string serialized_state = s.ColumnString(0); | |
145 return state->ParseFromString(serialized_state); | |
146 } | |
147 | |
148 } // namespace history | |
OLD | NEW |