OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "sql/connection.h" | 5 #include "sql/connection.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "base/lazy_instance.h" | |
15 #include "base/scoped_temp_dir.h" | |
14 #include "sql/statement.h" | 16 #include "sql/statement.h" |
15 #include "third_party/sqlite/sqlite3.h" | 17 #include "third_party/sqlite/sqlite3.h" |
16 | 18 |
19 #if defined(OS_ANDROID) | |
20 // The variable defined in sqlite3 used to set the temporary directory. | |
21 // On Android, since there is no public temporary directory, set the | |
22 // private temporary directory. | |
23 extern char* sqlite3_temp_directory; | |
Scott Hess - ex-Googler
2012/07/19 19:47:21
This is in sqlite3.h, why do you need it here? Al
yongsheng
2012/07/20 01:25:24
should be removed.
| |
24 #endif | |
25 | |
17 namespace { | 26 namespace { |
18 | 27 |
28 #if defined(OS_ANDROID) | |
29 class TempDirSetter { | |
30 public: | |
31 static void SetTempDir() { | |
32 if (!is_set_) { | |
33 if (scoped_temp_dir_.Get().CreateUniqueTempDir()) { | |
34 const char* dir = scoped_temp_dir_.Pointer()->path().value().c_str(); | |
35 sqlite3_temp_directory = const_cast<char*>(dir); | |
36 } else { | |
37 LOG(WARNING) << "unable to create temporary directory for sql"; | |
38 } | |
39 is_set_ = true; | |
40 } | |
41 } | |
42 private: | |
43 static bool is_set_; | |
44 static base::LazyInstance<ScopedTempDir> scoped_temp_dir_; | |
45 }; | |
46 bool TempDirSetter::is_set_ = false; | |
47 base::LazyInstance<ScopedTempDir> TempDirSetter::scoped_temp_dir_ = | |
48 LAZY_INSTANCE_INITIALIZER; | |
Scott Hess - ex-Googler
2012/07/19 19:47:21
All the methods and variables are static. Why do
yongsheng
2012/07/20 01:25:24
ok.
let's wait for the investigation result about
| |
49 #endif | |
50 | |
19 // Spin for up to a second waiting for the lock to clear when setting | 51 // Spin for up to a second waiting for the lock to clear when setting |
20 // up the database. | 52 // up the database. |
21 // TODO(shess): Better story on this. http://crbug.com/56559 | 53 // TODO(shess): Better story on this. http://crbug.com/56559 |
22 const int kBusyTimeoutSeconds = 1; | 54 const int kBusyTimeoutSeconds = 1; |
23 | 55 |
24 class ScopedBusyTimeout { | 56 class ScopedBusyTimeout { |
25 public: | 57 public: |
26 explicit ScopedBusyTimeout(sqlite3* db) | 58 explicit ScopedBusyTimeout(sqlite3* db) |
27 : db_(db) { | 59 : db_(db) { |
28 } | 60 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 connection_ = NULL; // The connection may be getting deleted. | 114 connection_ = NULL; // The connection may be getting deleted. |
83 } | 115 } |
84 | 116 |
85 Connection::Connection() | 117 Connection::Connection() |
86 : db_(NULL), | 118 : db_(NULL), |
87 page_size_(0), | 119 page_size_(0), |
88 cache_size_(0), | 120 cache_size_(0), |
89 exclusive_locking_(false), | 121 exclusive_locking_(false), |
90 transaction_nesting_(0), | 122 transaction_nesting_(0), |
91 needs_rollback_(false) { | 123 needs_rollback_(false) { |
124 #if defined(OS_ANDROID) | |
125 TempDirSetter::SetTempDir(); | |
126 #endif | |
92 } | 127 } |
93 | 128 |
94 Connection::~Connection() { | 129 Connection::~Connection() { |
95 Close(); | 130 Close(); |
96 } | 131 } |
97 | 132 |
98 bool Connection::Open(const FilePath& path) { | 133 bool Connection::Open(const FilePath& path) { |
99 #if defined(OS_WIN) | 134 #if defined(OS_WIN) |
100 return OpenInternal(WideToUTF8(path.value())); | 135 return OpenInternal(WideToUTF8(path.value())); |
101 #elif defined(OS_POSIX) | 136 #elif defined(OS_POSIX) |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
549 | 584 |
550 int Connection::OnSqliteError(int err, sql::Statement *stmt) { | 585 int Connection::OnSqliteError(int err, sql::Statement *stmt) { |
551 if (error_delegate_.get()) | 586 if (error_delegate_.get()) |
552 return error_delegate_->OnError(err, this, stmt); | 587 return error_delegate_->OnError(err, this, stmt); |
553 // The default handling is to assert on debug and to ignore on release. | 588 // The default handling is to assert on debug and to ignore on release. |
554 DLOG(FATAL) << GetErrorMessage(); | 589 DLOG(FATAL) << GetErrorMessage(); |
555 return err; | 590 return err; |
556 } | 591 } |
557 | 592 |
558 } // namespace sql | 593 } // namespace sql |
OLD | NEW |