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 sqlite3_vfs
* vfs
= sqlite3_vfs_find(NULL
);
684 // We only work with unix, win32 and mojo filesystems. If you're trying to
685 // use this code with any other VFS, you're not in a good place.
686 CHECK(strncmp(vfs
->zName
, "unix", 4) == 0 ||
687 strncmp(vfs
->zName
, "win32", 5) == 0 ||
688 strcmp(vfs
->zName
, "mojo") == 0);
690 vfs
->xDelete(vfs
, journal_str
.c_str(), 0);
691 vfs
->xDelete(vfs
, wal_str
.c_str(), 0);
692 vfs
->xDelete(vfs
, path_str
.c_str(), 0);
694 int journal_exists
= 0;
695 vfs
->xAccess(vfs
, journal_str
.c_str(), SQLITE_ACCESS_EXISTS
,
699 vfs
->xAccess(vfs
, wal_str
.c_str(), SQLITE_ACCESS_EXISTS
,
703 vfs
->xAccess(vfs
, path_str
.c_str(), SQLITE_ACCESS_EXISTS
,
706 return !journal_exists
&& !wal_exists
&& !path_exists
;
709 bool Connection::BeginTransaction() {
710 if (needs_rollback_
) {
711 DCHECK_GT(transaction_nesting_
, 0);
713 // When we're going to rollback, fail on this begin and don't actually
714 // mark us as entering the nested transaction.
719 if (!transaction_nesting_
) {
720 needs_rollback_
= false;
722 Statement
begin(GetCachedStatement(SQL_FROM_HERE
, "BEGIN TRANSACTION"));
723 RecordOneEvent(EVENT_BEGIN
);
727 transaction_nesting_
++;
731 void Connection::RollbackTransaction() {
732 if (!transaction_nesting_
) {
733 DLOG_IF(FATAL
, !poisoned_
) << "Rolling back a nonexistent transaction";
737 transaction_nesting_
--;
739 if (transaction_nesting_
> 0) {
740 // Mark the outermost transaction as needing rollback.
741 needs_rollback_
= true;
748 bool Connection::CommitTransaction() {
749 if (!transaction_nesting_
) {
750 DLOG_IF(FATAL
, !poisoned_
) << "Rolling back a nonexistent transaction";
753 transaction_nesting_
--;
755 if (transaction_nesting_
> 0) {
756 // Mark any nested transactions as failing after we've already got one.
757 return !needs_rollback_
;
760 if (needs_rollback_
) {
765 Statement
commit(GetCachedStatement(SQL_FROM_HERE
, "COMMIT"));
767 // Collect the commit time manually, sql::Statement would register it as query
769 const base::TimeTicks before
= Now();
770 bool ret
= commit
.RunWithoutTimers();
771 const base::TimeDelta delta
= Now() - before
;
773 RecordCommitTime(delta
);
774 RecordOneEvent(EVENT_COMMIT
);
779 void Connection::RollbackAllTransactions() {
780 if (transaction_nesting_
> 0) {
781 transaction_nesting_
= 0;
786 bool Connection::AttachDatabase(const base::FilePath
& other_db_path
,
787 const char* attachment_point
) {
788 DCHECK(ValidAttachmentPoint(attachment_point
));
790 Statement
s(GetUniqueStatement("ATTACH DATABASE ? AS ?"));
792 s
.BindString16(0, other_db_path
.value());
794 s
.BindString(0, other_db_path
.value());
796 s
.BindString(1, attachment_point
);
800 bool Connection::DetachDatabase(const char* attachment_point
) {
801 DCHECK(ValidAttachmentPoint(attachment_point
));
803 Statement
s(GetUniqueStatement("DETACH DATABASE ?"));
804 s
.BindString(0, attachment_point
);
808 // TODO(shess): Consider changing this to execute exactly one statement. If a
809 // caller wishes to execute multiple statements, that should be explicit, and
810 // perhaps tucked into an explicit transaction with rollback in case of error.
811 int Connection::ExecuteAndReturnErrorCode(const char* sql
) {
814 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
819 RecordOneEvent(EVENT_EXECUTE
);
821 while ((rc
== SQLITE_OK
) && *sql
) {
822 sqlite3_stmt
*stmt
= NULL
;
823 const char *leftover_sql
;
825 const base::TimeTicks before
= Now();
826 rc
= sqlite3_prepare_v2(db_
, sql
, -1, &stmt
, &leftover_sql
);
829 // Stop if an error is encountered.
833 // This happens if |sql| originally only contained comments or whitespace.
834 // TODO(shess): Audit to see if this can become a DCHECK(). Having
835 // extraneous comments and whitespace in the SQL statements increases
836 // runtime cost and can easily be shifted out to the C++ layer.
840 // Save for use after statement is finalized.
841 const bool read_only
= !!sqlite3_stmt_readonly(stmt
);
843 RecordOneEvent(Connection::EVENT_STATEMENT_RUN
);
844 while ((rc
= sqlite3_step(stmt
)) == SQLITE_ROW
) {
845 // TODO(shess): Audit to see if this can become a DCHECK. I think PRAGMA
846 // is the only legitimate case for this.
847 RecordOneEvent(Connection::EVENT_STATEMENT_ROWS
);
850 // sqlite3_finalize() returns SQLITE_OK if the most recent sqlite3_step()
851 // returned SQLITE_DONE or SQLITE_ROW, otherwise the error code.
852 rc
= sqlite3_finalize(stmt
);
854 RecordOneEvent(Connection::EVENT_STATEMENT_SUCCESS
);
856 // sqlite3_exec() does this, presumably to avoid spinning the parser for
857 // trailing whitespace.
858 // TODO(shess): Audit to see if this can become a DCHECK.
859 while (base::IsAsciiWhitespace(*sql
)) {
863 const base::TimeDelta delta
= Now() - before
;
864 RecordTimeAndChanges(delta
, read_only
);
869 bool Connection::Execute(const char* sql
) {
871 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
875 int error
= ExecuteAndReturnErrorCode(sql
);
876 if (error
!= SQLITE_OK
)
877 error
= OnSqliteError(error
, NULL
, sql
);
879 // This needs to be a FATAL log because the error case of arriving here is
880 // that there's a malformed SQL statement. This can arise in development if
881 // a change alters the schema but not all queries adjust. This can happen
882 // in production if the schema is corrupted.
883 if (error
== SQLITE_ERROR
)
884 DLOG(FATAL
) << "SQL Error in " << sql
<< ", " << GetErrorMessage();
885 return error
== SQLITE_OK
;
888 bool Connection::ExecuteWithTimeout(const char* sql
, base::TimeDelta timeout
) {
890 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
894 ScopedBusyTimeout
busy_timeout(db_
);
895 busy_timeout
.SetTimeout(timeout
);
899 bool Connection::HasCachedStatement(const StatementID
& id
) const {
900 return statement_cache_
.find(id
) != statement_cache_
.end();
903 scoped_refptr
<Connection::StatementRef
> Connection::GetCachedStatement(
904 const StatementID
& id
,
906 CachedStatementMap::iterator i
= statement_cache_
.find(id
);
907 if (i
!= statement_cache_
.end()) {
908 // Statement is in the cache. It should still be active (we're the only
909 // one invalidating cached statements, and we'll remove it from the cache
910 // if we do that. Make sure we reset it before giving out the cached one in
911 // case it still has some stuff bound.
912 DCHECK(i
->second
->is_valid());
913 sqlite3_reset(i
->second
->stmt());
917 scoped_refptr
<StatementRef
> statement
= GetUniqueStatement(sql
);
918 if (statement
->is_valid())
919 statement_cache_
[id
] = statement
; // Only cache valid statements.
923 scoped_refptr
<Connection::StatementRef
> Connection::GetUniqueStatement(
927 // Return inactive statement.
929 return new StatementRef(NULL
, NULL
, poisoned_
);
931 sqlite3_stmt
* stmt
= NULL
;
932 int rc
= sqlite3_prepare_v2(db_
, sql
, -1, &stmt
, NULL
);
933 if (rc
!= SQLITE_OK
) {
934 // This is evidence of a syntax error in the incoming SQL.
935 if (!ShouldIgnoreSqliteError(rc
))
936 DLOG(FATAL
) << "SQL compile error " << GetErrorMessage();
938 // It could also be database corruption.
939 OnSqliteError(rc
, NULL
, sql
);
940 return new StatementRef(NULL
, NULL
, false);
942 return new StatementRef(this, stmt
, true);
945 scoped_refptr
<Connection::StatementRef
> Connection::GetUntrackedStatement(
946 const char* sql
) const {
947 // Return inactive statement.
949 return new StatementRef(NULL
, NULL
, poisoned_
);
951 sqlite3_stmt
* stmt
= NULL
;
952 int rc
= sqlite3_prepare_v2(db_
, sql
, -1, &stmt
, NULL
);
953 if (rc
!= SQLITE_OK
) {
954 // This is evidence of a syntax error in the incoming SQL.
955 if (!ShouldIgnoreSqliteError(rc
))
956 DLOG(FATAL
) << "SQL compile error " << GetErrorMessage();
957 return new StatementRef(NULL
, NULL
, false);
959 return new StatementRef(NULL
, stmt
, true);
962 std::string
Connection::GetSchema() const {
963 // The ORDER BY should not be necessary, but relying on organic
964 // order for something like this is questionable.
966 "SELECT type, name, tbl_name, sql "
967 "FROM sqlite_master ORDER BY 1, 2, 3, 4";
968 Statement
statement(GetUntrackedStatement(kSql
));
971 while (statement
.Step()) {
972 schema
+= statement
.ColumnString(0);
974 schema
+= statement
.ColumnString(1);
976 schema
+= statement
.ColumnString(2);
978 schema
+= statement
.ColumnString(3);
985 bool Connection::IsSQLValid(const char* sql
) {
988 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
992 sqlite3_stmt
* stmt
= NULL
;
993 if (sqlite3_prepare_v2(db_
, sql
, -1, &stmt
, NULL
) != SQLITE_OK
)
996 sqlite3_finalize(stmt
);
1000 bool Connection::DoesTableExist(const char* table_name
) const {
1001 return DoesTableOrIndexExist(table_name
, "table");
1004 bool Connection::DoesIndexExist(const char* index_name
) const {
1005 return DoesTableOrIndexExist(index_name
, "index");
1008 bool Connection::DoesTableOrIndexExist(
1009 const char* name
, const char* type
) const {
1011 "SELECT name FROM sqlite_master WHERE type=? AND name=? COLLATE NOCASE";
1012 Statement
statement(GetUntrackedStatement(kSql
));
1014 // This can happen if the database is corrupt and the error is being ignored
1015 // for testing purposes.
1016 if (!statement
.is_valid())
1019 statement
.BindString(0, type
);
1020 statement
.BindString(1, name
);
1022 return statement
.Step(); // Table exists if any row was returned.
1025 bool Connection::DoesColumnExist(const char* table_name
,
1026 const char* column_name
) const {
1027 std::string
sql("PRAGMA TABLE_INFO(");
1028 sql
.append(table_name
);
1031 Statement
statement(GetUntrackedStatement(sql
.c_str()));
1033 // This can happen if the database is corrupt and the error is being ignored
1034 // for testing purposes.
1035 if (!statement
.is_valid())
1038 while (statement
.Step()) {
1039 if (base::EqualsCaseInsensitiveASCII(statement
.ColumnString(1),
1046 int64_t Connection::GetLastInsertRowId() const {
1048 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
1051 return sqlite3_last_insert_rowid(db_
);
1054 int Connection::GetLastChangeCount() const {
1056 DLOG_IF(FATAL
, !poisoned_
) << "Illegal use of connection without a db";
1059 return sqlite3_changes(db_
);
1062 int Connection::GetErrorCode() const {
1064 return SQLITE_ERROR
;
1065 return sqlite3_errcode(db_
);
1068 int Connection::GetLastErrno() const {
1073 if (SQLITE_OK
!= sqlite3_file_control(db_
, NULL
, SQLITE_LAST_ERRNO
, &err
))
1079 const char* Connection::GetErrorMessage() const {
1081 return "sql::Connection has no connection.";
1082 return sqlite3_errmsg(db_
);
1085 bool Connection::OpenInternal(const std::string
& file_name
,
1086 Connection::Retry retry_flag
) {
1090 DLOG(FATAL
) << "sql::Connection is already open.";
1094 // Make sure sqlite3_initialize() is called before anything else.
1097 // Setup the stats histograms immediately rather than allocating lazily.
1098 // Connections which won't exercise all of these probably shouldn't exist.
1099 if (!histogram_tag_
.empty()) {
1101 base::LinearHistogram::FactoryGet(
1102 "Sqlite.Stats." + histogram_tag_
,
1103 1, EVENT_MAX_VALUE
, EVENT_MAX_VALUE
+ 1,
1104 base::HistogramBase::kUmaTargetedHistogramFlag
);
1106 // The timer setup matches UMA_HISTOGRAM_MEDIUM_TIMES(). 3 minutes is an
1107 // unreasonable time for any single operation, so there is not much value to
1108 // knowing if it was 3 minutes or 5 minutes. In reality at that point
1109 // things are entirely busted.
1110 commit_time_histogram_
=
1111 GetMediumTimeHistogram("Sqlite.CommitTime." + histogram_tag_
);
1113 autocommit_time_histogram_
=
1114 GetMediumTimeHistogram("Sqlite.AutoCommitTime." + histogram_tag_
);
1116 update_time_histogram_
=
1117 GetMediumTimeHistogram("Sqlite.UpdateTime." + histogram_tag_
);
1119 query_time_histogram_
=
1120 GetMediumTimeHistogram("Sqlite.QueryTime." + histogram_tag_
);
1123 // If |poisoned_| is set, it means an error handler called
1124 // RazeAndClose(). Until regular Close() is called, the caller
1125 // should be treating the database as open, but is_open() currently
1126 // only considers the sqlite3 handle's state.
1127 // TODO(shess): Revise is_open() to consider poisoned_, and review
1128 // to see if any non-testing code even depends on it.
1129 DLOG_IF(FATAL
, poisoned_
) << "sql::Connection is already open.";
1132 int err
= sqlite3_open(file_name
.c_str(), &db_
);
1133 if (err
!= SQLITE_OK
) {
1134 // Extended error codes cannot be enabled until a handle is
1135 // available, fetch manually.
1136 err
= sqlite3_extended_errcode(db_
);
1138 // Histogram failures specific to initial open for debugging
1140 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenFailure", err
);
1142 OnSqliteError(err
, NULL
, "-- sqlite3_open()");
1143 bool was_poisoned
= poisoned_
;
1146 if (was_poisoned
&& retry_flag
== RETRY_ON_POISON
)
1147 return OpenInternal(file_name
, NO_RETRY
);
1151 // TODO(shess): OS_WIN support?
1152 #if defined(OS_POSIX)
1153 if (restrict_to_user_
) {
1154 DCHECK_NE(file_name
, std::string(":memory"));
1155 base::FilePath
file_path(file_name
);
1157 // TODO(shess): Arguably, failure to retrieve and change
1158 // permissions should be fatal if the file exists.
1159 if (base::GetPosixFilePermissions(file_path
, &mode
)) {
1160 mode
&= base::FILE_PERMISSION_USER_MASK
;
1161 base::SetPosixFilePermissions(file_path
, mode
);
1163 // SQLite sets the permissions on these files from the main
1164 // database on create. Set them here in case they already exist
1165 // at this point. Failure to set these permissions should not
1166 // be fatal unless the file doesn't exist.
1167 base::FilePath
journal_path(file_name
+ FILE_PATH_LITERAL("-journal"));
1168 base::FilePath
wal_path(file_name
+ FILE_PATH_LITERAL("-wal"));
1169 base::SetPosixFilePermissions(journal_path
, mode
);
1170 base::SetPosixFilePermissions(wal_path
, mode
);
1173 #endif // defined(OS_POSIX)
1175 // SQLite uses a lookaside buffer to improve performance of small mallocs.
1176 // Chromium already depends on small mallocs being efficient, so we disable
1177 // this to avoid the extra memory overhead.
1178 // This must be called immediatly after opening the database before any SQL
1179 // statements are run.
1180 sqlite3_db_config(db_
, SQLITE_DBCONFIG_LOOKASIDE
, NULL
, 0, 0);
1182 // Enable extended result codes to provide more color on I/O errors.
1183 // Not having extended result codes is not a fatal problem, as
1184 // Chromium code does not attempt to handle I/O errors anyhow. The
1185 // current implementation always returns SQLITE_OK, the DCHECK is to
1186 // quickly notify someone if SQLite changes.
1187 err
= sqlite3_extended_result_codes(db_
, 1);
1188 DCHECK_EQ(err
, SQLITE_OK
) << "Could not enable extended result codes";
1190 // sqlite3_open() does not actually read the database file (unless a
1191 // hot journal is found). Successfully executing this pragma on an
1192 // existing database requires a valid header on page 1.
1193 // TODO(shess): For now, just probing to see what the lay of the
1194 // land is. If it's mostly SQLITE_NOTADB, then the database should
1196 err
= ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
1197 if (err
!= SQLITE_OK
)
1198 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenProbeFailure", err
);
1200 #if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
1201 // The version of SQLite shipped with iOS doesn't enable ICU, which includes
1202 // REGEXP support. Add it in dynamically.
1203 err
= sqlite3IcuInit(db_
);
1204 DCHECK_EQ(err
, SQLITE_OK
) << "Could not enable ICU support";
1205 #endif // OS_IOS && USE_SYSTEM_SQLITE
1207 // If indicated, lock up the database before doing anything else, so
1208 // that the following code doesn't have to deal with locking.
1209 // TODO(shess): This code is brittle. Find the cases where code
1210 // doesn't request |exclusive_locking_| and audit that it does the
1211 // right thing with SQLITE_BUSY, and that it doesn't make
1212 // assumptions about who might change things in the database.
1213 // http://crbug.com/56559
1214 if (exclusive_locking_
) {
1215 // TODO(shess): This should probably be a failure. Code which
1216 // requests exclusive locking but doesn't get it is almost certain
1217 // to be ill-tested.
1218 ignore_result(Execute("PRAGMA locking_mode=EXCLUSIVE"));
1221 // http://www.sqlite.org/pragma.html#pragma_journal_mode
1222 // DELETE (default) - delete -journal file to commit.
1223 // TRUNCATE - truncate -journal file to commit.
1224 // PERSIST - zero out header of -journal file to commit.
1225 // TRUNCATE should be faster than DELETE because it won't need directory
1226 // changes for each transaction. PERSIST may break the spirit of using
1228 ignore_result(Execute("PRAGMA journal_mode = TRUNCATE"));
1230 const base::TimeDelta kBusyTimeout
=
1231 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds
);
1233 if (page_size_
!= 0) {
1234 // Enforce SQLite restrictions on |page_size_|.
1235 DCHECK(!(page_size_
& (page_size_
- 1)))
1236 << " page_size_ " << page_size_
<< " is not a power of two.";
1237 const int kSqliteMaxPageSize
= 32768; // from sqliteLimit.h
1238 DCHECK_LE(page_size_
, kSqliteMaxPageSize
);
1239 const std::string sql
=
1240 base::StringPrintf("PRAGMA page_size=%d", page_size_
);
1241 ignore_result(ExecuteWithTimeout(sql
.c_str(), kBusyTimeout
));
1244 if (cache_size_
!= 0) {
1245 const std::string sql
=
1246 base::StringPrintf("PRAGMA cache_size=%d", cache_size_
);
1247 ignore_result(ExecuteWithTimeout(sql
.c_str(), kBusyTimeout
));
1250 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout
)) {
1251 bool was_poisoned
= poisoned_
;
1253 if (was_poisoned
&& retry_flag
== RETRY_ON_POISON
)
1254 return OpenInternal(file_name
, NO_RETRY
);
1261 void Connection::DoRollback() {
1262 Statement
rollback(GetCachedStatement(SQL_FROM_HERE
, "ROLLBACK"));
1264 // Collect the rollback time manually, sql::Statement would register it as
1266 const base::TimeTicks before
= Now();
1267 rollback
.RunWithoutTimers();
1268 const base::TimeDelta delta
= Now() - before
;
1270 RecordUpdateTime(delta
);
1271 RecordOneEvent(EVENT_ROLLBACK
);
1273 needs_rollback_
= false;
1276 void Connection::StatementRefCreated(StatementRef
* ref
) {
1277 DCHECK(open_statements_
.find(ref
) == open_statements_
.end());
1278 open_statements_
.insert(ref
);
1281 void Connection::StatementRefDeleted(StatementRef
* ref
) {
1282 StatementRefSet::iterator i
= open_statements_
.find(ref
);
1283 if (i
== open_statements_
.end())
1284 DLOG(FATAL
) << "Could not find statement";
1286 open_statements_
.erase(i
);
1289 void Connection::set_histogram_tag(const std::string
& tag
) {
1291 histogram_tag_
= tag
;
1294 void Connection::AddTaggedHistogram(const std::string
& name
,
1295 size_t sample
) const {
1296 if (histogram_tag_
.empty())
1299 // TODO(shess): The histogram macros create a bit of static storage
1300 // for caching the histogram object. This code shouldn't execute
1301 // often enough for such caching to be crucial. If it becomes an
1302 // issue, the object could be cached alongside histogram_prefix_.
1303 std::string full_histogram_name
= name
+ "." + histogram_tag_
;
1304 base::HistogramBase
* histogram
=
1305 base::SparseHistogram::FactoryGet(
1306 full_histogram_name
,
1307 base::HistogramBase::kUmaTargetedHistogramFlag
);
1309 histogram
->Add(sample
);
1312 int Connection::OnSqliteError(int err
, sql::Statement
*stmt
, const char* sql
) {
1313 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err
);
1314 AddTaggedHistogram("Sqlite.Error", err
);
1316 // Always log the error.
1318 sql
= stmt
->GetSQLStatement();
1321 LOG(ERROR
) << histogram_tag_
<< " sqlite error " << err
1322 << ", errno " << GetLastErrno()
1323 << ": " << GetErrorMessage()
1324 << ", sql: " << sql
;
1326 if (!error_callback_
.is_null()) {
1327 // Fire from a copy of the callback in case of reentry into
1328 // re/set_error_callback().
1329 // TODO(shess): <http://crbug.com/254584>
1330 ErrorCallback(error_callback_
).Run(err
, stmt
);
1334 // The default handling is to assert on debug and to ignore on release.
1335 if (!ShouldIgnoreSqliteError(err
))
1336 DLOG(FATAL
) << GetErrorMessage();
1340 bool Connection::FullIntegrityCheck(std::vector
<std::string
>* messages
) {
1341 return IntegrityCheckHelper("PRAGMA integrity_check", messages
);
1344 bool Connection::QuickIntegrityCheck() {
1345 std::vector
<std::string
> messages
;
1346 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages
))
1348 return messages
.size() == 1 && messages
[0] == "ok";
1351 // TODO(shess): Allow specifying maximum results (default 100 lines).
1352 bool Connection::IntegrityCheckHelper(
1353 const char* pragma_sql
,
1354 std::vector
<std::string
>* messages
) {
1357 // This has the side effect of setting SQLITE_RecoveryMode, which
1358 // allows SQLite to process through certain cases of corruption.
1359 // Failing to set this pragma probably means that the database is
1361 const char kWritableSchema
[] = "PRAGMA writable_schema = ON";
1362 if (!Execute(kWritableSchema
))
1367 sql::Statement
stmt(GetUniqueStatement(pragma_sql
));
1369 // The pragma appears to return all results (up to 100 by default)
1370 // as a single string. This doesn't appear to be an API contract,
1371 // it could return separate lines, so loop _and_ split.
1372 while (stmt
.Step()) {
1373 std::string
result(stmt
.ColumnString(0));
1374 *messages
= base::SplitString(result
, "\n", base::TRIM_WHITESPACE
,
1375 base::SPLIT_WANT_ALL
);
1377 ret
= stmt
.Succeeded();
1380 // Best effort to put things back as they were before.
1381 const char kNoWritableSchema
[] = "PRAGMA writable_schema = OFF";
1382 ignore_result(Execute(kNoWritableSchema
));
1387 base::TimeTicks
TimeSource::Now() {
1388 return base::TimeTicks::Now();