[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] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 9 | #include "base/file_path.h" |
| 10 | #include "base/logging.h" |
| 11 | #include "base/string_util.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 12 | #include "base/stringprintf.h" |
[email protected] | d55194ca | 2010-03-11 18:25:45 | [diff] [blame] | 13 | #include "base/utf_string_conversions.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 14 | #include "sql/statement.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 15 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 16 | |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | // Spin for up to a second waiting for the lock to clear when setting |
| 20 | // up the database. |
| 21 | // TODO(shess): Better story on this. http://crbug.com/56559 |
[email protected] | c68ce17 | 2011-11-24 22:30:27 | [diff] [blame] | 22 | const int kBusyTimeoutSeconds = 1; |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 23 | |
| 24 | class ScopedBusyTimeout { |
| 25 | public: |
| 26 | explicit ScopedBusyTimeout(sqlite3* db) |
| 27 | : db_(db) { |
| 28 | } |
| 29 | ~ScopedBusyTimeout() { |
| 30 | sqlite3_busy_timeout(db_, 0); |
| 31 | } |
| 32 | |
| 33 | int SetTimeout(base::TimeDelta timeout) { |
| 34 | DCHECK_LT(timeout.InMilliseconds(), INT_MAX); |
| 35 | return sqlite3_busy_timeout(db_, |
| 36 | static_cast<int>(timeout.InMilliseconds())); |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | sqlite3* db_; |
| 41 | }; |
| 42 | |
| 43 | } // namespace |
| 44 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 45 | namespace sql { |
| 46 | |
| 47 | bool StatementID::operator<(const StatementID& other) const { |
| 48 | if (number_ != other.number_) |
| 49 | return number_ < other.number_; |
| 50 | return strcmp(str_, other.str_) < 0; |
| 51 | } |
| 52 | |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 53 | ErrorDelegate::ErrorDelegate() { |
| 54 | } |
| 55 | |
| 56 | ErrorDelegate::~ErrorDelegate() { |
| 57 | } |
| 58 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 59 | Connection::StatementRef::StatementRef() |
| 60 | : connection_(NULL), |
| 61 | stmt_(NULL) { |
| 62 | } |
| 63 | |
| 64 | Connection::StatementRef::StatementRef(Connection* connection, |
| 65 | sqlite3_stmt* stmt) |
| 66 | : connection_(connection), |
| 67 | stmt_(stmt) { |
| 68 | connection_->StatementRefCreated(this); |
| 69 | } |
| 70 | |
| 71 | Connection::StatementRef::~StatementRef() { |
| 72 | if (connection_) |
| 73 | connection_->StatementRefDeleted(this); |
| 74 | Close(); |
| 75 | } |
| 76 | |
| 77 | void Connection::StatementRef::Close() { |
| 78 | if (stmt_) { |
| 79 | sqlite3_finalize(stmt_); |
| 80 | stmt_ = NULL; |
| 81 | } |
| 82 | connection_ = NULL; // The connection may be getting deleted. |
| 83 | } |
| 84 | |
| 85 | Connection::Connection() |
| 86 | : db_(NULL), |
| 87 | page_size_(0), |
| 88 | cache_size_(0), |
| 89 | exclusive_locking_(false), |
| 90 | transaction_nesting_(0), |
| 91 | needs_rollback_(false) { |
| 92 | } |
| 93 | |
| 94 | Connection::~Connection() { |
| 95 | Close(); |
| 96 | } |
| 97 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 98 | bool Connection::Open(const FilePath& path) { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 99 | #if defined(OS_WIN) |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 100 | return OpenInternal(WideToUTF8(path.value())); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 101 | #elif defined(OS_POSIX) |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 102 | return OpenInternal(path.value()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 103 | #endif |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 104 | } |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 105 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 106 | bool Connection::OpenInMemory() { |
| 107 | return OpenInternal(":memory:"); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void Connection::Close() { |
[email protected] | 4e179ba6 | 2012-03-17 16:06:47 | [diff] [blame] | 111 | // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point |
| 112 | // will delete the -journal file. For ChromiumOS or other more |
| 113 | // embedded systems, this is probably not appropriate, whereas on |
| 114 | // desktop it might make some sense. |
| 115 | |
[email protected] | 4b35005 | 2012-02-24 20:40:48 | [diff] [blame] | 116 | // sqlite3_close() needs all prepared statements to be finalized. |
| 117 | // Release all cached statements, then assert that the client has |
| 118 | // released all statements. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 119 | statement_cache_.clear(); |
| 120 | DCHECK(open_statements_.empty()); |
[email protected] | 4b35005 | 2012-02-24 20:40:48 | [diff] [blame] | 121 | |
| 122 | // Additionally clear the prepared statements, because they contain |
| 123 | // weak references to this connection. This case has come up when |
| 124 | // error-handling code is hit in production. |
| 125 | ClearCache(); |
| 126 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 127 | if (db_) { |
[email protected] | e48e0515 | 2011-09-14 06:37:46 | [diff] [blame] | 128 | // TODO(shess): Some additional code to debug http://crbug.com/95527 . |
| 129 | // If you are reading this due to link errors or something, it can |
| 130 | // be safely removed. |
| 131 | #if defined(HAS_SQLITE3_95527) |
| 132 | unsigned int nTouched = 0; |
| 133 | sqlite3_95527(db_, &nTouched); |
| 134 | |
| 135 | // If a VERY large amount of memory was touched, crash. This |
| 136 | // should never happen. |
| 137 | // TODO(shess): Pull this in. It should be page_size * page_cache |
| 138 | // or something like that, 4M or 16M. For now it's just to |
| 139 | // prevent optimization. |
| 140 | CHECK_LT(nTouched, 1000*1000*1000U); |
| 141 | #endif |
[email protected] | 4b35005 | 2012-02-24 20:40:48 | [diff] [blame] | 142 | |
| 143 | // TODO(shess): Histogram for failure. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 144 | sqlite3_close(db_); |
| 145 | db_ = NULL; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void Connection::Preload() { |
| 150 | if (!db_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 151 | DLOG(FATAL) << "Cannot preload null db"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
| 155 | // A statement must be open for the preload command to work. If the meta |
| 156 | // table doesn't exist, it probably means this is a new database and there |
| 157 | // is nothing to preload (so it's OK we do nothing). |
| 158 | if (!DoesTableExist("meta")) |
| 159 | return; |
| 160 | Statement dummy(GetUniqueStatement("SELECT * FROM meta")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 161 | if (!dummy.Step()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 162 | return; |
| 163 | |
[email protected] | 4176eee4b | 2011-01-26 14:33:32 | [diff] [blame] | 164 | #if !defined(USE_SYSTEM_SQLITE) |
| 165 | // This function is only defined in Chromium's version of sqlite. |
| 166 | // Do not call it when using system sqlite. |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 167 | sqlite3_preload(db_); |
[email protected] | 4176eee4b | 2011-01-26 14:33:32 | [diff] [blame] | 168 | #endif |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | bool Connection::BeginTransaction() { |
| 172 | if (needs_rollback_) { |
[email protected] | 88563f6 | 2011-03-13 22:13:33 | [diff] [blame] | 173 | DCHECK_GT(transaction_nesting_, 0); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 174 | |
| 175 | // When we're going to rollback, fail on this begin and don't actually |
| 176 | // mark us as entering the nested transaction. |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | bool success = true; |
| 181 | if (!transaction_nesting_) { |
| 182 | needs_rollback_ = false; |
| 183 | |
| 184 | Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 185 | if (!begin.Run()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | transaction_nesting_++; |
| 189 | return success; |
| 190 | } |
| 191 | |
| 192 | void Connection::RollbackTransaction() { |
| 193 | if (!transaction_nesting_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 194 | DLOG(FATAL) << "Rolling back a nonexistent transaction"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
| 198 | transaction_nesting_--; |
| 199 | |
| 200 | if (transaction_nesting_ > 0) { |
| 201 | // Mark the outermost transaction as needing rollback. |
| 202 | needs_rollback_ = true; |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | DoRollback(); |
| 207 | } |
| 208 | |
| 209 | bool Connection::CommitTransaction() { |
| 210 | if (!transaction_nesting_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 211 | DLOG(FATAL) << "Rolling back a nonexistent transaction"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | transaction_nesting_--; |
| 215 | |
| 216 | if (transaction_nesting_ > 0) { |
| 217 | // Mark any nested transactions as failing after we've already got one. |
| 218 | return !needs_rollback_; |
| 219 | } |
| 220 | |
| 221 | if (needs_rollback_) { |
| 222 | DoRollback(); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 227 | return commit.Run(); |
| 228 | } |
| 229 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 230 | int Connection::ExecuteAndReturnErrorCode(const char* sql) { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 231 | if (!db_) |
| 232 | return false; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 233 | return sqlite3_exec(db_, sql, NULL, NULL, NULL); |
| 234 | } |
| 235 | |
| 236 | bool Connection::Execute(const char* sql) { |
| 237 | int error = ExecuteAndReturnErrorCode(sql); |
[email protected] | 28fe0ff | 2012-02-25 00:40:33 | [diff] [blame] | 238 | // This needs to be a FATAL log because the error case of arriving here is |
| 239 | // that there's a malformed SQL statement. This can arise in development if |
| 240 | // a change alters the schema but not all queries adjust. |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 241 | if (error == SQLITE_ERROR) |
[email protected] | 28fe0ff | 2012-02-25 00:40:33 | [diff] [blame] | 242 | DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage(); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 243 | return error == SQLITE_OK; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 246 | bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) { |
| 247 | if (!db_) |
| 248 | return false; |
| 249 | |
| 250 | ScopedBusyTimeout busy_timeout(db_); |
| 251 | busy_timeout.SetTimeout(timeout); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 252 | return Execute(sql); |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 253 | } |
| 254 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 255 | bool Connection::HasCachedStatement(const StatementID& id) const { |
| 256 | return statement_cache_.find(id) != statement_cache_.end(); |
| 257 | } |
| 258 | |
| 259 | scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement( |
| 260 | const StatementID& id, |
| 261 | const char* sql) { |
| 262 | CachedStatementMap::iterator i = statement_cache_.find(id); |
| 263 | if (i != statement_cache_.end()) { |
| 264 | // Statement is in the cache. It should still be active (we're the only |
| 265 | // one invalidating cached statements, and we'll remove it from the cache |
| 266 | // if we do that. Make sure we reset it before giving out the cached one in |
| 267 | // case it still has some stuff bound. |
| 268 | DCHECK(i->second->is_valid()); |
| 269 | sqlite3_reset(i->second->stmt()); |
| 270 | return i->second; |
| 271 | } |
| 272 | |
| 273 | scoped_refptr<StatementRef> statement = GetUniqueStatement(sql); |
| 274 | if (statement->is_valid()) |
| 275 | statement_cache_[id] = statement; // Only cache valid statements. |
| 276 | return statement; |
| 277 | } |
| 278 | |
| 279 | scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement( |
| 280 | const char* sql) { |
| 281 | if (!db_) |
| 282 | return new StatementRef(this, NULL); // Return inactive statement. |
| 283 | |
| 284 | sqlite3_stmt* stmt = NULL; |
| 285 | if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 286 | // This is evidence of a syntax error in the incoming SQL. |
| 287 | DLOG(FATAL) << "SQL compile error " << GetErrorMessage(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 288 | return new StatementRef(this, NULL); |
| 289 | } |
| 290 | return new StatementRef(this, stmt); |
| 291 | } |
| 292 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 293 | bool Connection::IsSQLValid(const char* sql) { |
| 294 | sqlite3_stmt* stmt = NULL; |
| 295 | if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) |
| 296 | return false; |
| 297 | |
| 298 | sqlite3_finalize(stmt); |
| 299 | return true; |
| 300 | } |
| 301 | |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 302 | bool Connection::DoesTableExist(const char* table_name) const { |
[email protected] | e2cadec8 | 2011-12-13 02:00:53 | [diff] [blame] | 303 | return DoesTableOrIndexExist(table_name, "table"); |
| 304 | } |
| 305 | |
| 306 | bool Connection::DoesIndexExist(const char* index_name) const { |
| 307 | return DoesTableOrIndexExist(index_name, "index"); |
| 308 | } |
| 309 | |
| 310 | bool Connection::DoesTableOrIndexExist( |
| 311 | const char* name, const char* type) const { |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 312 | // GetUniqueStatement can't be const since statements may modify the |
| 313 | // database, but we know ours doesn't modify it, so the cast is safe. |
| 314 | Statement statement(const_cast<Connection*>(this)->GetUniqueStatement( |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 315 | "SELECT name FROM sqlite_master " |
[email protected] | e2cadec8 | 2011-12-13 02:00:53 | [diff] [blame] | 316 | "WHERE type=? AND name=?")); |
| 317 | statement.BindString(0, type); |
| 318 | statement.BindString(1, name); |
[email protected] | 28fe0ff | 2012-02-25 00:40:33 | [diff] [blame] | 319 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 320 | return statement.Step(); // Table exists if any row was returned. |
| 321 | } |
| 322 | |
| 323 | bool Connection::DoesColumnExist(const char* table_name, |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 324 | const char* column_name) const { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 325 | std::string sql("PRAGMA TABLE_INFO("); |
| 326 | sql.append(table_name); |
| 327 | sql.append(")"); |
| 328 | |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 329 | // Our SQL is non-mutating, so this cast is OK. |
| 330 | Statement statement(const_cast<Connection*>(this)->GetUniqueStatement( |
| 331 | sql.c_str())); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 332 | |
| 333 | while (statement.Step()) { |
| 334 | if (!statement.ColumnString(1).compare(column_name)) |
| 335 | return true; |
| 336 | } |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | int64 Connection::GetLastInsertRowId() const { |
| 341 | if (!db_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 342 | DLOG(FATAL) << "Illegal use of connection without a db"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | return sqlite3_last_insert_rowid(db_); |
| 346 | } |
| 347 | |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 348 | int Connection::GetLastChangeCount() const { |
| 349 | if (!db_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 350 | DLOG(FATAL) << "Illegal use of connection without a db"; |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | return sqlite3_changes(db_); |
| 354 | } |
| 355 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 356 | int Connection::GetErrorCode() const { |
| 357 | if (!db_) |
| 358 | return SQLITE_ERROR; |
| 359 | return sqlite3_errcode(db_); |
| 360 | } |
| 361 | |
[email protected] | 767718e5 | 2010-09-21 23:18:49 | [diff] [blame] | 362 | int Connection::GetLastErrno() const { |
| 363 | if (!db_) |
| 364 | return -1; |
| 365 | |
| 366 | int err = 0; |
| 367 | if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err)) |
| 368 | return -2; |
| 369 | |
| 370 | return err; |
| 371 | } |
| 372 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 373 | const char* Connection::GetErrorMessage() const { |
| 374 | if (!db_) |
| 375 | return "sql::Connection has no connection."; |
| 376 | return sqlite3_errmsg(db_); |
| 377 | } |
| 378 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 379 | bool Connection::OpenInternal(const std::string& file_name) { |
[email protected] | 9cfbc92 | 2009-11-17 20:13:17 | [diff] [blame] | 380 | if (db_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 381 | DLOG(FATAL) << "sql::Connection is already open."; |
[email protected] | 9cfbc92 | 2009-11-17 20:13:17 | [diff] [blame] | 382 | return false; |
| 383 | } |
| 384 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 385 | int err = sqlite3_open(file_name.c_str(), &db_); |
| 386 | if (err != SQLITE_OK) { |
| 387 | OnSqliteError(err, NULL); |
[email protected] | 6402104 | 2012-02-10 20:02:29 | [diff] [blame] | 388 | Close(); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 389 | db_ = NULL; |
| 390 | return false; |
| 391 | } |
| 392 | |
[email protected] | 658f833 | 2010-09-18 04:40:43 | [diff] [blame] | 393 | // Enable extended result codes to provide more color on I/O errors. |
| 394 | // Not having extended result codes is not a fatal problem, as |
| 395 | // Chromium code does not attempt to handle I/O errors anyhow. The |
| 396 | // current implementation always returns SQLITE_OK, the DCHECK is to |
| 397 | // quickly notify someone if SQLite changes. |
| 398 | err = sqlite3_extended_result_codes(db_, 1); |
| 399 | DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes"; |
| 400 | |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 401 | // If indicated, lock up the database before doing anything else, so |
| 402 | // that the following code doesn't have to deal with locking. |
| 403 | // TODO(shess): This code is brittle. Find the cases where code |
| 404 | // doesn't request |exclusive_locking_| and audit that it does the |
| 405 | // right thing with SQLITE_BUSY, and that it doesn't make |
| 406 | // assumptions about who might change things in the database. |
| 407 | // http://crbug.com/56559 |
| 408 | if (exclusive_locking_) { |
| 409 | // TODO(shess): This should probably be a full CHECK(). Code |
| 410 | // which requests exclusive locking but doesn't get it is almost |
| 411 | // certain to be ill-tested. |
| 412 | if (!Execute("PRAGMA locking_mode=EXCLUSIVE")) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 413 | DLOG(FATAL) << "Could not set locking mode: " << GetErrorMessage(); |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 414 | } |
| 415 | |
[email protected] | 4e179ba6 | 2012-03-17 16:06:47 | [diff] [blame] | 416 | // http://www.sqlite.org/pragma.html#pragma_journal_mode |
| 417 | // DELETE (default) - delete -journal file to commit. |
| 418 | // TRUNCATE - truncate -journal file to commit. |
| 419 | // PERSIST - zero out header of -journal file to commit. |
| 420 | // journal_size_limit provides size to trim to in PERSIST. |
| 421 | // TODO(shess): Figure out if PERSIST and journal_size_limit really |
| 422 | // matter. In theory, it keeps pages pre-allocated, so if |
| 423 | // transactions usually fit, it should be faster. |
| 424 | ignore_result(Execute("PRAGMA journal_mode = PERSIST")); |
| 425 | ignore_result(Execute("PRAGMA journal_size_limit = 16384")); |
| 426 | |
[email protected] | c68ce17 | 2011-11-24 22:30:27 | [diff] [blame] | 427 | const base::TimeDelta kBusyTimeout = |
| 428 | base::TimeDelta::FromSeconds(kBusyTimeoutSeconds); |
| 429 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 430 | if (page_size_ != 0) { |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 431 | // Enforce SQLite restrictions on |page_size_|. |
| 432 | DCHECK(!(page_size_ & (page_size_ - 1))) |
| 433 | << " page_size_ " << page_size_ << " is not a power of two."; |
| 434 | static const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h |
| 435 | DCHECK_LE(page_size_, kSqliteMaxPageSize); |
| 436 | const std::string sql = StringPrintf("PRAGMA page_size=%d", page_size_); |
| 437 | if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout)) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 438 | DLOG(FATAL) << "Could not set page size: " << GetErrorMessage(); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | if (cache_size_ != 0) { |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 442 | const std::string sql = StringPrintf("PRAGMA cache_size=%d", cache_size_); |
| 443 | if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout)) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 444 | DLOG(FATAL) << "Could not set cache size: " << GetErrorMessage(); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 445 | } |
| 446 | |
[email protected] | 6e0b144 | 2011-08-09 23:23:58 | [diff] [blame] | 447 | if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 448 | DLOG(FATAL) << "Could not enable secure_delete: " << GetErrorMessage(); |
[email protected] | 6e0b144 | 2011-08-09 23:23:58 | [diff] [blame] | 449 | Close(); |
| 450 | return false; |
| 451 | } |
| 452 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 453 | return true; |
| 454 | } |
| 455 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 456 | void Connection::DoRollback() { |
| 457 | Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 458 | rollback.Run(); |
[email protected] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame^] | 459 | needs_rollback_ = false; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | void Connection::StatementRefCreated(StatementRef* ref) { |
| 463 | DCHECK(open_statements_.find(ref) == open_statements_.end()); |
| 464 | open_statements_.insert(ref); |
| 465 | } |
| 466 | |
| 467 | void Connection::StatementRefDeleted(StatementRef* ref) { |
| 468 | StatementRefSet::iterator i = open_statements_.find(ref); |
| 469 | if (i == open_statements_.end()) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 470 | DLOG(FATAL) << "Could not find statement"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 471 | else |
| 472 | open_statements_.erase(i); |
| 473 | } |
| 474 | |
| 475 | void Connection::ClearCache() { |
| 476 | statement_cache_.clear(); |
| 477 | |
| 478 | // The cache clear will get most statements. There may be still be references |
| 479 | // to some statements that are held by others (including one-shot statements). |
| 480 | // This will deactivate them so they can't be used again. |
| 481 | for (StatementRefSet::iterator i = open_statements_.begin(); |
| 482 | i != open_statements_.end(); ++i) |
| 483 | (*i)->Close(); |
| 484 | } |
| 485 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 486 | int Connection::OnSqliteError(int err, sql::Statement *stmt) { |
| 487 | if (error_delegate_.get()) |
| 488 | return error_delegate_->OnError(err, this, stmt); |
| 489 | // The default handling is to assert on debug and to ignore on release. |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 490 | DLOG(FATAL) << GetErrorMessage(); |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 491 | return err; |
| 492 | } |
| 493 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 494 | } // namespace sql |