blob: 9c7cb7d4099a811bc78b314070ab2fd1eebb1eb4 [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"
shessc8cd2a162015-10-22 20:30:4615#include "base/debug/dump_without_crashing.h"
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
thestig22dfc4012014-09-05 08:29:4417#include "base/files/file_util.h"
shessc8cd2a162015-10-22 20:30:4618#include "base/format_macros.h"
19#include "base/json/json_file_value_serializer.h"
[email protected]a7ec1292013-07-22 22:02:1820#include "base/lazy_instance.h"
fdoray2dfa76452016-06-07 13:11:2221#include "base/location.h"
[email protected]e5ffd0e42009-09-11 21:30:5622#include "base/logging.h"
[email protected]bd2ccdb4a2012-12-07 22:14:5023#include "base/metrics/histogram.h"
[email protected]210ce0af2013-05-15 09:10:3924#include "base/metrics/sparse_histogram.h"
fdoray2dfa76452016-06-07 13:11:2225#include "base/single_thread_task_runner.h"
[email protected]80abf152013-05-22 12:42:4226#include "base/strings/string_split.h"
[email protected]a4bbc1f92013-06-11 07:28:1927#include "base/strings/string_util.h"
28#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4529#include "base/strings/utf_string_conversions.h"
[email protected]a7ec1292013-07-22 22:02:1830#include "base/synchronization/lock.h"
fdoray2dfa76452016-06-07 13:11:2231#include "base/threading/thread_task_runner_handle.h"
ssid9f8022f2015-10-12 17:49:0332#include "base/trace_event/memory_dump_manager.h"
ssid3be5b1ec2016-01-13 14:21:5733#include "sql/connection_memory_dump_provider.h"
shess9bf2c672015-12-18 01:18:0834#include "sql/meta_table.h"
[email protected]f0a54b22011-07-19 18:40:2135#include "sql/statement.h"
[email protected]e33cba42010-08-18 23:37:0336#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5637
[email protected]2e1cee762013-07-09 14:40:0038#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
rohitrao83d6b83a2016-06-21 07:25:5739#include "base/ios/ios_util.h"
[email protected]2e1cee762013-07-09 14:40:0040#include "third_party/sqlite/src/ext/icu/sqliteicu.h"
41#endif
42
[email protected]5b96f3772010-09-28 16:30:5743namespace {
44
45// Spin for up to a second waiting for the lock to clear when setting
46// up the database.
47// TODO(shess): Better story on this. http://crbug.com/56559
[email protected]c68ce172011-11-24 22:30:2748const int kBusyTimeoutSeconds = 1;
[email protected]5b96f3772010-09-28 16:30:5749
50class ScopedBusyTimeout {
51 public:
52 explicit ScopedBusyTimeout(sqlite3* db)
53 : db_(db) {
54 }
55 ~ScopedBusyTimeout() {
56 sqlite3_busy_timeout(db_, 0);
57 }
58
59 int SetTimeout(base::TimeDelta timeout) {
60 DCHECK_LT(timeout.InMilliseconds(), INT_MAX);
61 return sqlite3_busy_timeout(db_,
62 static_cast<int>(timeout.InMilliseconds()));
63 }
64
65 private:
66 sqlite3* db_;
67};
68
[email protected]6d42f152012-11-10 00:38:2469// Helper to "safely" enable writable_schema. No error checking
70// because it is reasonable to just forge ahead in case of an error.
71// If turning it on fails, then most likely nothing will work, whereas
72// if turning it off fails, it only matters if some code attempts to
73// continue working with the database and tries to modify the
74// sqlite_master table (none of our code does this).
75class ScopedWritableSchema {
76 public:
77 explicit ScopedWritableSchema(sqlite3* db)
78 : db_(db) {
79 sqlite3_exec(db_, "PRAGMA writable_schema=1", NULL, NULL, NULL);
80 }
81 ~ScopedWritableSchema() {
82 sqlite3_exec(db_, "PRAGMA writable_schema=0", NULL, NULL, NULL);
83 }
84
85 private:
86 sqlite3* db_;
87};
88
[email protected]7bae5742013-07-10 20:46:1689// Helper to wrap the sqlite3_backup_*() step of Raze(). Return
90// SQLite error code from running the backup step.
91int BackupDatabase(sqlite3* src, sqlite3* dst, const char* db_name) {
92 DCHECK_NE(src, dst);
93 sqlite3_backup* backup = sqlite3_backup_init(dst, db_name, src, db_name);
94 if (!backup) {
95 // Since this call only sets things up, this indicates a gross
96 // error in SQLite.
97 DLOG(FATAL) << "Unable to start sqlite3_backup(): " << sqlite3_errmsg(dst);
98 return sqlite3_errcode(dst);
99 }
100
101 // -1 backs up the entire database.
102 int rc = sqlite3_backup_step(backup, -1);
103 int pages = sqlite3_backup_pagecount(backup);
104 sqlite3_backup_finish(backup);
105
106 // If successful, exactly one page should have been backed up. If
107 // this breaks, check this function to make sure assumptions aren't
108 // being broken.
109 if (rc == SQLITE_DONE)
110 DCHECK_EQ(pages, 1);
111
112 return rc;
113}
114
[email protected]8d409412013-07-19 18:25:30115// Be very strict on attachment point. SQLite can handle a much wider
116// character set with appropriate quoting, but Chromium code should
117// just use clean names to start with.
118bool ValidAttachmentPoint(const char* attachment_point) {
119 for (size_t i = 0; attachment_point[i]; ++i) {
zhongyi23960342016-04-12 23:13:20120 if (!(base::IsAsciiDigit(attachment_point[i]) ||
121 base::IsAsciiAlpha(attachment_point[i]) ||
[email protected]8d409412013-07-19 18:25:30122 attachment_point[i] == '_')) {
123 return false;
124 }
125 }
126 return true;
127}
128
shessc9e80ae22015-08-12 21:39:11129void RecordSqliteMemory10Min() {
avi0b519202015-12-21 07:25:19130 const int64_t used = sqlite3_memory_used();
shessc9e80ae22015-08-12 21:39:11131 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.TenMinutes", used / 1024);
132}
133
134void RecordSqliteMemoryHour() {
avi0b519202015-12-21 07:25:19135 const int64_t used = sqlite3_memory_used();
shessc9e80ae22015-08-12 21:39:11136 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneHour", used / 1024);
137}
138
139void RecordSqliteMemoryDay() {
avi0b519202015-12-21 07:25:19140 const int64_t used = sqlite3_memory_used();
shessc9e80ae22015-08-12 21:39:11141 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneDay", used / 1024);
142}
143
shess2d48e942015-08-25 17:39:51144void RecordSqliteMemoryWeek() {
avi0b519202015-12-21 07:25:19145 const int64_t used = sqlite3_memory_used();
shess2d48e942015-08-25 17:39:51146 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneWeek", used / 1024);
147}
148
[email protected]a7ec1292013-07-22 22:02:18149// SQLite automatically calls sqlite3_initialize() lazily, but
150// sqlite3_initialize() uses double-checked locking and thus can have
151// data races.
152//
153// TODO(shess): Another alternative would be to have
154// sqlite3_initialize() called as part of process bring-up. If this
155// is changed, remove the dynamic_annotations dependency in sql.gyp.
156base::LazyInstance<base::Lock>::Leaky
157 g_sqlite_init_lock = LAZY_INSTANCE_INITIALIZER;
158void InitializeSqlite() {
159 base::AutoLock lock(g_sqlite_init_lock.Get());
shessc9e80ae22015-08-12 21:39:11160 static bool first_call = true;
161 if (first_call) {
162 sqlite3_initialize();
163
164 // Schedule callback to record memory footprint histograms at 10m, 1h, and
fdoray2dfa76452016-06-07 13:11:22165 // 1d. There may not be a registered thread task runner in tests.
166 if (base::ThreadTaskRunnerHandle::IsSet()) {
167 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
shessc9e80ae22015-08-12 21:39:11168 FROM_HERE, base::Bind(&RecordSqliteMemory10Min),
169 base::TimeDelta::FromMinutes(10));
fdoray2dfa76452016-06-07 13:11:22170 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
shessc9e80ae22015-08-12 21:39:11171 FROM_HERE, base::Bind(&RecordSqliteMemoryHour),
172 base::TimeDelta::FromHours(1));
fdoray2dfa76452016-06-07 13:11:22173 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
shessc9e80ae22015-08-12 21:39:11174 FROM_HERE, base::Bind(&RecordSqliteMemoryDay),
175 base::TimeDelta::FromDays(1));
fdoray2dfa76452016-06-07 13:11:22176 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
shess2d48e942015-08-25 17:39:51177 FROM_HERE, base::Bind(&RecordSqliteMemoryWeek),
178 base::TimeDelta::FromDays(7));
shessc9e80ae22015-08-12 21:39:11179 }
180
181 first_call = false;
182 }
[email protected]a7ec1292013-07-22 22:02:18183}
184
[email protected]8ada10f2013-12-21 00:42:34185// Helper to get the sqlite3_file* associated with the "main" database.
186int GetSqlite3File(sqlite3* db, sqlite3_file** file) {
187 *file = NULL;
188 int rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_FILE_POINTER, file);
189 if (rc != SQLITE_OK)
190 return rc;
191
192 // TODO(shess): NULL in file->pMethods has been observed on android_dbg
193 // content_unittests, even though it should not be possible.
194 // http://crbug.com/329982
195 if (!*file || !(*file)->pMethods)
196 return SQLITE_ERROR;
197
198 return rc;
199}
200
shess5dac334f2015-11-05 20:47:42201// Convenience to get the sqlite3_file* and the size for the "main" database.
202int GetSqlite3FileAndSize(sqlite3* db,
203 sqlite3_file** file, sqlite3_int64* db_size) {
204 int rc = GetSqlite3File(db, file);
205 if (rc != SQLITE_OK)
206 return rc;
207
208 return (*file)->pMethods->xFileSize(*file, db_size);
209}
210
shess58b8df82015-06-03 00:19:32211// This should match UMA_HISTOGRAM_MEDIUM_TIMES().
212base::HistogramBase* GetMediumTimeHistogram(const std::string& name) {
213 return base::Histogram::FactoryTimeGet(
214 name,
215 base::TimeDelta::FromMilliseconds(10),
216 base::TimeDelta::FromMinutes(3),
217 50,
218 base::HistogramBase::kUmaTargetedHistogramFlag);
219}
220
erg102ceb412015-06-20 01:38:13221std::string AsUTF8ForSQL(const base::FilePath& path) {
222#if defined(OS_WIN)
223 return base::WideToUTF8(path.value());
224#elif defined(OS_POSIX)
225 return path.value();
226#endif
227}
228
[email protected]5b96f3772010-09-28 16:30:57229} // namespace
230
[email protected]e5ffd0e42009-09-11 21:30:56231namespace sql {
232
[email protected]4350e322013-06-18 22:18:10233// static
shess976814402016-06-21 06:56:25234Connection::ErrorExpecterCallback* Connection::current_expecter_cb_ = NULL;
[email protected]4350e322013-06-18 22:18:10235
236// static
shess976814402016-06-21 06:56:25237bool Connection::IsExpectedSqliteError(int error) {
238 if (!current_expecter_cb_)
[email protected]4350e322013-06-18 22:18:10239 return false;
shess976814402016-06-21 06:56:25240 return current_expecter_cb_->Run(error);
[email protected]4350e322013-06-18 22:18:10241}
242
shessc8cd2a162015-10-22 20:30:46243void Connection::ReportDiagnosticInfo(int extended_error, Statement* stmt) {
244 AssertIOAllowed();
245
afakhry7c9abe72016-08-05 17:33:19246 std::string debug_info = GetDiagnosticInfo(extended_error, stmt);
shessc8cd2a162015-10-22 20:30:46247 if (!debug_info.empty() && RegisterIntentToUpload()) {
248 char debug_buf[2000];
249 base::strlcpy(debug_buf, debug_info.c_str(), arraysize(debug_buf));
250 base::debug::Alias(&debug_buf);
251
252 base::debug::DumpWithoutCrashing();
253 }
254}
255
[email protected]4350e322013-06-18 22:18:10256// static
shess976814402016-06-21 06:56:25257void Connection::SetErrorExpecter(Connection::ErrorExpecterCallback* cb) {
258 CHECK(current_expecter_cb_ == NULL);
259 current_expecter_cb_ = cb;
[email protected]4350e322013-06-18 22:18:10260}
261
262// static
shess976814402016-06-21 06:56:25263void Connection::ResetErrorExpecter() {
264 CHECK(current_expecter_cb_);
265 current_expecter_cb_ = NULL;
[email protected]4350e322013-06-18 22:18:10266}
267
[email protected]e5ffd0e42009-09-11 21:30:56268bool StatementID::operator<(const StatementID& other) const {
269 if (number_ != other.number_)
270 return number_ < other.number_;
271 return strcmp(str_, other.str_) < 0;
272}
273
[email protected]e5ffd0e42009-09-11 21:30:56274Connection::StatementRef::StatementRef(Connection* connection,
[email protected]41a97c812013-02-07 02:35:38275 sqlite3_stmt* stmt,
276 bool was_valid)
[email protected]e5ffd0e42009-09-11 21:30:56277 : connection_(connection),
[email protected]41a97c812013-02-07 02:35:38278 stmt_(stmt),
279 was_valid_(was_valid) {
280 if (connection)
281 connection_->StatementRefCreated(this);
[email protected]e5ffd0e42009-09-11 21:30:56282}
283
284Connection::StatementRef::~StatementRef() {
285 if (connection_)
286 connection_->StatementRefDeleted(this);
[email protected]41a97c812013-02-07 02:35:38287 Close(false);
[email protected]e5ffd0e42009-09-11 21:30:56288}
289
[email protected]41a97c812013-02-07 02:35:38290void Connection::StatementRef::Close(bool forced) {
[email protected]e5ffd0e42009-09-11 21:30:56291 if (stmt_) {
[email protected]35f7e5392012-07-27 19:54:50292 // Call to AssertIOAllowed() cannot go at the beginning of the function
293 // because Close() is called unconditionally from destructor to clean
294 // connection_. And if this is inactive statement this won't cause any
295 // disk access and destructor most probably will be called on thread
296 // not allowing disk access.
297 // TODO([email protected]): This should move to the beginning
298 // of the function. http://crbug.com/136655.
299 AssertIOAllowed();
[email protected]e5ffd0e42009-09-11 21:30:56300 sqlite3_finalize(stmt_);
301 stmt_ = NULL;
302 }
303 connection_ = NULL; // The connection may be getting deleted.
[email protected]41a97c812013-02-07 02:35:38304
305 // Forced close is expected to happen from a statement error
306 // handler. In that case maintain the sense of |was_valid_| which
307 // previously held for this ref.
308 was_valid_ = was_valid_ && forced;
[email protected]e5ffd0e42009-09-11 21:30:56309}
310
311Connection::Connection()
312 : db_(NULL),
313 page_size_(0),
314 cache_size_(0),
315 exclusive_locking_(false),
[email protected]81a2a602013-07-17 19:10:36316 restrict_to_user_(false),
[email protected]e5ffd0e42009-09-11 21:30:56317 transaction_nesting_(0),
[email protected]35f7e5392012-07-27 19:54:50318 needs_rollback_(false),
[email protected]49dc4f22012-10-17 17:41:16319 in_memory_(false),
shess58b8df82015-06-03 00:19:32320 poisoned_(false),
kerz42ff2a012016-04-27 04:50:06321 mmap_disabled_(false),
shess7dbd4dee2015-10-06 17:39:16322 mmap_enabled_(false),
323 total_changes_at_last_release_(0),
shess58b8df82015-06-03 00:19:32324 stats_histogram_(NULL),
325 commit_time_histogram_(NULL),
326 autocommit_time_histogram_(NULL),
327 update_time_histogram_(NULL),
328 query_time_histogram_(NULL),
329 clock_(new TimeSource()) {
[email protected]526b4662013-06-14 04:09:12330}
[email protected]e5ffd0e42009-09-11 21:30:56331
332Connection::~Connection() {
333 Close();
334}
335
shess58b8df82015-06-03 00:19:32336void Connection::RecordEvent(Events event, size_t count) {
337 for (size_t i = 0; i < count; ++i) {
338 UMA_HISTOGRAM_ENUMERATION("Sqlite.Stats", event, EVENT_MAX_VALUE);
339 }
340
341 if (stats_histogram_) {
342 for (size_t i = 0; i < count; ++i) {
343 stats_histogram_->Add(event);
344 }
345 }
346}
347
348void Connection::RecordCommitTime(const base::TimeDelta& delta) {
349 RecordUpdateTime(delta);
350 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.CommitTime", delta);
351 if (commit_time_histogram_)
352 commit_time_histogram_->AddTime(delta);
353}
354
355void Connection::RecordAutoCommitTime(const base::TimeDelta& delta) {
356 RecordUpdateTime(delta);
357 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.AutoCommitTime", delta);
358 if (autocommit_time_histogram_)
359 autocommit_time_histogram_->AddTime(delta);
360}
361
362void Connection::RecordUpdateTime(const base::TimeDelta& delta) {
363 RecordQueryTime(delta);
364 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.UpdateTime", delta);
365 if (update_time_histogram_)
366 update_time_histogram_->AddTime(delta);
367}
368
369void Connection::RecordQueryTime(const base::TimeDelta& delta) {
370 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.QueryTime", delta);
371 if (query_time_histogram_)
372 query_time_histogram_->AddTime(delta);
373}
374
375void Connection::RecordTimeAndChanges(
376 const base::TimeDelta& delta, bool read_only) {
377 if (read_only) {
378 RecordQueryTime(delta);
379 } else {
380 const int changes = sqlite3_changes(db_);
381 if (sqlite3_get_autocommit(db_)) {
382 RecordAutoCommitTime(delta);
383 RecordEvent(EVENT_CHANGES_AUTOCOMMIT, changes);
384 } else {
385 RecordUpdateTime(delta);
386 RecordEvent(EVENT_CHANGES, changes);
387 }
388 }
389}
390
[email protected]a3ef4832013-02-02 05:12:33391bool Connection::Open(const base::FilePath& path) {
[email protected]348ac8f52013-05-21 03:27:02392 if (!histogram_tag_.empty()) {
tfarina720d4f32015-05-11 22:31:26393 int64_t size_64 = 0;
[email protected]56285702013-12-04 18:22:49394 if (base::GetFileSize(path, &size_64)) {
[email protected]348ac8f52013-05-21 03:27:02395 size_t sample = static_cast<size_t>(size_64 / 1024);
396 std::string full_histogram_name = "Sqlite.SizeKB." + histogram_tag_;
397 base::HistogramBase* histogram =
398 base::Histogram::FactoryGet(
399 full_histogram_name, 1, 1000000, 50,
400 base::HistogramBase::kUmaTargetedHistogramFlag);
401 if (histogram)
402 histogram->Add(sample);
shess9bf2c672015-12-18 01:18:08403 UMA_HISTOGRAM_COUNTS("Sqlite.SizeKB", sample);
[email protected]348ac8f52013-05-21 03:27:02404 }
405 }
406
erg102ceb412015-06-20 01:38:13407 return OpenInternal(AsUTF8ForSQL(path), RETRY_ON_POISON);
[email protected]765b44502009-10-02 05:01:42408}
[email protected]e5ffd0e42009-09-11 21:30:56409
[email protected]765b44502009-10-02 05:01:42410bool Connection::OpenInMemory() {
[email protected]35f7e5392012-07-27 19:54:50411 in_memory_ = true;
[email protected]fed734a2013-07-17 04:45:13412 return OpenInternal(":memory:", NO_RETRY);
[email protected]e5ffd0e42009-09-11 21:30:56413}
414
[email protected]8d409412013-07-19 18:25:30415bool Connection::OpenTemporary() {
416 return OpenInternal("", NO_RETRY);
417}
418
[email protected]41a97c812013-02-07 02:35:38419void Connection::CloseInternal(bool forced) {
[email protected]4e179ba2012-03-17 16:06:47420 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
421 // will delete the -journal file. For ChromiumOS or other more
422 // embedded systems, this is probably not appropriate, whereas on
423 // desktop it might make some sense.
424
[email protected]4b350052012-02-24 20:40:48425 // sqlite3_close() needs all prepared statements to be finalized.
[email protected]4b350052012-02-24 20:40:48426
[email protected]41a97c812013-02-07 02:35:38427 // Release cached statements.
428 statement_cache_.clear();
429
430 // With cached statements released, in-use statements will remain.
431 // Closing the database while statements are in use is an API
432 // violation, except for forced close (which happens from within a
433 // statement's error handler).
434 DCHECK(forced || open_statements_.empty());
435
436 // Deactivate any outstanding statements so sqlite3_close() works.
437 for (StatementRefSet::iterator i = open_statements_.begin();
438 i != open_statements_.end(); ++i)
439 (*i)->Close(forced);
440 open_statements_.clear();
[email protected]4b350052012-02-24 20:40:48441
[email protected]e5ffd0e42009-09-11 21:30:56442 if (db_) {
[email protected]35f7e5392012-07-27 19:54:50443 // Call to AssertIOAllowed() cannot go at the beginning of the function
444 // because Close() must be called from destructor to clean
445 // statement_cache_, it won't cause any disk access and it most probably
446 // will happen on thread not allowing disk access.
447 // TODO([email protected]): This should move to the beginning
448 // of the function. http://crbug.com/136655.
449 AssertIOAllowed();
[email protected]73fb8d52013-07-24 05:04:28450
ssid3be5b1ec2016-01-13 14:21:57451 // Reseting acquires a lock to ensure no dump is happening on the database
452 // at the same time. Unregister takes ownership of provider and it is safe
453 // since the db is reset. memory_dump_provider_ could be null if db_ was
454 // poisoned.
455 if (memory_dump_provider_) {
456 memory_dump_provider_->ResetDatabase();
457 base::trace_event::MemoryDumpManager::GetInstance()
458 ->UnregisterAndDeleteDumpProviderSoon(
459 std::move(memory_dump_provider_));
460 }
461
[email protected]73fb8d52013-07-24 05:04:28462 int rc = sqlite3_close(db_);
463 if (rc != SQLITE_OK) {
464 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.CloseFailure", rc);
465 DLOG(FATAL) << "sqlite3_close failed: " << GetErrorMessage();
466 }
[email protected]e5ffd0e42009-09-11 21:30:56467 }
[email protected]fed734a2013-07-17 04:45:13468 db_ = NULL;
[email protected]e5ffd0e42009-09-11 21:30:56469}
470
[email protected]41a97c812013-02-07 02:35:38471void Connection::Close() {
472 // If the database was already closed by RazeAndClose(), then no
473 // need to close again. Clear the |poisoned_| bit so that incorrect
474 // API calls are caught.
475 if (poisoned_) {
476 poisoned_ = false;
477 return;
478 }
479
480 CloseInternal(false);
481}
482
[email protected]e5ffd0e42009-09-11 21:30:56483void Connection::Preload() {
[email protected]35f7e5392012-07-27 19:54:50484 AssertIOAllowed();
485
[email protected]e5ffd0e42009-09-11 21:30:56486 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38487 DLOG_IF(FATAL, !poisoned_) << "Cannot preload null db";
[email protected]e5ffd0e42009-09-11 21:30:56488 return;
489 }
490
[email protected]8ada10f2013-12-21 00:42:34491 // Use local settings if provided, otherwise use documented defaults. The
492 // actual results could be fetching via PRAGMA calls.
493 const int page_size = page_size_ ? page_size_ : 1024;
494 sqlite3_int64 preload_size = page_size * (cache_size_ ? cache_size_ : 2000);
495 if (preload_size < 1)
[email protected]e5ffd0e42009-09-11 21:30:56496 return;
497
[email protected]8ada10f2013-12-21 00:42:34498 sqlite3_file* file = NULL;
[email protected]8ada10f2013-12-21 00:42:34499 sqlite3_int64 file_size = 0;
shess5dac334f2015-11-05 20:47:42500 int rc = GetSqlite3FileAndSize(db_, &file, &file_size);
[email protected]8ada10f2013-12-21 00:42:34501 if (rc != SQLITE_OK)
502 return;
503
504 // Don't preload more than the file contains.
505 if (preload_size > file_size)
506 preload_size = file_size;
507
mostynbd82cd9952016-04-11 20:05:34508 std::unique_ptr<char[]> buf(new char[page_size]);
shessde60c5f12015-04-21 17:34:46509 for (sqlite3_int64 pos = 0; pos < preload_size; pos += page_size) {
[email protected]8ada10f2013-12-21 00:42:34510 rc = file->pMethods->xRead(file, buf.get(), page_size, pos);
shessd90aeea82015-11-13 02:24:31511
512 // TODO(shess): Consider calling OnSqliteError().
[email protected]8ada10f2013-12-21 00:42:34513 if (rc != SQLITE_OK)
514 return;
515 }
[email protected]e5ffd0e42009-09-11 21:30:56516}
517
shess7dbd4dee2015-10-06 17:39:16518// SQLite keeps unused pages associated with a connection in a cache. It asks
519// the cache for pages by an id, and if the page is present and the database is
520// unchanged, it considers the content of the page valid and doesn't read it
521// from disk. When memory-mapped I/O is enabled, on read SQLite uses page
522// structures created from the memory map data before consulting the cache. On
523// write SQLite creates a new in-memory page structure, copies the data from the
524// memory map, and later writes it, releasing the updated page back to the
525// cache.
526//
527// This means that in memory-mapped mode, the contents of the cached pages are
528// not re-used for reads, but they are re-used for writes if the re-written page
529// is still in the cache. The implementation of sqlite3_db_release_memory() as
530// of SQLite 3.8.7.4 frees all pages from pcaches associated with the
531// connection, so it should free these pages.
532//
533// Unfortunately, the zero page is also freed. That page is never accessed
534// using memory-mapped I/O, and the cached copy can be re-used after verifying
535// the file change counter on disk. Also, fresh pages from cache receive some
536// pager-level initialization before they can be used. Since the information
537// involved will immediately be accessed in various ways, it is unclear if the
538// additional overhead is material, or just moving processor cache effects
539// around.
540//
541// TODO(shess): It would be better to release the pages immediately when they
542// are no longer needed. This would basically happen after SQLite commits a
543// transaction. I had implemented a pcache wrapper to do this, but it involved
544// layering violations, and it had to be setup before any other sqlite call,
545// which was brittle. Also, for large files it would actually make sense to
546// maintain the existing pcache behavior for blocks past the memory-mapped
547// segment. I think drh would accept a reasonable implementation of the overall
548// concept for upstreaming to SQLite core.
549//
550// TODO(shess): Another possibility would be to set the cache size small, which
551// would keep the zero page around, plus some pre-initialized pages, and SQLite
552// can manage things. The downside is that updates larger than the cache would
553// spill to the journal. That could be compensated by setting cache_spill to
554// false. The downside then is that it allows open-ended use of memory for
555// large transactions.
556//
557// TODO(shess): The TrimMemory() trick of bouncing the cache size would also
558// work. There could be two prepared statements, one for cache_size=1 one for
559// cache_size=goal.
560void Connection::ReleaseCacheMemoryIfNeeded(bool implicit_change_performed) {
shess644fc8a2016-02-26 18:15:58561 // The database could have been closed during a transaction as part of error
562 // recovery.
563 if (!db_) {
564 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
565 return;
566 }
shess7dbd4dee2015-10-06 17:39:16567
568 // If memory-mapping is not enabled, the page cache helps performance.
569 if (!mmap_enabled_)
570 return;
571
572 // On caller request, force the change comparison to fail. Done before the
573 // transaction-nesting test so that the signal can carry to transaction
574 // commit.
575 if (implicit_change_performed)
576 --total_changes_at_last_release_;
577
578 // Cached pages may be re-used within the same transaction.
579 if (transaction_nesting())
580 return;
581
582 // If no changes have been made, skip flushing. This allows the first page of
583 // the database to remain in cache across multiple reads.
584 const int total_changes = sqlite3_total_changes(db_);
585 if (total_changes == total_changes_at_last_release_)
586 return;
587
588 total_changes_at_last_release_ = total_changes;
589 sqlite3_db_release_memory(db_);
590}
591
shessc8cd2a162015-10-22 20:30:46592base::FilePath Connection::DbPath() const {
593 if (!is_open())
594 return base::FilePath();
595
596 const char* path = sqlite3_db_filename(db_, "main");
597 const base::StringPiece db_path(path);
598#if defined(OS_WIN)
599 return base::FilePath(base::UTF8ToWide(db_path));
600#elif defined(OS_POSIX)
601 return base::FilePath(db_path);
602#else
603 NOTREACHED();
604 return base::FilePath();
605#endif
606}
607
608// Data is persisted in a file shared between databases in the same directory.
609// The "sqlite-diag" file contains a dictionary with the version number, and an
610// array of histogram tags for databases which have been dumped.
611bool Connection::RegisterIntentToUpload() const {
612 static const char* kVersionKey = "version";
613 static const char* kDiagnosticDumpsKey = "DiagnosticDumps";
614 static int kVersion = 1;
615
616 AssertIOAllowed();
617
618 if (histogram_tag_.empty())
619 return false;
620
621 if (!is_open())
622 return false;
623
624 if (in_memory_)
625 return false;
626
627 const base::FilePath db_path = DbPath();
628 if (db_path.empty())
629 return false;
630
631 // Put the collection of diagnostic data next to the databases. In most
632 // cases, this is the profile directory, but safe-browsing stores a Cookies
633 // file in the directory above the profile directory.
634 base::FilePath breadcrumb_path(
635 db_path.DirName().Append(FILE_PATH_LITERAL("sqlite-diag")));
636
637 // Lock against multiple updates to the diagnostics file. This code should
638 // seldom be called in the first place, and when called it should seldom be
639 // called for multiple databases, and when called for multiple databases there
640 // is _probably_ something systemic wrong with the user's system. So the lock
641 // should never be contended, but when it is the database experience is
642 // already bad.
643 base::AutoLock lock(g_sqlite_init_lock.Get());
644
mostynbd82cd9952016-04-11 20:05:34645 std::unique_ptr<base::Value> root;
shessc8cd2a162015-10-22 20:30:46646 if (!base::PathExists(breadcrumb_path)) {
mostynbd82cd9952016-04-11 20:05:34647 std::unique_ptr<base::DictionaryValue> root_dict(
648 new base::DictionaryValue());
shessc8cd2a162015-10-22 20:30:46649 root_dict->SetInteger(kVersionKey, kVersion);
650
mostynbd82cd9952016-04-11 20:05:34651 std::unique_ptr<base::ListValue> dumps(new base::ListValue);
shessc8cd2a162015-10-22 20:30:46652 dumps->AppendString(histogram_tag_);
dchenge48600452015-12-28 02:24:50653 root_dict->Set(kDiagnosticDumpsKey, std::move(dumps));
shessc8cd2a162015-10-22 20:30:46654
dchenge48600452015-12-28 02:24:50655 root = std::move(root_dict);
shessc8cd2a162015-10-22 20:30:46656 } else {
657 // Failure to read a valid dictionary implies that something is going wrong
658 // on the system.
659 JSONFileValueDeserializer deserializer(breadcrumb_path);
mostynbd82cd9952016-04-11 20:05:34660 std::unique_ptr<base::Value> read_root(
shessc8cd2a162015-10-22 20:30:46661 deserializer.Deserialize(nullptr, nullptr));
662 if (!read_root.get())
663 return false;
mostynbd82cd9952016-04-11 20:05:34664 std::unique_ptr<base::DictionaryValue> root_dict =
dchenge48600452015-12-28 02:24:50665 base::DictionaryValue::From(std::move(read_root));
shessc8cd2a162015-10-22 20:30:46666 if (!root_dict)
667 return false;
668
669 // Don't upload if the version is missing or newer.
670 int version = 0;
671 if (!root_dict->GetInteger(kVersionKey, &version) || version > kVersion)
672 return false;
673
674 base::ListValue* dumps = nullptr;
675 if (!root_dict->GetList(kDiagnosticDumpsKey, &dumps))
676 return false;
677
678 const size_t size = dumps->GetSize();
679 for (size_t i = 0; i < size; ++i) {
680 std::string s;
681
682 // Don't upload if the value isn't a string, or indicates a prior upload.
683 if (!dumps->GetString(i, &s) || s == histogram_tag_)
684 return false;
685 }
686
687 // Record intention to proceed with upload.
688 dumps->AppendString(histogram_tag_);
dchenge48600452015-12-28 02:24:50689 root = std::move(root_dict);
shessc8cd2a162015-10-22 20:30:46690 }
691
692 const base::FilePath breadcrumb_new =
693 breadcrumb_path.AddExtension(FILE_PATH_LITERAL("new"));
694 base::DeleteFile(breadcrumb_new, false);
695
696 // No upload if the breadcrumb file cannot be updated.
697 // TODO(shess): Consider ImportantFileWriter::WriteFileAtomically() to land
698 // the data on disk. For now, losing the data is not a big problem, so the
699 // sync overhead would probably not be worth it.
700 JSONFileValueSerializer serializer(breadcrumb_new);
701 if (!serializer.Serialize(*root))
702 return false;
703 if (!base::PathExists(breadcrumb_new))
704 return false;
705 if (!base::ReplaceFile(breadcrumb_new, breadcrumb_path, nullptr)) {
706 base::DeleteFile(breadcrumb_new, false);
707 return false;
708 }
709
710 return true;
711}
712
713std::string Connection::CollectErrorInfo(int error, Statement* stmt) const {
714 // Buffer for accumulating debugging info about the error. Place
715 // more-relevant information earlier, in case things overflow the
716 // fixed-size reporting buffer.
717 std::string debug_info;
718
719 // The error message from the failed operation.
720 base::StringAppendF(&debug_info, "db error: %d/%s\n",
721 GetErrorCode(), GetErrorMessage());
722
723 // TODO(shess): |error| and |GetErrorCode()| should always be the same, but
724 // reading code does not entirely convince me. Remove if they turn out to be
725 // the same.
726 if (error != GetErrorCode())
727 base::StringAppendF(&debug_info, "reported error: %d\n", error);
728
729 // System error information. Interpretation of Windows errors is different
730 // from posix.
731#if defined(OS_WIN)
732 base::StringAppendF(&debug_info, "LastError: %d\n", GetLastErrno());
733#elif defined(OS_POSIX)
734 base::StringAppendF(&debug_info, "errno: %d\n", GetLastErrno());
735#else
736 NOTREACHED(); // Add appropriate log info.
737#endif
738
739 if (stmt) {
740 base::StringAppendF(&debug_info, "statement: %s\n",
741 stmt->GetSQLStatement());
742 } else {
743 base::StringAppendF(&debug_info, "statement: NULL\n");
744 }
745
746 // SQLITE_ERROR often indicates some sort of mismatch between the statement
747 // and the schema, possibly due to a failed schema migration.
748 if (error == SQLITE_ERROR) {
749 const char* kVersionSql = "SELECT value FROM meta WHERE key = 'version'";
750 sqlite3_stmt* s;
751 int rc = sqlite3_prepare_v2(db_, kVersionSql, -1, &s, nullptr);
752 if (rc == SQLITE_OK) {
753 rc = sqlite3_step(s);
754 if (rc == SQLITE_ROW) {
755 base::StringAppendF(&debug_info, "version: %d\n",
756 sqlite3_column_int(s, 0));
757 } else if (rc == SQLITE_DONE) {
758 debug_info += "version: none\n";
759 } else {
760 base::StringAppendF(&debug_info, "version: error %d\n", rc);
761 }
762 sqlite3_finalize(s);
763 } else {
764 base::StringAppendF(&debug_info, "version: prepare error %d\n", rc);
765 }
766
767 debug_info += "schema:\n";
768
769 // sqlite_master has columns:
770 // type - "index" or "table".
771 // name - name of created element.
772 // tbl_name - name of element, or target table in case of index.
773 // rootpage - root page of the element in database file.
774 // sql - SQL to create the element.
775 // In general, the |sql| column is sufficient to derive the other columns.
776 // |rootpage| is not interesting for debugging, without the contents of the
777 // database. The COALESCE is because certain automatic elements will have a
778 // |name| but no |sql|,
779 const char* kSchemaSql = "SELECT COALESCE(sql, name) FROM sqlite_master";
780 rc = sqlite3_prepare_v2(db_, kSchemaSql, -1, &s, nullptr);
781 if (rc == SQLITE_OK) {
782 while ((rc = sqlite3_step(s)) == SQLITE_ROW) {
783 base::StringAppendF(&debug_info, "%s\n", sqlite3_column_text(s, 0));
784 }
785 if (rc != SQLITE_DONE)
786 base::StringAppendF(&debug_info, "error %d\n", rc);
787 sqlite3_finalize(s);
788 } else {
789 base::StringAppendF(&debug_info, "prepare error %d\n", rc);
790 }
791 }
792
793 return debug_info;
794}
795
796// TODO(shess): Since this is only called in an error situation, it might be
797// prudent to rewrite in terms of SQLite API calls, and mark the function const.
798std::string Connection::CollectCorruptionInfo() {
799 AssertIOAllowed();
800
801 // If the file cannot be accessed it is unlikely that an integrity check will
802 // turn up actionable information.
803 const base::FilePath db_path = DbPath();
avi0b519202015-12-21 07:25:19804 int64_t db_size = -1;
shessc8cd2a162015-10-22 20:30:46805 if (!base::GetFileSize(db_path, &db_size) || db_size < 0)
806 return std::string();
807
808 // Buffer for accumulating debugging info about the error. Place
809 // more-relevant information earlier, in case things overflow the
810 // fixed-size reporting buffer.
811 std::string debug_info;
812 base::StringAppendF(&debug_info, "SQLITE_CORRUPT, db size %" PRId64 "\n",
813 db_size);
814
815 // Only check files up to 8M to keep things from blocking too long.
avi0b519202015-12-21 07:25:19816 const int64_t kMaxIntegrityCheckSize = 8192 * 1024;
shessc8cd2a162015-10-22 20:30:46817 if (db_size > kMaxIntegrityCheckSize) {
818 debug_info += "integrity_check skipped due to size\n";
819 } else {
820 std::vector<std::string> messages;
821
822 // TODO(shess): FullIntegrityCheck() splits into a vector while this joins
823 // into a string. Probably should be refactored.
824 const base::TimeTicks before = base::TimeTicks::Now();
825 FullIntegrityCheck(&messages);
826 base::StringAppendF(
827 &debug_info,
828 "integrity_check %" PRId64 " ms, %" PRIuS " records:\n",
829 (base::TimeTicks::Now() - before).InMilliseconds(),
830 messages.size());
831
832 // SQLite returns up to 100 messages by default, trim deeper to
833 // keep close to the 2000-character size limit for dumping.
834 const size_t kMaxMessages = 20;
835 for (size_t i = 0; i < kMaxMessages && i < messages.size(); ++i) {
836 base::StringAppendF(&debug_info, "%s\n", messages[i].c_str());
837 }
838 }
839
840 return debug_info;
841}
842
shessd90aeea82015-11-13 02:24:31843size_t Connection::GetAppropriateMmapSize() {
844 AssertIOAllowed();
845
rohitrao83d6b83a2016-06-21 07:25:57846#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
847 if (!base::ios::IsRunningOnIOS10OrLater()) {
848 // iOS SQLite does not support memory mapping.
849 return 0;
850 }
shessd90aeea82015-11-13 02:24:31851#endif
852
shess9bf2c672015-12-18 01:18:08853 // How much to map if no errors are found. 50MB encompasses the 99th
854 // percentile of Chrome databases in the wild, so this should be good.
855 const size_t kMmapEverything = 256 * 1024 * 1024;
856
857 // If the database doesn't have a place to track progress, assume the best.
858 // This will happen when new databases are created, or if a database doesn't
859 // use a meta table. sql::MetaTable::Init() will preload kMmapSuccess.
860 // TODO(shess): Databases not using meta include:
861 // DOMStorageDatabase (localstorage)
862 // ActivityDatabase (extensions activity log)
863 // PredictorDatabase (prefetch and autocomplete predictor data)
864 // SyncDirectory (sync metadata storage)
865 // For now, these all have mmap disabled to allow other databases to get the
866 // default-enable path. sqlite-diag could be an alternative for all but
867 // DOMStorageDatabase, which creates many small databases.
868 // http://crbug.com/537742
869 if (!MetaTable::DoesTableExist(this)) {
shessd90aeea82015-11-13 02:24:31870 RecordOneEvent(EVENT_MMAP_META_MISSING);
shess9bf2c672015-12-18 01:18:08871 return kMmapEverything;
shessd90aeea82015-11-13 02:24:31872 }
873
shess9bf2c672015-12-18 01:18:08874 int64_t mmap_ofs = 0;
875 if (!MetaTable::GetMmapStatus(this, &mmap_ofs)) {
876 RecordOneEvent(EVENT_MMAP_META_FAILURE_READ);
877 return 0;
shessd90aeea82015-11-13 02:24:31878 }
879
880 // Database read failed in the past, don't memory map.
shess9bf2c672015-12-18 01:18:08881 if (mmap_ofs == MetaTable::kMmapFailure) {
shessd90aeea82015-11-13 02:24:31882 RecordOneEvent(EVENT_MMAP_FAILED);
883 return 0;
shess9bf2c672015-12-18 01:18:08884 } else if (mmap_ofs != MetaTable::kMmapSuccess) {
shessd90aeea82015-11-13 02:24:31885 // Continue reading from previous offset.
886 DCHECK_GE(mmap_ofs, 0);
887
888 // TODO(shess): Could this reading code be shared with Preload()? It would
889 // require locking twice (this code wouldn't be able to access |db_size| so
890 // the helper would have to return amount read).
891
892 // Read more of the database looking for errors. The VFS interface is used
893 // to assure that the reads are valid for SQLite. |g_reads_allowed| is used
894 // to limit checking to 20MB per run of Chromium.
895 sqlite3_file* file = NULL;
896 sqlite3_int64 db_size = 0;
897 if (SQLITE_OK != GetSqlite3FileAndSize(db_, &file, &db_size)) {
898 RecordOneEvent(EVENT_MMAP_VFS_FAILURE);
899 return 0;
900 }
901
902 // Read the data left, or |g_reads_allowed|, whichever is smaller.
903 // |g_reads_allowed| limits the total amount of I/O to spend verifying data
904 // in a single Chromium run.
905 sqlite3_int64 amount = db_size - mmap_ofs;
906 if (amount < 0)
907 amount = 0;
908 if (amount > 0) {
909 base::AutoLock lock(g_sqlite_init_lock.Get());
910 static sqlite3_int64 g_reads_allowed = 20 * 1024 * 1024;
911 if (g_reads_allowed < amount)
912 amount = g_reads_allowed;
913 g_reads_allowed -= amount;
914 }
915
916 // |amount| can be <= 0 if |g_reads_allowed| ran out of quota, or if the
917 // database was truncated after a previous pass.
918 if (amount <= 0 && mmap_ofs < db_size) {
919 DCHECK_EQ(0, amount);
920 RecordOneEvent(EVENT_MMAP_SUCCESS_NO_PROGRESS);
921 } else {
922 static const int kPageSize = 4096;
923 char buf[kPageSize];
924 while (amount > 0) {
925 int rc = file->pMethods->xRead(file, buf, sizeof(buf), mmap_ofs);
926 if (rc == SQLITE_OK) {
927 mmap_ofs += sizeof(buf);
928 amount -= sizeof(buf);
929 } else if (rc == SQLITE_IOERR_SHORT_READ) {
930 // Reached EOF for a database with page size < |kPageSize|.
931 mmap_ofs = db_size;
932 break;
933 } else {
934 // TODO(shess): Consider calling OnSqliteError().
shess9bf2c672015-12-18 01:18:08935 mmap_ofs = MetaTable::kMmapFailure;
shessd90aeea82015-11-13 02:24:31936 break;
937 }
938 }
939
940 // Log these events after update to distinguish meta update failure.
941 Events event;
942 if (mmap_ofs >= db_size) {
shess9bf2c672015-12-18 01:18:08943 mmap_ofs = MetaTable::kMmapSuccess;
shessd90aeea82015-11-13 02:24:31944 event = EVENT_MMAP_SUCCESS_NEW;
945 } else if (mmap_ofs > 0) {
946 event = EVENT_MMAP_SUCCESS_PARTIAL;
947 } else {
shess9bf2c672015-12-18 01:18:08948 DCHECK_EQ(MetaTable::kMmapFailure, mmap_ofs);
shessd90aeea82015-11-13 02:24:31949 event = EVENT_MMAP_FAILED_NEW;
950 }
951
shess9bf2c672015-12-18 01:18:08952 if (!MetaTable::SetMmapStatus(this, mmap_ofs)) {
shessd90aeea82015-11-13 02:24:31953 RecordOneEvent(EVENT_MMAP_META_FAILURE_UPDATE);
954 return 0;
955 }
956
957 RecordOneEvent(event);
958 }
959 }
960
shess9bf2c672015-12-18 01:18:08961 if (mmap_ofs == MetaTable::kMmapFailure)
shessd90aeea82015-11-13 02:24:31962 return 0;
shess9bf2c672015-12-18 01:18:08963 if (mmap_ofs == MetaTable::kMmapSuccess)
964 return kMmapEverything;
shessd90aeea82015-11-13 02:24:31965 return mmap_ofs;
966}
967
[email protected]be7995f12013-07-18 18:49:14968void Connection::TrimMemory(bool aggressively) {
969 if (!db_)
970 return;
971
972 // TODO(shess): investigate using sqlite3_db_release_memory() when possible.
973 int original_cache_size;
974 {
975 Statement sql_get_original(GetUniqueStatement("PRAGMA cache_size"));
976 if (!sql_get_original.Step()) {
977 DLOG(WARNING) << "Could not get cache size " << GetErrorMessage();
978 return;
979 }
980 original_cache_size = sql_get_original.ColumnInt(0);
981 }
982 int shrink_cache_size = aggressively ? 1 : (original_cache_size / 2);
983
984 // Force sqlite to try to reduce page cache usage.
985 const std::string sql_shrink =
986 base::StringPrintf("PRAGMA cache_size=%d", shrink_cache_size);
987 if (!Execute(sql_shrink.c_str()))
988 DLOG(WARNING) << "Could not shrink cache size: " << GetErrorMessage();
989
990 // Restore cache size.
991 const std::string sql_restore =
992 base::StringPrintf("PRAGMA cache_size=%d", original_cache_size);
993 if (!Execute(sql_restore.c_str()))
994 DLOG(WARNING) << "Could not restore cache size: " << GetErrorMessage();
995}
996
[email protected]8e0c01282012-04-06 19:36:49997// Create an in-memory database with the existing database's page
998// size, then backup that database over the existing database.
999bool Connection::Raze() {
[email protected]35f7e5392012-07-27 19:54:501000 AssertIOAllowed();
1001
[email protected]8e0c01282012-04-06 19:36:491002 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381003 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:491004 return false;
1005 }
1006
1007 if (transaction_nesting_ > 0) {
1008 DLOG(FATAL) << "Cannot raze within a transaction";
1009 return false;
1010 }
1011
1012 sql::Connection null_db;
1013 if (!null_db.OpenInMemory()) {
1014 DLOG(FATAL) << "Unable to open in-memory database.";
1015 return false;
1016 }
1017
[email protected]6d42f152012-11-10 00:38:241018 if (page_size_) {
1019 // Enforce SQLite restrictions on |page_size_|.
1020 DCHECK(!(page_size_ & (page_size_ - 1)))
1021 << " page_size_ " << page_size_ << " is not a power of two.";
1022 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
1023 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:041024 const std::string sql =
1025 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]69c58452012-08-06 19:22:421026 if (!null_db.Execute(sql.c_str()))
1027 return false;
1028 }
1029
[email protected]6d42f152012-11-10 00:38:241030#if defined(OS_ANDROID)
1031 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
1032 // in-memory databases do not respect this define.
1033 // TODO(shess): Figure out a way to set this without using platform
1034 // specific code. AFAICT from sqlite3.c, the only way to do it
1035 // would be to create an actual filesystem database, which is
1036 // unfortunate.
1037 if (!null_db.Execute("PRAGMA auto_vacuum = 1"))
1038 return false;
1039#endif
[email protected]8e0c01282012-04-06 19:36:491040
1041 // The page size doesn't take effect until a database has pages, and
1042 // at this point the null database has none. Changing the schema
1043 // version will create the first page. This will not affect the
1044 // schema version in the resulting database, as SQLite's backup
1045 // implementation propagates the schema version from the original
1046 // connection to the new version of the database, incremented by one
1047 // so that other readers see the schema change and act accordingly.
1048 if (!null_db.Execute("PRAGMA schema_version = 1"))
1049 return false;
1050
[email protected]6d42f152012-11-10 00:38:241051 // SQLite tracks the expected number of database pages in the first
1052 // page, and if it does not match the total retrieved from a
1053 // filesystem call, treats the database as corrupt. This situation
1054 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
1055 // used to hint to SQLite to soldier on in that case, specifically
1056 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
1057 // sqlite3.c lockBtree().]
1058 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
1059 // page_size" can be used to query such a database.
1060 ScopedWritableSchema writable_schema(db_);
1061
[email protected]7bae5742013-07-10 20:46:161062 const char* kMain = "main";
1063 int rc = BackupDatabase(null_db.db_, db_, kMain);
1064 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase",rc);
[email protected]8e0c01282012-04-06 19:36:491065
1066 // The destination database was locked.
1067 if (rc == SQLITE_BUSY) {
1068 return false;
1069 }
1070
[email protected]7bae5742013-07-10 20:46:161071 // SQLITE_NOTADB can happen if page 1 of db_ exists, but is not
1072 // formatted correctly. SQLITE_IOERR_SHORT_READ can happen if db_
1073 // isn't even big enough for one page. Either way, reach in and
1074 // truncate it before trying again.
1075 // TODO(shess): Maybe it would be worthwhile to just truncate from
1076 // the get-go?
1077 if (rc == SQLITE_NOTADB || rc == SQLITE_IOERR_SHORT_READ) {
1078 sqlite3_file* file = NULL;
[email protected]8ada10f2013-12-21 00:42:341079 rc = GetSqlite3File(db_, &file);
[email protected]7bae5742013-07-10 20:46:161080 if (rc != SQLITE_OK) {
1081 DLOG(FATAL) << "Failure getting file handle.";
1082 return false;
[email protected]7bae5742013-07-10 20:46:161083 }
1084
1085 rc = file->pMethods->xTruncate(file, 0);
1086 if (rc != SQLITE_OK) {
1087 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabaseTruncate",rc);
1088 DLOG(FATAL) << "Failed to truncate file.";
1089 return false;
1090 }
1091
1092 rc = BackupDatabase(null_db.db_, db_, kMain);
1093 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase2",rc);
1094
1095 if (rc != SQLITE_DONE) {
1096 DLOG(FATAL) << "Failed retrying Raze().";
1097 }
1098 }
1099
[email protected]8e0c01282012-04-06 19:36:491100 // The entire database should have been backed up.
1101 if (rc != SQLITE_DONE) {
[email protected]7bae5742013-07-10 20:46:161102 // TODO(shess): Figure out which other cases can happen.
[email protected]8e0c01282012-04-06 19:36:491103 DLOG(FATAL) << "Unable to copy entire null database.";
1104 return false;
1105 }
1106
[email protected]8e0c01282012-04-06 19:36:491107 return true;
1108}
1109
1110bool Connection::RazeWithTimout(base::TimeDelta timeout) {
1111 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381112 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:491113 return false;
1114 }
1115
1116 ScopedBusyTimeout busy_timeout(db_);
1117 busy_timeout.SetTimeout(timeout);
1118 return Raze();
1119}
1120
[email protected]41a97c812013-02-07 02:35:381121bool Connection::RazeAndClose() {
1122 if (!db_) {
1123 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
1124 return false;
1125 }
1126
1127 // Raze() cannot run in a transaction.
[email protected]8d409412013-07-19 18:25:301128 RollbackAllTransactions();
[email protected]41a97c812013-02-07 02:35:381129
1130 bool result = Raze();
1131
1132 CloseInternal(true);
1133
1134 // Mark the database so that future API calls fail appropriately,
1135 // but don't DCHECK (because after calling this function they are
1136 // expected to fail).
1137 poisoned_ = true;
1138
1139 return result;
1140}
1141
[email protected]8d409412013-07-19 18:25:301142void Connection::Poison() {
1143 if (!db_) {
1144 DLOG_IF(FATAL, !poisoned_) << "Cannot poison null db";
1145 return;
1146 }
1147
1148 RollbackAllTransactions();
1149 CloseInternal(true);
1150
1151 // Mark the database so that future API calls fail appropriately,
1152 // but don't DCHECK (because after calling this function they are
1153 // expected to fail).
1154 poisoned_ = true;
1155}
1156
[email protected]8d2e39e2013-06-24 05:55:081157// TODO(shess): To the extent possible, figure out the optimal
1158// ordering for these deletes which will prevent other connections
1159// from seeing odd behavior. For instance, it may be necessary to
1160// manually lock the main database file in a SQLite-compatible fashion
1161// (to prevent other processes from opening it), then delete the
1162// journal files, then delete the main database file. Another option
1163// might be to lock the main database file and poison the header with
1164// junk to prevent other processes from opening it successfully (like
1165// Gears "SQLite poison 3" trick).
1166//
1167// static
1168bool Connection::Delete(const base::FilePath& path) {
1169 base::ThreadRestrictions::AssertIOAllowed();
1170
1171 base::FilePath journal_path(path.value() + FILE_PATH_LITERAL("-journal"));
1172 base::FilePath wal_path(path.value() + FILE_PATH_LITERAL("-wal"));
1173
erg102ceb412015-06-20 01:38:131174 std::string journal_str = AsUTF8ForSQL(journal_path);
1175 std::string wal_str = AsUTF8ForSQL(wal_path);
1176 std::string path_str = AsUTF8ForSQL(path);
[email protected]8d2e39e2013-06-24 05:55:081177
shess702467622015-09-16 19:04:551178 // Make sure sqlite3_initialize() is called before anything else.
1179 InitializeSqlite();
1180
erg102ceb412015-06-20 01:38:131181 sqlite3_vfs* vfs = sqlite3_vfs_find(NULL);
1182 CHECK(vfs);
1183 CHECK(vfs->xDelete);
1184 CHECK(vfs->xAccess);
1185
1186 // We only work with unix, win32 and mojo filesystems. If you're trying to
1187 // use this code with any other VFS, you're not in a good place.
1188 CHECK(strncmp(vfs->zName, "unix", 4) == 0 ||
1189 strncmp(vfs->zName, "win32", 5) == 0 ||
1190 strcmp(vfs->zName, "mojo") == 0);
1191
1192 vfs->xDelete(vfs, journal_str.c_str(), 0);
1193 vfs->xDelete(vfs, wal_str.c_str(), 0);
1194 vfs->xDelete(vfs, path_str.c_str(), 0);
1195
1196 int journal_exists = 0;
1197 vfs->xAccess(vfs, journal_str.c_str(), SQLITE_ACCESS_EXISTS,
1198 &journal_exists);
1199
1200 int wal_exists = 0;
1201 vfs->xAccess(vfs, wal_str.c_str(), SQLITE_ACCESS_EXISTS,
1202 &wal_exists);
1203
1204 int path_exists = 0;
1205 vfs->xAccess(vfs, path_str.c_str(), SQLITE_ACCESS_EXISTS,
1206 &path_exists);
1207
1208 return !journal_exists && !wal_exists && !path_exists;
[email protected]8d2e39e2013-06-24 05:55:081209}
1210
[email protected]e5ffd0e42009-09-11 21:30:561211bool Connection::BeginTransaction() {
1212 if (needs_rollback_) {
[email protected]88563f62011-03-13 22:13:331213 DCHECK_GT(transaction_nesting_, 0);
[email protected]e5ffd0e42009-09-11 21:30:561214
1215 // When we're going to rollback, fail on this begin and don't actually
1216 // mark us as entering the nested transaction.
1217 return false;
1218 }
1219
1220 bool success = true;
1221 if (!transaction_nesting_) {
1222 needs_rollback_ = false;
1223
1224 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
shess58b8df82015-06-03 00:19:321225 RecordOneEvent(EVENT_BEGIN);
[email protected]eff1fa522011-12-12 23:50:591226 if (!begin.Run())
[email protected]e5ffd0e42009-09-11 21:30:561227 return false;
1228 }
1229 transaction_nesting_++;
1230 return success;
1231}
1232
1233void Connection::RollbackTransaction() {
1234 if (!transaction_nesting_) {
[email protected]41a97c812013-02-07 02:35:381235 DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561236 return;
1237 }
1238
1239 transaction_nesting_--;
1240
1241 if (transaction_nesting_ > 0) {
1242 // Mark the outermost transaction as needing rollback.
1243 needs_rollback_ = true;
1244 return;
1245 }
1246
1247 DoRollback();
1248}
1249
1250bool Connection::CommitTransaction() {
1251 if (!transaction_nesting_) {
shess90244e12015-11-09 22:08:181252 DLOG_IF(FATAL, !poisoned_) << "Committing a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561253 return false;
1254 }
1255 transaction_nesting_--;
1256
1257 if (transaction_nesting_ > 0) {
1258 // Mark any nested transactions as failing after we've already got one.
1259 return !needs_rollback_;
1260 }
1261
1262 if (needs_rollback_) {
1263 DoRollback();
1264 return false;
1265 }
1266
1267 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
shess58b8df82015-06-03 00:19:321268
1269 // Collect the commit time manually, sql::Statement would register it as query
1270 // time only.
1271 const base::TimeTicks before = Now();
1272 bool ret = commit.RunWithoutTimers();
1273 const base::TimeDelta delta = Now() - before;
1274
1275 RecordCommitTime(delta);
1276 RecordOneEvent(EVENT_COMMIT);
1277
shess7dbd4dee2015-10-06 17:39:161278 // Release dirty cache pages after the transaction closes.
1279 ReleaseCacheMemoryIfNeeded(false);
1280
shess58b8df82015-06-03 00:19:321281 return ret;
[email protected]e5ffd0e42009-09-11 21:30:561282}
1283
[email protected]8d409412013-07-19 18:25:301284void Connection::RollbackAllTransactions() {
1285 if (transaction_nesting_ > 0) {
1286 transaction_nesting_ = 0;
1287 DoRollback();
1288 }
1289}
1290
1291bool Connection::AttachDatabase(const base::FilePath& other_db_path,
1292 const char* attachment_point) {
1293 DCHECK(ValidAttachmentPoint(attachment_point));
1294
1295 Statement s(GetUniqueStatement("ATTACH DATABASE ? AS ?"));
1296#if OS_WIN
1297 s.BindString16(0, other_db_path.value());
1298#else
1299 s.BindString(0, other_db_path.value());
1300#endif
1301 s.BindString(1, attachment_point);
1302 return s.Run();
1303}
1304
1305bool Connection::DetachDatabase(const char* attachment_point) {
1306 DCHECK(ValidAttachmentPoint(attachment_point));
1307
1308 Statement s(GetUniqueStatement("DETACH DATABASE ?"));
1309 s.BindString(0, attachment_point);
1310 return s.Run();
1311}
1312
shess58b8df82015-06-03 00:19:321313// TODO(shess): Consider changing this to execute exactly one statement. If a
1314// caller wishes to execute multiple statements, that should be explicit, and
1315// perhaps tucked into an explicit transaction with rollback in case of error.
[email protected]eff1fa522011-12-12 23:50:591316int Connection::ExecuteAndReturnErrorCode(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501317 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:381318 if (!db_) {
1319 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
1320 return SQLITE_ERROR;
1321 }
shess58b8df82015-06-03 00:19:321322 DCHECK(sql);
1323
1324 RecordOneEvent(EVENT_EXECUTE);
1325 int rc = SQLITE_OK;
1326 while ((rc == SQLITE_OK) && *sql) {
1327 sqlite3_stmt *stmt = NULL;
1328 const char *leftover_sql;
1329
1330 const base::TimeTicks before = Now();
1331 rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, &leftover_sql);
1332 sql = leftover_sql;
1333
1334 // Stop if an error is encountered.
1335 if (rc != SQLITE_OK)
1336 break;
1337
1338 // This happens if |sql| originally only contained comments or whitespace.
1339 // TODO(shess): Audit to see if this can become a DCHECK(). Having
1340 // extraneous comments and whitespace in the SQL statements increases
1341 // runtime cost and can easily be shifted out to the C++ layer.
1342 if (!stmt)
1343 continue;
1344
1345 // Save for use after statement is finalized.
1346 const bool read_only = !!sqlite3_stmt_readonly(stmt);
1347
1348 RecordOneEvent(Connection::EVENT_STATEMENT_RUN);
1349 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1350 // TODO(shess): Audit to see if this can become a DCHECK. I think PRAGMA
1351 // is the only legitimate case for this.
1352 RecordOneEvent(Connection::EVENT_STATEMENT_ROWS);
1353 }
1354
1355 // sqlite3_finalize() returns SQLITE_OK if the most recent sqlite3_step()
1356 // returned SQLITE_DONE or SQLITE_ROW, otherwise the error code.
1357 rc = sqlite3_finalize(stmt);
1358 if (rc == SQLITE_OK)
1359 RecordOneEvent(Connection::EVENT_STATEMENT_SUCCESS);
1360
1361 // sqlite3_exec() does this, presumably to avoid spinning the parser for
1362 // trailing whitespace.
1363 // TODO(shess): Audit to see if this can become a DCHECK.
brettwb3413062015-06-24 00:39:021364 while (base::IsAsciiWhitespace(*sql)) {
shess58b8df82015-06-03 00:19:321365 sql++;
1366 }
1367
1368 const base::TimeDelta delta = Now() - before;
1369 RecordTimeAndChanges(delta, read_only);
1370 }
shess7dbd4dee2015-10-06 17:39:161371
1372 // Most calls to Execute() modify the database. The main exceptions would be
1373 // calls such as CREATE TABLE IF NOT EXISTS which could modify the database
1374 // but sometimes don't.
1375 ReleaseCacheMemoryIfNeeded(true);
1376
shess58b8df82015-06-03 00:19:321377 return rc;
[email protected]eff1fa522011-12-12 23:50:591378}
1379
1380bool Connection::Execute(const char* sql) {
[email protected]41a97c812013-02-07 02:35:381381 if (!db_) {
1382 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
1383 return false;
1384 }
1385
[email protected]eff1fa522011-12-12 23:50:591386 int error = ExecuteAndReturnErrorCode(sql);
[email protected]473ad792012-11-10 00:55:001387 if (error != SQLITE_OK)
[email protected]2f496b42013-09-26 18:36:581388 error = OnSqliteError(error, NULL, sql);
[email protected]473ad792012-11-10 00:55:001389
[email protected]28fe0ff2012-02-25 00:40:331390 // This needs to be a FATAL log because the error case of arriving here is
1391 // that there's a malformed SQL statement. This can arise in development if
[email protected]4350e322013-06-18 22:18:101392 // a change alters the schema but not all queries adjust. This can happen
1393 // in production if the schema is corrupted.
[email protected]eff1fa522011-12-12 23:50:591394 if (error == SQLITE_ERROR)
[email protected]28fe0ff2012-02-25 00:40:331395 DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage();
[email protected]eff1fa522011-12-12 23:50:591396 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:561397}
1398
[email protected]5b96f3772010-09-28 16:30:571399bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
[email protected]41a97c812013-02-07 02:35:381400 if (!db_) {
1401 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]5b96f3772010-09-28 16:30:571402 return false;
[email protected]41a97c812013-02-07 02:35:381403 }
[email protected]5b96f3772010-09-28 16:30:571404
1405 ScopedBusyTimeout busy_timeout(db_);
1406 busy_timeout.SetTimeout(timeout);
[email protected]eff1fa522011-12-12 23:50:591407 return Execute(sql);
[email protected]5b96f3772010-09-28 16:30:571408}
1409
[email protected]e5ffd0e42009-09-11 21:30:561410bool Connection::HasCachedStatement(const StatementID& id) const {
1411 return statement_cache_.find(id) != statement_cache_.end();
1412}
1413
1414scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement(
1415 const StatementID& id,
1416 const char* sql) {
1417 CachedStatementMap::iterator i = statement_cache_.find(id);
1418 if (i != statement_cache_.end()) {
1419 // Statement is in the cache. It should still be active (we're the only
1420 // one invalidating cached statements, and we'll remove it from the cache
1421 // if we do that. Make sure we reset it before giving out the cached one in
1422 // case it still has some stuff bound.
1423 DCHECK(i->second->is_valid());
1424 sqlite3_reset(i->second->stmt());
1425 return i->second;
1426 }
1427
1428 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
1429 if (statement->is_valid())
1430 statement_cache_[id] = statement; // Only cache valid statements.
1431 return statement;
1432}
1433
1434scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement(
1435 const char* sql) {
shess9e77283d2016-06-13 23:53:201436 return GetStatementImpl(this, sql);
1437}
1438
1439scoped_refptr<Connection::StatementRef> Connection::GetStatementImpl(
1440 sql::Connection* tracking_db, const char* sql) const {
[email protected]35f7e5392012-07-27 19:54:501441 AssertIOAllowed();
shess9e77283d2016-06-13 23:53:201442 DCHECK(sql);
1443 DCHECK(!tracking_db || const_cast<Connection*>(tracking_db)==this);
[email protected]35f7e5392012-07-27 19:54:501444
[email protected]41a97c812013-02-07 02:35:381445 // Return inactive statement.
[email protected]e5ffd0e42009-09-11 21:30:561446 if (!db_)
[email protected]41a97c812013-02-07 02:35:381447 return new StatementRef(NULL, NULL, poisoned_);
[email protected]e5ffd0e42009-09-11 21:30:561448
1449 sqlite3_stmt* stmt = NULL;
[email protected]473ad792012-11-10 00:55:001450 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
1451 if (rc != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:591452 // This is evidence of a syntax error in the incoming SQL.
shess9e77283d2016-06-13 23:53:201453 if (rc == SQLITE_ERROR)
shess193bfb622015-04-10 22:30:021454 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]473ad792012-11-10 00:55:001455
1456 // It could also be database corruption.
[email protected]2f496b42013-09-26 18:36:581457 OnSqliteError(rc, NULL, sql);
[email protected]41a97c812013-02-07 02:35:381458 return new StatementRef(NULL, NULL, false);
[email protected]e5ffd0e42009-09-11 21:30:561459 }
shess9e77283d2016-06-13 23:53:201460 return new StatementRef(tracking_db, stmt, true);
[email protected]e5ffd0e42009-09-11 21:30:561461}
1462
[email protected]2eec0a22012-07-24 01:59:581463scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
1464 const char* sql) const {
shess9e77283d2016-06-13 23:53:201465 return GetStatementImpl(NULL, sql);
[email protected]2eec0a22012-07-24 01:59:581466}
1467
[email protected]92cd00a2013-08-16 11:09:581468std::string Connection::GetSchema() const {
1469 // The ORDER BY should not be necessary, but relying on organic
1470 // order for something like this is questionable.
1471 const char* kSql =
1472 "SELECT type, name, tbl_name, sql "
1473 "FROM sqlite_master ORDER BY 1, 2, 3, 4";
1474 Statement statement(GetUntrackedStatement(kSql));
1475
1476 std::string schema;
1477 while (statement.Step()) {
1478 schema += statement.ColumnString(0);
1479 schema += '|';
1480 schema += statement.ColumnString(1);
1481 schema += '|';
1482 schema += statement.ColumnString(2);
1483 schema += '|';
1484 schema += statement.ColumnString(3);
1485 schema += '\n';
1486 }
1487
1488 return schema;
1489}
1490
[email protected]eff1fa522011-12-12 23:50:591491bool Connection::IsSQLValid(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501492 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:381493 if (!db_) {
1494 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
1495 return false;
1496 }
1497
[email protected]eff1fa522011-12-12 23:50:591498 sqlite3_stmt* stmt = NULL;
1499 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK)
1500 return false;
1501
1502 sqlite3_finalize(stmt);
1503 return true;
1504}
1505
[email protected]1ed78a32009-09-15 20:24:171506bool Connection::DoesTableExist(const char* table_name) const {
[email protected]e2cadec82011-12-13 02:00:531507 return DoesTableOrIndexExist(table_name, "table");
1508}
1509
1510bool Connection::DoesIndexExist(const char* index_name) const {
1511 return DoesTableOrIndexExist(index_name, "index");
1512}
1513
1514bool Connection::DoesTableOrIndexExist(
1515 const char* name, const char* type) const {
shess92a2ab12015-04-09 01:59:471516 const char* kSql =
1517 "SELECT name FROM sqlite_master WHERE type=? AND name=? COLLATE NOCASE";
[email protected]2eec0a22012-07-24 01:59:581518 Statement statement(GetUntrackedStatement(kSql));
shess92a2ab12015-04-09 01:59:471519
shess976814402016-06-21 06:56:251520 // This can happen if the database is corrupt and the error is a test
1521 // expectation.
shess92a2ab12015-04-09 01:59:471522 if (!statement.is_valid())
1523 return false;
1524
[email protected]e2cadec82011-12-13 02:00:531525 statement.BindString(0, type);
1526 statement.BindString(1, name);
[email protected]28fe0ff2012-02-25 00:40:331527
[email protected]e5ffd0e42009-09-11 21:30:561528 return statement.Step(); // Table exists if any row was returned.
1529}
1530
1531bool Connection::DoesColumnExist(const char* table_name,
[email protected]1ed78a32009-09-15 20:24:171532 const char* column_name) const {
[email protected]e5ffd0e42009-09-11 21:30:561533 std::string sql("PRAGMA TABLE_INFO(");
1534 sql.append(table_name);
1535 sql.append(")");
1536
[email protected]2eec0a22012-07-24 01:59:581537 Statement statement(GetUntrackedStatement(sql.c_str()));
shess92a2ab12015-04-09 01:59:471538
shess976814402016-06-21 06:56:251539 // This can happen if the database is corrupt and the error is a test
1540 // expectation.
shess92a2ab12015-04-09 01:59:471541 if (!statement.is_valid())
1542 return false;
1543
[email protected]e5ffd0e42009-09-11 21:30:561544 while (statement.Step()) {
brettw8a800902015-07-10 18:28:331545 if (base::EqualsCaseInsensitiveASCII(statement.ColumnString(1),
1546 column_name))
[email protected]e5ffd0e42009-09-11 21:30:561547 return true;
1548 }
1549 return false;
1550}
1551
tfarina720d4f32015-05-11 22:31:261552int64_t Connection::GetLastInsertRowId() const {
[email protected]e5ffd0e42009-09-11 21:30:561553 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381554 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]e5ffd0e42009-09-11 21:30:561555 return 0;
1556 }
1557 return sqlite3_last_insert_rowid(db_);
1558}
1559
[email protected]1ed78a32009-09-15 20:24:171560int Connection::GetLastChangeCount() const {
1561 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381562 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]1ed78a32009-09-15 20:24:171563 return 0;
1564 }
1565 return sqlite3_changes(db_);
1566}
1567
[email protected]e5ffd0e42009-09-11 21:30:561568int Connection::GetErrorCode() const {
1569 if (!db_)
1570 return SQLITE_ERROR;
1571 return sqlite3_errcode(db_);
1572}
1573
[email protected]767718e52010-09-21 23:18:491574int Connection::GetLastErrno() const {
1575 if (!db_)
1576 return -1;
1577
1578 int err = 0;
1579 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err))
1580 return -2;
1581
1582 return err;
1583}
1584
[email protected]e5ffd0e42009-09-11 21:30:561585const char* Connection::GetErrorMessage() const {
1586 if (!db_)
1587 return "sql::Connection has no connection.";
1588 return sqlite3_errmsg(db_);
1589}
1590
[email protected]fed734a2013-07-17 04:45:131591bool Connection::OpenInternal(const std::string& file_name,
1592 Connection::Retry retry_flag) {
[email protected]35f7e5392012-07-27 19:54:501593 AssertIOAllowed();
1594
[email protected]9cfbc922009-11-17 20:13:171595 if (db_) {
[email protected]eff1fa522011-12-12 23:50:591596 DLOG(FATAL) << "sql::Connection is already open.";
[email protected]9cfbc922009-11-17 20:13:171597 return false;
1598 }
1599
[email protected]a7ec1292013-07-22 22:02:181600 // Make sure sqlite3_initialize() is called before anything else.
1601 InitializeSqlite();
1602
shess58b8df82015-06-03 00:19:321603 // Setup the stats histograms immediately rather than allocating lazily.
1604 // Connections which won't exercise all of these probably shouldn't exist.
1605 if (!histogram_tag_.empty()) {
1606 stats_histogram_ =
1607 base::LinearHistogram::FactoryGet(
1608 "Sqlite.Stats." + histogram_tag_,
1609 1, EVENT_MAX_VALUE, EVENT_MAX_VALUE + 1,
1610 base::HistogramBase::kUmaTargetedHistogramFlag);
1611
1612 // The timer setup matches UMA_HISTOGRAM_MEDIUM_TIMES(). 3 minutes is an
1613 // unreasonable time for any single operation, so there is not much value to
1614 // knowing if it was 3 minutes or 5 minutes. In reality at that point
1615 // things are entirely busted.
1616 commit_time_histogram_ =
1617 GetMediumTimeHistogram("Sqlite.CommitTime." + histogram_tag_);
1618
1619 autocommit_time_histogram_ =
1620 GetMediumTimeHistogram("Sqlite.AutoCommitTime." + histogram_tag_);
1621
1622 update_time_histogram_ =
1623 GetMediumTimeHistogram("Sqlite.UpdateTime." + histogram_tag_);
1624
1625 query_time_histogram_ =
1626 GetMediumTimeHistogram("Sqlite.QueryTime." + histogram_tag_);
1627 }
1628
[email protected]41a97c812013-02-07 02:35:381629 // If |poisoned_| is set, it means an error handler called
1630 // RazeAndClose(). Until regular Close() is called, the caller
1631 // should be treating the database as open, but is_open() currently
1632 // only considers the sqlite3 handle's state.
1633 // TODO(shess): Revise is_open() to consider poisoned_, and review
1634 // to see if any non-testing code even depends on it.
1635 DLOG_IF(FATAL, poisoned_) << "sql::Connection is already open.";
[email protected]7bae5742013-07-10 20:46:161636 poisoned_ = false;
[email protected]41a97c812013-02-07 02:35:381637
[email protected]765b44502009-10-02 05:01:421638 int err = sqlite3_open(file_name.c_str(), &db_);
1639 if (err != SQLITE_OK) {
[email protected]73fb8d52013-07-24 05:04:281640 // Extended error codes cannot be enabled until a handle is
1641 // available, fetch manually.
1642 err = sqlite3_extended_errcode(db_);
1643
[email protected]bd2ccdb4a2012-12-07 22:14:501644 // Histogram failures specific to initial open for debugging
1645 // purposes.
[email protected]73fb8d52013-07-24 05:04:281646 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenFailure", err);
[email protected]bd2ccdb4a2012-12-07 22:14:501647
[email protected]2f496b42013-09-26 18:36:581648 OnSqliteError(err, NULL, "-- sqlite3_open()");
[email protected]fed734a2013-07-17 04:45:131649 bool was_poisoned = poisoned_;
[email protected]64021042012-02-10 20:02:291650 Close();
[email protected]fed734a2013-07-17 04:45:131651
1652 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1653 return OpenInternal(file_name, NO_RETRY);
[email protected]765b44502009-10-02 05:01:421654 return false;
1655 }
1656
[email protected]81a2a602013-07-17 19:10:361657 // TODO(shess): OS_WIN support?
1658#if defined(OS_POSIX)
1659 if (restrict_to_user_) {
1660 DCHECK_NE(file_name, std::string(":memory"));
1661 base::FilePath file_path(file_name);
1662 int mode = 0;
1663 // TODO(shess): Arguably, failure to retrieve and change
1664 // permissions should be fatal if the file exists.
[email protected]b264eab2013-11-27 23:22:081665 if (base::GetPosixFilePermissions(file_path, &mode)) {
1666 mode &= base::FILE_PERMISSION_USER_MASK;
1667 base::SetPosixFilePermissions(file_path, mode);
[email protected]81a2a602013-07-17 19:10:361668
1669 // SQLite sets the permissions on these files from the main
1670 // database on create. Set them here in case they already exist
1671 // at this point. Failure to set these permissions should not
1672 // be fatal unless the file doesn't exist.
1673 base::FilePath journal_path(file_name + FILE_PATH_LITERAL("-journal"));
1674 base::FilePath wal_path(file_name + FILE_PATH_LITERAL("-wal"));
[email protected]b264eab2013-11-27 23:22:081675 base::SetPosixFilePermissions(journal_path, mode);
1676 base::SetPosixFilePermissions(wal_path, mode);
[email protected]81a2a602013-07-17 19:10:361677 }
1678 }
1679#endif // defined(OS_POSIX)
1680
[email protected]affa2da2013-06-06 22:20:341681 // SQLite uses a lookaside buffer to improve performance of small mallocs.
1682 // Chromium already depends on small mallocs being efficient, so we disable
1683 // this to avoid the extra memory overhead.
1684 // This must be called immediatly after opening the database before any SQL
1685 // statements are run.
1686 sqlite3_db_config(db_, SQLITE_DBCONFIG_LOOKASIDE, NULL, 0, 0);
1687
[email protected]73fb8d52013-07-24 05:04:281688 // Enable extended result codes to provide more color on I/O errors.
1689 // Not having extended result codes is not a fatal problem, as
1690 // Chromium code does not attempt to handle I/O errors anyhow. The
1691 // current implementation always returns SQLITE_OK, the DCHECK is to
1692 // quickly notify someone if SQLite changes.
1693 err = sqlite3_extended_result_codes(db_, 1);
1694 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
1695
[email protected]bd2ccdb4a2012-12-07 22:14:501696 // sqlite3_open() does not actually read the database file (unless a
1697 // hot journal is found). Successfully executing this pragma on an
1698 // existing database requires a valid header on page 1.
1699 // TODO(shess): For now, just probing to see what the lay of the
1700 // land is. If it's mostly SQLITE_NOTADB, then the database should
1701 // be razed.
1702 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
1703 if (err != SQLITE_OK)
[email protected]73fb8d52013-07-24 05:04:281704 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenProbeFailure", err);
[email protected]658f8332010-09-18 04:40:431705
[email protected]2e1cee762013-07-09 14:40:001706#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
1707 // The version of SQLite shipped with iOS doesn't enable ICU, which includes
1708 // REGEXP support. Add it in dynamically.
1709 err = sqlite3IcuInit(db_);
1710 DCHECK_EQ(err, SQLITE_OK) << "Could not enable ICU support";
1711#endif // OS_IOS && USE_SYSTEM_SQLITE
1712
[email protected]5b96f3772010-09-28 16:30:571713 // If indicated, lock up the database before doing anything else, so
1714 // that the following code doesn't have to deal with locking.
1715 // TODO(shess): This code is brittle. Find the cases where code
1716 // doesn't request |exclusive_locking_| and audit that it does the
1717 // right thing with SQLITE_BUSY, and that it doesn't make
1718 // assumptions about who might change things in the database.
1719 // http://crbug.com/56559
1720 if (exclusive_locking_) {
[email protected]4350e322013-06-18 22:18:101721 // TODO(shess): This should probably be a failure. Code which
1722 // requests exclusive locking but doesn't get it is almost certain
1723 // to be ill-tested.
1724 ignore_result(Execute("PRAGMA locking_mode=EXCLUSIVE"));
[email protected]5b96f3772010-09-28 16:30:571725 }
1726
[email protected]4e179ba2012-03-17 16:06:471727 // http://www.sqlite.org/pragma.html#pragma_journal_mode
1728 // DELETE (default) - delete -journal file to commit.
1729 // TRUNCATE - truncate -journal file to commit.
1730 // PERSIST - zero out header of -journal file to commit.
shess2c21ecf2015-06-02 01:31:091731 // TRUNCATE should be faster than DELETE because it won't need directory
1732 // changes for each transaction. PERSIST may break the spirit of using
1733 // secure_delete.
1734 ignore_result(Execute("PRAGMA journal_mode = TRUNCATE"));
[email protected]4e179ba2012-03-17 16:06:471735
[email protected]c68ce172011-11-24 22:30:271736 const base::TimeDelta kBusyTimeout =
1737 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
1738
[email protected]765b44502009-10-02 05:01:421739 if (page_size_ != 0) {
[email protected]5b96f3772010-09-28 16:30:571740 // Enforce SQLite restrictions on |page_size_|.
1741 DCHECK(!(page_size_ & (page_size_ - 1)))
1742 << " page_size_ " << page_size_ << " is not a power of two.";
[email protected]6d42f152012-11-10 00:38:241743 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
[email protected]5b96f3772010-09-28 16:30:571744 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:041745 const std::string sql =
1746 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]4350e322013-06-18 22:18:101747 ignore_result(ExecuteWithTimeout(sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421748 }
1749
1750 if (cache_size_ != 0) {
[email protected]7d3cbc92013-03-18 22:33:041751 const std::string sql =
1752 base::StringPrintf("PRAGMA cache_size=%d", cache_size_);
[email protected]4350e322013-06-18 22:18:101753 ignore_result(ExecuteWithTimeout(sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421754 }
1755
[email protected]6e0b1442011-08-09 23:23:581756 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) {
[email protected]fed734a2013-07-17 04:45:131757 bool was_poisoned = poisoned_;
[email protected]6e0b1442011-08-09 23:23:581758 Close();
[email protected]fed734a2013-07-17 04:45:131759 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1760 return OpenInternal(file_name, NO_RETRY);
[email protected]6e0b1442011-08-09 23:23:581761 return false;
1762 }
1763
shess5dac334f2015-11-05 20:47:421764 // Set a reasonable chunk size for larger files. This reduces churn from
1765 // remapping memory on size changes. It also reduces filesystem
1766 // fragmentation.
1767 // TODO(shess): It may make sense to have this be hinted by the client.
1768 // Database sizes seem to be bimodal, some clients have consistently small
1769 // databases (<20k) while other clients have a broad distribution of sizes
1770 // (hundreds of kilobytes to many megabytes).
1771 sqlite3_file* file = NULL;
1772 sqlite3_int64 db_size = 0;
1773 int rc = GetSqlite3FileAndSize(db_, &file, &db_size);
1774 if (rc == SQLITE_OK && db_size > 16 * 1024) {
1775 int chunk_size = 4 * 1024;
1776 if (db_size > 128 * 1024)
1777 chunk_size = 32 * 1024;
1778 sqlite3_file_control(db_, NULL, SQLITE_FCNTL_CHUNK_SIZE, &chunk_size);
1779 }
1780
shess2f3a814b2015-11-05 18:11:101781 // Enable memory-mapped access. The explicit-disable case is because SQLite
shessd90aeea82015-11-13 02:24:311782 // can be built to default-enable mmap. GetAppropriateMmapSize() calculates a
1783 // safe range to memory-map based on past regular I/O. This value will be
1784 // capped by SQLITE_MAX_MMAP_SIZE, which could be different between 32-bit and
1785 // 64-bit platforms.
1786 size_t mmap_size = mmap_disabled_ ? 0 : GetAppropriateMmapSize();
1787 std::string mmap_sql =
1788 base::StringPrintf("PRAGMA mmap_size = %" PRIuS, mmap_size);
1789 ignore_result(Execute(mmap_sql.c_str()));
shess2f3a814b2015-11-05 18:11:101790
1791 // Determine if memory-mapping has actually been enabled. The Execute() above
1792 // can succeed without changing the amount mapped.
1793 mmap_enabled_ = false;
1794 {
1795 Statement s(GetUniqueStatement("PRAGMA mmap_size"));
1796 if (s.Step() && s.ColumnInt64(0) > 0)
1797 mmap_enabled_ = true;
1798 }
1799
ssid3be5b1ec2016-01-13 14:21:571800 DCHECK(!memory_dump_provider_);
1801 memory_dump_provider_.reset(
1802 new ConnectionMemoryDumpProvider(db_, histogram_tag_));
1803 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
1804 memory_dump_provider_.get(), "sql::Connection", nullptr);
1805
[email protected]765b44502009-10-02 05:01:421806 return true;
1807}
1808
[email protected]e5ffd0e42009-09-11 21:30:561809void Connection::DoRollback() {
1810 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
shess58b8df82015-06-03 00:19:321811
1812 // Collect the rollback time manually, sql::Statement would register it as
1813 // query time only.
1814 const base::TimeTicks before = Now();
1815 rollback.RunWithoutTimers();
1816 const base::TimeDelta delta = Now() - before;
1817
1818 RecordUpdateTime(delta);
1819 RecordOneEvent(EVENT_ROLLBACK);
1820
shess7dbd4dee2015-10-06 17:39:161821 // The cache may have been accumulating dirty pages for commit. Note that in
1822 // some cases sql::Transaction can fire rollback after a database is closed.
1823 if (is_open())
1824 ReleaseCacheMemoryIfNeeded(false);
1825
[email protected]44ad7d902012-03-23 00:09:051826 needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:561827}
1828
1829void Connection::StatementRefCreated(StatementRef* ref) {
1830 DCHECK(open_statements_.find(ref) == open_statements_.end());
1831 open_statements_.insert(ref);
1832}
1833
1834void Connection::StatementRefDeleted(StatementRef* ref) {
1835 StatementRefSet::iterator i = open_statements_.find(ref);
1836 if (i == open_statements_.end())
[email protected]eff1fa522011-12-12 23:50:591837 DLOG(FATAL) << "Could not find statement";
[email protected]e5ffd0e42009-09-11 21:30:561838 else
1839 open_statements_.erase(i);
1840}
1841
shess58b8df82015-06-03 00:19:321842void Connection::set_histogram_tag(const std::string& tag) {
1843 DCHECK(!is_open());
1844 histogram_tag_ = tag;
1845}
1846
[email protected]210ce0af2013-05-15 09:10:391847void Connection::AddTaggedHistogram(const std::string& name,
1848 size_t sample) const {
1849 if (histogram_tag_.empty())
1850 return;
1851
1852 // TODO(shess): The histogram macros create a bit of static storage
1853 // for caching the histogram object. This code shouldn't execute
1854 // often enough for such caching to be crucial. If it becomes an
1855 // issue, the object could be cached alongside histogram_prefix_.
1856 std::string full_histogram_name = name + "." + histogram_tag_;
1857 base::HistogramBase* histogram =
1858 base::SparseHistogram::FactoryGet(
1859 full_histogram_name,
1860 base::HistogramBase::kUmaTargetedHistogramFlag);
1861 if (histogram)
1862 histogram->Add(sample);
1863}
1864
shess9e77283d2016-06-13 23:53:201865int Connection::OnSqliteError(
1866 int err, sql::Statement *stmt, const char* sql) const {
[email protected]210ce0af2013-05-15 09:10:391867 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err);
1868 AddTaggedHistogram("Sqlite.Error", err);
[email protected]c088e3a32013-01-03 23:59:141869
1870 // Always log the error.
[email protected]2f496b42013-09-26 18:36:581871 if (!sql && stmt)
1872 sql = stmt->GetSQLStatement();
1873 if (!sql)
1874 sql = "-- unknown";
shessf7e988f2015-11-13 00:41:061875
1876 std::string id = histogram_tag_;
1877 if (id.empty())
1878 id = DbPath().BaseName().AsUTF8Unsafe();
1879 LOG(ERROR) << id << " sqlite error " << err
[email protected]c088e3a32013-01-03 23:59:141880 << ", errno " << GetLastErrno()
[email protected]2f496b42013-09-26 18:36:581881 << ": " << GetErrorMessage()
1882 << ", sql: " << sql;
[email protected]c088e3a32013-01-03 23:59:141883
[email protected]c3881b372013-05-17 08:39:461884 if (!error_callback_.is_null()) {
[email protected]98cf3002013-07-12 01:38:561885 // Fire from a copy of the callback in case of reentry into
1886 // re/set_error_callback().
1887 // TODO(shess): <http://crbug.com/254584>
1888 ErrorCallback(error_callback_).Run(err, stmt);
[email protected]c3881b372013-05-17 08:39:461889 return err;
1890 }
1891
[email protected]faa604e2009-09-25 22:38:591892 // The default handling is to assert on debug and to ignore on release.
shess976814402016-06-21 06:56:251893 if (!IsExpectedSqliteError(err))
[email protected]4350e322013-06-18 22:18:101894 DLOG(FATAL) << GetErrorMessage();
[email protected]faa604e2009-09-25 22:38:591895 return err;
1896}
1897
[email protected]579446c2013-12-16 18:36:521898bool Connection::FullIntegrityCheck(std::vector<std::string>* messages) {
1899 return IntegrityCheckHelper("PRAGMA integrity_check", messages);
1900}
1901
1902bool Connection::QuickIntegrityCheck() {
1903 std::vector<std::string> messages;
1904 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages))
1905 return false;
1906 return messages.size() == 1 && messages[0] == "ok";
1907}
1908
afakhry7c9abe72016-08-05 17:33:191909std::string Connection::GetDiagnosticInfo(int extended_error,
1910 Statement* statement) {
1911 // Prevent reentrant calls to the error callback.
1912 ErrorCallback original_callback = std::move(error_callback_);
1913 reset_error_callback();
1914
1915 // Trim extended error codes.
1916 const int error = (extended_error & 0xFF);
1917 // CollectCorruptionInfo() is implemented in terms of sql::Connection,
1918 // TODO(shess): Rewrite IntegrityCheckHelper() in terms of raw SQLite.
1919 std::string result = (error == SQLITE_CORRUPT)
1920 ? CollectCorruptionInfo()
1921 : CollectErrorInfo(extended_error, statement);
1922
1923 // The following queries must be executed after CollectErrorInfo() above, so
1924 // if they result in their own errors, they don't interfere with
1925 // CollectErrorInfo().
1926 const bool has_valid_header =
1927 (ExecuteAndReturnErrorCode("PRAGMA auto_vacuum") == SQLITE_OK);
1928 const bool select_sqlite_master_result =
1929 (ExecuteAndReturnErrorCode("SELECT COUNT(*) FROM sqlite_master") ==
1930 SQLITE_OK);
1931
1932 // Restore the original error callback.
1933 error_callback_ = std::move(original_callback);
1934
1935 base::StringAppendF(&result, "Has valid header: %s\n",
1936 (has_valid_header ? "Yes" : "No"));
1937 base::StringAppendF(&result, "Has valid schema: %s\n",
1938 (select_sqlite_master_result ? "Yes" : "No"));
1939
1940 return result;
1941}
1942
[email protected]80abf152013-05-22 12:42:421943// TODO(shess): Allow specifying maximum results (default 100 lines).
[email protected]579446c2013-12-16 18:36:521944bool Connection::IntegrityCheckHelper(
1945 const char* pragma_sql,
1946 std::vector<std::string>* messages) {
[email protected]80abf152013-05-22 12:42:421947 messages->clear();
1948
[email protected]4658e2a02013-06-06 23:05:001949 // This has the side effect of setting SQLITE_RecoveryMode, which
1950 // allows SQLite to process through certain cases of corruption.
1951 // Failing to set this pragma probably means that the database is
1952 // beyond recovery.
1953 const char kWritableSchema[] = "PRAGMA writable_schema = ON";
1954 if (!Execute(kWritableSchema))
1955 return false;
1956
1957 bool ret = false;
1958 {
[email protected]579446c2013-12-16 18:36:521959 sql::Statement stmt(GetUniqueStatement(pragma_sql));
[email protected]4658e2a02013-06-06 23:05:001960
1961 // The pragma appears to return all results (up to 100 by default)
1962 // as a single string. This doesn't appear to be an API contract,
1963 // it could return separate lines, so loop _and_ split.
1964 while (stmt.Step()) {
1965 std::string result(stmt.ColumnString(0));
brettw83dc1612015-08-12 07:31:181966 *messages = base::SplitString(result, "\n", base::TRIM_WHITESPACE,
1967 base::SPLIT_WANT_ALL);
[email protected]4658e2a02013-06-06 23:05:001968 }
1969 ret = stmt.Succeeded();
[email protected]80abf152013-05-22 12:42:421970 }
[email protected]4658e2a02013-06-06 23:05:001971
1972 // Best effort to put things back as they were before.
1973 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF";
1974 ignore_result(Execute(kNoWritableSchema));
1975
1976 return ret;
[email protected]80abf152013-05-22 12:42:421977}
1978
shess58b8df82015-06-03 00:19:321979base::TimeTicks TimeSource::Now() {
1980 return base::TimeTicks::Now();
1981}
1982
[email protected]e5ffd0e42009-09-11 21:30:561983} // namespace sql