Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 SQL_RECOVERY_H_ | |
| 6 #define SQL_RECOVERY_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 | |
| 10 #include "sql/connection.h" | |
| 11 #include "sql/scoped_attach.h" | |
| 12 | |
| 13 namespace base { | |
| 14 class FilePath; | |
| 15 } | |
| 16 | |
| 17 namespace sql { | |
| 18 | |
| 19 // Recovery module for sql/. The basic idea is that a fresh database | |
| 20 // is created, which is populated with the recovered contents of the | |
| 21 // original database, and if recovery is successful the recovered | |
| 22 // database is backed up over the original database. If recovery is | |
| 23 // not successful, the original database is razed. | |
| 24 // | |
| 25 // Usage is something like: | |
| 26 // { | |
| 27 // sql::Recovery recover(orig_db); | |
| 28 // if (recover.Open(orig_db_path)) { | |
| 29 // if (recover.db()->Execute(kCreateSchemaSql) && | |
| 30 // recover.db()->Execute(kCopyDataFromOrigSql)) { | |
| 31 // recover.Recovered(); | |
| 32 // } | |
| 33 // } | |
| 34 // } | |
| 35 // | |
| 36 // If Recovered() does not return successfully, then RazeAndClose() is | |
| 37 // called on orig_db. | |
| 38 | |
| 39 | |
| 40 // TODO(shess): Move the scoper to a separate file. | |
| 41 class SQL_EXPORT Recovery { | |
|
erikwright (departed)
2013/07/05 19:02:30
An idiom I've been using from time to time is like
Scott Hess - ex-Googler
2013/07/08 21:41:51
I'll prototype that and see how it looks. My main
| |
| 42 public: | |
| 43 // TODO(shess): Allow specifying the attachment point? | |
| 44 explicit Recovery(Connection* connection); | |
| 45 ~Recovery(); | |
| 46 | |
| 47 // TODO(shess): Later versions of SQLite allow querying for the | |
| 48 // existing db's path. | |
| 49 bool Open(const base::FilePath& db_path) WARN_UNUSED_RESULT; | |
| 50 | |
| 51 // Mark recovery completed by replicating the recovery database over | |
| 52 // the original database, then closing the recovery database. The | |
| 53 // original database is closed and the handle poisoned, causing | |
| 54 // future calls against it to fail. | |
| 55 // | |
| 56 // If this is not called before the object goes out of scope, the | |
| 57 // destructor will call Unrecoverable(). | |
| 58 // TODO(shess): What does it mean for this to fail? Should there be | |
| 59 // an option for "Bail out without razing the original database"? | |
| 60 bool Recovered() WARN_UNUSED_RESULT; | |
| 61 | |
| 62 // Indicate that the database is unrecoverable. The original | |
| 63 // database is razed, and the handle is closed and poisoned. The | |
| 64 // recovery database is also closed. | |
| 65 void Unrecoverable(); | |
| 66 | |
| 67 // Handle to the temporary recovery database. | |
| 68 sql::Connection* db() { return &recover_db_; } | |
| 69 | |
| 70 private: | |
| 71 Connection* db_; | |
| 72 Connection recover_db_; | |
| 73 ScopedAttach attach_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(Recovery); | |
| 76 }; | |
| 77 | |
| 78 } // namespace sql | |
| 79 | |
| 80 #endif // SQL_RECOVERY_H_ | |
| OLD | NEW |