OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "sql/connection.h" | 5 #include "sql/connection.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) { | 261 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) { |
262 // Treat this as non-fatal, it can occur in a number of valid cases, and | 262 // Treat this as non-fatal, it can occur in a number of valid cases, and |
263 // callers should be doing their own error handling. | 263 // callers should be doing their own error handling. |
264 DLOG(WARNING) << "SQL compile error " << GetErrorMessage(); | 264 DLOG(WARNING) << "SQL compile error " << GetErrorMessage(); |
265 return new StatementRef(this, NULL); | 265 return new StatementRef(this, NULL); |
266 } | 266 } |
267 return new StatementRef(this, stmt); | 267 return new StatementRef(this, stmt); |
268 } | 268 } |
269 | 269 |
270 bool Connection::DoesTableExist(const char* table_name) const { | 270 bool Connection::DoesTableExist(const char* table_name) const { |
| 271 return DoesTableOrIndexExist(table_name, "table"); |
| 272 } |
| 273 |
| 274 bool Connection::DoesIndexExist(const char* index_name) const { |
| 275 return DoesTableOrIndexExist(index_name, "index"); |
| 276 } |
| 277 |
| 278 bool Connection::DoesTableOrIndexExist( |
| 279 const char* name, const char* type) const { |
271 // GetUniqueStatement can't be const since statements may modify the | 280 // GetUniqueStatement can't be const since statements may modify the |
272 // database, but we know ours doesn't modify it, so the cast is safe. | 281 // database, but we know ours doesn't modify it, so the cast is safe. |
273 Statement statement(const_cast<Connection*>(this)->GetUniqueStatement( | 282 Statement statement(const_cast<Connection*>(this)->GetUniqueStatement( |
274 "SELECT name FROM sqlite_master " | 283 "SELECT name FROM sqlite_master " |
275 "WHERE type='table' AND name=?")); | 284 "WHERE type=? AND name=?")); |
276 if (!statement) | 285 if (!statement) |
277 return false; | 286 return false; |
278 statement.BindString(0, table_name); | 287 statement.BindString(0, type); |
| 288 statement.BindString(1, name); |
279 return statement.Step(); // Table exists if any row was returned. | 289 return statement.Step(); // Table exists if any row was returned. |
280 } | 290 } |
281 | 291 |
282 bool Connection::DoesColumnExist(const char* table_name, | 292 bool Connection::DoesColumnExist(const char* table_name, |
283 const char* column_name) const { | 293 const char* column_name) const { |
284 std::string sql("PRAGMA TABLE_INFO("); | 294 std::string sql("PRAGMA TABLE_INFO("); |
285 sql.append(table_name); | 295 sql.append(table_name); |
286 sql.append(")"); | 296 sql.append(")"); |
287 | 297 |
288 // Our SQL is non-mutating, so this cast is OK. | 298 // Our SQL is non-mutating, so this cast is OK. |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 | 444 |
435 int Connection::OnSqliteError(int err, sql::Statement *stmt) { | 445 int Connection::OnSqliteError(int err, sql::Statement *stmt) { |
436 if (error_delegate_.get()) | 446 if (error_delegate_.get()) |
437 return error_delegate_->OnError(err, this, stmt); | 447 return error_delegate_->OnError(err, this, stmt); |
438 // The default handling is to assert on debug and to ignore on release. | 448 // The default handling is to assert on debug and to ignore on release. |
439 NOTREACHED() << GetErrorMessage(); | 449 NOTREACHED() << GetErrorMessage(); |
440 return err; | 450 return err; |
441 } | 451 } |
442 | 452 |
443 } // namespace sql | 453 } // namespace sql |
OLD | NEW |