blob: 300e5e6eaaba53f177224139dfc7d131065f0c32 [file] [log] [blame]
[email protected]64021042012-02-10 20:02:291// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f0a54b22011-07-19 18:40:215#include "sql/connection.h"
[email protected]e5ffd0e42009-09-11 21:30:566
7#include <string.h>
8
shessc9e80ae22015-08-12 21:39:119#include "base/bind.h"
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
thestig22dfc4012014-09-05 08:29:4411#include "base/files/file_util.h"
[email protected]a7ec1292013-07-22 22:02:1812#include "base/lazy_instance.h"
[email protected]e5ffd0e42009-09-11 21:30:5613#include "base/logging.h"
shessc9e80ae22015-08-12 21:39:1114#include "base/message_loop/message_loop.h"
[email protected]bd2ccdb4a2012-12-07 22:14:5015#include "base/metrics/histogram.h"
[email protected]210ce0af2013-05-15 09:10:3916#include "base/metrics/sparse_histogram.h"
[email protected]80abf152013-05-22 12:42:4217#include "base/strings/string_split.h"
[email protected]a4bbc1f92013-06-11 07:28:1918#include "base/strings/string_util.h"
19#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4520#include "base/strings/utf_string_conversions.h"
[email protected]a7ec1292013-07-22 22:02:1821#include "base/synchronization/lock.h"
[email protected]f0a54b22011-07-19 18:40:2122#include "sql/statement.h"
[email protected]e33cba42010-08-18 23:37:0323#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5624
[email protected]2e1cee762013-07-09 14:40:0025#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
26#include "third_party/sqlite/src/ext/icu/sqliteicu.h"
27#endif
28
[email protected]5b96f3772010-09-28 16:30:5729namespace {
30
31// Spin for up to a second waiting for the lock to clear when setting
32// up the database.
33// TODO(shess): Better story on this. http://crbug.com/56559
[email protected]c68ce172011-11-24 22:30:2734const int kBusyTimeoutSeconds = 1;
[email protected]5b96f3772010-09-28 16:30:5735
36class ScopedBusyTimeout {
37 public:
38 explicit ScopedBusyTimeout(sqlite3* db)
39 : db_(db) {
40 }
41 ~ScopedBusyTimeout() {
42 sqlite3_busy_timeout(db_, 0);
43 }
44
45 int SetTimeout(base::TimeDelta timeout) {
46 DCHECK_LT(timeout.InMilliseconds(), INT_MAX);
47 return sqlite3_busy_timeout(db_,
48 static_cast<int>(timeout.InMilliseconds()));
49 }
50
51 private:
52 sqlite3* db_;
53};
54
[email protected]6d42f152012-11-10 00:38:2455// Helper to "safely" enable writable_schema. No error checking
56// because it is reasonable to just forge ahead in case of an error.
57// If turning it on fails, then most likely nothing will work, whereas
58// if turning it off fails, it only matters if some code attempts to
59// continue working with the database and tries to modify the
60// sqlite_master table (none of our code does this).
61class ScopedWritableSchema {
62 public:
63 explicit ScopedWritableSchema(sqlite3* db)
64 : db_(db) {
65 sqlite3_exec(db_, "PRAGMA writable_schema=1", NULL, NULL, NULL);
66 }
67 ~ScopedWritableSchema() {
68 sqlite3_exec(db_, "PRAGMA writable_schema=0", NULL, NULL, NULL);
69 }
70
71 private:
72 sqlite3* db_;
73};
74
[email protected]7bae5742013-07-10 20:46:1675// Helper to wrap the sqlite3_backup_*() step of Raze(). Return
76// SQLite error code from running the backup step.
77int BackupDatabase(sqlite3* src, sqlite3* dst, const char* db_name) {
78 DCHECK_NE(src, dst);
79 sqlite3_backup* backup = sqlite3_backup_init(dst, db_name, src, db_name);
80 if (!backup) {
81 // Since this call only sets things up, this indicates a gross
82 // error in SQLite.
83 DLOG(FATAL) << "Unable to start sqlite3_backup(): " << sqlite3_errmsg(dst);
84 return sqlite3_errcode(dst);
85 }
86
87 // -1 backs up the entire database.
88 int rc = sqlite3_backup_step(backup, -1);
89 int pages = sqlite3_backup_pagecount(backup);
90 sqlite3_backup_finish(backup);
91
92 // If successful, exactly one page should have been backed up. If
93 // this breaks, check this function to make sure assumptions aren't
94 // being broken.
95 if (rc == SQLITE_DONE)
96 DCHECK_EQ(pages, 1);
97
98 return rc;
99}
100
[email protected]8d409412013-07-19 18:25:30101// Be very strict on attachment point. SQLite can handle a much wider
102// character set with appropriate quoting, but Chromium code should
103// just use clean names to start with.
104bool ValidAttachmentPoint(const char* attachment_point) {
105 for (size_t i = 0; attachment_point[i]; ++i) {
106 if (!((attachment_point[i] >= '0' && attachment_point[i] <= '9') ||
107 (attachment_point[i] >= 'a' && attachment_point[i] <= 'z') ||
108 (attachment_point[i] >= 'A' && attachment_point[i] <= 'Z') ||
109 attachment_point[i] == '_')) {
110 return false;
111 }
112 }
113 return true;
114}
115
shessc9e80ae22015-08-12 21:39:11116void RecordSqliteMemory10Min() {
117 const int64 used = sqlite3_memory_used();
118 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.TenMinutes", used / 1024);
119}
120
121void RecordSqliteMemoryHour() {
122 const int64 used = sqlite3_memory_used();
123 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneHour", used / 1024);
124}
125
126void RecordSqliteMemoryDay() {
127 const int64 used = sqlite3_memory_used();
128 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneDay", used / 1024);
129}
130
shess2d48e942015-08-25 17:39:51131void RecordSqliteMemoryWeek() {
132 const int64 used = sqlite3_memory_used();
133 UMA_HISTOGRAM_COUNTS("Sqlite.MemoryKB.OneWeek", used / 1024);
134}
135
[email protected]a7ec1292013-07-22 22:02:18136// SQLite automatically calls sqlite3_initialize() lazily, but
137// sqlite3_initialize() uses double-checked locking and thus can have
138// data races.
139//
140// TODO(shess): Another alternative would be to have
141// sqlite3_initialize() called as part of process bring-up. If this
142// is changed, remove the dynamic_annotations dependency in sql.gyp.
143base::LazyInstance<base::Lock>::Leaky
144 g_sqlite_init_lock = LAZY_INSTANCE_INITIALIZER;
145void InitializeSqlite() {
146 base::AutoLock lock(g_sqlite_init_lock.Get());
shessc9e80ae22015-08-12 21:39:11147 static bool first_call = true;
148 if (first_call) {
149 sqlite3_initialize();
150
151 // Schedule callback to record memory footprint histograms at 10m, 1h, and
152 // 1d. There may not be a message loop in tests.
153 if (base::MessageLoop::current()) {
154 base::MessageLoop::current()->PostDelayedTask(
155 FROM_HERE, base::Bind(&RecordSqliteMemory10Min),
156 base::TimeDelta::FromMinutes(10));
157 base::MessageLoop::current()->PostDelayedTask(
158 FROM_HERE, base::Bind(&RecordSqliteMemoryHour),
159 base::TimeDelta::FromHours(1));
160 base::MessageLoop::current()->PostDelayedTask(
161 FROM_HERE, base::Bind(&RecordSqliteMemoryDay),
162 base::TimeDelta::FromDays(1));
shess2d48e942015-08-25 17:39:51163 base::MessageLoop::current()->PostDelayedTask(
164 FROM_HERE, base::Bind(&RecordSqliteMemoryWeek),
165 base::TimeDelta::FromDays(7));
shessc9e80ae22015-08-12 21:39:11166 }
167
168 first_call = false;
169 }
[email protected]a7ec1292013-07-22 22:02:18170}
171
[email protected]8ada10f2013-12-21 00:42:34172// Helper to get the sqlite3_file* associated with the "main" database.
173int GetSqlite3File(sqlite3* db, sqlite3_file** file) {
174 *file = NULL;
175 int rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_FILE_POINTER, file);
176 if (rc != SQLITE_OK)
177 return rc;
178
179 // TODO(shess): NULL in file->pMethods has been observed on android_dbg
180 // content_unittests, even though it should not be possible.
181 // http://crbug.com/329982
182 if (!*file || !(*file)->pMethods)
183 return SQLITE_ERROR;
184
185 return rc;
186}
187
shess58b8df82015-06-03 00:19:32188// This should match UMA_HISTOGRAM_MEDIUM_TIMES().
189base::HistogramBase* GetMediumTimeHistogram(const std::string& name) {
190 return base::Histogram::FactoryTimeGet(
191 name,
192 base::TimeDelta::FromMilliseconds(10),
193 base::TimeDelta::FromMinutes(3),
194 50,
195 base::HistogramBase::kUmaTargetedHistogramFlag);
196}
197
erg102ceb412015-06-20 01:38:13198std::string AsUTF8ForSQL(const base::FilePath& path) {
199#if defined(OS_WIN)
200 return base::WideToUTF8(path.value());
201#elif defined(OS_POSIX)
202 return path.value();
203#endif
204}
205
[email protected]5b96f3772010-09-28 16:30:57206} // namespace
207
[email protected]e5ffd0e42009-09-11 21:30:56208namespace sql {
209
[email protected]4350e322013-06-18 22:18:10210// static
211Connection::ErrorIgnorerCallback* Connection::current_ignorer_cb_ = NULL;
212
213// static
[email protected]74cdede2013-09-25 05:39:57214bool Connection::ShouldIgnoreSqliteError(int error) {
[email protected]4350e322013-06-18 22:18:10215 if (!current_ignorer_cb_)
216 return false;
217 return current_ignorer_cb_->Run(error);
218}
219
220// static
221void Connection::SetErrorIgnorer(Connection::ErrorIgnorerCallback* cb) {
222 CHECK(current_ignorer_cb_ == NULL);
223 current_ignorer_cb_ = cb;
224}
225
226// static
227void Connection::ResetErrorIgnorer() {
228 CHECK(current_ignorer_cb_);
229 current_ignorer_cb_ = NULL;
230}
231
[email protected]e5ffd0e42009-09-11 21:30:56232bool StatementID::operator<(const StatementID& other) const {
233 if (number_ != other.number_)
234 return number_ < other.number_;
235 return strcmp(str_, other.str_) < 0;
236}
237
[email protected]e5ffd0e42009-09-11 21:30:56238Connection::StatementRef::StatementRef(Connection* connection,
[email protected]41a97c812013-02-07 02:35:38239 sqlite3_stmt* stmt,
240 bool was_valid)
[email protected]e5ffd0e42009-09-11 21:30:56241 : connection_(connection),
[email protected]41a97c812013-02-07 02:35:38242 stmt_(stmt),
243 was_valid_(was_valid) {
244 if (connection)
245 connection_->StatementRefCreated(this);
[email protected]e5ffd0e42009-09-11 21:30:56246}
247
248Connection::StatementRef::~StatementRef() {
249 if (connection_)
250 connection_->StatementRefDeleted(this);
[email protected]41a97c812013-02-07 02:35:38251 Close(false);
[email protected]e5ffd0e42009-09-11 21:30:56252}
253
[email protected]41a97c812013-02-07 02:35:38254void Connection::StatementRef::Close(bool forced) {
[email protected]e5ffd0e42009-09-11 21:30:56255 if (stmt_) {
[email protected]35f7e5392012-07-27 19:54:50256 // Call to AssertIOAllowed() cannot go at the beginning of the function
257 // because Close() is called unconditionally from destructor to clean
258 // connection_. And if this is inactive statement this won't cause any
259 // disk access and destructor most probably will be called on thread
260 // not allowing disk access.
261 // TODO([email protected]): This should move to the beginning
262 // of the function. http://crbug.com/136655.
263 AssertIOAllowed();
[email protected]e5ffd0e42009-09-11 21:30:56264 sqlite3_finalize(stmt_);
265 stmt_ = NULL;
266 }
267 connection_ = NULL; // The connection may be getting deleted.
[email protected]41a97c812013-02-07 02:35:38268
269 // Forced close is expected to happen from a statement error
270 // handler. In that case maintain the sense of |was_valid_| which
271 // previously held for this ref.
272 was_valid_ = was_valid_ && forced;
[email protected]e5ffd0e42009-09-11 21:30:56273}
274
275Connection::Connection()
276 : db_(NULL),
277 page_size_(0),
278 cache_size_(0),
279 exclusive_locking_(false),
[email protected]81a2a602013-07-17 19:10:36280 restrict_to_user_(false),
[email protected]e5ffd0e42009-09-11 21:30:56281 transaction_nesting_(0),
[email protected]35f7e5392012-07-27 19:54:50282 needs_rollback_(false),
[email protected]49dc4f22012-10-17 17:41:16283 in_memory_(false),
shess58b8df82015-06-03 00:19:32284 poisoned_(false),
285 stats_histogram_(NULL),
286 commit_time_histogram_(NULL),
287 autocommit_time_histogram_(NULL),
288 update_time_histogram_(NULL),
289 query_time_histogram_(NULL),
290 clock_(new TimeSource()) {
[email protected]526b4662013-06-14 04:09:12291}
[email protected]e5ffd0e42009-09-11 21:30:56292
293Connection::~Connection() {
294 Close();
295}
296
shess58b8df82015-06-03 00:19:32297void Connection::RecordEvent(Events event, size_t count) {
298 for (size_t i = 0; i < count; ++i) {
299 UMA_HISTOGRAM_ENUMERATION("Sqlite.Stats", event, EVENT_MAX_VALUE);
300 }
301
302 if (stats_histogram_) {
303 for (size_t i = 0; i < count; ++i) {
304 stats_histogram_->Add(event);
305 }
306 }
307}
308
309void Connection::RecordCommitTime(const base::TimeDelta& delta) {
310 RecordUpdateTime(delta);
311 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.CommitTime", delta);
312 if (commit_time_histogram_)
313 commit_time_histogram_->AddTime(delta);
314}
315
316void Connection::RecordAutoCommitTime(const base::TimeDelta& delta) {
317 RecordUpdateTime(delta);
318 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.AutoCommitTime", delta);
319 if (autocommit_time_histogram_)
320 autocommit_time_histogram_->AddTime(delta);
321}
322
323void Connection::RecordUpdateTime(const base::TimeDelta& delta) {
324 RecordQueryTime(delta);
325 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.UpdateTime", delta);
326 if (update_time_histogram_)
327 update_time_histogram_->AddTime(delta);
328}
329
330void Connection::RecordQueryTime(const base::TimeDelta& delta) {
331 UMA_HISTOGRAM_MEDIUM_TIMES("Sqlite.QueryTime", delta);
332 if (query_time_histogram_)
333 query_time_histogram_->AddTime(delta);
334}
335
336void Connection::RecordTimeAndChanges(
337 const base::TimeDelta& delta, bool read_only) {
338 if (read_only) {
339 RecordQueryTime(delta);
340 } else {
341 const int changes = sqlite3_changes(db_);
342 if (sqlite3_get_autocommit(db_)) {
343 RecordAutoCommitTime(delta);
344 RecordEvent(EVENT_CHANGES_AUTOCOMMIT, changes);
345 } else {
346 RecordUpdateTime(delta);
347 RecordEvent(EVENT_CHANGES, changes);
348 }
349 }
350}
351
[email protected]a3ef4832013-02-02 05:12:33352bool Connection::Open(const base::FilePath& path) {
[email protected]348ac8f52013-05-21 03:27:02353 if (!histogram_tag_.empty()) {
tfarina720d4f32015-05-11 22:31:26354 int64_t size_64 = 0;
[email protected]56285702013-12-04 18:22:49355 if (base::GetFileSize(path, &size_64)) {
[email protected]348ac8f52013-05-21 03:27:02356 size_t sample = static_cast<size_t>(size_64 / 1024);
357 std::string full_histogram_name = "Sqlite.SizeKB." + histogram_tag_;
358 base::HistogramBase* histogram =
359 base::Histogram::FactoryGet(
360 full_histogram_name, 1, 1000000, 50,
361 base::HistogramBase::kUmaTargetedHistogramFlag);
362 if (histogram)
363 histogram->Add(sample);
364 }
365 }
366
erg102ceb412015-06-20 01:38:13367 return OpenInternal(AsUTF8ForSQL(path), RETRY_ON_POISON);
[email protected]765b44502009-10-02 05:01:42368}
[email protected]e5ffd0e42009-09-11 21:30:56369
[email protected]765b44502009-10-02 05:01:42370bool Connection::OpenInMemory() {
[email protected]35f7e5392012-07-27 19:54:50371 in_memory_ = true;
[email protected]fed734a2013-07-17 04:45:13372 return OpenInternal(":memory:", NO_RETRY);
[email protected]e5ffd0e42009-09-11 21:30:56373}
374
[email protected]8d409412013-07-19 18:25:30375bool Connection::OpenTemporary() {
376 return OpenInternal("", NO_RETRY);
377}
378
[email protected]41a97c812013-02-07 02:35:38379void Connection::CloseInternal(bool forced) {
[email protected]4e179ba62012-03-17 16:06:47380 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
381 // will delete the -journal file. For ChromiumOS or other more
382 // embedded systems, this is probably not appropriate, whereas on
383 // desktop it might make some sense.
384
[email protected]4b350052012-02-24 20:40:48385 // sqlite3_close() needs all prepared statements to be finalized.
[email protected]4b350052012-02-24 20:40:48386
[email protected]41a97c812013-02-07 02:35:38387 // Release cached statements.
388 statement_cache_.clear();
389
390 // With cached statements released, in-use statements will remain.
391 // Closing the database while statements are in use is an API
392 // violation, except for forced close (which happens from within a
393 // statement's error handler).
394 DCHECK(forced || open_statements_.empty());
395
396 // Deactivate any outstanding statements so sqlite3_close() works.
397 for (StatementRefSet::iterator i = open_statements_.begin();
398 i != open_statements_.end(); ++i)
399 (*i)->Close(forced);
400 open_statements_.clear();
[email protected]4b350052012-02-24 20:40:48401
[email protected]e5ffd0e42009-09-11 21:30:56402 if (db_) {
[email protected]35f7e5392012-07-27 19:54:50403 // Call to AssertIOAllowed() cannot go at the beginning of the function
404 // because Close() must be called from destructor to clean
405 // statement_cache_, it won't cause any disk access and it most probably
406 // will happen on thread not allowing disk access.
407 // TODO([email protected]): This should move to the beginning
408 // of the function. http://crbug.com/136655.
409 AssertIOAllowed();
[email protected]73fb8d52013-07-24 05:04:28410
411 int rc = sqlite3_close(db_);
412 if (rc != SQLITE_OK) {
413 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.CloseFailure", rc);
414 DLOG(FATAL) << "sqlite3_close failed: " << GetErrorMessage();
415 }
[email protected]e5ffd0e42009-09-11 21:30:56416 }
[email protected]fed734a2013-07-17 04:45:13417 db_ = NULL;
[email protected]e5ffd0e42009-09-11 21:30:56418}
419
[email protected]41a97c812013-02-07 02:35:38420void Connection::Close() {
421 // If the database was already closed by RazeAndClose(), then no
422 // need to close again. Clear the |poisoned_| bit so that incorrect
423 // API calls are caught.
424 if (poisoned_) {
425 poisoned_ = false;
426 return;
427 }
428
429 CloseInternal(false);
430}
431
[email protected]e5ffd0e42009-09-11 21:30:56432void Connection::Preload() {
[email protected]35f7e5392012-07-27 19:54:50433 AssertIOAllowed();
434
[email protected]e5ffd0e42009-09-11 21:30:56435 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38436 DLOG_IF(FATAL, !poisoned_) << "Cannot preload null db";
[email protected]e5ffd0e42009-09-11 21:30:56437 return;
438 }
439
[email protected]8ada10f2013-12-21 00:42:34440 // Use local settings if provided, otherwise use documented defaults. The
441 // actual results could be fetching via PRAGMA calls.
442 const int page_size = page_size_ ? page_size_ : 1024;
443 sqlite3_int64 preload_size = page_size * (cache_size_ ? cache_size_ : 2000);
444 if (preload_size < 1)
[email protected]e5ffd0e42009-09-11 21:30:56445 return;
446
[email protected]8ada10f2013-12-21 00:42:34447 sqlite3_file* file = NULL;
448 int rc = GetSqlite3File(db_, &file);
449 if (rc != SQLITE_OK)
450 return;
451
452 sqlite3_int64 file_size = 0;
453 rc = file->pMethods->xFileSize(file, &file_size);
454 if (rc != SQLITE_OK)
455 return;
456
457 // Don't preload more than the file contains.
458 if (preload_size > file_size)
459 preload_size = file_size;
460
461 scoped_ptr<char[]> buf(new char[page_size]);
shessde60c5f12015-04-21 17:34:46462 for (sqlite3_int64 pos = 0; pos < preload_size; pos += page_size) {
[email protected]8ada10f2013-12-21 00:42:34463 rc = file->pMethods->xRead(file, buf.get(), page_size, pos);
464 if (rc != SQLITE_OK)
465 return;
466 }
[email protected]e5ffd0e42009-09-11 21:30:56467}
468
[email protected]be7995f12013-07-18 18:49:14469void Connection::TrimMemory(bool aggressively) {
470 if (!db_)
471 return;
472
473 // TODO(shess): investigate using sqlite3_db_release_memory() when possible.
474 int original_cache_size;
475 {
476 Statement sql_get_original(GetUniqueStatement("PRAGMA cache_size"));
477 if (!sql_get_original.Step()) {
478 DLOG(WARNING) << "Could not get cache size " << GetErrorMessage();
479 return;
480 }
481 original_cache_size = sql_get_original.ColumnInt(0);
482 }
483 int shrink_cache_size = aggressively ? 1 : (original_cache_size / 2);
484
485 // Force sqlite to try to reduce page cache usage.
486 const std::string sql_shrink =
487 base::StringPrintf("PRAGMA cache_size=%d", shrink_cache_size);
488 if (!Execute(sql_shrink.c_str()))
489 DLOG(WARNING) << "Could not shrink cache size: " << GetErrorMessage();
490
491 // Restore cache size.
492 const std::string sql_restore =
493 base::StringPrintf("PRAGMA cache_size=%d", original_cache_size);
494 if (!Execute(sql_restore.c_str()))
495 DLOG(WARNING) << "Could not restore cache size: " << GetErrorMessage();
496}
497
[email protected]8e0c01282012-04-06 19:36:49498// Create an in-memory database with the existing database's page
499// size, then backup that database over the existing database.
500bool Connection::Raze() {
[email protected]35f7e5392012-07-27 19:54:50501 AssertIOAllowed();
502
[email protected]8e0c01282012-04-06 19:36:49503 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38504 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:49505 return false;
506 }
507
508 if (transaction_nesting_ > 0) {
509 DLOG(FATAL) << "Cannot raze within a transaction";
510 return false;
511 }
512
513 sql::Connection null_db;
514 if (!null_db.OpenInMemory()) {
515 DLOG(FATAL) << "Unable to open in-memory database.";
516 return false;
517 }
518
[email protected]6d42f152012-11-10 00:38:24519 if (page_size_) {
520 // Enforce SQLite restrictions on |page_size_|.
521 DCHECK(!(page_size_ & (page_size_ - 1)))
522 << " page_size_ " << page_size_ << " is not a power of two.";
523 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
524 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:04525 const std::string sql =
526 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]69c58452012-08-06 19:22:42527 if (!null_db.Execute(sql.c_str()))
528 return false;
529 }
530
[email protected]6d42f152012-11-10 00:38:24531#if defined(OS_ANDROID)
532 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
533 // in-memory databases do not respect this define.
534 // TODO(shess): Figure out a way to set this without using platform
535 // specific code. AFAICT from sqlite3.c, the only way to do it
536 // would be to create an actual filesystem database, which is
537 // unfortunate.
538 if (!null_db.Execute("PRAGMA auto_vacuum = 1"))
539 return false;
540#endif
[email protected]8e0c01282012-04-06 19:36:49541
542 // The page size doesn't take effect until a database has pages, and
543 // at this point the null database has none. Changing the schema
544 // version will create the first page. This will not affect the
545 // schema version in the resulting database, as SQLite's backup
546 // implementation propagates the schema version from the original
547 // connection to the new version of the database, incremented by one
548 // so that other readers see the schema change and act accordingly.
549 if (!null_db.Execute("PRAGMA schema_version = 1"))
550 return false;
551
[email protected]6d42f152012-11-10 00:38:24552 // SQLite tracks the expected number of database pages in the first
553 // page, and if it does not match the total retrieved from a
554 // filesystem call, treats the database as corrupt. This situation
555 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
556 // used to hint to SQLite to soldier on in that case, specifically
557 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
558 // sqlite3.c lockBtree().]
559 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
560 // page_size" can be used to query such a database.
561 ScopedWritableSchema writable_schema(db_);
562
[email protected]7bae5742013-07-10 20:46:16563 const char* kMain = "main";
564 int rc = BackupDatabase(null_db.db_, db_, kMain);
565 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase",rc);
[email protected]8e0c01282012-04-06 19:36:49566
567 // The destination database was locked.
568 if (rc == SQLITE_BUSY) {
569 return false;
570 }
571
[email protected]7bae5742013-07-10 20:46:16572 // SQLITE_NOTADB can happen if page 1 of db_ exists, but is not
573 // formatted correctly. SQLITE_IOERR_SHORT_READ can happen if db_
574 // isn't even big enough for one page. Either way, reach in and
575 // truncate it before trying again.
576 // TODO(shess): Maybe it would be worthwhile to just truncate from
577 // the get-go?
578 if (rc == SQLITE_NOTADB || rc == SQLITE_IOERR_SHORT_READ) {
579 sqlite3_file* file = NULL;
[email protected]8ada10f2013-12-21 00:42:34580 rc = GetSqlite3File(db_, &file);
[email protected]7bae5742013-07-10 20:46:16581 if (rc != SQLITE_OK) {
582 DLOG(FATAL) << "Failure getting file handle.";
583 return false;
[email protected]7bae5742013-07-10 20:46:16584 }
585
586 rc = file->pMethods->xTruncate(file, 0);
587 if (rc != SQLITE_OK) {
588 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabaseTruncate",rc);
589 DLOG(FATAL) << "Failed to truncate file.";
590 return false;
591 }
592
593 rc = BackupDatabase(null_db.db_, db_, kMain);
594 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.RazeDatabase2",rc);
595
596 if (rc != SQLITE_DONE) {
597 DLOG(FATAL) << "Failed retrying Raze().";
598 }
599 }
600
[email protected]8e0c01282012-04-06 19:36:49601 // The entire database should have been backed up.
602 if (rc != SQLITE_DONE) {
[email protected]7bae5742013-07-10 20:46:16603 // TODO(shess): Figure out which other cases can happen.
[email protected]8e0c01282012-04-06 19:36:49604 DLOG(FATAL) << "Unable to copy entire null database.";
605 return false;
606 }
607
[email protected]8e0c01282012-04-06 19:36:49608 return true;
609}
610
611bool Connection::RazeWithTimout(base::TimeDelta timeout) {
612 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38613 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:49614 return false;
615 }
616
617 ScopedBusyTimeout busy_timeout(db_);
618 busy_timeout.SetTimeout(timeout);
619 return Raze();
620}
621
[email protected]41a97c812013-02-07 02:35:38622bool Connection::RazeAndClose() {
623 if (!db_) {
624 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
625 return false;
626 }
627
628 // Raze() cannot run in a transaction.
[email protected]8d409412013-07-19 18:25:30629 RollbackAllTransactions();
[email protected]41a97c812013-02-07 02:35:38630
631 bool result = Raze();
632
633 CloseInternal(true);
634
635 // Mark the database so that future API calls fail appropriately,
636 // but don't DCHECK (because after calling this function they are
637 // expected to fail).
638 poisoned_ = true;
639
640 return result;
641}
642
[email protected]8d409412013-07-19 18:25:30643void Connection::Poison() {
644 if (!db_) {
645 DLOG_IF(FATAL, !poisoned_) << "Cannot poison null db";
646 return;
647 }
648
649 RollbackAllTransactions();
650 CloseInternal(true);
651
652 // Mark the database so that future API calls fail appropriately,
653 // but don't DCHECK (because after calling this function they are
654 // expected to fail).
655 poisoned_ = true;
656}
657
[email protected]8d2e39e2013-06-24 05:55:08658// TODO(shess): To the extent possible, figure out the optimal
659// ordering for these deletes which will prevent other connections
660// from seeing odd behavior. For instance, it may be necessary to
661// manually lock the main database file in a SQLite-compatible fashion
662// (to prevent other processes from opening it), then delete the
663// journal files, then delete the main database file. Another option
664// might be to lock the main database file and poison the header with
665// junk to prevent other processes from opening it successfully (like
666// Gears "SQLite poison 3" trick).
667//
668// static
669bool Connection::Delete(const base::FilePath& path) {
670 base::ThreadRestrictions::AssertIOAllowed();
671
672 base::FilePath journal_path(path.value() + FILE_PATH_LITERAL("-journal"));
673 base::FilePath wal_path(path.value() + FILE_PATH_LITERAL("-wal"));
674
erg102ceb412015-06-20 01:38:13675 std::string journal_str = AsUTF8ForSQL(journal_path);
676 std::string wal_str = AsUTF8ForSQL(wal_path);
677 std::string path_str = AsUTF8ForSQL(path);
[email protected]8d2e39e2013-06-24 05:55:08678
shess702467622015-09-16 19:04:55679 // Make sure sqlite3_initialize() is called before anything else.
680 InitializeSqlite();
681
erg102ceb412015-06-20 01:38:13682 sqlite3_vfs* vfs = sqlite3_vfs_find(NULL);
683 CHECK(vfs);
684 CHECK(vfs->xDelete);
685 CHECK(vfs->xAccess);
686
687 // We only work with unix, win32 and mojo filesystems. If you're trying to
688 // use this code with any other VFS, you're not in a good place.
689 CHECK(strncmp(vfs->zName, "unix", 4) == 0 ||
690 strncmp(vfs->zName, "win32", 5) == 0 ||
691 strcmp(vfs->zName, "mojo") == 0);
692
693 vfs->xDelete(vfs, journal_str.c_str(), 0);
694 vfs->xDelete(vfs, wal_str.c_str(), 0);
695 vfs->xDelete(vfs, path_str.c_str(), 0);
696
697 int journal_exists = 0;
698 vfs->xAccess(vfs, journal_str.c_str(), SQLITE_ACCESS_EXISTS,
699 &journal_exists);
700
701 int wal_exists = 0;
702 vfs->xAccess(vfs, wal_str.c_str(), SQLITE_ACCESS_EXISTS,
703 &wal_exists);
704
705 int path_exists = 0;
706 vfs->xAccess(vfs, path_str.c_str(), SQLITE_ACCESS_EXISTS,
707 &path_exists);
708
709 return !journal_exists && !wal_exists && !path_exists;
[email protected]8d2e39e2013-06-24 05:55:08710}
711
[email protected]e5ffd0e42009-09-11 21:30:56712bool Connection::BeginTransaction() {
713 if (needs_rollback_) {
[email protected]88563f62011-03-13 22:13:33714 DCHECK_GT(transaction_nesting_, 0);
[email protected]e5ffd0e42009-09-11 21:30:56715
716 // When we're going to rollback, fail on this begin and don't actually
717 // mark us as entering the nested transaction.
718 return false;
719 }
720
721 bool success = true;
722 if (!transaction_nesting_) {
723 needs_rollback_ = false;
724
725 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
shess58b8df82015-06-03 00:19:32726 RecordOneEvent(EVENT_BEGIN);
[email protected]eff1fa522011-12-12 23:50:59727 if (!begin.Run())
[email protected]e5ffd0e42009-09-11 21:30:56728 return false;
729 }
730 transaction_nesting_++;
731 return success;
732}
733
734void Connection::RollbackTransaction() {
735 if (!transaction_nesting_) {
[email protected]41a97c812013-02-07 02:35:38736 DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:56737 return;
738 }
739
740 transaction_nesting_--;
741
742 if (transaction_nesting_ > 0) {
743 // Mark the outermost transaction as needing rollback.
744 needs_rollback_ = true;
745 return;
746 }
747
748 DoRollback();
749}
750
751bool Connection::CommitTransaction() {
752 if (!transaction_nesting_) {
[email protected]41a97c812013-02-07 02:35:38753 DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:56754 return false;
755 }
756 transaction_nesting_--;
757
758 if (transaction_nesting_ > 0) {
759 // Mark any nested transactions as failing after we've already got one.
760 return !needs_rollback_;
761 }
762
763 if (needs_rollback_) {
764 DoRollback();
765 return false;
766 }
767
768 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
shess58b8df82015-06-03 00:19:32769
770 // Collect the commit time manually, sql::Statement would register it as query
771 // time only.
772 const base::TimeTicks before = Now();
773 bool ret = commit.RunWithoutTimers();
774 const base::TimeDelta delta = Now() - before;
775
776 RecordCommitTime(delta);
777 RecordOneEvent(EVENT_COMMIT);
778
779 return ret;
[email protected]e5ffd0e42009-09-11 21:30:56780}
781
[email protected]8d409412013-07-19 18:25:30782void Connection::RollbackAllTransactions() {
783 if (transaction_nesting_ > 0) {
784 transaction_nesting_ = 0;
785 DoRollback();
786 }
787}
788
789bool Connection::AttachDatabase(const base::FilePath& other_db_path,
790 const char* attachment_point) {
791 DCHECK(ValidAttachmentPoint(attachment_point));
792
793 Statement s(GetUniqueStatement("ATTACH DATABASE ? AS ?"));
794#if OS_WIN
795 s.BindString16(0, other_db_path.value());
796#else
797 s.BindString(0, other_db_path.value());
798#endif
799 s.BindString(1, attachment_point);
800 return s.Run();
801}
802
803bool Connection::DetachDatabase(const char* attachment_point) {
804 DCHECK(ValidAttachmentPoint(attachment_point));
805
806 Statement s(GetUniqueStatement("DETACH DATABASE ?"));
807 s.BindString(0, attachment_point);
808 return s.Run();
809}
810
shess58b8df82015-06-03 00:19:32811// TODO(shess): Consider changing this to execute exactly one statement. If a
812// caller wishes to execute multiple statements, that should be explicit, and
813// perhaps tucked into an explicit transaction with rollback in case of error.
[email protected]eff1fa522011-12-12 23:50:59814int Connection::ExecuteAndReturnErrorCode(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:50815 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:38816 if (!db_) {
817 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
818 return SQLITE_ERROR;
819 }
shess58b8df82015-06-03 00:19:32820 DCHECK(sql);
821
822 RecordOneEvent(EVENT_EXECUTE);
823 int rc = SQLITE_OK;
824 while ((rc == SQLITE_OK) && *sql) {
825 sqlite3_stmt *stmt = NULL;
826 const char *leftover_sql;
827
828 const base::TimeTicks before = Now();
829 rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, &leftover_sql);
830 sql = leftover_sql;
831
832 // Stop if an error is encountered.
833 if (rc != SQLITE_OK)
834 break;
835
836 // This happens if |sql| originally only contained comments or whitespace.
837 // TODO(shess): Audit to see if this can become a DCHECK(). Having
838 // extraneous comments and whitespace in the SQL statements increases
839 // runtime cost and can easily be shifted out to the C++ layer.
840 if (!stmt)
841 continue;
842
843 // Save for use after statement is finalized.
844 const bool read_only = !!sqlite3_stmt_readonly(stmt);
845
846 RecordOneEvent(Connection::EVENT_STATEMENT_RUN);
847 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
848 // TODO(shess): Audit to see if this can become a DCHECK. I think PRAGMA
849 // is the only legitimate case for this.
850 RecordOneEvent(Connection::EVENT_STATEMENT_ROWS);
851 }
852
853 // sqlite3_finalize() returns SQLITE_OK if the most recent sqlite3_step()
854 // returned SQLITE_DONE or SQLITE_ROW, otherwise the error code.
855 rc = sqlite3_finalize(stmt);
856 if (rc == SQLITE_OK)
857 RecordOneEvent(Connection::EVENT_STATEMENT_SUCCESS);
858
859 // sqlite3_exec() does this, presumably to avoid spinning the parser for
860 // trailing whitespace.
861 // TODO(shess): Audit to see if this can become a DCHECK.
brettwb3413062015-06-24 00:39:02862 while (base::IsAsciiWhitespace(*sql)) {
shess58b8df82015-06-03 00:19:32863 sql++;
864 }
865
866 const base::TimeDelta delta = Now() - before;
867 RecordTimeAndChanges(delta, read_only);
868 }
869 return rc;
[email protected]eff1fa522011-12-12 23:50:59870}
871
872bool Connection::Execute(const char* sql) {
[email protected]41a97c812013-02-07 02:35:38873 if (!db_) {
874 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
875 return false;
876 }
877
[email protected]eff1fa522011-12-12 23:50:59878 int error = ExecuteAndReturnErrorCode(sql);
[email protected]473ad792012-11-10 00:55:00879 if (error != SQLITE_OK)
[email protected]2f496b42013-09-26 18:36:58880 error = OnSqliteError(error, NULL, sql);
[email protected]473ad792012-11-10 00:55:00881
[email protected]28fe0ff2012-02-25 00:40:33882 // This needs to be a FATAL log because the error case of arriving here is
883 // that there's a malformed SQL statement. This can arise in development if
[email protected]4350e322013-06-18 22:18:10884 // a change alters the schema but not all queries adjust. This can happen
885 // in production if the schema is corrupted.
[email protected]eff1fa522011-12-12 23:50:59886 if (error == SQLITE_ERROR)
[email protected]28fe0ff2012-02-25 00:40:33887 DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage();
[email protected]eff1fa522011-12-12 23:50:59888 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:56889}
890
[email protected]5b96f3772010-09-28 16:30:57891bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
[email protected]41a97c812013-02-07 02:35:38892 if (!db_) {
893 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]5b96f3772010-09-28 16:30:57894 return false;
[email protected]41a97c812013-02-07 02:35:38895 }
[email protected]5b96f3772010-09-28 16:30:57896
897 ScopedBusyTimeout busy_timeout(db_);
898 busy_timeout.SetTimeout(timeout);
[email protected]eff1fa522011-12-12 23:50:59899 return Execute(sql);
[email protected]5b96f3772010-09-28 16:30:57900}
901
[email protected]e5ffd0e42009-09-11 21:30:56902bool Connection::HasCachedStatement(const StatementID& id) const {
903 return statement_cache_.find(id) != statement_cache_.end();
904}
905
906scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement(
907 const StatementID& id,
908 const char* sql) {
909 CachedStatementMap::iterator i = statement_cache_.find(id);
910 if (i != statement_cache_.end()) {
911 // Statement is in the cache. It should still be active (we're the only
912 // one invalidating cached statements, and we'll remove it from the cache
913 // if we do that. Make sure we reset it before giving out the cached one in
914 // case it still has some stuff bound.
915 DCHECK(i->second->is_valid());
916 sqlite3_reset(i->second->stmt());
917 return i->second;
918 }
919
920 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
921 if (statement->is_valid())
922 statement_cache_[id] = statement; // Only cache valid statements.
923 return statement;
924}
925
926scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement(
927 const char* sql) {
[email protected]35f7e5392012-07-27 19:54:50928 AssertIOAllowed();
929
[email protected]41a97c812013-02-07 02:35:38930 // Return inactive statement.
[email protected]e5ffd0e42009-09-11 21:30:56931 if (!db_)
[email protected]41a97c812013-02-07 02:35:38932 return new StatementRef(NULL, NULL, poisoned_);
[email protected]e5ffd0e42009-09-11 21:30:56933
934 sqlite3_stmt* stmt = NULL;
[email protected]473ad792012-11-10 00:55:00935 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
936 if (rc != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:59937 // This is evidence of a syntax error in the incoming SQL.
shess193bfb622015-04-10 22:30:02938 if (!ShouldIgnoreSqliteError(rc))
939 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]473ad792012-11-10 00:55:00940
941 // It could also be database corruption.
[email protected]2f496b42013-09-26 18:36:58942 OnSqliteError(rc, NULL, sql);
[email protected]41a97c812013-02-07 02:35:38943 return new StatementRef(NULL, NULL, false);
[email protected]e5ffd0e42009-09-11 21:30:56944 }
[email protected]41a97c812013-02-07 02:35:38945 return new StatementRef(this, stmt, true);
[email protected]e5ffd0e42009-09-11 21:30:56946}
947
[email protected]2eec0a22012-07-24 01:59:58948scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
949 const char* sql) const {
[email protected]41a97c812013-02-07 02:35:38950 // Return inactive statement.
[email protected]2eec0a22012-07-24 01:59:58951 if (!db_)
[email protected]41a97c812013-02-07 02:35:38952 return new StatementRef(NULL, NULL, poisoned_);
[email protected]2eec0a22012-07-24 01:59:58953
954 sqlite3_stmt* stmt = NULL;
955 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
956 if (rc != SQLITE_OK) {
957 // This is evidence of a syntax error in the incoming SQL.
shess193bfb622015-04-10 22:30:02958 if (!ShouldIgnoreSqliteError(rc))
959 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]41a97c812013-02-07 02:35:38960 return new StatementRef(NULL, NULL, false);
[email protected]2eec0a22012-07-24 01:59:58961 }
[email protected]41a97c812013-02-07 02:35:38962 return new StatementRef(NULL, stmt, true);
[email protected]2eec0a22012-07-24 01:59:58963}
964
[email protected]92cd00a2013-08-16 11:09:58965std::string Connection::GetSchema() const {
966 // The ORDER BY should not be necessary, but relying on organic
967 // order for something like this is questionable.
968 const char* kSql =
969 "SELECT type, name, tbl_name, sql "
970 "FROM sqlite_master ORDER BY 1, 2, 3, 4";
971 Statement statement(GetUntrackedStatement(kSql));
972
973 std::string schema;
974 while (statement.Step()) {
975 schema += statement.ColumnString(0);
976 schema += '|';
977 schema += statement.ColumnString(1);
978 schema += '|';
979 schema += statement.ColumnString(2);
980 schema += '|';
981 schema += statement.ColumnString(3);
982 schema += '\n';
983 }
984
985 return schema;
986}
987
[email protected]eff1fa522011-12-12 23:50:59988bool Connection::IsSQLValid(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:50989 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:38990 if (!db_) {
991 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
992 return false;
993 }
994
[email protected]eff1fa522011-12-12 23:50:59995 sqlite3_stmt* stmt = NULL;
996 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK)
997 return false;
998
999 sqlite3_finalize(stmt);
1000 return true;
1001}
1002
[email protected]1ed78a32009-09-15 20:24:171003bool Connection::DoesTableExist(const char* table_name) const {
[email protected]e2cadec82011-12-13 02:00:531004 return DoesTableOrIndexExist(table_name, "table");
1005}
1006
1007bool Connection::DoesIndexExist(const char* index_name) const {
1008 return DoesTableOrIndexExist(index_name, "index");
1009}
1010
1011bool Connection::DoesTableOrIndexExist(
1012 const char* name, const char* type) const {
shess92a2ab12015-04-09 01:59:471013 const char* kSql =
1014 "SELECT name FROM sqlite_master WHERE type=? AND name=? COLLATE NOCASE";
[email protected]2eec0a22012-07-24 01:59:581015 Statement statement(GetUntrackedStatement(kSql));
shess92a2ab12015-04-09 01:59:471016
1017 // This can happen if the database is corrupt and the error is being ignored
1018 // for testing purposes.
1019 if (!statement.is_valid())
1020 return false;
1021
[email protected]e2cadec82011-12-13 02:00:531022 statement.BindString(0, type);
1023 statement.BindString(1, name);
[email protected]28fe0ff2012-02-25 00:40:331024
[email protected]e5ffd0e42009-09-11 21:30:561025 return statement.Step(); // Table exists if any row was returned.
1026}
1027
1028bool Connection::DoesColumnExist(const char* table_name,
[email protected]1ed78a32009-09-15 20:24:171029 const char* column_name) const {
[email protected]e5ffd0e42009-09-11 21:30:561030 std::string sql("PRAGMA TABLE_INFO(");
1031 sql.append(table_name);
1032 sql.append(")");
1033
[email protected]2eec0a22012-07-24 01:59:581034 Statement statement(GetUntrackedStatement(sql.c_str()));
shess92a2ab12015-04-09 01:59:471035
1036 // This can happen if the database is corrupt and the error is being ignored
1037 // for testing purposes.
1038 if (!statement.is_valid())
1039 return false;
1040
[email protected]e5ffd0e42009-09-11 21:30:561041 while (statement.Step()) {
brettw8a800902015-07-10 18:28:331042 if (base::EqualsCaseInsensitiveASCII(statement.ColumnString(1),
1043 column_name))
[email protected]e5ffd0e42009-09-11 21:30:561044 return true;
1045 }
1046 return false;
1047}
1048
tfarina720d4f32015-05-11 22:31:261049int64_t Connection::GetLastInsertRowId() const {
[email protected]e5ffd0e42009-09-11 21:30:561050 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381051 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]e5ffd0e42009-09-11 21:30:561052 return 0;
1053 }
1054 return sqlite3_last_insert_rowid(db_);
1055}
1056
[email protected]1ed78a32009-09-15 20:24:171057int Connection::GetLastChangeCount() const {
1058 if (!db_) {
[email protected]41a97c812013-02-07 02:35:381059 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]1ed78a32009-09-15 20:24:171060 return 0;
1061 }
1062 return sqlite3_changes(db_);
1063}
1064
[email protected]e5ffd0e42009-09-11 21:30:561065int Connection::GetErrorCode() const {
1066 if (!db_)
1067 return SQLITE_ERROR;
1068 return sqlite3_errcode(db_);
1069}
1070
[email protected]767718e52010-09-21 23:18:491071int Connection::GetLastErrno() const {
1072 if (!db_)
1073 return -1;
1074
1075 int err = 0;
1076 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err))
1077 return -2;
1078
1079 return err;
1080}
1081
[email protected]e5ffd0e42009-09-11 21:30:561082const char* Connection::GetErrorMessage() const {
1083 if (!db_)
1084 return "sql::Connection has no connection.";
1085 return sqlite3_errmsg(db_);
1086}
1087
[email protected]fed734a2013-07-17 04:45:131088bool Connection::OpenInternal(const std::string& file_name,
1089 Connection::Retry retry_flag) {
[email protected]35f7e5392012-07-27 19:54:501090 AssertIOAllowed();
1091
[email protected]9cfbc922009-11-17 20:13:171092 if (db_) {
[email protected]eff1fa522011-12-12 23:50:591093 DLOG(FATAL) << "sql::Connection is already open.";
[email protected]9cfbc922009-11-17 20:13:171094 return false;
1095 }
1096
[email protected]a7ec1292013-07-22 22:02:181097 // Make sure sqlite3_initialize() is called before anything else.
1098 InitializeSqlite();
1099
shess58b8df82015-06-03 00:19:321100 // Setup the stats histograms immediately rather than allocating lazily.
1101 // Connections which won't exercise all of these probably shouldn't exist.
1102 if (!histogram_tag_.empty()) {
1103 stats_histogram_ =
1104 base::LinearHistogram::FactoryGet(
1105 "Sqlite.Stats." + histogram_tag_,
1106 1, EVENT_MAX_VALUE, EVENT_MAX_VALUE + 1,
1107 base::HistogramBase::kUmaTargetedHistogramFlag);
1108
1109 // The timer setup matches UMA_HISTOGRAM_MEDIUM_TIMES(). 3 minutes is an
1110 // unreasonable time for any single operation, so there is not much value to
1111 // knowing if it was 3 minutes or 5 minutes. In reality at that point
1112 // things are entirely busted.
1113 commit_time_histogram_ =
1114 GetMediumTimeHistogram("Sqlite.CommitTime." + histogram_tag_);
1115
1116 autocommit_time_histogram_ =
1117 GetMediumTimeHistogram("Sqlite.AutoCommitTime." + histogram_tag_);
1118
1119 update_time_histogram_ =
1120 GetMediumTimeHistogram("Sqlite.UpdateTime." + histogram_tag_);
1121
1122 query_time_histogram_ =
1123 GetMediumTimeHistogram("Sqlite.QueryTime." + histogram_tag_);
1124 }
1125
[email protected]41a97c812013-02-07 02:35:381126 // If |poisoned_| is set, it means an error handler called
1127 // RazeAndClose(). Until regular Close() is called, the caller
1128 // should be treating the database as open, but is_open() currently
1129 // only considers the sqlite3 handle's state.
1130 // TODO(shess): Revise is_open() to consider poisoned_, and review
1131 // to see if any non-testing code even depends on it.
1132 DLOG_IF(FATAL, poisoned_) << "sql::Connection is already open.";
[email protected]7bae5742013-07-10 20:46:161133 poisoned_ = false;
[email protected]41a97c812013-02-07 02:35:381134
[email protected]765b44502009-10-02 05:01:421135 int err = sqlite3_open(file_name.c_str(), &db_);
1136 if (err != SQLITE_OK) {
[email protected]73fb8d52013-07-24 05:04:281137 // Extended error codes cannot be enabled until a handle is
1138 // available, fetch manually.
1139 err = sqlite3_extended_errcode(db_);
1140
[email protected]bd2ccdb4a2012-12-07 22:14:501141 // Histogram failures specific to initial open for debugging
1142 // purposes.
[email protected]73fb8d52013-07-24 05:04:281143 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenFailure", err);
[email protected]bd2ccdb4a2012-12-07 22:14:501144
[email protected]2f496b42013-09-26 18:36:581145 OnSqliteError(err, NULL, "-- sqlite3_open()");
[email protected]fed734a2013-07-17 04:45:131146 bool was_poisoned = poisoned_;
[email protected]64021042012-02-10 20:02:291147 Close();
[email protected]fed734a2013-07-17 04:45:131148
1149 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1150 return OpenInternal(file_name, NO_RETRY);
[email protected]765b44502009-10-02 05:01:421151 return false;
1152 }
1153
[email protected]81a2a602013-07-17 19:10:361154 // TODO(shess): OS_WIN support?
1155#if defined(OS_POSIX)
1156 if (restrict_to_user_) {
1157 DCHECK_NE(file_name, std::string(":memory"));
1158 base::FilePath file_path(file_name);
1159 int mode = 0;
1160 // TODO(shess): Arguably, failure to retrieve and change
1161 // permissions should be fatal if the file exists.
[email protected]b264eab2013-11-27 23:22:081162 if (base::GetPosixFilePermissions(file_path, &mode)) {
1163 mode &= base::FILE_PERMISSION_USER_MASK;
1164 base::SetPosixFilePermissions(file_path, mode);
[email protected]81a2a602013-07-17 19:10:361165
1166 // SQLite sets the permissions on these files from the main
1167 // database on create. Set them here in case they already exist
1168 // at this point. Failure to set these permissions should not
1169 // be fatal unless the file doesn't exist.
1170 base::FilePath journal_path(file_name + FILE_PATH_LITERAL("-journal"));
1171 base::FilePath wal_path(file_name + FILE_PATH_LITERAL("-wal"));
[email protected]b264eab2013-11-27 23:22:081172 base::SetPosixFilePermissions(journal_path, mode);
1173 base::SetPosixFilePermissions(wal_path, mode);
[email protected]81a2a602013-07-17 19:10:361174 }
1175 }
1176#endif // defined(OS_POSIX)
1177
[email protected]affa2da2013-06-06 22:20:341178 // SQLite uses a lookaside buffer to improve performance of small mallocs.
1179 // Chromium already depends on small mallocs being efficient, so we disable
1180 // this to avoid the extra memory overhead.
1181 // This must be called immediatly after opening the database before any SQL
1182 // statements are run.
1183 sqlite3_db_config(db_, SQLITE_DBCONFIG_LOOKASIDE, NULL, 0, 0);
1184
[email protected]73fb8d52013-07-24 05:04:281185 // Enable extended result codes to provide more color on I/O errors.
1186 // Not having extended result codes is not a fatal problem, as
1187 // Chromium code does not attempt to handle I/O errors anyhow. The
1188 // current implementation always returns SQLITE_OK, the DCHECK is to
1189 // quickly notify someone if SQLite changes.
1190 err = sqlite3_extended_result_codes(db_, 1);
1191 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
1192
[email protected]bd2ccdb4a2012-12-07 22:14:501193 // sqlite3_open() does not actually read the database file (unless a
1194 // hot journal is found). Successfully executing this pragma on an
1195 // existing database requires a valid header on page 1.
1196 // TODO(shess): For now, just probing to see what the lay of the
1197 // land is. If it's mostly SQLITE_NOTADB, then the database should
1198 // be razed.
1199 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
1200 if (err != SQLITE_OK)
[email protected]73fb8d52013-07-24 05:04:281201 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenProbeFailure", err);
[email protected]658f8332010-09-18 04:40:431202
[email protected]2e1cee762013-07-09 14:40:001203#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
1204 // The version of SQLite shipped with iOS doesn't enable ICU, which includes
1205 // REGEXP support. Add it in dynamically.
1206 err = sqlite3IcuInit(db_);
1207 DCHECK_EQ(err, SQLITE_OK) << "Could not enable ICU support";
1208#endif // OS_IOS && USE_SYSTEM_SQLITE
1209
[email protected]5b96f3772010-09-28 16:30:571210 // If indicated, lock up the database before doing anything else, so
1211 // that the following code doesn't have to deal with locking.
1212 // TODO(shess): This code is brittle. Find the cases where code
1213 // doesn't request |exclusive_locking_| and audit that it does the
1214 // right thing with SQLITE_BUSY, and that it doesn't make
1215 // assumptions about who might change things in the database.
1216 // http://crbug.com/56559
1217 if (exclusive_locking_) {
[email protected]4350e322013-06-18 22:18:101218 // TODO(shess): This should probably be a failure. Code which
1219 // requests exclusive locking but doesn't get it is almost certain
1220 // to be ill-tested.
1221 ignore_result(Execute("PRAGMA locking_mode=EXCLUSIVE"));
[email protected]5b96f3772010-09-28 16:30:571222 }
1223
[email protected]4e179ba62012-03-17 16:06:471224 // http://www.sqlite.org/pragma.html#pragma_journal_mode
1225 // DELETE (default) - delete -journal file to commit.
1226 // TRUNCATE - truncate -journal file to commit.
1227 // PERSIST - zero out header of -journal file to commit.
shess2c21ecf2015-06-02 01:31:091228 // TRUNCATE should be faster than DELETE because it won't need directory
1229 // changes for each transaction. PERSIST may break the spirit of using
1230 // secure_delete.
1231 ignore_result(Execute("PRAGMA journal_mode = TRUNCATE"));
[email protected]4e179ba62012-03-17 16:06:471232
[email protected]c68ce172011-11-24 22:30:271233 const base::TimeDelta kBusyTimeout =
1234 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
1235
[email protected]765b44502009-10-02 05:01:421236 if (page_size_ != 0) {
[email protected]5b96f3772010-09-28 16:30:571237 // Enforce SQLite restrictions on |page_size_|.
1238 DCHECK(!(page_size_ & (page_size_ - 1)))
1239 << " page_size_ " << page_size_ << " is not a power of two.";
[email protected]6d42f152012-11-10 00:38:241240 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
[email protected]5b96f3772010-09-28 16:30:571241 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:041242 const std::string sql =
1243 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]4350e322013-06-18 22:18:101244 ignore_result(ExecuteWithTimeout(sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421245 }
1246
1247 if (cache_size_ != 0) {
[email protected]7d3cbc92013-03-18 22:33:041248 const std::string sql =
1249 base::StringPrintf("PRAGMA cache_size=%d", cache_size_);
[email protected]4350e322013-06-18 22:18:101250 ignore_result(ExecuteWithTimeout(sql.c_str(), kBusyTimeout));
[email protected]765b44502009-10-02 05:01:421251 }
1252
[email protected]6e0b1442011-08-09 23:23:581253 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) {
[email protected]fed734a2013-07-17 04:45:131254 bool was_poisoned = poisoned_;
[email protected]6e0b1442011-08-09 23:23:581255 Close();
[email protected]fed734a2013-07-17 04:45:131256 if (was_poisoned && retry_flag == RETRY_ON_POISON)
1257 return OpenInternal(file_name, NO_RETRY);
[email protected]6e0b1442011-08-09 23:23:581258 return false;
1259 }
1260
[email protected]765b44502009-10-02 05:01:421261 return true;
1262}
1263
[email protected]e5ffd0e42009-09-11 21:30:561264void Connection::DoRollback() {
1265 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
shess58b8df82015-06-03 00:19:321266
1267 // Collect the rollback time manually, sql::Statement would register it as
1268 // query time only.
1269 const base::TimeTicks before = Now();
1270 rollback.RunWithoutTimers();
1271 const base::TimeDelta delta = Now() - before;
1272
1273 RecordUpdateTime(delta);
1274 RecordOneEvent(EVENT_ROLLBACK);
1275
[email protected]44ad7d902012-03-23 00:09:051276 needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:561277}
1278
1279void Connection::StatementRefCreated(StatementRef* ref) {
1280 DCHECK(open_statements_.find(ref) == open_statements_.end());
1281 open_statements_.insert(ref);
1282}
1283
1284void Connection::StatementRefDeleted(StatementRef* ref) {
1285 StatementRefSet::iterator i = open_statements_.find(ref);
1286 if (i == open_statements_.end())
[email protected]eff1fa522011-12-12 23:50:591287 DLOG(FATAL) << "Could not find statement";
[email protected]e5ffd0e42009-09-11 21:30:561288 else
1289 open_statements_.erase(i);
1290}
1291
shess58b8df82015-06-03 00:19:321292void Connection::set_histogram_tag(const std::string& tag) {
1293 DCHECK(!is_open());
1294 histogram_tag_ = tag;
1295}
1296
[email protected]210ce0af2013-05-15 09:10:391297void Connection::AddTaggedHistogram(const std::string& name,
1298 size_t sample) const {
1299 if (histogram_tag_.empty())
1300 return;
1301
1302 // TODO(shess): The histogram macros create a bit of static storage
1303 // for caching the histogram object. This code shouldn't execute
1304 // often enough for such caching to be crucial. If it becomes an
1305 // issue, the object could be cached alongside histogram_prefix_.
1306 std::string full_histogram_name = name + "." + histogram_tag_;
1307 base::HistogramBase* histogram =
1308 base::SparseHistogram::FactoryGet(
1309 full_histogram_name,
1310 base::HistogramBase::kUmaTargetedHistogramFlag);
1311 if (histogram)
1312 histogram->Add(sample);
1313}
1314
[email protected]2f496b42013-09-26 18:36:581315int Connection::OnSqliteError(int err, sql::Statement *stmt, const char* sql) {
[email protected]210ce0af2013-05-15 09:10:391316 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err);
1317 AddTaggedHistogram("Sqlite.Error", err);
[email protected]c088e3a32013-01-03 23:59:141318
1319 // Always log the error.
[email protected]2f496b42013-09-26 18:36:581320 if (!sql && stmt)
1321 sql = stmt->GetSQLStatement();
1322 if (!sql)
1323 sql = "-- unknown";
1324 LOG(ERROR) << histogram_tag_ << " sqlite error " << err
[email protected]c088e3a32013-01-03 23:59:141325 << ", errno " << GetLastErrno()
[email protected]2f496b42013-09-26 18:36:581326 << ": " << GetErrorMessage()
1327 << ", sql: " << sql;
[email protected]c088e3a32013-01-03 23:59:141328
[email protected]c3881b372013-05-17 08:39:461329 if (!error_callback_.is_null()) {
[email protected]98cf3002013-07-12 01:38:561330 // Fire from a copy of the callback in case of reentry into
1331 // re/set_error_callback().
1332 // TODO(shess): <http://crbug.com/254584>
1333 ErrorCallback(error_callback_).Run(err, stmt);
[email protected]c3881b372013-05-17 08:39:461334 return err;
1335 }
1336
[email protected]faa604e2009-09-25 22:38:591337 // The default handling is to assert on debug and to ignore on release.
[email protected]74cdede2013-09-25 05:39:571338 if (!ShouldIgnoreSqliteError(err))
[email protected]4350e322013-06-18 22:18:101339 DLOG(FATAL) << GetErrorMessage();
[email protected]faa604e2009-09-25 22:38:591340 return err;
1341}
1342
[email protected]579446c2013-12-16 18:36:521343bool Connection::FullIntegrityCheck(std::vector<std::string>* messages) {
1344 return IntegrityCheckHelper("PRAGMA integrity_check", messages);
1345}
1346
1347bool Connection::QuickIntegrityCheck() {
1348 std::vector<std::string> messages;
1349 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages))
1350 return false;
1351 return messages.size() == 1 && messages[0] == "ok";
1352}
1353
[email protected]80abf152013-05-22 12:42:421354// TODO(shess): Allow specifying maximum results (default 100 lines).
[email protected]579446c2013-12-16 18:36:521355bool Connection::IntegrityCheckHelper(
1356 const char* pragma_sql,
1357 std::vector<std::string>* messages) {
[email protected]80abf152013-05-22 12:42:421358 messages->clear();
1359
[email protected]4658e2a02013-06-06 23:05:001360 // This has the side effect of setting SQLITE_RecoveryMode, which
1361 // allows SQLite to process through certain cases of corruption.
1362 // Failing to set this pragma probably means that the database is
1363 // beyond recovery.
1364 const char kWritableSchema[] = "PRAGMA writable_schema = ON";
1365 if (!Execute(kWritableSchema))
1366 return false;
1367
1368 bool ret = false;
1369 {
[email protected]579446c2013-12-16 18:36:521370 sql::Statement stmt(GetUniqueStatement(pragma_sql));
[email protected]4658e2a02013-06-06 23:05:001371
1372 // The pragma appears to return all results (up to 100 by default)
1373 // as a single string. This doesn't appear to be an API contract,
1374 // it could return separate lines, so loop _and_ split.
1375 while (stmt.Step()) {
1376 std::string result(stmt.ColumnString(0));
brettw83dc1612015-08-12 07:31:181377 *messages = base::SplitString(result, "\n", base::TRIM_WHITESPACE,
1378 base::SPLIT_WANT_ALL);
[email protected]4658e2a02013-06-06 23:05:001379 }
1380 ret = stmt.Succeeded();
[email protected]80abf152013-05-22 12:42:421381 }
[email protected]4658e2a02013-06-06 23:05:001382
1383 // Best effort to put things back as they were before.
1384 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF";
1385 ignore_result(Execute(kNoWritableSchema));
1386
1387 return ret;
[email protected]80abf152013-05-22 12:42:421388}
1389
shess58b8df82015-06-03 00:19:321390base::TimeTicks TimeSource::Now() {
1391 return base::TimeTicks::Now();
1392}
1393
[email protected]e5ffd0e42009-09-11 21:30:561394} // namespace sql