[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 | |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame^] | 171 | // Create an in-memory database with the existing database's page |
| 172 | // size, then backup that database over the existing database. |
| 173 | bool Connection::Raze() { |
| 174 | if (!db_) { |
| 175 | DLOG(FATAL) << "Cannot raze null db"; |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | if (transaction_nesting_ > 0) { |
| 180 | DLOG(FATAL) << "Cannot raze within a transaction"; |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | sql::Connection null_db; |
| 185 | if (!null_db.OpenInMemory()) { |
| 186 | DLOG(FATAL) << "Unable to open in-memory database."; |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | // Get the page size from the current connection, then propagate it |
| 191 | // to the null database. |
| 192 | Statement s(GetUniqueStatement("PRAGMA page_size")); |
| 193 | if (!s.Step()) |
| 194 | return false; |
| 195 | const std::string sql = StringPrintf("PRAGMA page_size=%d", s.ColumnInt(0)); |
| 196 | if (!null_db.Execute(sql.c_str())) |
| 197 | return false; |
| 198 | |
| 199 | // The page size doesn't take effect until a database has pages, and |
| 200 | // at this point the null database has none. Changing the schema |
| 201 | // version will create the first page. This will not affect the |
| 202 | // schema version in the resulting database, as SQLite's backup |
| 203 | // implementation propagates the schema version from the original |
| 204 | // connection to the new version of the database, incremented by one |
| 205 | // so that other readers see the schema change and act accordingly. |
| 206 | if (!null_db.Execute("PRAGMA schema_version = 1")) |
| 207 | return false; |
| 208 | |
| 209 | sqlite3_backup* backup = sqlite3_backup_init(db_, "main", |
| 210 | null_db.db_, "main"); |
| 211 | if (!backup) { |
| 212 | DLOG(FATAL) << "Unable to start sqlite3_backup()."; |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | // -1 backs up the entire database. |
| 217 | int rc = sqlite3_backup_step(backup, -1); |
| 218 | int pages = sqlite3_backup_pagecount(backup); |
| 219 | sqlite3_backup_finish(backup); |
| 220 | |
| 221 | // The destination database was locked. |
| 222 | if (rc == SQLITE_BUSY) { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | // The entire database should have been backed up. |
| 227 | if (rc != SQLITE_DONE) { |
| 228 | DLOG(FATAL) << "Unable to copy entire null database."; |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | // Exactly one page should have been backed up. If this breaks, |
| 233 | // check this function to make sure assumptions aren't being broken. |
| 234 | DCHECK_EQ(pages, 1); |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | bool Connection::RazeWithTimout(base::TimeDelta timeout) { |
| 240 | if (!db_) { |
| 241 | DLOG(FATAL) << "Cannot raze null db"; |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | ScopedBusyTimeout busy_timeout(db_); |
| 246 | busy_timeout.SetTimeout(timeout); |
| 247 | return Raze(); |
| 248 | } |
| 249 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 250 | bool Connection::BeginTransaction() { |
| 251 | if (needs_rollback_) { |
[email protected] | 88563f6 | 2011-03-13 22:13:33 | [diff] [blame] | 252 | DCHECK_GT(transaction_nesting_, 0); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 253 | |
| 254 | // When we're going to rollback, fail on this begin and don't actually |
| 255 | // mark us as entering the nested transaction. |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | bool success = true; |
| 260 | if (!transaction_nesting_) { |
| 261 | needs_rollback_ = false; |
| 262 | |
| 263 | Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 264 | if (!begin.Run()) |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | transaction_nesting_++; |
| 268 | return success; |
| 269 | } |
| 270 | |
| 271 | void Connection::RollbackTransaction() { |
| 272 | if (!transaction_nesting_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 273 | DLOG(FATAL) << "Rolling back a nonexistent transaction"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | |
| 277 | transaction_nesting_--; |
| 278 | |
| 279 | if (transaction_nesting_ > 0) { |
| 280 | // Mark the outermost transaction as needing rollback. |
| 281 | needs_rollback_ = true; |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | DoRollback(); |
| 286 | } |
| 287 | |
| 288 | bool Connection::CommitTransaction() { |
| 289 | if (!transaction_nesting_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 290 | DLOG(FATAL) << "Rolling back a nonexistent transaction"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | transaction_nesting_--; |
| 294 | |
| 295 | if (transaction_nesting_ > 0) { |
| 296 | // Mark any nested transactions as failing after we've already got one. |
| 297 | return !needs_rollback_; |
| 298 | } |
| 299 | |
| 300 | if (needs_rollback_) { |
| 301 | DoRollback(); |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 306 | return commit.Run(); |
| 307 | } |
| 308 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 309 | int Connection::ExecuteAndReturnErrorCode(const char* sql) { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 310 | if (!db_) |
| 311 | return false; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 312 | return sqlite3_exec(db_, sql, NULL, NULL, NULL); |
| 313 | } |
| 314 | |
| 315 | bool Connection::Execute(const char* sql) { |
| 316 | int error = ExecuteAndReturnErrorCode(sql); |
[email protected] | 28fe0ff | 2012-02-25 00:40:33 | [diff] [blame] | 317 | // This needs to be a FATAL log because the error case of arriving here is |
| 318 | // that there's a malformed SQL statement. This can arise in development if |
| 319 | // a change alters the schema but not all queries adjust. |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 320 | if (error == SQLITE_ERROR) |
[email protected] | 28fe0ff | 2012-02-25 00:40:33 | [diff] [blame] | 321 | DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage(); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 322 | return error == SQLITE_OK; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 325 | bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) { |
| 326 | if (!db_) |
| 327 | return false; |
| 328 | |
| 329 | ScopedBusyTimeout busy_timeout(db_); |
| 330 | busy_timeout.SetTimeout(timeout); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 331 | return Execute(sql); |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 332 | } |
| 333 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 334 | bool Connection::HasCachedStatement(const StatementID& id) const { |
| 335 | return statement_cache_.find(id) != statement_cache_.end(); |
| 336 | } |
| 337 | |
| 338 | scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement( |
| 339 | const StatementID& id, |
| 340 | const char* sql) { |
| 341 | CachedStatementMap::iterator i = statement_cache_.find(id); |
| 342 | if (i != statement_cache_.end()) { |
| 343 | // Statement is in the cache. It should still be active (we're the only |
| 344 | // one invalidating cached statements, and we'll remove it from the cache |
| 345 | // if we do that. Make sure we reset it before giving out the cached one in |
| 346 | // case it still has some stuff bound. |
| 347 | DCHECK(i->second->is_valid()); |
| 348 | sqlite3_reset(i->second->stmt()); |
| 349 | return i->second; |
| 350 | } |
| 351 | |
| 352 | scoped_refptr<StatementRef> statement = GetUniqueStatement(sql); |
| 353 | if (statement->is_valid()) |
| 354 | statement_cache_[id] = statement; // Only cache valid statements. |
| 355 | return statement; |
| 356 | } |
| 357 | |
| 358 | scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement( |
| 359 | const char* sql) { |
| 360 | if (!db_) |
| 361 | return new StatementRef(this, NULL); // Return inactive statement. |
| 362 | |
| 363 | sqlite3_stmt* stmt = NULL; |
| 364 | if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 365 | // This is evidence of a syntax error in the incoming SQL. |
| 366 | DLOG(FATAL) << "SQL compile error " << GetErrorMessage(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 367 | return new StatementRef(this, NULL); |
| 368 | } |
| 369 | return new StatementRef(this, stmt); |
| 370 | } |
| 371 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 372 | bool Connection::IsSQLValid(const char* sql) { |
| 373 | sqlite3_stmt* stmt = NULL; |
| 374 | if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) |
| 375 | return false; |
| 376 | |
| 377 | sqlite3_finalize(stmt); |
| 378 | return true; |
| 379 | } |
| 380 | |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 381 | bool Connection::DoesTableExist(const char* table_name) const { |
[email protected] | e2cadec8 | 2011-12-13 02:00:53 | [diff] [blame] | 382 | return DoesTableOrIndexExist(table_name, "table"); |
| 383 | } |
| 384 | |
| 385 | bool Connection::DoesIndexExist(const char* index_name) const { |
| 386 | return DoesTableOrIndexExist(index_name, "index"); |
| 387 | } |
| 388 | |
| 389 | bool Connection::DoesTableOrIndexExist( |
| 390 | const char* name, const char* type) const { |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 391 | // GetUniqueStatement can't be const since statements may modify the |
| 392 | // database, but we know ours doesn't modify it, so the cast is safe. |
| 393 | Statement statement(const_cast<Connection*>(this)->GetUniqueStatement( |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 394 | "SELECT name FROM sqlite_master " |
[email protected] | e2cadec8 | 2011-12-13 02:00:53 | [diff] [blame] | 395 | "WHERE type=? AND name=?")); |
| 396 | statement.BindString(0, type); |
| 397 | statement.BindString(1, name); |
[email protected] | 28fe0ff | 2012-02-25 00:40:33 | [diff] [blame] | 398 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 399 | return statement.Step(); // Table exists if any row was returned. |
| 400 | } |
| 401 | |
| 402 | bool Connection::DoesColumnExist(const char* table_name, |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 403 | const char* column_name) const { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 404 | std::string sql("PRAGMA TABLE_INFO("); |
| 405 | sql.append(table_name); |
| 406 | sql.append(")"); |
| 407 | |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 408 | // Our SQL is non-mutating, so this cast is OK. |
| 409 | Statement statement(const_cast<Connection*>(this)->GetUniqueStatement( |
| 410 | sql.c_str())); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 411 | |
| 412 | while (statement.Step()) { |
| 413 | if (!statement.ColumnString(1).compare(column_name)) |
| 414 | return true; |
| 415 | } |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | int64 Connection::GetLastInsertRowId() const { |
| 420 | if (!db_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 421 | DLOG(FATAL) << "Illegal use of connection without a db"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | return sqlite3_last_insert_rowid(db_); |
| 425 | } |
| 426 | |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 427 | int Connection::GetLastChangeCount() const { |
| 428 | if (!db_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 429 | DLOG(FATAL) << "Illegal use of connection without a db"; |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | return sqlite3_changes(db_); |
| 433 | } |
| 434 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 435 | int Connection::GetErrorCode() const { |
| 436 | if (!db_) |
| 437 | return SQLITE_ERROR; |
| 438 | return sqlite3_errcode(db_); |
| 439 | } |
| 440 | |
[email protected] | 767718e5 | 2010-09-21 23:18:49 | [diff] [blame] | 441 | int Connection::GetLastErrno() const { |
| 442 | if (!db_) |
| 443 | return -1; |
| 444 | |
| 445 | int err = 0; |
| 446 | if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err)) |
| 447 | return -2; |
| 448 | |
| 449 | return err; |
| 450 | } |
| 451 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 452 | const char* Connection::GetErrorMessage() const { |
| 453 | if (!db_) |
| 454 | return "sql::Connection has no connection."; |
| 455 | return sqlite3_errmsg(db_); |
| 456 | } |
| 457 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 458 | bool Connection::OpenInternal(const std::string& file_name) { |
[email protected] | 9cfbc92 | 2009-11-17 20:13:17 | [diff] [blame] | 459 | if (db_) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 460 | DLOG(FATAL) << "sql::Connection is already open."; |
[email protected] | 9cfbc92 | 2009-11-17 20:13:17 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 464 | int err = sqlite3_open(file_name.c_str(), &db_); |
| 465 | if (err != SQLITE_OK) { |
| 466 | OnSqliteError(err, NULL); |
[email protected] | 6402104 | 2012-02-10 20:02:29 | [diff] [blame] | 467 | Close(); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 468 | db_ = NULL; |
| 469 | return false; |
| 470 | } |
| 471 | |
[email protected] | 658f833 | 2010-09-18 04:40:43 | [diff] [blame] | 472 | // Enable extended result codes to provide more color on I/O errors. |
| 473 | // Not having extended result codes is not a fatal problem, as |
| 474 | // Chromium code does not attempt to handle I/O errors anyhow. The |
| 475 | // current implementation always returns SQLITE_OK, the DCHECK is to |
| 476 | // quickly notify someone if SQLite changes. |
| 477 | err = sqlite3_extended_result_codes(db_, 1); |
| 478 | DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes"; |
| 479 | |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 480 | // If indicated, lock up the database before doing anything else, so |
| 481 | // that the following code doesn't have to deal with locking. |
| 482 | // TODO(shess): This code is brittle. Find the cases where code |
| 483 | // doesn't request |exclusive_locking_| and audit that it does the |
| 484 | // right thing with SQLITE_BUSY, and that it doesn't make |
| 485 | // assumptions about who might change things in the database. |
| 486 | // http://crbug.com/56559 |
| 487 | if (exclusive_locking_) { |
| 488 | // TODO(shess): This should probably be a full CHECK(). Code |
| 489 | // which requests exclusive locking but doesn't get it is almost |
| 490 | // certain to be ill-tested. |
| 491 | if (!Execute("PRAGMA locking_mode=EXCLUSIVE")) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 492 | DLOG(FATAL) << "Could not set locking mode: " << GetErrorMessage(); |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 493 | } |
| 494 | |
[email protected] | 4e179ba6 | 2012-03-17 16:06:47 | [diff] [blame] | 495 | // http://www.sqlite.org/pragma.html#pragma_journal_mode |
| 496 | // DELETE (default) - delete -journal file to commit. |
| 497 | // TRUNCATE - truncate -journal file to commit. |
| 498 | // PERSIST - zero out header of -journal file to commit. |
| 499 | // journal_size_limit provides size to trim to in PERSIST. |
| 500 | // TODO(shess): Figure out if PERSIST and journal_size_limit really |
| 501 | // matter. In theory, it keeps pages pre-allocated, so if |
| 502 | // transactions usually fit, it should be faster. |
| 503 | ignore_result(Execute("PRAGMA journal_mode = PERSIST")); |
| 504 | ignore_result(Execute("PRAGMA journal_size_limit = 16384")); |
| 505 | |
[email protected] | c68ce17 | 2011-11-24 22:30:27 | [diff] [blame] | 506 | const base::TimeDelta kBusyTimeout = |
| 507 | base::TimeDelta::FromSeconds(kBusyTimeoutSeconds); |
| 508 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 509 | if (page_size_ != 0) { |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 510 | // Enforce SQLite restrictions on |page_size_|. |
| 511 | DCHECK(!(page_size_ & (page_size_ - 1))) |
| 512 | << " page_size_ " << page_size_ << " is not a power of two."; |
| 513 | static const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h |
| 514 | DCHECK_LE(page_size_, kSqliteMaxPageSize); |
| 515 | const std::string sql = StringPrintf("PRAGMA page_size=%d", page_size_); |
| 516 | if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout)) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 517 | DLOG(FATAL) << "Could not set page size: " << GetErrorMessage(); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | if (cache_size_ != 0) { |
[email protected] | 5b96f377 | 2010-09-28 16:30:57 | [diff] [blame] | 521 | const std::string sql = StringPrintf("PRAGMA cache_size=%d", cache_size_); |
| 522 | if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout)) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 523 | DLOG(FATAL) << "Could not set cache size: " << GetErrorMessage(); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 524 | } |
| 525 | |
[email protected] | 6e0b144 | 2011-08-09 23:23:58 | [diff] [blame] | 526 | if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 527 | DLOG(FATAL) << "Could not enable secure_delete: " << GetErrorMessage(); |
[email protected] | 6e0b144 | 2011-08-09 23:23:58 | [diff] [blame] | 528 | Close(); |
| 529 | return false; |
| 530 | } |
| 531 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 532 | return true; |
| 533 | } |
| 534 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 535 | void Connection::DoRollback() { |
| 536 | Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 537 | rollback.Run(); |
[email protected] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 538 | needs_rollback_ = false; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | void Connection::StatementRefCreated(StatementRef* ref) { |
| 542 | DCHECK(open_statements_.find(ref) == open_statements_.end()); |
| 543 | open_statements_.insert(ref); |
| 544 | } |
| 545 | |
| 546 | void Connection::StatementRefDeleted(StatementRef* ref) { |
| 547 | StatementRefSet::iterator i = open_statements_.find(ref); |
| 548 | if (i == open_statements_.end()) |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 549 | DLOG(FATAL) << "Could not find statement"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 550 | else |
| 551 | open_statements_.erase(i); |
| 552 | } |
| 553 | |
| 554 | void Connection::ClearCache() { |
| 555 | statement_cache_.clear(); |
| 556 | |
| 557 | // The cache clear will get most statements. There may be still be references |
| 558 | // to some statements that are held by others (including one-shot statements). |
| 559 | // This will deactivate them so they can't be used again. |
| 560 | for (StatementRefSet::iterator i = open_statements_.begin(); |
| 561 | i != open_statements_.end(); ++i) |
| 562 | (*i)->Close(); |
| 563 | } |
| 564 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 565 | int Connection::OnSqliteError(int err, sql::Statement *stmt) { |
| 566 | if (error_delegate_.get()) |
| 567 | return error_delegate_->OnError(err, this, stmt); |
| 568 | // The default handling is to assert on debug and to ignore on release. |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 569 | DLOG(FATAL) << GetErrorMessage(); |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 570 | return err; |
| 571 | } |
| 572 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 573 | } // namespace sql |