blob: 049d7cf5b4e2f9715ffc4b7f67dd2e89dc367bff [file] [log] [blame]
[email protected]2eec0a22012-07-24 01:59:581// 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#ifndef SQL_CONNECTION_H_
6#define SQL_CONNECTION_H_
[email protected]e5ffd0e42009-09-11 21:30:567
8#include <map>
9#include <set>
[email protected]7d6aee4e2009-09-12 01:12:3310#include <string>
[email protected]80abf152013-05-22 12:42:4211#include <vector>
[email protected]e5ffd0e42009-09-11 21:30:5612
13#include "base/basictypes.h"
[email protected]c3881b372013-05-17 08:39:4614#include "base/callback.h"
[email protected]9fe37552011-12-23 17:07:2015#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/ref_counted.h"
[email protected]49dc4f22012-10-17 17:41:1617#include "base/memory/scoped_ptr.h"
[email protected]35f7e5392012-07-27 19:54:5018#include "base/threading/thread_restrictions.h"
[email protected]5b96f3772010-09-28 16:30:5719#include "base/time.h"
[email protected]d4526962011-11-10 21:40:2820#include "sql/sql_export.h"
[email protected]e5ffd0e42009-09-11 21:30:5621
[email protected]e5ffd0e42009-09-11 21:30:5622struct sqlite3;
23struct sqlite3_stmt;
24
[email protected]a3ef4832013-02-02 05:12:3325namespace base {
26class FilePath;
27}
28
[email protected]e5ffd0e42009-09-11 21:30:5629namespace sql {
30
31class Statement;
32
33// Uniquely identifies a statement. There are two modes of operation:
34//
35// - In the most common mode, you will use the source file and line number to
36// identify your statement. This is a convienient way to get uniqueness for
37// a statement that is only used in one place. Use the SQL_FROM_HERE macro
38// to generate a StatementID.
39//
40// - In the "custom" mode you may use the statement from different places or
41// need to manage it yourself for whatever reason. In this case, you should
42// make up your own unique name and pass it to the StatementID. This name
43// must be a static string, since this object only deals with pointers and
44// assumes the underlying string doesn't change or get deleted.
45//
46// This object is copyable and assignable using the compiler-generated
47// operator= and copy constructor.
48class StatementID {
49 public:
50 // Creates a uniquely named statement with the given file ane line number.
51 // Normally you will use SQL_FROM_HERE instead of calling yourself.
52 StatementID(const char* file, int line)
53 : number_(line),
54 str_(file) {
55 }
56
57 // Creates a uniquely named statement with the given user-defined name.
58 explicit StatementID(const char* unique_name)
59 : number_(-1),
60 str_(unique_name) {
61 }
62
63 // This constructor is unimplemented and will generate a linker error if
64 // called. It is intended to try to catch people dynamically generating
65 // a statement name that will be deallocated and will cause a crash later.
66 // All strings must be static and unchanging!
67 explicit StatementID(const std::string& dont_ever_do_this);
68
69 // We need this to insert into our map.
70 bool operator<(const StatementID& other) const;
71
72 private:
73 int number_;
74 const char* str_;
75};
76
77#define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__)
78
[email protected]faa604e2009-09-25 22:38:5979class Connection;
80
81// ErrorDelegate defines the interface to implement error handling and recovery
82// for sqlite operations. This allows the rest of the classes to return true or
83// false while the actual error code and causing statement are delivered using
84// the OnError() callback.
85// The tipical usage is to centralize the code designed to handle database
86// corruption, low-level IO errors or locking violations.
[email protected]49dc4f22012-10-17 17:41:1687class SQL_EXPORT ErrorDelegate {
[email protected]faa604e2009-09-25 22:38:5988 public:
[email protected]49dc4f22012-10-17 17:41:1689 virtual ~ErrorDelegate();
[email protected]d4799a32010-09-28 22:54:5890
[email protected]0d04ede2012-10-18 04:31:5391 // |error| is an sqlite result code as seen in sqlite3.h. |connection| is the
92 // db connection where the error happened and |stmt| is our best guess at the
93 // statement that triggered the error. Do not store these pointers.
[email protected]765b44502009-10-02 05:01:4294 //
95 // |stmt| MAY BE NULL if there is no statement causing the problem (i.e. on
96 // initialization).
97 //
[email protected]0d04ede2012-10-18 04:31:5398 // If the error condition has been fixed and the original statement succesfuly
99 // re-tried then returning SQLITE_OK is appropriate; otherwise it is
100 // recommended that you return the original |error| or the appropriate error
101 // code.
[email protected]faa604e2009-09-25 22:38:59102 virtual int OnError(int error, Connection* connection, Statement* stmt) = 0;
103};
104
[email protected]d4526962011-11-10 21:40:28105class SQL_EXPORT Connection {
[email protected]e5ffd0e42009-09-11 21:30:56106 private:
107 class StatementRef; // Forward declaration, see real one below.
108
109 public:
[email protected]765b44502009-10-02 05:01:42110 // The database is opened by calling Open[InMemory](). Any uncommitted
111 // transactions will be rolled back when this object is deleted.
[email protected]e5ffd0e42009-09-11 21:30:56112 Connection();
113 ~Connection();
114
115 // Pre-init configuration ----------------------------------------------------
116
[email protected]765b44502009-10-02 05:01:42117 // Sets the page size that will be used when creating a new database. This
[email protected]e5ffd0e42009-09-11 21:30:56118 // must be called before Init(), and will only have an effect on new
119 // databases.
120 //
121 // From sqlite.org: "The page size must be a power of two greater than or
122 // equal to 512 and less than or equal to SQLITE_MAX_PAGE_SIZE. The maximum
123 // value for SQLITE_MAX_PAGE_SIZE is 32768."
124 void set_page_size(int page_size) { page_size_ = page_size; }
125
126 // Sets the number of pages that will be cached in memory by sqlite. The
127 // total cache size in bytes will be page_size * cache_size. This must be
[email protected]765b44502009-10-02 05:01:42128 // called before Open() to have an effect.
[email protected]e5ffd0e42009-09-11 21:30:56129 void set_cache_size(int cache_size) { cache_size_ = cache_size; }
130
131 // Call to put the database in exclusive locking mode. There is no "back to
132 // normal" flag because of some additional requirements sqlite puts on this
133 // transaition (requires another access to the DB) and because we don't
134 // actually need it.
135 //
136 // Exclusive mode means that the database is not unlocked at the end of each
137 // transaction, which means there may be less time spent initializing the
138 // next transaction because it doesn't have to re-aquire locks.
139 //
[email protected]765b44502009-10-02 05:01:42140 // This must be called before Open() to have an effect.
[email protected]e5ffd0e42009-09-11 21:30:56141 void set_exclusive_locking() { exclusive_locking_ = true; }
142
[email protected]c3881b372013-05-17 08:39:46143 // Set an error-handling callback. On errors, the error number (and
144 // statement, if available) will be passed to the callback.
145 //
146 // If no callback is set, the default action is to crash in debug
147 // mode or return failure in release mode.
148 //
149 // TODO(shess): ErrorDelegate allowed for returning a different
150 // error. Determine if this is necessary for the callback. In my
151 // experience, this is not well-tested and probably not safe, and
152 // current clients always return the same error passed.
153 // Additionally, most errors don't admit to a clean way to retry the
154 // failed operation, so converting an error to SQLITE_OK is probably
155 // not feasible.
156 typedef base::Callback<void(int, Statement*)> ErrorCallback;
157 void set_error_callback(const ErrorCallback& callback) {
158 error_callback_ = callback;
159 }
160 void reset_error_callback() {
161 error_callback_.Reset();
162 }
163
[email protected]faa604e2009-09-25 22:38:59164 // Sets the object that will handle errors. Recomended that it should be set
[email protected]765b44502009-10-02 05:01:42165 // before calling Open(). If not set, the default is to ignore errors on
[email protected]faa604e2009-09-25 22:38:59166 // release and assert on debug builds.
[email protected]49dc4f22012-10-17 17:41:16167 // Takes ownership of |delegate|.
[email protected]c3881b372013-05-17 08:39:46168 // NOTE(shess): Deprecated, use set_error_callback().
[email protected]faa604e2009-09-25 22:38:59169 void set_error_delegate(ErrorDelegate* delegate) {
[email protected]49dc4f22012-10-17 17:41:16170 error_delegate_.reset(delegate);
[email protected]faa604e2009-09-25 22:38:59171 }
172
[email protected]210ce0af2013-05-15 09:10:39173 // Set this tag to enable additional connection-type histogramming
174 // for SQLite error codes and database version numbers.
175 void set_histogram_tag(const std::string& tag) {
176 histogram_tag_ = tag;
[email protected]c088e3a32013-01-03 23:59:14177 }
178
[email protected]210ce0af2013-05-15 09:10:39179 // Record a sparse UMA histogram sample under
180 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no
181 // histogram is recorded.
182 void AddTaggedHistogram(const std::string& name, size_t sample) const;
183
[email protected]80abf152013-05-22 12:42:42184 // Run "PRAGMA integrity_check" and post each line of results into
185 // |messages|. Returns the success of running the statement - per
186 // the SQLite documentation, if no errors are found the call should
187 // succeed, and a single value "ok" should be in messages.
188 bool IntegrityCheck(std::vector<std::string>* messages);
189
[email protected]e5ffd0e42009-09-11 21:30:56190 // Initialization ------------------------------------------------------------
191
192 // Initializes the SQL connection for the given file, returning true if the
[email protected]35f2094c2009-12-29 22:46:55193 // file could be opened. You can call this or OpenInMemory.
[email protected]a3ef4832013-02-02 05:12:33194 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42195
196 // Initializes the SQL connection for a temporary in-memory database. There
197 // will be no associated file on disk, and the initial database will be
[email protected]35f2094c2009-12-29 22:46:55198 // empty. You can call this or Open.
[email protected]9fe37552011-12-23 17:07:20199 bool OpenInMemory() WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42200
[email protected]41a97c812013-02-07 02:35:38201 // Returns true if the database has been successfully opened.
[email protected]765b44502009-10-02 05:01:42202 bool is_open() const { return !!db_; }
[email protected]e5ffd0e42009-09-11 21:30:56203
204 // Closes the database. This is automatically performed on destruction for
205 // you, but this allows you to close the database early. You must not call
206 // any other functions after closing it. It is permissable to call Close on
207 // an uninitialized or already-closed database.
208 void Close();
209
210 // Pre-loads the first <cache-size> pages into the cache from the file.
211 // If you expect to soon use a substantial portion of the database, this
212 // is much more efficient than allowing the pages to be populated organically
213 // since there is no per-page hard drive seeking. If the file is larger than
214 // the cache, the last part that doesn't fit in the cache will be brought in
215 // organically.
216 //
217 // This function assumes your class is using a meta table on the current
218 // database, as it openes a transaction on the meta table to force the
219 // database to be initialized. You should feel free to initialize the meta
220 // table after calling preload since the meta table will already be in the
221 // database if it exists, and if it doesn't exist, the database won't
222 // generally exist either.
223 void Preload();
224
[email protected]8e0c01282012-04-06 19:36:49225 // Raze the database to the ground. This approximates creating a
226 // fresh database from scratch, within the constraints of SQLite's
227 // locking protocol (locks and open handles can make doing this with
228 // filesystem operations problematic). Returns true if the database
229 // was razed.
230 //
231 // false is returned if the database is locked by some other
232 // process. RazeWithTimeout() may be used if appropriate.
233 //
234 // NOTE(shess): Raze() will DCHECK in the following situations:
235 // - database is not open.
236 // - the connection has a transaction open.
237 // - a SQLite issue occurs which is structural in nature (like the
238 // statements used are broken).
239 // Since Raze() is expected to be called in unexpected situations,
240 // these all return false, since it is unlikely that the caller
241 // could fix them.
[email protected]6d42f152012-11-10 00:38:24242 //
243 // The database's page size is taken from |page_size_|. The
244 // existing database's |auto_vacuum| setting is lost (the
245 // possibility of corruption makes it unreliable to pull it from the
246 // existing database). To re-enable on the empty database requires
247 // running "PRAGMA auto_vacuum = 1;" then "VACUUM".
248 //
249 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1,
250 // so Raze() sets auto_vacuum to 1.
251 //
252 // TODO(shess): Raze() needs a connection so cannot clear SQLITE_NOTADB.
253 // TODO(shess): Bake auto_vacuum into Connection's API so it can
254 // just pick up the default.
[email protected]8e0c01282012-04-06 19:36:49255 bool Raze();
256 bool RazeWithTimout(base::TimeDelta timeout);
257
[email protected]41a97c812013-02-07 02:35:38258 // Breaks all outstanding transactions (as initiated by
259 // BeginTransaction()), calls Raze() to destroy the database, then
260 // closes the database. After this is called, any operations
261 // against the connections (or statements prepared by the
262 // connection) should fail safely.
263 //
264 // The value from Raze() is returned, with Close() called in all
265 // cases.
266 bool RazeAndClose();
267
[email protected]e5ffd0e42009-09-11 21:30:56268 // Transactions --------------------------------------------------------------
269
270 // Transaction management. We maintain a virtual transaction stack to emulate
271 // nested transactions since sqlite can't do nested transactions. The
272 // limitation is you can't roll back a sub transaction: if any transaction
273 // fails, all transactions open will also be rolled back. Any nested
274 // transactions after one has rolled back will return fail for Begin(). If
275 // Begin() fails, you must not call Commit or Rollback().
276 //
277 // Normally you should use sql::Transaction to manage a transaction, which
278 // will scope it to a C++ context.
279 bool BeginTransaction();
280 void RollbackTransaction();
281 bool CommitTransaction();
282
283 // Returns the current transaction nesting, which will be 0 if there are
284 // no open transactions.
285 int transaction_nesting() const { return transaction_nesting_; }
286
287 // Statements ----------------------------------------------------------------
288
289 // Executes the given SQL string, returning true on success. This is
290 // normally used for simple, 1-off statements that don't take any bound
291 // parameters and don't return any data (e.g. CREATE TABLE).
[email protected]9fe37552011-12-23 17:07:20292 //
[email protected]eff1fa522011-12-12 23:50:59293 // This will DCHECK if the |sql| contains errors.
[email protected]9fe37552011-12-23 17:07:20294 //
295 // Do not use ignore_result() to ignore all errors. Use
296 // ExecuteAndReturnErrorCode() and ignore only specific errors.
297 bool Execute(const char* sql) WARN_UNUSED_RESULT;
[email protected]e5ffd0e42009-09-11 21:30:56298
[email protected]eff1fa522011-12-12 23:50:59299 // Like Execute(), but returns the error code given by SQLite.
[email protected]9fe37552011-12-23 17:07:20300 int ExecuteAndReturnErrorCode(const char* sql) WARN_UNUSED_RESULT;
[email protected]eff1fa522011-12-12 23:50:59301
[email protected]e5ffd0e42009-09-11 21:30:56302 // Returns true if we have a statement with the given identifier already
303 // cached. This is normally not necessary to call, but can be useful if the
304 // caller has to dynamically build up SQL to avoid doing so if it's already
305 // cached.
306 bool HasCachedStatement(const StatementID& id) const;
307
308 // Returns a statement for the given SQL using the statement cache. It can
309 // take a nontrivial amount of work to parse and compile a statement, so
310 // keeping commonly-used ones around for future use is important for
311 // performance.
312 //
[email protected]eff1fa522011-12-12 23:50:59313 // If the |sql| has an error, an invalid, inert StatementRef is returned (and
314 // the code will crash in debug). The caller must deal with this eventuality,
315 // either by checking validity of the |sql| before calling, by correctly
316 // handling the return of an inert statement, or both.
[email protected]e5ffd0e42009-09-11 21:30:56317 //
318 // The StatementID and the SQL must always correspond to one-another. The
319 // ID is the lookup into the cache, so crazy things will happen if you use
320 // different SQL with the same ID.
321 //
322 // You will normally use the SQL_FROM_HERE macro to generate a statement
323 // ID associated with the current line of code. This gives uniqueness without
324 // you having to manage unique names. See StatementID above for more.
325 //
326 // Example:
[email protected]3273dce2010-01-27 16:08:08327 // sql::Statement stmt(connection_.GetCachedStatement(
328 // SQL_FROM_HERE, "SELECT * FROM foo"));
[email protected]e5ffd0e42009-09-11 21:30:56329 // if (!stmt)
330 // return false; // Error creating statement.
331 scoped_refptr<StatementRef> GetCachedStatement(const StatementID& id,
332 const char* sql);
333
[email protected]eff1fa522011-12-12 23:50:59334 // Used to check a |sql| statement for syntactic validity. If the statement is
335 // valid SQL, returns true.
336 bool IsSQLValid(const char* sql);
337
[email protected]e5ffd0e42009-09-11 21:30:56338 // Returns a non-cached statement for the given SQL. Use this for SQL that
339 // is only executed once or only rarely (there is overhead associated with
340 // keeping a statement cached).
341 //
342 // See GetCachedStatement above for examples and error information.
343 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql);
344
345 // Info querying -------------------------------------------------------------
346
347 // Returns true if the given table exists.
[email protected]765b44502009-10-02 05:01:42348 bool DoesTableExist(const char* table_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56349
[email protected]e2cadec82011-12-13 02:00:53350 // Returns true if the given index exists.
351 bool DoesIndexExist(const char* index_name) const;
352
[email protected]e5ffd0e42009-09-11 21:30:56353 // Returns true if a column with the given name exists in the given table.
[email protected]1ed78a32009-09-15 20:24:17354 bool DoesColumnExist(const char* table_name, const char* column_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56355
356 // Returns sqlite's internal ID for the last inserted row. Valid only
357 // immediately after an insert.
358 int64 GetLastInsertRowId() const;
359
[email protected]1ed78a32009-09-15 20:24:17360 // Returns sqlite's count of the number of rows modified by the last
361 // statement executed. Will be 0 if no statement has executed or the database
362 // is closed.
363 int GetLastChangeCount() const;
364
[email protected]e5ffd0e42009-09-11 21:30:56365 // Errors --------------------------------------------------------------------
366
367 // Returns the error code associated with the last sqlite operation.
368 int GetErrorCode() const;
369
[email protected]767718e52010-09-21 23:18:49370 // Returns the errno associated with GetErrorCode(). See
371 // SQLITE_LAST_ERRNO in SQLite documentation.
372 int GetLastErrno() const;
373
[email protected]e5ffd0e42009-09-11 21:30:56374 // Returns a pointer to a statically allocated string associated with the
375 // last sqlite operation.
376 const char* GetErrorMessage() const;
377
378 private:
[email protected]eff1fa522011-12-12 23:50:59379 // Statement accesses StatementRef which we don't want to expose to everybody
[email protected]e5ffd0e42009-09-11 21:30:56380 // (they should go through Statement).
381 friend class Statement;
382
[email protected]765b44502009-10-02 05:01:42383 // Internal initialize function used by both Init and InitInMemory. The file
384 // name is always 8 bits since we want to use the 8-bit version of
385 // sqlite3_open. The string can also be sqlite's special ":memory:" string.
386 bool OpenInternal(const std::string& file_name);
387
[email protected]41a97c812013-02-07 02:35:38388 // Internal close function used by Close() and RazeAndClose().
389 // |forced| indicates that orderly-shutdown checks should not apply.
390 void CloseInternal(bool forced);
391
[email protected]35f7e5392012-07-27 19:54:50392 // Check whether the current thread is allowed to make IO calls, but only
393 // if database wasn't open in memory. Function is inlined to be a no-op in
394 // official build.
395 void AssertIOAllowed() {
396 if (!in_memory_)
397 base::ThreadRestrictions::AssertIOAllowed();
398 }
399
[email protected]e2cadec82011-12-13 02:00:53400 // Internal helper for DoesTableExist and DoesIndexExist.
401 bool DoesTableOrIndexExist(const char* name, const char* type) const;
402
[email protected]e5ffd0e42009-09-11 21:30:56403 // A StatementRef is a refcounted wrapper around a sqlite statement pointer.
404 // Refcounting allows us to give these statements out to sql::Statement
405 // objects while also optionally maintaining a cache of compiled statements
406 // by just keeping a refptr to these objects.
407 //
408 // A statement ref can be valid, in which case it can be used, or invalid to
409 // indicate that the statement hasn't been created yet, has an error, or has
410 // been destroyed.
411 //
412 // The Connection may revoke a StatementRef in some error cases, so callers
413 // should always check validity before using.
[email protected]601dc6a2011-11-12 01:14:23414 class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> {
[email protected]e5ffd0e42009-09-11 21:30:56415 public:
[email protected]41a97c812013-02-07 02:35:38416 // |connection| is the sql::Connection instance associated with
417 // the statement, and is used for tracking outstanding statements
418 // and for error handling. Set to NULL for invalid or untracked
419 // refs. |stmt| is the actual statement, and should only be NULL
420 // to create an invalid ref. |was_valid| indicates whether the
421 // statement should be considered valid for diagnistic purposes.
422 // |was_valid| can be true for NULL |stmt| if the connection has
423 // been forcibly closed by an error handler.
424 StatementRef(Connection* connection, sqlite3_stmt* stmt, bool was_valid);
[email protected]e5ffd0e42009-09-11 21:30:56425
426 // When true, the statement can be used.
427 bool is_valid() const { return !!stmt_; }
428
[email protected]41a97c812013-02-07 02:35:38429 // When true, the statement is either currently valid, or was
430 // previously valid but the connection was forcibly closed. Used
431 // for diagnostic checks.
432 bool was_valid() const { return was_valid_; }
433
[email protected]b4c363b2013-01-17 13:11:17434 // If we've not been linked to a connection, this will be NULL.
435 // TODO(shess): connection_ can be NULL in case of GetUntrackedStatement(),
436 // which prevents Statement::OnError() from forwarding errors.
[email protected]e5ffd0e42009-09-11 21:30:56437 Connection* connection() const { return connection_; }
438
439 // Returns the sqlite statement if any. If the statement is not active,
440 // this will return NULL.
441 sqlite3_stmt* stmt() const { return stmt_; }
442
443 // Destroys the compiled statement and marks it NULL. The statement will
[email protected]41a97c812013-02-07 02:35:38444 // no longer be active. |forced| is used to indicate if orderly-shutdown
445 // checks should apply (see Connection::RazeAndClose()).
446 void Close(bool forced);
[email protected]e5ffd0e42009-09-11 21:30:56447
[email protected]35f7e5392012-07-27 19:54:50448 // Check whether the current thread is allowed to make IO calls, but only
449 // if database wasn't open in memory.
450 void AssertIOAllowed() { if (connection_) connection_->AssertIOAllowed(); }
451
[email protected]e5ffd0e42009-09-11 21:30:56452 private:
[email protected]877d55d2009-11-05 21:53:08453 friend class base::RefCounted<StatementRef>;
454
455 ~StatementRef();
456
[email protected]e5ffd0e42009-09-11 21:30:56457 Connection* connection_;
458 sqlite3_stmt* stmt_;
[email protected]41a97c812013-02-07 02:35:38459 bool was_valid_;
[email protected]e5ffd0e42009-09-11 21:30:56460
461 DISALLOW_COPY_AND_ASSIGN(StatementRef);
462 };
463 friend class StatementRef;
464
465 // Executes a rollback statement, ignoring all transaction state. Used
466 // internally in the transaction management code.
467 void DoRollback();
468
469 // Called by a StatementRef when it's being created or destroyed. See
470 // open_statements_ below.
471 void StatementRefCreated(StatementRef* ref);
472 void StatementRefDeleted(StatementRef* ref);
473
[email protected]faa604e2009-09-25 22:38:59474 // Called by Statement objects when an sqlite function returns an error.
475 // The return value is the error code reflected back to client code.
476 int OnSqliteError(int err, Statement* stmt);
477
[email protected]5b96f3772010-09-28 16:30:57478 // Like |Execute()|, but retries if the database is locked.
[email protected]9fe37552011-12-23 17:07:20479 bool ExecuteWithTimeout(const char* sql, base::TimeDelta ms_timeout)
480 WARN_UNUSED_RESULT;
[email protected]5b96f3772010-09-28 16:30:57481
[email protected]2eec0a22012-07-24 01:59:58482 // Internal helper for const functions. Like GetUniqueStatement(),
483 // except the statement is not entered into open_statements_,
484 // allowing this function to be const. Open statements can block
485 // closing the database, so only use in cases where the last ref is
486 // released before close could be called (which should always be the
487 // case for const functions).
488 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const;
489
[email protected]e5ffd0e42009-09-11 21:30:56490 // The actual sqlite database. Will be NULL before Init has been called or if
491 // Init resulted in an error.
492 sqlite3* db_;
493
494 // Parameters we'll configure in sqlite before doing anything else. Zero means
495 // use the default value.
496 int page_size_;
497 int cache_size_;
498 bool exclusive_locking_;
499
500 // All cached statements. Keeping a reference to these statements means that
501 // they'll remain active.
502 typedef std::map<StatementID, scoped_refptr<StatementRef> >
503 CachedStatementMap;
504 CachedStatementMap statement_cache_;
505
506 // A list of all StatementRefs we've given out. Each ref must register with
507 // us when it's created or destroyed. This allows us to potentially close
508 // any open statements when we encounter an error.
509 typedef std::set<StatementRef*> StatementRefSet;
510 StatementRefSet open_statements_;
511
512 // Number of currently-nested transactions.
513 int transaction_nesting_;
514
515 // True if any of the currently nested transactions have been rolled back.
516 // When we get to the outermost transaction, this will determine if we do
517 // a rollback instead of a commit.
518 bool needs_rollback_;
519
[email protected]35f7e5392012-07-27 19:54:50520 // True if database is open with OpenInMemory(), False if database is open
521 // with Open().
522 bool in_memory_;
523
[email protected]41a97c812013-02-07 02:35:38524 // |true| if the connection was closed using RazeAndClose(). Used
525 // to enable diagnostics to distinguish calls to never-opened
526 // databases (incorrect use of the API) from calls to once-valid
527 // databases.
528 bool poisoned_;
529
[email protected]c3881b372013-05-17 08:39:46530 ErrorCallback error_callback_;
531
[email protected]faa604e2009-09-25 22:38:59532 // This object handles errors resulting from all forms of executing sqlite
533 // commands or statements. It can be null which means default handling.
[email protected]49dc4f22012-10-17 17:41:16534 scoped_ptr<ErrorDelegate> error_delegate_;
[email protected]faa604e2009-09-25 22:38:59535
[email protected]210ce0af2013-05-15 09:10:39536 // Tag for auxiliary histograms.
537 std::string histogram_tag_;
[email protected]c088e3a32013-01-03 23:59:14538
[email protected]e5ffd0e42009-09-11 21:30:56539 DISALLOW_COPY_AND_ASSIGN(Connection);
540};
541
542} // namespace sql
543
[email protected]f0a54b22011-07-19 18:40:21544#endif // SQL_CONNECTION_H_