blob: 669344a30b58839600914a4a8bac342a4d47d2dd [file] [log] [blame]
[email protected]64021042012-02-10 20:02:291// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f0a54b22011-07-19 18:40:215#include "sql/connection.h"
[email protected]e5ffd0e42009-09-11 21:30:566
7#include <string.h>
8
shessc9e80ae22015-08-12 21:39:119#include "base/bind.h"
shessc8cd2a162015-10-22 20:30:4610#include "base/debug/dump_without_crashing.h"
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
thestig22dfc4012014-09-05 08:29:4412#include "base/files/file_util.h"
shessc8cd2a162015-10-22 20:30:4613#include "base/format_macros.h"
14#include "base/json/json_file_value_serializer.h"
[email protected]a7ec1292013-07-22 22:02:1815#include "base/lazy_instance.h"
[email protected]e5ffd0e42009-09-11 21:30:5616#include "base/logging.h"
shessc9e80ae22015-08-12 21:39:1117#include "base/message_loop/message_loop.h"
[email protected]bd2ccdb4a2012-12-07 22:14:5018#include "base/metrics/histogram.h"
[email protected]210ce0af2013-05-15 09:10:3919#include "base/metrics/sparse_histogram.h"
[email protected]80abf152013-05-22 12:42:4220#include "base/strings/string_split.h"
[email protected]a4bbc1f92013-06-11 07:28:1921#include "base/strings/string_util.h"
22#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4523#include "base/strings/utf_string_conversions.h"
[email protected]a7ec1292013-07-22 22:02:1824#include "base/synchronization/lock.h"
ssid9f8022f2015-10-12 17:49:0325#include "base/trace_event/memory_dump_manager.h"
26#include "base/trace_event/process_memory_dump.h"
[email protected]f0a54b22011-07-19 18:40:2127#include "sql/statement.h"
[email protected]e33cba42010-08-18 23:37:0328#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5629
[email protected]2e1cee762013-07-09 14:40:0030#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
31#include "third_party/sqlite/src/ext/icu/sqliteicu.h"
32#endif
33
[email protected]5b96f3772010-09-28 16:30:5734namespace {
35
36// Spin for up to a second waiting for the lock to clear when setting
37// up the database.
38// TODO(shess): Better story on this. http://crbug.com/56559
[email protected]c68ce172011-11-24 22:30:2739const int kBusyTimeoutSeconds = 1;
[email protected]5b96f3772010-09-28 16:30:5740
41class ScopedBusyTimeout {
42 public:
43 explicit ScopedBusyTimeout(sqlite3* db)
44 : db_(db) {
45 }
46 ~ScopedBusyTimeout() {
47 sqlite3_busy_timeout(db_, 0);
48 }
49
50 int SetTimeout(base::TimeDelta timeout) {
51 DCHECK_LT(timeout.InMilliseconds(), INT_MAX);
52 return sqlite3_busy_timeout(db_,
53 static_cast<int>(timeout.InMilliseconds()));
54 }
55
56 private:
57 sqlite3* db_;
58};
59
[email protected]6d42f152012-11-10 00:38:2460// Helper to "safely" enable writable_schema. No error checking
61// because it is reasonable to just forge ahead in case of an error.
62// If turning it on fails, then most likely nothing will work, whereas
63// if turning it off fails, it only matters if some code attempts to
64// continue working with the database and tries to modify the
65// sqlite_master table (none of our code does this).
66class ScopedWritableSchema {
67 public:
68 explicit ScopedWritableSchema(sqlite3* db)
69 : db_(db) {
70 sqlite3_exec(db_, "PRAGMA writable_schema=1", NULL, NULL, NULL);
71 }
72 ~ScopedWritableSchema() {
73 sqlite3_exec(db_, "PRAGMA writable_schema=0", NULL, NULL, NULL);
74 }
75
76 private:
77 sqlite3* db_;
78};
79
[email protected]7bae5742013-07-10 20:46:1680// Helper to wrap the sqlite3_backup_*() step of Raze(). Return
81// SQLite error code from running the backup step.
82int BackupDatabase(sqlite3* src, sqlite3* dst, const char* db_name) {
83 DCHECK_NE(src, dst);
84 sqlite3_backup* backup = sqlite3_backup_init(dst, db_name, src, db_name);
85 if (!backup) {
86 // Since this call only sets things up, this indicates a gross
87 // error in SQLite.
88 DLOG(FATAL) << "Unable to start sqlite3_backup(): " << sqlite3_errmsg(dst);
89 return sqlite3_errcode(dst);
90 }
91
92 // -1 backs up the entire database.
93 int rc = sqlite3_backup_step(backup, -1);
94 int pages = sqlite3_backup_pagecount(backup);
95 sqlite3_backup_finish(backup);
96
97 // If successful, exactly one page should have been backed up. If
98 // this breaks, check this function to make sure assumptions aren't
99 // being broken.
100 if (rc == SQLITE_DONE)
101 DCHECK_EQ(pages, 1);
102
103 return rc;
104}
105
[email protected]8d409412013-07-19 18:25:30106// Be very strict on attachment point. SQLite can handle a much wider
107// character set with appropriate quoting, but Chromium code should
108// just use clean names to start with.
109bool ValidAttachmentPoint(const char* attachment_point) {
110 for (size_t i = 0; attachment_point[i]; ++i) {
111 if (!((attachment_point[i] >= '0' && attachment_point[i] <= '9') ||
112 (attachment_point[i] >= 'a' && attachment_point[i] <= 'z') ||
113 (attachment_point[i] >= 'A' && attachment_point[i] <= 'Z') ||
114 attachment_point[i] == '_')) {
115 return false;
116 }
117 }
118 return true;
119}
120
shessc9e80ae22015-08-12 21:39:11121void RecordSqliteMemory10Min() {
122 const int64 used = sqlite3_memory_used();
123 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.TenMinutes", used / 1024);
124}
125
126void RecordSqliteMemoryHour() {
127 const int64 used = sqlite3_memory_used();
128 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneHour", used / 1024);
129}
130
131void RecordSqliteMemoryDay() {
132 const int64 used = sqlite3_memory_used();
133 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneDay", used / 1024);
134}
135
shess2d48e942015-08-25 17:39:51136void RecordSqliteMemoryWeek() {
137 const int64 used = sqlite3_memory_used();
138 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneWeek", used / 1024);
139}
140
[email protected]a7ec1292013-07-22 22:02:18141// SQLite automatically calls sqlite3_initialize() lazily, but
142// sqlite3_initialize() uses double-checked locking and thus can have
143// data races.
144//
145// TODO(shess): Another alternative would be to have
146// sqlite3_initialize() called as part of process bring-up. If this
147// is changed, remove the dynamic_annotations dependency in sql.gyp.
148base::LazyInstance<base::Lock>::Leaky
149 g_sqlite_init_lock = LAZY_INSTANCE_INITIALIZER;
150void InitializeSqlite() {
151 base::AutoLock lock(g_sqlite_init_lock.Get());
shessc9e80ae22015-08-12 21:39:11152 static bool first_call = true;
153 if (first_call) {
154 sqlite3_initialize();
155
156 // Schedule callback to record memory footprint histograms at 10m, 1h, and
157 // 1d. There may not be a message loop in tests.
158 if (base::MessageLoop::current()) {
159 base::MessageLoop::current()->PostDelayedTask(
160 FROM_HERE, base::Bind(&RecordSqliteMemory10Min),
161 base::TimeDelta::FromMinutes(10));
162 base::MessageLoop::current()->PostDelayedTask(
163 FROM_HERE, base::Bind(&RecordSqliteMemoryHour),
164 base::TimeDelta::FromHours(1));
165 base::MessageLoop::current()->PostDelayedTask(
166 FROM_HERE, base::Bind(&RecordSqliteMemoryDay),
167 base::TimeDelta::FromDays(1));
shess2d48e942015-08-25 17:39:51168 base::MessageLoop::current()->PostDelayedTask(
169 FROM_HERE, base::Bind(&RecordSqliteMemoryWeek),
170 base::TimeDelta::FromDays(7));
shessc9e80ae22015-08-12 21:39:11171 }
172
173 first_call = false;
174 }
[email protected]a7ec1292013-07-22 22:02:18175}
176
[email protected]8ada10f2013-12-21 00:42:34177// Helper to get the sqlite3_file* associated with the "main" database.
178int GetSqlite3File(sqlite3* db, sqlite3_file** file) {
179 *file = NULL;
180 int rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_FILE_POINTER, file);
181 if (rc != SQLITE_OK)
182 return rc;
183
184 // TODO(shess): NULL in file->pMethods has been observed on android_dbg
185 // content_unittests, even though it should not be possible.
186 // http://crbug.com/329982
187 if (!*file || !(*file)->pMethods)
188 return SQLITE_ERROR;
189
190 return rc;
191}
192
shess58b8df82015-06-03 00:19:32193// This should match UMA_HISTOGRAM_MEDIUM_TIMES().
194base::HistogramBase* GetMediumTimeHistogram(const std::string& name) {
195 return base::Histogram::FactoryTimeGet(
196 name,
197 base::TimeDelta::FromMilliseconds(10),
198 base::TimeDelta::FromMinutes(3),
199 50,
200 base::HistogramBase::kUmaTargetedHistogramFlag);
201}
202
erg102ceb412015-06-20 01:38:13203std::string AsUTF8ForSQL(const base::FilePath& path) {
204#if defined(OS_WIN)
205 return base::WideToUTF8(path.value());
206#elif defined(OS_POSIX)
207 return path.value();
208#endif
209}
210
[email protected]5b96f3772010-09-28 16:30:57211} // namespace
212
[email protected]e5ffd0e42009-09-11 21:30:56213namespace sql {
214
[email protected]4350e322013-06-18 22:18:10215// static
216Connection::ErrorIgnorerCallback* Connection::current_ignorer_cb_ = NULL;
217
218// static
[email protected]74cdede2013-09-25 05:39:57219bool Connection::ShouldIgnoreSqliteError(int error) {
[email protected]4350e322013-06-18 22:18:10220 if (!current_ignorer_cb_)
221 return false;
222 return current_ignorer_cb_->Run(error);
223}
224
ssid9f8022f2015-10-12 17:49:03225bool Connection::OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
226 base::trace_event::ProcessMemoryDump* pmd) {
227 if (args.level_of_detail ==
228 base::trace_event::MemoryDumpLevelOfDetail::LIGHT ||
229 !db_) {
230 return true;
231 }
232
233 // The high water mark is not tracked for the following usages.
234 int cache_size, dummy_int;
235 sqlite3_db_status(db_, SQLITE_DBSTATUS_CACHE_USED, &cache_size, &dummy_int,
236 0 /* resetFlag */);
237 int schema_size;
238 sqlite3_db_status(db_, SQLITE_DBSTATUS_SCHEMA_USED, &schema_size, &dummy_int,
239 0 /* resetFlag */);
240 int statement_size;
241 sqlite3_db_status(db_, SQLITE_DBSTATUS_STMT_USED, &statement_size, &dummy_int,
242 0 /* resetFlag */);
243
244 std::string name = base::StringPrintf(
245 "sqlite/%s_connection/%p",
246 histogram_tag_.empty() ? "Unknown" : histogram_tag_.c_str(), this);
247 base::trace_event::MemoryAllocatorDump* dump = pmd->CreateAllocatorDump(name);
248 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
249 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
250 cache_size + schema_size + statement_size);
251 dump->AddScalar("cache_size",
252 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
253 cache_size);
254 dump->AddScalar("schema_size",
255 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
256 schema_size);
257 dump->AddScalar("statement_size",
258 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
259 statement_size);
260 return true;
261}
262
shessc8cd2a162015-10-22 20:30:46263void Connection::ReportDiagnosticInfo(int extended_error, Statement* stmt) {
264 AssertIOAllowed();
265
266 std::string debug_info;
267 const int error = (extended_error & 0xFF);
268 if (error == SQLITE_CORRUPT) {
269 debug_info = CollectCorruptionInfo();
270 } else {
271 debug_info = CollectErrorInfo(extended_error, stmt);
272 }
273
274 if (!debug_info.empty() && RegisterIntentToUpload()) {
275 char debug_buf[2000];
276 base::strlcpy(debug_buf, debug_info.c_str(), arraysize(debug_buf));
277 base::debug::Alias(&debug_buf);
278
279 base::debug::DumpWithoutCrashing();
280 }
281}
282
[email protected]4350e322013-06-18 22:18:10283// static
284void Connection::SetErrorIgnorer(Connection::ErrorIgnorerCallback* cb) {
285 CHECK(current_ignorer_cb_ == NULL);
286 current_ignorer_cb_ = cb;
287}
288
289// static
290void Connection::ResetErrorIgnorer() {
291 CHECK(current_ignorer_cb_);
292 current_ignorer_cb_ = NULL;
293}
294
[email protected]e5ffd0e42009-09-11 21:30:56295bool StatementID::operator<(const StatementID& other) const {
296 if (number_ != other.number_)
297 return number_ < other.number_;
298 return strcmp(str_, other.str_) < 0;
299}
300
[email protected]e5ffd0e42009-09-11 21:30:56301Connection::StatementRef::StatementRef(Connection* connection,
[email protected]41a97c812013-02-07 02:35:38302 sqlite3_stmt* stmt,
303 bool was_valid)
[email protected]e5ffd0e42009-09-11 21:30:56304 : connection_(connection),
[email protected]41a97c812013-02-07 02:35:38305 stmt_(stmt),
306 was_valid_(was_valid) {
307 if (connection)
308 connection_->StatementRefCreated(this);
[email protected]e5ffd0e42009-09-11 21:30:56309}
310
311Connection::StatementRef::~StatementRef() {
312 if (connection_)
313 connection_->StatementRefDeleted(this);
[email protected]41a97c812013-02-07 02:35:38314 Close(false);
[email protected]e5ffd0e42009-09-11 21:30:56315}
316
[email protected]41a97c812013-02-07 02:35:38317void Connection::StatementRef::Close(bool forced) {
[email protected]e5ffd0e42009-09-11 21:30:56318 if (stmt_) {
[email protected]35f7e5392012-07-27 19:54:50319 // Call to AssertIOAllowed() cannot go at the beginning of the function
320 // because Close() is called unconditionally from destructor to clean
321 // connection_. And if this is inactive statement this won't cause any
322 // disk access and destructor most probably will be called on thread
323 // not allowing disk access.
324 // TODO([email protected]): This should move to the beginning
325 // of the function. http://crbug.com/136655.
326 AssertIOAllowed();
[email protected]e5ffd0e42009-09-11 21:30:56327 sqlite3_finalize(stmt_);
328 stmt_ = NULL;
329 }
330 connection_ = NULL; // The connection may be getting deleted.
[email protected]41a97c812013-02-07 02:35:38331
332 // Forced close is expected to happen from a statement error
333 // handler. In that case maintain the sense of |was_valid_| which
334 // previously held for this ref.
335 was_valid_ = was_valid_ && forced;
[email protected]e5ffd0e42009-09-11 21:30:56336}
337
338Connection::Connection()
339 : db_(NULL),
340 page_size_(0),
341 cache_size_(0),
342 exclusive_locking_(false),
[email protected]81a2a602013-07-17 19:10:36343 restrict_to_user_(false),
[email protected]e5ffd0e42009-09-11 21:30:56344 transaction_nesting_(0),
[email protected]35f7e5392012-07-27 19:54:50345 needs_rollback_(false),
[email protected]49dc4f22012-10-17 17:41:16346 in_memory_(false),
shess58b8df82015-06-03 00:19:32347 poisoned_(false),
shess7dbd4dee2015-10-06 17:39:16348 mmap_disabled_(false),
349 mmap_enabled_(false),
350 total_changes_at_last_release_(0),
shess58b8df82015-06-03 00:19:32351 stats_histogram_(NULL),
352 commit_time_histogram_(NULL),
353 autocommit_time_histogram_(NULL),
354 update_time_histogram_(NULL),
355 query_time_histogram_(NULL),
356 clock_(new TimeSource()) {
ssid9f8022f2015-10-12 17:49:03357 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
358 this);
[email protected]526b4662013-06-14 04:09:12359}
[email protected]e5ffd0e42009-09-11 21:30:56360
361Connection::~Connection() {
ssid9f8022f2015-10-12 17:49:03362 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
363 this);
[email protected]e5ffd0e42009-09-11 21:30:56364 Close();
365}
366
shess58b8df82015-06-03 00:19:32367void Connection::RecordEvent(Events event, size_t count) {
368 for (size_t i = 0; i < count; ++i) {
369 UMA_HISTOGRAM_ENUMERATION("Sqlite.Stats", event, EVENT_MAX_VALUE);
370 }
371
372 if (stats_histogram_) {
373 for (size_t i = 0; i < count; ++i) {
374 stats_histogram_->Add(event);
375 }
376 }
377}
378
379void Connection::RecordCommitTime(const base::TimeDelta& delta) {
380 RecordUpdateTime(delta);
381 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.CommitTime", delta);
382 if (commit_time_histogram_)
383 commit_time_histogram_->AddTime(delta);
384}
385
386void Connection::RecordAutoCommitTime(const base::TimeDelta& delta) {
387 RecordUpdateTime(delta);
388 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.AutoCommitTime", delta);
389 if (autocommit_time_histogram_)
390 autocommit_time_histogram_->AddTime(delta);
391}
392
393void Connection::RecordUpdateTime(const base::TimeDelta& delta) {
394 RecordQueryTime(delta);
395 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.UpdateTime", delta);
396 if (update_time_histogram_)
397 update_time_histogram_->AddTime(delta);
398}
399
400void Connection::RecordQueryTime(const base::TimeDelta& delta) {
401 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.QueryTime", delta);
402 if (query_time_histogram_)
403 query_time_histogram_->AddTime(delta);
404}
405
406void Connection::RecordTimeAndChanges(
407 const base::TimeDelta& delta, bool read_only) {
408 if (read_only) {
409 RecordQueryTime(delta);
410 } else {
411 const int changes = sqlite3_changes(db_);
412 if (sqlite3_get_autocommit(db_)) {
413 RecordAutoCommitTime(delta);
414 RecordEvent(EVENT_CHANGES_AUTOCOMMIT, changes);
415 } else {
416 RecordUpdateTime(delta);
417 RecordEvent(EVENT_CHANGES, changes);
418 }
419 }
420}
421
[email protected]a3ef4832013-02-02 05:12:33422bool Connection::Open(const base::FilePath& path) {
[email protected]348ac8f52013-05-21 03:27:02423 if (!histogram_tag_.empty()) {
tfarina720d4f32015-05-11 22:31:26424 int64_t size_64 = 0;
[email protected]56285702013-12-04 18:22:49425 if (base::GetFileSize(path, &size_64)) {
[email protected]348ac8f52013-05-21 03:27:02426 size_t sample = static_cast<size_t>(size_64 / 1024);
427 std::string full_histogram_name = "Sqlite.SizeKB." + histogram_tag_;
428 base::HistogramBase* histogram =
429 base::Histogram::FactoryGet(
430 full_histogram_name, 1, 1000000, 50,
431 base::HistogramBase::kUmaTargetedHistogramFlag);
432 if (histogram)
433 histogram->Add(sample);
434 }
435 }
436
erg102ceb412015-06-20 01:38:13437 return OpenInternal(AsUTF8ForSQL(path), RETRY_ON_POISON);
[email protected]765b44502009-10-02 05:01:42438}
[email protected]e5ffd0e42009-09-11 21:30:56439
[email protected]765b44502009-10-02 05:01:42440bool Connection::OpenInMemory() {
[email protected]35f7e5392012-07-27 19:54:50441 in_memory_ = true;
[email protected]fed734a2013-07-17 04:45:13442 return OpenInternal(":memory:", NO_RETRY);
[email protected]e5ffd0e42009-09-11 21:30:56443}
444
[email protected]8d409412013-07-19 18:25:30445bool Connection::OpenTemporary() {
446 return OpenInternal("", NO_RETRY);
447}
448
[email protected]41a97c812013-02-07 02:35:38449void Connection::CloseInternal(bool forced) {
[email protected]4e179ba62012-03-17 16:06:47450 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
451 // will delete the -journal file. For ChromiumOS or other more
452 // embedded systems, this is probably not appropriate, whereas on
453 // desktop it might make some sense.
454
[email protected]4b350052012-02-24 20:40:48455 // sqlite3_close() needs all prepared statements to be finalized.
[email protected]4b350052012-02-24 20:40:48456
[email protected]41a97c812013-02-07 02:35:38457 // Release cached statements.
458 statement_cache_.clear();
459
460 // With cached statements released, in-use statements will remain.
461 // Closing the database while statements are in use is an API
462 // violation, except for forced close (which happens from within a
463 // statement's error handler).
464 DCHECK(forced || open_statements_.empty());
465
466 // Deactivate any outstanding statements so sqlite3_close() works.
467 for (StatementRefSet::iterator i = open_statements_.begin();
468 i != open_statements_.end(); ++i)
469 (*i)->Close(forced);
470 open_statements_.clear();
[email protected]4b350052012-02-24 20:40:48471
[email protected]e5ffd0e42009-09-11 21:30:56472 if (db_) {
[email protected]35f7e5392012-07-27 19:54:50473 // Call to AssertIOAllowed() cannot go at the beginning of the function
474 // because Close() must be called from destructor to clean
475 // statement_cache_, it won't cause any disk access and it most probably
476 // will happen on thread not allowing disk access.
477 // TODO([email protected]): This should move to the beginning
478 // of the function. http://crbug.com/136655.
479 AssertIOAllowed();
[email protected]73fb8d52013-07-24 05:04:28480
481 int rc = sqlite3_close(db_);
482 if (rc != SQLITE_OK) {
483 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.CloseFailure", rc);
484 DLOG(FATAL) << "sqlite3_close failed: " << GetErrorMessage();
485 }
[email protected]e5ffd0e42009-09-11 21:30:56486 }
[email protected]fed734a2013-07-17 04:45:13487 db_ = NULL;
[email protected]e5ffd0e42009-09-11 21:30:56488}
489
[email protected]41a97c812013-02-07 02:35:38490void Connection::Close() {
491 // If the database was already closed by RazeAndClose(), then no
492 // need to close again. Clear the |poisoned_| bit so that incorrect
493 // API calls are caught.
494 if (poisoned_) {
495 poisoned_ = false;
496 return;
497 }
498
499 CloseInternal(false);
500}
501
[email protected]e5ffd0e42009-09-11 21:30:56502void Connection::Preload() {
[email protected]35f7e5392012-07-27 19:54:50503 AssertIOAllowed();
504
[email protected]e5ffd0e42009-09-11 21:30:56505 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38506 DLOG_IF(FATAL, !poisoned_) << "Cannot preload null db";
[email protected]e5ffd0e42009-09-11 21:30:56507 return;
508 }
509
[email protected]8ada10f2013-12-21 00:42:34510 // Use local settings if provided, otherwise use documented defaults. The
511 // actual results could be fetching via PRAGMA calls.
512 const int page_size = page_size_ ? page_size_ : 1024;
513 sqlite3_int64 preload_size = page_size * (cache_size_ ? cache_size_ : 2000);
514 if (preload_size < 1)
[email protected]e5ffd0e42009-09-11 21:30:56515 return;
516
[email protected]8ada10f2013-12-21 00:42:34517 sqlite3_file* file = NULL;
518 int rc = GetSqlite3File(db_, &file);
519 if (rc != SQLITE_OK)
520 return;
521
522 sqlite3_int64 file_size = 0;
523 rc = file->pMethods->xFileSize(file, &file_size);
524 if (rc != SQLITE_OK)
525 return;
526
527 // Don't preload more than the file contains.
528 if (preload_size > file_size)
529 preload_size = file_size;
530
531 scoped_ptr<char[]> buf(new char[page_size]);
shessde60c5f12015-04-21 17:34:46532 for (sqlite3_int64 pos = 0; pos < preload_size; pos += page_size) {
[email protected]8ada10f2013-12-21 00:42:34533 rc = file->pMethods->xRead(file, buf.get(), page_size, pos);
534 if (rc != SQLITE_OK)
535 return;
536 }
[email protected]e5ffd0e42009-09-11 21:30:56537}
538
shess7dbd4dee2015-10-06 17:39:16539// SQLite keeps unused pages associated with a connection in a cache. It asks
540// the cache for pages by an id, and if the page is present and the database is
541// unchanged, it considers the content of the page valid and doesn't read it
542// from disk. When memory-mapped I/O is enabled, on read SQLite uses page
543// structures created from the memory map data before consulting the cache. On
544// write SQLite creates a new in-memory page structure, copies the data from the
545// memory map, and later writes it, releasing the updated page back to the
546// cache.
547//
548// This means that in memory-mapped mode, the contents of the cached pages are
549// not re-used for reads, but they are re-used for writes if the re-written page
550// is still in the cache. The implementation of sqlite3_db_release_memory() as
551// of SQLite 3.8.7.4 frees all pages from pcaches associated with the
552// connection, so it should free these pages.
553//
554// Unfortunately, the zero page is also freed. That page is never accessed
555// using memory-mapped I/O, and the cached copy can be re-used after verifying
556// the file change counter on disk. Also, fresh pages from cache receive some
557// pager-level initialization before they can be used. Since the information
558// involved will immediately be accessed in various ways, it is unclear if the
559// additional overhead is material, or just moving processor cache effects
560// around.
561//
562// TODO(shess): It would be better to release the pages immediately when they
563// are no longer needed. This would basically happen after SQLite commits a
564// transaction. I had implemented a pcache wrapper to do this, but it involved
565// layering violations, and it had to be setup before any other sqlite call,
566// which was brittle. Also, for large files it would actually make sense to
567// maintain the existing pcache behavior for blocks past the memory-mapped
568// segment. I think drh would accept a reasonable implementation of the overall
569// concept for upstreaming to SQLite core.
570//
571// TODO(shess): Another possibility would be to set the cache size small, which
572// would keep the zero page around, plus some pre-initialized pages, and SQLite
573// can manage things. The downside is that updates larger than the cache would
574// spill to the journal. That could be compensated by setting cache_spill to
575// false. The downside then is that it allows open-ended use of memory for
576// large transactions.
577//
578// TODO(shess): The TrimMemory() trick of bouncing the cache size would also
579// work. There could be two prepared statements, one for cache_size=1 one for
580// cache_size=goal.
581void Connection::ReleaseCacheMemoryIfNeeded(bool implicit_change_performed) {
582 DCHECK(is_open());
583
584 // If memory-mapping is not enabled, the page cache helps performance.
585 if (!mmap_enabled_)
586 return;
587
588 // On caller request, force the change comparison to fail. Done before the
589 // transaction-nesting test so that the signal can carry to transaction
590 // commit.
591 if (implicit_change_performed)
592 --total_changes_at_last_release_;
593
594 // Cached pages may be re-used within the same transaction.
595 if (transaction_nesting())
596 return;
597
598 // If no changes have been made, skip flushing. This allows the first page of
599 // the database to remain in cache across multiple reads.
600 const int total_changes = sqlite3_total_changes(db_);
601 if (total_changes == total_changes_at_last_release_)
602 return;
603
604 total_changes_at_last_release_ = total_changes;
605 sqlite3_db_release_memory(db_);
606}
607
shessc8cd2a162015-10-22 20:30:46608base::FilePath Connection::DbPath() const {
609 if (!is_open())
610 return base::FilePath();
611
612 const char* path = sqlite3_db_filename(db_, "main");
613 const base::StringPiece db_path(path);
614#if defined(OS_WIN)
615 return base::FilePath(base::UTF8ToWide(db_path));
616#elif defined(OS_POSIX)
617 return base::FilePath(db_path);
618#else
619 NOTREACHED();
620 return base::FilePath();
621#endif
622}
623
624// Data is persisted in a file shared between databases in the same directory.
625// The "sqlite-diag" file contains a dictionary with the version number, and an
626// array of histogram tags for databases which have been dumped.
627bool Connection::RegisterIntentToUpload() const {
628 static const char* kVersionKey = "version";
629 static const char* kDiagnosticDumpsKey = "DiagnosticDumps";
630 static int kVersion = 1;
631
632 AssertIOAllowed();
633
634 if (histogram_tag_.empty())
635 return false;
636
637 if (!is_open())
638 return false;
639
640 if (in_memory_)
641 return false;
642
643 const base::FilePath db_path = DbPath();
644 if (db_path.empty())
645 return false;
646
647 // Put the collection of diagnostic data next to the databases. In most
648 // cases, this is the profile directory, but safe-browsing stores a Cookies
649 // file in the directory above the profile directory.
650 base::FilePath breadcrumb_path(
651 db_path.DirName().Append(FILE_PATH_LITERAL("sqlite-diag")));
652
653 // Lock against multiple updates to the diagnostics file. This code should
654 // seldom be called in the first place, and when called it should seldom be
655 // called for multiple databases, and when called for multiple databases there
656 // is _probably_ something systemic wrong with the user's system. So the lock
657 // should never be contended, but when it is the database experience is
658 // already bad.
659 base::AutoLock lock(g_sqlite_init_lock.Get());
660
661 scoped_ptr<base::Value> root;
662 if (!base::PathExists(breadcrumb_path)) {
663 scoped_ptr<base::DictionaryValue> root_dict(new base::DictionaryValue());
664 root_dict->SetInteger(kVersionKey, kVersion);
665
666 scoped_ptr<base::ListValue> dumps(new base::ListValue);
667 dumps->AppendString(histogram_tag_);
668 root_dict->Set(kDiagnosticDumpsKey, dumps.Pass());
669
670 root = root_dict.Pass();
671 } else {
672 // Failure to read a valid dictionary implies that something is going wrong
673 // on the system.
674 JSONFileValueDeserializer deserializer(breadcrumb_path);
675 scoped_ptr<base::Value> read_root(
676 deserializer.Deserialize(nullptr, nullptr));
677 if (!read_root.get())
678 return false;
679 scoped_ptr<base::DictionaryValue> root_dict =
680 base::DictionaryValue::From(read_root.Pass());
681 if (!root_dict)
682 return false;
683
684 // Don't upload if the version is missing or newer.
685 int version = 0;
686 if (!root_dict->GetInteger(kVersionKey, &version) || version > kVersion)
687 return false;
688
689 base::ListValue* dumps = nullptr;
690 if (!root_dict->GetList(kDiagnosticDumpsKey, &dumps))
691 return false;
692
693 const size_t size = dumps->GetSize();
694 for (size_t i = 0; i < size; ++i) {
695 std::string s;
696
697 // Don't upload if the value isn't a string, or indicates a prior upload.
698 if (!dumps->GetString(i, &s) || s == histogram_tag_)
699 return false;
700 }
701
702 // Record intention to proceed with upload.
703 dumps->AppendString(histogram_tag_);
704 root = root_dict.Pass();
705 }
706
707 const base::FilePath breadcrumb_new =
708 breadcrumb_path.AddExtension(FILE_PATH_LITERAL("new"));
709 base::DeleteFile(breadcrumb_new, false);
710
711 // No upload if the breadcrumb file cannot be updated.
712 // TODO(shess): Consider ImportantFileWriter::WriteFileAtomically() to land
713 // the data on disk. For now, losing the data is not a big problem, so the
714 // sync overhead would probably not be worth it.
715 JSONFileValueSerializer serializer(breadcrumb_new);
716 if (!serializer.Serialize(*root))
717 return false;
718 if (!base::PathExists(breadcrumb_new))
719 return false;
720 if (!base::ReplaceFile(breadcrumb_new, breadcrumb_path, nullptr)) {
721 base::DeleteFile(breadcrumb_new, false);
722 return false;
723 }
724
725 return true;
726}
727
728std::string Connection::CollectErrorInfo(int error, Statement* stmt) const {
729 // Buffer for accumulating debugging info about the error. Place
730 // more-relevant information earlier, in case things overflow the
731 // fixed-size reporting buffer.
732 std::string debug_info;
733
734 // The error message from the failed operation.
735 base::StringAppendF(&debug_info, "db error: %d/%s\n",
736 GetErrorCode(), GetErrorMessage());
737
738 // TODO(shess): |error| and |GetErrorCode()| should always be the same, but
739 // reading code does not entirely convince me. Remove if they turn out to be
740 // the same.
741 if (error != GetErrorCode())
742 base::StringAppendF(&debug_info, "reported error: %d\n", error);
743
744 // System error information. Interpretation of Windows errors is different
745 // from posix.
746#if defined(OS_WIN)
747 base::StringAppendF(&debug_info, "LastError: %d\n", GetLastErrno());
748#elif defined(OS_POSIX)
749 base::StringAppendF(&debug_info, "errno: %d\n", GetLastErrno());
750#else
751 NOTREACHED(); // Add appropriate log info.
752#endif
753
754 if (stmt) {
755 base::StringAppendF(&debug_info, "statement: %s\n",
756 stmt->GetSQLStatement());
757 } else {
758 base::StringAppendF(&debug_info, "statement: NULL\n");
759 }
760
761 // SQLITE_ERROR often indicates some sort of mismatch between the statement
762 // and the schema, possibly due to a failed schema migration.
763 if (error == SQLITE_ERROR) {
764 const char* kVersionSql = "SELECT value FROM meta WHERE key = 'version'";
765 sqlite3_stmt* s;
766 int rc = sqlite3_prepare_v2(db_, kVersionSql, -1, &s, nullptr);
767 if (rc == SQLITE_OK) {
768 rc = sqlite3_step(s);
769 if (rc == SQLITE_ROW) {
770 base::StringAppendF(&debug_info, "version: %d\n",
771 sqlite3_column_int(s, 0));
772 } else if (rc == SQLITE_DONE) {
773 debug_info += "version: none\n";
774 } else {
775 base::StringAppendF(&debug_info, "version: error %d\n", rc);
776 }
777 sqlite3_finalize(s);
778 } else {
779 base::StringAppendF(&debug_info, "version: prepare error %d\n", rc);
780 }
781
782 debug_info += "schema:\n";
783
784 // sqlite_master has columns:
785 // type - "index" or "table".
786 // name - name of created element.
787 // tbl_name - name of element, or target table in case of index.
788 // rootpage - root page of the element in database file.
789 // sql - SQL to create the element.
790 // In general, the |sql| column is sufficient to derive the other columns.
791 // |rootpage| is not interesting for debugging, without the contents of the
792 // database. The COALESCE is because certain automatic elements will have a
793 // |name| but no |sql|,
794 const char* kSchemaSql = "SELECT COALESCE(sql, name) FROM sqlite_master";
795 rc = sqlite3_prepare_v2(db_, kSchemaSql, -1, &s, nullptr);
796 if (rc == SQLITE_OK) {
797 while ((rc = sqlite3_step(s)) == SQLITE_ROW) {
798 base::StringAppendF(&debug_info, "%s\n", sqlite3_column_text(s, 0));
799 }
800 if (rc != SQLITE_DONE)
801 base::StringAppendF(&debug_info, "error %d\n", rc);
802 sqlite3_finalize(s);
803 } else {
804 base::StringAppendF(&debug_info, "prepare error %d\n", rc);
805 }
806 }
807
808 return debug_info;
809}
810
811// TODO(shess): Since this is only called in an error situation, it might be
812// prudent to rewrite in terms of SQLite API calls, and mark the function const.
813std::string Connection::CollectCorruptionInfo() {
814 AssertIOAllowed();
815
816 // If the file cannot be accessed it is unlikely that an integrity check will
817 // turn up actionable information.
818 const base::FilePath db_path = DbPath();
819 int64 db_size = -1;
820 if (!base::GetFileSize(db_path, &db_size) || db_size < 0)
821 return std::string();
822
823 // Buffer for accumulating debugging info about the error. Place
824 // more-relevant information earlier, in case things overflow the
825 // fixed-size reporting buffer.
826 std::string debug_info;
827 base::StringAppendF(&debug_info, "SQLITE_CORRUPT, db size %" PRId64 "\n",
828 db_size);
829
830 // Only check files up to 8M to keep things from blocking too long.
831 const int64 kMaxIntegrityCheckSize = 8192 * 1024;
832 if (db_size > kMaxIntegrityCheckSize) {
833 debug_info += "integrity_check skipped due to size\n";
834 } else {
835 std::vector<std::string> messages;
836
837 // TODO(shess): FullIntegrityCheck() splits into a vector while this joins
838 // into a string. Probably should be refactored.
839 const base::TimeTicks before = base::TimeTicks::Now();
840 FullIntegrityCheck(&messages);
841 base::StringAppendF(
842 &debug_info,
843 "integrity_check %" PRId64 " ms, %" PRIuS " records:\n",
844 (base::TimeTicks::Now() - before).InMilliseconds(),
845 messages.size());
846
847 // SQLite returns up to 100 messages by default, trim deeper to
848 // keep close to the 2000-character size limit for dumping.
849 const size_t kMaxMessages = 20;
850 for (size_t i = 0; i < kMaxMessages && i < messages.size(); ++i) {
851 base::StringAppendF(&debug_info, "%s\n", messages[i].c_str());
852 }
853 }
854
855 return debug_info;
856}
857
[email protected]be7995f12013-07-18 18:49:14858void Connection::TrimMemory(bool aggressively) {
859 if (!db_)
860 return;
861
862 // TODO(shess): investigate using sqlite3_db_release_memory() when possible.
863 int original_cache_size;
864 {
865 Statement sql_get_original(GetUniqueStatement("PRAGMA cache_size"));
866 if (!sql_get_original.Step()) {
867 DLOG(WARNING) << "Could not get cache size " << GetErrorMessage();
868 return;
869 }
870 original_cache_size = sql_get_original.ColumnInt(0);
871 }
872 int shrink_cache_size = aggressively ? 1 : (original_cache_size / 2);
873
874 // Force sqlite to try to reduce page cache usage.
875 const std::string sql_shrink =
876 base::StringPrintf("PRAGMA cache_size=%d", shrink_cache_size);
877 if (!Execute(sql_shrink.c_str()))
878 DLOG(WARNING) << "Could not shrink cache size: " << GetErrorMessage();
879
880 // Restore cache size.
881 const std::string sql_restore =
882 base::StringPrintf("PRAGMA cache_size=%d", original_cache_size);
883 if (!Execute(sql_restore.c_str()))
884 DLOG(WARNING) << "Could not restore cache size: " << GetErrorMessage();
885}
886
[email protected]8e0c01282012-04-06 19:36:49887// Create an in-memory database with the existing database's page
888// size, then backup that database over the existing database.
889bool Connection::Raze() {
[email protected]35f7e5392012-07-27 19:54:50890 AssertIOAllowed();
891
[email protected]8e0c01282012-04-06 19:36:49892 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38893 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:49894 return false;
895 }
896
897 if (transaction_nesting_ > 0) {
898 DLOG(FATAL) << "Cannot raze within a transaction";
899 return false;
900 }
901
902 sql::Connection null_db;
903 if (!null_db.OpenInMemory()) {
904 DLOG(FATAL) << "Unable to open in-memory database.";
905 return false;
906 }
907
[email protected]6d42f152012-11-10 00:38:24908 if (page_size_) {
909 // Enforce SQLite restrictions on |page_size_|.
910 DCHECK(!(page_size_ & (page_size_ - 1)))
911 << " page_size_ " << page_size_ << " is not a power of two.";
912 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
913 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:04914 const std::string sql =
915 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]69c58452012-08-06 19:22:42916 if (!null_db.Execute(sql.c_str()))
917 return false;
918 }
919
[email protected]6d42f152012-11-10 00:38:24920#if defined(OS_ANDROID)
921 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
922 // in-memory databases do not respect this define.
923 // TODO(shess): Figure out a way to set this without using platform
924 // specific code. AFAICT from sqlite3.c, the only way to do it
925 // would be to create an actual filesystem database, which is
926 // unfortunate.
927 if (!null_db.Execute("PRAGMA auto_vacuum = 1"))
928 return false;
929#endif
[email protected]8e0c01282012-04-06 19:36:49930
931 // The page size doesn't take effect until a database has pages, and
932 // at this point the null database has none. Changing the schema
933 // version will create the first page. This will not affect the
934 // schema version in the resulting database, as SQLite's backup
935 // implementation propagates the schema version from the original
936 // connection to the new version of the database, incremented by one
937 // so that other readers see the schema change and act accordingly.
938 if (!null_db.Execute("PRAGMA schema_version = 1"))
939 return false;
940
[email protected]6d42f152012-11-10 00:38:24941 // SQLite tracks the expected number of database pages in the first
942 // page, and if it does not match the total retrieved from a
943 // filesystem call, treats the database as corrupt. This situation
944 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
945 // used to hint to SQLite to soldier on in that case, specifically
946 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
947 // sqlite3.c lockBtree().]
948 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
949 // page_size" can be used to query such a database.
950 ScopedWritableSchema writable_schema(db_);
951
[email protected]7bae5742013-07-10 20:46:16952 const char* kMain = "main";
953 int rc = BackupDatabase(null_db.db_, db_, kMain);
954 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase",rc);
[email protected]8e0c01282012-04-06 19:36:49955
956 // The destination database was locked.
957 if (rc == SQLITE_BUSY) {
958 return false;
959 }
960
[email protected]7bae5742013-07-10 20:46:16961 // SQLITE_NOTADB can happen if page 1 of db_ exists, but is not
962 // formatted correctly. SQLITE_IOERR_SHORT_READ can happen if db_
963 // isn't even big enough for one page. Either way, reach in and
964 // truncate it before trying again.
965 // TODO(shess): Maybe it would be worthwhile to just truncate from
966 // the get-go?
967 if (rc == SQLITE_NOTADB || rc == SQLITE_IOERR_SHORT_READ) {
968 sqlite3_file* file = NULL;
[email protected]8ada10f2013-12-21 00:42:34969 rc = GetSqlite3File(db_, &file);
[email protected]7bae5742013-07-10 20:46:16970 if (rc != SQLITE_OK) {
971 DLOG(FATAL) << "Failure getting file handle.";
972 return false;
[email protected]7bae5742013-07-10 20:46:16973 }
974
975 rc = file->pMethods->xTruncate(file, 0);
976 if (rc != SQLITE_OK) {
977 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabaseTruncate",rc);
978 DLOG(FATAL) << "Failed to truncate file.";
979 return false;
980 }
981
982 rc = BackupDatabase(null_db.db_, db_, kMain);
983 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase2",rc);
984
985 if (rc != SQLITE_DONE) {
986 DLOG(FATAL) << "Failed retrying Raze().";
987 }
988 }
989
[email protected]8e0c01282012-04-06 19:36:49990 // The entire database should have been backed up.
991 if (rc != SQLITE_DONE) {
[email protected]7bae5742013-07-10 20:46:16992 // TODO(shess): Figure out which other cases can happen.
[email protected]8e0c01282012-04-06 19:36:49993 DLOG(FATAL) << "Unable to copy entire null database.";
994 return false;
995 }
996
[email protected]8e0c01282012-04-06 19:36:49997 return true;
998}
999
1000bool Connection::RazeWithTimout(base::TimeDelta timeout) {
1001 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381002 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:491003 return false;
1004 }
1005
1006 ScopedBusyTimeout busy_timeout(db_);
1007 busy_timeout.SetTimeout(timeout);
1008 return Raze();
1009}
1010
[email protected]41a97c812013-02-07 02:35:381011bool Connection::RazeAndClose() {
1012 if (!db_) {
1013 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
1014 return false;
1015 }
1016
1017 // Raze() cannot run in a transaction.
[email protected]8d409412013-07-19 18:25:301018 RollbackAllTransactions();
[email protected]41a97c812013-02-07 02:35:381019
1020 bool result = Raze();
1021
1022 CloseInternal(true);
1023
1024 // Mark the database so that future API calls fail appropriately,
1025 // but don't DCHECK (because after calling this function they are
1026 // expected to fail).
1027 poisoned_ = true;
1028
1029 return result;
1030}
1031
[email protected]8d409412013-07-19 18:25:301032void Connection::Poison() {
1033 if (!db_) {
1034 DLOG_IF(FATAL, !poisoned_) << "Cannot poison null db";
1035 return;
1036 }
1037
1038 RollbackAllTransactions();
1039 CloseInternal(true);
1040
1041 // Mark the database so that future API calls fail appropriately,
1042 // but don't DCHECK (because after calling this function they are
1043 // expected to fail).
1044 poisoned_ = true;
1045}
1046
[email protected]8d2e39e2013-06-24 05:55:081047// TODO(shess): To the extent possible, figure out the optimal
1048// ordering for these deletes which will prevent other connections
1049// from seeing odd behavior. For instance, it may be necessary to
1050// manually lock the main database file in a SQLite-compatible fashion
1051// (to prevent other processes from opening it), then delete the
1052// journal files, then delete the main database file. Another option
1053// might be to lock the main database file and poison the header with
1054// junk to prevent other processes from opening it successfully (like
1055// Gears "SQLite poison 3" trick).
1056//
1057// static
1058bool Connection::Delete(const base::FilePath& path) {
1059 base::ThreadRestrictions::AssertIOAllowed();
1060
1061 base::FilePath journal_path(path.value() + FILE_PATH_LITERAL("-journal"));
1062 base::FilePath wal_path(path.value() + FILE_PATH_LITERAL("-wal"));
1063
erg102ceb412015-06-20 01:38:131064 std::string journal_str = AsUTF8ForSQL(journal_path);
1065 std::string wal_str = AsUTF8ForSQL(wal_path);
1066 std::string path_str = AsUTF8ForSQL(path);
[email protected]8d2e39e2013-06-24 05:55:081067
shess702467622015-09-16 19:04:551068 // Make sure sqlite3_initialize() is called before anything else.
1069 InitializeSqlite();
1070
erg102ceb412015-06-20 01:38:131071 sqlite3_vfs* vfs = sqlite3_vfs_find(NULL);
1072 CHECK(vfs);
1073 CHECK(vfs->xDelete);
1074 CHECK(vfs->xAccess);
1075
1076 // We only work with unix, win32 and mojo filesystems. If you're trying to
1077 // use this code with any other VFS, you're not in a good place.
1078 CHECK(strncmp(vfs->zName, "unix", 4) == 0 ||
1079 strncmp(vfs->zName, "win32", 5) == 0 ||
1080 strcmp(vfs->zName, "mojo") == 0);
1081
1082 vfs->xDelete(vfs, journal_str.c_str(), 0);
1083 vfs->xDelete(vfs, wal_str.c_str(), 0);
1084 vfs->xDelete(vfs, path_str.c_str(), 0);
1085
1086 int journal_exists = 0;
1087 vfs->xAccess(vfs, journal_str.c_str(), SQLITE_ACCESS_EXISTS,
1088 &journal_exists);
1089
1090 int wal_exists = 0;
1091 vfs->xAccess(vfs, wal_str.c_str(), SQLITE_ACCESS_EXISTS,
1092 &wal_exists);
1093
1094 int path_exists = 0;
1095 vfs->xAccess(vfs, path_str.c_str(), SQLITE_ACCESS_EXISTS,
1096 &path_exists);
1097
1098 return !journal_exists && !wal_exists && !path_exists;
[email protected]8d2e39e2013-06-24 05:55:081099}
1100
[email protected]e5ffd0e42009-09-11 21:30:561101bool Connection::BeginTransaction() {
1102 if (needs_rollback_) {
[email protected]88563f62011-03-13 22:13:331103 DCHECK_GT(transaction_nesting_, 0);
[email protected]e5ffd0e42009-09-11 21:30:561104
1105 // When we're going to rollback, fail on this begin and don't actually
1106 // mark us as entering the nested transaction.
1107 return false;
1108 }
1109
1110 bool success = true;
1111 if (!transaction_nesting_) {
1112 needs_rollback_ = false;
1113
1114 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
shess58b8df82015-06-03 00:19:321115 RecordOneEvent(EVENT_BEGIN);
[email protected]eff1fa522011-12-12 23:50:591116 if (!begin.Run())
[email protected]e5ffd0e42009-09-11 21:30:561117 return false;
1118 }
1119 transaction_nesting_++;
1120 return success;
1121}
1122
1123void Connection::RollbackTransaction() {
1124 if (!transaction_nesting_) {
[email protected]41a97c812013-02-07 02:35:381125 DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561126 return;
1127 }
1128
1129 transaction_nesting_--;
1130
1131 if (transaction_nesting_ > 0) {
1132 // Mark the outermost transaction as needing rollback.
1133 needs_rollback_ = true;
1134 return;
1135 }
1136
1137 DoRollback();
1138}
1139
1140bool Connection::CommitTransaction() {
1141 if (!transaction_nesting_) {
[email protected]41a97c812013-02-07 02:35:381142 DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:561143 return false;
1144 }
1145 transaction_nesting_--;
1146
1147 if (transaction_nesting_ > 0) {
1148 // Mark any nested transactions as failing after we've already got one.
1149 return !needs_rollback_;
1150 }
1151
1152 if (needs_rollback_) {
1153 DoRollback();
1154 return false;
1155 }
1156
1157 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
shess58b8df82015-06-03 00:19:321158
1159 // Collect the commit time manually, sql::Statement would register it as query
1160 // time only.
1161 const base::TimeTicks before = Now();
1162 bool ret = commit.RunWithoutTimers();
1163 const base::TimeDelta delta = Now() - before;
1164
1165 RecordCommitTime(delta);
1166 RecordOneEvent(EVENT_COMMIT);
1167
shess7dbd4dee2015-10-06 17:39:161168 // Release dirty cache pages after the transaction closes.
1169 ReleaseCacheMemoryIfNeeded(false);
1170
shess58b8df82015-06-03 00:19:321171 return ret;
[email protected]e5ffd0e42009-09-11 21:30:561172}
1173
[email protected]8d409412013-07-19 18:25:301174void Connection::RollbackAllTransactions() {
1175 if (transaction_nesting_ > 0) {
1176 transaction_nesting_ = 0;
1177 DoRollback();
1178 }
1179}
1180
1181bool Connection::AttachDatabase(const base::FilePath& other_db_path,
1182 const char* attachment_point) {
1183 DCHECK(ValidAttachmentPoint(attachment_point));
1184
1185 Statement s(GetUniqueStatement("ATTACH DATABASE ? AS ?"));
1186#if OS_WIN
1187 s.BindString16(0, other_db_path.value());
1188#else
1189 s.BindString(0, other_db_path.value());
1190#endif
1191 s.BindString(1, attachment_point);
1192 return s.Run();
1193}
1194
1195bool Connection::DetachDatabase(const char* attachment_point) {
1196 DCHECK(ValidAttachmentPoint(attachment_point));
1197
1198 Statement s(GetUniqueStatement("DETACH DATABASE ?"));
1199 s.BindString(0, attachment_point);
1200 return s.Run();
1201}
1202
shess58b8df82015-06-03 00:19:321203// TODO(shess): Consider changing this to execute exactly one statement. If a
1204// caller wishes to execute multiple statements, that should be explicit, and
1205// perhaps tucked into an explicit transaction with rollback in case of error.
[email protected]eff1fa522011-12-12 23:50:591206int Connection::ExecuteAndReturnErrorCode(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501207 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:381208 if (!db_) {
1209 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
1210 return SQLITE_ERROR;
1211 }
shess58b8df82015-06-03 00:19:321212 DCHECK(sql);
1213
1214 RecordOneEvent(EVENT_EXECUTE);
1215 int rc = SQLITE_OK;
1216 while ((rc == SQLITE_OK) && *sql) {
1217 sqlite3_stmt *stmt = NULL;
1218 const char *leftover_sql;
1219
1220 const base::TimeTicks before = Now();
1221 rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, &leftover_sql);
1222 sql = leftover_sql;
1223
1224 // Stop if an error is encountered.
1225 if (rc != SQLITE_OK)
1226 break;
1227
1228 // This happens if |sql| originally only contained comments or whitespace.
1229 // TODO(shess): Audit to see if this can become a DCHECK(). Having
1230 // extraneous comments and whitespace in the SQL statements increases
1231 // runtime cost and can easily be shifted out to the C++ layer.
1232 if (!stmt)
1233 continue;
1234
1235 // Save for use after statement is finalized.
1236 const bool read_only = !!sqlite3_stmt_readonly(stmt);
1237
1238 RecordOneEvent(Connection::EVENT_STATEMENT_RUN);
1239 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1240 // TODO(shess): Audit to see if this can become a DCHECK. I think PRAGMA
1241 // is the only legitimate case for this.
1242 RecordOneEvent(Connection::EVENT_STATEMENT_ROWS);
1243 }
1244
1245 // sqlite3_finalize() returns SQLITE_OK if the most recent sqlite3_step()
1246 // returned SQLITE_DONE or SQLITE_ROW, otherwise the error code.
1247 rc = sqlite3_finalize(stmt);
1248 if (rc == SQLITE_OK)
1249 RecordOneEvent(Connection::EVENT_STATEMENT_SUCCESS);
1250
1251 // sqlite3_exec() does this, presumably to avoid spinning the parser for
1252 // trailing whitespace.
1253 // TODO(shess): Audit to see if this can become a DCHECK.
brettwb3413062015-06-24 00:39:021254 while (base::IsAsciiWhitespace(*sql)) {
shess58b8df82015-06-03 00:19:321255 sql++;
1256 }
1257
1258 const base::TimeDelta delta = Now() - before;
1259 RecordTimeAndChanges(delta, read_only);
1260 }
shess7dbd4dee2015-10-06 17:39:161261
1262 // Most calls to Execute() modify the database. The main exceptions would be
1263 // calls such as CREATE TABLE IF NOT EXISTS which could modify the database
1264 // but sometimes don't.
1265 ReleaseCacheMemoryIfNeeded(true);
1266
shess58b8df82015-06-03 00:19:321267 return rc;
[email protected]eff1fa522011-12-12 23:50:591268}
1269
1270bool Connection::Execute(const char* sql) {
[email protected]41a97c812013-02-07 02:35:381271 if (!db_) {
1272 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
1273 return false;
1274 }
1275
[email protected]eff1fa522011-12-12 23:50:591276 int error = ExecuteAndReturnErrorCode(sql);
[email protected]473ad792012-11-10 00:55:001277 if (error != SQLITE_OK)
[email protected]2f496b42013-09-26 18:36:581278 error = OnSqliteError(error, NULL, sql);
[email protected]473ad792012-11-10 00:55:001279
[email protected]28fe0ff2012-02-25 00:40:331280 // This needs to be a FATAL log because the error case of arriving here is
1281 // that there's a malformed SQL statement. This can arise in development if
[email protected]4350e322013-06-18 22:18:101282 // a change alters the schema but not all queries adjust. This can happen
1283 // in production if the schema is corrupted.
[email protected]eff1fa522011-12-12 23:50:591284 if (error == SQLITE_ERROR)
[email protected]28fe0ff2012-02-25 00:40:331285 DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage();
[email protected]eff1fa522011-12-12 23:50:591286 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:561287}
1288
[email protected]5b96f3772010-09-28 16:30:571289bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
[email protected]41a97c812013-02-07 02:35:381290 if (!db_) {
1291 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]5b96f3772010-09-28 16:30:571292 return false;
[email protected]41a97c812013-02-07 02:35:381293 }
[email protected]5b96f3772010-09-28 16:30:571294
1295 ScopedBusyTimeout busy_timeout(db_);
1296 busy_timeout.SetTimeout(timeout);
[email protected]eff1fa522011-12-12 23:50:591297 return Execute(sql);
[email protected]5b96f3772010-09-28 16:30:571298}
1299
[email protected]e5ffd0e42009-09-11 21:30:561300bool Connection::HasCachedStatement(const StatementID& id) const {
1301 return statement_cache_.find(id) != statement_cache_.end();
1302}
1303
1304scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement(
1305 const StatementID& id,
1306 const char* sql) {
1307 CachedStatementMap::iterator i = statement_cache_.find(id);
1308 if (i != statement_cache_.end()) {
1309 // Statement is in the cache. It should still be active (we're the only
1310 // one invalidating cached statements, and we'll remove it from the cache
1311 // if we do that. Make sure we reset it before giving out the cached one in
1312 // case it still has some stuff bound.
1313 DCHECK(i->second->is_valid());
1314 sqlite3_reset(i->second->stmt());
1315 return i->second;
1316 }
1317
1318 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
1319 if (statement->is_valid())
1320 statement_cache_[id] = statement; // Only cache valid statements.
1321 return statement;
1322}
1323
1324scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement(
1325 const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501326 AssertIOAllowed();
1327
[email protected]41a97c812013-02-07 02:35:381328 // Return inactive statement.
[email protected]e5ffd0e42009-09-11 21:30:561329 if (!db_)
[email protected]41a97c812013-02-07 02:35:381330 return new StatementRef(NULL, NULL, poisoned_);
[email protected]e5ffd0e42009-09-11 21:30:561331
1332 sqlite3_stmt* stmt = NULL;
[email protected]473ad792012-11-10 00:55:001333 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
1334 if (rc != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:591335 // This is evidence of a syntax error in the incoming SQL.
shess193bfb622015-04-10 22:30:021336 if (!ShouldIgnoreSqliteError(rc))
1337 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]473ad792012-11-10 00:55:001338
1339 // It could also be database corruption.
[email protected]2f496b42013-09-26 18:36:581340 OnSqliteError(rc, NULL, sql);
[email protected]41a97c812013-02-07 02:35:381341 return new StatementRef(NULL, NULL, false);
[email protected]e5ffd0e42009-09-11 21:30:561342 }
[email protected]41a97c812013-02-07 02:35:381343 return new StatementRef(this, stmt, true);
[email protected]e5ffd0e42009-09-11 21:30:561344}
1345
[email protected]2eec0a22012-07-24 01:59:581346scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
1347 const char* sql) const {
[email protected]41a97c812013-02-07 02:35:381348 // Return inactive statement.
[email protected]2eec0a22012-07-24 01:59:581349 if (!db_)
[email protected]41a97c812013-02-07 02:35:381350 return new StatementRef(NULL, NULL, poisoned_);
[email protected]2eec0a22012-07-24 01:59:581351
1352 sqlite3_stmt* stmt = NULL;
1353 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
1354 if (rc != SQLITE_OK) {
1355 // This is evidence of a syntax error in the incoming SQL.
shess193bfb622015-04-10 22:30:021356 if (!ShouldIgnoreSqliteError(rc))
1357 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]41a97c812013-02-07 02:35:381358 return new StatementRef(NULL, NULL, false);
[email protected]2eec0a22012-07-24 01:59:581359 }
[email protected]41a97c812013-02-07 02:35:381360 return new StatementRef(NULL, stmt, true);
[email protected]2eec0a22012-07-24 01:59:581361}
1362
[email protected]92cd00a2013-08-16 11:09:581363std::string Connection::GetSchema() const {
1364 // The ORDER BY should not be necessary, but relying on organic
1365 // order for something like this is questionable.
1366 const char* kSql =
1367 "SELECT type, name, tbl_name, sql "
1368 "FROM sqlite_master ORDER BY 1, 2, 3, 4";
1369 Statement statement(GetUntrackedStatement(kSql));
1370
1371 std::string schema;
1372 while (statement.Step()) {
1373 schema += statement.ColumnString(0);
1374 schema += '|';
1375 schema += statement.ColumnString(1);
1376 schema += '|';
1377 schema += statement.ColumnString(2);
1378 schema += '|';
1379 schema += statement.ColumnString(3);
1380 schema += '\n';
1381 }
1382
1383 return schema;
1384}
1385
[email protected]eff1fa522011-12-12 23:50:591386bool Connection::IsSQLValid(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:501387 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:381388 if (!db_) {
1389 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
1390 return false;
1391 }
1392
[email protected]eff1fa522011-12-12 23:50:591393 sqlite3_stmt* stmt = NULL;
1394 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK)
1395 return false;
1396
1397 sqlite3_finalize(stmt);
1398 return true;
1399}
1400
[email protected]1ed78a32009-09-15 20:24:171401bool Connection::DoesTableExist(const char* table_name) const {
[email protected]e2cadec82011-12-13 02:00:531402 return DoesTableOrIndexExist(table_name, "table");
1403}
1404
1405bool Connection::DoesIndexExist(const char* index_name) const {
1406 return DoesTableOrIndexExist(index_name, "index");
1407}
1408
1409bool Connection::DoesTableOrIndexExist(
1410 const char* name, const char* type) const {
shess92a2ab12015-04-09 01:59:471411 const char* kSql =
1412 "SELECT name FROM sqlite_master WHERE type=? AND name=? COLLATE NOCASE";
[email protected]2eec0a22012-07-24 01:59:581413 Statement statement(GetUntrackedStatement(kSql));
shess92a2ab12015-04-09 01:59:471414
1415 // This can happen if the database is corrupt and the error is being ignored
1416 // for testing purposes.
1417 if (!statement.is_valid())
1418 return false;
1419
[email protected]e2cadec82011-12-13 02:00:531420 statement.BindString(0, type);
1421 statement.BindString(1, name);
[email protected]28fe0ff2012-02-25 00:40:331422
[email protected]e5ffd0e42009-09-11 21:30:561423 return statement.Step(); // Table exists if any row was returned.
1424}
1425
1426bool Connection::DoesColumnExist(const char* table_name,
[email protected]1ed78a32009-09-15 20:24:171427 const char* column_name) const {
[email protected]e5ffd0e42009-09-11 21:30:561428 std::string sql("PRAGMA TABLE_INFO(");
1429 sql.append(table_name);
1430 sql.append(")");
1431
[email protected]2eec0a22012-07-24 01:59:581432 Statement statement(GetUntrackedStatement(sql.c_str()));
shess92a2ab12015-04-09 01:59:471433
1434 // This can happen if the database is corrupt and the error is being ignored
1435 // for testing purposes.
1436 if (!statement.is_valid())
1437 return false;
1438
[email protected]e5ffd0e42009-09-11 21:30:561439 while (statement.Step()) {
brettw8a800902015-07-10 18:28:331440 if (base::EqualsCaseInsensitiveASCII(statement.ColumnString(1),
1441 column_name))
[email protected]e5ffd0e42009-09-11 21:30:561442 return true;
1443 }
1444 return false;
1445}
1446
tfarina720d4f32015-05-11 22:31:261447int64_t Connection::GetLastInsertRowId() const {
[email protected]e5ffd0e42009-09-11 21:30:561448 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381449 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]e5ffd0e42009-09-11 21:30:561450 return 0;
1451 }
1452 return sqlite3_last_insert_rowid(db_);
1453}
1454
[email protected]1ed78a32009-09-15 20:24:171455int Connection::GetLastChangeCount() const {
1456 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381457 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]1ed78a32009-09-15 20:24:171458 return 0;
1459 }
1460 return sqlite3_changes(db_);
1461}
1462
[email protected]e5ffd0e42009-09-11 21:30:561463int Connection::GetErrorCode() const {
1464 if (!db_)
1465 return SQLITE_ERROR;
1466 return sqlite3_errcode(db_);
1467}
1468
[email protected]767718e52010-09-21 23:18:491469int Connection::GetLastErrno() const {
1470 if (!db_)
1471 return -1;
1472
1473 int err = 0;
1474 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err))
1475 return -2;
1476
1477 return err;
1478}
1479
[email protected]e5ffd0e42009-09-11 21:30:561480const char* Connection::GetErrorMessage() const {
1481 if (!db_)
1482 return "sql::Connection has no connection.";
1483 return sqlite3_errmsg(db_);
1484}
1485
[email protected]fed734a2013-07-17 04:45:131486bool Connection::OpenInternal(const std::string& file_name,
1487 Connection::Retry retry_flag) {
[email protected]35f7e5392012-07-27 19:54:501488 AssertIOAllowed();
1489
[email protected]9cfbc922009-11-17 20:13:171490 if (db_) {
[email protected]eff1fa522011-12-12 23:50:591491 DLOG(FATAL) << "sql::Connection is already open.";
[email protected]9cfbc922009-11-17 20:13:171492 return false;
1493 }
1494
[email protected]a7ec1292013-07-22 22:02:181495 // Make sure sqlite3_initialize() is called before anything else.
1496 InitializeSqlite();
1497
shess58b8df82015-06-03 00:19:321498 // Setup the stats histograms immediately rather than allocating lazily.
1499 // Connections which won't exercise all of these probably shouldn't exist.
1500 if (!histogram_tag_.empty()) {
1501 stats_histogram_ =
1502 base::LinearHistogram::FactoryGet(
1503 "Sqlite.Stats." + histogram_tag_,
1504 1, EVENT_MAX_VALUE, EVENT_MAX_VALUE + 1,
1505 base::HistogramBase::kUmaTargetedHistogramFlag);
1506
1507 // The timer setup matches UMA_HISTOGRAM_MEDIUM_TIMES(). 3 minutes is an
1508 // unreasonable time for any single operation, so there is not much value to
1509 // knowing if it was 3 minutes or 5 minutes. In reality at that point
1510 // things are entirely busted.
1511 commit_time_histogram_ =
1512 GetMediumTimeHistogram("Sqlite.CommitTime." + histogram_tag_);
1513
1514 autocommit_time_histogram_ =
1515 GetMediumTimeHistogram("Sqlite.AutoCommitTime." + histogram_tag_);
1516
1517 update_time_histogram_ =
1518 GetMediumTimeHistogram("Sqlite.UpdateTime." + histogram_tag_);
1519
1520 query_time_histogram_ =
1521 GetMediumTimeHistogram("Sqlite.QueryTime." + histogram_tag_);
1522 }
1523
[email protected]41a97c812013-02-07 02:35:381524 // If |poisoned_| is set, it means an error handler called
1525 // RazeAndClose(). Until regular Close() is called, the caller
1526 // should be treating the database as open, but is_open() currently
1527 // only considers the sqlite3 handle's state.
1528 // TODO(shess): Revise is_open() to consider poisoned_, and review
1529 // to see if any non-testing code even depends on it.
1530 DLOG_IF(FATAL, poisoned_) << "sql::Connection is already open.";
[email protected]7bae5742013-07-10 20:46:161531 poisoned_ = false;
[email protected]41a97c812013-02-07 02:35:381532
[email protected]765b44502009-10-02 05:01:421533 int err = sqlite3_open(file_name.c_str(), &db_);
1534 if (err != SQLITE_OK) {
[email protected]73fb8d52013-07-24 05:04:281535 // Extended error codes cannot be enabled until a handle is
1536 // available, fetch manually.
1537 err = sqlite3_extended_errcode(db_);
1538
[email protected]bd2ccdb4a2012-12-07 22:14:501539 // Histogram failures specific to initial open for debugging
1540 // purposes.
[email protected]73fb8d52013-07-24 05:04:281541 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenFailure", err);
[email protected]bd2ccdb4a2012-12-07 22:14:501542
[email protected]2f496b42013-09-26 18:36:581543 OnSqliteError(err, NULL, "-- sqlite3_open()");
[email protected]fed734a2013-07-17 04:45:131544 bool was_poisoned = poisoned_;
[email protected]64021042012-02-10 20:02:291545 Close();
[email protected]fed734a2013-07-17 04:45:131546
1547 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1548 return OpenInternal(file_name, NO_RETRY);
[email protected]765b44502009-10-02 05:01:421549 return false;
1550 }
1551
[email protected]81a2a602013-07-17 19:10:361552 // TODO(shess): OS_WIN support?
1553#if defined(OS_POSIX)
1554 if (restrict_to_user_) {
1555 DCHECK_NE(file_name, std::string(":memory"));
1556 base::FilePath file_path(file_name);
1557 int mode = 0;
1558 // TODO(shess): Arguably, failure to retrieve and change
1559 // permissions should be fatal if the file exists.
[email protected]b264eab2013-11-27 23:22:081560 if (base::GetPosixFilePermissions(file_path, &mode)) {
1561 mode &= base::FILE_PERMISSION_USER_MASK;
1562 base::SetPosixFilePermissions(file_path, mode);
[email protected]81a2a602013-07-17 19:10:361563
1564 // SQLite sets the permissions on these files from the main
1565 // database on create. Set them here in case they already exist
1566 // at this point. Failure to set these permissions should not
1567 // be fatal unless the file doesn't exist.
1568 base::FilePath journal_path(file_name + FILE_PATH_LITERAL("-journal"));
1569 base::FilePath wal_path(file_name + FILE_PATH_LITERAL("-wal"));
[email protected]b264eab2013-11-27 23:22:081570 base::SetPosixFilePermissions(journal_path, mode);
1571 base::SetPosixFilePermissions(wal_path, mode);
[email protected]81a2a602013-07-17 19:10:361572 }
1573 }
1574#endif // defined(OS_POSIX)
1575
[email protected]affa2da2013-06-06 22:20:341576 // SQLite uses a lookaside buffer to improve performance of small mallocs.
1577 // Chromium already depends on small mallocs being efficient, so we disable
1578 // this to avoid the extra memory overhead.
1579 // This must be called immediatly after opening the database before any SQL
1580 // statements are run.
1581 sqlite3_db_config(db_, SQLITE_DBCONFIG_LOOKASIDE, NULL, 0, 0);
1582
[email protected]73fb8d52013-07-24 05:04:281583 // Enable extended result codes to provide more color on I/O errors.
1584 // Not having extended result codes is not a fatal problem, as
1585 // Chromium code does not attempt to handle I/O errors anyhow. The
1586 // current implementation always returns SQLITE_OK, the DCHECK is to
1587 // quickly notify someone if SQLite changes.
1588 err = sqlite3_extended_result_codes(db_, 1);
1589 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
1590
[email protected]bd2ccdb4a2012-12-07 22:14:501591 // sqlite3_open() does not actually read the database file (unless a
1592 // hot journal is found). Successfully executing this pragma on an
1593 // existing database requires a valid header on page 1.
1594 // TODO(shess): For now, just probing to see what the lay of the
1595 // land is. If it's mostly SQLITE_NOTADB, then the database should
1596 // be razed.
1597 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
1598 if (err != SQLITE_OK)
[email protected]73fb8d52013-07-24 05:04:281599 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenProbeFailure", err);
[email protected]658f8332010-09-18 04:40:431600
[email protected]2e1cee762013-07-09 14:40:001601#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
1602 // The version of SQLite shipped with iOS doesn't enable ICU, which includes
1603 // REGEXP support. Add it in dynamically.
1604 err = sqlite3IcuInit(db_);
1605 DCHECK_EQ(err, SQLITE_OK) << "Could not enable ICU support";
1606#endif // OS_IOS && USE_SYSTEM_SQLITE
1607
[email protected]5b96f3772010-09-28 16:30:571608 // If indicated, lock up the database before doing anything else, so
1609 // that the following code doesn't have to deal with locking.
1610 // TODO(shess): This code is brittle. Find the cases where code
1611 // doesn't request |exclusive_locking_| and audit that it does the
1612 // right thing with SQLITE_BUSY, and that it doesn't make
1613 // assumptions about who might change things in the database.
1614 // http://crbug.com/56559
1615 if (exclusive_locking_) {
[email protected]4350e322013-06-18 22:18:101616 // TODO(shess): This should probably be a failure. Code which
1617 // requests exclusive locking but doesn't get it is almost certain
1618 // to be ill-tested.
1619 ignore_result(Execute("PRAGMA locking_mode=EXCLUSIVE"));
[email protected]5b96f3772010-09-28 16:30:571620 }
1621
[email protected]4e179ba62012-03-17 16:06:471622 // http://www.sqlite.org/pragma.html#pragma_journal_mode
1623 // DELETE (default) - delete -journal file to commit.
1624 // TRUNCATE - truncate -journal file to commit.
1625 // PERSIST - zero out header of -journal file to commit.
shess2c21ecf2015-06-02 01:31:091626 // TRUNCATE should be faster than DELETE because it won't need directory
1627 // changes for each transaction. PERSIST may break the spirit of using
1628 // secure_delete.
1629 ignore_result(Execute("PRAGMA journal_mode = TRUNCATE"));
[email protected]4e179ba62012-03-17 16:06:471630
shess7dbd4dee2015-10-06 17:39:161631 // Enable memory-mapped access. This value will be capped by
1632 // SQLITE_MAX_MMAP_SIZE, which could be different between 32-bit and 64-bit
1633 // platforms.
1634 mmap_enabled_ = false;
1635 if (!mmap_disabled_)
1636 ignore_result(Execute("PRAGMA mmap_size = 268435456")); // 256MB.
1637 {
1638 Statement s(GetUniqueStatement("PRAGMA mmap_size"));
1639 if (s.Step() && s.ColumnInt64(0) > 0)
1640 mmap_enabled_ = true;
1641 }
1642
[email protected]c68ce172011-11-24 22:30:271643 const base::TimeDelta kBusyTimeout =
1644 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
1645
[email protected]765b44502009-10-02 05:01:421646 if (page_size_ != 0) {
[email protected]5b96f3772010-09-28 16:30:571647 // Enforce SQLite restrictions on |page_size_|.
1648 DCHECK(!(page_size_ & (page_size_ - 1)))
1649 << " page_size_ " << page_size_ << " is not a power of two.";
[email protected]6d42f152012-11-10 00:38:241650 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
[email protected]5b96f3772010-09-28 16:30:571651 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:041652 const std::string sql =
1653 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]4350e322013-06-18 22:18:101654 ignore_result(ExecuteWithTimeout(sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421655 }
1656
1657 if (cache_size_ != 0) {
[email protected]7d3cbc92013-03-18 22:33:041658 const std::string sql =
1659 base::StringPrintf("PRAGMA cache_size=%d", cache_size_);
[email protected]4350e322013-06-18 22:18:101660 ignore_result(ExecuteWithTimeout(sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421661 }
1662
[email protected]6e0b1442011-08-09 23:23:581663 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) {
[email protected]fed734a2013-07-17 04:45:131664 bool was_poisoned = poisoned_;
[email protected]6e0b1442011-08-09 23:23:581665 Close();
[email protected]fed734a2013-07-17 04:45:131666 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1667 return OpenInternal(file_name, NO_RETRY);
[email protected]6e0b1442011-08-09 23:23:581668 return false;
1669 }
1670
[email protected]765b44502009-10-02 05:01:421671 return true;
1672}
1673
[email protected]e5ffd0e42009-09-11 21:30:561674void Connection::DoRollback() {
1675 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
shess58b8df82015-06-03 00:19:321676
1677 // Collect the rollback time manually, sql::Statement would register it as
1678 // query time only.
1679 const base::TimeTicks before = Now();
1680 rollback.RunWithoutTimers();
1681 const base::TimeDelta delta = Now() - before;
1682
1683 RecordUpdateTime(delta);
1684 RecordOneEvent(EVENT_ROLLBACK);
1685
shess7dbd4dee2015-10-06 17:39:161686 // The cache may have been accumulating dirty pages for commit. Note that in
1687 // some cases sql::Transaction can fire rollback after a database is closed.
1688 if (is_open())
1689 ReleaseCacheMemoryIfNeeded(false);
1690
[email protected]44ad7d902012-03-23 00:09:051691 needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:561692}
1693
1694void Connection::StatementRefCreated(StatementRef* ref) {
1695 DCHECK(open_statements_.find(ref) == open_statements_.end());
1696 open_statements_.insert(ref);
1697}
1698
1699void Connection::StatementRefDeleted(StatementRef* ref) {
1700 StatementRefSet::iterator i = open_statements_.find(ref);
1701 if (i == open_statements_.end())
[email protected]eff1fa522011-12-12 23:50:591702 DLOG(FATAL) << "Could not find statement";
[email protected]e5ffd0e42009-09-11 21:30:561703 else
1704 open_statements_.erase(i);
1705}
1706
shess58b8df82015-06-03 00:19:321707void Connection::set_histogram_tag(const std::string& tag) {
1708 DCHECK(!is_open());
1709 histogram_tag_ = tag;
1710}
1711
[email protected]210ce0af2013-05-15 09:10:391712void Connection::AddTaggedHistogram(const std::string& name,
1713 size_t sample) const {
1714 if (histogram_tag_.empty())
1715 return;
1716
1717 // TODO(shess): The histogram macros create a bit of static storage
1718 // for caching the histogram object. This code shouldn't execute
1719 // often enough for such caching to be crucial. If it becomes an
1720 // issue, the object could be cached alongside histogram_prefix_.
1721 std::string full_histogram_name = name + "." + histogram_tag_;
1722 base::HistogramBase* histogram =
1723 base::SparseHistogram::FactoryGet(
1724 full_histogram_name,
1725 base::HistogramBase::kUmaTargetedHistogramFlag);
1726 if (histogram)
1727 histogram->Add(sample);
1728}
1729
[email protected]2f496b42013-09-26 18:36:581730int Connection::OnSqliteError(int err, sql::Statement *stmt, const char* sql) {
[email protected]210ce0af2013-05-15 09:10:391731 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err);
1732 AddTaggedHistogram("Sqlite.Error", err);
[email protected]c088e3a32013-01-03 23:59:141733
1734 // Always log the error.
[email protected]2f496b42013-09-26 18:36:581735 if (!sql && stmt)
1736 sql = stmt->GetSQLStatement();
1737 if (!sql)
1738 sql = "-- unknown";
1739 LOG(ERROR) << histogram_tag_ << " sqlite error " << err
[email protected]c088e3a32013-01-03 23:59:141740 << ", errno " << GetLastErrno()
[email protected]2f496b42013-09-26 18:36:581741 << ": " << GetErrorMessage()
1742 << ", sql: " << sql;
[email protected]c088e3a32013-01-03 23:59:141743
[email protected]c3881b372013-05-17 08:39:461744 if (!error_callback_.is_null()) {
[email protected]98cf3002013-07-12 01:38:561745 // Fire from a copy of the callback in case of reentry into
1746 // re/set_error_callback().
1747 // TODO(shess): <http://crbug.com/254584>
1748 ErrorCallback(error_callback_).Run(err, stmt);
[email protected]c3881b372013-05-17 08:39:461749 return err;
1750 }
1751
[email protected]faa604e2009-09-25 22:38:591752 // The default handling is to assert on debug and to ignore on release.
[email protected]74cdede2013-09-25 05:39:571753 if (!ShouldIgnoreSqliteError(err))
[email protected]4350e322013-06-18 22:18:101754 DLOG(FATAL) << GetErrorMessage();
[email protected]faa604e2009-09-25 22:38:591755 return err;
1756}
1757
[email protected]579446c2013-12-16 18:36:521758bool Connection::FullIntegrityCheck(std::vector<std::string>* messages) {
1759 return IntegrityCheckHelper("PRAGMA integrity_check", messages);
1760}
1761
1762bool Connection::QuickIntegrityCheck() {
1763 std::vector<std::string> messages;
1764 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages))
1765 return false;
1766 return messages.size() == 1 && messages[0] == "ok";
1767}
1768
[email protected]80abf152013-05-22 12:42:421769// TODO(shess): Allow specifying maximum results (default 100 lines).
[email protected]579446c2013-12-16 18:36:521770bool Connection::IntegrityCheckHelper(
1771 const char* pragma_sql,
1772 std::vector<std::string>* messages) {
[email protected]80abf152013-05-22 12:42:421773 messages->clear();
1774
[email protected]4658e2a02013-06-06 23:05:001775 // This has the side effect of setting SQLITE_RecoveryMode, which
1776 // allows SQLite to process through certain cases of corruption.
1777 // Failing to set this pragma probably means that the database is
1778 // beyond recovery.
1779 const char kWritableSchema[] = "PRAGMA writable_schema = ON";
1780 if (!Execute(kWritableSchema))
1781 return false;
1782
1783 bool ret = false;
1784 {
[email protected]579446c2013-12-16 18:36:521785 sql::Statement stmt(GetUniqueStatement(pragma_sql));
[email protected]4658e2a02013-06-06 23:05:001786
1787 // The pragma appears to return all results (up to 100 by default)
1788 // as a single string. This doesn't appear to be an API contract,
1789 // it could return separate lines, so loop _and_ split.
1790 while (stmt.Step()) {
1791 std::string result(stmt.ColumnString(0));
brettw83dc1612015-08-12 07:31:181792 *messages = base::SplitString(result, "\n", base::TRIM_WHITESPACE,
1793 base::SPLIT_WANT_ALL);
[email protected]4658e2a02013-06-06 23:05:001794 }
1795 ret = stmt.Succeeded();
[email protected]80abf152013-05-22 12:42:421796 }
[email protected]4658e2a02013-06-06 23:05:001797
1798 // Best effort to put things back as they were before.
1799 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF";
1800 ignore_result(Execute(kNoWritableSchema));
1801
1802 return ret;
[email protected]80abf152013-05-22 12:42:421803}
1804
shess58b8df82015-06-03 00:19:321805base::TimeTicks TimeSource::Now() {
1806 return base::TimeTicks::Now();
1807}
1808
[email protected]e5ffd0e42009-09-11 21:30:561809} // namespace sql