OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "app/sql/connection.h" | 5 #include "app/sql/connection.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "app/sql/statement.h" | 9 #include "app/sql/statement.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 sqlite3_stmt* stmt = NULL; | 217 sqlite3_stmt* stmt = NULL; |
218 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) { | 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 | 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. | 220 // callers should be doing their own error handling. |
221 DLOG(WARNING) << "SQL compile error " << GetErrorMessage(); | 221 DLOG(WARNING) << "SQL compile error " << GetErrorMessage(); |
222 return new StatementRef(this, NULL); | 222 return new StatementRef(this, NULL); |
223 } | 223 } |
224 return new StatementRef(this, stmt); | 224 return new StatementRef(this, stmt); |
225 } | 225 } |
226 | 226 |
227 bool Connection::DoesTableExist(const char* table_name) { | 227 bool Connection::DoesTableExist(const char* table_name) const { |
228 Statement statement(GetUniqueStatement( | 228 // GetUniqueStatement can't be const since statements may modify the |
| 229 // database, but we know ours doesn't modify it, so the cast is safe. |
| 230 Statement statement(const_cast<Connection*>(this)->GetUniqueStatement( |
229 "SELECT name FROM sqlite_master " | 231 "SELECT name FROM sqlite_master " |
230 "WHERE type='table' AND name=?")); | 232 "WHERE type='table' AND name=?")); |
231 if (!statement) | 233 if (!statement) |
232 return false; | 234 return false; |
233 statement.BindString(0, table_name); | 235 statement.BindString(0, table_name); |
234 return statement.Step(); // Table exists if any row was returned. | 236 return statement.Step(); // Table exists if any row was returned. |
235 } | 237 } |
236 | 238 |
237 bool Connection::DoesColumnExist(const char* table_name, | 239 bool Connection::DoesColumnExist(const char* table_name, |
238 const char* column_name) { | 240 const char* column_name) const { |
239 std::string sql("PRAGMA TABLE_INFO("); | 241 std::string sql("PRAGMA TABLE_INFO("); |
240 sql.append(table_name); | 242 sql.append(table_name); |
241 sql.append(")"); | 243 sql.append(")"); |
242 | 244 |
243 Statement statement(GetUniqueStatement(sql.c_str())); | 245 // Our SQL is non-mutating, so this cast is OK. |
| 246 Statement statement(const_cast<Connection*>(this)->GetUniqueStatement( |
| 247 sql.c_str())); |
244 if (!statement) | 248 if (!statement) |
245 return false; | 249 return false; |
246 | 250 |
247 while (statement.Step()) { | 251 while (statement.Step()) { |
248 if (!statement.ColumnString(1).compare(column_name)) | 252 if (!statement.ColumnString(1).compare(column_name)) |
249 return true; | 253 return true; |
250 } | 254 } |
251 return false; | 255 return false; |
252 } | 256 } |
253 | 257 |
254 int64 Connection::GetLastInsertRowId() const { | 258 int64 Connection::GetLastInsertRowId() const { |
255 if (!db_) { | 259 if (!db_) { |
256 NOTREACHED(); | 260 NOTREACHED(); |
257 return 0; | 261 return 0; |
258 } | 262 } |
259 return sqlite3_last_insert_rowid(db_); | 263 return sqlite3_last_insert_rowid(db_); |
260 } | 264 } |
261 | 265 |
| 266 int Connection::GetLastChangeCount() const { |
| 267 if (!db_) { |
| 268 NOTREACHED(); |
| 269 return 0; |
| 270 } |
| 271 return sqlite3_changes(db_); |
| 272 } |
| 273 |
262 int Connection::GetErrorCode() const { | 274 int Connection::GetErrorCode() const { |
263 if (!db_) | 275 if (!db_) |
264 return SQLITE_ERROR; | 276 return SQLITE_ERROR; |
265 return sqlite3_errcode(db_); | 277 return sqlite3_errcode(db_); |
266 } | 278 } |
267 | 279 |
268 const char* Connection::GetErrorMessage() const { | 280 const char* Connection::GetErrorMessage() const { |
269 if (!db_) | 281 if (!db_) |
270 return "sql::Connection has no connection."; | 282 return "sql::Connection has no connection."; |
271 return sqlite3_errmsg(db_); | 283 return sqlite3_errmsg(db_); |
(...skipping 23 matching lines...) Expand all Loading... |
295 | 307 |
296 // The cache clear will get most statements. There may be still be references | 308 // 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). | 309 // to some statements that are held by others (including one-shot statements). |
298 // This will deactivate them so they can't be used again. | 310 // This will deactivate them so they can't be used again. |
299 for (StatementRefSet::iterator i = open_statements_.begin(); | 311 for (StatementRefSet::iterator i = open_statements_.begin(); |
300 i != open_statements_.end(); ++i) | 312 i != open_statements_.end(); ++i) |
301 (*i)->Close(); | 313 (*i)->Close(); |
302 } | 314 } |
303 | 315 |
304 } // namespace sql | 316 } // namespace sql |
OLD | NEW |