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"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/metrics/histogram.h"
16 #include "base/metrics/sparse_histogram.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/synchronization/lock.h"
22 #include "sql/statement.h"
23 #include "third_party/sqlite/sqlite3.h"
25 #if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
26 #include "third_party/sqlite/src/ext/icu/sqliteicu.h"
31 // Spin for up to a second waiting for the lock to clear when setting
33 // TODO(shess): Better story on this. http://crbug.com/56559
34 const int kBusyTimeoutSeconds
= 1;
36 class ScopedBusyTimeout
{
38 explicit ScopedBusyTimeout(sqlite3
* db
)
41 ~ScopedBusyTimeout() {
42 sqlite3_busy_timeout(db_
, 0);
45 int SetTimeout(base::TimeDelta timeout
) {
46 DCHECK_LT(timeout
.InMilliseconds(), INT_MAX
);
47 return sqlite3_busy_timeout(db_
,
48 static_cast<int>(timeout
.InMilliseconds()));
55 // Helper to "safely" enable writable_schema. No error checking
56 // because it is reasonable to just forge ahead in case of an error.
57 // If turning it on fails, then most likely nothing will work, whereas
58 // if turning it off fails, it only matters if some code attempts to
59 // continue working with the database and tries to modify the
60 // sqlite_master table (none of our code does this).
61 class ScopedWritableSchema
{
63 explicit ScopedWritableSchema(sqlite3
* db
)
65 sqlite3_exec(db_
, "PRAGMA writable_schema=1", NULL
, NULL
, NULL
);
67 ~ScopedWritableSchema() {
68 sqlite3_exec(db_
, "PRAGMA writable_schema=0", NULL
, NULL
, NULL
);
75 // Helper to wrap the sqlite3_backup_*() step of Raze(). Return
76 // SQLite error code from running the backup step.
77 int BackupDatabase(sqlite3
* src
, sqlite3
* dst
, const char* db_name
) {
79 sqlite3_backup
* backup
= sqlite3_backup_init(dst
, db_name
, src
, db_name
);
81 // Since this call only sets things up, this indicates a gross
83 DLOG(FATAL
) << "Unable to start sqlite3_backup(): " << sqlite3_errmsg(dst
);
84 return sqlite3_errcode(dst
);
87 // -1 backs up the entire database.
88 int rc
= sqlite3_backup_step(backup
, -1);
89 int pages
= sqlite3_backup_pagecount(backup
);
90 sqlite3_backup_finish(backup
);
92 // If successful, exactly one page should have been backed up. If
93 // this breaks, check this function to make sure assumptions aren't
95 if (rc
== SQLITE_DONE
)
101 // Be very strict on attachment point. SQLite can handle a much wider
102 // character set with appropriate quoting, but Chromium code should
103 // just use clean names to start with.
104 bool ValidAttachmentPoint(const char* attachment_point
) {
105 for (size_t i
= 0; attachment_point
[i
]; ++i
) {
106 if (!((attachment_point
[i
] >= '0' && attachment_point
[i
] <= '9') ||
107 (attachment_point
[i
] >= 'a' && attachment_point
[i
] <= 'z') ||
108 (attachment_point
[i
] >= 'A' && attachment_point
[i
] <= 'Z') ||
109 attachment_point
[i
] == '_')) {
116 void RecordSqliteMemory10Min() {
117 const int64 used
= sqlite3_memory_used();
118 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.TenMinutes", used
/ 1024);
121 void RecordSqliteMemoryHour() {
122 const int64 used
= sqlite3_memory_used();
123 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneHour", used
/ 1024);
126 void RecordSqliteMemoryDay() {
127 const int64 used
= sqlite3_memory_used();
128 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneDay", used
/ 1024);
131 void RecordSqliteMemoryWeek() {
132 const int64 used
= sqlite3_memory_used();
133 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneWeek", used
/ 1024);
136 // SQLite automatically calls sqlite3_initialize() lazily, but
137 // sqlite3_initialize() uses double-checked locking and thus can have
140 // TODO(shess): Another alternative would be to have
141 // sqlite3_initialize() called as part of process bring-up. If this
142 // is changed, remove the dynamic_annotations dependency in sql.gyp.
143 base::LazyInstance
<base::Lock
>::Leaky
144 g_sqlite_init_lock
= LAZY_INSTANCE_INITIALIZER
;
145 void InitializeSqlite() {
146 base::AutoLock
lock(g_sqlite_init_lock
.Get());
147 static bool first_call
= true;
149 sqlite3_initialize();
151 // Schedule callback to record memory footprint histograms at 10m, 1h, and
152 // 1d. There may not be a message loop in tests.
153 if (base::MessageLoop::current()) {
154 base::MessageLoop::current()->PostDelayedTask(
155 FROM_HERE
, base::Bind(&RecordSqliteMemory10Min
),
156 base::TimeDelta::FromMinutes(10));
157 base::MessageLoop::current()->PostDelayedTask(
158 FROM_HERE
, base::Bind(&RecordSqliteMemoryHour
),
159 base::TimeDelta::FromHours(1));
160 base::MessageLoop::current()->PostDelayedTask(
161 FROM_HERE
, base::Bind(&RecordSqliteMemoryDay
),
162 base::TimeDelta::FromDays(1));
163 base::MessageLoop::current()->PostDelayedTask(
164 FROM_HERE
, base::Bind(&RecordSqliteMemoryWeek
),
165 base::TimeDelta::FromDays(7));
172 // Helper to get the sqlite3_file* associated with the "main" database.
173 int GetSqlite3File(sqlite3
* db
, sqlite3_file
** file
) {
175 int rc
= sqlite3_file_control(db
, NULL
, SQLITE_FCNTL_FILE_POINTER
, file
);
179 // TODO(shess): NULL in file->pMethods has been observed on android_dbg
180 // content_unittests, even though it should not be possible.
181 // http://crbug.com/329982
182 if (!*file
|| !(*file
)->pMethods
)
188 // This should match UMA_HISTOGRAM_MEDIUM_TIMES().
189 base::HistogramBase
* GetMediumTimeHistogram(const std::string
& name
) {
190 return base::Histogram::FactoryTimeGet(
192 base::TimeDelta::FromMilliseconds(10),
193 base::TimeDelta::FromMinutes(3),
195 base::HistogramBase::kUmaTargetedHistogramFlag
);
198 std::string
AsUTF8ForSQL(const base::FilePath
& path
) {
200 return base::WideToUTF8(path
.value());
201 #elif defined(OS_POSIX)
211 Connection::ErrorIgnorerCallback
* Connection::current_ignorer_cb_
= NULL
;
214 bool Connection::ShouldIgnoreSqliteError(int error
) {
215 if (!current_ignorer_cb_
)
217 return current_ignorer_cb_
->Run(error
);
221 void Connection::SetErrorIgnorer(Connection::ErrorIgnorerCallback
* cb
) {
222 CHECK(current_ignorer_cb_
== NULL
);
223 current_ignorer_cb_
= cb
;
227 void Connection::ResetErrorIgnorer() {
228 CHECK(current_ignorer_cb_
);
229 current_ignorer_cb_
= NULL
;
232 bool StatementID::operator<(const StatementID
& other
) const {
233 if (number_
!= other
.number_
)
234 return number_
< other
.number_
;
235 return strcmp(str_
, other
.str_
) < 0;
238 Connection::StatementRef::StatementRef(Connection
* connection
,
241 : connection_(connection
),
243 was_valid_(was_valid
) {
245 connection_
->StatementRefCreated(this);
248 Connection::StatementRef::~StatementRef() {
250 connection_
->StatementRefDeleted(this);
254 void Connection::StatementRef::Close(bool forced
) {
256 // Call to AssertIOAllowed() cannot go at the beginning of the function
257 // because Close() is called unconditionally from destructor to clean
258 // connection_. And if this is inactive statement this won't cause any
259 // disk access and destructor most probably will be called on thread
260 // not allowing disk access.
261 // TODO(paivanof@gmail.com): This should move to the beginning
262 // of the function. http://crbug.com/136655.
264 sqlite3_finalize(stmt_
);
267 connection_
= NULL
; // The connection may be getting deleted.
269 // Forced close is expected to happen from a statement error
270 // handler. In that case maintain the sense of |was_valid_| which
271 // previously held for this ref.
272 was_valid_
= was_valid_
&& forced
;
275 Connection::Connection()
279 exclusive_locking_(false),
280 restrict_to_user_(false),
281 transaction_nesting_(0),
282 needs_rollback_(false),
285 stats_histogram_(NULL
),
286 commit_time_histogram_(NULL
),
287 autocommit_time_histogram_(NULL
),
288 update_time_histogram_(NULL
),
289 query_time_histogram_(NULL
),
290 clock_(new TimeSource()) {
293 Connection::~Connection() {
297 void Connection::RecordEvent(Events event
, size_t count
) {
298 for (size_t i
= 0; i
< count
; ++i
) {
299 UMA_HISTOGRAM_ENUMERATION("Sqlite.Stats", event
, EVENT_MAX_VALUE
);
302 if (stats_histogram_
) {
303 for (size_t i
= 0; i
< count
; ++i
) {
304 stats_histogram_
->Add(event
);
309 void Connection::RecordCommitTime(const base::TimeDelta
& delta
) {
310 RecordUpdateTime(delta
);
311 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.CommitTime", delta
);
312 if (commit_time_histogram_
)
313 commit_time_histogram_
->AddTime(delta
);
316 void Connection::RecordAutoCommitTime(const base::TimeDelta
& delta
) {
317 RecordUpdateTime(delta
);
318 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.AutoCommitTime", delta
);
319 if (autocommit_time_histogram_
)
320 autocommit_time_histogram_
->AddTime(delta
);
323 void Connection::RecordUpdateTime(const base::TimeDelta
& delta
) {
324 RecordQueryTime(delta
);
325 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.UpdateTime", delta
);
326 if (update_time_histogram_
)
327 update_time_histogram_
->AddTime(delta
);
330 void Connection::RecordQueryTime(const base::TimeDelta
& delta
) {
331 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.QueryTime", delta
);
332 if (query_time_histogram_
)
333 query_time_histogram_
->AddTime(delta
);
336 void Connection::RecordTimeAndChanges(
337 const base::TimeDelta
& delta
, bool read_only
) {
339 RecordQueryTime(delta
);
341 const int changes
= sqlite3_changes(db_
);
342 if (sqlite3_get_autocommit(db_
)) {
343 RecordAutoCommitTime(delta
);
344 RecordEvent(EVENT_CHANGES_AUTOCOMMIT
, changes
);
346 RecordUpdateTime(delta
);
347 RecordEvent(EVENT_CHANGES
, changes
);
352 bool Connection::Open(const base::FilePath
& path
) {
353 if (!histogram_tag_
.empty()) {
355 if (base::GetFileSize(path
, &size_64
)) {
356 size_t sample
= static_cast<size_t>(size_64
/ 1024);
357 std::string full_histogram_name
= "Sqlite.SizeKB." + histogram_tag_
;
358 base::HistogramBase
* histogram
=
359 base::Histogram::FactoryGet(
360 full_histogram_name
, 1, 1000000, 50,
361 base::HistogramBase::kUmaTargetedHistogramFlag
);
363 histogram
->Add(sample
);
367 return OpenInternal(AsUTF8ForSQL(path
), RETRY_ON_POISON
);
370 bool Connection::OpenInMemory() {
372 return OpenInternal(":memory:", NO_RETRY
);
375 bool Connection::OpenTemporary() {
376 return OpenInternal("", NO_RETRY
);
379 void Connection::CloseInternal(bool forced
) {
380 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
381 // will delete the -journal file. For ChromiumOS or other more
382 // embedded systems, this is probably not appropriate, whereas on
383 // desktop it might make some sense.
385 // sqlite3_close() needs all prepared statements to be finalized.
387 // Release cached statements.
388 statement_cache_
.clear();
390 // With cached statements released, in-use statements will remain.
391 // Closing the database while statements are in use is an API
392 // violation, except for forced close (which happens from within a
393 // statement's error handler).
394 DCHECK(forced
|| open_statements_
.empty());
396 // Deactivate any outstanding statements so sqlite3_close() works.
397 for (StatementRefSet::iterator i
= open_statements_
.begin();
398 i
!= open_statements_
.end(); ++i
)
400 open_statements_
.clear();
403 // Call to AssertIOAllowed() cannot go at the beginning of the function
404 // because Close() must be called from destructor to clean
405 // statement_cache_, it won't cause any disk access and it most probably
406 // will happen on thread not allowing disk access.
407 // TODO(paivanof@gmail.com): This should move to the beginning
408 // of the function. http://crbug.com/136655.
411 int rc
= sqlite3_close(db_
);
412 if (rc
!= SQLITE_OK
) {
413 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.CloseFailure", rc
);
414 DLOG(FATAL
) << "sqlite3_close failed: " << GetErrorMessage();
420 void Connection::Close() {
421 // If the database was already closed by RazeAndClose(), then no
422 // need to close again. Clear the |poisoned_| bit so that incorrect
423 // API calls are caught.
429 CloseInternal(false);
432 void Connection::Preload() {
436 DLOG_IF(FATAL
, !poisoned_
) << "Cannot preload null db";
440 // Use local settings if provided, otherwise use documented defaults. The
441 // actual results could be fetching via PRAGMA calls.
442 const int page_size
= page_size_
? page_size_
: 1024;
443 sqlite3_int64 preload_size
= page_size
* (cache_size_
? cache_size_
: 2000);
444 if (preload_size
< 1)
447 sqlite3_file
* file
= NULL
;
448 int rc
= GetSqlite3File(db_
, &file
);
452 sqlite3_int64 file_size
= 0;
453 rc
= file
->pMethods
->xFileSize(file
, &file_size
);
457 // Don't preload more than the file contains.
458 if (preload_size
> file_size
)
459 preload_size
= file_size
;
461 scoped_ptr
<char[]> buf(new char[page_size
]);
462 for (sqlite3_int64 pos
= 0; pos
< preload_size
; pos
+= page_size
) {
463 rc
= file
->pMethods
->xRead(file
, buf
.get(), page_size
, pos
);
469 void Connection::TrimMemory(bool aggressively
) {
473 // TODO(shess): investigate using sqlite3_db_release_memory() when possible.
474 int original_cache_size
;
476 Statement
sql_get_original(GetUniqueStatement("PRAGMA cache_size"));
477 if (!sql_get_original
.Step()) {
478 DLOG(WARNING
) << "Could not get cache size " << GetErrorMessage();
481 original_cache_size
= sql_get_original
.ColumnInt(0);
483 int shrink_cache_size
= aggressively
? 1 : (original_cache_size
/ 2);
485 // Force sqlite to try to reduce page cache usage.
486 const std::string sql_shrink
=
487 base::StringPrintf("PRAGMA cache_size=%d", shrink_cache_size
);
488 if (!Execute(sql_shrink
.c_str()))
489 DLOG(WARNING
) << "Could not shrink cache size: " << GetErrorMessage();
491 // Restore cache size.
492 const std::string sql_restore
=
493 base::StringPrintf("PRAGMA cache_size=%d", original_cache_size
);
494 if (!Execute(sql_restore
.c_str()))
495 DLOG(WARNING
) << "Could not restore cache size: " << GetErrorMessage();
498 // Create an in-memory database with the existing database's page
499 // size, then backup that database over the existing database.
500 bool Connection::Raze() {
504 DLOG_IF(FATAL
, !poisoned_
) << "Cannot raze null db";
508 if (transaction_nesting_
> 0) {
509 DLOG(FATAL
) << "Cannot raze within a transaction";
513 sql::Connection null_db
;
514 if (!null_db
.OpenInMemory()) {
515 DLOG(FATAL
) << "Unable to open in-memory database.";
520 // Enforce SQLite restrictions on |page_size_|.
521 DCHECK(!(page_size_
& (page_size_
- 1)))
522 << " page_size_ " << page_size_
<< " is not a power of two.";
523 const int kSqliteMaxPageSize
= 32768; // from sqliteLimit.h
524 DCHECK_LE(page_size_
, kSqliteMaxPageSize
);
525 const std::string sql
=
526 base::StringPrintf("PRAGMA page_size=%d", page_size_
);
527 if (!null_db
.Execute(sql
.c_str()))
531 #if defined(OS_ANDROID)
532 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
533 // in-memory databases do not respect this define.
534 // TODO(shess): Figure out a way to set this without using platform
535 // specific code. AFAICT from sqlite3.c, the only way to do it
536 // would be to create an actual filesystem database, which is
538 if (!null_db
.Execute("PRAGMA auto_vacuum = 1"))
542 // The page size doesn't take effect until a database has pages, and
543 // at this point the null database has none. Changing the schema
544 // version will create the first page. This will not affect the
545 // schema version in the resulting database, as SQLite's backup
546 // implementation propagates the schema version from the original
547 // connection to the new version of the database, incremented by one
548 // so that other readers see the schema change and act accordingly.
549 if (!null_db
.Execute("PRAGMA schema_version = 1"))
552 // SQLite tracks the expected number of database pages in the first
553 // page, and if it does not match the total retrieved from a
554 // filesystem call, treats the database as corrupt. This situation
555 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
556 // used to hint to SQLite to soldier on in that case, specifically
557 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
558 // sqlite3.c lockBtree().]
559 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
560 // page_size" can be used to query such a database.
561 ScopedWritableSchema
writable_schema(db_
);
563 const char* kMain
= "main";
564 int rc
= BackupDatabase(null_db
.db_
, db_
, kMain
);
565 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase",rc
);
567 // The destination database was locked.
568 if (rc
== SQLITE_BUSY
) {
572 // SQLITE_NOTADB can happen if page 1 of db_ exists, but is not
573 // formatted correctly. SQLITE_IOERR_SHORT_READ can happen if db_
574 // isn't even big enough for one page. Either way, reach in and
575 // truncate it before trying again.
576 // TODO(shess): Maybe it would be worthwhile to just truncate from
578 if (rc
== SQLITE_NOTADB
|| rc
== SQLITE_IOERR_SHORT_READ
) {
579 sqlite3_file
* file
= NULL
;
580 rc
= GetSqlite3File(db_
, &file
);
581 if (rc
!= SQLITE_OK
) {
582 DLOG(FATAL
) << "Failure getting file handle.";
586 rc
= file
->pMethods
->xTruncate(file
, 0);
587 if (rc
!= SQLITE_OK
) {
588 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabaseTruncate",rc
);
589 DLOG(FATAL
) << "Failed to truncate file.";
593 rc
= BackupDatabase(null_db
.db_
, db_
, kMain
);
594 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase2",rc
);
596 if (rc
!= SQLITE_DONE
) {
597 DLOG(FATAL
) << "Failed retrying Raze().";
601 // The entire database should have been backed up.
602 if (rc
!= SQLITE_DONE
) {
603 // TODO(shess): Figure out which other cases can happen.
604 DLOG(FATAL
) << "Unable to copy entire null database.";
611 bool Connection::RazeWithTimout(base::TimeDelta timeout
) {
613 DLOG_IF(FATAL
, !poisoned_
) << "Cannot raze null db";
617 ScopedBusyTimeout
busy_timeout(db_
);
618 busy_timeout
.SetTimeout(timeout
);
622 bool Connection::RazeAndClose() {
624 DLOG_IF(FATAL
, !poisoned_
) << "Cannot raze null db";
628 // Raze() cannot run in a transaction.
629 RollbackAllTransactions();
631 bool result
= Raze();
635 // Mark the database so that future API calls fail appropriately,
636 // but don't DCHECK (because after calling this function they are
637 // expected to fail).
643 void Connection::Poison() {
645 DLOG_IF(FATAL
, !poisoned_
) << "Cannot poison null db";
649 RollbackAllTransactions();
652 // Mark the database so that future API calls fail appropriately,
653 // but don't DCHECK (because after calling this function they are
654 // expected to fail).
658 // TODO(shess): To the extent possible, figure out the optimal
659 // ordering for these deletes which will prevent other connections
660 // from seeing odd behavior. For instance, it may be necessary to
661 // manually lock the main database file in a SQLite-compatible fashion
662 // (to prevent other processes from opening it), then delete the
663 // journal files, then delete the main database file. Another option
664 // might be to lock the main database file and poison the header with
665 // junk to prevent other processes from opening it successfully (like
666 // Gears "SQLite poison 3" trick).
669 bool Connection::Delete(const base::FilePath
& path
) {
670 base::ThreadRestrictions::AssertIOAllowed();
672 base::FilePath
journal_path(path
.value() + FILE_PATH_LITERAL("-journal"));
673 base::FilePath
wal_path(path
.value() + FILE_PATH_LITERAL("-wal"));
675 std::string journal_str
= AsUTF8ForSQL(journal_path
);
676 std::string wal_str
= AsUTF8ForSQL(wal_path
);
677 std::string path_str
= AsUTF8ForSQL(path
);
679 // Make sure sqlite3_initialize() is called before anything else.
682 sqlite3_vfs
* vfs
= sqlite3_vfs_find(NULL
);
687 // We only work with unix, win32 and mojo filesystems. If you're trying to
688 // use this code with any other VFS, you're not in a good place.
689 CHECK(strncmp(vfs
->zName
, "unix", 4) == 0 ||
690 strncmp(vfs
->zName
, "win32", 5) == 0 ||
691 strcmp(vfs
->zName
, "mojo") == 0);
693 vfs
->xDelete(vfs
, journal_str
.c_str(), 0);
694 vfs
->xDelete(vfs
, wal_str
.c_str(), 0);
695 vfs
->xDelete(vfs
, path_str
.c_str(), 0);
697 int journal_exists
= 0;
698 vfs
->xAccess(vfs
, journal_str
.c_str(), SQLITE_ACCESS_EXISTS
,
702 vfs
->xAccess(vfs
, wal_str
.c_str(), SQLITE_ACCESS_EXISTS
,
706 vfs
->xAccess(vfs
, path_str
.c_str(), SQLITE_ACCESS_EXISTS
,
709 return !journal_exists
&& !wal_exists
&& !path_exists
;
712 bool Connection::BeginTransaction() {
713 if (needs_rollback_
) {
714 DCHECK_GT(transaction_nesting_
, 0);
716 // When we're going to rollback, fail on this begin and don't actually
717 // mark us as entering the nested transaction.
722 if (!transaction_nesting_
) {
723 needs_rollback_
= false;
725 Statement
begin(GetCachedStatement(SQL_FROM_HERE
, "BEGIN TRANSACTION"));
726 RecordOneEvent(EVENT_BEGIN
);
730 transaction_nesting_
++;
734 void Connection::RollbackTransaction() {
735 if (!transaction_nesting_
) {
736 DLOG_IF(FATAL
, !poisoned_
) << "Rolling back a nonexistent transaction";
740 transaction_nesting_
--;
742 if (transaction_nesting_
> 0) {
743 // Mark the outermost transaction as needing rollback.
744 needs_rollback_
= true;
751 bool Connection::CommitTransaction() {
752 if (!transaction_nesting_
) {
753 DLOG_IF(FATAL
, !poisoned_
) << "Rolling back a nonexistent transaction";
756 transaction_nesting_
--;
758 if (transaction_nesting_
> 0) {
759 // Mark any nested transactions as failing after we've already got one.
760 return !needs_rollback_
;
763 if (needs_rollback_
) {
768 Statement
commit(GetCachedStatement(SQL_FROM_HERE
, "COMMIT"));
770 // Collect the commit time manually, sql::Statement would register it as query
772 const base::TimeTicks before
= Now();
773 bool ret
= commit
.RunWithoutTimers();
774 const base::TimeDelta delta
= Now() - before
;
776 RecordCommitTime(delta
);
777 RecordOneEvent(EVENT_COMMIT
);
782 void Connection::RollbackAllTransactions() {
783 if (transaction_nesting_
> 0) {
784 transaction_nesting_
= 0;
789 bool Connection::AttachDatabase(const base::FilePath
& other_db_path
,
790 const char* attachment_point
) {
791 DCHECK(ValidAttachmentPoint(attachment_point
));
793 Statement
s(GetUniqueStatement("ATTACH DATABASE ? AS ?"));
795 s
.BindString16(0, other_db_path
.value());
797 s
.BindString(0, other_db_path
.value());
799 s
.BindString(1, attachment_point
);
803 bool Connection::DetachDatabase(const char* attachment_point
) {
804 DCHECK(ValidAttachmentPoint(attachment_point
));
806 Statement
s(GetUniqueStatement("DETACH DATABASE ?"));
807 s
.BindString(0, attachment_point
);
811 // TODO(shess): Consider changing this to execute exactly one statement. If a
812 // caller wishes to execute multiple statements, that should be explicit, and
813 // perhaps tucked into an explicit transaction with rollback in case of error.
814 int Connection::ExecuteAndReturnErrorCode(const char* sql
) {
817 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
822 RecordOneEvent(EVENT_EXECUTE
);
824 while ((rc
== SQLITE_OK
) && *sql
) {
825 sqlite3_stmt
*stmt
= NULL
;
826 const char *leftover_sql
;
828 const base::TimeTicks before
= Now();
829 rc
= sqlite3_prepare_v2(db_
, sql
, -1, &stmt
, &leftover_sql
);
832 // Stop if an error is encountered.
836 // This happens if |sql| originally only contained comments or whitespace.
837 // TODO(shess): Audit to see if this can become a DCHECK(). Having
838 // extraneous comments and whitespace in the SQL statements increases
839 // runtime cost and can easily be shifted out to the C++ layer.
843 // Save for use after statement is finalized.
844 const bool read_only
= !!sqlite3_stmt_readonly(stmt
);
846 RecordOneEvent(Connection::EVENT_STATEMENT_RUN
);
847 while ((rc
= sqlite3_step(stmt
)) == SQLITE_ROW
) {
848 // TODO(shess): Audit to see if this can become a DCHECK. I think PRAGMA
849 // is the only legitimate case for this.
850 RecordOneEvent(Connection::EVENT_STATEMENT_ROWS
);
853 // sqlite3_finalize() returns SQLITE_OK if the most recent sqlite3_step()
854 // returned SQLITE_DONE or SQLITE_ROW, otherwise the error code.
855 rc
= sqlite3_finalize(stmt
);
857 RecordOneEvent(Connection::EVENT_STATEMENT_SUCCESS
);
859 // sqlite3_exec() does this, presumably to avoid spinning the parser for
860 // trailing whitespace.
861 // TODO(shess): Audit to see if this can become a DCHECK.
862 while (base::IsAsciiWhitespace(*sql
)) {
866 const base::TimeDelta delta
= Now() - before
;
867 RecordTimeAndChanges(delta
, read_only
);
872 bool Connection::Execute(const char* sql
) {
874 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
878 int error
= ExecuteAndReturnErrorCode(sql
);
879 if (error
!= SQLITE_OK
)
880 error
= OnSqliteError(error
, NULL
, sql
);
882 // This needs to be a FATAL log because the error case of arriving here is
883 // that there's a malformed SQL statement. This can arise in development if
884 // a change alters the schema but not all queries adjust. This can happen
885 // in production if the schema is corrupted.
886 if (error
== SQLITE_ERROR
)
887 DLOG(FATAL
) << "SQL Error in " << sql
<< ", " << GetErrorMessage();
888 return error
== SQLITE_OK
;
891 bool Connection::ExecuteWithTimeout(const char* sql
, base::TimeDelta timeout
) {
893 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
897 ScopedBusyTimeout
busy_timeout(db_
);
898 busy_timeout
.SetTimeout(timeout
);
902 bool Connection::HasCachedStatement(const StatementID
& id
) const {
903 return statement_cache_
.find(id
) != statement_cache_
.end();
906 scoped_refptr
<Connection::StatementRef
> Connection::GetCachedStatement(
907 const StatementID
& id
,
909 CachedStatementMap::iterator i
= statement_cache_
.find(id
);
910 if (i
!= statement_cache_
.end()) {
911 // Statement is in the cache. It should still be active (we're the only
912 // one invalidating cached statements, and we'll remove it from the cache
913 // if we do that. Make sure we reset it before giving out the cached one in
914 // case it still has some stuff bound.
915 DCHECK(i
->second
->is_valid());
916 sqlite3_reset(i
->second
->stmt());
920 scoped_refptr
<StatementRef
> statement
= GetUniqueStatement(sql
);
921 if (statement
->is_valid())
922 statement_cache_
[id
] = statement
; // Only cache valid statements.
926 scoped_refptr
<Connection::StatementRef
> Connection::GetUniqueStatement(
930 // Return inactive statement.
932 return new StatementRef(NULL
, NULL
, poisoned_
);
934 sqlite3_stmt
* stmt
= NULL
;
935 int rc
= sqlite3_prepare_v2(db_
, sql
, -1, &stmt
, NULL
);
936 if (rc
!= SQLITE_OK
) {
937 // This is evidence of a syntax error in the incoming SQL.
938 if (!ShouldIgnoreSqliteError(rc
))
939 DLOG(FATAL
) << "SQL compile error " << GetErrorMessage();
941 // It could also be database corruption.
942 OnSqliteError(rc
, NULL
, sql
);
943 return new StatementRef(NULL
, NULL
, false);
945 return new StatementRef(this, stmt
, true);
948 scoped_refptr
<Connection::StatementRef
> Connection::GetUntrackedStatement(
949 const char* sql
) const {
950 // Return inactive statement.
952 return new StatementRef(NULL
, NULL
, poisoned_
);
954 sqlite3_stmt
* stmt
= NULL
;
955 int rc
= sqlite3_prepare_v2(db_
, sql
, -1, &stmt
, NULL
);
956 if (rc
!= SQLITE_OK
) {
957 // This is evidence of a syntax error in the incoming SQL.
958 if (!ShouldIgnoreSqliteError(rc
))
959 DLOG(FATAL
) << "SQL compile error " << GetErrorMessage();
960 return new StatementRef(NULL
, NULL
, false);
962 return new StatementRef(NULL
, stmt
, true);
965 std::string
Connection::GetSchema() const {
966 // The ORDER BY should not be necessary, but relying on organic
967 // order for something like this is questionable.
969 "SELECT type, name, tbl_name, sql "
970 "FROM sqlite_master ORDER BY 1, 2, 3, 4";
971 Statement
statement(GetUntrackedStatement(kSql
));
974 while (statement
.Step()) {
975 schema
+= statement
.ColumnString(0);
977 schema
+= statement
.ColumnString(1);
979 schema
+= statement
.ColumnString(2);
981 schema
+= statement
.ColumnString(3);
988 bool Connection::IsSQLValid(const char* sql
) {
991 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
995 sqlite3_stmt
* stmt
= NULL
;
996 if (sqlite3_prepare_v2(db_
, sql
, -1, &stmt
, NULL
) != SQLITE_OK
)
999 sqlite3_finalize(stmt
);
1003 bool Connection::DoesTableExist(const char* table_name
) const {
1004 return DoesTableOrIndexExist(table_name
, "table");
1007 bool Connection::DoesIndexExist(const char* index_name
) const {
1008 return DoesTableOrIndexExist(index_name
, "index");
1011 bool Connection::DoesTableOrIndexExist(
1012 const char* name
, const char* type
) const {
1014 "SELECT name FROM sqlite_master WHERE type=? AND name=? COLLATE NOCASE";
1015 Statement
statement(GetUntrackedStatement(kSql
));
1017 // This can happen if the database is corrupt and the error is being ignored
1018 // for testing purposes.
1019 if (!statement
.is_valid())
1022 statement
.BindString(0, type
);
1023 statement
.BindString(1, name
);
1025 return statement
.Step(); // Table exists if any row was returned.
1028 bool Connection::DoesColumnExist(const char* table_name
,
1029 const char* column_name
) const {
1030 std::string
sql("PRAGMA TABLE_INFO(");
1031 sql
.append(table_name
);
1034 Statement
statement(GetUntrackedStatement(sql
.c_str()));
1036 // This can happen if the database is corrupt and the error is being ignored
1037 // for testing purposes.
1038 if (!statement
.is_valid())
1041 while (statement
.Step()) {
1042 if (base::EqualsCaseInsensitiveASCII(statement
.ColumnString(1),
1049 int64_t Connection::GetLastInsertRowId() const {
1051 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
1054 return sqlite3_last_insert_rowid(db_
);
1057 int Connection::GetLastChangeCount() const {
1059 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
1062 return sqlite3_changes(db_
);
1065 int Connection::GetErrorCode() const {
1067 return SQLITE_ERROR
;
1068 return sqlite3_errcode(db_
);
1071 int Connection::GetLastErrno() const {
1076 if (SQLITE_OK
!= sqlite3_file_control(db_
, NULL
, SQLITE_LAST_ERRNO
, &err
))
1082 const char* Connection::GetErrorMessage() const {
1084 return "sql::Connection has no connection.";
1085 return sqlite3_errmsg(db_
);
1088 bool Connection::OpenInternal(const std::string
& file_name
,
1089 Connection::Retry retry_flag
) {
1093 DLOG(FATAL
) << "sql::Connection is already open.";
1097 // Make sure sqlite3_initialize() is called before anything else.
1100 // Setup the stats histograms immediately rather than allocating lazily.
1101 // Connections which won't exercise all of these probably shouldn't exist.
1102 if (!histogram_tag_
.empty()) {
1104 base::LinearHistogram::FactoryGet(
1105 "Sqlite.Stats." + histogram_tag_
,
1106 1, EVENT_MAX_VALUE
, EVENT_MAX_VALUE
+ 1,
1107 base::HistogramBase::kUmaTargetedHistogramFlag
);
1109 // The timer setup matches UMA_HISTOGRAM_MEDIUM_TIMES(). 3 minutes is an
1110 // unreasonable time for any single operation, so there is not much value to
1111 // knowing if it was 3 minutes or 5 minutes. In reality at that point
1112 // things are entirely busted.
1113 commit_time_histogram_
=
1114 GetMediumTimeHistogram("Sqlite.CommitTime." + histogram_tag_
);
1116 autocommit_time_histogram_
=
1117 GetMediumTimeHistogram("Sqlite.AutoCommitTime." + histogram_tag_
);
1119 update_time_histogram_
=
1120 GetMediumTimeHistogram("Sqlite.UpdateTime." + histogram_tag_
);
1122 query_time_histogram_
=
1123 GetMediumTimeHistogram("Sqlite.QueryTime." + histogram_tag_
);
1126 // If |poisoned_| is set, it means an error handler called
1127 // RazeAndClose(). Until regular Close() is called, the caller
1128 // should be treating the database as open, but is_open() currently
1129 // only considers the sqlite3 handle's state.
1130 // TODO(shess): Revise is_open() to consider poisoned_, and review
1131 // to see if any non-testing code even depends on it.
1132 DLOG_IF(FATAL
, poisoned_
) << "sql::Connection is already open.";
1135 int err
= sqlite3_open(file_name
.c_str(), &db_
);
1136 if (err
!= SQLITE_OK
) {
1137 // Extended error codes cannot be enabled until a handle is
1138 // available, fetch manually.
1139 err
= sqlite3_extended_errcode(db_
);
1141 // Histogram failures specific to initial open for debugging
1143 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenFailure", err
);
1145 OnSqliteError(err
, NULL
, "-- sqlite3_open()");
1146 bool was_poisoned
= poisoned_
;
1149 if (was_poisoned
&& retry_flag
== RETRY_ON_POISON
)
1150 return OpenInternal(file_name
, NO_RETRY
);
1154 // TODO(shess): OS_WIN support?
1155 #if defined(OS_POSIX)
1156 if (restrict_to_user_
) {
1157 DCHECK_NE(file_name
, std::string(":memory"));
1158 base::FilePath
file_path(file_name
);
1160 // TODO(shess): Arguably, failure to retrieve and change
1161 // permissions should be fatal if the file exists.
1162 if (base::GetPosixFilePermissions(file_path
, &mode
)) {
1163 mode
&= base::FILE_PERMISSION_USER_MASK
;
1164 base::SetPosixFilePermissions(file_path
, mode
);
1166 // SQLite sets the permissions on these files from the main
1167 // database on create. Set them here in case they already exist
1168 // at this point. Failure to set these permissions should not
1169 // be fatal unless the file doesn't exist.
1170 base::FilePath
journal_path(file_name
+ FILE_PATH_LITERAL("-journal"));
1171 base::FilePath
wal_path(file_name
+ FILE_PATH_LITERAL("-wal"));
1172 base::SetPosixFilePermissions(journal_path
, mode
);
1173 base::SetPosixFilePermissions(wal_path
, mode
);
1176 #endif // defined(OS_POSIX)
1178 // SQLite uses a lookaside buffer to improve performance of small mallocs.
1179 // Chromium already depends on small mallocs being efficient, so we disable
1180 // this to avoid the extra memory overhead.
1181 // This must be called immediatly after opening the database before any SQL
1182 // statements are run.
1183 sqlite3_db_config(db_
, SQLITE_DBCONFIG_LOOKASIDE
, NULL
, 0, 0);
1185 // Enable extended result codes to provide more color on I/O errors.
1186 // Not having extended result codes is not a fatal problem, as
1187 // Chromium code does not attempt to handle I/O errors anyhow. The
1188 // current implementation always returns SQLITE_OK, the DCHECK is to
1189 // quickly notify someone if SQLite changes.
1190 err
= sqlite3_extended_result_codes(db_
, 1);
1191 DCHECK_EQ(err
, SQLITE_OK
) << "Could not enable extended result codes";
1193 // sqlite3_open() does not actually read the database file (unless a
1194 // hot journal is found). Successfully executing this pragma on an
1195 // existing database requires a valid header on page 1.
1196 // TODO(shess): For now, just probing to see what the lay of the
1197 // land is. If it's mostly SQLITE_NOTADB, then the database should
1199 err
= ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
1200 if (err
!= SQLITE_OK
)
1201 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenProbeFailure", err
);
1203 #if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
1204 // The version of SQLite shipped with iOS doesn't enable ICU, which includes
1205 // REGEXP support. Add it in dynamically.
1206 err
= sqlite3IcuInit(db_
);
1207 DCHECK_EQ(err
, SQLITE_OK
) << "Could not enable ICU support";
1208 #endif // OS_IOS && USE_SYSTEM_SQLITE
1210 // If indicated, lock up the database before doing anything else, so
1211 // that the following code doesn't have to deal with locking.
1212 // TODO(shess): This code is brittle. Find the cases where code
1213 // doesn't request |exclusive_locking_| and audit that it does the
1214 // right thing with SQLITE_BUSY, and that it doesn't make
1215 // assumptions about who might change things in the database.
1216 // http://crbug.com/56559
1217 if (exclusive_locking_
) {
1218 // TODO(shess): This should probably be a failure. Code which
1219 // requests exclusive locking but doesn't get it is almost certain
1220 // to be ill-tested.
1221 ignore_result(Execute("PRAGMA locking_mode=EXCLUSIVE"));
1224 // http://www.sqlite.org/pragma.html#pragma_journal_mode
1225 // DELETE (default) - delete -journal file to commit.
1226 // TRUNCATE - truncate -journal file to commit.
1227 // PERSIST - zero out header of -journal file to commit.
1228 // TRUNCATE should be faster than DELETE because it won't need directory
1229 // changes for each transaction. PERSIST may break the spirit of using
1231 ignore_result(Execute("PRAGMA journal_mode = TRUNCATE"));
1233 const base::TimeDelta kBusyTimeout
=
1234 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds
);
1236 if (page_size_
!= 0) {
1237 // Enforce SQLite restrictions on |page_size_|.
1238 DCHECK(!(page_size_
& (page_size_
- 1)))
1239 << " page_size_ " << page_size_
<< " is not a power of two.";
1240 const int kSqliteMaxPageSize
= 32768; // from sqliteLimit.h
1241 DCHECK_LE(page_size_
, kSqliteMaxPageSize
);
1242 const std::string sql
=
1243 base::StringPrintf("PRAGMA page_size=%d", page_size_
);
1244 ignore_result(ExecuteWithTimeout(sql
.c_str(), kBusyTimeout
));
1247 if (cache_size_
!= 0) {
1248 const std::string sql
=
1249 base::StringPrintf("PRAGMA cache_size=%d", cache_size_
);
1250 ignore_result(ExecuteWithTimeout(sql
.c_str(), kBusyTimeout
));
1253 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout
)) {
1254 bool was_poisoned
= poisoned_
;
1256 if (was_poisoned
&& retry_flag
== RETRY_ON_POISON
)
1257 return OpenInternal(file_name
, NO_RETRY
);
1264 void Connection::DoRollback() {
1265 Statement
rollback(GetCachedStatement(SQL_FROM_HERE
, "ROLLBACK"));
1267 // Collect the rollback time manually, sql::Statement would register it as
1269 const base::TimeTicks before
= Now();
1270 rollback
.RunWithoutTimers();
1271 const base::TimeDelta delta
= Now() - before
;
1273 RecordUpdateTime(delta
);
1274 RecordOneEvent(EVENT_ROLLBACK
);
1276 needs_rollback_
= false;
1279 void Connection::StatementRefCreated(StatementRef
* ref
) {
1280 DCHECK(open_statements_
.find(ref
) == open_statements_
.end());
1281 open_statements_
.insert(ref
);
1284 void Connection::StatementRefDeleted(StatementRef
* ref
) {
1285 StatementRefSet::iterator i
= open_statements_
.find(ref
);
1286 if (i
== open_statements_
.end())
1287 DLOG(FATAL
) << "Could not find statement";
1289 open_statements_
.erase(i
);
1292 void Connection::set_histogram_tag(const std::string
& tag
) {
1294 histogram_tag_
= tag
;
1297 void Connection::AddTaggedHistogram(const std::string
& name
,
1298 size_t sample
) const {
1299 if (histogram_tag_
.empty())
1302 // TODO(shess): The histogram macros create a bit of static storage
1303 // for caching the histogram object. This code shouldn't execute
1304 // often enough for such caching to be crucial. If it becomes an
1305 // issue, the object could be cached alongside histogram_prefix_.
1306 std::string full_histogram_name
= name
+ "." + histogram_tag_
;
1307 base::HistogramBase
* histogram
=
1308 base::SparseHistogram::FactoryGet(
1309 full_histogram_name
,
1310 base::HistogramBase::kUmaTargetedHistogramFlag
);
1312 histogram
->Add(sample
);
1315 int Connection::OnSqliteError(int err
, sql::Statement
*stmt
, const char* sql
) {
1316 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err
);
1317 AddTaggedHistogram("Sqlite.Error", err
);
1319 // Always log the error.
1321 sql
= stmt
->GetSQLStatement();
1324 LOG(ERROR
) << histogram_tag_
<< " sqlite error " << err
1325 << ", errno " << GetLastErrno()
1326 << ": " << GetErrorMessage()
1327 << ", sql: " << sql
;
1329 if (!error_callback_
.is_null()) {
1330 // Fire from a copy of the callback in case of reentry into
1331 // re/set_error_callback().
1332 // TODO(shess): <http://crbug.com/254584>
1333 ErrorCallback(error_callback_
).Run(err
, stmt
);
1337 // The default handling is to assert on debug and to ignore on release.
1338 if (!ShouldIgnoreSqliteError(err
))
1339 DLOG(FATAL
) << GetErrorMessage();
1343 bool Connection::FullIntegrityCheck(std::vector
<std::string
>* messages
) {
1344 return IntegrityCheckHelper("PRAGMA integrity_check", messages
);
1347 bool Connection::QuickIntegrityCheck() {
1348 std::vector
<std::string
> messages
;
1349 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages
))
1351 return messages
.size() == 1 && messages
[0] == "ok";
1354 // TODO(shess): Allow specifying maximum results (default 100 lines).
1355 bool Connection::IntegrityCheckHelper(
1356 const char* pragma_sql
,
1357 std::vector
<std::string
>* messages
) {
1360 // This has the side effect of setting SQLITE_RecoveryMode, which
1361 // allows SQLite to process through certain cases of corruption.
1362 // Failing to set this pragma probably means that the database is
1364 const char kWritableSchema
[] = "PRAGMA writable_schema = ON";
1365 if (!Execute(kWritableSchema
))
1370 sql::Statement
stmt(GetUniqueStatement(pragma_sql
));
1372 // The pragma appears to return all results (up to 100 by default)
1373 // as a single string. This doesn't appear to be an API contract,
1374 // it could return separate lines, so loop _and_ split.
1375 while (stmt
.Step()) {
1376 std::string
result(stmt
.ColumnString(0));
1377 *messages
= base::SplitString(result
, "\n", base::TRIM_WHITESPACE
,
1378 base::SPLIT_WANT_ALL
);
1380 ret
= stmt
.Succeeded();
1383 // Best effort to put things back as they were before.
1384 const char kNoWritableSchema
[] = "PRAGMA writable_schema = OFF";
1385 ignore_result(Execute(kNoWritableSchema
));
1390 base::TimeTicks
TimeSource::Now() {
1391 return base::TimeTicks::Now();