[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame^] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "app/sql/connection.h" |
| 6 | |
| 7 | #include <string.h> |
| 8 | |
| 9 | #include "app/sql/statement.h" |
| 10 | #include "base/file_path.h" |
| 11 | #include "base/logging.h" |
| 12 | #include "base/string_util.h" |
| 13 | #include "third_party/sqlite/preprocessed/sqlite3.h" |
| 14 | |
| 15 | namespace sql { |
| 16 | |
| 17 | bool StatementID::operator<(const StatementID& other) const { |
| 18 | if (number_ != other.number_) |
| 19 | return number_ < other.number_; |
| 20 | return strcmp(str_, other.str_) < 0; |
| 21 | } |
| 22 | |
| 23 | Connection::StatementRef::StatementRef() |
| 24 | : connection_(NULL), |
| 25 | stmt_(NULL) { |
| 26 | } |
| 27 | |
| 28 | Connection::StatementRef::StatementRef(Connection* connection, |
| 29 | sqlite3_stmt* stmt) |
| 30 | : connection_(connection), |
| 31 | stmt_(stmt) { |
| 32 | connection_->StatementRefCreated(this); |
| 33 | } |
| 34 | |
| 35 | Connection::StatementRef::~StatementRef() { |
| 36 | if (connection_) |
| 37 | connection_->StatementRefDeleted(this); |
| 38 | Close(); |
| 39 | } |
| 40 | |
| 41 | void Connection::StatementRef::Close() { |
| 42 | if (stmt_) { |
| 43 | sqlite3_finalize(stmt_); |
| 44 | stmt_ = NULL; |
| 45 | } |
| 46 | connection_ = NULL; // The connection may be getting deleted. |
| 47 | } |
| 48 | |
| 49 | Connection::Connection() |
| 50 | : db_(NULL), |
| 51 | page_size_(0), |
| 52 | cache_size_(0), |
| 53 | exclusive_locking_(false), |
| 54 | transaction_nesting_(0), |
| 55 | needs_rollback_(false) { |
| 56 | } |
| 57 | |
| 58 | Connection::~Connection() { |
| 59 | Close(); |
| 60 | } |
| 61 | |
| 62 | bool Connection::Init(const FilePath& path) { |
| 63 | #if defined(OS_WIN) |
| 64 | // We want the default encoding to always be UTF-8, so we use the |
| 65 | // 8-bit version of open(). |
| 66 | int err = sqlite3_open(WideToUTF8(path.value()).c_str(), &db_); |
| 67 | #elif defined(OS_POSIX) |
| 68 | int err = sqlite3_open(path.value().c_str(), &db_); |
| 69 | #endif |
| 70 | |
| 71 | if (err != SQLITE_OK) { |
| 72 | db_ = NULL; |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | if (page_size_ != 0) { |
| 77 | if (!Execute(StringPrintf("PRAGMA page_size=%d", page_size_).c_str())) |
| 78 | NOTREACHED() << "Could not set page size"; |
| 79 | } |
| 80 | |
| 81 | if (cache_size_ != 0) { |
| 82 | if (!Execute(StringPrintf("PRAGMA cache_size=%d", cache_size_).c_str())) |
| 83 | NOTREACHED() << "Could not set page size"; |
| 84 | } |
| 85 | |
| 86 | if (exclusive_locking_) { |
| 87 | if (!Execute("PRAGMA locking_mode=EXCLUSIVE")) |
| 88 | NOTREACHED() << "Could not set locking mode."; |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | void Connection::Close() { |
| 95 | statement_cache_.clear(); |
| 96 | DCHECK(open_statements_.empty()); |
| 97 | if (db_) { |
| 98 | sqlite3_close(db_); |
| 99 | db_ = NULL; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void Connection::Preload() { |
| 104 | if (!db_) { |
| 105 | NOTREACHED(); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // A statement must be open for the preload command to work. If the meta |
| 110 | // table doesn't exist, it probably means this is a new database and there |
| 111 | // is nothing to preload (so it's OK we do nothing). |
| 112 | if (!DoesTableExist("meta")) |
| 113 | return; |
| 114 | Statement dummy(GetUniqueStatement("SELECT * FROM meta")); |
| 115 | if (!dummy || !dummy.Run()) |
| 116 | return; |
| 117 | |
| 118 | sqlite3Preload(db_); |
| 119 | } |
| 120 | |
| 121 | bool Connection::BeginTransaction() { |
| 122 | if (needs_rollback_) { |
| 123 | DCHECK(transaction_nesting_ > 0); |
| 124 | |
| 125 | // When we're going to rollback, fail on this begin and don't actually |
| 126 | // mark us as entering the nested transaction. |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | bool success = true; |
| 131 | if (!transaction_nesting_) { |
| 132 | needs_rollback_ = false; |
| 133 | |
| 134 | Statement begin(GetCachedStatement(SQL_FROM_HERE, "BEGIN TRANSACTION")); |
| 135 | if (!begin || !begin.Run()) |
| 136 | return false; |
| 137 | } |
| 138 | transaction_nesting_++; |
| 139 | return success; |
| 140 | } |
| 141 | |
| 142 | void Connection::RollbackTransaction() { |
| 143 | if (!transaction_nesting_) { |
| 144 | NOTREACHED() << "Rolling back a nonexistant transaction"; |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | transaction_nesting_--; |
| 149 | |
| 150 | if (transaction_nesting_ > 0) { |
| 151 | // Mark the outermost transaction as needing rollback. |
| 152 | needs_rollback_ = true; |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | DoRollback(); |
| 157 | } |
| 158 | |
| 159 | bool Connection::CommitTransaction() { |
| 160 | if (!transaction_nesting_) { |
| 161 | NOTREACHED() << "Rolling back a nonexistant transaction"; |
| 162 | return false; |
| 163 | } |
| 164 | transaction_nesting_--; |
| 165 | |
| 166 | if (transaction_nesting_ > 0) { |
| 167 | // Mark any nested transactions as failing after we've already got one. |
| 168 | return !needs_rollback_; |
| 169 | } |
| 170 | |
| 171 | if (needs_rollback_) { |
| 172 | DoRollback(); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | Statement commit(GetCachedStatement(SQL_FROM_HERE, "COMMIT")); |
| 177 | if (!commit) |
| 178 | return false; |
| 179 | return commit.Run(); |
| 180 | } |
| 181 | |
| 182 | bool Connection::Execute(const char* sql) { |
| 183 | if (!db_) |
| 184 | return false; |
| 185 | return sqlite3_exec(db_, sql, NULL, NULL, NULL) == SQLITE_OK; |
| 186 | } |
| 187 | |
| 188 | bool Connection::HasCachedStatement(const StatementID& id) const { |
| 189 | return statement_cache_.find(id) != statement_cache_.end(); |
| 190 | } |
| 191 | |
| 192 | scoped_refptr<Connection::StatementRef> Connection::GetCachedStatement( |
| 193 | const StatementID& id, |
| 194 | const char* sql) { |
| 195 | CachedStatementMap::iterator i = statement_cache_.find(id); |
| 196 | if (i != statement_cache_.end()) { |
| 197 | // Statement is in the cache. It should still be active (we're the only |
| 198 | // one invalidating cached statements, and we'll remove it from the cache |
| 199 | // if we do that. Make sure we reset it before giving out the cached one in |
| 200 | // case it still has some stuff bound. |
| 201 | DCHECK(i->second->is_valid()); |
| 202 | sqlite3_reset(i->second->stmt()); |
| 203 | return i->second; |
| 204 | } |
| 205 | |
| 206 | scoped_refptr<StatementRef> statement = GetUniqueStatement(sql); |
| 207 | if (statement->is_valid()) |
| 208 | statement_cache_[id] = statement; // Only cache valid statements. |
| 209 | return statement; |
| 210 | } |
| 211 | |
| 212 | scoped_refptr<Connection::StatementRef> Connection::GetUniqueStatement( |
| 213 | const char* sql) { |
| 214 | if (!db_) |
| 215 | return new StatementRef(this, NULL); // Return inactive statement. |
| 216 | |
| 217 | sqlite3_stmt* stmt = NULL; |
| 218 | if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) { |
| 219 | // Treat this as non-fatal, it can occur in a number of valid cases, and |
| 220 | // callers should be doing their own error handling. |
| 221 | DLOG(WARNING) << "SQL compile error " << GetErrorMessage(); |
| 222 | return new StatementRef(this, NULL); |
| 223 | } |
| 224 | return new StatementRef(this, stmt); |
| 225 | } |
| 226 | |
| 227 | bool Connection::DoesTableExist(const char* table_name) { |
| 228 | Statement statement(GetUniqueStatement( |
| 229 | "SELECT name FROM sqlite_master " |
| 230 | "WHERE type='table' AND name=?")); |
| 231 | if (!statement) |
| 232 | return false; |
| 233 | statement.BindString(0, table_name); |
| 234 | return statement.Step(); // Table exists if any row was returned. |
| 235 | } |
| 236 | |
| 237 | bool Connection::DoesColumnExist(const char* table_name, |
| 238 | const char* column_name) { |
| 239 | std::string sql("PRAGMA TABLE_INFO("); |
| 240 | sql.append(table_name); |
| 241 | sql.append(")"); |
| 242 | |
| 243 | Statement statement(GetUniqueStatement(sql.c_str())); |
| 244 | if (!statement) |
| 245 | return false; |
| 246 | |
| 247 | while (statement.Step()) { |
| 248 | if (!statement.ColumnString(1).compare(column_name)) |
| 249 | return true; |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | int64 Connection::GetLastInsertRowId() const { |
| 255 | if (!db_) { |
| 256 | NOTREACHED(); |
| 257 | return 0; |
| 258 | } |
| 259 | return sqlite3_last_insert_rowid(db_); |
| 260 | } |
| 261 | |
| 262 | int Connection::GetErrorCode() const { |
| 263 | if (!db_) |
| 264 | return SQLITE_ERROR; |
| 265 | return sqlite3_errcode(db_); |
| 266 | } |
| 267 | |
| 268 | const char* Connection::GetErrorMessage() const { |
| 269 | if (!db_) |
| 270 | return "sql::Connection has no connection."; |
| 271 | return sqlite3_errmsg(db_); |
| 272 | } |
| 273 | |
| 274 | void Connection::DoRollback() { |
| 275 | Statement rollback(GetCachedStatement(SQL_FROM_HERE, "ROLLBACK")); |
| 276 | if (rollback) |
| 277 | rollback.Run(); |
| 278 | } |
| 279 | |
| 280 | void Connection::StatementRefCreated(StatementRef* ref) { |
| 281 | DCHECK(open_statements_.find(ref) == open_statements_.end()); |
| 282 | open_statements_.insert(ref); |
| 283 | } |
| 284 | |
| 285 | void Connection::StatementRefDeleted(StatementRef* ref) { |
| 286 | StatementRefSet::iterator i = open_statements_.find(ref); |
| 287 | if (i == open_statements_.end()) |
| 288 | NOTREACHED(); |
| 289 | else |
| 290 | open_statements_.erase(i); |
| 291 | } |
| 292 | |
| 293 | void Connection::ClearCache() { |
| 294 | statement_cache_.clear(); |
| 295 | |
| 296 | // The cache clear will get most statements. There may be still be references |
| 297 | // to some statements that are held by others (including one-shot statements). |
| 298 | // This will deactivate them so they can't be used again. |
| 299 | for (StatementRefSet::iterator i = open_statements_.begin(); |
| 300 | i != open_statements_.end(); ++i) |
| 301 | (*i)->Close(); |
| 302 | } |
| 303 | |
| 304 | } // namespace sql |