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

Side by Side Diff: sql/scoped_attach.cc

Issue 18180013: Scoped recovery module for sql/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Oops, add recover_unittest.cc Created 7 years, 5 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
OLDNEW
(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 #include "sql/scoped_attach.h"
6
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "sql/connection.h"
10 #include "sql/statement.h"
11
12 namespace sql {
13
14 ScopedAttach::ScopedAttach(Connection* connection)
15 : db_(connection),
16 attached_as_() {
17 }
18
19 ScopedAttach::~ScopedAttach() {
20 Detach();
21 }
22
23 bool ScopedAttach::Attach(const base::FilePath& other_db_path,
24 const char* attach_as) {
25 DCHECK(attached_as_.empty());
26
27 // Be strict on attachment point.
28 for (size_t i = 0; attach_as[i]; ++i) {
29 DCHECK((attach_as[i] >= '0' && attach_as[i] <= '9') ||
30 (attach_as[i] >= 'a' && attach_as[i] <= 'z') ||
31 (attach_as[i] >= 'A' && attach_as[i] <= 'Z') ||
32 attach_as[i] == '_');
33 }
34
35 Statement s(db_->GetUniqueStatement("ATTACH DATABASE ? AS ?"));
36 #if OS_WIN
37 s.BindString16(0, other_db_path.value());
38 #else
39 s.BindString(0, other_db_path.value());
40 #endif
41 s.BindString(1, attach_as);
42 if (!s.Run()) {
43 LOG(ERROR) << "Failed attach.";
44 return false;
45 }
46
47 attached_as_ = attach_as;
48 return true;
49 }
50
51 void ScopedAttach::Detach() {
52 if (!attached_as_.empty()) {
53 Statement s(db_->GetUniqueStatement("DETACH DATABASE ?"));
54 s.BindString(0, attached_as_);
55 if (!s.Run())
56 LOG(ERROR) << "Failed detach.";
57 attached_as_.clear();
58 }
59 }
60
61 } // namespace sql
OLDNEW
« sql/recovery.h ('K') | « sql/scoped_attach.h ('k') | sql/sql.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698