| 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 #ifndef SQL_CONNECTION_H_ | 5 #ifndef SQL_CONNECTION_H_ |
| 6 #define SQL_CONNECTION_H_ | 6 #define SQL_CONNECTION_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/compiler_specific.h" |
| 14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 15 #include "base/time.h" | 16 #include "base/time.h" |
| 16 #include "sql/sql_export.h" | 17 #include "sql/sql_export.h" |
| 17 | 18 |
| 18 class FilePath; | 19 class FilePath; |
| 19 struct sqlite3; | 20 struct sqlite3; |
| 20 struct sqlite3_stmt; | 21 struct sqlite3_stmt; |
| 21 | 22 |
| 22 namespace sql { | 23 namespace sql { |
| 23 | 24 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 // before calling Open(). If not set, the default is to ignore errors on | 143 // before calling Open(). If not set, the default is to ignore errors on |
| 143 // release and assert on debug builds. | 144 // release and assert on debug builds. |
| 144 void set_error_delegate(ErrorDelegate* delegate) { | 145 void set_error_delegate(ErrorDelegate* delegate) { |
| 145 error_delegate_ = delegate; | 146 error_delegate_ = delegate; |
| 146 } | 147 } |
| 147 | 148 |
| 148 // Initialization ------------------------------------------------------------ | 149 // Initialization ------------------------------------------------------------ |
| 149 | 150 |
| 150 // Initializes the SQL connection for the given file, returning true if the | 151 // Initializes the SQL connection for the given file, returning true if the |
| 151 // file could be opened. You can call this or OpenInMemory. | 152 // file could be opened. You can call this or OpenInMemory. |
| 152 bool Open(const FilePath& path); | 153 bool Open(const FilePath& path) WARN_UNUSED_RESULT; |
| 153 | 154 |
| 154 // Initializes the SQL connection for a temporary in-memory database. There | 155 // Initializes the SQL connection for a temporary in-memory database. There |
| 155 // will be no associated file on disk, and the initial database will be | 156 // will be no associated file on disk, and the initial database will be |
| 156 // empty. You can call this or Open. | 157 // empty. You can call this or Open. |
| 157 bool OpenInMemory(); | 158 bool OpenInMemory() WARN_UNUSED_RESULT; |
| 158 | 159 |
| 159 // Returns trie if the database has been successfully opened. | 160 // Returns trie if the database has been successfully opened. |
| 160 bool is_open() const { return !!db_; } | 161 bool is_open() const { return !!db_; } |
| 161 | 162 |
| 162 // Closes the database. This is automatically performed on destruction for | 163 // Closes the database. This is automatically performed on destruction for |
| 163 // you, but this allows you to close the database early. You must not call | 164 // you, but this allows you to close the database early. You must not call |
| 164 // any other functions after closing it. It is permissable to call Close on | 165 // any other functions after closing it. It is permissable to call Close on |
| 165 // an uninitialized or already-closed database. | 166 // an uninitialized or already-closed database. |
| 166 void Close(); | 167 void Close(); |
| 167 | 168 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 197 | 198 |
| 198 // Returns the current transaction nesting, which will be 0 if there are | 199 // Returns the current transaction nesting, which will be 0 if there are |
| 199 // no open transactions. | 200 // no open transactions. |
| 200 int transaction_nesting() const { return transaction_nesting_; } | 201 int transaction_nesting() const { return transaction_nesting_; } |
| 201 | 202 |
| 202 // Statements ---------------------------------------------------------------- | 203 // Statements ---------------------------------------------------------------- |
| 203 | 204 |
| 204 // Executes the given SQL string, returning true on success. This is | 205 // Executes the given SQL string, returning true on success. This is |
| 205 // normally used for simple, 1-off statements that don't take any bound | 206 // normally used for simple, 1-off statements that don't take any bound |
| 206 // parameters and don't return any data (e.g. CREATE TABLE). | 207 // parameters and don't return any data (e.g. CREATE TABLE). |
| 208 // |
| 207 // This will DCHECK if the |sql| contains errors. | 209 // This will DCHECK if the |sql| contains errors. |
| 208 bool Execute(const char* sql); | 210 // |
| 211 // Do not use ignore_result() to ignore all errors. Use |
| 212 // ExecuteAndReturnErrorCode() and ignore only specific errors. |
| 213 bool Execute(const char* sql) WARN_UNUSED_RESULT; |
| 209 | 214 |
| 210 // Like Execute(), but returns the error code given by SQLite. | 215 // Like Execute(), but returns the error code given by SQLite. |
| 211 int ExecuteAndReturnErrorCode(const char* sql); | 216 int ExecuteAndReturnErrorCode(const char* sql) WARN_UNUSED_RESULT; |
| 212 | 217 |
| 213 // Returns true if we have a statement with the given identifier already | 218 // Returns true if we have a statement with the given identifier already |
| 214 // cached. This is normally not necessary to call, but can be useful if the | 219 // cached. This is normally not necessary to call, but can be useful if the |
| 215 // caller has to dynamically build up SQL to avoid doing so if it's already | 220 // caller has to dynamically build up SQL to avoid doing so if it's already |
| 216 // cached. | 221 // cached. |
| 217 bool HasCachedStatement(const StatementID& id) const; | 222 bool HasCachedStatement(const StatementID& id) const; |
| 218 | 223 |
| 219 // Returns a statement for the given SQL using the statement cache. It can | 224 // Returns a statement for the given SQL using the statement cache. It can |
| 220 // take a nontrivial amount of work to parse and compile a statement, so | 225 // take a nontrivial amount of work to parse and compile a statement, so |
| 221 // keeping commonly-used ones around for future use is important for | 226 // keeping commonly-used ones around for future use is important for |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 void StatementRefDeleted(StatementRef* ref); | 358 void StatementRefDeleted(StatementRef* ref); |
| 354 | 359 |
| 355 // Frees all cached statements from statement_cache_. | 360 // Frees all cached statements from statement_cache_. |
| 356 void ClearCache(); | 361 void ClearCache(); |
| 357 | 362 |
| 358 // Called by Statement objects when an sqlite function returns an error. | 363 // Called by Statement objects when an sqlite function returns an error. |
| 359 // The return value is the error code reflected back to client code. | 364 // The return value is the error code reflected back to client code. |
| 360 int OnSqliteError(int err, Statement* stmt); | 365 int OnSqliteError(int err, Statement* stmt); |
| 361 | 366 |
| 362 // Like |Execute()|, but retries if the database is locked. | 367 // Like |Execute()|, but retries if the database is locked. |
| 363 bool ExecuteWithTimeout(const char* sql, base::TimeDelta ms_timeout); | 368 bool ExecuteWithTimeout(const char* sql, base::TimeDelta ms_timeout) |
| 369 WARN_UNUSED_RESULT; |
| 364 | 370 |
| 365 // The actual sqlite database. Will be NULL before Init has been called or if | 371 // The actual sqlite database. Will be NULL before Init has been called or if |
| 366 // Init resulted in an error. | 372 // Init resulted in an error. |
| 367 sqlite3* db_; | 373 sqlite3* db_; |
| 368 | 374 |
| 369 // Parameters we'll configure in sqlite before doing anything else. Zero means | 375 // Parameters we'll configure in sqlite before doing anything else. Zero means |
| 370 // use the default value. | 376 // use the default value. |
| 371 int page_size_; | 377 int page_size_; |
| 372 int cache_size_; | 378 int cache_size_; |
| 373 bool exclusive_locking_; | 379 bool exclusive_locking_; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 395 // This object handles errors resulting from all forms of executing sqlite | 401 // This object handles errors resulting from all forms of executing sqlite |
| 396 // commands or statements. It can be null which means default handling. | 402 // commands or statements. It can be null which means default handling. |
| 397 scoped_refptr<ErrorDelegate> error_delegate_; | 403 scoped_refptr<ErrorDelegate> error_delegate_; |
| 398 | 404 |
| 399 DISALLOW_COPY_AND_ASSIGN(Connection); | 405 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 400 }; | 406 }; |
| 401 | 407 |
| 402 } // namespace sql | 408 } // namespace sql |
| 403 | 409 |
| 404 #endif // SQL_CONNECTION_H_ | 410 #endif // SQL_CONNECTION_H_ |
| OLD | NEW |