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