blob: 20f0f075cbafb0dd6c09f4afe5f4a021f5522b36 [file] [log] [blame]
[email protected]64021042012-02-10 20:02:291// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f0a54b22011-07-19 18:40:215#include "sql/connection.h"
[email protected]e5ffd0e42009-09-11 21:30:566
7#include <string.h>
8
shessc9e80ae22015-08-12 21:39:119#include "base/bind.h"
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
thestig22dfc4012014-09-05 08:29:4411#include "base/files/file_util.h"
[email protected]a7ec1292013-07-22 22:02:1812#include "base/lazy_instance.h"
[email protected]e5ffd0e42009-09-11 21:30:5613#include "base/logging.h"
shessc9e80ae22015-08-12 21:39:1114#include "base/message_loop/message_loop.h"
[email protected]bd2ccdb4a2012-12-07 22:14:5015#include "base/metrics/histogram.h"
[email protected]210ce0af2013-05-15 09:10:3916#include "base/metrics/sparse_histogram.h"
[email protected]80abf152013-05-22 12:42:4217#include "base/strings/string_split.h"
[email protected]a4bbc1f92013-06-11 07:28:1918#include "base/strings/string_util.h"
19#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4520#include "base/strings/utf_string_conversions.h"
[email protected]a7ec1292013-07-22 22:02:1821#include "base/synchronization/lock.h"
[email protected]f0a54b22011-07-19 18:40:2122#include "sql/statement.h"
[email protected]e33cba42010-08-18 23:37:0323#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5624
[email protected]2e1cee762013-07-09 14:40:0025#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
26#include "third_party/sqlite/src/ext/icu/sqliteicu.h"
27#endif
28
[email protected]5b96f3772010-09-28 16:30:5729namespace {
30
31// Spin for up to a second waiting for the lock to clear when setting
32// up the database.
33// TODO(shess): Better story on this. http://crbug.com/56559
[email protected]c68ce172011-11-24 22:30:2734const int kBusyTimeoutSeconds = 1;
[email protected]5b96f3772010-09-28 16:30:5735
36class ScopedBusyTimeout {
37 public:
38 explicit ScopedBusyTimeout(sqlite3* db)
39 : db_(db) {
40 }
41 ~ScopedBusyTimeout() {
42 sqlite3_busy_timeout(db_, 0);
43 }
44
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()));
49 }
50
51 private:
52 sqlite3* db_;
53};
54
[email protected]6d42f152012-11-10 00:38:2455// 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).
61class ScopedWritableSchema {
62 public:
63 explicit ScopedWritableSchema(sqlite3* db)
64 : db_(db) {
65 sqlite3_exec(db_, "PRAGMA writable_schema=1", NULL, NULL, NULL);
66 }
67 ~ScopedWritableSchema() {
68 sqlite3_exec(db_, "PRAGMA writable_schema=0", NULL, NULL, NULL);
69 }
70
71 private:
72 sqlite3* db_;
73};
74
[email protected]7bae5742013-07-10 20:46:1675// Helper to wrap the sqlite3_backup_*() step of Raze(). Return
76// SQLite error code from running the backup step.
77int BackupDatabase(sqlite3* src, sqlite3* dst, const char* db_name) {
78 DCHECK_NE(src, dst);
79 sqlite3_backup* backup = sqlite3_backup_init(dst, db_name, src, db_name);
80 if (!backup) {
81 // Since this call only sets things up, this indicates a gross
82 // error in SQLite.
83 DLOG(FATAL) << "Unable to start sqlite3_backup(): " << sqlite3_errmsg(dst);
84 return sqlite3_errcode(dst);
85 }
86
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);
91
92 // If successful, exactly one page should have been backed up. If
93 // this breaks, check this function to make sure assumptions aren't
94 // being broken.
95 if (rc == SQLITE_DONE)
96 DCHECK_EQ(pages, 1);
97
98 return rc;
99}
100
[email protected]8d409412013-07-19 18:25:30101// 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.
104bool 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] == '_')) {
110 return false;
111 }
112 }
113 return true;
114}
115
shessc9e80ae22015-08-12 21:39:11116void RecordSqliteMemory10Min() {
117 const int64 used = sqlite3_memory_used();
118 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.TenMinutes", used / 1024);
119}
120
121void RecordSqliteMemoryHour() {
122 const int64 used = sqlite3_memory_used();
123 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneHour", used / 1024);
124}
125
126void RecordSqliteMemoryDay() {
127 const int64 used = sqlite3_memory_used();
128 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneDay", used / 1024);
129}
130
shess2d48e942015-08-25 17:39:51131void RecordSqliteMemoryWeek() {
132 const int64 used = sqlite3_memory_used();
133 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneWeek", used / 1024);
134}
135
[email protected]a7ec1292013-07-22 22:02:18136// SQLite automatically calls sqlite3_initialize() lazily, but
137// sqlite3_initialize() uses double-checked locking and thus can have
138// data races.
139//
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.
143base::LazyInstance<base::Lock>::Leaky
144 g_sqlite_init_lock = LAZY_INSTANCE_INITIALIZER;
145void InitializeSqlite() {
146 base::AutoLock lock(g_sqlite_init_lock.Get());
shessc9e80ae22015-08-12 21:39:11147 static bool first_call = true;
148 if (first_call) {
149 sqlite3_initialize();
150
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));
shess2d48e942015-08-25 17:39:51163 base::MessageLoop::current()->PostDelayedTask(
164 FROM_HERE, base::Bind(&RecordSqliteMemoryWeek),
165 base::TimeDelta::FromDays(7));
shessc9e80ae22015-08-12 21:39:11166 }
167
168 first_call = false;
169 }
[email protected]a7ec1292013-07-22 22:02:18170}
171
[email protected]8ada10f2013-12-21 00:42:34172// Helper to get the sqlite3_file* associated with the "main" database.
173int GetSqlite3File(sqlite3* db, sqlite3_file** file) {
174 *file = NULL;
175 int rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_FILE_POINTER, file);
176 if (rc != SQLITE_OK)
177 return rc;
178
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)
183 return SQLITE_ERROR;
184
185 return rc;
186}
187
shess58b8df82015-06-03 00:19:32188// This should match UMA_HISTOGRAM_MEDIUM_TIMES().
189base::HistogramBase* GetMediumTimeHistogram(const std::string& name) {
190 return base::Histogram::FactoryTimeGet(
191 name,
192 base::TimeDelta::FromMilliseconds(10),
193 base::TimeDelta::FromMinutes(3),
194 50,
195 base::HistogramBase::kUmaTargetedHistogramFlag);
196}
197
erg102ceb412015-06-20 01:38:13198std::string AsUTF8ForSQL(const base::FilePath& path) {
199#if defined(OS_WIN)
200 return base::WideToUTF8(path.value());
201#elif defined(OS_POSIX)
202 return path.value();
203#endif
204}
205
[email protected]5b96f3772010-09-28 16:30:57206} // namespace
207
[email protected]e5ffd0e42009-09-11 21:30:56208namespace sql {
209
[email protected]4350e322013-06-18 22:18:10210// static
211Connection::ErrorIgnorerCallback* Connection::current_ignorer_cb_ = NULL;
212
213// static
[email protected]74cdede2013-09-25 05:39:57214bool Connection::ShouldIgnoreSqliteError(int error) {
[email protected]4350e322013-06-18 22:18:10215 if (!current_ignorer_cb_)
216 return false;
217 return current_ignorer_cb_->Run(error);
218}
219
220// static
221void Connection::SetErrorIgnorer(Connection::ErrorIgnorerCallback* cb) {
222 CHECK(current_ignorer_cb_ == NULL);
223 current_ignorer_cb_ = cb;
224}
225
226// static
227void Connection::ResetErrorIgnorer() {
228 CHECK(current_ignorer_cb_);
229 current_ignorer_cb_ = NULL;
230}
231
[email protected]e5ffd0e42009-09-11 21:30:56232bool StatementID::operator<(const StatementID& other) const {
233 if (number_ != other.number_)
234 return number_ < other.number_;
235 return strcmp(str_, other.str_) < 0;
236}
237
[email protected]e5ffd0e42009-09-11 21:30:56238Connection::StatementRef::StatementRef(Connection* connection,
[email protected]41a97c812013-02-07 02:35:38239 sqlite3_stmt* stmt,
240 bool was_valid)
[email protected]e5ffd0e42009-09-11 21:30:56241 : connection_(connection),
[email protected]41a97c812013-02-07 02:35:38242 stmt_(stmt),
243 was_valid_(was_valid) {
244 if (connection)
245 connection_->StatementRefCreated(this);
[email protected]e5ffd0e42009-09-11 21:30:56246}
247
248Connection::StatementRef::~StatementRef() {
249 if (connection_)
250 connection_->StatementRefDeleted(this);
[email protected]41a97c812013-02-07 02:35:38251 Close(false);
[email protected]e5ffd0e42009-09-11 21:30:56252}
253
[email protected]41a97c812013-02-07 02:35:38254void Connection::StatementRef::Close(bool forced) {
[email protected]e5ffd0e42009-09-11 21:30:56255 if (stmt_) {
[email protected]35f7e5392012-07-27 19:54:50256 // 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([email protected]): This should move to the beginning
262 // of the function. http://crbug.com/136655.
263 AssertIOAllowed();
[email protected]e5ffd0e42009-09-11 21:30:56264 sqlite3_finalize(stmt_);
265 stmt_ = NULL;
266 }
267 connection_ = NULL; // The connection may be getting deleted.
[email protected]41a97c812013-02-07 02:35:38268
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;
[email protected]e5ffd0e42009-09-11 21:30:56273}
274
275Connection::Connection()
276 : db_(NULL),
277 page_size_(0),
278 cache_size_(0),
279 exclusive_locking_(false),
[email protected]81a2a602013-07-17 19:10:36280 restrict_to_user_(false),
[email protected]e5ffd0e42009-09-11 21:30:56281 transaction_nesting_(0),
[email protected]35f7e5392012-07-27 19:54:50282 needs_rollback_(false),
[email protected]49dc4f22012-10-17 17:41:16283 in_memory_(false),
shess58b8df82015-06-03 00:19:32284 poisoned_(false),
shess7dbd4dee2015-10-06 17:39:16285 mmap_disabled_(false),
286 mmap_enabled_(false),
287 total_changes_at_last_release_(0),
shess58b8df82015-06-03 00:19:32288 stats_histogram_(NULL),
289 commit_time_histogram_(NULL),
290 autocommit_time_histogram_(NULL),
291 update_time_histogram_(NULL),
292 query_time_histogram_(NULL),
293 clock_(new TimeSource()) {
[email protected]526b4662013-06-14 04:09:12294}
[email protected]e5ffd0e42009-09-11 21:30:56295
296Connection::~Connection() {
297 Close();
298}
299
shess58b8df82015-06-03 00:19:32300void Connection::RecordEvent(Events event, size_t count) {
301 for (size_t i = 0; i < count; ++i) {
302 UMA_HISTOGRAM_ENUMERATION("Sqlite.Stats", event, EVENT_MAX_VALUE);
303 }
304
305 if (stats_histogram_) {
306 for (size_t i = 0; i < count; ++i) {
307 stats_histogram_->Add(event);
308 }
309 }
310}
311
312void Connection::RecordCommitTime(const base::TimeDelta& delta) {
313 RecordUpdateTime(delta);
314 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.CommitTime", delta);
315 if (commit_time_histogram_)
316 commit_time_histogram_->AddTime(delta);
317}
318
319void Connection::RecordAutoCommitTime(const base::TimeDelta& delta) {
320 RecordUpdateTime(delta);
321 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.AutoCommitTime", delta);
322 if (autocommit_time_histogram_)
323 autocommit_time_histogram_->AddTime(delta);
324}
325
326void Connection::RecordUpdateTime(const base::TimeDelta& delta) {
327 RecordQueryTime(delta);
328 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.UpdateTime", delta);
329 if (update_time_histogram_)
330 update_time_histogram_->AddTime(delta);
331}
332
333void Connection::RecordQueryTime(const base::TimeDelta& delta) {
334 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.QueryTime", delta);
335 if (query_time_histogram_)
336 query_time_histogram_->AddTime(delta);
337}
338
339void Connection::RecordTimeAndChanges(
340 const base::TimeDelta& delta, bool read_only) {
341 if (read_only) {
342 RecordQueryTime(delta);
343 } else {
344 const int changes = sqlite3_changes(db_);
345 if (sqlite3_get_autocommit(db_)) {
346 RecordAutoCommitTime(delta);
347 RecordEvent(EVENT_CHANGES_AUTOCOMMIT, changes);
348 } else {
349 RecordUpdateTime(delta);
350 RecordEvent(EVENT_CHANGES, changes);
351 }
352 }
353}
354
[email protected]a3ef4832013-02-02 05:12:33355bool Connection::Open(const base::FilePath& path) {
[email protected]348ac8f52013-05-21 03:27:02356 if (!histogram_tag_.empty()) {
tfarina720d4f32015-05-11 22:31:26357 int64_t size_64 = 0;
[email protected]56285702013-12-04 18:22:49358 if (base::GetFileSize(path, &size_64)) {
[email protected]348ac8f52013-05-21 03:27:02359 size_t sample = static_cast<size_t>(size_64 / 1024);
360 std::string full_histogram_name = "Sqlite.SizeKB." + histogram_tag_;
361 base::HistogramBase* histogram =
362 base::Histogram::FactoryGet(
363 full_histogram_name, 1, 1000000, 50,
364 base::HistogramBase::kUmaTargetedHistogramFlag);
365 if (histogram)
366 histogram->Add(sample);
367 }
368 }
369
erg102ceb412015-06-20 01:38:13370 return OpenInternal(AsUTF8ForSQL(path), RETRY_ON_POISON);
[email protected]765b44502009-10-02 05:01:42371}
[email protected]e5ffd0e42009-09-11 21:30:56372
[email protected]765b44502009-10-02 05:01:42373bool Connection::OpenInMemory() {
[email protected]35f7e5392012-07-27 19:54:50374 in_memory_ = true;
[email protected]fed734a2013-07-17 04:45:13375 return OpenInternal(":memory:", NO_RETRY);
[email protected]e5ffd0e42009-09-11 21:30:56376}
377
[email protected]8d409412013-07-19 18:25:30378bool Connection::OpenTemporary() {
379 return OpenInternal("", NO_RETRY);
380}
381
[email protected]41a97c812013-02-07 02:35:38382void Connection::CloseInternal(bool forced) {
[email protected]4e179ba2012-03-17 16:06:47383 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
384 // will delete the -journal file. For ChromiumOS or other more
385 // embedded systems, this is probably not appropriate, whereas on
386 // desktop it might make some sense.
387
[email protected]4b350052012-02-24 20:40:48388 // sqlite3_close() needs all prepared statements to be finalized.
[email protected]4b350052012-02-24 20:40:48389
[email protected]41a97c812013-02-07 02:35:38390 // Release cached statements.
391 statement_cache_.clear();
392
393 // With cached statements released, in-use statements will remain.
394 // Closing the database while statements are in use is an API
395 // violation, except for forced close (which happens from within a
396 // statement's error handler).
397 DCHECK(forced || open_statements_.empty());
398
399 // Deactivate any outstanding statements so sqlite3_close() works.
400 for (StatementRefSet::iterator i = open_statements_.begin();
401 i != open_statements_.end(); ++i)
402 (*i)->Close(forced);
403 open_statements_.clear();
[email protected]4b350052012-02-24 20:40:48404
[email protected]e5ffd0e42009-09-11 21:30:56405 if (db_) {
[email protected]35f7e5392012-07-27 19:54:50406 // Call to AssertIOAllowed() cannot go at the beginning of the function
407 // because Close() must be called from destructor to clean
408 // statement_cache_, it won't cause any disk access and it most probably
409 // will happen on thread not allowing disk access.
410 // TODO([email protected]): This should move to the beginning
411 // of the function. http://crbug.com/136655.
412 AssertIOAllowed();
[email protected]73fb8d52013-07-24 05:04:28413
414 int rc = sqlite3_close(db_);
415 if (rc != SQLITE_OK) {
416 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.CloseFailure", rc);
417 DLOG(FATAL) << "sqlite3_close failed: " << GetErrorMessage();
418 }
[email protected]e5ffd0e42009-09-11 21:30:56419 }
[email protected]fed734a2013-07-17 04:45:13420 db_ = NULL;
[email protected]e5ffd0e42009-09-11 21:30:56421}
422
[email protected]41a97c812013-02-07 02:35:38423void Connection::Close() {
424 // If the database was already closed by RazeAndClose(), then no
425 // need to close again. Clear the |poisoned_| bit so that incorrect
426 // API calls are caught.
427 if (poisoned_) {
428 poisoned_ = false;
429 return;
430 }
431
432 CloseInternal(false);
433}
434
[email protected]e5ffd0e42009-09-11 21:30:56435void Connection::Preload() {
[email protected]35f7e5392012-07-27 19:54:50436 AssertIOAllowed();
437
[email protected]e5ffd0e42009-09-11 21:30:56438 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38439 DLOG_IF(FATAL, !poisoned_) << "Cannot preload null db";
[email protected]e5ffd0e42009-09-11 21:30:56440 return;
441 }
442
[email protected]8ada10f2013-12-21 00:42:34443 // Use local settings if provided, otherwise use documented defaults. The
444 // actual results could be fetching via PRAGMA calls.
445 const int page_size = page_size_ ? page_size_ : 1024;
446 sqlite3_int64 preload_size = page_size * (cache_size_ ? cache_size_ : 2000);
447 if (preload_size < 1)
[email protected]e5ffd0e42009-09-11 21:30:56448 return;
449
[email protected]8ada10f2013-12-21 00:42:34450 sqlite3_file* file = NULL;
451 int rc = GetSqlite3File(db_, &file);
452 if (rc != SQLITE_OK)
453 return;
454
455 sqlite3_int64 file_size = 0;
456 rc = file->pMethods->xFileSize(file, &file_size);
457 if (rc != SQLITE_OK)
458 return;
459
460 // Don't preload more than the file contains.
461 if (preload_size > file_size)
462 preload_size = file_size;
463
464 scoped_ptr<char[]> buf(new char[page_size]);
shessde60c5f12015-04-21 17:34:46465 for (sqlite3_int64 pos = 0; pos < preload_size; pos += page_size) {
[email protected]8ada10f2013-12-21 00:42:34466 rc = file->pMethods->xRead(file, buf.get(), page_size, pos);
467 if (rc != SQLITE_OK)
468 return;
469 }
[email protected]e5ffd0e42009-09-11 21:30:56470}
471
shess7dbd4dee2015-10-06 17:39:16472// SQLite keeps unused pages associated with a connection in a cache. It asks
473// the cache for pages by an id, and if the page is present and the database is
474// unchanged, it considers the content of the page valid and doesn't read it
475// from disk. When memory-mapped I/O is enabled, on read SQLite uses page
476// structures created from the memory map data before consulting the cache. On
477// write SQLite creates a new in-memory page structure, copies the data from the
478// memory map, and later writes it, releasing the updated page back to the
479// cache.
480//
481// This means that in memory-mapped mode, the contents of the cached pages are
482// not re-used for reads, but they are re-used for writes if the re-written page
483// is still in the cache. The implementation of sqlite3_db_release_memory() as
484// of SQLite 3.8.7.4 frees all pages from pcaches associated with the
485// connection, so it should free these pages.
486//
487// Unfortunately, the zero page is also freed. That page is never accessed
488// using memory-mapped I/O, and the cached copy can be re-used after verifying
489// the file change counter on disk. Also, fresh pages from cache receive some
490// pager-level initialization before they can be used. Since the information
491// involved will immediately be accessed in various ways, it is unclear if the
492// additional overhead is material, or just moving processor cache effects
493// around.
494//
495// TODO(shess): It would be better to release the pages immediately when they
496// are no longer needed. This would basically happen after SQLite commits a
497// transaction. I had implemented a pcache wrapper to do this, but it involved
498// layering violations, and it had to be setup before any other sqlite call,
499// which was brittle. Also, for large files it would actually make sense to
500// maintain the existing pcache behavior for blocks past the memory-mapped
501// segment. I think drh would accept a reasonable implementation of the overall
502// concept for upstreaming to SQLite core.
503//
504// TODO(shess): Another possibility would be to set the cache size small, which
505// would keep the zero page around, plus some pre-initialized pages, and SQLite
506// can manage things. The downside is that updates larger than the cache would
507// spill to the journal. That could be compensated by setting cache_spill to
508// false. The downside then is that it allows open-ended use of memory for
509// large transactions.
510//
511// TODO(shess): The TrimMemory() trick of bouncing the cache size would also
512// work. There could be two prepared statements, one for cache_size=1 one for
513// cache_size=goal.
514void Connection::ReleaseCacheMemoryIfNeeded(bool implicit_change_performed) {
515 DCHECK(is_open());
516
517 // If memory-mapping is not enabled, the page cache helps performance.
518 if (!mmap_enabled_)
519 return;
520
521 // On caller request, force the change comparison to fail. Done before the
522 // transaction-nesting test so that the signal can carry to transaction
523 // commit.
524 if (implicit_change_performed)
525 --total_changes_at_last_release_;
526
527 // Cached pages may be re-used within the same transaction.
528 if (transaction_nesting())
529 return;
530
531 // If no changes have been made, skip flushing. This allows the first page of
532 // the database to remain in cache across multiple reads.
533 const int total_changes = sqlite3_total_changes(db_);
534 if (total_changes == total_changes_at_last_release_)
535 return;
536
537 total_changes_at_last_release_ = total_changes;
538 sqlite3_db_release_memory(db_);
539}
540
[email protected]be7995f12013-07-18 18:49:14541void Connection::TrimMemory(bool aggressively) {
542 if (!db_)
543 return;
544
545 // TODO(shess): investigate using sqlite3_db_release_memory() when possible.
546 int original_cache_size;
547 {
548 Statement sql_get_original(GetUniqueStatement("PRAGMA cache_size"));
549 if (!sql_get_original.Step()) {
550 DLOG(WARNING) << "Could not get cache size " << GetErrorMessage();
551 return;
552 }
553 original_cache_size = sql_get_original.ColumnInt(0);
554 }
555 int shrink_cache_size = aggressively ? 1 : (original_cache_size / 2);
556
557 // Force sqlite to try to reduce page cache usage.
558 const std::string sql_shrink =
559 base::StringPrintf("PRAGMA cache_size=%d", shrink_cache_size);
560 if (!Execute(sql_shrink.c_str()))
561 DLOG(WARNING) << "Could not shrink cache size: " << GetErrorMessage();
562
563 // Restore cache size.
564 const std::string sql_restore =
565 base::StringPrintf("PRAGMA cache_size=%d", original_cache_size);
566 if (!Execute(sql_restore.c_str()))
567 DLOG(WARNING) << "Could not restore cache size: " << GetErrorMessage();
568}
569
[email protected]8e0c01282012-04-06 19:36:49570// Create an in-memory database with the existing database's page
571// size, then backup that database over the existing database.
572bool Connection::Raze() {
[email protected]35f7e5392012-07-27 19:54:50573 AssertIOAllowed();
574
[email protected]8e0c01282012-04-06 19:36:49575 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38576 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:49577 return false;
578 }
579
580 if (transaction_nesting_ > 0) {
581 DLOG(FATAL) << "Cannot raze within a transaction";
582 return false;
583 }
584
585 sql::Connection null_db;
586 if (!null_db.OpenInMemory()) {
587 DLOG(FATAL) << "Unable to open in-memory database.";
588 return false;
589 }
590
[email protected]6d42f152012-11-10 00:38:24591 if (page_size_) {
592 // Enforce SQLite restrictions on |page_size_|.
593 DCHECK(!(page_size_ & (page_size_ - 1)))
594 << " page_size_ " << page_size_ << " is not a power of two.";
595 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
596 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:04597 const std::string sql =
598 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]69c58452012-08-06 19:22:42599 if (!null_db.Execute(sql.c_str()))
600 return false;
601 }
602
[email protected]6d42f152012-11-10 00:38:24603#if defined(OS_ANDROID)
604 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
605 // in-memory databases do not respect this define.
606 // TODO(shess): Figure out a way to set this without using platform
607 // specific code. AFAICT from sqlite3.c, the only way to do it
608 // would be to create an actual filesystem database, which is
609 // unfortunate.
610 if (!null_db.Execute("PRAGMA auto_vacuum = 1"))
611 return false;
612#endif
[email protected]8e0c01282012-04-06 19:36:49613
614 // The page size doesn't take effect until a database has pages, and
615 // at this point the null database has none. Changing the schema
616 // version will create the first page. This will not affect the
617 // schema version in the resulting database, as SQLite's backup
618 // implementation propagates the schema version from the original
619 // connection to the new version of the database, incremented by one
620 // so that other readers see the schema change and act accordingly.
621 if (!null_db.Execute("PRAGMA schema_version = 1"))
622 return false;
623
[email protected]6d42f152012-11-10 00:38:24624 // SQLite tracks the expected number of database pages in the first
625 // page, and if it does not match the total retrieved from a
626 // filesystem call, treats the database as corrupt. This situation
627 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
628 // used to hint to SQLite to soldier on in that case, specifically
629 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
630 // sqlite3.c lockBtree().]
631 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
632 // page_size" can be used to query such a database.
633 ScopedWritableSchema writable_schema(db_);
634
[email protected]7bae5742013-07-10 20:46:16635 const char* kMain = "main";
636 int rc = BackupDatabase(null_db.db_, db_, kMain);
637 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase",rc);
[email protected]8e0c01282012-04-06 19:36:49638
639 // The destination database was locked.
640 if (rc == SQLITE_BUSY) {
641 return false;
642 }
643
[email protected]7bae5742013-07-10 20:46:16644 // SQLITE_NOTADB can happen if page 1 of db_ exists, but is not
645 // formatted correctly. SQLITE_IOERR_SHORT_READ can happen if db_
646 // isn't even big enough for one page. Either way, reach in and
647 // truncate it before trying again.
648 // TODO(shess): Maybe it would be worthwhile to just truncate from
649 // the get-go?
650 if (rc == SQLITE_NOTADB || rc == SQLITE_IOERR_SHORT_READ) {
651 sqlite3_file* file = NULL;
[email protected]8ada10f2013-12-21 00:42:34652 rc = GetSqlite3File(db_, &file);
[email protected]7bae5742013-07-10 20:46:16653 if (rc != SQLITE_OK) {
654 DLOG(FATAL) << "Failure getting file handle.";
655 return false;
[email protected]7bae5742013-07-10 20:46:16656 }
657
658 rc = file->pMethods->xTruncate(file, 0);
659 if (rc != SQLITE_OK) {
660 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabaseTruncate",rc);
661 DLOG(FATAL) << "Failed to truncate file.";
662 return false;
663 }
664
665 rc = BackupDatabase(null_db.db_, db_, kMain);
666 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase2",rc);
667
668 if (rc != SQLITE_DONE) {
669 DLOG(FATAL) << "Failed retrying Raze().";
670 }
671 }
672
[email protected]8e0c01282012-04-06 19:36:49673 // The entire database should have been backed up.
674 if (rc != SQLITE_DONE) {
[email protected]7bae5742013-07-10 20:46:16675 // TODO(shess): Figure out which other cases can happen.
[email protected]8e0c01282012-04-06 19:36:49676 DLOG(FATAL) << "Unable to copy entire null database.";
677 return false;
678 }
679
[email protected]8e0c01282012-04-06 19:36:49680 return true;
681}
682
683bool Connection::RazeWithTimout(base::TimeDelta timeout) {
684 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38685 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:49686 return false;
687 }
688
689 ScopedBusyTimeout busy_timeout(db_);
690 busy_timeout.SetTimeout(timeout);
691 return Raze();
692}
693
[email protected]41a97c812013-02-07 02:35:38694bool Connection::RazeAndClose() {
695 if (!db_) {
696 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
697 return false;
698 }
699
700 // Raze() cannot run in a transaction.
[email protected]8d409412013-07-19 18:25:30701 RollbackAllTransactions();
[email protected]41a97c812013-02-07 02:35:38702
703 bool result = Raze();
704
705 CloseInternal(true);
706
707 // Mark the database so that future API calls fail appropriately,
708 // but don't DCHECK (because after calling this function they are
709 // expected to fail).
710 poisoned_ = true;
711
712 return result;
713}
714
[email protected]8d409412013-07-19 18:25:30715void Connection::Poison() {
716 if (!db_) {
717 DLOG_IF(FATAL, !poisoned_) << "Cannot poison null db";
718 return;
719 }
720
721 RollbackAllTransactions();
722 CloseInternal(true);
723
724 // Mark the database so that future API calls fail appropriately,
725 // but don't DCHECK (because after calling this function they are
726 // expected to fail).
727 poisoned_ = true;
728}
729
[email protected]8d2e39e2013-06-24 05:55:08730// TODO(shess): To the extent possible, figure out the optimal
731// ordering for these deletes which will prevent other connections
732// from seeing odd behavior. For instance, it may be necessary to
733// manually lock the main database file in a SQLite-compatible fashion
734// (to prevent other processes from opening it), then delete the
735// journal files, then delete the main database file. Another option
736// might be to lock the main database file and poison the header with
737// junk to prevent other processes from opening it successfully (like
738// Gears "SQLite poison 3" trick).
739//
740// static
741bool Connection::Delete(const base::FilePath& path) {
742 base::ThreadRestrictions::AssertIOAllowed();
743
744 base::FilePath journal_path(path.value() + FILE_PATH_LITERAL("-journal"));
745 base::FilePath wal_path(path.value() + FILE_PATH_LITERAL("-wal"));
746
erg102ceb412015-06-20 01:38:13747 std::string journal_str = AsUTF8ForSQL(journal_path);
748 std::string wal_str = AsUTF8ForSQL(wal_path);
749 std::string path_str = AsUTF8ForSQL(path);
[email protected]8d2e39e2013-06-24 05:55:08750
shess702467622015-09-16 19:04:55751 // Make sure sqlite3_initialize() is called before anything else.
752 InitializeSqlite();
753
erg102ceb412015-06-20 01:38:13754 sqlite3_vfs* vfs = sqlite3_vfs_find(NULL);
755 CHECK(vfs);
756 CHECK(vfs->xDelete);
757 CHECK(vfs->xAccess);
758
759 // We only work with unix, win32 and mojo filesystems. If you're trying to
760 // use this code with any other VFS, you're not in a good place.
761 CHECK(strncmp(vfs->zName, "unix", 4) == 0 ||
762 strncmp(vfs->zName, "win32", 5) == 0 ||
763 strcmp(vfs->zName, "mojo") == 0);
764
765 vfs->xDelete(vfs, journal_str.c_str(), 0);
766 vfs->xDelete(vfs, wal_str.c_str(), 0);
767 vfs->xDelete(vfs, path_str.c_str(), 0);
768
769 int journal_exists = 0;
770 vfs->xAccess(vfs, journal_str.c_str(), SQLITE_ACCESS_EXISTS,
771 &journal_exists);
772
773 int wal_exists = 0;
774 vfs->xAccess(vfs, wal_str.c_str(), SQLITE_ACCESS_EXISTS,
775 &wal_exists);
776
777 int path_exists = 0;
778 vfs->xAccess(vfs, path_str.c_str(), SQLITE_ACCESS_EXISTS,
779 &path_exists);
780
781 return !journal_exists && !wal_exists && !path_exists;
[email protected]8d2e39e2013-06-24 05:55:08782}
783
[email protected]e5ffd0e42009-09-11 21:30:56784bool Connection::BeginTransaction() {
785 if (needs_rollback_) {
[email protected]88563f62011-03-13 22:13:33786 DCHECK_GT(transaction_nesting_, 0);
[email protected]e5ffd0e42009-09-11 21:30:56787
788 // When we're going to rollback, fail on this begin and don't actually
789 // mark us as entering the nested transaction.
790 return false;
791 }
792
793 bool success = true;
794 if (!transaction_nesting_) {
795 needs_rollback_ = false;
796
797 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
shess58b8df82015-06-03 00:19:32798 RecordOneEvent(EVENT_BEGIN);
[email protected]eff1fa522011-12-12 23:50:59799 if (!begin.Run())
[email protected]e5ffd0e42009-09-11 21:30:56800 return false;
801 }
802 transaction_nesting_++;
803 return success;
804}
805
806void Connection::RollbackTransaction() {
807 if (!transaction_nesting_) {
[email protected]41a97c812013-02-07 02:35:38808 DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:56809 return;
810 }
811
812 transaction_nesting_--;
813
814 if (transaction_nesting_ > 0) {
815 // Mark the outermost transaction as needing rollback.
816 needs_rollback_ = true;
817 return;
818 }
819
820 DoRollback();
821}
822
823bool Connection::CommitTransaction() {
824 if (!transaction_nesting_) {
[email protected]41a97c812013-02-07 02:35:38825 DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:56826 return false;
827 }
828 transaction_nesting_--;
829
830 if (transaction_nesting_ > 0) {
831 // Mark any nested transactions as failing after we've already got one.
832 return !needs_rollback_;
833 }
834
835 if (needs_rollback_) {
836 DoRollback();
837 return false;
838 }
839
840 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
shess58b8df82015-06-03 00:19:32841
842 // Collect the commit time manually, sql::Statement would register it as query
843 // time only.
844 const base::TimeTicks before = Now();
845 bool ret = commit.RunWithoutTimers();
846 const base::TimeDelta delta = Now() - before;
847
848 RecordCommitTime(delta);
849 RecordOneEvent(EVENT_COMMIT);
850
shess7dbd4dee2015-10-06 17:39:16851 // Release dirty cache pages after the transaction closes.
852 ReleaseCacheMemoryIfNeeded(false);
853
shess58b8df82015-06-03 00:19:32854 return ret;
[email protected]e5ffd0e42009-09-11 21:30:56855}
856
[email protected]8d409412013-07-19 18:25:30857void Connection::RollbackAllTransactions() {
858 if (transaction_nesting_ > 0) {
859 transaction_nesting_ = 0;
860 DoRollback();
861 }
862}
863
864bool Connection::AttachDatabase(const base::FilePath& other_db_path,
865 const char* attachment_point) {
866 DCHECK(ValidAttachmentPoint(attachment_point));
867
868 Statement s(GetUniqueStatement("ATTACH DATABASE ? AS ?"));
869#if OS_WIN
870 s.BindString16(0, other_db_path.value());
871#else
872 s.BindString(0, other_db_path.value());
873#endif
874 s.BindString(1, attachment_point);
875 return s.Run();
876}
877
878bool Connection::DetachDatabase(const char* attachment_point) {
879 DCHECK(ValidAttachmentPoint(attachment_point));
880
881 Statement s(GetUniqueStatement("DETACH DATABASE ?"));
882 s.BindString(0, attachment_point);
883 return s.Run();
884}
885
shess58b8df82015-06-03 00:19:32886// TODO(shess): Consider changing this to execute exactly one statement. If a
887// caller wishes to execute multiple statements, that should be explicit, and
888// perhaps tucked into an explicit transaction with rollback in case of error.
[email protected]eff1fa522011-12-12 23:50:59889int Connection::ExecuteAndReturnErrorCode(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:50890 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:38891 if (!db_) {
892 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
893 return SQLITE_ERROR;
894 }
shess58b8df82015-06-03 00:19:32895 DCHECK(sql);
896
897 RecordOneEvent(EVENT_EXECUTE);
898 int rc = SQLITE_OK;
899 while ((rc == SQLITE_OK) && *sql) {
900 sqlite3_stmt *stmt = NULL;
901 const char *leftover_sql;
902
903 const base::TimeTicks before = Now();
904 rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, &leftover_sql);
905 sql = leftover_sql;
906
907 // Stop if an error is encountered.
908 if (rc != SQLITE_OK)
909 break;
910
911 // This happens if |sql| originally only contained comments or whitespace.
912 // TODO(shess): Audit to see if this can become a DCHECK(). Having
913 // extraneous comments and whitespace in the SQL statements increases
914 // runtime cost and can easily be shifted out to the C++ layer.
915 if (!stmt)
916 continue;
917
918 // Save for use after statement is finalized.
919 const bool read_only = !!sqlite3_stmt_readonly(stmt);
920
921 RecordOneEvent(Connection::EVENT_STATEMENT_RUN);
922 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
923 // TODO(shess): Audit to see if this can become a DCHECK. I think PRAGMA
924 // is the only legitimate case for this.
925 RecordOneEvent(Connection::EVENT_STATEMENT_ROWS);
926 }
927
928 // sqlite3_finalize() returns SQLITE_OK if the most recent sqlite3_step()
929 // returned SQLITE_DONE or SQLITE_ROW, otherwise the error code.
930 rc = sqlite3_finalize(stmt);
931 if (rc == SQLITE_OK)
932 RecordOneEvent(Connection::EVENT_STATEMENT_SUCCESS);
933
934 // sqlite3_exec() does this, presumably to avoid spinning the parser for
935 // trailing whitespace.
936 // TODO(shess): Audit to see if this can become a DCHECK.
brettwb3413062015-06-24 00:39:02937 while (base::IsAsciiWhitespace(*sql)) {
shess58b8df82015-06-03 00:19:32938 sql++;
939 }
940
941 const base::TimeDelta delta = Now() - before;
942 RecordTimeAndChanges(delta, read_only);
943 }
shess7dbd4dee2015-10-06 17:39:16944
945 // Most calls to Execute() modify the database. The main exceptions would be
946 // calls such as CREATE TABLE IF NOT EXISTS which could modify the database
947 // but sometimes don't.
948 ReleaseCacheMemoryIfNeeded(true);
949
shess58b8df82015-06-03 00:19:32950 return rc;
[email protected]eff1fa522011-12-12 23:50:59951}
952
953bool Connection::Execute(const char* sql) {
[email protected]41a97c812013-02-07 02:35:38954 if (!db_) {
955 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
956 return false;
957 }
958
[email protected]eff1fa522011-12-12 23:50:59959 int error = ExecuteAndReturnErrorCode(sql);
[email protected]473ad792012-11-10 00:55:00960 if (error != SQLITE_OK)
[email protected]2f496b42013-09-26 18:36:58961 error = OnSqliteError(error, NULL, sql);
[email protected]473ad792012-11-10 00:55:00962
[email protected]28fe0ff2012-02-25 00:40:33963 // This needs to be a FATAL log because the error case of arriving here is
964 // that there's a malformed SQL statement. This can arise in development if
[email protected]4350e322013-06-18 22:18:10965 // a change alters the schema but not all queries adjust. This can happen
966 // in production if the schema is corrupted.
[email protected]eff1fa522011-12-12 23:50:59967 if (error == SQLITE_ERROR)
[email protected]28fe0ff2012-02-25 00:40:33968 DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage();
[email protected]eff1fa522011-12-12 23:50:59969 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:56970}
971
[email protected]5b96f3772010-09-28 16:30:57972bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
[email protected]41a97c812013-02-07 02:35:38973 if (!db_) {
974 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]5b96f3772010-09-28 16:30:57975 return false;
[email protected]41a97c812013-02-07 02:35:38976 }
[email protected]5b96f3772010-09-28 16:30:57977
978 ScopedBusyTimeout busy_timeout(db_);
979 busy_timeout.SetTimeout(timeout);
[email protected]eff1fa522011-12-12 23:50:59980 return Execute(sql);
[email protected]5b96f3772010-09-28 16:30:57981}
982
[email protected]e5ffd0e42009-09-11 21:30:56983bool Connection::HasCachedStatement(const StatementID& id) const {
984 return statement_cache_.find(id) != statement_cache_.end();
985}
986
987scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement(
988 const StatementID& id,
989 const char* sql) {
990 CachedStatementMap::iterator i = statement_cache_.find(id);
991 if (i != statement_cache_.end()) {
992 // Statement is in the cache. It should still be active (we're the only
993 // one invalidating cached statements, and we'll remove it from the cache
994 // if we do that. Make sure we reset it before giving out the cached one in
995 // case it still has some stuff bound.
996 DCHECK(i->second->is_valid());
997 sqlite3_reset(i->second->stmt());
998 return i->second;
999 }
1000
1001 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
1002 if (statement->is_valid())
1003 statement_cache_[id] = statement; // Only cache valid statements.
1004 return statement;
1005}
1006
1007scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement(
1008 const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501009 AssertIOAllowed();
1010
[email protected]41a97c812013-02-07 02:35:381011 // Return inactive statement.
[email protected]e5ffd0e42009-09-11 21:30:561012 if (!db_)
[email protected]41a97c812013-02-07 02:35:381013 return new StatementRef(NULL, NULL, poisoned_);
[email protected]e5ffd0e42009-09-11 21:30:561014
1015 sqlite3_stmt* stmt = NULL;
[email protected]473ad792012-11-10 00:55:001016 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
1017 if (rc != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:591018 // This is evidence of a syntax error in the incoming SQL.
shess193bfb622015-04-10 22:30:021019 if (!ShouldIgnoreSqliteError(rc))
1020 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]473ad792012-11-10 00:55:001021
1022 // It could also be database corruption.
[email protected]2f496b42013-09-26 18:36:581023 OnSqliteError(rc, NULL, sql);
[email protected]41a97c812013-02-07 02:35:381024 return new StatementRef(NULL, NULL, false);
[email protected]e5ffd0e42009-09-11 21:30:561025 }
[email protected]41a97c812013-02-07 02:35:381026 return new StatementRef(this, stmt, true);
[email protected]e5ffd0e42009-09-11 21:30:561027}
1028
[email protected]2eec0a22012-07-24 01:59:581029scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
1030 const char* sql) const {
[email protected]41a97c812013-02-07 02:35:381031 // Return inactive statement.
[email protected]2eec0a22012-07-24 01:59:581032 if (!db_)
[email protected]41a97c812013-02-07 02:35:381033 return new StatementRef(NULL, NULL, poisoned_);
[email protected]2eec0a22012-07-24 01:59:581034
1035 sqlite3_stmt* stmt = NULL;
1036 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
1037 if (rc != SQLITE_OK) {
1038 // This is evidence of a syntax error in the incoming SQL.
shess193bfb622015-04-10 22:30:021039 if (!ShouldIgnoreSqliteError(rc))
1040 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]41a97c812013-02-07 02:35:381041 return new StatementRef(NULL, NULL, false);
[email protected]2eec0a22012-07-24 01:59:581042 }
[email protected]41a97c812013-02-07 02:35:381043 return new StatementRef(NULL, stmt, true);
[email protected]2eec0a22012-07-24 01:59:581044}
1045
[email protected]92cd00a2013-08-16 11:09:581046std::string Connection::GetSchema() const {
1047 // The ORDER BY should not be necessary, but relying on organic
1048 // order for something like this is questionable.
1049 const char* kSql =
1050 "SELECT type, name, tbl_name, sql "
1051 "FROM sqlite_master ORDER BY 1, 2, 3, 4";
1052 Statement statement(GetUntrackedStatement(kSql));
1053
1054 std::string schema;
1055 while (statement.Step()) {
1056 schema += statement.ColumnString(0);
1057 schema += '|';
1058 schema += statement.ColumnString(1);
1059 schema += '|';
1060 schema += statement.ColumnString(2);
1061 schema += '|';
1062 schema += statement.ColumnString(3);
1063 schema += '\n';
1064 }
1065
1066 return schema;
1067}
1068
[email protected]eff1fa522011-12-12 23:50:591069bool Connection::IsSQLValid(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501070 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:381071 if (!db_) {
1072 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
1073 return false;
1074 }
1075
[email protected]eff1fa522011-12-12 23:50:591076 sqlite3_stmt* stmt = NULL;
1077 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK)
1078 return false;
1079
1080 sqlite3_finalize(stmt);
1081 return true;
1082}
1083
[email protected]1ed78a32009-09-15 20:24:171084bool Connection::DoesTableExist(const char* table_name) const {
[email protected]e2cadec82011-12-13 02:00:531085 return DoesTableOrIndexExist(table_name, "table");
1086}
1087
1088bool Connection::DoesIndexExist(const char* index_name) const {
1089 return DoesTableOrIndexExist(index_name, "index");
1090}
1091
1092bool Connection::DoesTableOrIndexExist(
1093 const char* name, const char* type) const {
shess92a2ab12015-04-09 01:59:471094 const char* kSql =
1095 "SELECT name FROM sqlite_master WHERE type=? AND name=? COLLATE NOCASE";
[email protected]2eec0a22012-07-24 01:59:581096 Statement statement(GetUntrackedStatement(kSql));
shess92a2ab12015-04-09 01:59:471097
1098 // This can happen if the database is corrupt and the error is being ignored
1099 // for testing purposes.
1100 if (!statement.is_valid())
1101 return false;
1102
[email protected]e2cadec82011-12-13 02:00:531103 statement.BindString(0, type);
1104 statement.BindString(1, name);
[email protected]28fe0ff2012-02-25 00:40:331105
[email protected]e5ffd0e42009-09-11 21:30:561106 return statement.Step(); // Table exists if any row was returned.
1107}
1108
1109bool Connection::DoesColumnExist(const char* table_name,
[email protected]1ed78a32009-09-15 20:24:171110 const char* column_name) const {
[email protected]e5ffd0e42009-09-11 21:30:561111 std::string sql("PRAGMA TABLE_INFO(");
1112 sql.append(table_name);
1113 sql.append(")");
1114
[email protected]2eec0a22012-07-24 01:59:581115 Statement statement(GetUntrackedStatement(sql.c_str()));
shess92a2ab12015-04-09 01:59:471116
1117 // This can happen if the database is corrupt and the error is being ignored
1118 // for testing purposes.
1119 if (!statement.is_valid())
1120 return false;
1121
[email protected]e5ffd0e42009-09-11 21:30:561122 while (statement.Step()) {
brettw8a800902015-07-10 18:28:331123 if (base::EqualsCaseInsensitiveASCII(statement.ColumnString(1),
1124 column_name))
[email protected]e5ffd0e42009-09-11 21:30:561125 return true;
1126 }
1127 return false;
1128}
1129
tfarina720d4f32015-05-11 22:31:261130int64_t Connection::GetLastInsertRowId() const {
[email protected]e5ffd0e42009-09-11 21:30:561131 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381132 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]e5ffd0e42009-09-11 21:30:561133 return 0;
1134 }
1135 return sqlite3_last_insert_rowid(db_);
1136}
1137
[email protected]1ed78a32009-09-15 20:24:171138int Connection::GetLastChangeCount() const {
1139 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381140 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]1ed78a32009-09-15 20:24:171141 return 0;
1142 }
1143 return sqlite3_changes(db_);
1144}
1145
[email protected]e5ffd0e42009-09-11 21:30:561146int Connection::GetErrorCode() const {
1147 if (!db_)
1148 return SQLITE_ERROR;
1149 return sqlite3_errcode(db_);
1150}
1151
[email protected]767718e52010-09-21 23:18:491152int Connection::GetLastErrno() const {
1153 if (!db_)
1154 return -1;
1155
1156 int err = 0;
1157 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err))
1158 return -2;
1159
1160 return err;
1161}
1162
[email protected]e5ffd0e42009-09-11 21:30:561163const char* Connection::GetErrorMessage() const {
1164 if (!db_)
1165 return "sql::Connection has no connection.";
1166 return sqlite3_errmsg(db_);
1167}
1168
[email protected]fed734a2013-07-17 04:45:131169bool Connection::OpenInternal(const std::string& file_name,
1170 Connection::Retry retry_flag) {
[email protected]35f7e5392012-07-27 19:54:501171 AssertIOAllowed();
1172
[email protected]9cfbc922009-11-17 20:13:171173 if (db_) {
[email protected]eff1fa522011-12-12 23:50:591174 DLOG(FATAL) << "sql::Connection is already open.";
[email protected]9cfbc922009-11-17 20:13:171175 return false;
1176 }
1177
[email protected]a7ec1292013-07-22 22:02:181178 // Make sure sqlite3_initialize() is called before anything else.
1179 InitializeSqlite();
1180
shess58b8df82015-06-03 00:19:321181 // Setup the stats histograms immediately rather than allocating lazily.
1182 // Connections which won't exercise all of these probably shouldn't exist.
1183 if (!histogram_tag_.empty()) {
1184 stats_histogram_ =
1185 base::LinearHistogram::FactoryGet(
1186 "Sqlite.Stats." + histogram_tag_,
1187 1, EVENT_MAX_VALUE, EVENT_MAX_VALUE + 1,
1188 base::HistogramBase::kUmaTargetedHistogramFlag);
1189
1190 // The timer setup matches UMA_HISTOGRAM_MEDIUM_TIMES(). 3 minutes is an
1191 // unreasonable time for any single operation, so there is not much value to
1192 // knowing if it was 3 minutes or 5 minutes. In reality at that point
1193 // things are entirely busted.
1194 commit_time_histogram_ =
1195 GetMediumTimeHistogram("Sqlite.CommitTime." + histogram_tag_);
1196
1197 autocommit_time_histogram_ =
1198 GetMediumTimeHistogram("Sqlite.AutoCommitTime." + histogram_tag_);
1199
1200 update_time_histogram_ =
1201 GetMediumTimeHistogram("Sqlite.UpdateTime." + histogram_tag_);
1202
1203 query_time_histogram_ =
1204 GetMediumTimeHistogram("Sqlite.QueryTime." + histogram_tag_);
1205 }
1206
[email protected]41a97c812013-02-07 02:35:381207 // If |poisoned_| is set, it means an error handler called
1208 // RazeAndClose(). Until regular Close() is called, the caller
1209 // should be treating the database as open, but is_open() currently
1210 // only considers the sqlite3 handle's state.
1211 // TODO(shess): Revise is_open() to consider poisoned_, and review
1212 // to see if any non-testing code even depends on it.
1213 DLOG_IF(FATAL, poisoned_) << "sql::Connection is already open.";
[email protected]7bae5742013-07-10 20:46:161214 poisoned_ = false;
[email protected]41a97c812013-02-07 02:35:381215
[email protected]765b44502009-10-02 05:01:421216 int err = sqlite3_open(file_name.c_str(), &db_);
1217 if (err != SQLITE_OK) {
[email protected]73fb8d52013-07-24 05:04:281218 // Extended error codes cannot be enabled until a handle is
1219 // available, fetch manually.
1220 err = sqlite3_extended_errcode(db_);
1221
[email protected]bd2ccdb4a2012-12-07 22:14:501222 // Histogram failures specific to initial open for debugging
1223 // purposes.
[email protected]73fb8d52013-07-24 05:04:281224 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenFailure", err);
[email protected]bd2ccdb4a2012-12-07 22:14:501225
[email protected]2f496b42013-09-26 18:36:581226 OnSqliteError(err, NULL, "-- sqlite3_open()");
[email protected]fed734a2013-07-17 04:45:131227 bool was_poisoned = poisoned_;
[email protected]64021042012-02-10 20:02:291228 Close();
[email protected]fed734a2013-07-17 04:45:131229
1230 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1231 return OpenInternal(file_name, NO_RETRY);
[email protected]765b44502009-10-02 05:01:421232 return false;
1233 }
1234
[email protected]81a2a602013-07-17 19:10:361235 // TODO(shess): OS_WIN support?
1236#if defined(OS_POSIX)
1237 if (restrict_to_user_) {
1238 DCHECK_NE(file_name, std::string(":memory"));
1239 base::FilePath file_path(file_name);
1240 int mode = 0;
1241 // TODO(shess): Arguably, failure to retrieve and change
1242 // permissions should be fatal if the file exists.
[email protected]b264eab2013-11-27 23:22:081243 if (base::GetPosixFilePermissions(file_path, &mode)) {
1244 mode &= base::FILE_PERMISSION_USER_MASK;
1245 base::SetPosixFilePermissions(file_path, mode);
[email protected]81a2a602013-07-17 19:10:361246
1247 // SQLite sets the permissions on these files from the main
1248 // database on create. Set them here in case they already exist
1249 // at this point. Failure to set these permissions should not
1250 // be fatal unless the file doesn't exist.
1251 base::FilePath journal_path(file_name + FILE_PATH_LITERAL("-journal"));
1252 base::FilePath wal_path(file_name + FILE_PATH_LITERAL("-wal"));
[email protected]b264eab2013-11-27 23:22:081253 base::SetPosixFilePermissions(journal_path, mode);
1254 base::SetPosixFilePermissions(wal_path, mode);
[email protected]81a2a602013-07-17 19:10:361255 }
1256 }
1257#endif // defined(OS_POSIX)
1258
[email protected]affa2da2013-06-06 22:20:341259 // SQLite uses a lookaside buffer to improve performance of small mallocs.
1260 // Chromium already depends on small mallocs being efficient, so we disable
1261 // this to avoid the extra memory overhead.
1262 // This must be called immediatly after opening the database before any SQL
1263 // statements are run.
1264 sqlite3_db_config(db_, SQLITE_DBCONFIG_LOOKASIDE, NULL, 0, 0);
1265
[email protected]73fb8d52013-07-24 05:04:281266 // Enable extended result codes to provide more color on I/O errors.
1267 // Not having extended result codes is not a fatal problem, as
1268 // Chromium code does not attempt to handle I/O errors anyhow. The
1269 // current implementation always returns SQLITE_OK, the DCHECK is to
1270 // quickly notify someone if SQLite changes.
1271 err = sqlite3_extended_result_codes(db_, 1);
1272 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
1273
[email protected]bd2ccdb4a2012-12-07 22:14:501274 // sqlite3_open() does not actually read the database file (unless a
1275 // hot journal is found). Successfully executing this pragma on an
1276 // existing database requires a valid header on page 1.
1277 // TODO(shess): For now, just probing to see what the lay of the
1278 // land is. If it's mostly SQLITE_NOTADB, then the database should
1279 // be razed.
1280 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
1281 if (err != SQLITE_OK)
[email protected]73fb8d52013-07-24 05:04:281282 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenProbeFailure", err);
[email protected]658f8332010-09-18 04:40:431283
[email protected]2e1cee762013-07-09 14:40:001284#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
1285 // The version of SQLite shipped with iOS doesn't enable ICU, which includes
1286 // REGEXP support. Add it in dynamically.
1287 err = sqlite3IcuInit(db_);
1288 DCHECK_EQ(err, SQLITE_OK) << "Could not enable ICU support";
1289#endif // OS_IOS && USE_SYSTEM_SQLITE
1290
[email protected]5b96f3772010-09-28 16:30:571291 // If indicated, lock up the database before doing anything else, so
1292 // that the following code doesn't have to deal with locking.
1293 // TODO(shess): This code is brittle. Find the cases where code
1294 // doesn't request |exclusive_locking_| and audit that it does the
1295 // right thing with SQLITE_BUSY, and that it doesn't make
1296 // assumptions about who might change things in the database.
1297 // http://crbug.com/56559
1298 if (exclusive_locking_) {
[email protected]4350e322013-06-18 22:18:101299 // TODO(shess): This should probably be a failure. Code which
1300 // requests exclusive locking but doesn't get it is almost certain
1301 // to be ill-tested.
1302 ignore_result(Execute("PRAGMA locking_mode=EXCLUSIVE"));
[email protected]5b96f3772010-09-28 16:30:571303 }
1304
[email protected]4e179ba2012-03-17 16:06:471305 // http://www.sqlite.org/pragma.html#pragma_journal_mode
1306 // DELETE (default) - delete -journal file to commit.
1307 // TRUNCATE - truncate -journal file to commit.
1308 // PERSIST - zero out header of -journal file to commit.
shess2c21ecf2015-06-02 01:31:091309 // TRUNCATE should be faster than DELETE because it won't need directory
1310 // changes for each transaction. PERSIST may break the spirit of using
1311 // secure_delete.
1312 ignore_result(Execute("PRAGMA journal_mode = TRUNCATE"));
[email protected]4e179ba2012-03-17 16:06:471313
shess7dbd4dee2015-10-06 17:39:161314 // Enable memory-mapped access. This value will be capped by
1315 // SQLITE_MAX_MMAP_SIZE, which could be different between 32-bit and 64-bit
1316 // platforms.
1317 mmap_enabled_ = false;
1318 if (!mmap_disabled_)
1319 ignore_result(Execute("PRAGMA mmap_size = 268435456")); // 256MB.
1320 {
1321 Statement s(GetUniqueStatement("PRAGMA mmap_size"));
1322 if (s.Step() && s.ColumnInt64(0) > 0)
1323 mmap_enabled_ = true;
1324 }
1325
[email protected]c68ce172011-11-24 22:30:271326 const base::TimeDelta kBusyTimeout =
1327 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
1328
[email protected]765b44502009-10-02 05:01:421329 if (page_size_ != 0) {
[email protected]5b96f3772010-09-28 16:30:571330 // Enforce SQLite restrictions on |page_size_|.
1331 DCHECK(!(page_size_ & (page_size_ - 1)))
1332 << " page_size_ " << page_size_ << " is not a power of two.";
[email protected]6d42f152012-11-10 00:38:241333 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
[email protected]5b96f3772010-09-28 16:30:571334 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:041335 const std::string sql =
1336 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]4350e322013-06-18 22:18:101337 ignore_result(ExecuteWithTimeout(sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421338 }
1339
1340 if (cache_size_ != 0) {
[email protected]7d3cbc92013-03-18 22:33:041341 const std::string sql =
1342 base::StringPrintf("PRAGMA cache_size=%d", cache_size_);
[email protected]4350e322013-06-18 22:18:101343 ignore_result(ExecuteWithTimeout(sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421344 }
1345
[email protected]6e0b1442011-08-09 23:23:581346 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) {
[email protected]fed734a2013-07-17 04:45:131347 bool was_poisoned = poisoned_;
[email protected]6e0b1442011-08-09 23:23:581348 Close();
[email protected]fed734a2013-07-17 04:45:131349 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1350 return OpenInternal(file_name, NO_RETRY);
[email protected]6e0b1442011-08-09 23:23:581351 return false;
1352 }
1353
[email protected]765b44502009-10-02 05:01:421354 return true;
1355}
1356
[email protected]e5ffd0e42009-09-11 21:30:561357void Connection::DoRollback() {
1358 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
shess58b8df82015-06-03 00:19:321359
1360 // Collect the rollback time manually, sql::Statement would register it as
1361 // query time only.
1362 const base::TimeTicks before = Now();
1363 rollback.RunWithoutTimers();
1364 const base::TimeDelta delta = Now() - before;
1365
1366 RecordUpdateTime(delta);
1367 RecordOneEvent(EVENT_ROLLBACK);
1368
shess7dbd4dee2015-10-06 17:39:161369 // The cache may have been accumulating dirty pages for commit. Note that in
1370 // some cases sql::Transaction can fire rollback after a database is closed.
1371 if (is_open())
1372 ReleaseCacheMemoryIfNeeded(false);
1373
[email protected]44ad7d902012-03-23 00:09:051374 needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:561375}
1376
1377void Connection::StatementRefCreated(StatementRef* ref) {
1378 DCHECK(open_statements_.find(ref) == open_statements_.end());
1379 open_statements_.insert(ref);
1380}
1381
1382void Connection::StatementRefDeleted(StatementRef* ref) {
1383 StatementRefSet::iterator i = open_statements_.find(ref);
1384 if (i == open_statements_.end())
[email protected]eff1fa522011-12-12 23:50:591385 DLOG(FATAL) << "Could not find statement";
[email protected]e5ffd0e42009-09-11 21:30:561386 else
1387 open_statements_.erase(i);
1388}
1389
shess58b8df82015-06-03 00:19:321390void Connection::set_histogram_tag(const std::string& tag) {
1391 DCHECK(!is_open());
1392 histogram_tag_ = tag;
1393}
1394
[email protected]210ce0af2013-05-15 09:10:391395void Connection::AddTaggedHistogram(const std::string& name,
1396 size_t sample) const {
1397 if (histogram_tag_.empty())
1398 return;
1399
1400 // TODO(shess): The histogram macros create a bit of static storage
1401 // for caching the histogram object. This code shouldn't execute
1402 // often enough for such caching to be crucial. If it becomes an
1403 // issue, the object could be cached alongside histogram_prefix_.
1404 std::string full_histogram_name = name + "." + histogram_tag_;
1405 base::HistogramBase* histogram =
1406 base::SparseHistogram::FactoryGet(
1407 full_histogram_name,
1408 base::HistogramBase::kUmaTargetedHistogramFlag);
1409 if (histogram)
1410 histogram->Add(sample);
1411}
1412
[email protected]2f496b42013-09-26 18:36:581413int Connection::OnSqliteError(int err, sql::Statement *stmt, const char* sql) {
[email protected]210ce0af2013-05-15 09:10:391414 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err);
1415 AddTaggedHistogram("Sqlite.Error", err);
[email protected]c088e3a32013-01-03 23:59:141416
1417 // Always log the error.
[email protected]2f496b42013-09-26 18:36:581418 if (!sql && stmt)
1419 sql = stmt->GetSQLStatement();
1420 if (!sql)
1421 sql = "-- unknown";
1422 LOG(ERROR) << histogram_tag_ << " sqlite error " << err
[email protected]c088e3a32013-01-03 23:59:141423 << ", errno " << GetLastErrno()
[email protected]2f496b42013-09-26 18:36:581424 << ": " << GetErrorMessage()
1425 << ", sql: " << sql;
[email protected]c088e3a32013-01-03 23:59:141426
[email protected]c3881b372013-05-17 08:39:461427 if (!error_callback_.is_null()) {
[email protected]98cf3002013-07-12 01:38:561428 // Fire from a copy of the callback in case of reentry into
1429 // re/set_error_callback().
1430 // TODO(shess): <http://crbug.com/254584>
1431 ErrorCallback(error_callback_).Run(err, stmt);
[email protected]c3881b372013-05-17 08:39:461432 return err;
1433 }
1434
[email protected]faa604e2009-09-25 22:38:591435 // The default handling is to assert on debug and to ignore on release.
[email protected]74cdede2013-09-25 05:39:571436 if (!ShouldIgnoreSqliteError(err))
[email protected]4350e322013-06-18 22:18:101437 DLOG(FATAL) << GetErrorMessage();
[email protected]faa604e2009-09-25 22:38:591438 return err;
1439}
1440
[email protected]579446c2013-12-16 18:36:521441bool Connection::FullIntegrityCheck(std::vector<std::string>* messages) {
1442 return IntegrityCheckHelper("PRAGMA integrity_check", messages);
1443}
1444
1445bool Connection::QuickIntegrityCheck() {
1446 std::vector<std::string> messages;
1447 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages))
1448 return false;
1449 return messages.size() == 1 && messages[0] == "ok";
1450}
1451
[email protected]80abf152013-05-22 12:42:421452// TODO(shess): Allow specifying maximum results (default 100 lines).
[email protected]579446c2013-12-16 18:36:521453bool Connection::IntegrityCheckHelper(
1454 const char* pragma_sql,
1455 std::vector<std::string>* messages) {
[email protected]80abf152013-05-22 12:42:421456 messages->clear();
1457
[email protected]4658e2a02013-06-06 23:05:001458 // This has the side effect of setting SQLITE_RecoveryMode, which
1459 // allows SQLite to process through certain cases of corruption.
1460 // Failing to set this pragma probably means that the database is
1461 // beyond recovery.
1462 const char kWritableSchema[] = "PRAGMA writable_schema = ON";
1463 if (!Execute(kWritableSchema))
1464 return false;
1465
1466 bool ret = false;
1467 {
[email protected]579446c2013-12-16 18:36:521468 sql::Statement stmt(GetUniqueStatement(pragma_sql));
[email protected]4658e2a02013-06-06 23:05:001469
1470 // The pragma appears to return all results (up to 100 by default)
1471 // as a single string. This doesn't appear to be an API contract,
1472 // it could return separate lines, so loop _and_ split.
1473 while (stmt.Step()) {
1474 std::string result(stmt.ColumnString(0));
brettw83dc1612015-08-12 07:31:181475 *messages = base::SplitString(result, "\n", base::TRIM_WHITESPACE,
1476 base::SPLIT_WANT_ALL);
[email protected]4658e2a02013-06-06 23:05:001477 }
1478 ret = stmt.Succeeded();
[email protected]80abf152013-05-22 12:42:421479 }
[email protected]4658e2a02013-06-06 23:05:001480
1481 // Best effort to put things back as they were before.
1482 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF";
1483 ignore_result(Execute(kNoWritableSchema));
1484
1485 return ret;
[email protected]80abf152013-05-22 12:42:421486}
1487
shess58b8df82015-06-03 00:19:321488base::TimeTicks TimeSource::Now() {
1489 return base::TimeTicks::Now();
1490}
1491
[email protected]e5ffd0e42009-09-11 21:30:561492} // namespace sql