blob: d7477f57dbfca926fb8c8c78c63cb02a810e5c05 [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
avi51ba3e692015-12-26 17:30:507#include <limits.h>
avi0b519202015-12-21 07:25:198#include <stddef.h>
9#include <stdint.h>
[email protected]e5ffd0e42009-09-11 21:30:5610#include <string.h>
mostynbd82cd9952016-04-11 20:05:3411
dchenge48600452015-12-28 02:24:5012#include <utility>
[email protected]e5ffd0e42009-09-11 21:30:5613
shessc9e80ae22015-08-12 21:39:1114#include "base/bind.h"
tzikb9dae932017-02-10 03:57:3015#include "base/debug/alias.h"
shessc8cd2a162015-10-22 20:30:4616#include "base/debug/dump_without_crashing.h"
[email protected]57999812013-02-24 05:40:5217#include "base/files/file_path.h"
thestig22dfc4012014-09-05 08:29:4418#include "base/files/file_util.h"
shessc8cd2a162015-10-22 20:30:4619#include "base/format_macros.h"
20#include "base/json/json_file_value_serializer.h"
[email protected]a7ec1292013-07-22 22:02:1821#include "base/lazy_instance.h"
fdoray2dfa76452016-06-07 13:11:2222#include "base/location.h"
[email protected]e5ffd0e42009-09-11 21:30:5623#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0824#include "base/metrics/histogram_macros.h"
[email protected]210ce0af2013-05-15 09:10:3925#include "base/metrics/sparse_histogram.h"
fdoray2dfa76452016-06-07 13:11:2226#include "base/single_thread_task_runner.h"
[email protected]80abf152013-05-22 12:42:4227#include "base/strings/string_split.h"
[email protected]a4bbc1f92013-06-11 07:28:1928#include "base/strings/string_util.h"
29#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4530#include "base/strings/utf_string_conversions.h"
[email protected]a7ec1292013-07-22 22:02:1831#include "base/synchronization/lock.h"
fdoray2dfa76452016-06-07 13:11:2232#include "base/threading/thread_task_runner_handle.h"
ssid9f8022f2015-10-12 17:49:0333#include "base/trace_event/memory_dump_manager.h"
Kevin Marshalla9f05ec2017-07-14 02:10:0534#include "build/build_config.h"
ssid3be5b1ec2016-01-13 14:21:5735#include "sql/connection_memory_dump_provider.h"
shess9bf2c672015-12-18 01:18:0836#include "sql/meta_table.h"
[email protected]f0a54b22011-07-19 18:40:2137#include "sql/statement.h"
shess5f2c3442017-01-24 02:15:1038#include "sql/vfs_wrapper.h"
[email protected]e33cba42010-08-18 23:37:0339#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5640
[email protected]2e1cee762013-07-09 14:40:0041#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
rohitrao83d6b83a2016-06-21 07:25:5742#include "base/ios/ios_util.h"
[email protected]2e1cee762013-07-09 14:40:0043#include "third_party/sqlite/src/ext/icu/sqliteicu.h"
44#endif
45
[email protected]5b96f3772010-09-28 16:30:5746namespace {
47
48// Spin for up to a second waiting for the lock to clear when setting
49// up the database.
50// TODO(shess): Better story on this. http://crbug.com/56559
[email protected]c68ce172011-11-24 22:30:2751const int kBusyTimeoutSeconds = 1;
[email protected]5b96f3772010-09-28 16:30:5752
53class ScopedBusyTimeout {
54 public:
55 explicit ScopedBusyTimeout(sqlite3* db)
56 : db_(db) {
57 }
58 ~ScopedBusyTimeout() {
59 sqlite3_busy_timeout(db_, 0);
60 }
61
62 int SetTimeout(base::TimeDelta timeout) {
63 DCHECK_LT(timeout.InMilliseconds(), INT_MAX);
64 return sqlite3_busy_timeout(db_,
65 static_cast<int>(timeout.InMilliseconds()));
66 }
67
68 private:
69 sqlite3* db_;
70};
71
[email protected]6d42f152012-11-10 00:38:2472// Helper to "safely" enable writable_schema. No error checking
73// because it is reasonable to just forge ahead in case of an error.
74// If turning it on fails, then most likely nothing will work, whereas
75// if turning it off fails, it only matters if some code attempts to
76// continue working with the database and tries to modify the
77// sqlite_master table (none of our code does this).
78class ScopedWritableSchema {
79 public:
80 explicit ScopedWritableSchema(sqlite3* db)
81 : db_(db) {
82 sqlite3_exec(db_, "PRAGMA writable_schema=1", NULL, NULL, NULL);
83 }
84 ~ScopedWritableSchema() {
85 sqlite3_exec(db_, "PRAGMA writable_schema=0", NULL, NULL, NULL);
86 }
87
88 private:
89 sqlite3* db_;
90};
91
[email protected]7bae5742013-07-10 20:46:1692// Helper to wrap the sqlite3_backup_*() step of Raze(). Return
93// SQLite error code from running the backup step.
94int BackupDatabase(sqlite3* src, sqlite3* dst, const char* db_name) {
95 DCHECK_NE(src, dst);
96 sqlite3_backup* backup = sqlite3_backup_init(dst, db_name, src, db_name);
97 if (!backup) {
98 // Since this call only sets things up, this indicates a gross
99 // error in SQLite.
100 DLOG(FATAL) << "Unable to start sqlite3_backup(): " << sqlite3_errmsg(dst);
101 return sqlite3_errcode(dst);
102 }
103
104 // -1 backs up the entire database.
105 int rc = sqlite3_backup_step(backup, -1);
106 int pages = sqlite3_backup_pagecount(backup);
107 sqlite3_backup_finish(backup);
108
109 // If successful, exactly one page should have been backed up. If
110 // this breaks, check this function to make sure assumptions aren't
111 // being broken.
112 if (rc == SQLITE_DONE)
113 DCHECK_EQ(pages, 1);
114
115 return rc;
116}
117
[email protected]8d409412013-07-19 18:25:30118// Be very strict on attachment point. SQLite can handle a much wider
119// character set with appropriate quoting, but Chromium code should
120// just use clean names to start with.
121bool ValidAttachmentPoint(const char* attachment_point) {
122 for (size_t i = 0; attachment_point[i]; ++i) {
zhongyi23960342016-04-12 23:13:20123 if (!(base::IsAsciiDigit(attachment_point[i]) ||
124 base::IsAsciiAlpha(attachment_point[i]) ||
[email protected]8d409412013-07-19 18:25:30125 attachment_point[i] == '_')) {
126 return false;
127 }
128 }
129 return true;
130}
131
shessc9e80ae22015-08-12 21:39:11132void RecordSqliteMemory10Min() {
avi0b519202015-12-21 07:25:19133 const int64_t used = sqlite3_memory_used();
shessc9e80ae22015-08-12 21:39:11134 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.TenMinutes", used / 1024);
135}
136
137void RecordSqliteMemoryHour() {
avi0b519202015-12-21 07:25:19138 const int64_t used = sqlite3_memory_used();
shessc9e80ae22015-08-12 21:39:11139 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneHour", used / 1024);
140}
141
142void RecordSqliteMemoryDay() {
avi0b519202015-12-21 07:25:19143 const int64_t used = sqlite3_memory_used();
shessc9e80ae22015-08-12 21:39:11144 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneDay", used / 1024);
145}
146
shess2d48e942015-08-25 17:39:51147void RecordSqliteMemoryWeek() {
avi0b519202015-12-21 07:25:19148 const int64_t used = sqlite3_memory_used();
shess2d48e942015-08-25 17:39:51149 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneWeek", used / 1024);
150}
151
[email protected]a7ec1292013-07-22 22:02:18152// SQLite automatically calls sqlite3_initialize() lazily, but
153// sqlite3_initialize() uses double-checked locking and thus can have
154// data races.
155//
156// TODO(shess): Another alternative would be to have
157// sqlite3_initialize() called as part of process bring-up. If this
158// is changed, remove the dynamic_annotations dependency in sql.gyp.
159base::LazyInstance<base::Lock>::Leaky
160 g_sqlite_init_lock = LAZY_INSTANCE_INITIALIZER;
161void InitializeSqlite() {
162 base::AutoLock lock(g_sqlite_init_lock.Get());
shessc9e80ae22015-08-12 21:39:11163 static bool first_call = true;
164 if (first_call) {
165 sqlite3_initialize();
166
167 // Schedule callback to record memory footprint histograms at 10m, 1h, and
fdoray2dfa76452016-06-07 13:11:22168 // 1d. There may not be a registered thread task runner in tests.
169 if (base::ThreadTaskRunnerHandle::IsSet()) {
170 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
shessc9e80ae22015-08-12 21:39:11171 FROM_HERE, base::Bind(&RecordSqliteMemory10Min),
172 base::TimeDelta::FromMinutes(10));
fdoray2dfa76452016-06-07 13:11:22173 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
shessc9e80ae22015-08-12 21:39:11174 FROM_HERE, base::Bind(&RecordSqliteMemoryHour),
175 base::TimeDelta::FromHours(1));
fdoray2dfa76452016-06-07 13:11:22176 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
shessc9e80ae22015-08-12 21:39:11177 FROM_HERE, base::Bind(&RecordSqliteMemoryDay),
178 base::TimeDelta::FromDays(1));
fdoray2dfa76452016-06-07 13:11:22179 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
shess2d48e942015-08-25 17:39:51180 FROM_HERE, base::Bind(&RecordSqliteMemoryWeek),
181 base::TimeDelta::FromDays(7));
shessc9e80ae22015-08-12 21:39:11182 }
183
184 first_call = false;
185 }
[email protected]a7ec1292013-07-22 22:02:18186}
187
[email protected]8ada10f2013-12-21 00:42:34188// Helper to get the sqlite3_file* associated with the "main" database.
189int GetSqlite3File(sqlite3* db, sqlite3_file** file) {
190 *file = NULL;
191 int rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_FILE_POINTER, file);
192 if (rc != SQLITE_OK)
193 return rc;
194
195 // TODO(shess): NULL in file->pMethods has been observed on android_dbg
196 // content_unittests, even though it should not be possible.
197 // http://crbug.com/329982
198 if (!*file || !(*file)->pMethods)
199 return SQLITE_ERROR;
200
201 return rc;
202}
203
shess5dac334f2015-11-05 20:47:42204// Convenience to get the sqlite3_file* and the size for the "main" database.
205int GetSqlite3FileAndSize(sqlite3* db,
206 sqlite3_file** file, sqlite3_int64* db_size) {
207 int rc = GetSqlite3File(db, file);
208 if (rc != SQLITE_OK)
209 return rc;
210
211 return (*file)->pMethods->xFileSize(*file, db_size);
212}
213
shess58b8df82015-06-03 00:19:32214// This should match UMA_HISTOGRAM_MEDIUM_TIMES().
215base::HistogramBase* GetMediumTimeHistogram(const std::string& name) {
216 return base::Histogram::FactoryTimeGet(
217 name,
218 base::TimeDelta::FromMilliseconds(10),
219 base::TimeDelta::FromMinutes(3),
220 50,
221 base::HistogramBase::kUmaTargetedHistogramFlag);
222}
223
erg102ceb412015-06-20 01:38:13224std::string AsUTF8ForSQL(const base::FilePath& path) {
225#if defined(OS_WIN)
226 return base::WideToUTF8(path.value());
227#elif defined(OS_POSIX)
228 return path.value();
229#endif
230}
231
[email protected]5b96f3772010-09-28 16:30:57232} // namespace
233
[email protected]e5ffd0e42009-09-11 21:30:56234namespace sql {
235
[email protected]4350e322013-06-18 22:18:10236// static
shess976814402016-06-21 06:56:25237Connection::ErrorExpecterCallback* Connection::current_expecter_cb_ = NULL;
[email protected]4350e322013-06-18 22:18:10238
239// static
shess976814402016-06-21 06:56:25240bool Connection::IsExpectedSqliteError(int error) {
241 if (!current_expecter_cb_)
[email protected]4350e322013-06-18 22:18:10242 return false;
shess976814402016-06-21 06:56:25243 return current_expecter_cb_->Run(error);
[email protected]4350e322013-06-18 22:18:10244}
245
shessc8cd2a162015-10-22 20:30:46246void Connection::ReportDiagnosticInfo(int extended_error, Statement* stmt) {
247 AssertIOAllowed();
248
afakhry7c9abe72016-08-05 17:33:19249 std::string debug_info = GetDiagnosticInfo(extended_error, stmt);
shessc8cd2a162015-10-22 20:30:46250 if (!debug_info.empty() && RegisterIntentToUpload()) {
251 char debug_buf[2000];
252 base::strlcpy(debug_buf, debug_info.c_str(), arraysize(debug_buf));
253 base::debug::Alias(&debug_buf);
254
255 base::debug::DumpWithoutCrashing();
256 }
257}
258
[email protected]4350e322013-06-18 22:18:10259// static
shess976814402016-06-21 06:56:25260void Connection::SetErrorExpecter(Connection::ErrorExpecterCallback* cb) {
261 CHECK(current_expecter_cb_ == NULL);
262 current_expecter_cb_ = cb;
[email protected]4350e322013-06-18 22:18:10263}
264
265// static
shess976814402016-06-21 06:56:25266void Connection::ResetErrorExpecter() {
267 CHECK(current_expecter_cb_);
268 current_expecter_cb_ = NULL;
[email protected]4350e322013-06-18 22:18:10269}
270
[email protected]e5ffd0e42009-09-11 21:30:56271bool StatementID::operator<(const StatementID& other) const {
272 if (number_ != other.number_)
273 return number_ < other.number_;
274 return strcmp(str_, other.str_) < 0;
275}
276
[email protected]e5ffd0e42009-09-11 21:30:56277Connection::StatementRef::StatementRef(Connection* connection,
[email protected]41a97c812013-02-07 02:35:38278 sqlite3_stmt* stmt,
279 bool was_valid)
[email protected]e5ffd0e42009-09-11 21:30:56280 : connection_(connection),
[email protected]41a97c812013-02-07 02:35:38281 stmt_(stmt),
282 was_valid_(was_valid) {
283 if (connection)
284 connection_->StatementRefCreated(this);
[email protected]e5ffd0e42009-09-11 21:30:56285}
286
287Connection::StatementRef::~StatementRef() {
288 if (connection_)
289 connection_->StatementRefDeleted(this);
[email protected]41a97c812013-02-07 02:35:38290 Close(false);
[email protected]e5ffd0e42009-09-11 21:30:56291}
292
[email protected]41a97c812013-02-07 02:35:38293void Connection::StatementRef::Close(bool forced) {
[email protected]e5ffd0e42009-09-11 21:30:56294 if (stmt_) {
[email protected]35f7e5392012-07-27 19:54:50295 // Call to AssertIOAllowed() cannot go at the beginning of the function
296 // because Close() is called unconditionally from destructor to clean
297 // connection_. And if this is inactive statement this won't cause any
298 // disk access and destructor most probably will be called on thread
299 // not allowing disk access.
300 // TODO([email protected]): This should move to the beginning
301 // of the function. http://crbug.com/136655.
302 AssertIOAllowed();
[email protected]e5ffd0e42009-09-11 21:30:56303 sqlite3_finalize(stmt_);
304 stmt_ = NULL;
305 }
306 connection_ = NULL; // The connection may be getting deleted.
[email protected]41a97c812013-02-07 02:35:38307
308 // Forced close is expected to happen from a statement error
309 // handler. In that case maintain the sense of |was_valid_| which
310 // previously held for this ref.
311 was_valid_ = was_valid_ && forced;
[email protected]e5ffd0e42009-09-11 21:30:56312}
313
314Connection::Connection()
315 : db_(NULL),
316 page_size_(0),
317 cache_size_(0),
318 exclusive_locking_(false),
[email protected]81a2a602013-07-17 19:10:36319 restrict_to_user_(false),
[email protected]e5ffd0e42009-09-11 21:30:56320 transaction_nesting_(0),
[email protected]35f7e5392012-07-27 19:54:50321 needs_rollback_(false),
[email protected]49dc4f22012-10-17 17:41:16322 in_memory_(false),
shess58b8df82015-06-03 00:19:32323 poisoned_(false),
shessa62504d2016-11-07 19:26:12324 mmap_alt_status_(false),
kerz42ff2a012016-04-27 04:50:06325 mmap_disabled_(false),
shess7dbd4dee2015-10-06 17:39:16326 mmap_enabled_(false),
327 total_changes_at_last_release_(0),
shess58b8df82015-06-03 00:19:32328 stats_histogram_(NULL),
329 commit_time_histogram_(NULL),
330 autocommit_time_histogram_(NULL),
331 update_time_histogram_(NULL),
332 query_time_histogram_(NULL),
333 clock_(new TimeSource()) {
[email protected]526b4662013-06-14 04:09:12334}
[email protected]e5ffd0e42009-09-11 21:30:56335
336Connection::~Connection() {
337 Close();
338}
339
shess58b8df82015-06-03 00:19:32340void Connection::RecordEvent(Events event, size_t count) {
341 for (size_t i = 0; i < count; ++i) {
342 UMA_HISTOGRAM_ENUMERATION("Sqlite.Stats", event, EVENT_MAX_VALUE);
343 }
344
345 if (stats_histogram_) {
346 for (size_t i = 0; i < count; ++i) {
347 stats_histogram_->Add(event);
348 }
349 }
350}
351
352void Connection::RecordCommitTime(const base::TimeDelta& delta) {
353 RecordUpdateTime(delta);
354 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.CommitTime", delta);
355 if (commit_time_histogram_)
356 commit_time_histogram_->AddTime(delta);
357}
358
359void Connection::RecordAutoCommitTime(const base::TimeDelta& delta) {
360 RecordUpdateTime(delta);
361 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.AutoCommitTime", delta);
362 if (autocommit_time_histogram_)
363 autocommit_time_histogram_->AddTime(delta);
364}
365
366void Connection::RecordUpdateTime(const base::TimeDelta& delta) {
367 RecordQueryTime(delta);
368 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.UpdateTime", delta);
369 if (update_time_histogram_)
370 update_time_histogram_->AddTime(delta);
371}
372
373void Connection::RecordQueryTime(const base::TimeDelta& delta) {
374 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.QueryTime", delta);
375 if (query_time_histogram_)
376 query_time_histogram_->AddTime(delta);
377}
378
379void Connection::RecordTimeAndChanges(
380 const base::TimeDelta& delta, bool read_only) {
381 if (read_only) {
382 RecordQueryTime(delta);
383 } else {
384 const int changes = sqlite3_changes(db_);
385 if (sqlite3_get_autocommit(db_)) {
386 RecordAutoCommitTime(delta);
387 RecordEvent(EVENT_CHANGES_AUTOCOMMIT, changes);
388 } else {
389 RecordUpdateTime(delta);
390 RecordEvent(EVENT_CHANGES, changes);
391 }
392 }
393}
394
[email protected]a3ef4832013-02-02 05:12:33395bool Connection::Open(const base::FilePath& path) {
[email protected]348ac8f52013-05-21 03:27:02396 if (!histogram_tag_.empty()) {
tfarina720d4f32015-05-11 22:31:26397 int64_t size_64 = 0;
[email protected]56285702013-12-04 18:22:49398 if (base::GetFileSize(path, &size_64)) {
[email protected]348ac8f52013-05-21 03:27:02399 size_t sample = static_cast<size_t>(size_64 / 1024);
400 std::string full_histogram_name = "Sqlite.SizeKB." + histogram_tag_;
401 base::HistogramBase* histogram =
402 base::Histogram::FactoryGet(
403 full_histogram_name, 1, 1000000, 50,
404 base::HistogramBase::kUmaTargetedHistogramFlag);
405 if (histogram)
406 histogram->Add(sample);
shess9bf2c672015-12-18 01:18:08407 UMA_HISTOGRAM_COUNTS("Sqlite.SizeKB", sample);
[email protected]348ac8f52013-05-21 03:27:02408 }
409 }
410
erg102ceb412015-06-20 01:38:13411 return OpenInternal(AsUTF8ForSQL(path), RETRY_ON_POISON);
[email protected]765b44502009-10-02 05:01:42412}
[email protected]e5ffd0e42009-09-11 21:30:56413
[email protected]765b44502009-10-02 05:01:42414bool Connection::OpenInMemory() {
[email protected]35f7e5392012-07-27 19:54:50415 in_memory_ = true;
[email protected]fed734a2013-07-17 04:45:13416 return OpenInternal(":memory:", NO_RETRY);
[email protected]e5ffd0e42009-09-11 21:30:56417}
418
[email protected]8d409412013-07-19 18:25:30419bool Connection::OpenTemporary() {
420 return OpenInternal("", NO_RETRY);
421}
422
[email protected]41a97c812013-02-07 02:35:38423void Connection::CloseInternal(bool forced) {
[email protected]4e179ba2012-03-17 16:06:47424 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
425 // will delete the -journal file. For ChromiumOS or other more
426 // embedded systems, this is probably not appropriate, whereas on
427 // desktop it might make some sense.
428
[email protected]4b350052012-02-24 20:40:48429 // sqlite3_close() needs all prepared statements to be finalized.
[email protected]4b350052012-02-24 20:40:48430
[email protected]41a97c812013-02-07 02:35:38431 // Release cached statements.
432 statement_cache_.clear();
433
434 // With cached statements released, in-use statements will remain.
435 // Closing the database while statements are in use is an API
436 // violation, except for forced close (which happens from within a
437 // statement's error handler).
438 DCHECK(forced || open_statements_.empty());
439
440 // Deactivate any outstanding statements so sqlite3_close() works.
441 for (StatementRefSet::iterator i = open_statements_.begin();
442 i != open_statements_.end(); ++i)
443 (*i)->Close(forced);
444 open_statements_.clear();
[email protected]4b350052012-02-24 20:40:48445
[email protected]e5ffd0e42009-09-11 21:30:56446 if (db_) {
[email protected]35f7e5392012-07-27 19:54:50447 // Call to AssertIOAllowed() cannot go at the beginning of the function
448 // because Close() must be called from destructor to clean
449 // statement_cache_, it won't cause any disk access and it most probably
450 // will happen on thread not allowing disk access.
451 // TODO([email protected]): This should move to the beginning
452 // of the function. http://crbug.com/136655.
453 AssertIOAllowed();
[email protected]73fb8d52013-07-24 05:04:28454
ssid3be5b1ec2016-01-13 14:21:57455 // Reseting acquires a lock to ensure no dump is happening on the database
456 // at the same time. Unregister takes ownership of provider and it is safe
457 // since the db is reset. memory_dump_provider_ could be null if db_ was
458 // poisoned.
459 if (memory_dump_provider_) {
460 memory_dump_provider_->ResetDatabase();
461 base::trace_event::MemoryDumpManager::GetInstance()
462 ->UnregisterAndDeleteDumpProviderSoon(
463 std::move(memory_dump_provider_));
464 }
465
[email protected]73fb8d52013-07-24 05:04:28466 int rc = sqlite3_close(db_);
467 if (rc != SQLITE_OK) {
468 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.CloseFailure", rc);
469 DLOG(FATAL) << "sqlite3_close failed: " << GetErrorMessage();
470 }
[email protected]e5ffd0e42009-09-11 21:30:56471 }
[email protected]fed734a2013-07-17 04:45:13472 db_ = NULL;
[email protected]e5ffd0e42009-09-11 21:30:56473}
474
[email protected]41a97c812013-02-07 02:35:38475void Connection::Close() {
476 // If the database was already closed by RazeAndClose(), then no
477 // need to close again. Clear the |poisoned_| bit so that incorrect
478 // API calls are caught.
479 if (poisoned_) {
480 poisoned_ = false;
481 return;
482 }
483
484 CloseInternal(false);
485}
486
[email protected]e5ffd0e42009-09-11 21:30:56487void Connection::Preload() {
[email protected]35f7e5392012-07-27 19:54:50488 AssertIOAllowed();
489
[email protected]e5ffd0e42009-09-11 21:30:56490 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38491 DLOG_IF(FATAL, !poisoned_) << "Cannot preload null db";
[email protected]e5ffd0e42009-09-11 21:30:56492 return;
493 }
494
[email protected]8ada10f2013-12-21 00:42:34495 // Use local settings if provided, otherwise use documented defaults. The
496 // actual results could be fetching via PRAGMA calls.
497 const int page_size = page_size_ ? page_size_ : 1024;
498 sqlite3_int64 preload_size = page_size * (cache_size_ ? cache_size_ : 2000);
499 if (preload_size < 1)
[email protected]e5ffd0e42009-09-11 21:30:56500 return;
501
[email protected]8ada10f2013-12-21 00:42:34502 sqlite3_file* file = NULL;
[email protected]8ada10f2013-12-21 00:42:34503 sqlite3_int64 file_size = 0;
shess5dac334f2015-11-05 20:47:42504 int rc = GetSqlite3FileAndSize(db_, &file, &file_size);
[email protected]8ada10f2013-12-21 00:42:34505 if (rc != SQLITE_OK)
506 return;
507
508 // Don't preload more than the file contains.
509 if (preload_size > file_size)
510 preload_size = file_size;
511
mostynbd82cd9952016-04-11 20:05:34512 std::unique_ptr<char[]> buf(new char[page_size]);
shessde60c5f12015-04-21 17:34:46513 for (sqlite3_int64 pos = 0; pos < preload_size; pos += page_size) {
[email protected]8ada10f2013-12-21 00:42:34514 rc = file->pMethods->xRead(file, buf.get(), page_size, pos);
shessd90aeea82015-11-13 02:24:31515
516 // TODO(shess): Consider calling OnSqliteError().
[email protected]8ada10f2013-12-21 00:42:34517 if (rc != SQLITE_OK)
518 return;
519 }
[email protected]e5ffd0e42009-09-11 21:30:56520}
521
shess7dbd4dee2015-10-06 17:39:16522// SQLite keeps unused pages associated with a connection in a cache. It asks
523// the cache for pages by an id, and if the page is present and the database is
524// unchanged, it considers the content of the page valid and doesn't read it
525// from disk. When memory-mapped I/O is enabled, on read SQLite uses page
526// structures created from the memory map data before consulting the cache. On
527// write SQLite creates a new in-memory page structure, copies the data from the
528// memory map, and later writes it, releasing the updated page back to the
529// cache.
530//
531// This means that in memory-mapped mode, the contents of the cached pages are
532// not re-used for reads, but they are re-used for writes if the re-written page
533// is still in the cache. The implementation of sqlite3_db_release_memory() as
534// of SQLite 3.8.7.4 frees all pages from pcaches associated with the
535// connection, so it should free these pages.
536//
537// Unfortunately, the zero page is also freed. That page is never accessed
538// using memory-mapped I/O, and the cached copy can be re-used after verifying
539// the file change counter on disk. Also, fresh pages from cache receive some
540// pager-level initialization before they can be used. Since the information
541// involved will immediately be accessed in various ways, it is unclear if the
542// additional overhead is material, or just moving processor cache effects
543// around.
544//
545// TODO(shess): It would be better to release the pages immediately when they
546// are no longer needed. This would basically happen after SQLite commits a
547// transaction. I had implemented a pcache wrapper to do this, but it involved
548// layering violations, and it had to be setup before any other sqlite call,
549// which was brittle. Also, for large files it would actually make sense to
550// maintain the existing pcache behavior for blocks past the memory-mapped
551// segment. I think drh would accept a reasonable implementation of the overall
552// concept for upstreaming to SQLite core.
553//
554// TODO(shess): Another possibility would be to set the cache size small, which
555// would keep the zero page around, plus some pre-initialized pages, and SQLite
556// can manage things. The downside is that updates larger than the cache would
557// spill to the journal. That could be compensated by setting cache_spill to
558// false. The downside then is that it allows open-ended use of memory for
559// large transactions.
560//
561// TODO(shess): The TrimMemory() trick of bouncing the cache size would also
562// work. There could be two prepared statements, one for cache_size=1 one for
563// cache_size=goal.
564void Connection::ReleaseCacheMemoryIfNeeded(bool implicit_change_performed) {
shess644fc8a2016-02-26 18:15:58565 // The database could have been closed during a transaction as part of error
566 // recovery.
567 if (!db_) {
568 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
569 return;
570 }
shess7dbd4dee2015-10-06 17:39:16571
572 // If memory-mapping is not enabled, the page cache helps performance.
573 if (!mmap_enabled_)
574 return;
575
576 // On caller request, force the change comparison to fail. Done before the
577 // transaction-nesting test so that the signal can carry to transaction
578 // commit.
579 if (implicit_change_performed)
580 --total_changes_at_last_release_;
581
582 // Cached pages may be re-used within the same transaction.
583 if (transaction_nesting())
584 return;
585
586 // If no changes have been made, skip flushing. This allows the first page of
587 // the database to remain in cache across multiple reads.
588 const int total_changes = sqlite3_total_changes(db_);
589 if (total_changes == total_changes_at_last_release_)
590 return;
591
592 total_changes_at_last_release_ = total_changes;
593 sqlite3_db_release_memory(db_);
594}
595
shessc8cd2a162015-10-22 20:30:46596base::FilePath Connection::DbPath() const {
597 if (!is_open())
598 return base::FilePath();
599
600 const char* path = sqlite3_db_filename(db_, "main");
601 const base::StringPiece db_path(path);
602#if defined(OS_WIN)
603 return base::FilePath(base::UTF8ToWide(db_path));
604#elif defined(OS_POSIX)
605 return base::FilePath(db_path);
606#else
607 NOTREACHED();
608 return base::FilePath();
609#endif
610}
611
612// Data is persisted in a file shared between databases in the same directory.
613// The "sqlite-diag" file contains a dictionary with the version number, and an
614// array of histogram tags for databases which have been dumped.
615bool Connection::RegisterIntentToUpload() const {
616 static const char* kVersionKey = "version";
617 static const char* kDiagnosticDumpsKey = "DiagnosticDumps";
618 static int kVersion = 1;
619
620 AssertIOAllowed();
621
622 if (histogram_tag_.empty())
623 return false;
624
625 if (!is_open())
626 return false;
627
628 if (in_memory_)
629 return false;
630
631 const base::FilePath db_path = DbPath();
632 if (db_path.empty())
633 return false;
634
635 // Put the collection of diagnostic data next to the databases. In most
636 // cases, this is the profile directory, but safe-browsing stores a Cookies
637 // file in the directory above the profile directory.
638 base::FilePath breadcrumb_path(
639 db_path.DirName().Append(FILE_PATH_LITERAL("sqlite-diag")));
640
641 // Lock against multiple updates to the diagnostics file. This code should
642 // seldom be called in the first place, and when called it should seldom be
643 // called for multiple databases, and when called for multiple databases there
644 // is _probably_ something systemic wrong with the user's system. So the lock
645 // should never be contended, but when it is the database experience is
646 // already bad.
647 base::AutoLock lock(g_sqlite_init_lock.Get());
648
mostynbd82cd9952016-04-11 20:05:34649 std::unique_ptr<base::Value> root;
shessc8cd2a162015-10-22 20:30:46650 if (!base::PathExists(breadcrumb_path)) {
mostynbd82cd9952016-04-11 20:05:34651 std::unique_ptr<base::DictionaryValue> root_dict(
652 new base::DictionaryValue());
shessc8cd2a162015-10-22 20:30:46653 root_dict->SetInteger(kVersionKey, kVersion);
654
mostynbd82cd9952016-04-11 20:05:34655 std::unique_ptr<base::ListValue> dumps(new base::ListValue);
shessc8cd2a162015-10-22 20:30:46656 dumps->AppendString(histogram_tag_);
dchenge48600452015-12-28 02:24:50657 root_dict->Set(kDiagnosticDumpsKey, std::move(dumps));
shessc8cd2a162015-10-22 20:30:46658
dchenge48600452015-12-28 02:24:50659 root = std::move(root_dict);
shessc8cd2a162015-10-22 20:30:46660 } else {
661 // Failure to read a valid dictionary implies that something is going wrong
662 // on the system.
663 JSONFileValueDeserializer deserializer(breadcrumb_path);
mostynbd82cd9952016-04-11 20:05:34664 std::unique_ptr<base::Value> read_root(
shessc8cd2a162015-10-22 20:30:46665 deserializer.Deserialize(nullptr, nullptr));
666 if (!read_root.get())
667 return false;
mostynbd82cd9952016-04-11 20:05:34668 std::unique_ptr<base::DictionaryValue> root_dict =
dchenge48600452015-12-28 02:24:50669 base::DictionaryValue::From(std::move(read_root));
shessc8cd2a162015-10-22 20:30:46670 if (!root_dict)
671 return false;
672
673 // Don't upload if the version is missing or newer.
674 int version = 0;
675 if (!root_dict->GetInteger(kVersionKey, &version) || version > kVersion)
676 return false;
677
678 base::ListValue* dumps = nullptr;
679 if (!root_dict->GetList(kDiagnosticDumpsKey, &dumps))
680 return false;
681
682 const size_t size = dumps->GetSize();
683 for (size_t i = 0; i < size; ++i) {
684 std::string s;
685
686 // Don't upload if the value isn't a string, or indicates a prior upload.
687 if (!dumps->GetString(i, &s) || s == histogram_tag_)
688 return false;
689 }
690
691 // Record intention to proceed with upload.
692 dumps->AppendString(histogram_tag_);
dchenge48600452015-12-28 02:24:50693 root = std::move(root_dict);
shessc8cd2a162015-10-22 20:30:46694 }
695
696 const base::FilePath breadcrumb_new =
697 breadcrumb_path.AddExtension(FILE_PATH_LITERAL("new"));
698 base::DeleteFile(breadcrumb_new, false);
699
700 // No upload if the breadcrumb file cannot be updated.
701 // TODO(shess): Consider ImportantFileWriter::WriteFileAtomically() to land
702 // the data on disk. For now, losing the data is not a big problem, so the
703 // sync overhead would probably not be worth it.
704 JSONFileValueSerializer serializer(breadcrumb_new);
705 if (!serializer.Serialize(*root))
706 return false;
707 if (!base::PathExists(breadcrumb_new))
708 return false;
709 if (!base::ReplaceFile(breadcrumb_new, breadcrumb_path, nullptr)) {
710 base::DeleteFile(breadcrumb_new, false);
711 return false;
712 }
713
714 return true;
715}
716
717std::string Connection::CollectErrorInfo(int error, Statement* stmt) const {
718 // Buffer for accumulating debugging info about the error. Place
719 // more-relevant information earlier, in case things overflow the
720 // fixed-size reporting buffer.
721 std::string debug_info;
722
723 // The error message from the failed operation.
724 base::StringAppendF(&debug_info, "db error: %d/%s\n",
725 GetErrorCode(), GetErrorMessage());
726
727 // TODO(shess): |error| and |GetErrorCode()| should always be the same, but
728 // reading code does not entirely convince me. Remove if they turn out to be
729 // the same.
730 if (error != GetErrorCode())
731 base::StringAppendF(&debug_info, "reported error: %d\n", error);
732
733 // System error information. Interpretation of Windows errors is different
734 // from posix.
735#if defined(OS_WIN)
736 base::StringAppendF(&debug_info, "LastError: %d\n", GetLastErrno());
737#elif defined(OS_POSIX)
738 base::StringAppendF(&debug_info, "errno: %d\n", GetLastErrno());
739#else
740 NOTREACHED(); // Add appropriate log info.
741#endif
742
743 if (stmt) {
744 base::StringAppendF(&debug_info, "statement: %s\n",
745 stmt->GetSQLStatement());
746 } else {
747 base::StringAppendF(&debug_info, "statement: NULL\n");
748 }
749
750 // SQLITE_ERROR often indicates some sort of mismatch between the statement
751 // and the schema, possibly due to a failed schema migration.
752 if (error == SQLITE_ERROR) {
753 const char* kVersionSql = "SELECT value FROM meta WHERE key = 'version'";
754 sqlite3_stmt* s;
755 int rc = sqlite3_prepare_v2(db_, kVersionSql, -1, &s, nullptr);
756 if (rc == SQLITE_OK) {
757 rc = sqlite3_step(s);
758 if (rc == SQLITE_ROW) {
759 base::StringAppendF(&debug_info, "version: %d\n",
760 sqlite3_column_int(s, 0));
761 } else if (rc == SQLITE_DONE) {
762 debug_info += "version: none\n";
763 } else {
764 base::StringAppendF(&debug_info, "version: error %d\n", rc);
765 }
766 sqlite3_finalize(s);
767 } else {
768 base::StringAppendF(&debug_info, "version: prepare error %d\n", rc);
769 }
770
771 debug_info += "schema:\n";
772
773 // sqlite_master has columns:
774 // type - "index" or "table".
775 // name - name of created element.
776 // tbl_name - name of element, or target table in case of index.
777 // rootpage - root page of the element in database file.
778 // sql - SQL to create the element.
779 // In general, the |sql| column is sufficient to derive the other columns.
780 // |rootpage| is not interesting for debugging, without the contents of the
781 // database. The COALESCE is because certain automatic elements will have a
782 // |name| but no |sql|,
783 const char* kSchemaSql = "SELECT COALESCE(sql, name) FROM sqlite_master";
784 rc = sqlite3_prepare_v2(db_, kSchemaSql, -1, &s, nullptr);
785 if (rc == SQLITE_OK) {
786 while ((rc = sqlite3_step(s)) == SQLITE_ROW) {
787 base::StringAppendF(&debug_info, "%s\n", sqlite3_column_text(s, 0));
788 }
789 if (rc != SQLITE_DONE)
790 base::StringAppendF(&debug_info, "error %d\n", rc);
791 sqlite3_finalize(s);
792 } else {
793 base::StringAppendF(&debug_info, "prepare error %d\n", rc);
794 }
795 }
796
797 return debug_info;
798}
799
800// TODO(shess): Since this is only called in an error situation, it might be
801// prudent to rewrite in terms of SQLite API calls, and mark the function const.
802std::string Connection::CollectCorruptionInfo() {
803 AssertIOAllowed();
804
805 // If the file cannot be accessed it is unlikely that an integrity check will
806 // turn up actionable information.
807 const base::FilePath db_path = DbPath();
avi0b519202015-12-21 07:25:19808 int64_t db_size = -1;
shessc8cd2a162015-10-22 20:30:46809 if (!base::GetFileSize(db_path, &db_size) || db_size < 0)
810 return std::string();
811
812 // Buffer for accumulating debugging info about the error. Place
813 // more-relevant information earlier, in case things overflow the
814 // fixed-size reporting buffer.
815 std::string debug_info;
816 base::StringAppendF(&debug_info, "SQLITE_CORRUPT, db size %" PRId64 "\n",
817 db_size);
818
819 // Only check files up to 8M to keep things from blocking too long.
avi0b519202015-12-21 07:25:19820 const int64_t kMaxIntegrityCheckSize = 8192 * 1024;
shessc8cd2a162015-10-22 20:30:46821 if (db_size > kMaxIntegrityCheckSize) {
822 debug_info += "integrity_check skipped due to size\n";
823 } else {
824 std::vector<std::string> messages;
825
826 // TODO(shess): FullIntegrityCheck() splits into a vector while this joins
827 // into a string. Probably should be refactored.
828 const base::TimeTicks before = base::TimeTicks::Now();
829 FullIntegrityCheck(&messages);
830 base::StringAppendF(
831 &debug_info,
832 "integrity_check %" PRId64 " ms, %" PRIuS " records:\n",
833 (base::TimeTicks::Now() - before).InMilliseconds(),
834 messages.size());
835
836 // SQLite returns up to 100 messages by default, trim deeper to
837 // keep close to the 2000-character size limit for dumping.
838 const size_t kMaxMessages = 20;
839 for (size_t i = 0; i < kMaxMessages && i < messages.size(); ++i) {
840 base::StringAppendF(&debug_info, "%s\n", messages[i].c_str());
841 }
842 }
843
844 return debug_info;
845}
846
shessa62504d2016-11-07 19:26:12847bool Connection::GetMmapAltStatus(int64_t* status) {
848 // The [meta] version uses a missing table as a signal for a fresh database.
849 // That will not work for the view, which would not exist in either a new or
850 // an existing database. A new database _should_ be only one page long, so
851 // just don't bother optimizing this case (start at offset 0).
852 // TODO(shess): Could the [meta] case also get simpler, then?
853 if (!DoesViewExist("MmapStatus")) {
854 *status = 0;
855 return true;
856 }
857
858 const char* kMmapStatusSql = "SELECT * FROM MmapStatus";
859 Statement s(GetUniqueStatement(kMmapStatusSql));
860 if (s.Step())
861 *status = s.ColumnInt64(0);
862 return s.Succeeded();
863}
864
865bool Connection::SetMmapAltStatus(int64_t status) {
866 if (!BeginTransaction())
867 return false;
868
869 // View may not exist on first run.
870 if (!Execute("DROP VIEW IF EXISTS MmapStatus")) {
871 RollbackTransaction();
872 return false;
873 }
874
875 // Views live in the schema, so they cannot be parameterized. For an integer
876 // value, this construct should be safe from SQL injection, if the value
877 // becomes more complicated use "SELECT quote(?)" to generate a safe quoted
878 // value.
879 const std::string createViewSql =
880 base::StringPrintf("CREATE VIEW MmapStatus (value) AS SELECT %" PRId64,
881 status);
882 if (!Execute(createViewSql.c_str())) {
883 RollbackTransaction();
884 return false;
885 }
886
887 return CommitTransaction();
888}
889
shessd90aeea82015-11-13 02:24:31890size_t Connection::GetAppropriateMmapSize() {
891 AssertIOAllowed();
892
rohitrao83d6b83a2016-06-21 07:25:57893#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
894 if (!base::ios::IsRunningOnIOS10OrLater()) {
895 // iOS SQLite does not support memory mapping.
896 return 0;
897 }
shessd90aeea82015-11-13 02:24:31898#endif
899
shess9bf2c672015-12-18 01:18:08900 // How much to map if no errors are found. 50MB encompasses the 99th
901 // percentile of Chrome databases in the wild, so this should be good.
902 const size_t kMmapEverything = 256 * 1024 * 1024;
903
shessa62504d2016-11-07 19:26:12904 // Progress information is tracked in the [meta] table for databases which use
905 // sql::MetaTable, otherwise it is tracked in a special view.
906 // TODO(shess): Move all cases to the view implementation.
shess9bf2c672015-12-18 01:18:08907 int64_t mmap_ofs = 0;
shessa62504d2016-11-07 19:26:12908 if (mmap_alt_status_) {
909 if (!GetMmapAltStatus(&mmap_ofs)) {
910 RecordOneEvent(EVENT_MMAP_STATUS_FAILURE_READ);
911 return 0;
912 }
913 } else {
914 // If [meta] doesn't exist, yet, it's a new database, assume the best.
915 // sql::MetaTable::Init() will preload kMmapSuccess.
916 if (!MetaTable::DoesTableExist(this)) {
917 RecordOneEvent(EVENT_MMAP_META_MISSING);
918 return kMmapEverything;
919 }
920
921 if (!MetaTable::GetMmapStatus(this, &mmap_ofs)) {
922 RecordOneEvent(EVENT_MMAP_META_FAILURE_READ);
923 return 0;
924 }
shessd90aeea82015-11-13 02:24:31925 }
926
927 // Database read failed in the past, don't memory map.
shess9bf2c672015-12-18 01:18:08928 if (mmap_ofs == MetaTable::kMmapFailure) {
shessd90aeea82015-11-13 02:24:31929 RecordOneEvent(EVENT_MMAP_FAILED);
930 return 0;
shess9bf2c672015-12-18 01:18:08931 } else if (mmap_ofs != MetaTable::kMmapSuccess) {
shessd90aeea82015-11-13 02:24:31932 // Continue reading from previous offset.
933 DCHECK_GE(mmap_ofs, 0);
934
935 // TODO(shess): Could this reading code be shared with Preload()? It would
936 // require locking twice (this code wouldn't be able to access |db_size| so
937 // the helper would have to return amount read).
938
939 // Read more of the database looking for errors. The VFS interface is used
940 // to assure that the reads are valid for SQLite. |g_reads_allowed| is used
941 // to limit checking to 20MB per run of Chromium.
942 sqlite3_file* file = NULL;
943 sqlite3_int64 db_size = 0;
944 if (SQLITE_OK != GetSqlite3FileAndSize(db_, &file, &db_size)) {
945 RecordOneEvent(EVENT_MMAP_VFS_FAILURE);
946 return 0;
947 }
948
949 // Read the data left, or |g_reads_allowed|, whichever is smaller.
950 // |g_reads_allowed| limits the total amount of I/O to spend verifying data
951 // in a single Chromium run.
952 sqlite3_int64 amount = db_size - mmap_ofs;
953 if (amount < 0)
954 amount = 0;
955 if (amount > 0) {
956 base::AutoLock lock(g_sqlite_init_lock.Get());
957 static sqlite3_int64 g_reads_allowed = 20 * 1024 * 1024;
958 if (g_reads_allowed < amount)
959 amount = g_reads_allowed;
960 g_reads_allowed -= amount;
961 }
962
963 // |amount| can be <= 0 if |g_reads_allowed| ran out of quota, or if the
964 // database was truncated after a previous pass.
965 if (amount <= 0 && mmap_ofs < db_size) {
966 DCHECK_EQ(0, amount);
967 RecordOneEvent(EVENT_MMAP_SUCCESS_NO_PROGRESS);
968 } else {
969 static const int kPageSize = 4096;
970 char buf[kPageSize];
971 while (amount > 0) {
972 int rc = file->pMethods->xRead(file, buf, sizeof(buf), mmap_ofs);
973 if (rc == SQLITE_OK) {
974 mmap_ofs += sizeof(buf);
975 amount -= sizeof(buf);
976 } else if (rc == SQLITE_IOERR_SHORT_READ) {
977 // Reached EOF for a database with page size < |kPageSize|.
978 mmap_ofs = db_size;
979 break;
980 } else {
981 // TODO(shess): Consider calling OnSqliteError().
shess9bf2c672015-12-18 01:18:08982 mmap_ofs = MetaTable::kMmapFailure;
shessd90aeea82015-11-13 02:24:31983 break;
984 }
985 }
986
987 // Log these events after update to distinguish meta update failure.
988 Events event;
989 if (mmap_ofs >= db_size) {
shess9bf2c672015-12-18 01:18:08990 mmap_ofs = MetaTable::kMmapSuccess;
shessd90aeea82015-11-13 02:24:31991 event = EVENT_MMAP_SUCCESS_NEW;
992 } else if (mmap_ofs > 0) {
993 event = EVENT_MMAP_SUCCESS_PARTIAL;
994 } else {
shess9bf2c672015-12-18 01:18:08995 DCHECK_EQ(MetaTable::kMmapFailure, mmap_ofs);
shessd90aeea82015-11-13 02:24:31996 event = EVENT_MMAP_FAILED_NEW;
997 }
998
shessa62504d2016-11-07 19:26:12999 if (mmap_alt_status_) {
1000 if (!SetMmapAltStatus(mmap_ofs)) {
1001 RecordOneEvent(EVENT_MMAP_STATUS_FAILURE_UPDATE);
1002 return 0;
1003 }
1004 } else {
1005 if (!MetaTable::SetMmapStatus(this, mmap_ofs)) {
1006 RecordOneEvent(EVENT_MMAP_META_FAILURE_UPDATE);
1007 return 0;
1008 }
shessd90aeea82015-11-13 02:24:311009 }
1010
1011 RecordOneEvent(event);
1012 }
1013 }
1014
shess9bf2c672015-12-18 01:18:081015 if (mmap_ofs == MetaTable::kMmapFailure)
shessd90aeea82015-11-13 02:24:311016 return 0;
shess9bf2c672015-12-18 01:18:081017 if (mmap_ofs == MetaTable::kMmapSuccess)
1018 return kMmapEverything;
shessd90aeea82015-11-13 02:24:311019 return mmap_ofs;
1020}
1021
[email protected]be7995f12013-07-18 18:49:141022void Connection::TrimMemory(bool aggressively) {
1023 if (!db_)
1024 return;
1025
1026 // TODO(shess): investigate using sqlite3_db_release_memory() when possible.
1027 int original_cache_size;
1028 {
1029 Statement sql_get_original(GetUniqueStatement("PRAGMA cache_size"));
1030 if (!sql_get_original.Step()) {
1031 DLOG(WARNING) << "Could not get cache size " << GetErrorMessage();
1032 return;
1033 }
1034 original_cache_size = sql_get_original.ColumnInt(0);
1035 }
1036 int shrink_cache_size = aggressively ? 1 : (original_cache_size / 2);
1037
1038 // Force sqlite to try to reduce page cache usage.
1039 const std::string sql_shrink =
1040 base::StringPrintf("PRAGMA cache_size=%d", shrink_cache_size);
1041 if (!Execute(sql_shrink.c_str()))
1042 DLOG(WARNING) << "Could not shrink cache size: " << GetErrorMessage();
1043
1044 // Restore cache size.
1045 const std::string sql_restore =
1046 base::StringPrintf("PRAGMA cache_size=%d", original_cache_size);
1047 if (!Execute(sql_restore.c_str()))
1048 DLOG(WARNING) << "Could not restore cache size: " << GetErrorMessage();
1049}
1050
[email protected]8e0c01282012-04-06 19:36:491051// Create an in-memory database with the existing database's page
1052// size, then backup that database over the existing database.
1053bool Connection::Raze() {
[email protected]35f7e5392012-07-27 19:54:501054 AssertIOAllowed();
1055
[email protected]8e0c01282012-04-06 19:36:491056 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381057 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:491058 return false;
1059 }
1060
1061 if (transaction_nesting_ > 0) {
1062 DLOG(FATAL) << "Cannot raze within a transaction";
1063 return false;
1064 }
1065
1066 sql::Connection null_db;
1067 if (!null_db.OpenInMemory()) {
1068 DLOG(FATAL) << "Unable to open in-memory database.";
1069 return false;
1070 }
1071
[email protected]6d42f152012-11-10 00:38:241072 if (page_size_) {
1073 // Enforce SQLite restrictions on |page_size_|.
1074 DCHECK(!(page_size_ & (page_size_ - 1)))
1075 << " page_size_ " << page_size_ << " is not a power of two.";
1076 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
1077 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:041078 const std::string sql =
1079 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]69c58452012-08-06 19:22:421080 if (!null_db.Execute(sql.c_str()))
1081 return false;
1082 }
1083
[email protected]6d42f152012-11-10 00:38:241084#if defined(OS_ANDROID)
1085 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
1086 // in-memory databases do not respect this define.
1087 // TODO(shess): Figure out a way to set this without using platform
1088 // specific code. AFAICT from sqlite3.c, the only way to do it
1089 // would be to create an actual filesystem database, which is
1090 // unfortunate.
1091 if (!null_db.Execute("PRAGMA auto_vacuum = 1"))
1092 return false;
1093#endif
[email protected]8e0c01282012-04-06 19:36:491094
1095 // The page size doesn't take effect until a database has pages, and
1096 // at this point the null database has none. Changing the schema
1097 // version will create the first page. This will not affect the
1098 // schema version in the resulting database, as SQLite's backup
1099 // implementation propagates the schema version from the original
1100 // connection to the new version of the database, incremented by one
1101 // so that other readers see the schema change and act accordingly.
1102 if (!null_db.Execute("PRAGMA schema_version = 1"))
1103 return false;
1104
[email protected]6d42f152012-11-10 00:38:241105 // SQLite tracks the expected number of database pages in the first
1106 // page, and if it does not match the total retrieved from a
1107 // filesystem call, treats the database as corrupt. This situation
1108 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
1109 // used to hint to SQLite to soldier on in that case, specifically
1110 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
1111 // sqlite3.c lockBtree().]
1112 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
1113 // page_size" can be used to query such a database.
1114 ScopedWritableSchema writable_schema(db_);
1115
shess92a6fb22017-04-23 04:33:301116#if defined(OS_WIN)
1117 // On Windows, truncate silently fails when applied to memory-mapped files.
1118 // Disable memory-mapping so that the truncate succeeds. Note that other
1119 // connections may have memory-mapped the file, so this may not entirely
1120 // prevent the problem.
1121 // [Source: <https://sqlite.org/mmap.html> plus experiments.]
1122 ignore_result(Execute("PRAGMA mmap_size = 0"));
1123#endif
1124
[email protected]7bae5742013-07-10 20:46:161125 const char* kMain = "main";
1126 int rc = BackupDatabase(null_db.db_, db_, kMain);
1127 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase",rc);
[email protected]8e0c01282012-04-06 19:36:491128
1129 // The destination database was locked.
1130 if (rc == SQLITE_BUSY) {
1131 return false;
1132 }
1133
[email protected]7bae5742013-07-10 20:46:161134 // SQLITE_NOTADB can happen if page 1 of db_ exists, but is not
1135 // formatted correctly. SQLITE_IOERR_SHORT_READ can happen if db_
1136 // isn't even big enough for one page. Either way, reach in and
1137 // truncate it before trying again.
1138 // TODO(shess): Maybe it would be worthwhile to just truncate from
1139 // the get-go?
1140 if (rc == SQLITE_NOTADB || rc == SQLITE_IOERR_SHORT_READ) {
1141 sqlite3_file* file = NULL;
[email protected]8ada10f2013-12-21 00:42:341142 rc = GetSqlite3File(db_, &file);
[email protected]7bae5742013-07-10 20:46:161143 if (rc != SQLITE_OK) {
1144 DLOG(FATAL) << "Failure getting file handle.";
1145 return false;
[email protected]7bae5742013-07-10 20:46:161146 }
1147
1148 rc = file->pMethods->xTruncate(file, 0);
1149 if (rc != SQLITE_OK) {
1150 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabaseTruncate",rc);
1151 DLOG(FATAL) << "Failed to truncate file.";
1152 return false;
1153 }
1154
1155 rc = BackupDatabase(null_db.db_, db_, kMain);
1156 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase2",rc);
1157
1158 if (rc != SQLITE_DONE) {
1159 DLOG(FATAL) << "Failed retrying Raze().";
1160 }
1161 }
1162
[email protected]8e0c01282012-04-06 19:36:491163 // The entire database should have been backed up.
1164 if (rc != SQLITE_DONE) {
[email protected]7bae5742013-07-10 20:46:161165 // TODO(shess): Figure out which other cases can happen.
[email protected]8e0c01282012-04-06 19:36:491166 DLOG(FATAL) << "Unable to copy entire null database.";
1167 return false;
1168 }
1169
[email protected]8e0c01282012-04-06 19:36:491170 return true;
1171}
1172
1173bool Connection::RazeWithTimout(base::TimeDelta timeout) {
1174 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381175 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:491176 return false;
1177 }
1178
1179 ScopedBusyTimeout busy_timeout(db_);
1180 busy_timeout.SetTimeout(timeout);
1181 return Raze();
1182}
1183
[email protected]41a97c812013-02-07 02:35:381184bool Connection::RazeAndClose() {
1185 if (!db_) {
1186 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
1187 return false;
1188 }
1189
1190 // Raze() cannot run in a transaction.
[email protected]8d409412013-07-19 18:25:301191 RollbackAllTransactions();
[email protected]41a97c812013-02-07 02:35:381192
1193 bool result = Raze();
1194
1195 CloseInternal(true);
1196
1197 // Mark the database so that future API calls fail appropriately,
1198 // but don't DCHECK (because after calling this function they are
1199 // expected to fail).
1200 poisoned_ = true;
1201
1202 return result;
1203}
1204
[email protected]8d409412013-07-19 18:25:301205void Connection::Poison() {
1206 if (!db_) {
1207 DLOG_IF(FATAL, !poisoned_) << "Cannot poison null db";
1208 return;
1209 }
1210
1211 RollbackAllTransactions();
1212 CloseInternal(true);
1213
1214 // Mark the database so that future API calls fail appropriately,
1215 // but don't DCHECK (because after calling this function they are
1216 // expected to fail).
1217 poisoned_ = true;
1218}
1219
[email protected]8d2e39e2013-06-24 05:55:081220// TODO(shess): To the extent possible, figure out the optimal
1221// ordering for these deletes which will prevent other connections
1222// from seeing odd behavior. For instance, it may be necessary to
1223// manually lock the main database file in a SQLite-compatible fashion
1224// (to prevent other processes from opening it), then delete the
1225// journal files, then delete the main database file. Another option
1226// might be to lock the main database file and poison the header with
1227// junk to prevent other processes from opening it successfully (like
1228// Gears "SQLite poison 3" trick).
1229//
1230// static
1231bool Connection::Delete(const base::FilePath& path) {
1232 base::ThreadRestrictions::AssertIOAllowed();
1233
1234 base::FilePath journal_path(path.value() + FILE_PATH_LITERAL("-journal"));
1235 base::FilePath wal_path(path.value() + FILE_PATH_LITERAL("-wal"));
1236
erg102ceb412015-06-20 01:38:131237 std::string journal_str = AsUTF8ForSQL(journal_path);
1238 std::string wal_str = AsUTF8ForSQL(wal_path);
1239 std::string path_str = AsUTF8ForSQL(path);
[email protected]8d2e39e2013-06-24 05:55:081240
shess702467622015-09-16 19:04:551241 // Make sure sqlite3_initialize() is called before anything else.
1242 InitializeSqlite();
1243
erg102ceb412015-06-20 01:38:131244 sqlite3_vfs* vfs = sqlite3_vfs_find(NULL);
1245 CHECK(vfs);
1246 CHECK(vfs->xDelete);
1247 CHECK(vfs->xAccess);
1248
1249 // We only work with unix, win32 and mojo filesystems. If you're trying to
1250 // use this code with any other VFS, you're not in a good place.
1251 CHECK(strncmp(vfs->zName, "unix", 4) == 0 ||
1252 strncmp(vfs->zName, "win32", 5) == 0 ||
1253 strcmp(vfs->zName, "mojo") == 0);
1254
1255 vfs->xDelete(vfs, journal_str.c_str(), 0);
1256 vfs->xDelete(vfs, wal_str.c_str(), 0);
1257 vfs->xDelete(vfs, path_str.c_str(), 0);
1258
1259 int journal_exists = 0;
1260 vfs->xAccess(vfs, journal_str.c_str(), SQLITE_ACCESS_EXISTS,
1261 &journal_exists);
1262
1263 int wal_exists = 0;
1264 vfs->xAccess(vfs, wal_str.c_str(), SQLITE_ACCESS_EXISTS,
1265 &wal_exists);
1266
1267 int path_exists = 0;
1268 vfs->xAccess(vfs, path_str.c_str(), SQLITE_ACCESS_EXISTS,
1269 &path_exists);
1270
1271 return !journal_exists && !wal_exists && !path_exists;
[email protected]8d2e39e2013-06-24 05:55:081272}
1273
[email protected]e5ffd0e42009-09-11 21:30:561274bool Connection::BeginTransaction() {
1275 if (needs_rollback_) {
[email protected]88563f62011-03-13 22:13:331276 DCHECK_GT(transaction_nesting_, 0);
[email protected]e5ffd0e42009-09-11 21:30:561277
1278 // When we're going to rollback, fail on this begin and don't actually
1279 // mark us as entering the nested transaction.
1280 return false;
1281 }
1282
1283 bool success = true;
1284 if (!transaction_nesting_) {
1285 needs_rollback_ = false;
1286
1287 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
shess58b8df82015-06-03 00:19:321288 RecordOneEvent(EVENT_BEGIN);
[email protected]eff1fa522011-12-12 23:50:591289 if (!begin.Run())
[email protected]e5ffd0e42009-09-11 21:30:561290 return false;
1291 }
1292 transaction_nesting_++;
1293 return success;
1294}
1295
1296void Connection::RollbackTransaction() {
1297 if (!transaction_nesting_) {
[email protected]41a97c812013-02-07 02:35:381298 DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561299 return;
1300 }
1301
1302 transaction_nesting_--;
1303
1304 if (transaction_nesting_ > 0) {
1305 // Mark the outermost transaction as needing rollback.
1306 needs_rollback_ = true;
1307 return;
1308 }
1309
1310 DoRollback();
1311}
1312
1313bool Connection::CommitTransaction() {
1314 if (!transaction_nesting_) {
shess90244e12015-11-09 22:08:181315 DLOG_IF(FATAL, !poisoned_) << "Committing a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561316 return false;
1317 }
1318 transaction_nesting_--;
1319
1320 if (transaction_nesting_ > 0) {
1321 // Mark any nested transactions as failing after we've already got one.
1322 return !needs_rollback_;
1323 }
1324
1325 if (needs_rollback_) {
1326 DoRollback();
1327 return false;
1328 }
1329
1330 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
shess58b8df82015-06-03 00:19:321331
1332 // Collect the commit time manually, sql::Statement would register it as query
1333 // time only.
1334 const base::TimeTicks before = Now();
1335 bool ret = commit.RunWithoutTimers();
1336 const base::TimeDelta delta = Now() - before;
1337
1338 RecordCommitTime(delta);
1339 RecordOneEvent(EVENT_COMMIT);
1340
shess7dbd4dee2015-10-06 17:39:161341 // Release dirty cache pages after the transaction closes.
1342 ReleaseCacheMemoryIfNeeded(false);
1343
shess58b8df82015-06-03 00:19:321344 return ret;
[email protected]e5ffd0e42009-09-11 21:30:561345}
1346
[email protected]8d409412013-07-19 18:25:301347void Connection::RollbackAllTransactions() {
1348 if (transaction_nesting_ > 0) {
1349 transaction_nesting_ = 0;
1350 DoRollback();
1351 }
1352}
1353
1354bool Connection::AttachDatabase(const base::FilePath& other_db_path,
1355 const char* attachment_point) {
1356 DCHECK(ValidAttachmentPoint(attachment_point));
1357
1358 Statement s(GetUniqueStatement("ATTACH DATABASE ? AS ?"));
1359#if OS_WIN
1360 s.BindString16(0, other_db_path.value());
1361#else
1362 s.BindString(0, other_db_path.value());
1363#endif
1364 s.BindString(1, attachment_point);
1365 return s.Run();
1366}
1367
1368bool Connection::DetachDatabase(const char* attachment_point) {
1369 DCHECK(ValidAttachmentPoint(attachment_point));
1370
1371 Statement s(GetUniqueStatement("DETACH DATABASE ?"));
1372 s.BindString(0, attachment_point);
1373 return s.Run();
1374}
1375
shess58b8df82015-06-03 00:19:321376// TODO(shess): Consider changing this to execute exactly one statement. If a
1377// caller wishes to execute multiple statements, that should be explicit, and
1378// perhaps tucked into an explicit transaction with rollback in case of error.
[email protected]eff1fa522011-12-12 23:50:591379int Connection::ExecuteAndReturnErrorCode(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501380 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:381381 if (!db_) {
1382 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
1383 return SQLITE_ERROR;
1384 }
shess58b8df82015-06-03 00:19:321385 DCHECK(sql);
1386
1387 RecordOneEvent(EVENT_EXECUTE);
1388 int rc = SQLITE_OK;
1389 while ((rc == SQLITE_OK) && *sql) {
1390 sqlite3_stmt *stmt = NULL;
1391 const char *leftover_sql;
1392
1393 const base::TimeTicks before = Now();
1394 rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, &leftover_sql);
1395 sql = leftover_sql;
1396
1397 // Stop if an error is encountered.
1398 if (rc != SQLITE_OK)
1399 break;
1400
1401 // This happens if |sql| originally only contained comments or whitespace.
1402 // TODO(shess): Audit to see if this can become a DCHECK(). Having
1403 // extraneous comments and whitespace in the SQL statements increases
1404 // runtime cost and can easily be shifted out to the C++ layer.
1405 if (!stmt)
1406 continue;
1407
1408 // Save for use after statement is finalized.
1409 const bool read_only = !!sqlite3_stmt_readonly(stmt);
1410
1411 RecordOneEvent(Connection::EVENT_STATEMENT_RUN);
1412 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1413 // TODO(shess): Audit to see if this can become a DCHECK. I think PRAGMA
1414 // is the only legitimate case for this.
1415 RecordOneEvent(Connection::EVENT_STATEMENT_ROWS);
1416 }
1417
1418 // sqlite3_finalize() returns SQLITE_OK if the most recent sqlite3_step()
1419 // returned SQLITE_DONE or SQLITE_ROW, otherwise the error code.
1420 rc = sqlite3_finalize(stmt);
1421 if (rc == SQLITE_OK)
1422 RecordOneEvent(Connection::EVENT_STATEMENT_SUCCESS);
1423
1424 // sqlite3_exec() does this, presumably to avoid spinning the parser for
1425 // trailing whitespace.
1426 // TODO(shess): Audit to see if this can become a DCHECK.
brettwb3413062015-06-24 00:39:021427 while (base::IsAsciiWhitespace(*sql)) {
shess58b8df82015-06-03 00:19:321428 sql++;
1429 }
1430
1431 const base::TimeDelta delta = Now() - before;
1432 RecordTimeAndChanges(delta, read_only);
1433 }
shess7dbd4dee2015-10-06 17:39:161434
1435 // Most calls to Execute() modify the database. The main exceptions would be
1436 // calls such as CREATE TABLE IF NOT EXISTS which could modify the database
1437 // but sometimes don't.
1438 ReleaseCacheMemoryIfNeeded(true);
1439
shess58b8df82015-06-03 00:19:321440 return rc;
[email protected]eff1fa522011-12-12 23:50:591441}
1442
1443bool Connection::Execute(const char* sql) {
[email protected]41a97c812013-02-07 02:35:381444 if (!db_) {
1445 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
1446 return false;
1447 }
1448
[email protected]eff1fa522011-12-12 23:50:591449 int error = ExecuteAndReturnErrorCode(sql);
[email protected]473ad792012-11-10 00:55:001450 if (error != SQLITE_OK)
[email protected]2f496b42013-09-26 18:36:581451 error = OnSqliteError(error, NULL, sql);
[email protected]473ad792012-11-10 00:55:001452
[email protected]28fe0ff2012-02-25 00:40:331453 // This needs to be a FATAL log because the error case of arriving here is
1454 // that there's a malformed SQL statement. This can arise in development if
[email protected]4350e322013-06-18 22:18:101455 // a change alters the schema but not all queries adjust. This can happen
1456 // in production if the schema is corrupted.
[email protected]eff1fa522011-12-12 23:50:591457 if (error == SQLITE_ERROR)
[email protected]28fe0ff2012-02-25 00:40:331458 DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage();
[email protected]eff1fa522011-12-12 23:50:591459 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:561460}
1461
[email protected]5b96f3772010-09-28 16:30:571462bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
[email protected]41a97c812013-02-07 02:35:381463 if (!db_) {
1464 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]5b96f3772010-09-28 16:30:571465 return false;
[email protected]41a97c812013-02-07 02:35:381466 }
[email protected]5b96f3772010-09-28 16:30:571467
1468 ScopedBusyTimeout busy_timeout(db_);
1469 busy_timeout.SetTimeout(timeout);
[email protected]eff1fa522011-12-12 23:50:591470 return Execute(sql);
[email protected]5b96f3772010-09-28 16:30:571471}
1472
[email protected]e5ffd0e42009-09-11 21:30:561473bool Connection::HasCachedStatement(const StatementID& id) const {
1474 return statement_cache_.find(id) != statement_cache_.end();
1475}
1476
1477scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement(
1478 const StatementID& id,
1479 const char* sql) {
1480 CachedStatementMap::iterator i = statement_cache_.find(id);
1481 if (i != statement_cache_.end()) {
1482 // Statement is in the cache. It should still be active (we're the only
1483 // one invalidating cached statements, and we'll remove it from the cache
1484 // if we do that. Make sure we reset it before giving out the cached one in
1485 // case it still has some stuff bound.
1486 DCHECK(i->second->is_valid());
1487 sqlite3_reset(i->second->stmt());
1488 return i->second;
1489 }
1490
1491 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
1492 if (statement->is_valid())
1493 statement_cache_[id] = statement; // Only cache valid statements.
1494 return statement;
1495}
1496
1497scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement(
1498 const char* sql) {
shess9e77283d2016-06-13 23:53:201499 return GetStatementImpl(this, sql);
1500}
1501
1502scoped_refptr<Connection::StatementRef> Connection::GetStatementImpl(
1503 sql::Connection* tracking_db, const char* sql) const {
[email protected]35f7e5392012-07-27 19:54:501504 AssertIOAllowed();
shess9e77283d2016-06-13 23:53:201505 DCHECK(sql);
1506 DCHECK(!tracking_db || const_cast<Connection*>(tracking_db)==this);
[email protected]35f7e5392012-07-27 19:54:501507
[email protected]41a97c812013-02-07 02:35:381508 // Return inactive statement.
[email protected]e5ffd0e42009-09-11 21:30:561509 if (!db_)
[email protected]41a97c812013-02-07 02:35:381510 return new StatementRef(NULL, NULL, poisoned_);
[email protected]e5ffd0e42009-09-11 21:30:561511
1512 sqlite3_stmt* stmt = NULL;
[email protected]473ad792012-11-10 00:55:001513 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
1514 if (rc != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:591515 // This is evidence of a syntax error in the incoming SQL.
shess9e77283d2016-06-13 23:53:201516 if (rc == SQLITE_ERROR)
shess193bfb622015-04-10 22:30:021517 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]473ad792012-11-10 00:55:001518
1519 // It could also be database corruption.
[email protected]2f496b42013-09-26 18:36:581520 OnSqliteError(rc, NULL, sql);
[email protected]41a97c812013-02-07 02:35:381521 return new StatementRef(NULL, NULL, false);
[email protected]e5ffd0e42009-09-11 21:30:561522 }
shess9e77283d2016-06-13 23:53:201523 return new StatementRef(tracking_db, stmt, true);
[email protected]e5ffd0e42009-09-11 21:30:561524}
1525
[email protected]2eec0a22012-07-24 01:59:581526scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
1527 const char* sql) const {
shess9e77283d2016-06-13 23:53:201528 return GetStatementImpl(NULL, sql);
[email protected]2eec0a22012-07-24 01:59:581529}
1530
[email protected]92cd00a2013-08-16 11:09:581531std::string Connection::GetSchema() const {
1532 // The ORDER BY should not be necessary, but relying on organic
1533 // order for something like this is questionable.
1534 const char* kSql =
1535 "SELECT type, name, tbl_name, sql "
1536 "FROM sqlite_master ORDER BY 1, 2, 3, 4";
1537 Statement statement(GetUntrackedStatement(kSql));
1538
1539 std::string schema;
1540 while (statement.Step()) {
1541 schema += statement.ColumnString(0);
1542 schema += '|';
1543 schema += statement.ColumnString(1);
1544 schema += '|';
1545 schema += statement.ColumnString(2);
1546 schema += '|';
1547 schema += statement.ColumnString(3);
1548 schema += '\n';
1549 }
1550
1551 return schema;
1552}
1553
[email protected]eff1fa522011-12-12 23:50:591554bool Connection::IsSQLValid(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501555 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:381556 if (!db_) {
1557 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
1558 return false;
1559 }
1560
[email protected]eff1fa522011-12-12 23:50:591561 sqlite3_stmt* stmt = NULL;
1562 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK)
1563 return false;
1564
1565 sqlite3_finalize(stmt);
1566 return true;
1567}
1568
[email protected]e2cadec82011-12-13 02:00:531569bool Connection::DoesIndexExist(const char* index_name) const {
shessa62504d2016-11-07 19:26:121570 return DoesSchemaItemExist(index_name, "index");
[email protected]e2cadec82011-12-13 02:00:531571}
1572
shessa62504d2016-11-07 19:26:121573bool Connection::DoesTableExist(const char* table_name) const {
1574 return DoesSchemaItemExist(table_name, "table");
1575}
1576
1577bool Connection::DoesViewExist(const char* view_name) const {
1578 return DoesSchemaItemExist(view_name, "view");
1579}
1580
1581bool Connection::DoesSchemaItemExist(
[email protected]e2cadec82011-12-13 02:00:531582 const char* name, const char* type) const {
shess92a2ab12015-04-09 01:59:471583 const char* kSql =
1584 "SELECT name FROM sqlite_master WHERE type=? AND name=? COLLATE NOCASE";
[email protected]2eec0a22012-07-24 01:59:581585 Statement statement(GetUntrackedStatement(kSql));
shess92a2ab12015-04-09 01:59:471586
shess976814402016-06-21 06:56:251587 // This can happen if the database is corrupt and the error is a test
1588 // expectation.
shess92a2ab12015-04-09 01:59:471589 if (!statement.is_valid())
1590 return false;
1591
[email protected]e2cadec82011-12-13 02:00:531592 statement.BindString(0, type);
1593 statement.BindString(1, name);
[email protected]28fe0ff2012-02-25 00:40:331594
[email protected]e5ffd0e42009-09-11 21:30:561595 return statement.Step(); // Table exists if any row was returned.
1596}
1597
1598bool Connection::DoesColumnExist(const char* table_name,
[email protected]1ed78a32009-09-15 20:24:171599 const char* column_name) const {
[email protected]e5ffd0e42009-09-11 21:30:561600 std::string sql("PRAGMA TABLE_INFO(");
1601 sql.append(table_name);
1602 sql.append(")");
1603
[email protected]2eec0a22012-07-24 01:59:581604 Statement statement(GetUntrackedStatement(sql.c_str()));
shess92a2ab12015-04-09 01:59:471605
shess976814402016-06-21 06:56:251606 // This can happen if the database is corrupt and the error is a test
1607 // expectation.
shess92a2ab12015-04-09 01:59:471608 if (!statement.is_valid())
1609 return false;
1610
[email protected]e5ffd0e42009-09-11 21:30:561611 while (statement.Step()) {
brettw8a800902015-07-10 18:28:331612 if (base::EqualsCaseInsensitiveASCII(statement.ColumnString(1),
1613 column_name))
[email protected]e5ffd0e42009-09-11 21:30:561614 return true;
1615 }
1616 return false;
1617}
1618
tfarina720d4f32015-05-11 22:31:261619int64_t Connection::GetLastInsertRowId() const {
[email protected]e5ffd0e42009-09-11 21:30:561620 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381621 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]e5ffd0e42009-09-11 21:30:561622 return 0;
1623 }
1624 return sqlite3_last_insert_rowid(db_);
1625}
1626
[email protected]1ed78a32009-09-15 20:24:171627int Connection::GetLastChangeCount() const {
1628 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381629 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]1ed78a32009-09-15 20:24:171630 return 0;
1631 }
1632 return sqlite3_changes(db_);
1633}
1634
[email protected]e5ffd0e42009-09-11 21:30:561635int Connection::GetErrorCode() const {
1636 if (!db_)
1637 return SQLITE_ERROR;
1638 return sqlite3_errcode(db_);
1639}
1640
[email protected]767718e52010-09-21 23:18:491641int Connection::GetLastErrno() const {
1642 if (!db_)
1643 return -1;
1644
1645 int err = 0;
1646 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err))
1647 return -2;
1648
1649 return err;
1650}
1651
[email protected]e5ffd0e42009-09-11 21:30:561652const char* Connection::GetErrorMessage() const {
1653 if (!db_)
1654 return "sql::Connection has no connection.";
1655 return sqlite3_errmsg(db_);
1656}
1657
[email protected]fed734a2013-07-17 04:45:131658bool Connection::OpenInternal(const std::string& file_name,
1659 Connection::Retry retry_flag) {
[email protected]35f7e5392012-07-27 19:54:501660 AssertIOAllowed();
1661
[email protected]9cfbc922009-11-17 20:13:171662 if (db_) {
[email protected]eff1fa522011-12-12 23:50:591663 DLOG(FATAL) << "sql::Connection is already open.";
[email protected]9cfbc922009-11-17 20:13:171664 return false;
1665 }
1666
[email protected]a7ec1292013-07-22 22:02:181667 // Make sure sqlite3_initialize() is called before anything else.
1668 InitializeSqlite();
1669
shess58b8df82015-06-03 00:19:321670 // Setup the stats histograms immediately rather than allocating lazily.
1671 // Connections which won't exercise all of these probably shouldn't exist.
1672 if (!histogram_tag_.empty()) {
1673 stats_histogram_ =
1674 base::LinearHistogram::FactoryGet(
1675 "Sqlite.Stats." + histogram_tag_,
1676 1, EVENT_MAX_VALUE, EVENT_MAX_VALUE + 1,
1677 base::HistogramBase::kUmaTargetedHistogramFlag);
1678
1679 // The timer setup matches UMA_HISTOGRAM_MEDIUM_TIMES(). 3 minutes is an
1680 // unreasonable time for any single operation, so there is not much value to
1681 // knowing if it was 3 minutes or 5 minutes. In reality at that point
1682 // things are entirely busted.
1683 commit_time_histogram_ =
1684 GetMediumTimeHistogram("Sqlite.CommitTime." + histogram_tag_);
1685
1686 autocommit_time_histogram_ =
1687 GetMediumTimeHistogram("Sqlite.AutoCommitTime." + histogram_tag_);
1688
1689 update_time_histogram_ =
1690 GetMediumTimeHistogram("Sqlite.UpdateTime." + histogram_tag_);
1691
1692 query_time_histogram_ =
1693 GetMediumTimeHistogram("Sqlite.QueryTime." + histogram_tag_);
1694 }
1695
[email protected]41a97c812013-02-07 02:35:381696 // If |poisoned_| is set, it means an error handler called
1697 // RazeAndClose(). Until regular Close() is called, the caller
1698 // should be treating the database as open, but is_open() currently
1699 // only considers the sqlite3 handle's state.
1700 // TODO(shess): Revise is_open() to consider poisoned_, and review
1701 // to see if any non-testing code even depends on it.
1702 DLOG_IF(FATAL, poisoned_) << "sql::Connection is already open.";
[email protected]7bae5742013-07-10 20:46:161703 poisoned_ = false;
[email protected]41a97c812013-02-07 02:35:381704
shess5f2c3442017-01-24 02:15:101705 // Custom memory-mapping VFS which reads pages using regular I/O on first hit.
1706 sqlite3_vfs* vfs = VFSWrapper();
1707 const char* vfs_name = (vfs ? vfs->zName : nullptr);
1708 int err = sqlite3_open_v2(file_name.c_str(), &db_,
1709 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
1710 vfs_name);
[email protected]765b44502009-10-02 05:01:421711 if (err != SQLITE_OK) {
[email protected]73fb8d52013-07-24 05:04:281712 // Extended error codes cannot be enabled until a handle is
1713 // available, fetch manually.
1714 err = sqlite3_extended_errcode(db_);
1715
[email protected]bd2ccdb4a2012-12-07 22:14:501716 // Histogram failures specific to initial open for debugging
1717 // purposes.
[email protected]73fb8d52013-07-24 05:04:281718 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenFailure", err);
[email protected]bd2ccdb4a2012-12-07 22:14:501719
[email protected]2f496b42013-09-26 18:36:581720 OnSqliteError(err, NULL, "-- sqlite3_open()");
[email protected]fed734a2013-07-17 04:45:131721 bool was_poisoned = poisoned_;
[email protected]64021042012-02-10 20:02:291722 Close();
[email protected]fed734a2013-07-17 04:45:131723
1724 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1725 return OpenInternal(file_name, NO_RETRY);
[email protected]765b44502009-10-02 05:01:421726 return false;
1727 }
1728
[email protected]81a2a602013-07-17 19:10:361729 // TODO(shess): OS_WIN support?
Kevin Marshalla9f05ec2017-07-14 02:10:051730#if defined(OS_POSIX) && !defined(OS_FUCHSIA)
[email protected]81a2a602013-07-17 19:10:361731 if (restrict_to_user_) {
1732 DCHECK_NE(file_name, std::string(":memory"));
1733 base::FilePath file_path(file_name);
1734 int mode = 0;
1735 // TODO(shess): Arguably, failure to retrieve and change
1736 // permissions should be fatal if the file exists.
[email protected]b264eab2013-11-27 23:22:081737 if (base::GetPosixFilePermissions(file_path, &mode)) {
1738 mode &= base::FILE_PERMISSION_USER_MASK;
1739 base::SetPosixFilePermissions(file_path, mode);
[email protected]81a2a602013-07-17 19:10:361740
1741 // SQLite sets the permissions on these files from the main
1742 // database on create. Set them here in case they already exist
1743 // at this point. Failure to set these permissions should not
1744 // be fatal unless the file doesn't exist.
1745 base::FilePath journal_path(file_name + FILE_PATH_LITERAL("-journal"));
1746 base::FilePath wal_path(file_name + FILE_PATH_LITERAL("-wal"));
[email protected]b264eab2013-11-27 23:22:081747 base::SetPosixFilePermissions(journal_path, mode);
1748 base::SetPosixFilePermissions(wal_path, mode);
[email protected]81a2a602013-07-17 19:10:361749 }
1750 }
Kevin Marshalla9f05ec2017-07-14 02:10:051751#endif // defined(OS_POSIX) && !defined(OS_FUCHSIA)
[email protected]81a2a602013-07-17 19:10:361752
[email protected]affa2da2013-06-06 22:20:341753 // SQLite uses a lookaside buffer to improve performance of small mallocs.
1754 // Chromium already depends on small mallocs being efficient, so we disable
1755 // this to avoid the extra memory overhead.
1756 // This must be called immediatly after opening the database before any SQL
1757 // statements are run.
1758 sqlite3_db_config(db_, SQLITE_DBCONFIG_LOOKASIDE, NULL, 0, 0);
1759
[email protected]73fb8d52013-07-24 05:04:281760 // Enable extended result codes to provide more color on I/O errors.
1761 // Not having extended result codes is not a fatal problem, as
1762 // Chromium code does not attempt to handle I/O errors anyhow. The
1763 // current implementation always returns SQLITE_OK, the DCHECK is to
1764 // quickly notify someone if SQLite changes.
1765 err = sqlite3_extended_result_codes(db_, 1);
1766 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
1767
shessbccd300e2016-08-20 00:06:561768 // sqlite3_open() does not actually read the database file (unless a hot
1769 // journal is found). Successfully executing this pragma on an existing
1770 // database requires a valid header on page 1. ExecuteAndReturnErrorCode() to
1771 // get the error code before error callback (potentially) overwrites.
[email protected]bd2ccdb4a2012-12-07 22:14:501772 // TODO(shess): For now, just probing to see what the lay of the
1773 // land is. If it's mostly SQLITE_NOTADB, then the database should
1774 // be razed.
1775 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
shessbccd300e2016-08-20 00:06:561776 if (err != SQLITE_OK) {
[email protected]73fb8d52013-07-24 05:04:281777 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenProbeFailure", err);
shessbccd300e2016-08-20 00:06:561778 OnSqliteError(err, nullptr, "PRAGMA auto_vacuum");
1779
1780 // Retry or bail out if the error handler poisoned the handle.
1781 // TODO(shess): Move this handling to one place (see also sqlite3_open and
1782 // secure_delete). Possibly a wrapper function?
1783 if (poisoned_) {
1784 Close();
1785 if (retry_flag == RETRY_ON_POISON)
1786 return OpenInternal(file_name, NO_RETRY);
1787 return false;
1788 }
1789 }
[email protected]658f8332010-09-18 04:40:431790
[email protected]2e1cee762013-07-09 14:40:001791#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
1792 // The version of SQLite shipped with iOS doesn't enable ICU, which includes
1793 // REGEXP support. Add it in dynamically.
1794 err = sqlite3IcuInit(db_);
1795 DCHECK_EQ(err, SQLITE_OK) << "Could not enable ICU support";
1796#endif // OS_IOS && USE_SYSTEM_SQLITE
1797
[email protected]5b96f3772010-09-28 16:30:571798 // If indicated, lock up the database before doing anything else, so
1799 // that the following code doesn't have to deal with locking.
1800 // TODO(shess): This code is brittle. Find the cases where code
1801 // doesn't request |exclusive_locking_| and audit that it does the
1802 // right thing with SQLITE_BUSY, and that it doesn't make
1803 // assumptions about who might change things in the database.
1804 // http://crbug.com/56559
1805 if (exclusive_locking_) {
[email protected]4350e322013-06-18 22:18:101806 // TODO(shess): This should probably be a failure. Code which
1807 // requests exclusive locking but doesn't get it is almost certain
1808 // to be ill-tested.
1809 ignore_result(Execute("PRAGMA locking_mode=EXCLUSIVE"));
[email protected]5b96f3772010-09-28 16:30:571810 }
1811
[email protected]4e179ba2012-03-17 16:06:471812 // http://www.sqlite.org/pragma.html#pragma_journal_mode
1813 // DELETE (default) - delete -journal file to commit.
1814 // TRUNCATE - truncate -journal file to commit.
1815 // PERSIST - zero out header of -journal file to commit.
shess2c21ecf2015-06-02 01:31:091816 // TRUNCATE should be faster than DELETE because it won't need directory
1817 // changes for each transaction. PERSIST may break the spirit of using
1818 // secure_delete.
1819 ignore_result(Execute("PRAGMA journal_mode = TRUNCATE"));
[email protected]4e179ba2012-03-17 16:06:471820
[email protected]c68ce172011-11-24 22:30:271821 const base::TimeDelta kBusyTimeout =
1822 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
1823
[email protected]765b44502009-10-02 05:01:421824 if (page_size_ != 0) {
[email protected]5b96f3772010-09-28 16:30:571825 // Enforce SQLite restrictions on |page_size_|.
1826 DCHECK(!(page_size_ & (page_size_ - 1)))
1827 << " page_size_ " << page_size_ << " is not a power of two.";
[email protected]6d42f152012-11-10 00:38:241828 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
[email protected]5b96f3772010-09-28 16:30:571829 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:041830 const std::string sql =
1831 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]4350e322013-06-18 22:18:101832 ignore_result(ExecuteWithTimeout(sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421833 }
1834
1835 if (cache_size_ != 0) {
[email protected]7d3cbc92013-03-18 22:33:041836 const std::string sql =
1837 base::StringPrintf("PRAGMA cache_size=%d", cache_size_);
[email protected]4350e322013-06-18 22:18:101838 ignore_result(ExecuteWithTimeout(sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421839 }
1840
[email protected]6e0b1442011-08-09 23:23:581841 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) {
[email protected]fed734a2013-07-17 04:45:131842 bool was_poisoned = poisoned_;
[email protected]6e0b1442011-08-09 23:23:581843 Close();
[email protected]fed734a2013-07-17 04:45:131844 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1845 return OpenInternal(file_name, NO_RETRY);
[email protected]6e0b1442011-08-09 23:23:581846 return false;
1847 }
1848
shess5dac334f2015-11-05 20:47:421849 // Set a reasonable chunk size for larger files. This reduces churn from
1850 // remapping memory on size changes. It also reduces filesystem
1851 // fragmentation.
1852 // TODO(shess): It may make sense to have this be hinted by the client.
1853 // Database sizes seem to be bimodal, some clients have consistently small
1854 // databases (<20k) while other clients have a broad distribution of sizes
1855 // (hundreds of kilobytes to many megabytes).
1856 sqlite3_file* file = NULL;
1857 sqlite3_int64 db_size = 0;
1858 int rc = GetSqlite3FileAndSize(db_, &file, &db_size);
1859 if (rc == SQLITE_OK && db_size > 16 * 1024) {
1860 int chunk_size = 4 * 1024;
1861 if (db_size > 128 * 1024)
1862 chunk_size = 32 * 1024;
1863 sqlite3_file_control(db_, NULL, SQLITE_FCNTL_CHUNK_SIZE, &chunk_size);
1864 }
1865
shess2f3a814b2015-11-05 18:11:101866 // Enable memory-mapped access. The explicit-disable case is because SQLite
shessd90aeea82015-11-13 02:24:311867 // can be built to default-enable mmap. GetAppropriateMmapSize() calculates a
1868 // safe range to memory-map based on past regular I/O. This value will be
1869 // capped by SQLITE_MAX_MMAP_SIZE, which could be different between 32-bit and
1870 // 64-bit platforms.
1871 size_t mmap_size = mmap_disabled_ ? 0 : GetAppropriateMmapSize();
1872 std::string mmap_sql =
1873 base::StringPrintf("PRAGMA mmap_size = %" PRIuS, mmap_size);
1874 ignore_result(Execute(mmap_sql.c_str()));
shess2f3a814b2015-11-05 18:11:101875
1876 // Determine if memory-mapping has actually been enabled. The Execute() above
1877 // can succeed without changing the amount mapped.
1878 mmap_enabled_ = false;
1879 {
1880 Statement s(GetUniqueStatement("PRAGMA mmap_size"));
1881 if (s.Step() && s.ColumnInt64(0) > 0)
1882 mmap_enabled_ = true;
1883 }
1884
ssid3be5b1ec2016-01-13 14:21:571885 DCHECK(!memory_dump_provider_);
1886 memory_dump_provider_.reset(
1887 new ConnectionMemoryDumpProvider(db_, histogram_tag_));
1888 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
1889 memory_dump_provider_.get(), "sql::Connection", nullptr);
1890
[email protected]765b44502009-10-02 05:01:421891 return true;
1892}
1893
[email protected]e5ffd0e42009-09-11 21:30:561894void Connection::DoRollback() {
1895 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
shess58b8df82015-06-03 00:19:321896
1897 // Collect the rollback time manually, sql::Statement would register it as
1898 // query time only.
1899 const base::TimeTicks before = Now();
1900 rollback.RunWithoutTimers();
1901 const base::TimeDelta delta = Now() - before;
1902
1903 RecordUpdateTime(delta);
1904 RecordOneEvent(EVENT_ROLLBACK);
1905
shess7dbd4dee2015-10-06 17:39:161906 // The cache may have been accumulating dirty pages for commit. Note that in
1907 // some cases sql::Transaction can fire rollback after a database is closed.
1908 if (is_open())
1909 ReleaseCacheMemoryIfNeeded(false);
1910
[email protected]44ad7d902012-03-23 00:09:051911 needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:561912}
1913
1914void Connection::StatementRefCreated(StatementRef* ref) {
1915 DCHECK(open_statements_.find(ref) == open_statements_.end());
1916 open_statements_.insert(ref);
1917}
1918
1919void Connection::StatementRefDeleted(StatementRef* ref) {
1920 StatementRefSet::iterator i = open_statements_.find(ref);
1921 if (i == open_statements_.end())
[email protected]eff1fa522011-12-12 23:50:591922 DLOG(FATAL) << "Could not find statement";
[email protected]e5ffd0e42009-09-11 21:30:561923 else
1924 open_statements_.erase(i);
1925}
1926
shess58b8df82015-06-03 00:19:321927void Connection::set_histogram_tag(const std::string& tag) {
1928 DCHECK(!is_open());
1929 histogram_tag_ = tag;
1930}
1931
[email protected]210ce0af2013-05-15 09:10:391932void Connection::AddTaggedHistogram(const std::string& name,
1933 size_t sample) const {
1934 if (histogram_tag_.empty())
1935 return;
1936
1937 // TODO(shess): The histogram macros create a bit of static storage
1938 // for caching the histogram object. This code shouldn't execute
1939 // often enough for such caching to be crucial. If it becomes an
1940 // issue, the object could be cached alongside histogram_prefix_.
1941 std::string full_histogram_name = name + "." + histogram_tag_;
1942 base::HistogramBase* histogram =
1943 base::SparseHistogram::FactoryGet(
1944 full_histogram_name,
1945 base::HistogramBase::kUmaTargetedHistogramFlag);
1946 if (histogram)
1947 histogram->Add(sample);
1948}
1949
shess9e77283d2016-06-13 23:53:201950int Connection::OnSqliteError(
1951 int err, sql::Statement *stmt, const char* sql) const {
[email protected]210ce0af2013-05-15 09:10:391952 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err);
1953 AddTaggedHistogram("Sqlite.Error", err);
[email protected]c088e3a32013-01-03 23:59:141954
1955 // Always log the error.
[email protected]2f496b42013-09-26 18:36:581956 if (!sql && stmt)
1957 sql = stmt->GetSQLStatement();
1958 if (!sql)
1959 sql = "-- unknown";
shessf7e988f2015-11-13 00:41:061960
1961 std::string id = histogram_tag_;
1962 if (id.empty())
1963 id = DbPath().BaseName().AsUTF8Unsafe();
1964 LOG(ERROR) << id << " sqlite error " << err
[email protected]c088e3a32013-01-03 23:59:141965 << ", errno " << GetLastErrno()
[email protected]2f496b42013-09-26 18:36:581966 << ": " << GetErrorMessage()
1967 << ", sql: " << sql;
[email protected]c088e3a32013-01-03 23:59:141968
[email protected]c3881b372013-05-17 08:39:461969 if (!error_callback_.is_null()) {
[email protected]98cf3002013-07-12 01:38:561970 // Fire from a copy of the callback in case of reentry into
1971 // re/set_error_callback().
1972 // TODO(shess): <http://crbug.com/254584>
1973 ErrorCallback(error_callback_).Run(err, stmt);
[email protected]c3881b372013-05-17 08:39:461974 return err;
1975 }
1976
[email protected]faa604e2009-09-25 22:38:591977 // The default handling is to assert on debug and to ignore on release.
shess976814402016-06-21 06:56:251978 if (!IsExpectedSqliteError(err))
[email protected]4350e322013-06-18 22:18:101979 DLOG(FATAL) << GetErrorMessage();
[email protected]faa604e2009-09-25 22:38:591980 return err;
1981}
1982
[email protected]579446c2013-12-16 18:36:521983bool Connection::FullIntegrityCheck(std::vector<std::string>* messages) {
1984 return IntegrityCheckHelper("PRAGMA integrity_check", messages);
1985}
1986
1987bool Connection::QuickIntegrityCheck() {
1988 std::vector<std::string> messages;
1989 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages))
1990 return false;
1991 return messages.size() == 1 && messages[0] == "ok";
1992}
1993
afakhry7c9abe72016-08-05 17:33:191994std::string Connection::GetDiagnosticInfo(int extended_error,
1995 Statement* statement) {
1996 // Prevent reentrant calls to the error callback.
1997 ErrorCallback original_callback = std::move(error_callback_);
1998 reset_error_callback();
1999
2000 // Trim extended error codes.
2001 const int error = (extended_error & 0xFF);
2002 // CollectCorruptionInfo() is implemented in terms of sql::Connection,
2003 // TODO(shess): Rewrite IntegrityCheckHelper() in terms of raw SQLite.
2004 std::string result = (error == SQLITE_CORRUPT)
2005 ? CollectCorruptionInfo()
2006 : CollectErrorInfo(extended_error, statement);
2007
2008 // The following queries must be executed after CollectErrorInfo() above, so
2009 // if they result in their own errors, they don't interfere with
2010 // CollectErrorInfo().
2011 const bool has_valid_header =
2012 (ExecuteAndReturnErrorCode("PRAGMA auto_vacuum") == SQLITE_OK);
2013 const bool select_sqlite_master_result =
2014 (ExecuteAndReturnErrorCode("SELECT COUNT(*) FROM sqlite_master") ==
2015 SQLITE_OK);
2016
2017 // Restore the original error callback.
2018 error_callback_ = std::move(original_callback);
2019
2020 base::StringAppendF(&result, "Has valid header: %s\n",
2021 (has_valid_header ? "Yes" : "No"));
2022 base::StringAppendF(&result, "Has valid schema: %s\n",
2023 (select_sqlite_master_result ? "Yes" : "No"));
2024
2025 return result;
2026}
2027
[email protected]80abf152013-05-22 12:42:422028// TODO(shess): Allow specifying maximum results (default 100 lines).
[email protected]579446c2013-12-16 18:36:522029bool Connection::IntegrityCheckHelper(
2030 const char* pragma_sql,
2031 std::vector<std::string>* messages) {
[email protected]80abf152013-05-22 12:42:422032 messages->clear();
2033
[email protected]4658e2a02013-06-06 23:05:002034 // This has the side effect of setting SQLITE_RecoveryMode, which
2035 // allows SQLite to process through certain cases of corruption.
2036 // Failing to set this pragma probably means that the database is
2037 // beyond recovery.
2038 const char kWritableSchema[] = "PRAGMA writable_schema = ON";
2039 if (!Execute(kWritableSchema))
2040 return false;
2041
2042 bool ret = false;
2043 {
[email protected]579446c2013-12-16 18:36:522044 sql::Statement stmt(GetUniqueStatement(pragma_sql));
[email protected]4658e2a02013-06-06 23:05:002045
2046 // The pragma appears to return all results (up to 100 by default)
2047 // as a single string. This doesn't appear to be an API contract,
2048 // it could return separate lines, so loop _and_ split.
2049 while (stmt.Step()) {
2050 std::string result(stmt.ColumnString(0));
brettw83dc1612015-08-12 07:31:182051 *messages = base::SplitString(result, "\n", base::TRIM_WHITESPACE,
2052 base::SPLIT_WANT_ALL);
[email protected]4658e2a02013-06-06 23:05:002053 }
2054 ret = stmt.Succeeded();
[email protected]80abf152013-05-22 12:42:422055 }
[email protected]4658e2a02013-06-06 23:05:002056
2057 // Best effort to put things back as they were before.
2058 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF";
2059 ignore_result(Execute(kNoWritableSchema));
2060
2061 return ret;
[email protected]80abf152013-05-22 12:42:422062}
2063
ssid1f4e5362016-12-08 20:41:382064bool Connection::ReportMemoryUsage(base::trace_event::ProcessMemoryDump* pmd,
2065 const std::string& dump_name) {
dskibab4199f82016-11-21 20:16:132066 return memory_dump_provider_ &&
ssid1f4e5362016-12-08 20:41:382067 memory_dump_provider_->ReportMemoryUsage(pmd, dump_name);
dskibab4199f82016-11-21 20:16:132068}
2069
shess58b8df82015-06-03 00:19:322070base::TimeTicks TimeSource::Now() {
2071 return base::TimeTicks::Now();
2072}
2073
[email protected]e5ffd0e42009-09-11 21:30:562074} // namespace sql