blob: a7a599c0da6602402d1eaad8bababcfc1185cf16 [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
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
[email protected]348ac8f52013-05-21 03:27:0210#include "base/file_util.h"
[email protected]e5ffd0e42009-09-11 21:30:5611#include "base/logging.h"
[email protected]bd2ccdb4a2012-12-07 22:14:5012#include "base/metrics/histogram.h"
[email protected]210ce0af2013-05-15 09:10:3913#include "base/metrics/sparse_histogram.h"
[email protected]e5ffd0e42009-09-11 21:30:5614#include "base/string_util.h"
[email protected]f0a54b22011-07-19 18:40:2115#include "base/stringprintf.h"
[email protected]80abf152013-05-22 12:42:4216#include "base/strings/string_split.h"
[email protected]d55194ca2010-03-11 18:25:4517#include "base/utf_string_conversions.h"
[email protected]f0a54b22011-07-19 18:40:2118#include "sql/statement.h"
[email protected]e33cba42010-08-18 23:37:0319#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5620
[email protected]5b96f3772010-09-28 16:30:5721namespace {
22
23// Spin for up to a second waiting for the lock to clear when setting
24// up the database.
25// TODO(shess): Better story on this. http://crbug.com/56559
[email protected]c68ce172011-11-24 22:30:2726const int kBusyTimeoutSeconds = 1;
[email protected]5b96f3772010-09-28 16:30:5727
28class ScopedBusyTimeout {
29 public:
30 explicit ScopedBusyTimeout(sqlite3* db)
31 : db_(db) {
32 }
33 ~ScopedBusyTimeout() {
34 sqlite3_busy_timeout(db_, 0);
35 }
36
37 int SetTimeout(base::TimeDelta timeout) {
38 DCHECK_LT(timeout.InMilliseconds(), INT_MAX);
39 return sqlite3_busy_timeout(db_,
40 static_cast<int>(timeout.InMilliseconds()));
41 }
42
43 private:
44 sqlite3* db_;
45};
46
[email protected]6d42f152012-11-10 00:38:2447// Helper to "safely" enable writable_schema. No error checking
48// because it is reasonable to just forge ahead in case of an error.
49// If turning it on fails, then most likely nothing will work, whereas
50// if turning it off fails, it only matters if some code attempts to
51// continue working with the database and tries to modify the
52// sqlite_master table (none of our code does this).
53class ScopedWritableSchema {
54 public:
55 explicit ScopedWritableSchema(sqlite3* db)
56 : db_(db) {
57 sqlite3_exec(db_, "PRAGMA writable_schema=1", NULL, NULL, NULL);
58 }
59 ~ScopedWritableSchema() {
60 sqlite3_exec(db_, "PRAGMA writable_schema=0", NULL, NULL, NULL);
61 }
62
63 private:
64 sqlite3* db_;
65};
66
[email protected]5b96f3772010-09-28 16:30:5767} // namespace
68
[email protected]e5ffd0e42009-09-11 21:30:5669namespace sql {
70
71bool StatementID::operator<(const StatementID& other) const {
72 if (number_ != other.number_)
73 return number_ < other.number_;
74 return strcmp(str_, other.str_) < 0;
75}
76
[email protected]d4799a32010-09-28 22:54:5877ErrorDelegate::~ErrorDelegate() {
78}
79
[email protected]e5ffd0e42009-09-11 21:30:5680Connection::StatementRef::StatementRef(Connection* connection,
[email protected]41a97c812013-02-07 02:35:3881 sqlite3_stmt* stmt,
82 bool was_valid)
[email protected]e5ffd0e42009-09-11 21:30:5683 : connection_(connection),
[email protected]41a97c812013-02-07 02:35:3884 stmt_(stmt),
85 was_valid_(was_valid) {
86 if (connection)
87 connection_->StatementRefCreated(this);
[email protected]e5ffd0e42009-09-11 21:30:5688}
89
90Connection::StatementRef::~StatementRef() {
91 if (connection_)
92 connection_->StatementRefDeleted(this);
[email protected]41a97c812013-02-07 02:35:3893 Close(false);
[email protected]e5ffd0e42009-09-11 21:30:5694}
95
[email protected]41a97c812013-02-07 02:35:3896void Connection::StatementRef::Close(bool forced) {
[email protected]e5ffd0e42009-09-11 21:30:5697 if (stmt_) {
[email protected]35f7e5392012-07-27 19:54:5098 // Call to AssertIOAllowed() cannot go at the beginning of the function
99 // because Close() is called unconditionally from destructor to clean
100 // connection_. And if this is inactive statement this won't cause any
101 // disk access and destructor most probably will be called on thread
102 // not allowing disk access.
103 // TODO([email protected]): This should move to the beginning
104 // of the function. http://crbug.com/136655.
105 AssertIOAllowed();
[email protected]e5ffd0e42009-09-11 21:30:56106 sqlite3_finalize(stmt_);
107 stmt_ = NULL;
108 }
109 connection_ = NULL; // The connection may be getting deleted.
[email protected]41a97c812013-02-07 02:35:38110
111 // Forced close is expected to happen from a statement error
112 // handler. In that case maintain the sense of |was_valid_| which
113 // previously held for this ref.
114 was_valid_ = was_valid_ && forced;
[email protected]e5ffd0e42009-09-11 21:30:56115}
116
117Connection::Connection()
118 : db_(NULL),
119 page_size_(0),
120 cache_size_(0),
121 exclusive_locking_(false),
122 transaction_nesting_(0),
[email protected]35f7e5392012-07-27 19:54:50123 needs_rollback_(false),
[email protected]49dc4f22012-10-17 17:41:16124 in_memory_(false),
[email protected]41a97c812013-02-07 02:35:38125 poisoned_(false),
[email protected]49dc4f22012-10-17 17:41:16126 error_delegate_(NULL) {
[email protected]e5ffd0e42009-09-11 21:30:56127}
128
129Connection::~Connection() {
130 Close();
131}
132
[email protected]a3ef4832013-02-02 05:12:33133bool Connection::Open(const base::FilePath& path) {
[email protected]348ac8f52013-05-21 03:27:02134 if (!histogram_tag_.empty()) {
135 int64 size_64 = 0;
136 if (file_util::GetFileSize(path, &size_64)) {
137 size_t sample = static_cast<size_t>(size_64 / 1024);
138 std::string full_histogram_name = "Sqlite.SizeKB." + histogram_tag_;
139 base::HistogramBase* histogram =
140 base::Histogram::FactoryGet(
141 full_histogram_name, 1, 1000000, 50,
142 base::HistogramBase::kUmaTargetedHistogramFlag);
143 if (histogram)
144 histogram->Add(sample);
145 }
146 }
147
[email protected]e5ffd0e42009-09-11 21:30:56148#if defined(OS_WIN)
[email protected]765b44502009-10-02 05:01:42149 return OpenInternal(WideToUTF8(path.value()));
[email protected]e5ffd0e42009-09-11 21:30:56150#elif defined(OS_POSIX)
[email protected]765b44502009-10-02 05:01:42151 return OpenInternal(path.value());
[email protected]e5ffd0e42009-09-11 21:30:56152#endif
[email protected]765b44502009-10-02 05:01:42153}
[email protected]e5ffd0e42009-09-11 21:30:56154
[email protected]765b44502009-10-02 05:01:42155bool Connection::OpenInMemory() {
[email protected]35f7e5392012-07-27 19:54:50156 in_memory_ = true;
[email protected]765b44502009-10-02 05:01:42157 return OpenInternal(":memory:");
[email protected]e5ffd0e42009-09-11 21:30:56158}
159
[email protected]41a97c812013-02-07 02:35:38160void Connection::CloseInternal(bool forced) {
[email protected]4e179ba2012-03-17 16:06:47161 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
162 // will delete the -journal file. For ChromiumOS or other more
163 // embedded systems, this is probably not appropriate, whereas on
164 // desktop it might make some sense.
165
[email protected]4b350052012-02-24 20:40:48166 // sqlite3_close() needs all prepared statements to be finalized.
[email protected]4b350052012-02-24 20:40:48167
[email protected]41a97c812013-02-07 02:35:38168 // Release cached statements.
169 statement_cache_.clear();
170
171 // With cached statements released, in-use statements will remain.
172 // Closing the database while statements are in use is an API
173 // violation, except for forced close (which happens from within a
174 // statement's error handler).
175 DCHECK(forced || open_statements_.empty());
176
177 // Deactivate any outstanding statements so sqlite3_close() works.
178 for (StatementRefSet::iterator i = open_statements_.begin();
179 i != open_statements_.end(); ++i)
180 (*i)->Close(forced);
181 open_statements_.clear();
[email protected]4b350052012-02-24 20:40:48182
[email protected]e5ffd0e42009-09-11 21:30:56183 if (db_) {
[email protected]35f7e5392012-07-27 19:54:50184 // Call to AssertIOAllowed() cannot go at the beginning of the function
185 // because Close() must be called from destructor to clean
186 // statement_cache_, it won't cause any disk access and it most probably
187 // will happen on thread not allowing disk access.
188 // TODO([email protected]): This should move to the beginning
189 // of the function. http://crbug.com/136655.
190 AssertIOAllowed();
[email protected]4b350052012-02-24 20:40:48191 // TODO(shess): Histogram for failure.
[email protected]e5ffd0e42009-09-11 21:30:56192 sqlite3_close(db_);
193 db_ = NULL;
194 }
195}
196
[email protected]41a97c812013-02-07 02:35:38197void Connection::Close() {
198 // If the database was already closed by RazeAndClose(), then no
199 // need to close again. Clear the |poisoned_| bit so that incorrect
200 // API calls are caught.
201 if (poisoned_) {
202 poisoned_ = false;
203 return;
204 }
205
206 CloseInternal(false);
207}
208
[email protected]e5ffd0e42009-09-11 21:30:56209void Connection::Preload() {
[email protected]35f7e5392012-07-27 19:54:50210 AssertIOAllowed();
211
[email protected]e5ffd0e42009-09-11 21:30:56212 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38213 DLOG_IF(FATAL, !poisoned_) << "Cannot preload null db";
[email protected]e5ffd0e42009-09-11 21:30:56214 return;
215 }
216
217 // A statement must be open for the preload command to work. If the meta
218 // table doesn't exist, it probably means this is a new database and there
219 // is nothing to preload (so it's OK we do nothing).
220 if (!DoesTableExist("meta"))
221 return;
222 Statement dummy(GetUniqueStatement("SELECT * FROM meta"));
[email protected]eff1fa522011-12-12 23:50:59223 if (!dummy.Step())
[email protected]e5ffd0e42009-09-11 21:30:56224 return;
225
[email protected]4176eee4b2011-01-26 14:33:32226#if !defined(USE_SYSTEM_SQLITE)
227 // This function is only defined in Chromium's version of sqlite.
228 // Do not call it when using system sqlite.
[email protected]67361b32011-04-12 20:13:06229 sqlite3_preload(db_);
[email protected]4176eee4b2011-01-26 14:33:32230#endif
[email protected]e5ffd0e42009-09-11 21:30:56231}
232
[email protected]8e0c01282012-04-06 19:36:49233// Create an in-memory database with the existing database's page
234// size, then backup that database over the existing database.
235bool Connection::Raze() {
[email protected]35f7e5392012-07-27 19:54:50236 AssertIOAllowed();
237
[email protected]8e0c01282012-04-06 19:36:49238 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38239 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:49240 return false;
241 }
242
243 if (transaction_nesting_ > 0) {
244 DLOG(FATAL) << "Cannot raze within a transaction";
245 return false;
246 }
247
248 sql::Connection null_db;
249 if (!null_db.OpenInMemory()) {
250 DLOG(FATAL) << "Unable to open in-memory database.";
251 return false;
252 }
253
[email protected]6d42f152012-11-10 00:38:24254 if (page_size_) {
255 // Enforce SQLite restrictions on |page_size_|.
256 DCHECK(!(page_size_ & (page_size_ - 1)))
257 << " page_size_ " << page_size_ << " is not a power of two.";
258 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
259 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:04260 const std::string sql =
261 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]69c58452012-08-06 19:22:42262 if (!null_db.Execute(sql.c_str()))
263 return false;
264 }
265
[email protected]6d42f152012-11-10 00:38:24266#if defined(OS_ANDROID)
267 // Android compiles with SQLITE_DEFAULT_AUTOVACUUM. Unfortunately,
268 // in-memory databases do not respect this define.
269 // TODO(shess): Figure out a way to set this without using platform
270 // specific code. AFAICT from sqlite3.c, the only way to do it
271 // would be to create an actual filesystem database, which is
272 // unfortunate.
273 if (!null_db.Execute("PRAGMA auto_vacuum = 1"))
274 return false;
275#endif
[email protected]8e0c01282012-04-06 19:36:49276
277 // The page size doesn't take effect until a database has pages, and
278 // at this point the null database has none. Changing the schema
279 // version will create the first page. This will not affect the
280 // schema version in the resulting database, as SQLite's backup
281 // implementation propagates the schema version from the original
282 // connection to the new version of the database, incremented by one
283 // so that other readers see the schema change and act accordingly.
284 if (!null_db.Execute("PRAGMA schema_version = 1"))
285 return false;
286
[email protected]6d42f152012-11-10 00:38:24287 // SQLite tracks the expected number of database pages in the first
288 // page, and if it does not match the total retrieved from a
289 // filesystem call, treats the database as corrupt. This situation
290 // breaks almost all SQLite calls. "PRAGMA writable_schema" can be
291 // used to hint to SQLite to soldier on in that case, specifically
292 // for purposes of recovery. [See SQLITE_CORRUPT_BKPT case in
293 // sqlite3.c lockBtree().]
294 // TODO(shess): With this, "PRAGMA auto_vacuum" and "PRAGMA
295 // page_size" can be used to query such a database.
296 ScopedWritableSchema writable_schema(db_);
297
[email protected]8e0c01282012-04-06 19:36:49298 sqlite3_backup* backup = sqlite3_backup_init(db_, "main",
299 null_db.db_, "main");
300 if (!backup) {
301 DLOG(FATAL) << "Unable to start sqlite3_backup().";
302 return false;
303 }
304
305 // -1 backs up the entire database.
306 int rc = sqlite3_backup_step(backup, -1);
307 int pages = sqlite3_backup_pagecount(backup);
308 sqlite3_backup_finish(backup);
309
310 // The destination database was locked.
311 if (rc == SQLITE_BUSY) {
312 return false;
313 }
314
315 // The entire database should have been backed up.
316 if (rc != SQLITE_DONE) {
317 DLOG(FATAL) << "Unable to copy entire null database.";
318 return false;
319 }
320
321 // Exactly one page should have been backed up. If this breaks,
322 // check this function to make sure assumptions aren't being broken.
323 DCHECK_EQ(pages, 1);
324
325 return true;
326}
327
328bool Connection::RazeWithTimout(base::TimeDelta timeout) {
329 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38330 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
[email protected]8e0c01282012-04-06 19:36:49331 return false;
332 }
333
334 ScopedBusyTimeout busy_timeout(db_);
335 busy_timeout.SetTimeout(timeout);
336 return Raze();
337}
338
[email protected]41a97c812013-02-07 02:35:38339bool Connection::RazeAndClose() {
340 if (!db_) {
341 DLOG_IF(FATAL, !poisoned_) << "Cannot raze null db";
342 return false;
343 }
344
345 // Raze() cannot run in a transaction.
346 while (transaction_nesting_) {
347 RollbackTransaction();
348 }
349
350 bool result = Raze();
351
352 CloseInternal(true);
353
354 // Mark the database so that future API calls fail appropriately,
355 // but don't DCHECK (because after calling this function they are
356 // expected to fail).
357 poisoned_ = true;
358
359 return result;
360}
361
[email protected]e5ffd0e42009-09-11 21:30:56362bool Connection::BeginTransaction() {
363 if (needs_rollback_) {
[email protected]88563f62011-03-13 22:13:33364 DCHECK_GT(transaction_nesting_, 0);
[email protected]e5ffd0e42009-09-11 21:30:56365
366 // When we're going to rollback, fail on this begin and don't actually
367 // mark us as entering the nested transaction.
368 return false;
369 }
370
371 bool success = true;
372 if (!transaction_nesting_) {
373 needs_rollback_ = false;
374
375 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
[email protected]eff1fa522011-12-12 23:50:59376 if (!begin.Run())
[email protected]e5ffd0e42009-09-11 21:30:56377 return false;
378 }
379 transaction_nesting_++;
380 return success;
381}
382
383void Connection::RollbackTransaction() {
384 if (!transaction_nesting_) {
[email protected]41a97c812013-02-07 02:35:38385 DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:56386 return;
387 }
388
389 transaction_nesting_--;
390
391 if (transaction_nesting_ > 0) {
392 // Mark the outermost transaction as needing rollback.
393 needs_rollback_ = true;
394 return;
395 }
396
397 DoRollback();
398}
399
400bool Connection::CommitTransaction() {
401 if (!transaction_nesting_) {
[email protected]41a97c812013-02-07 02:35:38402 DLOG_IF(FATAL, !poisoned_) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:56403 return false;
404 }
405 transaction_nesting_--;
406
407 if (transaction_nesting_ > 0) {
408 // Mark any nested transactions as failing after we've already got one.
409 return !needs_rollback_;
410 }
411
412 if (needs_rollback_) {
413 DoRollback();
414 return false;
415 }
416
417 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
[email protected]e5ffd0e42009-09-11 21:30:56418 return commit.Run();
419}
420
[email protected]eff1fa522011-12-12 23:50:59421int Connection::ExecuteAndReturnErrorCode(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:50422 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:38423 if (!db_) {
424 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
425 return SQLITE_ERROR;
426 }
[email protected]eff1fa522011-12-12 23:50:59427 return sqlite3_exec(db_, sql, NULL, NULL, NULL);
428}
429
430bool Connection::Execute(const char* sql) {
[email protected]41a97c812013-02-07 02:35:38431 if (!db_) {
432 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
433 return false;
434 }
435
[email protected]eff1fa522011-12-12 23:50:59436 int error = ExecuteAndReturnErrorCode(sql);
[email protected]473ad792012-11-10 00:55:00437 if (error != SQLITE_OK)
438 error = OnSqliteError(error, NULL);
439
[email protected]28fe0ff2012-02-25 00:40:33440 // This needs to be a FATAL log because the error case of arriving here is
441 // that there's a malformed SQL statement. This can arise in development if
442 // a change alters the schema but not all queries adjust.
[email protected]eff1fa522011-12-12 23:50:59443 if (error == SQLITE_ERROR)
[email protected]28fe0ff2012-02-25 00:40:33444 DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage();
[email protected]eff1fa522011-12-12 23:50:59445 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:56446}
447
[email protected]5b96f3772010-09-28 16:30:57448bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
[email protected]41a97c812013-02-07 02:35:38449 if (!db_) {
450 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]5b96f3772010-09-28 16:30:57451 return false;
[email protected]41a97c812013-02-07 02:35:38452 }
[email protected]5b96f3772010-09-28 16:30:57453
454 ScopedBusyTimeout busy_timeout(db_);
455 busy_timeout.SetTimeout(timeout);
[email protected]eff1fa522011-12-12 23:50:59456 return Execute(sql);
[email protected]5b96f3772010-09-28 16:30:57457}
458
[email protected]e5ffd0e42009-09-11 21:30:56459bool Connection::HasCachedStatement(const StatementID& id) const {
460 return statement_cache_.find(id) != statement_cache_.end();
461}
462
463scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement(
464 const StatementID& id,
465 const char* sql) {
466 CachedStatementMap::iterator i = statement_cache_.find(id);
467 if (i != statement_cache_.end()) {
468 // Statement is in the cache. It should still be active (we're the only
469 // one invalidating cached statements, and we'll remove it from the cache
470 // if we do that. Make sure we reset it before giving out the cached one in
471 // case it still has some stuff bound.
472 DCHECK(i->second->is_valid());
473 sqlite3_reset(i->second->stmt());
474 return i->second;
475 }
476
477 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
478 if (statement->is_valid())
479 statement_cache_[id] = statement; // Only cache valid statements.
480 return statement;
481}
482
483scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement(
484 const char* sql) {
[email protected]35f7e5392012-07-27 19:54:50485 AssertIOAllowed();
486
[email protected]41a97c812013-02-07 02:35:38487 // Return inactive statement.
[email protected]e5ffd0e42009-09-11 21:30:56488 if (!db_)
[email protected]41a97c812013-02-07 02:35:38489 return new StatementRef(NULL, NULL, poisoned_);
[email protected]e5ffd0e42009-09-11 21:30:56490
491 sqlite3_stmt* stmt = NULL;
[email protected]473ad792012-11-10 00:55:00492 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
493 if (rc != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:59494 // This is evidence of a syntax error in the incoming SQL.
495 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]473ad792012-11-10 00:55:00496
497 // It could also be database corruption.
498 OnSqliteError(rc, NULL);
[email protected]41a97c812013-02-07 02:35:38499 return new StatementRef(NULL, NULL, false);
[email protected]e5ffd0e42009-09-11 21:30:56500 }
[email protected]41a97c812013-02-07 02:35:38501 return new StatementRef(this, stmt, true);
[email protected]e5ffd0e42009-09-11 21:30:56502}
503
[email protected]2eec0a22012-07-24 01:59:58504scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
505 const char* sql) const {
[email protected]41a97c812013-02-07 02:35:38506 // Return inactive statement.
[email protected]2eec0a22012-07-24 01:59:58507 if (!db_)
[email protected]41a97c812013-02-07 02:35:38508 return new StatementRef(NULL, NULL, poisoned_);
[email protected]2eec0a22012-07-24 01:59:58509
510 sqlite3_stmt* stmt = NULL;
511 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
512 if (rc != SQLITE_OK) {
513 // This is evidence of a syntax error in the incoming SQL.
514 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]41a97c812013-02-07 02:35:38515 return new StatementRef(NULL, NULL, false);
[email protected]2eec0a22012-07-24 01:59:58516 }
[email protected]41a97c812013-02-07 02:35:38517 return new StatementRef(NULL, stmt, true);
[email protected]2eec0a22012-07-24 01:59:58518}
519
[email protected]eff1fa522011-12-12 23:50:59520bool Connection::IsSQLValid(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:50521 AssertIOAllowed();
[email protected]41a97c812013-02-07 02:35:38522 if (!db_) {
523 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
524 return false;
525 }
526
[email protected]eff1fa522011-12-12 23:50:59527 sqlite3_stmt* stmt = NULL;
528 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK)
529 return false;
530
531 sqlite3_finalize(stmt);
532 return true;
533}
534
[email protected]1ed78a32009-09-15 20:24:17535bool Connection::DoesTableExist(const char* table_name) const {
[email protected]e2cadec82011-12-13 02:00:53536 return DoesTableOrIndexExist(table_name, "table");
537}
538
539bool Connection::DoesIndexExist(const char* index_name) const {
540 return DoesTableOrIndexExist(index_name, "index");
541}
542
543bool Connection::DoesTableOrIndexExist(
544 const char* name, const char* type) const {
[email protected]2eec0a22012-07-24 01:59:58545 const char* kSql = "SELECT name FROM sqlite_master WHERE type=? AND name=?";
546 Statement statement(GetUntrackedStatement(kSql));
[email protected]e2cadec82011-12-13 02:00:53547 statement.BindString(0, type);
548 statement.BindString(1, name);
[email protected]28fe0ff2012-02-25 00:40:33549
[email protected]e5ffd0e42009-09-11 21:30:56550 return statement.Step(); // Table exists if any row was returned.
551}
552
553bool Connection::DoesColumnExist(const char* table_name,
[email protected]1ed78a32009-09-15 20:24:17554 const char* column_name) const {
[email protected]e5ffd0e42009-09-11 21:30:56555 std::string sql("PRAGMA TABLE_INFO(");
556 sql.append(table_name);
557 sql.append(")");
558
[email protected]2eec0a22012-07-24 01:59:58559 Statement statement(GetUntrackedStatement(sql.c_str()));
[email protected]e5ffd0e42009-09-11 21:30:56560 while (statement.Step()) {
561 if (!statement.ColumnString(1).compare(column_name))
562 return true;
563 }
564 return false;
565}
566
567int64 Connection::GetLastInsertRowId() const {
568 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38569 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]e5ffd0e42009-09-11 21:30:56570 return 0;
571 }
572 return sqlite3_last_insert_rowid(db_);
573}
574
[email protected]1ed78a32009-09-15 20:24:17575int Connection::GetLastChangeCount() const {
576 if (!db_) {
[email protected]41a97c812013-02-07 02:35:38577 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
[email protected]1ed78a32009-09-15 20:24:17578 return 0;
579 }
580 return sqlite3_changes(db_);
581}
582
[email protected]e5ffd0e42009-09-11 21:30:56583int Connection::GetErrorCode() const {
584 if (!db_)
585 return SQLITE_ERROR;
586 return sqlite3_errcode(db_);
587}
588
[email protected]767718e52010-09-21 23:18:49589int Connection::GetLastErrno() const {
590 if (!db_)
591 return -1;
592
593 int err = 0;
594 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err))
595 return -2;
596
597 return err;
598}
599
[email protected]e5ffd0e42009-09-11 21:30:56600const char* Connection::GetErrorMessage() const {
601 if (!db_)
602 return "sql::Connection has no connection.";
603 return sqlite3_errmsg(db_);
604}
605
[email protected]765b44502009-10-02 05:01:42606bool Connection::OpenInternal(const std::string& file_name) {
[email protected]35f7e5392012-07-27 19:54:50607 AssertIOAllowed();
608
[email protected]9cfbc922009-11-17 20:13:17609 if (db_) {
[email protected]eff1fa522011-12-12 23:50:59610 DLOG(FATAL) << "sql::Connection is already open.";
[email protected]9cfbc922009-11-17 20:13:17611 return false;
612 }
613
[email protected]41a97c812013-02-07 02:35:38614 // If |poisoned_| is set, it means an error handler called
615 // RazeAndClose(). Until regular Close() is called, the caller
616 // should be treating the database as open, but is_open() currently
617 // only considers the sqlite3 handle's state.
618 // TODO(shess): Revise is_open() to consider poisoned_, and review
619 // to see if any non-testing code even depends on it.
620 DLOG_IF(FATAL, poisoned_) << "sql::Connection is already open.";
621
[email protected]765b44502009-10-02 05:01:42622 int err = sqlite3_open(file_name.c_str(), &db_);
623 if (err != SQLITE_OK) {
[email protected]bd2ccdb4a2012-12-07 22:14:50624 // Histogram failures specific to initial open for debugging
625 // purposes.
626 UMA_HISTOGRAM_ENUMERATION("Sqlite.OpenFailure", err & 0xff, 50);
627
[email protected]765b44502009-10-02 05:01:42628 OnSqliteError(err, NULL);
[email protected]64021042012-02-10 20:02:29629 Close();
[email protected]765b44502009-10-02 05:01:42630 db_ = NULL;
631 return false;
632 }
633
[email protected]affa2da2013-06-06 22:20:34634 // SQLite uses a lookaside buffer to improve performance of small mallocs.
635 // Chromium already depends on small mallocs being efficient, so we disable
636 // this to avoid the extra memory overhead.
637 // This must be called immediatly after opening the database before any SQL
638 // statements are run.
639 sqlite3_db_config(db_, SQLITE_DBCONFIG_LOOKASIDE, NULL, 0, 0);
640
[email protected]bd2ccdb4a2012-12-07 22:14:50641 // sqlite3_open() does not actually read the database file (unless a
642 // hot journal is found). Successfully executing this pragma on an
643 // existing database requires a valid header on page 1.
644 // TODO(shess): For now, just probing to see what the lay of the
645 // land is. If it's mostly SQLITE_NOTADB, then the database should
646 // be razed.
647 err = ExecuteAndReturnErrorCode("PRAGMA auto_vacuum");
648 if (err != SQLITE_OK)
649 UMA_HISTOGRAM_ENUMERATION("Sqlite.OpenProbeFailure", err & 0xff, 50);
650
[email protected]658f8332010-09-18 04:40:43651 // Enable extended result codes to provide more color on I/O errors.
652 // Not having extended result codes is not a fatal problem, as
653 // Chromium code does not attempt to handle I/O errors anyhow. The
654 // current implementation always returns SQLITE_OK, the DCHECK is to
655 // quickly notify someone if SQLite changes.
656 err = sqlite3_extended_result_codes(db_, 1);
657 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
658
[email protected]5b96f3772010-09-28 16:30:57659 // If indicated, lock up the database before doing anything else, so
660 // that the following code doesn't have to deal with locking.
661 // TODO(shess): This code is brittle. Find the cases where code
662 // doesn't request |exclusive_locking_| and audit that it does the
663 // right thing with SQLITE_BUSY, and that it doesn't make
664 // assumptions about who might change things in the database.
665 // http://crbug.com/56559
666 if (exclusive_locking_) {
667 // TODO(shess): This should probably be a full CHECK(). Code
668 // which requests exclusive locking but doesn't get it is almost
669 // certain to be ill-tested.
670 if (!Execute("PRAGMA locking_mode=EXCLUSIVE"))
[email protected]eff1fa522011-12-12 23:50:59671 DLOG(FATAL) << "Could not set locking mode: " << GetErrorMessage();
[email protected]5b96f3772010-09-28 16:30:57672 }
673
[email protected]4e179ba2012-03-17 16:06:47674 // http://www.sqlite.org/pragma.html#pragma_journal_mode
675 // DELETE (default) - delete -journal file to commit.
676 // TRUNCATE - truncate -journal file to commit.
677 // PERSIST - zero out header of -journal file to commit.
678 // journal_size_limit provides size to trim to in PERSIST.
679 // TODO(shess): Figure out if PERSIST and journal_size_limit really
680 // matter. In theory, it keeps pages pre-allocated, so if
681 // transactions usually fit, it should be faster.
682 ignore_result(Execute("PRAGMA journal_mode = PERSIST"));
683 ignore_result(Execute("PRAGMA journal_size_limit = 16384"));
684
[email protected]c68ce172011-11-24 22:30:27685 const base::TimeDelta kBusyTimeout =
686 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
687
[email protected]765b44502009-10-02 05:01:42688 if (page_size_ != 0) {
[email protected]5b96f3772010-09-28 16:30:57689 // Enforce SQLite restrictions on |page_size_|.
690 DCHECK(!(page_size_ & (page_size_ - 1)))
691 << " page_size_ " << page_size_ << " is not a power of two.";
[email protected]6d42f152012-11-10 00:38:24692 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
[email protected]5b96f3772010-09-28 16:30:57693 DCHECK_LE(page_size_, kSqliteMaxPageSize);
[email protected]7d3cbc92013-03-18 22:33:04694 const std::string sql =
695 base::StringPrintf("PRAGMA page_size=%d", page_size_);
[email protected]5b96f3772010-09-28 16:30:57696 if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout))
[email protected]eff1fa522011-12-12 23:50:59697 DLOG(FATAL) << "Could not set page size: " << GetErrorMessage();
[email protected]765b44502009-10-02 05:01:42698 }
699
700 if (cache_size_ != 0) {
[email protected]7d3cbc92013-03-18 22:33:04701 const std::string sql =
702 base::StringPrintf("PRAGMA cache_size=%d", cache_size_);
[email protected]5b96f3772010-09-28 16:30:57703 if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout))
[email protected]eff1fa522011-12-12 23:50:59704 DLOG(FATAL) << "Could not set cache size: " << GetErrorMessage();
[email protected]765b44502009-10-02 05:01:42705 }
706
[email protected]6e0b1442011-08-09 23:23:58707 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) {
[email protected]eff1fa522011-12-12 23:50:59708 DLOG(FATAL) << "Could not enable secure_delete: " << GetErrorMessage();
[email protected]6e0b1442011-08-09 23:23:58709 Close();
710 return false;
711 }
712
[email protected]765b44502009-10-02 05:01:42713 return true;
714}
715
[email protected]e5ffd0e42009-09-11 21:30:56716void Connection::DoRollback() {
717 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
[email protected]eff1fa522011-12-12 23:50:59718 rollback.Run();
[email protected]44ad7d902012-03-23 00:09:05719 needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:56720}
721
722void Connection::StatementRefCreated(StatementRef* ref) {
723 DCHECK(open_statements_.find(ref) == open_statements_.end());
724 open_statements_.insert(ref);
725}
726
727void Connection::StatementRefDeleted(StatementRef* ref) {
728 StatementRefSet::iterator i = open_statements_.find(ref);
729 if (i == open_statements_.end())
[email protected]eff1fa522011-12-12 23:50:59730 DLOG(FATAL) << "Could not find statement";
[email protected]e5ffd0e42009-09-11 21:30:56731 else
732 open_statements_.erase(i);
733}
734
[email protected]210ce0af2013-05-15 09:10:39735void Connection::AddTaggedHistogram(const std::string& name,
736 size_t sample) const {
737 if (histogram_tag_.empty())
738 return;
739
740 // TODO(shess): The histogram macros create a bit of static storage
741 // for caching the histogram object. This code shouldn't execute
742 // often enough for such caching to be crucial. If it becomes an
743 // issue, the object could be cached alongside histogram_prefix_.
744 std::string full_histogram_name = name + "." + histogram_tag_;
745 base::HistogramBase* histogram =
746 base::SparseHistogram::FactoryGet(
747 full_histogram_name,
748 base::HistogramBase::kUmaTargetedHistogramFlag);
749 if (histogram)
750 histogram->Add(sample);
751}
752
[email protected]faa604e2009-09-25 22:38:59753int Connection::OnSqliteError(int err, sql::Statement *stmt) {
[email protected]210ce0af2013-05-15 09:10:39754 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err);
755 AddTaggedHistogram("Sqlite.Error", err);
[email protected]c088e3a32013-01-03 23:59:14756
757 // Always log the error.
758 LOG(ERROR) << "sqlite error " << err
759 << ", errno " << GetLastErrno()
760 << ": " << GetErrorMessage();
761
[email protected]c3881b372013-05-17 08:39:46762 if (!error_callback_.is_null()) {
763 error_callback_.Run(err, stmt);
764 return err;
765 }
766
767 // TODO(shess): Remove |error_delegate_| once everything is
768 // converted to |error_callback_|.
[email protected]faa604e2009-09-25 22:38:59769 if (error_delegate_.get())
770 return error_delegate_->OnError(err, this, stmt);
[email protected]c088e3a32013-01-03 23:59:14771
[email protected]faa604e2009-09-25 22:38:59772 // The default handling is to assert on debug and to ignore on release.
[email protected]eff1fa522011-12-12 23:50:59773 DLOG(FATAL) << GetErrorMessage();
[email protected]faa604e2009-09-25 22:38:59774 return err;
775}
776
[email protected]80abf152013-05-22 12:42:42777// TODO(shess): Allow specifying integrity_check versus quick_check.
778// TODO(shess): Allow specifying maximum results (default 100 lines).
779bool Connection::IntegrityCheck(std::vector<std::string>* messages) {
[email protected]80abf152013-05-22 12:42:42780 messages->clear();
781
[email protected]4658e2a02013-06-06 23:05:00782 // This has the side effect of setting SQLITE_RecoveryMode, which
783 // allows SQLite to process through certain cases of corruption.
784 // Failing to set this pragma probably means that the database is
785 // beyond recovery.
786 const char kWritableSchema[] = "PRAGMA writable_schema = ON";
787 if (!Execute(kWritableSchema))
788 return false;
789
790 bool ret = false;
791 {
792 const char kSql[] = "PRAGMA integrity_check";
793 sql::Statement stmt(GetUniqueStatement(kSql));
794
795 // The pragma appears to return all results (up to 100 by default)
796 // as a single string. This doesn't appear to be an API contract,
797 // it could return separate lines, so loop _and_ split.
798 while (stmt.Step()) {
799 std::string result(stmt.ColumnString(0));
800 base::SplitString(result, '\n', messages);
801 }
802 ret = stmt.Succeeded();
[email protected]80abf152013-05-22 12:42:42803 }
[email protected]4658e2a02013-06-06 23:05:00804
805 // Best effort to put things back as they were before.
806 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF";
807 ignore_result(Execute(kNoWritableSchema));
808
809 return ret;
[email protected]80abf152013-05-22 12:42:42810}
811
[email protected]e5ffd0e42009-09-11 21:30:56812} // namespace sql