Update .DEPS.git
[chromium-blink-merge.git] / sql / connection.cc
blob99bc38173ee2daa7cb486abc2deddcd674a251a3
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sql/connection.h"
7 #include <string.h>
9 #include "base/file_path.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/string_util.h"
13 #include "base/stringprintf.h"
14 #include "base/utf_string_conversions.h"
15 #include "sql/statement.h"
16 #include "third_party/sqlite/sqlite3.h"
18 namespace {
20 // Spin for up to a second waiting for the lock to clear when setting
21 // up the database.
22 // TODO(shess): Better story on this. http://crbug.com/56559
23 const int kBusyTimeoutSeconds = 1;
25 class ScopedBusyTimeout {
26 public:
27 explicit ScopedBusyTimeout(sqlite3* db)
28 : db_(db) {
30 ~ScopedBusyTimeout() {
31 sqlite3_busy_timeout(db_, 0);
34 int SetTimeout(base::TimeDelta timeout) {
35 DCHECK_LT(timeout.InMilliseconds(), INT_MAX);
36 return sqlite3_busy_timeout(db_,
37 static_cast<int>(timeout.InMilliseconds()));
40 private:
41 sqlite3* db_;
44 // Helper to "safely" enable writable_schema. No error checking
45 // because it is reasonable to just forge ahead in case of an error.
46 // If turning it on fails, then most likely nothing will work, whereas
47 // if turning it off fails, it only matters if some code attempts to
48 // continue working with the database and tries to modify the
49 // sqlite_master table (none of our code does this).
50 class ScopedWritableSchema {
51 public:
52 explicit ScopedWritableSchema(sqlite3* db)
53 : db_(db) {
54 sqlite3_exec(db_, "PRAGMA writable_schema=1", NULL, NULL, NULL);
56 ~ScopedWritableSchema() {
57 sqlite3_exec(db_, "PRAGMA writable_schema=0", NULL, NULL, NULL);
60 private:
61 sqlite3* db_;
64 } // namespace
66 namespace sql {
68 bool StatementID::operator<(const StatementID& other) const {
69 if (number_ != other.number_)
70 return number_ < other.number_;
71 return strcmp(str_, other.str_) < 0;
74 ErrorDelegate::~ErrorDelegate() {
77 Connection::StatementRef::StatementRef()
78 : connection_(NULL),
79 stmt_(NULL) {
82 Connection::StatementRef::StatementRef(sqlite3_stmt* stmt)
83 : connection_(NULL),
84 stmt_(stmt) {
87 Connection::StatementRef::StatementRef(Connection* connection,
88 sqlite3_stmt* stmt)
89 : connection_(connection),
90 stmt_(stmt) {
91 connection_->StatementRefCreated(this);
94 Connection::StatementRef::~StatementRef() {
95 if (connection_)
96 connection_->StatementRefDeleted(this);
97 Close();
100 void Connection::StatementRef::Close() {
101 if (stmt_) {
102 // Call to AssertIOAllowed() cannot go at the beginning of the function
103 // because Close() is called unconditionally from destructor to clean
104 // connection_. And if this is inactive statement this won't cause any
105 // disk access and destructor most probably will be called on thread
106 // not allowing disk access.
107 // TODO(paivanof@gmail.com): This should move to the beginning
108 // of the function. http://crbug.com/136655.
109 AssertIOAllowed();
110 sqlite3_finalize(stmt_);
111 stmt_ = NULL;
113 connection_ = NULL; // The connection may be getting deleted.
116 Connection::Connection()
117 : db_(NULL),
118 page_size_(0),
119 cache_size_(0),
120 exclusive_locking_(false),
121 transaction_nesting_(0),
122 needs_rollback_(false),
123 in_memory_(false),
124 error_delegate_(NULL) {
127 Connection::~Connection() {
128 Close();
131 bool Connection::Open(const FilePath& path) {
132 #if defined(OS_WIN)
133 return OpenInternal(WideToUTF8(path.value()));
134 #elif defined(OS_POSIX)
135 return OpenInternal(path.value());
136 #endif
139 bool Connection::OpenInMemory() {
140 in_memory_ = true;
141 return OpenInternal(":memory:");
144 void Connection::Close() {
145 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
146 // will delete the -journal file. For ChromiumOS or other more
147 // embedded systems, this is probably not appropriate, whereas on
148 // desktop it might make some sense.
150 // sqlite3_close() needs all prepared statements to be finalized.
151 // Release all cached statements, then assert that the client has
152 // released all statements.
153 statement_cache_.clear();
154 DCHECK(open_statements_.empty());
156 // Additionally clear the prepared statements, because they contain
157 // weak references to this connection. This case has come up when
158 // error-handling code is hit in production.
159 ClearCache();
161 if (db_) {
162 // Call to AssertIOAllowed() cannot go at the beginning of the function
163 // because Close() must be called from destructor to clean
164 // statement_cache_, it won't cause any disk access and it most probably
165 // will happen on thread not allowing disk access.
166 // TODO(paivanof@gmail.com): This should move to the beginning
167 // of the function. http://crbug.com/136655.
168 AssertIOAllowed();
169 // TODO(shess): Histogram for failure.
170 sqlite3_close(db_);
171 db_ = NULL;
175 void Connection::Preload() {
176 AssertIOAllowed();
178 if (!db_) {
179 DLOG(FATAL) << "Cannot preload null db";
180 return;
183 // A statement must be open for the preload command to work. If the meta
184 // table doesn't exist, it probably means this is a new database and there
185 // is nothing to preload (so it's OK we do nothing).
186 if (!DoesTableExist("meta"))
187 return;
188 Statement dummy(GetUniqueStatement("SELECT * FROM meta"));
189 if (!dummy.Step())
190 return;
192 #if !defined(USE_SYSTEM_SQLITE)
193 // This function is only defined in Chromium's version of sqlite.
194 // Do not call it when using system sqlite.
195 sqlite3_preload(db_);
196 #endif
199 // Create an in-memory database with the existing database's page
200 // size, then backup that database over the existing database.
201 bool Connection::Raze() {
202 AssertIOAllowed();
204 if (!db_) {
205 DLOG(FATAL) << "Cannot raze null db";
206 return false;
209 if (transaction_nesting_ > 0) {
210 DLOG(FATAL) << "Cannot raze within a transaction";
211 return false;
214 sql::Connection null_db;
215 if (!null_db.OpenInMemory()) {
216 DLOG(FATAL) << "Unable to open in-memory database.";
217 return false;
220 if (page_size_) {
221 // Enforce SQLite restrictions on |page_size_|.
222 DCHECK(!(page_size_ & (page_size_ - 1)))
223 << " page_size_ " << page_size_ << " is not a power of two.";
224 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
225 DCHECK_LE(page_size_, kSqliteMaxPageSize);
226 const std::string sql = StringPrintf("PRAGMA page_size=%d", page_size_);
227 if (!null_db.Execute(sql.c_str()))
228 return false;
231 #if defined(OS_ANDROID)
232 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
233 // in-memory databases do not respect this define.
234 // TODO(shess): Figure out a way to set this without using platform
235 // specific code. AFAICT from sqlite3.c, the only way to do it
236 // would be to create an actual filesystem database, which is
237 // unfortunate.
238 if (!null_db.Execute("PRAGMA auto_vacuum = 1"))
239 return false;
240 #endif
242 // The page size doesn't take effect until a database has pages, and
243 // at this point the null database has none. Changing the schema
244 // version will create the first page. This will not affect the
245 // schema version in the resulting database, as SQLite's backup
246 // implementation propagates the schema version from the original
247 // connection to the new version of the database, incremented by one
248 // so that other readers see the schema change and act accordingly.
249 if (!null_db.Execute("PRAGMA schema_version = 1"))
250 return false;
252 // SQLite tracks the expected number of database pages in the first
253 // page, and if it does not match the total retrieved from a
254 // filesystem call, treats the database as corrupt. This situation
255 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
256 // used to hint to SQLite to soldier on in that case, specifically
257 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
258 // sqlite3.c lockBtree().]
259 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
260 // page_size" can be used to query such a database.
261 ScopedWritableSchema writable_schema(db_);
263 sqlite3_backup* backup = sqlite3_backup_init(db_, "main",
264 null_db.db_, "main");
265 if (!backup) {
266 DLOG(FATAL) << "Unable to start sqlite3_backup().";
267 return false;
270 // -1 backs up the entire database.
271 int rc = sqlite3_backup_step(backup, -1);
272 int pages = sqlite3_backup_pagecount(backup);
273 sqlite3_backup_finish(backup);
275 // The destination database was locked.
276 if (rc == SQLITE_BUSY) {
277 return false;
280 // The entire database should have been backed up.
281 if (rc != SQLITE_DONE) {
282 DLOG(FATAL) << "Unable to copy entire null database.";
283 return false;
286 // Exactly one page should have been backed up. If this breaks,
287 // check this function to make sure assumptions aren't being broken.
288 DCHECK_EQ(pages, 1);
290 return true;
293 bool Connection::RazeWithTimout(base::TimeDelta timeout) {
294 if (!db_) {
295 DLOG(FATAL) << "Cannot raze null db";
296 return false;
299 ScopedBusyTimeout busy_timeout(db_);
300 busy_timeout.SetTimeout(timeout);
301 return Raze();
304 bool Connection::BeginTransaction() {
305 if (needs_rollback_) {
306 DCHECK_GT(transaction_nesting_, 0);
308 // When we're going to rollback, fail on this begin and don't actually
309 // mark us as entering the nested transaction.
310 return false;
313 bool success = true;
314 if (!transaction_nesting_) {
315 needs_rollback_ = false;
317 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
318 if (!begin.Run())
319 return false;
321 transaction_nesting_++;
322 return success;
325 void Connection::RollbackTransaction() {
326 if (!transaction_nesting_) {
327 DLOG(FATAL) << "Rolling back a nonexistent transaction";
328 return;
331 transaction_nesting_--;
333 if (transaction_nesting_ > 0) {
334 // Mark the outermost transaction as needing rollback.
335 needs_rollback_ = true;
336 return;
339 DoRollback();
342 bool Connection::CommitTransaction() {
343 if (!transaction_nesting_) {
344 DLOG(FATAL) << "Rolling back a nonexistent transaction";
345 return false;
347 transaction_nesting_--;
349 if (transaction_nesting_ > 0) {
350 // Mark any nested transactions as failing after we've already got one.
351 return !needs_rollback_;
354 if (needs_rollback_) {
355 DoRollback();
356 return false;
359 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
360 return commit.Run();
363 int Connection::ExecuteAndReturnErrorCode(const char* sql) {
364 AssertIOAllowed();
365 if (!db_)
366 return false;
367 return sqlite3_exec(db_, sql, NULL, NULL, NULL);
370 bool Connection::Execute(const char* sql) {
371 int error = ExecuteAndReturnErrorCode(sql);
372 if (error != SQLITE_OK)
373 error = OnSqliteError(error, NULL);
375 // This needs to be a FATAL log because the error case of arriving here is
376 // that there's a malformed SQL statement. This can arise in development if
377 // a change alters the schema but not all queries adjust.
378 if (error == SQLITE_ERROR)
379 DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage();
380 return error == SQLITE_OK;
383 bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
384 if (!db_)
385 return false;
387 ScopedBusyTimeout busy_timeout(db_);
388 busy_timeout.SetTimeout(timeout);
389 return Execute(sql);
392 bool Connection::HasCachedStatement(const StatementID& id) const {
393 return statement_cache_.find(id) != statement_cache_.end();
396 scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement(
397 const StatementID& id,
398 const char* sql) {
399 CachedStatementMap::iterator i = statement_cache_.find(id);
400 if (i != statement_cache_.end()) {
401 // Statement is in the cache. It should still be active (we're the only
402 // one invalidating cached statements, and we'll remove it from the cache
403 // if we do that. Make sure we reset it before giving out the cached one in
404 // case it still has some stuff bound.
405 DCHECK(i->second->is_valid());
406 sqlite3_reset(i->second->stmt());
407 return i->second;
410 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
411 if (statement->is_valid())
412 statement_cache_[id] = statement; // Only cache valid statements.
413 return statement;
416 scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement(
417 const char* sql) {
418 AssertIOAllowed();
420 if (!db_)
421 return new StatementRef(); // Return inactive statement.
423 sqlite3_stmt* stmt = NULL;
424 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
425 if (rc != SQLITE_OK) {
426 // This is evidence of a syntax error in the incoming SQL.
427 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
429 // It could also be database corruption.
430 OnSqliteError(rc, NULL);
431 return new StatementRef();
433 return new StatementRef(this, stmt);
436 scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
437 const char* sql) const {
438 if (!db_)
439 return new StatementRef(); // Return inactive statement.
441 sqlite3_stmt* stmt = NULL;
442 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
443 if (rc != SQLITE_OK) {
444 // This is evidence of a syntax error in the incoming SQL.
445 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
446 return new StatementRef();
448 return new StatementRef(stmt);
451 bool Connection::IsSQLValid(const char* sql) {
452 AssertIOAllowed();
453 sqlite3_stmt* stmt = NULL;
454 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK)
455 return false;
457 sqlite3_finalize(stmt);
458 return true;
461 bool Connection::DoesTableExist(const char* table_name) const {
462 return DoesTableOrIndexExist(table_name, "table");
465 bool Connection::DoesIndexExist(const char* index_name) const {
466 return DoesTableOrIndexExist(index_name, "index");
469 bool Connection::DoesTableOrIndexExist(
470 const char* name, const char* type) const {
471 const char* kSql = "SELECT name FROM sqlite_master WHERE type=? AND name=?";
472 Statement statement(GetUntrackedStatement(kSql));
473 statement.BindString(0, type);
474 statement.BindString(1, name);
476 return statement.Step(); // Table exists if any row was returned.
479 bool Connection::DoesColumnExist(const char* table_name,
480 const char* column_name) const {
481 std::string sql("PRAGMA TABLE_INFO(");
482 sql.append(table_name);
483 sql.append(")");
485 Statement statement(GetUntrackedStatement(sql.c_str()));
486 while (statement.Step()) {
487 if (!statement.ColumnString(1).compare(column_name))
488 return true;
490 return false;
493 int64 Connection::GetLastInsertRowId() const {
494 if (!db_) {
495 DLOG(FATAL) << "Illegal use of connection without a db";
496 return 0;
498 return sqlite3_last_insert_rowid(db_);
501 int Connection::GetLastChangeCount() const {
502 if (!db_) {
503 DLOG(FATAL) << "Illegal use of connection without a db";
504 return 0;
506 return sqlite3_changes(db_);
509 int Connection::GetErrorCode() const {
510 if (!db_)
511 return SQLITE_ERROR;
512 return sqlite3_errcode(db_);
515 int Connection::GetLastErrno() const {
516 if (!db_)
517 return -1;
519 int err = 0;
520 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err))
521 return -2;
523 return err;
526 const char* Connection::GetErrorMessage() const {
527 if (!db_)
528 return "sql::Connection has no connection.";
529 return sqlite3_errmsg(db_);
532 bool Connection::OpenInternal(const std::string& file_name) {
533 AssertIOAllowed();
535 if (db_) {
536 DLOG(FATAL) << "sql::Connection is already open.";
537 return false;
540 int err = sqlite3_open(file_name.c_str(), &db_);
541 if (err != SQLITE_OK) {
542 // Histogram failures specific to initial open for debugging
543 // purposes.
544 UMA_HISTOGRAM_ENUMERATION("Sqlite.OpenFailure", err & 0xff, 50);
546 OnSqliteError(err, NULL);
547 Close();
548 db_ = NULL;
549 return false;
552 // sqlite3_open() does not actually read the database file (unless a
553 // hot journal is found). Successfully executing this pragma on an
554 // existing database requires a valid header on page 1.
555 // TODO(shess): For now, just probing to see what the lay of the
556 // land is. If it's mostly SQLITE_NOTADB, then the database should
557 // be razed.
558 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
559 if (err != SQLITE_OK)
560 UMA_HISTOGRAM_ENUMERATION("Sqlite.OpenProbeFailure", err & 0xff, 50);
562 // Enable extended result codes to provide more color on I/O errors.
563 // Not having extended result codes is not a fatal problem, as
564 // Chromium code does not attempt to handle I/O errors anyhow. The
565 // current implementation always returns SQLITE_OK, the DCHECK is to
566 // quickly notify someone if SQLite changes.
567 err = sqlite3_extended_result_codes(db_, 1);
568 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
570 // If indicated, lock up the database before doing anything else, so
571 // that the following code doesn't have to deal with locking.
572 // TODO(shess): This code is brittle. Find the cases where code
573 // doesn't request |exclusive_locking_| and audit that it does the
574 // right thing with SQLITE_BUSY, and that it doesn't make
575 // assumptions about who might change things in the database.
576 // http://crbug.com/56559
577 if (exclusive_locking_) {
578 // TODO(shess): This should probably be a full CHECK(). Code
579 // which requests exclusive locking but doesn't get it is almost
580 // certain to be ill-tested.
581 if (!Execute("PRAGMA locking_mode=EXCLUSIVE"))
582 DLOG(FATAL) << "Could not set locking mode: " << GetErrorMessage();
585 // http://www.sqlite.org/pragma.html#pragma_journal_mode
586 // DELETE (default) - delete -journal file to commit.
587 // TRUNCATE - truncate -journal file to commit.
588 // PERSIST - zero out header of -journal file to commit.
589 // journal_size_limit provides size to trim to in PERSIST.
590 // TODO(shess): Figure out if PERSIST and journal_size_limit really
591 // matter. In theory, it keeps pages pre-allocated, so if
592 // transactions usually fit, it should be faster.
593 ignore_result(Execute("PRAGMA journal_mode = PERSIST"));
594 ignore_result(Execute("PRAGMA journal_size_limit = 16384"));
596 const base::TimeDelta kBusyTimeout =
597 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
599 if (page_size_ != 0) {
600 // Enforce SQLite restrictions on |page_size_|.
601 DCHECK(!(page_size_ & (page_size_ - 1)))
602 << " page_size_ " << page_size_ << " is not a power of two.";
603 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
604 DCHECK_LE(page_size_, kSqliteMaxPageSize);
605 const std::string sql = StringPrintf("PRAGMA page_size=%d", page_size_);
606 if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout))
607 DLOG(FATAL) << "Could not set page size: " << GetErrorMessage();
610 if (cache_size_ != 0) {
611 const std::string sql = StringPrintf("PRAGMA cache_size=%d", cache_size_);
612 if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout))
613 DLOG(FATAL) << "Could not set cache size: " << GetErrorMessage();
616 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) {
617 DLOG(FATAL) << "Could not enable secure_delete: " << GetErrorMessage();
618 Close();
619 return false;
622 return true;
625 void Connection::DoRollback() {
626 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
627 rollback.Run();
628 needs_rollback_ = false;
631 void Connection::StatementRefCreated(StatementRef* ref) {
632 DCHECK(open_statements_.find(ref) == open_statements_.end());
633 open_statements_.insert(ref);
636 void Connection::StatementRefDeleted(StatementRef* ref) {
637 StatementRefSet::iterator i = open_statements_.find(ref);
638 if (i == open_statements_.end())
639 DLOG(FATAL) << "Could not find statement";
640 else
641 open_statements_.erase(i);
644 void Connection::ClearCache() {
645 statement_cache_.clear();
647 // The cache clear will get most statements. There may be still be references
648 // to some statements that are held by others (including one-shot statements).
649 // This will deactivate them so they can't be used again.
650 for (StatementRefSet::iterator i = open_statements_.begin();
651 i != open_statements_.end(); ++i)
652 (*i)->Close();
655 int Connection::OnSqliteError(int err, sql::Statement *stmt) {
656 if (error_delegate_.get())
657 return error_delegate_->OnError(err, this, stmt);
658 // The default handling is to assert on debug and to ignore on release.
659 DLOG(FATAL) << GetErrorMessage();
660 return err;
663 } // namespace sql