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