[email protected] | 6402104 | 2012-02-10 20:02:29 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 5 | #include "sql/connection.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 6 | |
| 7 | #include <string.h> |
| 8 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | 348ac8f5 | 2013-05-21 03:27:02 | [diff] [blame] | 10 | #include "base/file_util.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | bd2ccdb4a | 2012-12-07 22:14:50 | [diff] [blame] | 12 | #include "base/metrics/histogram.h" |
[email protected] | 210ce0af | 2013-05-15 09:10:39 | [diff] [blame] | 13 | #include "base/metrics/sparse_histogram.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 14 | #include "base/string_util.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 15 | #include "base/stringprintf.h" |
[email protected] | 80abf15 | 2013-05-22 12:42:42 | [diff] [blame^] | 16 | #include "base/strings/string_split.h" |
[email protected] | d55194ca | 2010-03-11 18:25:45 | [diff] [blame] | 17 | #include "base/utf_string_conversions.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 18 | #include "sql/statement.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 19 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 20 | |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | // Spin for up to a second waiting for the lock to clear when setting |
| 24 | // up the database. |
| 25 | // TODO(shess): Better story on this. http://crbug.com/56559 |
[email protected] | c68ce17 | 2011-11-24 22:30:27 | [diff] [blame] | 26 | const int kBusyTimeoutSeconds = 1; |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 27 | |
| 28 | class ScopedBusyTimeout { |
| 29 | public: |
| 30 | explicit ScopedBusyTimeout(sqlite3* db) |
| 31 | : db_(db) { |
| 32 | } |
| 33 | ~ScopedBusyTimeout() { |
| 34 | sqlite3_busy_timeout(db_, 0); |
| 35 | } |
| 36 | |
| 37 | int SetTimeout(base::TimeDelta timeout) { |
| 38 | DCHECK_LT(timeout.InMilliseconds(), INT_MAX); |
| 39 | return sqlite3_busy_timeout(db_, |
| 40 | static_cast<int>(timeout.InMilliseconds())); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | sqlite3* db_; |
| 45 | }; |
| 46 | |
[email protected] | 6d42f15 | 2012-11-10 00:38:24 | [diff] [blame] | 47 | // Helper to "safely" enable writable_schema. No error checking |
| 48 | // because it is reasonable to just forge ahead in case of an error. |
| 49 | // If turning it on fails, then most likely nothing will work, whereas |
| 50 | // if turning it off fails, it only matters if some code attempts to |
| 51 | // continue working with the database and tries to modify the |
| 52 | // sqlite_master table (none of our code does this). |
| 53 | class ScopedWritableSchema { |
| 54 | public: |
| 55 | explicit ScopedWritableSchema(sqlite3* db) |
| 56 | : db_(db) { |
| 57 | sqlite3_exec(db_, "PRAGMA writable_schema=1", NULL, NULL, NULL); |
| 58 | } |
| 59 | ~ScopedWritableSchema() { |
| 60 | sqlite3_exec(db_, "PRAGMA writable_schema=0", NULL, NULL, NULL); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | sqlite3* db_; |
| 65 | }; |
| 66 | |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 67 | } // namespace |
| 68 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 69 | namespace sql { |
| 70 | |
| 71 | bool StatementID::operator<(const StatementID& other) const { |
| 72 | if (number_ != other.number_) |
| 73 | return number_ < other.number_; |
| 74 | return strcmp(str_, other.str_) < 0; |
| 75 | } |
| 76 | |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 77 | ErrorDelegate::~ErrorDelegate() { |
| 78 | } |
| 79 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 80 | Connection::StatementRef::StatementRef(Connection* connection, |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 81 | sqlite3_stmt* stmt, |
| 82 | bool was_valid) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 83 | : connection_(connection), |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 84 | stmt_(stmt), |
| 85 | was_valid_(was_valid) { |
| 86 | if (connection) |
| 87 | connection_->StatementRefCreated(this); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | Connection::StatementRef::~StatementRef() { |
| 91 | if (connection_) |
| 92 | connection_->StatementRefDeleted(this); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 93 | Close(false); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 96 | void Connection::StatementRef::Close(bool forced) { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 97 | if (stmt_) { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 98 | // Call to AssertIOAllowed() cannot go at the beginning of the function |
| 99 | // because Close() is called unconditionally from destructor to clean |
| 100 | // connection_. And if this is inactive statement this won't cause any |
| 101 | // disk access and destructor most probably will be called on thread |
| 102 | // not allowing disk access. |
| 103 | // TODO([email protected]): This should move to the beginning |
| 104 | // of the function. http://crbug.com/136655. |
| 105 | AssertIOAllowed(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 106 | sqlite3_finalize(stmt_); |
| 107 | stmt_ = NULL; |
| 108 | } |
| 109 | connection_ = NULL; // The connection may be getting deleted. |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 110 | |
| 111 | // Forced close is expected to happen from a statement error |
| 112 | // handler. In that case maintain the sense of |was_valid_| which |
| 113 | // previously held for this ref. |
| 114 | was_valid_ = was_valid_ && forced; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | Connection::Connection() |
| 118 | : db_(NULL), |
| 119 | page_size_(0), |
| 120 | cache_size_(0), |
| 121 | exclusive_locking_(false), |
| 122 | transaction_nesting_(0), |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 123 | needs_rollback_(false), |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 124 | in_memory_(false), |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 125 | poisoned_(false), |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 126 | error_delegate_(NULL) { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | Connection::~Connection() { |
| 130 | Close(); |
| 131 | } |
| 132 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 133 | bool Connection::Open(const base::FilePath& path) { |
[email protected] | 348ac8f5 | 2013-05-21 03:27:02 | [diff] [blame] | 134 | if (!histogram_tag_.empty()) { |
| 135 | int64 size_64 = 0; |
| 136 | if (file_util::GetFileSize(path, &size_64)) { |
| 137 | size_t sample = static_cast<size_t>(size_64 / 1024); |
| 138 | std::string full_histogram_name = "Sqlite.SizeKB." + histogram_tag_; |
| 139 | base::HistogramBase* histogram = |
| 140 | base::Histogram::FactoryGet( |
| 141 | full_histogram_name, 1, 1000000, 50, |
| 142 | base::HistogramBase::kUmaTargetedHistogramFlag); |
| 143 | if (histogram) |
| 144 | histogram->Add(sample); |
| 145 | } |
| 146 | } |
| 147 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 148 | #if defined(OS_WIN) |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 149 | return OpenInternal(WideToUTF8(path.value())); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 150 | #elif defined(OS_POSIX) |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 151 | return OpenInternal(path.value()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 152 | #endif |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 153 | } |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 154 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 155 | bool Connection::OpenInMemory() { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 156 | in_memory_ = true; |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 157 | return OpenInternal(":memory:"); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 160 | void Connection::CloseInternal(bool forced) { |
[email protected] | 4e179ba | 2012-03-17 16:06:47 | [diff] [blame] | 161 | // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point |
| 162 | // will delete the -journal file. For ChromiumOS or other more |
| 163 | // embedded systems, this is probably not appropriate, whereas on |
| 164 | // desktop it might make some sense. |
| 165 | |
[email protected] | 4b35005 | 2012-02-24 20:40:48 | [diff] [blame] | 166 | // sqlite3_close() needs all prepared statements to be finalized. |
[email protected] | 4b35005 | 2012-02-24 20:40:48 | [diff] [blame] | 167 | |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 168 | // Release cached statements. |
| 169 | statement_cache_.clear(); |
| 170 | |
| 171 | // With cached statements released, in-use statements will remain. |
| 172 | // Closing the database while statements are in use is an API |
| 173 | // violation, except for forced close (which happens from within a |
| 174 | // statement's error handler). |
| 175 | DCHECK(forced || open_statements_.empty()); |
| 176 | |
| 177 | // Deactivate any outstanding statements so sqlite3_close() works. |
| 178 | for (StatementRefSet::iterator i = open_statements_.begin(); |
| 179 | i != open_statements_.end(); ++i) |
| 180 | (*i)->Close(forced); |
| 181 | open_statements_.clear(); |
[email protected] | 4b35005 | 2012-02-24 20:40:48 | [diff] [blame] | 182 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 183 | if (db_) { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 184 | // Call to AssertIOAllowed() cannot go at the beginning of the function |
| 185 | // because Close() must be called from destructor to clean |
| 186 | // statement_cache_, it won't cause any disk access and it most probably |
| 187 | // will happen on thread not allowing disk access. |
| 188 | // TODO([email protected]): This should move to the beginning |
| 189 | // of the function. http://crbug.com/136655. |
| 190 | AssertIOAllowed(); |
[email protected] | 4b35005 | 2012-02-24 20:40:48 | [diff] [blame] | 191 | // TODO(shess): Histogram for failure. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 192 | sqlite3_close(db_); |
| 193 | db_ = NULL; |
| 194 | } |
| 195 | } |
| 196 | |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 197 | void Connection::Close() { |
| 198 | // If the database was already closed by RazeAndClose(), then no |
| 199 | // need to close again. Clear the |poisoned_| bit so that incorrect |
| 200 | // API calls are caught. |
| 201 | if (poisoned_) { |
| 202 | poisoned_ = false; |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | CloseInternal(false); |
| 207 | } |
| 208 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 209 | void Connection::Preload() { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 210 | AssertIOAllowed(); |
| 211 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 212 | if (!db_) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 213 | DLOG_IF(FATAL, !poisoned_) << "Cannot preload null db"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 214 | return; |
| 215 | } |
| 216 | |
| 217 | // A statement must be open for the preload command to work. If the meta |
| 218 | // table doesn't exist, it probably means this is a new database and there |
| 219 | // is nothing to preload (so it's OK we do nothing). |
| 220 | if (!DoesTableExist("meta")) |
| 221 | return; |
| 222 | Statement dummy(GetUniqueStatement("SELECT * FROM meta")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 223 | if (!dummy.Step()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 224 | return; |
| 225 | |
[email protected] | 4176eee4b | 2011-01-26 14:33:32 | [diff] [blame] | 226 | #if !defined(USE_SYSTEM_SQLITE) |
| 227 | // This function is only defined in Chromium's version of sqlite. |
| 228 | // Do not call it when using system sqlite. |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 229 | sqlite3_preload(db_); |
[email protected] | 4176eee4b | 2011-01-26 14:33:32 | [diff] [blame] | 230 | #endif |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 233 | // Create an in-memory database with the existing database's page |
| 234 | // size, then backup that database over the existing database. |
| 235 | bool Connection::Raze() { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 236 | AssertIOAllowed(); |
| 237 | |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 238 | if (!db_) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 239 | DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db"; |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | |
| 243 | if (transaction_nesting_ > 0) { |
| 244 | DLOG(FATAL) << "Cannot raze within a transaction"; |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | sql::Connection null_db; |
| 249 | if (!null_db.OpenInMemory()) { |
| 250 | DLOG(FATAL) << "Unable to open in-memory database."; |
| 251 | return false; |
| 252 | } |
| 253 | |
[email protected] | 6d42f15 | 2012-11-10 00:38:24 | [diff] [blame] | 254 | if (page_size_) { |
| 255 | // Enforce SQLite restrictions on |page_size_|. |
| 256 | DCHECK(!(page_size_ & (page_size_ - 1))) |
| 257 | << " page_size_ " << page_size_ << " is not a power of two."; |
| 258 | const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h |
| 259 | DCHECK_LE(page_size_, kSqliteMaxPageSize); |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 260 | const std::string sql = |
| 261 | base::StringPrintf("PRAGMA page_size=%d", page_size_); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 262 | if (!null_db.Execute(sql.c_str())) |
| 263 | return false; |
| 264 | } |
| 265 | |
[email protected] | 6d42f15 | 2012-11-10 00:38:24 | [diff] [blame] | 266 | #if defined(OS_ANDROID) |
| 267 | // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately, |
| 268 | // in-memory databases do not respect this define. |
| 269 | // TODO(shess): Figure out a way to set this without using platform |
| 270 | // specific code. AFAICT from sqlite3.c, the only way to do it |
| 271 | // would be to create an actual filesystem database, which is |
| 272 | // unfortunate. |
| 273 | if (!null_db.Execute("PRAGMA auto_vacuum = 1")) |
| 274 | return false; |
| 275 | #endif |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 276 | |
| 277 | // The page size doesn't take effect until a database has pages, and |
| 278 | // at this point the null database has none. Changing the schema |
| 279 | // version will create the first page. This will not affect the |
| 280 | // schema version in the resulting database, as SQLite's backup |
| 281 | // implementation propagates the schema version from the original |
| 282 | // connection to the new version of the database, incremented by one |
| 283 | // so that other readers see the schema change and act accordingly. |
| 284 | if (!null_db.Execute("PRAGMA schema_version = 1")) |
| 285 | return false; |
| 286 | |
[email protected] | 6d42f15 | 2012-11-10 00:38:24 | [diff] [blame] | 287 | // SQLite tracks the expected number of database pages in the first |
| 288 | // page, and if it does not match the total retrieved from a |
| 289 | // filesystem call, treats the database as corrupt. This situation |
| 290 | // breaks almost all SQLite calls. "PRAGMA writable_schema" can be |
| 291 | // used to hint to SQLite to soldier on in that case, specifically |
| 292 | // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in |
| 293 | // sqlite3.c lockBtree().] |
| 294 | // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA |
| 295 | // page_size" can be used to query such a database. |
| 296 | ScopedWritableSchema writable_schema(db_); |
| 297 | |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 298 | sqlite3_backup* backup = sqlite3_backup_init(db_, "main", |
| 299 | null_db.db_, "main"); |
| 300 | if (!backup) { |
| 301 | DLOG(FATAL) << "Unable to start sqlite3_backup()."; |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | // -1 backs up the entire database. |
| 306 | int rc = sqlite3_backup_step(backup, -1); |
| 307 | int pages = sqlite3_backup_pagecount(backup); |
| 308 | sqlite3_backup_finish(backup); |
| 309 | |
| 310 | // The destination database was locked. |
| 311 | if (rc == SQLITE_BUSY) { |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | // The entire database should have been backed up. |
| 316 | if (rc != SQLITE_DONE) { |
| 317 | DLOG(FATAL) << "Unable to copy entire null database."; |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | // Exactly one page should have been backed up. If this breaks, |
| 322 | // check this function to make sure assumptions aren't being broken. |
| 323 | DCHECK_EQ(pages, 1); |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | bool Connection::RazeWithTimout(base::TimeDelta timeout) { |
| 329 | if (!db_) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 330 | DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db"; |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | |
| 334 | ScopedBusyTimeout busy_timeout(db_); |
| 335 | busy_timeout.SetTimeout(timeout); |
| 336 | return Raze(); |
| 337 | } |
| 338 | |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 339 | bool Connection::RazeAndClose() { |
| 340 | if (!db_) { |
| 341 | DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db"; |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | // Raze() cannot run in a transaction. |
| 346 | while (transaction_nesting_) { |
| 347 | RollbackTransaction(); |
| 348 | } |
| 349 | |
| 350 | bool result = Raze(); |
| 351 | |
| 352 | CloseInternal(true); |
| 353 | |
| 354 | // Mark the database so that future API calls fail appropriately, |
| 355 | // but don't DCHECK (because after calling this function they are |
| 356 | // expected to fail). |
| 357 | poisoned_ = true; |
| 358 | |
| 359 | return result; |
| 360 | } |
| 361 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 362 | bool Connection::BeginTransaction() { |
| 363 | if (needs_rollback_) { |
[email protected] | 88563f6 | 2011-03-13 22:13:33 | [diff] [blame] | 364 | DCHECK_GT(transaction_nesting_, 0); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 365 | |
| 366 | // When we're going to rollback, fail on this begin and don't actually |
| 367 | // mark us as entering the nested transaction. |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | bool success = true; |
| 372 | if (!transaction_nesting_) { |
| 373 | needs_rollback_ = false; |
| 374 | |
| 375 | Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 376 | if (!begin.Run()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 377 | return false; |
| 378 | } |
| 379 | transaction_nesting_++; |
| 380 | return success; |
| 381 | } |
| 382 | |
| 383 | void Connection::RollbackTransaction() { |
| 384 | if (!transaction_nesting_) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 385 | DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
| 389 | transaction_nesting_--; |
| 390 | |
| 391 | if (transaction_nesting_ > 0) { |
| 392 | // Mark the outermost transaction as needing rollback. |
| 393 | needs_rollback_ = true; |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | DoRollback(); |
| 398 | } |
| 399 | |
| 400 | bool Connection::CommitTransaction() { |
| 401 | if (!transaction_nesting_) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 402 | DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 403 | return false; |
| 404 | } |
| 405 | transaction_nesting_--; |
| 406 | |
| 407 | if (transaction_nesting_ > 0) { |
| 408 | // Mark any nested transactions as failing after we've already got one. |
| 409 | return !needs_rollback_; |
| 410 | } |
| 411 | |
| 412 | if (needs_rollback_) { |
| 413 | DoRollback(); |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 418 | return commit.Run(); |
| 419 | } |
| 420 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 421 | int Connection::ExecuteAndReturnErrorCode(const char* sql) { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 422 | AssertIOAllowed(); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 423 | if (!db_) { |
| 424 | DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db"; |
| 425 | return SQLITE_ERROR; |
| 426 | } |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 427 | return sqlite3_exec(db_, sql, NULL, NULL, NULL); |
| 428 | } |
| 429 | |
| 430 | bool Connection::Execute(const char* sql) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 431 | if (!db_) { |
| 432 | DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db"; |
| 433 | return false; |
| 434 | } |
| 435 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 436 | int error = ExecuteAndReturnErrorCode(sql); |
[email protected] | 473ad79 | 2012-11-10 00:55:00 | [diff] [blame] | 437 | if (error != SQLITE_OK) |
| 438 | error = OnSqliteError(error, NULL); |
| 439 | |
[email protected] | 28fe0ff | 2012-02-25 00:40:33 | [diff] [blame] | 440 | // This needs to be a FATAL log because the error case of arriving here is |
| 441 | // that there's a malformed SQL statement. This can arise in development if |
| 442 | // a change alters the schema but not all queries adjust. |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 443 | if (error == SQLITE_ERROR) |
[email protected] | 28fe0ff | 2012-02-25 00:40:33 | [diff] [blame] | 444 | DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage(); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 445 | return error == SQLITE_OK; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 446 | } |
| 447 | |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 448 | bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 449 | if (!db_) { |
| 450 | DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db"; |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 451 | return false; |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 452 | } |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 453 | |
| 454 | ScopedBusyTimeout busy_timeout(db_); |
| 455 | busy_timeout.SetTimeout(timeout); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 456 | return Execute(sql); |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 457 | } |
| 458 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 459 | bool Connection::HasCachedStatement(const StatementID& id) const { |
| 460 | return statement_cache_.find(id) != statement_cache_.end(); |
| 461 | } |
| 462 | |
| 463 | scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement( |
| 464 | const StatementID& id, |
| 465 | const char* sql) { |
| 466 | CachedStatementMap::iterator i = statement_cache_.find(id); |
| 467 | if (i != statement_cache_.end()) { |
| 468 | // Statement is in the cache. It should still be active (we're the only |
| 469 | // one invalidating cached statements, and we'll remove it from the cache |
| 470 | // if we do that. Make sure we reset it before giving out the cached one in |
| 471 | // case it still has some stuff bound. |
| 472 | DCHECK(i->second->is_valid()); |
| 473 | sqlite3_reset(i->second->stmt()); |
| 474 | return i->second; |
| 475 | } |
| 476 | |
| 477 | scoped_refptr<StatementRef> statement = GetUniqueStatement(sql); |
| 478 | if (statement->is_valid()) |
| 479 | statement_cache_[id] = statement; // Only cache valid statements. |
| 480 | return statement; |
| 481 | } |
| 482 | |
| 483 | scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement( |
| 484 | const char* sql) { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 485 | AssertIOAllowed(); |
| 486 | |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 487 | // Return inactive statement. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 488 | if (!db_) |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 489 | return new StatementRef(NULL, NULL, poisoned_); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 490 | |
| 491 | sqlite3_stmt* stmt = NULL; |
[email protected] | 473ad79 | 2012-11-10 00:55:00 | [diff] [blame] | 492 | int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL); |
| 493 | if (rc != SQLITE_OK) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 494 | // This is evidence of a syntax error in the incoming SQL. |
| 495 | DLOG(FATAL) << "SQL compile error " << GetErrorMessage(); |
[email protected] | 473ad79 | 2012-11-10 00:55:00 | [diff] [blame] | 496 | |
| 497 | // It could also be database corruption. |
| 498 | OnSqliteError(rc, NULL); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 499 | return new StatementRef(NULL, NULL, false); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 500 | } |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 501 | return new StatementRef(this, stmt, true); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 502 | } |
| 503 | |
[email protected] | 2eec0a2 | 2012-07-24 01:59:58 | [diff] [blame] | 504 | scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement( |
| 505 | const char* sql) const { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 506 | // Return inactive statement. |
[email protected] | 2eec0a2 | 2012-07-24 01:59:58 | [diff] [blame] | 507 | if (!db_) |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 508 | return new StatementRef(NULL, NULL, poisoned_); |
[email protected] | 2eec0a2 | 2012-07-24 01:59:58 | [diff] [blame] | 509 | |
| 510 | sqlite3_stmt* stmt = NULL; |
| 511 | int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL); |
| 512 | if (rc != SQLITE_OK) { |
| 513 | // This is evidence of a syntax error in the incoming SQL. |
| 514 | DLOG(FATAL) << "SQL compile error " << GetErrorMessage(); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 515 | return new StatementRef(NULL, NULL, false); |
[email protected] | 2eec0a2 | 2012-07-24 01:59:58 | [diff] [blame] | 516 | } |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 517 | return new StatementRef(NULL, stmt, true); |
[email protected] | 2eec0a2 | 2012-07-24 01:59:58 | [diff] [blame] | 518 | } |
| 519 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 520 | bool Connection::IsSQLValid(const char* sql) { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 521 | AssertIOAllowed(); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 522 | if (!db_) { |
| 523 | DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db"; |
| 524 | return false; |
| 525 | } |
| 526 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 527 | sqlite3_stmt* stmt = NULL; |
| 528 | if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) |
| 529 | return false; |
| 530 | |
| 531 | sqlite3_finalize(stmt); |
| 532 | return true; |
| 533 | } |
| 534 | |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 535 | bool Connection::DoesTableExist(const char* table_name) const { |
[email protected] | e2cadec8 | 2011-12-13 02:00:53 | [diff] [blame] | 536 | return DoesTableOrIndexExist(table_name, "table"); |
| 537 | } |
| 538 | |
| 539 | bool Connection::DoesIndexExist(const char* index_name) const { |
| 540 | return DoesTableOrIndexExist(index_name, "index"); |
| 541 | } |
| 542 | |
| 543 | bool Connection::DoesTableOrIndexExist( |
| 544 | const char* name, const char* type) const { |
[email protected] | 2eec0a2 | 2012-07-24 01:59:58 | [diff] [blame] | 545 | const char* kSql = "SELECT name FROM sqlite_master WHERE type=? AND name=?"; |
| 546 | Statement statement(GetUntrackedStatement(kSql)); |
[email protected] | e2cadec8 | 2011-12-13 02:00:53 | [diff] [blame] | 547 | statement.BindString(0, type); |
| 548 | statement.BindString(1, name); |
[email protected] | 28fe0ff | 2012-02-25 00:40:33 | [diff] [blame] | 549 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 550 | return statement.Step(); // Table exists if any row was returned. |
| 551 | } |
| 552 | |
| 553 | bool Connection::DoesColumnExist(const char* table_name, |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 554 | const char* column_name) const { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 555 | std::string sql("PRAGMA TABLE_INFO("); |
| 556 | sql.append(table_name); |
| 557 | sql.append(")"); |
| 558 | |
[email protected] | 2eec0a2 | 2012-07-24 01:59:58 | [diff] [blame] | 559 | Statement statement(GetUntrackedStatement(sql.c_str())); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 560 | while (statement.Step()) { |
| 561 | if (!statement.ColumnString(1).compare(column_name)) |
| 562 | return true; |
| 563 | } |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | int64 Connection::GetLastInsertRowId() const { |
| 568 | if (!db_) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 569 | DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 570 | return 0; |
| 571 | } |
| 572 | return sqlite3_last_insert_rowid(db_); |
| 573 | } |
| 574 | |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 575 | int Connection::GetLastChangeCount() const { |
| 576 | if (!db_) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 577 | DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db"; |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 578 | return 0; |
| 579 | } |
| 580 | return sqlite3_changes(db_); |
| 581 | } |
| 582 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 583 | int Connection::GetErrorCode() const { |
| 584 | if (!db_) |
| 585 | return SQLITE_ERROR; |
| 586 | return sqlite3_errcode(db_); |
| 587 | } |
| 588 | |
[email protected] | 767718e5 | 2010-09-21 23:18:49 | [diff] [blame] | 589 | int Connection::GetLastErrno() const { |
| 590 | if (!db_) |
| 591 | return -1; |
| 592 | |
| 593 | int err = 0; |
| 594 | if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err)) |
| 595 | return -2; |
| 596 | |
| 597 | return err; |
| 598 | } |
| 599 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 600 | const char* Connection::GetErrorMessage() const { |
| 601 | if (!db_) |
| 602 | return "sql::Connection has no connection."; |
| 603 | return sqlite3_errmsg(db_); |
| 604 | } |
| 605 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 606 | bool Connection::OpenInternal(const std::string& file_name) { |
[email protected] | 35f7e539 | 2012-07-27 19:54:50 | [diff] [blame] | 607 | AssertIOAllowed(); |
| 608 | |
[email protected] | 9cfbc92 | 2009-11-17 20:13:17 | [diff] [blame] | 609 | if (db_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 610 | DLOG(FATAL) << "sql::Connection is already open."; |
[email protected] | 9cfbc92 | 2009-11-17 20:13:17 | [diff] [blame] | 611 | return false; |
| 612 | } |
| 613 | |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 614 | // If |poisoned_| is set, it means an error handler called |
| 615 | // RazeAndClose(). Until regular Close() is called, the caller |
| 616 | // should be treating the database as open, but is_open() currently |
| 617 | // only considers the sqlite3 handle's state. |
| 618 | // TODO(shess): Revise is_open() to consider poisoned_, and review |
| 619 | // to see if any non-testing code even depends on it. |
| 620 | DLOG_IF(FATAL, poisoned_) << "sql::Connection is already open."; |
| 621 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 622 | int err = sqlite3_open(file_name.c_str(), &db_); |
| 623 | if (err != SQLITE_OK) { |
[email protected] | bd2ccdb4a | 2012-12-07 22:14:50 | [diff] [blame] | 624 | // Histogram failures specific to initial open for debugging |
| 625 | // purposes. |
| 626 | UMA_HISTOGRAM_ENUMERATION("Sqlite.OpenFailure", err & 0xff, 50); |
| 627 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 628 | OnSqliteError(err, NULL); |
[email protected] | 6402104 | 2012-02-10 20:02:29 | [diff] [blame] | 629 | Close(); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 630 | db_ = NULL; |
| 631 | return false; |
| 632 | } |
| 633 | |
[email protected] | bd2ccdb4a | 2012-12-07 22:14:50 | [diff] [blame] | 634 | // sqlite3_open() does not actually read the database file (unless a |
| 635 | // hot journal is found). Successfully executing this pragma on an |
| 636 | // existing database requires a valid header on page 1. |
| 637 | // TODO(shess): For now, just probing to see what the lay of the |
| 638 | // land is. If it's mostly SQLITE_NOTADB, then the database should |
| 639 | // be razed. |
| 640 | err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum"); |
| 641 | if (err != SQLITE_OK) |
| 642 | UMA_HISTOGRAM_ENUMERATION("Sqlite.OpenProbeFailure", err & 0xff, 50); |
| 643 | |
[email protected] | 658f833 | 2010-09-18 04:40:43 | [diff] [blame] | 644 | // Enable extended result codes to provide more color on I/O errors. |
| 645 | // Not having extended result codes is not a fatal problem, as |
| 646 | // Chromium code does not attempt to handle I/O errors anyhow. The |
| 647 | // current implementation always returns SQLITE_OK, the DCHECK is to |
| 648 | // quickly notify someone if SQLite changes. |
| 649 | err = sqlite3_extended_result_codes(db_, 1); |
| 650 | DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes"; |
| 651 | |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 652 | // If indicated, lock up the database before doing anything else, so |
| 653 | // that the following code doesn't have to deal with locking. |
| 654 | // TODO(shess): This code is brittle. Find the cases where code |
| 655 | // doesn't request |exclusive_locking_| and audit that it does the |
| 656 | // right thing with SQLITE_BUSY, and that it doesn't make |
| 657 | // assumptions about who might change things in the database. |
| 658 | // http://crbug.com/56559 |
| 659 | if (exclusive_locking_) { |
| 660 | // TODO(shess): This should probably be a full CHECK(). Code |
| 661 | // which requests exclusive locking but doesn't get it is almost |
| 662 | // certain to be ill-tested. |
| 663 | if (!Execute("PRAGMA locking_mode=EXCLUSIVE")) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 664 | DLOG(FATAL) << "Could not set locking mode: " << GetErrorMessage(); |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 665 | } |
| 666 | |
[email protected] | 4e179ba | 2012-03-17 16:06:47 | [diff] [blame] | 667 | // http://www.sqlite.org/pragma.html#pragma_journal_mode |
| 668 | // DELETE (default) - delete -journal file to commit. |
| 669 | // TRUNCATE - truncate -journal file to commit. |
| 670 | // PERSIST - zero out header of -journal file to commit. |
| 671 | // journal_size_limit provides size to trim to in PERSIST. |
| 672 | // TODO(shess): Figure out if PERSIST and journal_size_limit really |
| 673 | // matter. In theory, it keeps pages pre-allocated, so if |
| 674 | // transactions usually fit, it should be faster. |
| 675 | ignore_result(Execute("PRAGMA journal_mode = PERSIST")); |
| 676 | ignore_result(Execute("PRAGMA journal_size_limit = 16384")); |
| 677 | |
[email protected] | c68ce17 | 2011-11-24 22:30:27 | [diff] [blame] | 678 | const base::TimeDelta kBusyTimeout = |
| 679 | base::TimeDelta::FromSeconds(kBusyTimeoutSeconds); |
| 680 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 681 | if (page_size_ != 0) { |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 682 | // Enforce SQLite restrictions on |page_size_|. |
| 683 | DCHECK(!(page_size_ & (page_size_ - 1))) |
| 684 | << " page_size_ " << page_size_ << " is not a power of two."; |
[email protected] | 6d42f15 | 2012-11-10 00:38:24 | [diff] [blame] | 685 | const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 686 | DCHECK_LE(page_size_, kSqliteMaxPageSize); |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 687 | const std::string sql = |
| 688 | base::StringPrintf("PRAGMA page_size=%d", page_size_); |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 689 | if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout)) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 690 | DLOG(FATAL) << "Could not set page size: " << GetErrorMessage(); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | if (cache_size_ != 0) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 694 | const std::string sql = |
| 695 | base::StringPrintf("PRAGMA cache_size=%d", cache_size_); |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 696 | if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout)) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 697 | DLOG(FATAL) << "Could not set cache size: " << GetErrorMessage(); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 698 | } |
| 699 | |
[email protected] | 6e0b144 | 2011-08-09 23:23:58 | [diff] [blame] | 700 | if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 701 | DLOG(FATAL) << "Could not enable secure_delete: " << GetErrorMessage(); |
[email protected] | 6e0b144 | 2011-08-09 23:23:58 | [diff] [blame] | 702 | Close(); |
| 703 | return false; |
| 704 | } |
| 705 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 706 | return true; |
| 707 | } |
| 708 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 709 | void Connection::DoRollback() { |
| 710 | Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 711 | rollback.Run(); |
[email protected] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 712 | needs_rollback_ = false; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void Connection::StatementRefCreated(StatementRef* ref) { |
| 716 | DCHECK(open_statements_.find(ref) == open_statements_.end()); |
| 717 | open_statements_.insert(ref); |
| 718 | } |
| 719 | |
| 720 | void Connection::StatementRefDeleted(StatementRef* ref) { |
| 721 | StatementRefSet::iterator i = open_statements_.find(ref); |
| 722 | if (i == open_statements_.end()) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 723 | DLOG(FATAL) << "Could not find statement"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 724 | else |
| 725 | open_statements_.erase(i); |
| 726 | } |
| 727 | |
[email protected] | 210ce0af | 2013-05-15 09:10:39 | [diff] [blame] | 728 | void Connection::AddTaggedHistogram(const std::string& name, |
| 729 | size_t sample) const { |
| 730 | if (histogram_tag_.empty()) |
| 731 | return; |
| 732 | |
| 733 | // TODO(shess): The histogram macros create a bit of static storage |
| 734 | // for caching the histogram object. This code shouldn't execute |
| 735 | // often enough for such caching to be crucial. If it becomes an |
| 736 | // issue, the object could be cached alongside histogram_prefix_. |
| 737 | std::string full_histogram_name = name + "." + histogram_tag_; |
| 738 | base::HistogramBase* histogram = |
| 739 | base::SparseHistogram::FactoryGet( |
| 740 | full_histogram_name, |
| 741 | base::HistogramBase::kUmaTargetedHistogramFlag); |
| 742 | if (histogram) |
| 743 | histogram->Add(sample); |
| 744 | } |
| 745 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 746 | int Connection::OnSqliteError(int err, sql::Statement *stmt) { |
[email protected] | 210ce0af | 2013-05-15 09:10:39 | [diff] [blame] | 747 | UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err); |
| 748 | AddTaggedHistogram("Sqlite.Error", err); |
[email protected] | c088e3a3 | 2013-01-03 23:59:14 | [diff] [blame] | 749 | |
| 750 | // Always log the error. |
| 751 | LOG(ERROR) << "sqlite error " << err |
| 752 | << ", errno " << GetLastErrno() |
| 753 | << ": " << GetErrorMessage(); |
| 754 | |
[email protected] | c3881b37 | 2013-05-17 08:39:46 | [diff] [blame] | 755 | if (!error_callback_.is_null()) { |
| 756 | error_callback_.Run(err, stmt); |
| 757 | return err; |
| 758 | } |
| 759 | |
| 760 | // TODO(shess): Remove |error_delegate_| once everything is |
| 761 | // converted to |error_callback_|. |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 762 | if (error_delegate_.get()) |
| 763 | return error_delegate_->OnError(err, this, stmt); |
[email protected] | c088e3a3 | 2013-01-03 23:59:14 | [diff] [blame] | 764 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 765 | // The default handling is to assert on debug and to ignore on release. |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 766 | DLOG(FATAL) << GetErrorMessage(); |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 767 | return err; |
| 768 | } |
| 769 | |
[email protected] | 80abf15 | 2013-05-22 12:42:42 | [diff] [blame^] | 770 | // TODO(shess): Allow specifying integrity_check versus quick_check. |
| 771 | // TODO(shess): Allow specifying maximum results (default 100 lines). |
| 772 | bool Connection::IntegrityCheck(std::vector<std::string>* messages) { |
| 773 | const char kSql[] = "PRAGMA integrity_check"; |
| 774 | sql::Statement stmt(GetUniqueStatement(kSql)); |
| 775 | |
| 776 | messages->clear(); |
| 777 | |
| 778 | // The pragma appears to return all results (up to 100 by default) |
| 779 | // as a single string. This doesn't appear to be an API contract, |
| 780 | // it could return separate lines, so loop _and_ split. |
| 781 | while (stmt.Step()) { |
| 782 | std::string result(stmt.ColumnString(0)); |
| 783 | base::SplitString(result, '\n', messages); |
| 784 | } |
| 785 | return stmt.Succeeded(); |
| 786 | } |
| 787 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 788 | } // namespace sql |