blob: 96312c47b99d37846ac3617770a6d5dae318595d [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]e5ffd0e42009-09-11 21:30:5611
12#include "base/basictypes.h"
[email protected]c3881b372013-05-17 08:39:4613#include "base/callback.h"
[email protected]9fe37552011-12-23 17:07:2014#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/ref_counted.h"
[email protected]49dc4f22012-10-17 17:41:1616#include "base/memory/scoped_ptr.h"
[email protected]35f7e5392012-07-27 19:54:5017#include "base/threading/thread_restrictions.h"
[email protected]5b96f3772010-09-28 16:30:5718#include "base/time.h"
[email protected]d4526962011-11-10 21:40:2819#include "sql/sql_export.h"
[email protected]e5ffd0e42009-09-11 21:30:5620
[email protected]e5ffd0e42009-09-11 21:30:5621struct sqlite3;
22struct sqlite3_stmt;
23
[email protected]a3ef4832013-02-02 05:12:3324namespace base {
25class FilePath;
26}
27
[email protected]e5ffd0e42009-09-11 21:30:5628namespace sql {
29
30class Statement;
31
32// Uniquely identifies a statement. There are two modes of operation:
33//
34// - In the most common mode, you will use the source file and line number to
35// identify your statement. This is a convienient way to get uniqueness for
36// a statement that is only used in one place. Use the SQL_FROM_HERE macro
37// to generate a StatementID.
38//
39// - In the "custom" mode you may use the statement from different places or
40// need to manage it yourself for whatever reason. In this case, you should
41// make up your own unique name and pass it to the StatementID. This name
42// must be a static string, since this object only deals with pointers and
43// assumes the underlying string doesn't change or get deleted.
44//
45// This object is copyable and assignable using the compiler-generated
46// operator= and copy constructor.
47class StatementID {
48 public:
49 // Creates a uniquely named statement with the given file ane line number.
50 // Normally you will use SQL_FROM_HERE instead of calling yourself.
51 StatementID(const char* file, int line)
52 : number_(line),
53 str_(file) {
54 }
55
56 // Creates a uniquely named statement with the given user-defined name.
57 explicit StatementID(const char* unique_name)
58 : number_(-1),
59 str_(unique_name) {
60 }
61
62 // This constructor is unimplemented and will generate a linker error if
63 // called. It is intended to try to catch people dynamically generating
64 // a statement name that will be deallocated and will cause a crash later.
65 // All strings must be static and unchanging!
66 explicit StatementID(const std::string& dont_ever_do_this);
67
68 // We need this to insert into our map.
69 bool operator<(const StatementID& other) const;
70
71 private:
72 int number_;
73 const char* str_;
74};
75
76#define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__)
77
[email protected]faa604e2009-09-25 22:38:5978class Connection;
79
80// ErrorDelegate defines the interface to implement error handling and recovery
81// for sqlite operations. This allows the rest of the classes to return true or
82// false while the actual error code and causing statement are delivered using
83// the OnError() callback.
84// The tipical usage is to centralize the code designed to handle database
85// corruption, low-level IO errors or locking violations.
[email protected]49dc4f22012-10-17 17:41:1686class SQL_EXPORT ErrorDelegate {
[email protected]faa604e2009-09-25 22:38:5987 public:
[email protected]49dc4f22012-10-17 17:41:1688 virtual ~ErrorDelegate();
[email protected]d4799a32010-09-28 22:54:5889
[email protected]0d04ede2012-10-18 04:31:5390 // |error| is an sqlite result code as seen in sqlite3.h. |connection| is the
91 // db connection where the error happened and |stmt| is our best guess at the
92 // statement that triggered the error. Do not store these pointers.
[email protected]765b44502009-10-02 05:01:4293 //
94 // |stmt| MAY BE NULL if there is no statement causing the problem (i.e. on
95 // initialization).
96 //
[email protected]0d04ede2012-10-18 04:31:5397 // If the error condition has been fixed and the original statement succesfuly
98 // re-tried then returning SQLITE_OK is appropriate; otherwise it is
99 // recommended that you return the original |error| or the appropriate error
100 // code.
[email protected]faa604e2009-09-25 22:38:59101 virtual int OnError(int error, Connection* connection, Statement* stmt) = 0;
102};
103
[email protected]d4526962011-11-10 21:40:28104class SQL_EXPORT Connection {
[email protected]e5ffd0e42009-09-11 21:30:56105 private:
106 class StatementRef; // Forward declaration, see real one below.
107
108 public:
[email protected]765b44502009-10-02 05:01:42109 // The database is opened by calling Open[InMemory](). Any uncommitted
110 // transactions will be rolled back when this object is deleted.
[email protected]e5ffd0e42009-09-11 21:30:56111 Connection();
112 ~Connection();
113
114 // Pre-init configuration ----------------------------------------------------
115
[email protected]765b44502009-10-02 05:01:42116 // Sets the page size that will be used when creating a new database. This
[email protected]e5ffd0e42009-09-11 21:30:56117 // must be called before Init(), and will only have an effect on new
118 // databases.
119 //
120 // From sqlite.org: "The page size must be a power of two greater than or
121 // equal to 512 and less than or equal to SQLITE_MAX_PAGE_SIZE. The maximum
122 // value for SQLITE_MAX_PAGE_SIZE is 32768."
123 void set_page_size(int page_size) { page_size_ = page_size; }
124
125 // Sets the number of pages that will be cached in memory by sqlite. The
126 // total cache size in bytes will be page_size * cache_size. This must be
[email protected]765b44502009-10-02 05:01:42127 // called before Open() to have an effect.
[email protected]e5ffd0e42009-09-11 21:30:56128 void set_cache_size(int cache_size) { cache_size_ = cache_size; }
129
130 // Call to put the database in exclusive locking mode. There is no "back to
131 // normal" flag because of some additional requirements sqlite puts on this
132 // transaition (requires another access to the DB) and because we don't
133 // actually need it.
134 //
135 // Exclusive mode means that the database is not unlocked at the end of each
136 // transaction, which means there may be less time spent initializing the
137 // next transaction because it doesn't have to re-aquire locks.
138 //
[email protected]765b44502009-10-02 05:01:42139 // This must be called before Open() to have an effect.
[email protected]e5ffd0e42009-09-11 21:30:56140 void set_exclusive_locking() { exclusive_locking_ = true; }
141
[email protected]c3881b372013-05-17 08:39:46142 // Set an error-handling callback. On errors, the error number (and
143 // statement, if available) will be passed to the callback.
144 //
145 // If no callback is set, the default action is to crash in debug
146 // mode or return failure in release mode.
147 //
148 // TODO(shess): ErrorDelegate allowed for returning a different
149 // error. Determine if this is necessary for the callback. In my
150 // experience, this is not well-tested and probably not safe, and
151 // current clients always return the same error passed.
152 // Additionally, most errors don't admit to a clean way to retry the
153 // failed operation, so converting an error to SQLITE_OK is probably
154 // not feasible.
155 typedef base::Callback<void(int, Statement*)> ErrorCallback;
156 void set_error_callback(const ErrorCallback& callback) {
157 error_callback_ = callback;
158 }
159 void reset_error_callback() {
160 error_callback_.Reset();
161 }
162
[email protected]faa604e2009-09-25 22:38:59163 // Sets the object that will handle errors. Recomended that it should be set
[email protected]765b44502009-10-02 05:01:42164 // before calling Open(). If not set, the default is to ignore errors on
[email protected]faa604e2009-09-25 22:38:59165 // release and assert on debug builds.
[email protected]49dc4f22012-10-17 17:41:16166 // Takes ownership of |delegate|.
[email protected]c3881b372013-05-17 08:39:46167 // NOTE(shess): Deprecated, use set_error_callback().
[email protected]faa604e2009-09-25 22:38:59168 void set_error_delegate(ErrorDelegate* delegate) {
[email protected]49dc4f22012-10-17 17:41:16169 error_delegate_.reset(delegate);
[email protected]faa604e2009-09-25 22:38:59170 }
171
[email protected]210ce0af2013-05-15 09:10:39172 // Set this tag to enable additional connection-type histogramming
173 // for SQLite error codes and database version numbers.
174 void set_histogram_tag(const std::string& tag) {
175 histogram_tag_ = tag;
[email protected]c088e3a32013-01-03 23:59:14176 }
177
[email protected]210ce0af2013-05-15 09:10:39178 // Record a sparse UMA histogram sample under
179 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no
180 // histogram is recorded.
181 void AddTaggedHistogram(const std::string& name, size_t sample) const;
182
[email protected]e5ffd0e42009-09-11 21:30:56183 // Initialization ------------------------------------------------------------
184
185 // Initializes the SQL connection for the given file, returning true if the
[email protected]35f2094c2009-12-29 22:46:55186 // file could be opened. You can call this or OpenInMemory.
[email protected]a3ef4832013-02-02 05:12:33187 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42188
189 // Initializes the SQL connection for a temporary in-memory database. There
190 // will be no associated file on disk, and the initial database will be
[email protected]35f2094c2009-12-29 22:46:55191 // empty. You can call this or Open.
[email protected]9fe37552011-12-23 17:07:20192 bool OpenInMemory() WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42193
[email protected]41a97c812013-02-07 02:35:38194 // Returns true if the database has been successfully opened.
[email protected]765b44502009-10-02 05:01:42195 bool is_open() const { return !!db_; }
[email protected]e5ffd0e42009-09-11 21:30:56196
197 // Closes the database. This is automatically performed on destruction for
198 // you, but this allows you to close the database early. You must not call
199 // any other functions after closing it. It is permissable to call Close on
200 // an uninitialized or already-closed database.
201 void Close();
202
203 // Pre-loads the first <cache-size> pages into the cache from the file.
204 // If you expect to soon use a substantial portion of the database, this
205 // is much more efficient than allowing the pages to be populated organically
206 // since there is no per-page hard drive seeking. If the file is larger than
207 // the cache, the last part that doesn't fit in the cache will be brought in
208 // organically.
209 //
210 // This function assumes your class is using a meta table on the current
211 // database, as it openes a transaction on the meta table to force the
212 // database to be initialized. You should feel free to initialize the meta
213 // table after calling preload since the meta table will already be in the
214 // database if it exists, and if it doesn't exist, the database won't
215 // generally exist either.
216 void Preload();
217
[email protected]8e0c01282012-04-06 19:36:49218 // Raze the database to the ground. This approximates creating a
219 // fresh database from scratch, within the constraints of SQLite's
220 // locking protocol (locks and open handles can make doing this with
221 // filesystem operations problematic). Returns true if the database
222 // was razed.
223 //
224 // false is returned if the database is locked by some other
225 // process. RazeWithTimeout() may be used if appropriate.
226 //
227 // NOTE(shess): Raze() will DCHECK in the following situations:
228 // - database is not open.
229 // - the connection has a transaction open.
230 // - a SQLite issue occurs which is structural in nature (like the
231 // statements used are broken).
232 // Since Raze() is expected to be called in unexpected situations,
233 // these all return false, since it is unlikely that the caller
234 // could fix them.
[email protected]6d42f152012-11-10 00:38:24235 //
236 // The database's page size is taken from |page_size_|. The
237 // existing database's |auto_vacuum| setting is lost (the
238 // possibility of corruption makes it unreliable to pull it from the
239 // existing database). To re-enable on the empty database requires
240 // running "PRAGMA auto_vacuum = 1;" then "VACUUM".
241 //
242 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1,
243 // so Raze() sets auto_vacuum to 1.
244 //
245 // TODO(shess): Raze() needs a connection so cannot clear SQLITE_NOTADB.
246 // TODO(shess): Bake auto_vacuum into Connection's API so it can
247 // just pick up the default.
[email protected]8e0c01282012-04-06 19:36:49248 bool Raze();
249 bool RazeWithTimout(base::TimeDelta timeout);
250
[email protected]41a97c812013-02-07 02:35:38251 // Breaks all outstanding transactions (as initiated by
252 // BeginTransaction()), calls Raze() to destroy the database, then
253 // closes the database. After this is called, any operations
254 // against the connections (or statements prepared by the
255 // connection) should fail safely.
256 //
257 // The value from Raze() is returned, with Close() called in all
258 // cases.
259 bool RazeAndClose();
260
[email protected]e5ffd0e42009-09-11 21:30:56261 // Transactions --------------------------------------------------------------
262
263 // Transaction management. We maintain a virtual transaction stack to emulate
264 // nested transactions since sqlite can't do nested transactions. The
265 // limitation is you can't roll back a sub transaction: if any transaction
266 // fails, all transactions open will also be rolled back. Any nested
267 // transactions after one has rolled back will return fail for Begin(). If
268 // Begin() fails, you must not call Commit or Rollback().
269 //
270 // Normally you should use sql::Transaction to manage a transaction, which
271 // will scope it to a C++ context.
272 bool BeginTransaction();
273 void RollbackTransaction();
274 bool CommitTransaction();
275
276 // Returns the current transaction nesting, which will be 0 if there are
277 // no open transactions.
278 int transaction_nesting() const { return transaction_nesting_; }
279
280 // Statements ----------------------------------------------------------------
281
282 // Executes the given SQL string, returning true on success. This is
283 // normally used for simple, 1-off statements that don't take any bound
284 // parameters and don't return any data (e.g. CREATE TABLE).
[email protected]9fe37552011-12-23 17:07:20285 //
[email protected]eff1fa522011-12-12 23:50:59286 // This will DCHECK if the |sql| contains errors.
[email protected]9fe37552011-12-23 17:07:20287 //
288 // Do not use ignore_result() to ignore all errors. Use
289 // ExecuteAndReturnErrorCode() and ignore only specific errors.
290 bool Execute(const char* sql) WARN_UNUSED_RESULT;
[email protected]e5ffd0e42009-09-11 21:30:56291
[email protected]eff1fa522011-12-12 23:50:59292 // Like Execute(), but returns the error code given by SQLite.
[email protected]9fe37552011-12-23 17:07:20293 int ExecuteAndReturnErrorCode(const char* sql) WARN_UNUSED_RESULT;
[email protected]eff1fa522011-12-12 23:50:59294
[email protected]e5ffd0e42009-09-11 21:30:56295 // Returns true if we have a statement with the given identifier already
296 // cached. This is normally not necessary to call, but can be useful if the
297 // caller has to dynamically build up SQL to avoid doing so if it's already
298 // cached.
299 bool HasCachedStatement(const StatementID& id) const;
300
301 // Returns a statement for the given SQL using the statement cache. It can
302 // take a nontrivial amount of work to parse and compile a statement, so
303 // keeping commonly-used ones around for future use is important for
304 // performance.
305 //
[email protected]eff1fa522011-12-12 23:50:59306 // If the |sql| has an error, an invalid, inert StatementRef is returned (and
307 // the code will crash in debug). The caller must deal with this eventuality,
308 // either by checking validity of the |sql| before calling, by correctly
309 // handling the return of an inert statement, or both.
[email protected]e5ffd0e42009-09-11 21:30:56310 //
311 // The StatementID and the SQL must always correspond to one-another. The
312 // ID is the lookup into the cache, so crazy things will happen if you use
313 // different SQL with the same ID.
314 //
315 // You will normally use the SQL_FROM_HERE macro to generate a statement
316 // ID associated with the current line of code. This gives uniqueness without
317 // you having to manage unique names. See StatementID above for more.
318 //
319 // Example:
[email protected]3273dce2010-01-27 16:08:08320 // sql::Statement stmt(connection_.GetCachedStatement(
321 // SQL_FROM_HERE, "SELECT * FROM foo"));
[email protected]e5ffd0e42009-09-11 21:30:56322 // if (!stmt)
323 // return false; // Error creating statement.
324 scoped_refptr<StatementRef> GetCachedStatement(const StatementID& id,
325 const char* sql);
326
[email protected]eff1fa522011-12-12 23:50:59327 // Used to check a |sql| statement for syntactic validity. If the statement is
328 // valid SQL, returns true.
329 bool IsSQLValid(const char* sql);
330
[email protected]e5ffd0e42009-09-11 21:30:56331 // Returns a non-cached statement for the given SQL. Use this for SQL that
332 // is only executed once or only rarely (there is overhead associated with
333 // keeping a statement cached).
334 //
335 // See GetCachedStatement above for examples and error information.
336 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql);
337
338 // Info querying -------------------------------------------------------------
339
340 // Returns true if the given table exists.
[email protected]765b44502009-10-02 05:01:42341 bool DoesTableExist(const char* table_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56342
[email protected]e2cadec82011-12-13 02:00:53343 // Returns true if the given index exists.
344 bool DoesIndexExist(const char* index_name) const;
345
[email protected]e5ffd0e42009-09-11 21:30:56346 // Returns true if a column with the given name exists in the given table.
[email protected]1ed78a32009-09-15 20:24:17347 bool DoesColumnExist(const char* table_name, const char* column_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56348
349 // Returns sqlite's internal ID for the last inserted row. Valid only
350 // immediately after an insert.
351 int64 GetLastInsertRowId() const;
352
[email protected]1ed78a32009-09-15 20:24:17353 // Returns sqlite's count of the number of rows modified by the last
354 // statement executed. Will be 0 if no statement has executed or the database
355 // is closed.
356 int GetLastChangeCount() const;
357
[email protected]e5ffd0e42009-09-11 21:30:56358 // Errors --------------------------------------------------------------------
359
360 // Returns the error code associated with the last sqlite operation.
361 int GetErrorCode() const;
362
[email protected]767718e52010-09-21 23:18:49363 // Returns the errno associated with GetErrorCode(). See
364 // SQLITE_LAST_ERRNO in SQLite documentation.
365 int GetLastErrno() const;
366
[email protected]e5ffd0e42009-09-11 21:30:56367 // Returns a pointer to a statically allocated string associated with the
368 // last sqlite operation.
369 const char* GetErrorMessage() const;
370
371 private:
[email protected]eff1fa522011-12-12 23:50:59372 // Statement accesses StatementRef which we don't want to expose to everybody
[email protected]e5ffd0e42009-09-11 21:30:56373 // (they should go through Statement).
374 friend class Statement;
375
[email protected]765b44502009-10-02 05:01:42376 // Internal initialize function used by both Init and InitInMemory. The file
377 // name is always 8 bits since we want to use the 8-bit version of
378 // sqlite3_open. The string can also be sqlite's special ":memory:" string.
379 bool OpenInternal(const std::string& file_name);
380
[email protected]41a97c812013-02-07 02:35:38381 // Internal close function used by Close() and RazeAndClose().
382 // |forced| indicates that orderly-shutdown checks should not apply.
383 void CloseInternal(bool forced);
384
[email protected]35f7e5392012-07-27 19:54:50385 // Check whether the current thread is allowed to make IO calls, but only
386 // if database wasn't open in memory. Function is inlined to be a no-op in
387 // official build.
388 void AssertIOAllowed() {
389 if (!in_memory_)
390 base::ThreadRestrictions::AssertIOAllowed();
391 }
392
[email protected]e2cadec82011-12-13 02:00:53393 // Internal helper for DoesTableExist and DoesIndexExist.
394 bool DoesTableOrIndexExist(const char* name, const char* type) const;
395
[email protected]e5ffd0e42009-09-11 21:30:56396 // A StatementRef is a refcounted wrapper around a sqlite statement pointer.
397 // Refcounting allows us to give these statements out to sql::Statement
398 // objects while also optionally maintaining a cache of compiled statements
399 // by just keeping a refptr to these objects.
400 //
401 // A statement ref can be valid, in which case it can be used, or invalid to
402 // indicate that the statement hasn't been created yet, has an error, or has
403 // been destroyed.
404 //
405 // The Connection may revoke a StatementRef in some error cases, so callers
406 // should always check validity before using.
[email protected]601dc6a2011-11-12 01:14:23407 class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> {
[email protected]e5ffd0e42009-09-11 21:30:56408 public:
[email protected]41a97c812013-02-07 02:35:38409 // |connection| is the sql::Connection instance associated with
410 // the statement, and is used for tracking outstanding statements
411 // and for error handling. Set to NULL for invalid or untracked
412 // refs. |stmt| is the actual statement, and should only be NULL
413 // to create an invalid ref. |was_valid| indicates whether the
414 // statement should be considered valid for diagnistic purposes.
415 // |was_valid| can be true for NULL |stmt| if the connection has
416 // been forcibly closed by an error handler.
417 StatementRef(Connection* connection, sqlite3_stmt* stmt, bool was_valid);
[email protected]e5ffd0e42009-09-11 21:30:56418
419 // When true, the statement can be used.
420 bool is_valid() const { return !!stmt_; }
421
[email protected]41a97c812013-02-07 02:35:38422 // When true, the statement is either currently valid, or was
423 // previously valid but the connection was forcibly closed. Used
424 // for diagnostic checks.
425 bool was_valid() const { return was_valid_; }
426
[email protected]b4c363b2013-01-17 13:11:17427 // If we've not been linked to a connection, this will be NULL.
428 // TODO(shess): connection_ can be NULL in case of GetUntrackedStatement(),
429 // which prevents Statement::OnError() from forwarding errors.
[email protected]e5ffd0e42009-09-11 21:30:56430 Connection* connection() const { return connection_; }
431
432 // Returns the sqlite statement if any. If the statement is not active,
433 // this will return NULL.
434 sqlite3_stmt* stmt() const { return stmt_; }
435
436 // Destroys the compiled statement and marks it NULL. The statement will
[email protected]41a97c812013-02-07 02:35:38437 // no longer be active. |forced| is used to indicate if orderly-shutdown
438 // checks should apply (see Connection::RazeAndClose()).
439 void Close(bool forced);
[email protected]e5ffd0e42009-09-11 21:30:56440
[email protected]35f7e5392012-07-27 19:54:50441 // Check whether the current thread is allowed to make IO calls, but only
442 // if database wasn't open in memory.
443 void AssertIOAllowed() { if (connection_) connection_->AssertIOAllowed(); }
444
[email protected]e5ffd0e42009-09-11 21:30:56445 private:
[email protected]877d55d2009-11-05 21:53:08446 friend class base::RefCounted<StatementRef>;
447
448 ~StatementRef();
449
[email protected]e5ffd0e42009-09-11 21:30:56450 Connection* connection_;
451 sqlite3_stmt* stmt_;
[email protected]41a97c812013-02-07 02:35:38452 bool was_valid_;
[email protected]e5ffd0e42009-09-11 21:30:56453
454 DISALLOW_COPY_AND_ASSIGN(StatementRef);
455 };
456 friend class StatementRef;
457
458 // Executes a rollback statement, ignoring all transaction state. Used
459 // internally in the transaction management code.
460 void DoRollback();
461
462 // Called by a StatementRef when it's being created or destroyed. See
463 // open_statements_ below.
464 void StatementRefCreated(StatementRef* ref);
465 void StatementRefDeleted(StatementRef* ref);
466
[email protected]faa604e2009-09-25 22:38:59467 // Called by Statement objects when an sqlite function returns an error.
468 // The return value is the error code reflected back to client code.
469 int OnSqliteError(int err, Statement* stmt);
470
[email protected]5b96f3772010-09-28 16:30:57471 // Like |Execute()|, but retries if the database is locked.
[email protected]9fe37552011-12-23 17:07:20472 bool ExecuteWithTimeout(const char* sql, base::TimeDelta ms_timeout)
473 WARN_UNUSED_RESULT;
[email protected]5b96f3772010-09-28 16:30:57474
[email protected]2eec0a22012-07-24 01:59:58475 // Internal helper for const functions. Like GetUniqueStatement(),
476 // except the statement is not entered into open_statements_,
477 // allowing this function to be const. Open statements can block
478 // closing the database, so only use in cases where the last ref is
479 // released before close could be called (which should always be the
480 // case for const functions).
481 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const;
482
[email protected]e5ffd0e42009-09-11 21:30:56483 // The actual sqlite database. Will be NULL before Init has been called or if
484 // Init resulted in an error.
485 sqlite3* db_;
486
487 // Parameters we'll configure in sqlite before doing anything else. Zero means
488 // use the default value.
489 int page_size_;
490 int cache_size_;
491 bool exclusive_locking_;
492
493 // All cached statements. Keeping a reference to these statements means that
494 // they'll remain active.
495 typedef std::map<StatementID, scoped_refptr<StatementRef> >
496 CachedStatementMap;
497 CachedStatementMap statement_cache_;
498
499 // A list of all StatementRefs we've given out. Each ref must register with
500 // us when it's created or destroyed. This allows us to potentially close
501 // any open statements when we encounter an error.
502 typedef std::set<StatementRef*> StatementRefSet;
503 StatementRefSet open_statements_;
504
505 // Number of currently-nested transactions.
506 int transaction_nesting_;
507
508 // True if any of the currently nested transactions have been rolled back.
509 // When we get to the outermost transaction, this will determine if we do
510 // a rollback instead of a commit.
511 bool needs_rollback_;
512
[email protected]35f7e5392012-07-27 19:54:50513 // True if database is open with OpenInMemory(), False if database is open
514 // with Open().
515 bool in_memory_;
516
[email protected]41a97c812013-02-07 02:35:38517 // |true| if the connection was closed using RazeAndClose(). Used
518 // to enable diagnostics to distinguish calls to never-opened
519 // databases (incorrect use of the API) from calls to once-valid
520 // databases.
521 bool poisoned_;
522
[email protected]c3881b372013-05-17 08:39:46523 ErrorCallback error_callback_;
524
[email protected]faa604e2009-09-25 22:38:59525 // This object handles errors resulting from all forms of executing sqlite
526 // commands or statements. It can be null which means default handling.
[email protected]49dc4f22012-10-17 17:41:16527 scoped_ptr<ErrorDelegate> error_delegate_;
[email protected]faa604e2009-09-25 22:38:59528
[email protected]210ce0af2013-05-15 09:10:39529 // Tag for auxiliary histograms.
530 std::string histogram_tag_;
[email protected]c088e3a32013-01-03 23:59:14531
[email protected]e5ffd0e42009-09-11 21:30:56532 DISALLOW_COPY_AND_ASSIGN(Connection);
533};
534
535} // namespace sql
536
[email protected]f0a54b22011-07-19 18:40:21537#endif // SQL_CONNECTION_H_