Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: chrome/browser/history/starred_url_database.cc

Issue 9071014: Database usage adjustment for .../history (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Simplify SQL Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/history/shortcuts_database.cc ('k') | chrome/browser/history/text_database.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "chrome/browser/history/starred_url_database.h" 5 #include "chrome/browser/history/starred_url_database.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/json/json_writer.h" 8 #include "base/json/json_writer.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_vector.h" 10 #include "base/memory/scoped_vector.h"
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 143
144 bool StarredURLDatabase::GetAllStarredEntries( 144 bool StarredURLDatabase::GetAllStarredEntries(
145 std::vector<StarredEntry>* entries) { 145 std::vector<StarredEntry>* entries) {
146 DCHECK(entries); 146 DCHECK(entries);
147 std::string sql = "SELECT "; 147 std::string sql = "SELECT ";
148 sql.append(kHistoryStarFields); 148 sql.append(kHistoryStarFields);
149 sql.append("FROM starred LEFT JOIN urls ON starred.url_id = urls.id "); 149 sql.append("FROM starred LEFT JOIN urls ON starred.url_id = urls.id ");
150 sql += "ORDER BY parent_id, visual_order"; 150 sql += "ORDER BY parent_id, visual_order";
151 151
152 sql::Statement s(GetDB().GetUniqueStatement(sql.c_str())); 152 sql::Statement s(GetDB().GetUniqueStatement(sql.c_str()));
153 if (!s) { 153 if (!s.is_valid())
154 NOTREACHED() << "Statement prepare failed";
155 return false; 154 return false;
156 }
157 155
158 history::StarredEntry entry; 156 history::StarredEntry entry;
159 while (s.Step()) { 157 while (s.Step()) {
160 FillInStarredEntry(s, &entry); 158 FillInStarredEntry(s, &entry);
161 // Reset the url for non-url types. This is needed as we're reusing the 159 // Reset the url for non-url types. This is needed as we're reusing the
162 // same entry for the loop. 160 // same entry for the loop.
163 if (entry.type != history::StarredEntry::URL) 161 if (entry.type != history::StarredEntry::URL)
164 entry.url = GURL(); 162 entry.url = GURL();
165 entries->push_back(entry); 163 entries->push_back(entry);
166 } 164 }
(...skipping 25 matching lines...) Expand all
192 190
193 bool StarredURLDatabase::UpdateStarredEntryRow(StarID star_id, 191 bool StarredURLDatabase::UpdateStarredEntryRow(StarID star_id,
194 const string16& title, 192 const string16& title,
195 UIStarID parent_folder_id, 193 UIStarID parent_folder_id,
196 int visual_order, 194 int visual_order,
197 base::Time date_modified) { 195 base::Time date_modified) {
198 DCHECK(star_id && visual_order >= 0); 196 DCHECK(star_id && visual_order >= 0);
199 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 197 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
200 "UPDATE starred SET title=?, parent_id=?, visual_order=?, " 198 "UPDATE starred SET title=?, parent_id=?, visual_order=?, "
201 "date_modified=? WHERE id=?")); 199 "date_modified=? WHERE id=?"));
202 if (!statement)
203 return 0;
204
205 statement.BindString16(0, title); 200 statement.BindString16(0, title);
206 statement.BindInt64(1, parent_folder_id); 201 statement.BindInt64(1, parent_folder_id);
207 statement.BindInt(2, visual_order); 202 statement.BindInt(2, visual_order);
208 statement.BindInt64(3, date_modified.ToInternalValue()); 203 statement.BindInt64(3, date_modified.ToInternalValue());
209 statement.BindInt64(4, star_id); 204 statement.BindInt64(4, star_id);
205
210 return statement.Run(); 206 return statement.Run();
211 } 207 }
212 208
213 bool StarredURLDatabase::AdjustStarredVisualOrder(UIStarID parent_folder_id, 209 bool StarredURLDatabase::AdjustStarredVisualOrder(UIStarID parent_folder_id,
214 int start_visual_order, 210 int start_visual_order,
215 int delta) { 211 int delta) {
216 DCHECK(parent_folder_id && start_visual_order >= 0); 212 DCHECK(parent_folder_id && start_visual_order >= 0);
217 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 213 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
218 "UPDATE starred SET visual_order=visual_order+? " 214 "UPDATE starred SET visual_order=visual_order+? "
219 "WHERE parent_id=? AND visual_order >= ?")); 215 "WHERE parent_id=? AND visual_order >= ?"));
220 if (!statement)
221 return false;
222
223 statement.BindInt(0, delta); 216 statement.BindInt(0, delta);
224 statement.BindInt64(1, parent_folder_id); 217 statement.BindInt64(1, parent_folder_id);
225 statement.BindInt(2, start_visual_order); 218 statement.BindInt(2, start_visual_order);
219
226 return statement.Run(); 220 return statement.Run();
227 } 221 }
228 222
229 StarID StarredURLDatabase::CreateStarredEntryRow(URLID url_id, 223 StarID StarredURLDatabase::CreateStarredEntryRow(URLID url_id,
230 UIStarID folder_id, 224 UIStarID folder_id,
231 UIStarID parent_folder_id, 225 UIStarID parent_folder_id,
232 const string16& title, 226 const string16& title,
233 const base::Time& date_added, 227 const base::Time& date_added,
234 int visual_order, 228 int visual_order,
235 StarredEntry::Type type) { 229 StarredEntry::Type type) {
236 DCHECK(visual_order >= 0 && 230 DCHECK(visual_order >= 0 &&
237 (type != history::StarredEntry::URL || url_id)); 231 (type != history::StarredEntry::URL || url_id));
238 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 232 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
239 "INSERT INTO starred " 233 "INSERT INTO starred "
240 "(type, url_id, group_id, title, date_added, visual_order, parent_id, " 234 "(type, url_id, group_id, title, date_added, visual_order, parent_id, "
241 "date_modified) VALUES (?,?,?,?,?,?,?,?)")); 235 "date_modified) VALUES (?,?,?,?,?,?,?,?)"));
242 if (!statement)
243 return 0;
244 236
245 switch (type) { 237 switch (type) {
246 case history::StarredEntry::URL: 238 case history::StarredEntry::URL:
247 statement.BindInt(0, 0); 239 statement.BindInt(0, 0);
248 break; 240 break;
249 case history::StarredEntry::BOOKMARK_BAR: 241 case history::StarredEntry::BOOKMARK_BAR:
250 statement.BindInt(0, 1); 242 statement.BindInt(0, 1);
251 break; 243 break;
252 case history::StarredEntry::USER_FOLDER: 244 case history::StarredEntry::USER_FOLDER:
253 statement.BindInt(0, 2); 245 statement.BindInt(0, 2);
254 break; 246 break;
255 case history::StarredEntry::OTHER: 247 case history::StarredEntry::OTHER:
256 statement.BindInt(0, 3); 248 statement.BindInt(0, 3);
257 break; 249 break;
258 default: 250 default:
259 NOTREACHED(); 251 NOTREACHED();
260 } 252 }
261 statement.BindInt64(1, url_id); 253 statement.BindInt64(1, url_id);
262 statement.BindInt64(2, folder_id); 254 statement.BindInt64(2, folder_id);
263 statement.BindString16(3, title); 255 statement.BindString16(3, title);
264 statement.BindInt64(4, date_added.ToInternalValue()); 256 statement.BindInt64(4, date_added.ToInternalValue());
265 statement.BindInt(5, visual_order); 257 statement.BindInt(5, visual_order);
266 statement.BindInt64(6, parent_folder_id); 258 statement.BindInt64(6, parent_folder_id);
267 statement.BindInt64(7, base::Time().ToInternalValue()); 259 statement.BindInt64(7, base::Time().ToInternalValue());
260
268 if (statement.Run()) 261 if (statement.Run())
269 return GetDB().GetLastInsertRowId(); 262 return GetDB().GetLastInsertRowId();
270 return 0; 263 return 0;
271 } 264 }
272 265
273 bool StarredURLDatabase::DeleteStarredEntryRow(StarID star_id) { 266 bool StarredURLDatabase::DeleteStarredEntryRow(StarID star_id) {
274 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 267 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
275 "DELETE FROM starred WHERE id=?")); 268 "DELETE FROM starred WHERE id=?"));
276 if (!statement) 269 statement.BindInt64(0, star_id);
277 return false;
278 270
279 statement.BindInt64(0, star_id);
280 return statement.Run(); 271 return statement.Run();
281 } 272 }
282 273
283 bool StarredURLDatabase::GetStarredEntry(StarID star_id, StarredEntry* entry) { 274 bool StarredURLDatabase::GetStarredEntry(StarID star_id, StarredEntry* entry) {
284 DCHECK(entry && star_id); 275 DCHECK(entry && star_id);
285 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 276 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
286 "SELECT" STAR_FIELDS "FROM starred LEFT JOIN urls ON " 277 "SELECT" STAR_FIELDS "FROM starred LEFT JOIN urls ON "
287 "starred.url_id = urls.id WHERE starred.id=?")); 278 "starred.url_id = urls.id WHERE starred.id=?"));
288 if (!statement)
289 return false;
290
291 statement.BindInt64(0, star_id); 279 statement.BindInt64(0, star_id);
292 280
293 if (statement.Step()) { 281 if (statement.Step()) {
294 FillInStarredEntry(statement, entry); 282 FillInStarredEntry(statement, entry);
295 return true; 283 return true;
296 } 284 }
297 return false; 285 return false;
298 } 286 }
299 287
300 StarID StarredURLDatabase::CreateStarredEntry(StarredEntry* entry) { 288 StarID StarredURLDatabase::CreateStarredEntry(StarredEntry* entry) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 default: 326 default:
339 NOTREACHED(); 327 NOTREACHED();
340 break; 328 break;
341 } 329 }
342 return entry->id; 330 return entry->id;
343 } 331 }
344 332
345 UIStarID StarredURLDatabase::GetMaxFolderID() { 333 UIStarID StarredURLDatabase::GetMaxFolderID() {
346 sql::Statement max_folder_id_statement(GetDB().GetUniqueStatement( 334 sql::Statement max_folder_id_statement(GetDB().GetUniqueStatement(
347 "SELECT MAX(group_id) FROM starred")); 335 "SELECT MAX(group_id) FROM starred"));
348 if (!max_folder_id_statement) { 336
349 NOTREACHED() << GetDB().GetErrorMessage();
350 return 0;
351 }
352 if (!max_folder_id_statement.Step()) { 337 if (!max_folder_id_statement.Step()) {
353 NOTREACHED() << GetDB().GetErrorMessage();
354 return 0; 338 return 0;
355 } 339 }
356 return max_folder_id_statement.ColumnInt64(0); 340 return max_folder_id_statement.ColumnInt64(0);
357 } 341 }
358 342
359 bool StarredURLDatabase::BuildStarNodes( 343 bool StarredURLDatabase::BuildStarNodes(
360 std::set<StarredURLDatabase::StarredNode*>* roots, 344 std::set<StarredURLDatabase::StarredNode*>* roots,
361 std::set<StarID>* folders_with_duplicate_ids, 345 std::set<StarID>* folders_with_duplicate_ids,
362 std::set<StarredNode*>* unparented_urls, 346 std::set<StarredNode*>* unparented_urls,
363 std::set<StarID>* empty_url_ids) { 347 std::set<StarID>* empty_url_ids) {
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 scoped_ptr<Value> encoded_bookmarks( 650 scoped_ptr<Value> encoded_bookmarks(
667 encoder.Encode(&bookmark_bar_node, &other_node, &mobile_node)); 651 encoder.Encode(&bookmark_bar_node, &other_node, &mobile_node));
668 std::string content; 652 std::string content;
669 base::JSONWriter::Write(encoded_bookmarks.get(), true, &content); 653 base::JSONWriter::Write(encoded_bookmarks.get(), true, &content);
670 654
671 return (file_util::WriteFile(path, content.c_str(), 655 return (file_util::WriteFile(path, content.c_str(),
672 static_cast<int>(content.length())) != -1); 656 static_cast<int>(content.length())) != -1);
673 } 657 }
674 658
675 } // namespace history 659 } // namespace history
OLDNEW
« no previous file with comments | « chrome/browser/history/shortcuts_database.cc ('k') | chrome/browser/history/text_database.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698