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"
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"
20 // Spin for up to a second waiting for the lock to clear when setting
22 // TODO(shess): Better story on this. http://crbug.com/56559
23 const int kBusyTimeoutSeconds
= 1;
25 class ScopedBusyTimeout
{
27 explicit ScopedBusyTimeout(sqlite3
* 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()));
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
{
52 explicit ScopedWritableSchema(sqlite3
* 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
);
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()
82 Connection::StatementRef::StatementRef(sqlite3_stmt
* stmt
)
87 Connection::StatementRef::StatementRef(Connection
* connection
,
89 : connection_(connection
),
91 connection_
->StatementRefCreated(this);
94 Connection::StatementRef::~StatementRef() {
96 connection_
->StatementRefDeleted(this);
100 void Connection::StatementRef::Close() {
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.
110 sqlite3_finalize(stmt_
);
113 connection_
= NULL
; // The connection may be getting deleted.
116 Connection::Connection()
120 exclusive_locking_(false),
121 transaction_nesting_(0),
122 needs_rollback_(false),
124 error_delegate_(NULL
) {
127 Connection::~Connection() {
131 bool Connection::Open(const FilePath
& path
) {
133 return OpenInternal(WideToUTF8(path
.value()));
134 #elif defined(OS_POSIX)
135 return OpenInternal(path
.value());
139 bool Connection::OpenInMemory() {
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.
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.
169 // TODO(shess): Histogram for failure.
175 void Connection::Preload() {
179 DLOG(FATAL
) << "Cannot preload null db";
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"))
188 Statement
dummy(GetUniqueStatement("SELECT * FROM meta"));
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_
);
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() {
205 DLOG(FATAL
) << "Cannot raze null db";
209 if (transaction_nesting_
> 0) {
210 DLOG(FATAL
) << "Cannot raze within a transaction";
214 sql::Connection null_db
;
215 if (!null_db
.OpenInMemory()) {
216 DLOG(FATAL
) << "Unable to open in-memory database.";
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()))
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
238 if (!null_db
.Execute("PRAGMA auto_vacuum = 1"))
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"))
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");
266 DLOG(FATAL
) << "Unable to start sqlite3_backup().";
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
) {
280 // The entire database should have been backed up.
281 if (rc
!= SQLITE_DONE
) {
282 DLOG(FATAL
) << "Unable to copy entire null database.";
286 // Exactly one page should have been backed up. If this breaks,
287 // check this function to make sure assumptions aren't being broken.
293 bool Connection::RazeWithTimout(base::TimeDelta timeout
) {
295 DLOG(FATAL
) << "Cannot raze null db";
299 ScopedBusyTimeout
busy_timeout(db_
);
300 busy_timeout
.SetTimeout(timeout
);
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.
314 if (!transaction_nesting_
) {
315 needs_rollback_
= false;
317 Statement
begin(GetCachedStatement(SQL_FROM_HERE
, "BEGIN TRANSACTION"));
321 transaction_nesting_
++;
325 void Connection::RollbackTransaction() {
326 if (!transaction_nesting_
) {
327 DLOG(FATAL
) << "Rolling back a nonexistent transaction";
331 transaction_nesting_
--;
333 if (transaction_nesting_
> 0) {
334 // Mark the outermost transaction as needing rollback.
335 needs_rollback_
= true;
342 bool Connection::CommitTransaction() {
343 if (!transaction_nesting_
) {
344 DLOG(FATAL
) << "Rolling back a nonexistent transaction";
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_
) {
359 Statement
commit(GetCachedStatement(SQL_FROM_HERE
, "COMMIT"));
363 int Connection::ExecuteAndReturnErrorCode(const char* sql
) {
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
) {
387 ScopedBusyTimeout
busy_timeout(db_
);
388 busy_timeout
.SetTimeout(timeout
);
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
,
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());
410 scoped_refptr
<StatementRef
> statement
= GetUniqueStatement(sql
);
411 if (statement
->is_valid())
412 statement_cache_
[id
] = statement
; // Only cache valid statements.
416 scoped_refptr
<Connection::StatementRef
> Connection::GetUniqueStatement(
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 {
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
) {
453 sqlite3_stmt
* stmt
= NULL
;
454 if (sqlite3_prepare_v2(db_
, sql
, -1, &stmt
, NULL
) != SQLITE_OK
)
457 sqlite3_finalize(stmt
);
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
);
485 Statement
statement(GetUntrackedStatement(sql
.c_str()));
486 while (statement
.Step()) {
487 if (!statement
.ColumnString(1).compare(column_name
))
493 int64
Connection::GetLastInsertRowId() const {
495 DLOG(FATAL
) << "Illegal use of connection without a db";
498 return sqlite3_last_insert_rowid(db_
);
501 int Connection::GetLastChangeCount() const {
503 DLOG(FATAL
) << "Illegal use of connection without a db";
506 return sqlite3_changes(db_
);
509 int Connection::GetErrorCode() const {
512 return sqlite3_errcode(db_
);
515 int Connection::GetLastErrno() const {
520 if (SQLITE_OK
!= sqlite3_file_control(db_
, NULL
, SQLITE_LAST_ERRNO
, &err
))
526 const char* Connection::GetErrorMessage() const {
528 return "sql::Connection has no connection.";
529 return sqlite3_errmsg(db_
);
532 bool Connection::OpenInternal(const std::string
& file_name
) {
536 DLOG(FATAL
) << "sql::Connection is already open.";
540 int err
= sqlite3_open(file_name
.c_str(), &db_
);
541 if (err
!= SQLITE_OK
) {
542 // Histogram failures specific to initial open for debugging
544 UMA_HISTOGRAM_ENUMERATION("Sqlite.OpenFailure", err
& 0xff, 50);
546 OnSqliteError(err
, NULL
);
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
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();
625 void Connection::DoRollback() {
626 Statement
rollback(GetCachedStatement(SQL_FROM_HERE
, "ROLLBACK"));
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";
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
)
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();