blob: c7666f9a77542180cdf96143f65547b25b33c0bf [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]e5ffd0e42009-09-11 21:30:569#include "base/file_path.h"
10#include "base/logging.h"
11#include "base/string_util.h"
[email protected]f0a54b22011-07-19 18:40:2112#include "base/stringprintf.h"
[email protected]d55194ca2010-03-11 18:25:4513#include "base/utf_string_conversions.h"
[email protected]f0a54b22011-07-19 18:40:2114#include "sql/statement.h"
[email protected]e33cba42010-08-18 23:37:0315#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5616
[email protected]5b96f3772010-09-28 16:30:5717namespace {
18
19// Spin for up to a second waiting for the lock to clear when setting
20// up the database.
21// TODO(shess): Better story on this. http://crbug.com/56559
[email protected]c68ce172011-11-24 22:30:2722const int kBusyTimeoutSeconds = 1;
[email protected]5b96f3772010-09-28 16:30:5723
24class ScopedBusyTimeout {
25 public:
26 explicit ScopedBusyTimeout(sqlite3* db)
27 : db_(db) {
28 }
29 ~ScopedBusyTimeout() {
30 sqlite3_busy_timeout(db_, 0);
31 }
32
33 int SetTimeout(base::TimeDelta timeout) {
34 DCHECK_LT(timeout.InMilliseconds(), INT_MAX);
35 return sqlite3_busy_timeout(db_,
36 static_cast<int>(timeout.InMilliseconds()));
37 }
38
39 private:
40 sqlite3* db_;
41};
42
43} // namespace
44
[email protected]e5ffd0e42009-09-11 21:30:5645namespace sql {
46
47bool StatementID::operator<(const StatementID& other) const {
48 if (number_ != other.number_)
49 return number_ < other.number_;
50 return strcmp(str_, other.str_) < 0;
51}
52
[email protected]d4799a32010-09-28 22:54:5853ErrorDelegate::ErrorDelegate() {
54}
55
56ErrorDelegate::~ErrorDelegate() {
57}
58
[email protected]e5ffd0e42009-09-11 21:30:5659Connection::StatementRef::StatementRef()
60 : connection_(NULL),
61 stmt_(NULL) {
62}
63
[email protected]2eec0a22012-07-24 01:59:5864Connection::StatementRef::StatementRef(sqlite3_stmt* stmt)
65 : connection_(NULL),
66 stmt_(stmt) {
67}
68
[email protected]e5ffd0e42009-09-11 21:30:5669Connection::StatementRef::StatementRef(Connection* connection,
70 sqlite3_stmt* stmt)
71 : connection_(connection),
72 stmt_(stmt) {
73 connection_->StatementRefCreated(this);
74}
75
76Connection::StatementRef::~StatementRef() {
77 if (connection_)
78 connection_->StatementRefDeleted(this);
79 Close();
80}
81
82void Connection::StatementRef::Close() {
83 if (stmt_) {
[email protected]35f7e5392012-07-27 19:54:5084 // Call to AssertIOAllowed() cannot go at the beginning of the function
85 // because Close() is called unconditionally from destructor to clean
86 // connection_. And if this is inactive statement this won't cause any
87 // disk access and destructor most probably will be called on thread
88 // not allowing disk access.
89 // TODO([email protected]): This should move to the beginning
90 // of the function. http://crbug.com/136655.
91 AssertIOAllowed();
[email protected]e5ffd0e42009-09-11 21:30:5692 sqlite3_finalize(stmt_);
93 stmt_ = NULL;
94 }
95 connection_ = NULL; // The connection may be getting deleted.
96}
97
98Connection::Connection()
99 : db_(NULL),
100 page_size_(0),
101 cache_size_(0),
102 exclusive_locking_(false),
103 transaction_nesting_(0),
[email protected]35f7e5392012-07-27 19:54:50104 needs_rollback_(false),
105 in_memory_(false) {
[email protected]e5ffd0e42009-09-11 21:30:56106}
107
108Connection::~Connection() {
109 Close();
110}
111
[email protected]765b44502009-10-02 05:01:42112bool Connection::Open(const FilePath& path) {
[email protected]e5ffd0e42009-09-11 21:30:56113#if defined(OS_WIN)
[email protected]765b44502009-10-02 05:01:42114 return OpenInternal(WideToUTF8(path.value()));
[email protected]e5ffd0e42009-09-11 21:30:56115#elif defined(OS_POSIX)
[email protected]765b44502009-10-02 05:01:42116 return OpenInternal(path.value());
[email protected]e5ffd0e42009-09-11 21:30:56117#endif
[email protected]765b44502009-10-02 05:01:42118}
[email protected]e5ffd0e42009-09-11 21:30:56119
[email protected]765b44502009-10-02 05:01:42120bool Connection::OpenInMemory() {
[email protected]35f7e5392012-07-27 19:54:50121 in_memory_ = true;
[email protected]765b44502009-10-02 05:01:42122 return OpenInternal(":memory:");
[email protected]e5ffd0e42009-09-11 21:30:56123}
124
125void Connection::Close() {
[email protected]4e179ba2012-03-17 16:06:47126 // TODO(shess): Calling "PRAGMA journal_mode = DELETE" at this point
127 // will delete the -journal file. For ChromiumOS or other more
128 // embedded systems, this is probably not appropriate, whereas on
129 // desktop it might make some sense.
130
[email protected]4b350052012-02-24 20:40:48131 // sqlite3_close() needs all prepared statements to be finalized.
132 // Release all cached statements, then assert that the client has
133 // released all statements.
[email protected]e5ffd0e42009-09-11 21:30:56134 statement_cache_.clear();
135 DCHECK(open_statements_.empty());
[email protected]4b350052012-02-24 20:40:48136
137 // Additionally clear the prepared statements, because they contain
138 // weak references to this connection. This case has come up when
139 // error-handling code is hit in production.
140 ClearCache();
141
[email protected]e5ffd0e42009-09-11 21:30:56142 if (db_) {
[email protected]35f7e5392012-07-27 19:54:50143 // Call to AssertIOAllowed() cannot go at the beginning of the function
144 // because Close() must be called from destructor to clean
145 // statement_cache_, it won't cause any disk access and it most probably
146 // will happen on thread not allowing disk access.
147 // TODO([email protected]): This should move to the beginning
148 // of the function. http://crbug.com/136655.
149 AssertIOAllowed();
[email protected]4b350052012-02-24 20:40:48150 // TODO(shess): Histogram for failure.
[email protected]e5ffd0e42009-09-11 21:30:56151 sqlite3_close(db_);
152 db_ = NULL;
153 }
154}
155
156void Connection::Preload() {
[email protected]35f7e5392012-07-27 19:54:50157 AssertIOAllowed();
158
[email protected]e5ffd0e42009-09-11 21:30:56159 if (!db_) {
[email protected]eff1fa522011-12-12 23:50:59160 DLOG(FATAL) << "Cannot preload null db";
[email protected]e5ffd0e42009-09-11 21:30:56161 return;
162 }
163
164 // A statement must be open for the preload command to work. If the meta
165 // table doesn't exist, it probably means this is a new database and there
166 // is nothing to preload (so it's OK we do nothing).
167 if (!DoesTableExist("meta"))
168 return;
169 Statement dummy(GetUniqueStatement("SELECT * FROM meta"));
[email protected]eff1fa522011-12-12 23:50:59170 if (!dummy.Step())
[email protected]e5ffd0e42009-09-11 21:30:56171 return;
172
[email protected]4176eee4b2011-01-26 14:33:32173#if !defined(USE_SYSTEM_SQLITE)
174 // This function is only defined in Chromium's version of sqlite.
175 // Do not call it when using system sqlite.
[email protected]67361b32011-04-12 20:13:06176 sqlite3_preload(db_);
[email protected]4176eee4b2011-01-26 14:33:32177#endif
[email protected]e5ffd0e42009-09-11 21:30:56178}
179
[email protected]8e0c01282012-04-06 19:36:49180// Create an in-memory database with the existing database's page
181// size, then backup that database over the existing database.
182bool Connection::Raze() {
[email protected]35f7e5392012-07-27 19:54:50183 AssertIOAllowed();
184
[email protected]8e0c01282012-04-06 19:36:49185 if (!db_) {
186 DLOG(FATAL) << "Cannot raze null db";
187 return false;
188 }
189
190 if (transaction_nesting_ > 0) {
191 DLOG(FATAL) << "Cannot raze within a transaction";
192 return false;
193 }
194
195 sql::Connection null_db;
196 if (!null_db.OpenInMemory()) {
197 DLOG(FATAL) << "Unable to open in-memory database.";
198 return false;
199 }
200
201 // Get the page size from the current connection, then propagate it
202 // to the null database.
203 Statement s(GetUniqueStatement("PRAGMA page_size"));
204 if (!s.Step())
205 return false;
206 const std::string sql = StringPrintf("PRAGMA page_size=%d", s.ColumnInt(0));
207 if (!null_db.Execute(sql.c_str()))
208 return false;
209
210 // The page size doesn't take effect until a database has pages, and
211 // at this point the null database has none. Changing the schema
212 // version will create the first page. This will not affect the
213 // schema version in the resulting database, as SQLite's backup
214 // implementation propagates the schema version from the original
215 // connection to the new version of the database, incremented by one
216 // so that other readers see the schema change and act accordingly.
217 if (!null_db.Execute("PRAGMA schema_version = 1"))
218 return false;
219
220 sqlite3_backup* backup = sqlite3_backup_init(db_, "main",
221 null_db.db_, "main");
222 if (!backup) {
223 DLOG(FATAL) << "Unable to start sqlite3_backup().";
224 return false;
225 }
226
227 // -1 backs up the entire database.
228 int rc = sqlite3_backup_step(backup, -1);
229 int pages = sqlite3_backup_pagecount(backup);
230 sqlite3_backup_finish(backup);
231
232 // The destination database was locked.
233 if (rc == SQLITE_BUSY) {
234 return false;
235 }
236
237 // The entire database should have been backed up.
238 if (rc != SQLITE_DONE) {
239 DLOG(FATAL) << "Unable to copy entire null database.";
240 return false;
241 }
242
243 // Exactly one page should have been backed up. If this breaks,
244 // check this function to make sure assumptions aren't being broken.
245 DCHECK_EQ(pages, 1);
246
247 return true;
248}
249
250bool Connection::RazeWithTimout(base::TimeDelta timeout) {
251 if (!db_) {
252 DLOG(FATAL) << "Cannot raze null db";
253 return false;
254 }
255
256 ScopedBusyTimeout busy_timeout(db_);
257 busy_timeout.SetTimeout(timeout);
258 return Raze();
259}
260
[email protected]e5ffd0e42009-09-11 21:30:56261bool Connection::BeginTransaction() {
262 if (needs_rollback_) {
[email protected]88563f62011-03-13 22:13:33263 DCHECK_GT(transaction_nesting_, 0);
[email protected]e5ffd0e42009-09-11 21:30:56264
265 // When we're going to rollback, fail on this begin and don't actually
266 // mark us as entering the nested transaction.
267 return false;
268 }
269
270 bool success = true;
271 if (!transaction_nesting_) {
272 needs_rollback_ = false;
273
274 Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION"));
[email protected]eff1fa522011-12-12 23:50:59275 if (!begin.Run())
[email protected]e5ffd0e42009-09-11 21:30:56276 return false;
277 }
278 transaction_nesting_++;
279 return success;
280}
281
282void Connection::RollbackTransaction() {
283 if (!transaction_nesting_) {
[email protected]eff1fa522011-12-12 23:50:59284 DLOG(FATAL) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:56285 return;
286 }
287
288 transaction_nesting_--;
289
290 if (transaction_nesting_ > 0) {
291 // Mark the outermost transaction as needing rollback.
292 needs_rollback_ = true;
293 return;
294 }
295
296 DoRollback();
297}
298
299bool Connection::CommitTransaction() {
300 if (!transaction_nesting_) {
[email protected]eff1fa522011-12-12 23:50:59301 DLOG(FATAL) << "Rolling back a nonexistent transaction";
[email protected]e5ffd0e42009-09-11 21:30:56302 return false;
303 }
304 transaction_nesting_--;
305
306 if (transaction_nesting_ > 0) {
307 // Mark any nested transactions as failing after we've already got one.
308 return !needs_rollback_;
309 }
310
311 if (needs_rollback_) {
312 DoRollback();
313 return false;
314 }
315
316 Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT"));
[email protected]e5ffd0e42009-09-11 21:30:56317 return commit.Run();
318}
319
[email protected]eff1fa522011-12-12 23:50:59320int Connection::ExecuteAndReturnErrorCode(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:50321 AssertIOAllowed();
[email protected]e5ffd0e42009-09-11 21:30:56322 if (!db_)
323 return false;
[email protected]eff1fa522011-12-12 23:50:59324 return sqlite3_exec(db_, sql, NULL, NULL, NULL);
325}
326
327bool Connection::Execute(const char* sql) {
328 int error = ExecuteAndReturnErrorCode(sql);
[email protected]28fe0ff2012-02-25 00:40:33329 // This needs to be a FATAL log because the error case of arriving here is
330 // that there's a malformed SQL statement. This can arise in development if
331 // a change alters the schema but not all queries adjust.
[email protected]eff1fa522011-12-12 23:50:59332 if (error == SQLITE_ERROR)
[email protected]28fe0ff2012-02-25 00:40:33333 DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage();
[email protected]eff1fa522011-12-12 23:50:59334 return error == SQLITE_OK;
[email protected]e5ffd0e42009-09-11 21:30:56335}
336
[email protected]5b96f3772010-09-28 16:30:57337bool Connection::ExecuteWithTimeout(const char* sql, base::TimeDelta timeout) {
338 if (!db_)
339 return false;
340
341 ScopedBusyTimeout busy_timeout(db_);
342 busy_timeout.SetTimeout(timeout);
[email protected]eff1fa522011-12-12 23:50:59343 return Execute(sql);
[email protected]5b96f3772010-09-28 16:30:57344}
345
[email protected]e5ffd0e42009-09-11 21:30:56346bool Connection::HasCachedStatement(const StatementID& id) const {
347 return statement_cache_.find(id) != statement_cache_.end();
348}
349
350scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement(
351 const StatementID& id,
352 const char* sql) {
353 CachedStatementMap::iterator i = statement_cache_.find(id);
354 if (i != statement_cache_.end()) {
355 // Statement is in the cache. It should still be active (we're the only
356 // one invalidating cached statements, and we'll remove it from the cache
357 // if we do that. Make sure we reset it before giving out the cached one in
358 // case it still has some stuff bound.
359 DCHECK(i->second->is_valid());
360 sqlite3_reset(i->second->stmt());
361 return i->second;
362 }
363
364 scoped_refptr<StatementRef> statement = GetUniqueStatement(sql);
365 if (statement->is_valid())
366 statement_cache_[id] = statement; // Only cache valid statements.
367 return statement;
368}
369
370scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement(
371 const char* sql) {
[email protected]35f7e5392012-07-27 19:54:50372 AssertIOAllowed();
373
[email protected]e5ffd0e42009-09-11 21:30:56374 if (!db_)
[email protected]2eec0a22012-07-24 01:59:58375 return new StatementRef(); // Return inactive statement.
[email protected]e5ffd0e42009-09-11 21:30:56376
377 sqlite3_stmt* stmt = NULL;
378 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) {
[email protected]eff1fa522011-12-12 23:50:59379 // This is evidence of a syntax error in the incoming SQL.
380 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
[email protected]2eec0a22012-07-24 01:59:58381 return new StatementRef();
[email protected]e5ffd0e42009-09-11 21:30:56382 }
383 return new StatementRef(this, stmt);
384}
385
[email protected]2eec0a22012-07-24 01:59:58386scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
387 const char* sql) const {
388 if (!db_)
389 return new StatementRef(); // Return inactive statement.
390
391 sqlite3_stmt* stmt = NULL;
392 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
393 if (rc != SQLITE_OK) {
394 // This is evidence of a syntax error in the incoming SQL.
395 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
396 return new StatementRef();
397 }
398 return new StatementRef(stmt);
399}
400
[email protected]eff1fa522011-12-12 23:50:59401bool Connection::IsSQLValid(const char* sql) {
[email protected]35f7e5392012-07-27 19:54:50402 AssertIOAllowed();
[email protected]eff1fa522011-12-12 23:50:59403 sqlite3_stmt* stmt = NULL;
404 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK)
405 return false;
406
407 sqlite3_finalize(stmt);
408 return true;
409}
410
[email protected]1ed78a32009-09-15 20:24:17411bool Connection::DoesTableExist(const char* table_name) const {
[email protected]e2cadec82011-12-13 02:00:53412 return DoesTableOrIndexExist(table_name, "table");
413}
414
415bool Connection::DoesIndexExist(const char* index_name) const {
416 return DoesTableOrIndexExist(index_name, "index");
417}
418
419bool Connection::DoesTableOrIndexExist(
420 const char* name, const char* type) const {
[email protected]2eec0a22012-07-24 01:59:58421 const char* kSql = "SELECT name FROM sqlite_master WHERE type=? AND name=?";
422 Statement statement(GetUntrackedStatement(kSql));
[email protected]e2cadec82011-12-13 02:00:53423 statement.BindString(0, type);
424 statement.BindString(1, name);
[email protected]28fe0ff2012-02-25 00:40:33425
[email protected]e5ffd0e42009-09-11 21:30:56426 return statement.Step(); // Table exists if any row was returned.
427}
428
429bool Connection::DoesColumnExist(const char* table_name,
[email protected]1ed78a32009-09-15 20:24:17430 const char* column_name) const {
[email protected]e5ffd0e42009-09-11 21:30:56431 std::string sql("PRAGMA TABLE_INFO(");
432 sql.append(table_name);
433 sql.append(")");
434
[email protected]2eec0a22012-07-24 01:59:58435 Statement statement(GetUntrackedStatement(sql.c_str()));
[email protected]e5ffd0e42009-09-11 21:30:56436 while (statement.Step()) {
437 if (!statement.ColumnString(1).compare(column_name))
438 return true;
439 }
440 return false;
441}
442
443int64 Connection::GetLastInsertRowId() const {
444 if (!db_) {
[email protected]eff1fa522011-12-12 23:50:59445 DLOG(FATAL) << "Illegal use of connection without a db";
[email protected]e5ffd0e42009-09-11 21:30:56446 return 0;
447 }
448 return sqlite3_last_insert_rowid(db_);
449}
450
[email protected]1ed78a32009-09-15 20:24:17451int Connection::GetLastChangeCount() const {
452 if (!db_) {
[email protected]eff1fa522011-12-12 23:50:59453 DLOG(FATAL) << "Illegal use of connection without a db";
[email protected]1ed78a32009-09-15 20:24:17454 return 0;
455 }
456 return sqlite3_changes(db_);
457}
458
[email protected]e5ffd0e42009-09-11 21:30:56459int Connection::GetErrorCode() const {
460 if (!db_)
461 return SQLITE_ERROR;
462 return sqlite3_errcode(db_);
463}
464
[email protected]767718e52010-09-21 23:18:49465int Connection::GetLastErrno() const {
466 if (!db_)
467 return -1;
468
469 int err = 0;
470 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err))
471 return -2;
472
473 return err;
474}
475
[email protected]e5ffd0e42009-09-11 21:30:56476const char* Connection::GetErrorMessage() const {
477 if (!db_)
478 return "sql::Connection has no connection.";
479 return sqlite3_errmsg(db_);
480}
481
[email protected]765b44502009-10-02 05:01:42482bool Connection::OpenInternal(const std::string& file_name) {
[email protected]35f7e5392012-07-27 19:54:50483 AssertIOAllowed();
484
[email protected]9cfbc922009-11-17 20:13:17485 if (db_) {
[email protected]eff1fa522011-12-12 23:50:59486 DLOG(FATAL) << "sql::Connection is already open.";
[email protected]9cfbc922009-11-17 20:13:17487 return false;
488 }
489
[email protected]765b44502009-10-02 05:01:42490 int err = sqlite3_open(file_name.c_str(), &db_);
491 if (err != SQLITE_OK) {
492 OnSqliteError(err, NULL);
[email protected]64021042012-02-10 20:02:29493 Close();
[email protected]765b44502009-10-02 05:01:42494 db_ = NULL;
495 return false;
496 }
497
[email protected]658f8332010-09-18 04:40:43498 // Enable extended result codes to provide more color on I/O errors.
499 // Not having extended result codes is not a fatal problem, as
500 // Chromium code does not attempt to handle I/O errors anyhow. The
501 // current implementation always returns SQLITE_OK, the DCHECK is to
502 // quickly notify someone if SQLite changes.
503 err = sqlite3_extended_result_codes(db_, 1);
504 DCHECK_EQ(err, SQLITE_OK) << "Could not enable extended result codes";
505
[email protected]5b96f3772010-09-28 16:30:57506 // If indicated, lock up the database before doing anything else, so
507 // that the following code doesn't have to deal with locking.
508 // TODO(shess): This code is brittle. Find the cases where code
509 // doesn't request |exclusive_locking_| and audit that it does the
510 // right thing with SQLITE_BUSY, and that it doesn't make
511 // assumptions about who might change things in the database.
512 // http://crbug.com/56559
513 if (exclusive_locking_) {
514 // TODO(shess): This should probably be a full CHECK(). Code
515 // which requests exclusive locking but doesn't get it is almost
516 // certain to be ill-tested.
517 if (!Execute("PRAGMA locking_mode=EXCLUSIVE"))
[email protected]eff1fa522011-12-12 23:50:59518 DLOG(FATAL) << "Could not set locking mode: " << GetErrorMessage();
[email protected]5b96f3772010-09-28 16:30:57519 }
520
[email protected]4e179ba2012-03-17 16:06:47521 // http://www.sqlite.org/pragma.html#pragma_journal_mode
522 // DELETE (default) - delete -journal file to commit.
523 // TRUNCATE - truncate -journal file to commit.
524 // PERSIST - zero out header of -journal file to commit.
525 // journal_size_limit provides size to trim to in PERSIST.
526 // TODO(shess): Figure out if PERSIST and journal_size_limit really
527 // matter. In theory, it keeps pages pre-allocated, so if
528 // transactions usually fit, it should be faster.
529 ignore_result(Execute("PRAGMA journal_mode = PERSIST"));
530 ignore_result(Execute("PRAGMA journal_size_limit = 16384"));
531
[email protected]c68ce172011-11-24 22:30:27532 const base::TimeDelta kBusyTimeout =
533 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds);
534
[email protected]765b44502009-10-02 05:01:42535 if (page_size_ != 0) {
[email protected]5b96f3772010-09-28 16:30:57536 // Enforce SQLite restrictions on |page_size_|.
537 DCHECK(!(page_size_ & (page_size_ - 1)))
538 << " page_size_ " << page_size_ << " is not a power of two.";
539 static const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h
540 DCHECK_LE(page_size_, kSqliteMaxPageSize);
541 const std::string sql = StringPrintf("PRAGMA page_size=%d", page_size_);
542 if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout))
[email protected]eff1fa522011-12-12 23:50:59543 DLOG(FATAL) << "Could not set page size: " << GetErrorMessage();
[email protected]765b44502009-10-02 05:01:42544 }
545
546 if (cache_size_ != 0) {
[email protected]5b96f3772010-09-28 16:30:57547 const std::string sql = StringPrintf("PRAGMA cache_size=%d", cache_size_);
548 if (!ExecuteWithTimeout(sql.c_str(), kBusyTimeout))
[email protected]eff1fa522011-12-12 23:50:59549 DLOG(FATAL) << "Could not set cache size: " << GetErrorMessage();
[email protected]765b44502009-10-02 05:01:42550 }
551
[email protected]6e0b1442011-08-09 23:23:58552 if (!ExecuteWithTimeout("PRAGMA secure_delete=ON", kBusyTimeout)) {
[email protected]eff1fa522011-12-12 23:50:59553 DLOG(FATAL) << "Could not enable secure_delete: " << GetErrorMessage();
[email protected]6e0b1442011-08-09 23:23:58554 Close();
555 return false;
556 }
557
[email protected]765b44502009-10-02 05:01:42558 return true;
559}
560
[email protected]e5ffd0e42009-09-11 21:30:56561void Connection::DoRollback() {
562 Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK"));
[email protected]eff1fa522011-12-12 23:50:59563 rollback.Run();
[email protected]44ad7d902012-03-23 00:09:05564 needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:56565}
566
567void Connection::StatementRefCreated(StatementRef* ref) {
568 DCHECK(open_statements_.find(ref) == open_statements_.end());
569 open_statements_.insert(ref);
570}
571
572void Connection::StatementRefDeleted(StatementRef* ref) {
573 StatementRefSet::iterator i = open_statements_.find(ref);
574 if (i == open_statements_.end())
[email protected]eff1fa522011-12-12 23:50:59575 DLOG(FATAL) << "Could not find statement";
[email protected]e5ffd0e42009-09-11 21:30:56576 else
577 open_statements_.erase(i);
578}
579
580void Connection::ClearCache() {
581 statement_cache_.clear();
582
583 // The cache clear will get most statements. There may be still be references
584 // to some statements that are held by others (including one-shot statements).
585 // This will deactivate them so they can't be used again.
586 for (StatementRefSet::iterator i = open_statements_.begin();
587 i != open_statements_.end(); ++i)
588 (*i)->Close();
589}
590
[email protected]faa604e2009-09-25 22:38:59591int Connection::OnSqliteError(int err, sql::Statement *stmt) {
592 if (error_delegate_.get())
593 return error_delegate_->OnError(err, this, stmt);
594 // The default handling is to assert on debug and to ignore on release.
[email protected]eff1fa522011-12-12 23:50:59595 DLOG(FATAL) << GetErrorMessage();
[email protected]faa604e2009-09-25 22:38:59596 return err;
597}
598
[email protected]e5ffd0e42009-09-11 21:30:56599} // namespace sql