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 #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 |
OLD | NEW |