blob: 3ee91efded007bddfb29e26c549bf871abcc99bc [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]2b59d682013-06-28 15:22:0319#include "base/time/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
[email protected]d4526962011-11-10 21:40:2881class SQL_EXPORT Connection {
[email protected]e5ffd0e42009-09-11 21:30:5682 private:
83 class StatementRef; // Forward declaration, see real one below.
84
85 public:
[email protected]765b44502009-10-02 05:01:4286 // The database is opened by calling Open[InMemory](). Any uncommitted
87 // transactions will be rolled back when this object is deleted.
[email protected]e5ffd0e42009-09-11 21:30:5688 Connection();
89 ~Connection();
90
91 // Pre-init configuration ----------------------------------------------------
92
[email protected]765b44502009-10-02 05:01:4293 // Sets the page size that will be used when creating a new database. This
[email protected]e5ffd0e42009-09-11 21:30:5694 // must be called before Init(), and will only have an effect on new
95 // databases.
96 //
97 // From sqlite.org: "The page size must be a power of two greater than or
98 // equal to 512 and less than or equal to SQLITE_MAX_PAGE_SIZE. The maximum
99 // value for SQLITE_MAX_PAGE_SIZE is 32768."
100 void set_page_size(int page_size) { page_size_ = page_size; }
101
102 // Sets the number of pages that will be cached in memory by sqlite. The
103 // total cache size in bytes will be page_size * cache_size. This must be
[email protected]765b44502009-10-02 05:01:42104 // called before Open() to have an effect.
[email protected]e5ffd0e42009-09-11 21:30:56105 void set_cache_size(int cache_size) { cache_size_ = cache_size; }
106
107 // Call to put the database in exclusive locking mode. There is no "back to
108 // normal" flag because of some additional requirements sqlite puts on this
109 // transaition (requires another access to the DB) and because we don't
110 // actually need it.
111 //
112 // Exclusive mode means that the database is not unlocked at the end of each
113 // transaction, which means there may be less time spent initializing the
114 // next transaction because it doesn't have to re-aquire locks.
115 //
[email protected]765b44502009-10-02 05:01:42116 // This must be called before Open() to have an effect.
[email protected]e5ffd0e42009-09-11 21:30:56117 void set_exclusive_locking() { exclusive_locking_ = true; }
118
[email protected]81a2a602013-07-17 19:10:36119 // Call to cause Open() to restrict access permissions of the
120 // database file to only the owner.
121 // TODO(shess): Currently only supported on OS_POSIX, is a noop on
122 // other platforms.
123 void set_restrict_to_user() { restrict_to_user_ = true; }
124
[email protected]c3881b372013-05-17 08:39:46125 // Set an error-handling callback. On errors, the error number (and
126 // statement, if available) will be passed to the callback.
127 //
128 // If no callback is set, the default action is to crash in debug
129 // mode or return failure in release mode.
[email protected]c3881b372013-05-17 08:39:46130 typedef base::Callback<void(int, Statement*)> ErrorCallback;
131 void set_error_callback(const ErrorCallback& callback) {
132 error_callback_ = callback;
133 }
[email protected]98cf3002013-07-12 01:38:56134 bool has_error_callback() const {
135 return !error_callback_.is_null();
136 }
[email protected]c3881b372013-05-17 08:39:46137 void reset_error_callback() {
138 error_callback_.Reset();
139 }
140
[email protected]210ce0af2013-05-15 09:10:39141 // Set this tag to enable additional connection-type histogramming
142 // for SQLite error codes and database version numbers.
143 void set_histogram_tag(const std::string& tag) {
144 histogram_tag_ = tag;
[email protected]c088e3a32013-01-03 23:59:14145 }
146
[email protected]210ce0af2013-05-15 09:10:39147 // Record a sparse UMA histogram sample under
148 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no
149 // histogram is recorded.
150 void AddTaggedHistogram(const std::string& name, size_t sample) const;
151
[email protected]80abf152013-05-22 12:42:42152 // Run "PRAGMA integrity_check" and post each line of results into
153 // |messages|. Returns the success of running the statement - per
154 // the SQLite documentation, if no errors are found the call should
155 // succeed, and a single value "ok" should be in messages.
156 bool IntegrityCheck(std::vector<std::string>* messages);
157
[email protected]e5ffd0e42009-09-11 21:30:56158 // Initialization ------------------------------------------------------------
159
160 // Initializes the SQL connection for the given file, returning true if the
[email protected]35f2094c2009-12-29 22:46:55161 // file could be opened. You can call this or OpenInMemory.
[email protected]a3ef4832013-02-02 05:12:33162 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42163
164 // Initializes the SQL connection for a temporary in-memory database. There
165 // will be no associated file on disk, and the initial database will be
[email protected]35f2094c2009-12-29 22:46:55166 // empty. You can call this or Open.
[email protected]9fe37552011-12-23 17:07:20167 bool OpenInMemory() WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42168
[email protected]41a97c812013-02-07 02:35:38169 // Returns true if the database has been successfully opened.
[email protected]765b44502009-10-02 05:01:42170 bool is_open() const { return !!db_; }
[email protected]e5ffd0e42009-09-11 21:30:56171
172 // Closes the database. This is automatically performed on destruction for
173 // you, but this allows you to close the database early. You must not call
174 // any other functions after closing it. It is permissable to call Close on
175 // an uninitialized or already-closed database.
176 void Close();
177
178 // Pre-loads the first <cache-size> pages into the cache from the file.
179 // If you expect to soon use a substantial portion of the database, this
180 // is much more efficient than allowing the pages to be populated organically
181 // since there is no per-page hard drive seeking. If the file is larger than
182 // the cache, the last part that doesn't fit in the cache will be brought in
183 // organically.
184 //
185 // This function assumes your class is using a meta table on the current
186 // database, as it openes a transaction on the meta table to force the
187 // database to be initialized. You should feel free to initialize the meta
188 // table after calling preload since the meta table will already be in the
189 // database if it exists, and if it doesn't exist, the database won't
190 // generally exist either.
191 void Preload();
192
[email protected]be7995f12013-07-18 18:49:14193 // Try to trim the cache memory used by the database. If |aggressively| is
194 // true, this function will try to free all of the cache memory it can. If
195 // |aggressively| is false, this function will try to cut cache memory
196 // usage by half.
197 void TrimMemory(bool aggressively);
198
[email protected]8e0c01282012-04-06 19:36:49199 // Raze the database to the ground. This approximates creating a
200 // fresh database from scratch, within the constraints of SQLite's
201 // locking protocol (locks and open handles can make doing this with
202 // filesystem operations problematic). Returns true if the database
203 // was razed.
204 //
205 // false is returned if the database is locked by some other
206 // process. RazeWithTimeout() may be used if appropriate.
207 //
208 // NOTE(shess): Raze() will DCHECK in the following situations:
209 // - database is not open.
210 // - the connection has a transaction open.
211 // - a SQLite issue occurs which is structural in nature (like the
212 // statements used are broken).
213 // Since Raze() is expected to be called in unexpected situations,
214 // these all return false, since it is unlikely that the caller
215 // could fix them.
[email protected]6d42f152012-11-10 00:38:24216 //
217 // The database's page size is taken from |page_size_|. The
218 // existing database's |auto_vacuum| setting is lost (the
219 // possibility of corruption makes it unreliable to pull it from the
220 // existing database). To re-enable on the empty database requires
221 // running "PRAGMA auto_vacuum = 1;" then "VACUUM".
222 //
223 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1,
224 // so Raze() sets auto_vacuum to 1.
225 //
226 // TODO(shess): Raze() needs a connection so cannot clear SQLITE_NOTADB.
227 // TODO(shess): Bake auto_vacuum into Connection's API so it can
228 // just pick up the default.
[email protected]8e0c01282012-04-06 19:36:49229 bool Raze();
230 bool RazeWithTimout(base::TimeDelta timeout);
231
[email protected]41a97c812013-02-07 02:35:38232 // Breaks all outstanding transactions (as initiated by
233 // BeginTransaction()), calls Raze() to destroy the database, then
234 // closes the database. After this is called, any operations
235 // against the connections (or statements prepared by the
236 // connection) should fail safely.
237 //
238 // The value from Raze() is returned, with Close() called in all
239 // cases.
240 bool RazeAndClose();
241
[email protected]8d2e39e2013-06-24 05:55:08242 // Delete the underlying database files associated with |path|.
243 // This should be used on a database which has no existing
244 // connections. If any other connections are open to the same
245 // database, this could cause odd results or corruption (for
246 // instance if a hot journal is deleted but the associated database
247 // is not).
248 //
249 // Returns true if the database file and associated journals no
250 // longer exist, false otherwise. If the database has never
251 // existed, this will return true.
252 static bool Delete(const base::FilePath& path);
253
[email protected]e5ffd0e42009-09-11 21:30:56254 // Transactions --------------------------------------------------------------
255
256 // Transaction management. We maintain a virtual transaction stack to emulate
257 // nested transactions since sqlite can't do nested transactions. The
258 // limitation is you can't roll back a sub transaction: if any transaction
259 // fails, all transactions open will also be rolled back. Any nested
260 // transactions after one has rolled back will return fail for Begin(). If
261 // Begin() fails, you must not call Commit or Rollback().
262 //
263 // Normally you should use sql::Transaction to manage a transaction, which
264 // will scope it to a C++ context.
265 bool BeginTransaction();
266 void RollbackTransaction();
267 bool CommitTransaction();
268
269 // Returns the current transaction nesting, which will be 0 if there are
270 // no open transactions.
271 int transaction_nesting() const { return transaction_nesting_; }
272
273 // Statements ----------------------------------------------------------------
274
275 // Executes the given SQL string, returning true on success. This is
276 // normally used for simple, 1-off statements that don't take any bound
277 // parameters and don't return any data (e.g. CREATE TABLE).
[email protected]9fe37552011-12-23 17:07:20278 //
[email protected]eff1fa522011-12-12 23:50:59279 // This will DCHECK if the |sql| contains errors.
[email protected]9fe37552011-12-23 17:07:20280 //
281 // Do not use ignore_result() to ignore all errors. Use
282 // ExecuteAndReturnErrorCode() and ignore only specific errors.
283 bool Execute(const char* sql) WARN_UNUSED_RESULT;
[email protected]e5ffd0e42009-09-11 21:30:56284
[email protected]eff1fa522011-12-12 23:50:59285 // Like Execute(), but returns the error code given by SQLite.
[email protected]9fe37552011-12-23 17:07:20286 int ExecuteAndReturnErrorCode(const char* sql) WARN_UNUSED_RESULT;
[email protected]eff1fa522011-12-12 23:50:59287
[email protected]e5ffd0e42009-09-11 21:30:56288 // Returns true if we have a statement with the given identifier already
289 // cached. This is normally not necessary to call, but can be useful if the
290 // caller has to dynamically build up SQL to avoid doing so if it's already
291 // cached.
292 bool HasCachedStatement(const StatementID& id) const;
293
294 // Returns a statement for the given SQL using the statement cache. It can
295 // take a nontrivial amount of work to parse and compile a statement, so
296 // keeping commonly-used ones around for future use is important for
297 // performance.
298 //
[email protected]eff1fa522011-12-12 23:50:59299 // If the |sql| has an error, an invalid, inert StatementRef is returned (and
300 // the code will crash in debug). The caller must deal with this eventuality,
301 // either by checking validity of the |sql| before calling, by correctly
302 // handling the return of an inert statement, or both.
[email protected]e5ffd0e42009-09-11 21:30:56303 //
304 // The StatementID and the SQL must always correspond to one-another. The
305 // ID is the lookup into the cache, so crazy things will happen if you use
306 // different SQL with the same ID.
307 //
308 // You will normally use the SQL_FROM_HERE macro to generate a statement
309 // ID associated with the current line of code. This gives uniqueness without
310 // you having to manage unique names. See StatementID above for more.
311 //
312 // Example:
[email protected]3273dce2010-01-27 16:08:08313 // sql::Statement stmt(connection_.GetCachedStatement(
314 // SQL_FROM_HERE, "SELECT * FROM foo"));
[email protected]e5ffd0e42009-09-11 21:30:56315 // if (!stmt)
316 // return false; // Error creating statement.
317 scoped_refptr<StatementRef> GetCachedStatement(const StatementID& id,
318 const char* sql);
319
[email protected]eff1fa522011-12-12 23:50:59320 // Used to check a |sql| statement for syntactic validity. If the statement is
321 // valid SQL, returns true.
322 bool IsSQLValid(const char* sql);
323
[email protected]e5ffd0e42009-09-11 21:30:56324 // Returns a non-cached statement for the given SQL. Use this for SQL that
325 // is only executed once or only rarely (there is overhead associated with
326 // keeping a statement cached).
327 //
328 // See GetCachedStatement above for examples and error information.
329 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql);
330
331 // Info querying -------------------------------------------------------------
332
333 // Returns true if the given table exists.
[email protected]765b44502009-10-02 05:01:42334 bool DoesTableExist(const char* table_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56335
[email protected]e2cadec82011-12-13 02:00:53336 // Returns true if the given index exists.
337 bool DoesIndexExist(const char* index_name) const;
338
[email protected]e5ffd0e42009-09-11 21:30:56339 // Returns true if a column with the given name exists in the given table.
[email protected]1ed78a32009-09-15 20:24:17340 bool DoesColumnExist(const char* table_name, const char* column_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56341
342 // Returns sqlite's internal ID for the last inserted row. Valid only
343 // immediately after an insert.
344 int64 GetLastInsertRowId() const;
345
[email protected]1ed78a32009-09-15 20:24:17346 // Returns sqlite's count of the number of rows modified by the last
347 // statement executed. Will be 0 if no statement has executed or the database
348 // is closed.
349 int GetLastChangeCount() const;
350
[email protected]e5ffd0e42009-09-11 21:30:56351 // Errors --------------------------------------------------------------------
352
353 // Returns the error code associated with the last sqlite operation.
354 int GetErrorCode() const;
355
[email protected]767718e52010-09-21 23:18:49356 // Returns the errno associated with GetErrorCode(). See
357 // SQLITE_LAST_ERRNO in SQLite documentation.
358 int GetLastErrno() const;
359
[email protected]e5ffd0e42009-09-11 21:30:56360 // Returns a pointer to a statically allocated string associated with the
361 // last sqlite operation.
362 const char* GetErrorMessage() const;
363
364 private:
[email protected]4350e322013-06-18 22:18:10365 // Allow test-support code to set/reset error ignorer.
366 friend class ScopedErrorIgnorer;
367
[email protected]eff1fa522011-12-12 23:50:59368 // Statement accesses StatementRef which we don't want to expose to everybody
[email protected]e5ffd0e42009-09-11 21:30:56369 // (they should go through Statement).
370 friend class Statement;
371
[email protected]765b44502009-10-02 05:01:42372 // Internal initialize function used by both Init and InitInMemory. The file
373 // name is always 8 bits since we want to use the 8-bit version of
374 // sqlite3_open. The string can also be sqlite's special ":memory:" string.
[email protected]fed734a2013-07-17 04:45:13375 //
376 // |retry_flag| controls retrying the open if the error callback
377 // addressed errors using RazeAndClose().
378 enum Retry {
379 NO_RETRY = 0,
380 RETRY_ON_POISON
381 };
382 bool OpenInternal(const std::string& file_name, Retry retry_flag);
[email protected]765b44502009-10-02 05:01:42383
[email protected]41a97c812013-02-07 02:35:38384 // Internal close function used by Close() and RazeAndClose().
385 // |forced| indicates that orderly-shutdown checks should not apply.
386 void CloseInternal(bool forced);
387
[email protected]35f7e5392012-07-27 19:54:50388 // Check whether the current thread is allowed to make IO calls, but only
389 // if database wasn't open in memory. Function is inlined to be a no-op in
390 // official build.
391 void AssertIOAllowed() {
392 if (!in_memory_)
393 base::ThreadRestrictions::AssertIOAllowed();
394 }
395
[email protected]e2cadec82011-12-13 02:00:53396 // Internal helper for DoesTableExist and DoesIndexExist.
397 bool DoesTableOrIndexExist(const char* name, const char* type) const;
398
[email protected]4350e322013-06-18 22:18:10399 // Accessors for global error-ignorer, for injecting behavior during tests.
400 // See test/scoped_error_ignorer.h.
401 typedef base::Callback<bool(int)> ErrorIgnorerCallback;
402 static ErrorIgnorerCallback* current_ignorer_cb_;
403 static bool ShouldIgnore(int error);
404 static void SetErrorIgnorer(ErrorIgnorerCallback* ignorer);
405 static void ResetErrorIgnorer();
406
[email protected]e5ffd0e42009-09-11 21:30:56407 // A StatementRef is a refcounted wrapper around a sqlite statement pointer.
408 // Refcounting allows us to give these statements out to sql::Statement
409 // objects while also optionally maintaining a cache of compiled statements
410 // by just keeping a refptr to these objects.
411 //
412 // A statement ref can be valid, in which case it can be used, or invalid to
413 // indicate that the statement hasn't been created yet, has an error, or has
414 // been destroyed.
415 //
416 // The Connection may revoke a StatementRef in some error cases, so callers
417 // should always check validity before using.
[email protected]601dc6a2011-11-12 01:14:23418 class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> {
[email protected]e5ffd0e42009-09-11 21:30:56419 public:
[email protected]41a97c812013-02-07 02:35:38420 // |connection| is the sql::Connection instance associated with
421 // the statement, and is used for tracking outstanding statements
422 // and for error handling. Set to NULL for invalid or untracked
423 // refs. |stmt| is the actual statement, and should only be NULL
424 // to create an invalid ref. |was_valid| indicates whether the
425 // statement should be considered valid for diagnistic purposes.
426 // |was_valid| can be true for NULL |stmt| if the connection has
427 // been forcibly closed by an error handler.
428 StatementRef(Connection* connection, sqlite3_stmt* stmt, bool was_valid);
[email protected]e5ffd0e42009-09-11 21:30:56429
430 // When true, the statement can be used.
431 bool is_valid() const { return !!stmt_; }
432
[email protected]41a97c812013-02-07 02:35:38433 // When true, the statement is either currently valid, or was
434 // previously valid but the connection was forcibly closed. Used
435 // for diagnostic checks.
436 bool was_valid() const { return was_valid_; }
437
[email protected]b4c363b2013-01-17 13:11:17438 // If we've not been linked to a connection, this will be NULL.
439 // TODO(shess): connection_ can be NULL in case of GetUntrackedStatement(),
440 // which prevents Statement::OnError() from forwarding errors.
[email protected]e5ffd0e42009-09-11 21:30:56441 Connection* connection() const { return connection_; }
442
443 // Returns the sqlite statement if any. If the statement is not active,
444 // this will return NULL.
445 sqlite3_stmt* stmt() const { return stmt_; }
446
447 // Destroys the compiled statement and marks it NULL. The statement will
[email protected]41a97c812013-02-07 02:35:38448 // no longer be active. |forced| is used to indicate if orderly-shutdown
449 // checks should apply (see Connection::RazeAndClose()).
450 void Close(bool forced);
[email protected]e5ffd0e42009-09-11 21:30:56451
[email protected]35f7e5392012-07-27 19:54:50452 // Check whether the current thread is allowed to make IO calls, but only
453 // if database wasn't open in memory.
454 void AssertIOAllowed() { if (connection_) connection_->AssertIOAllowed(); }
455
[email protected]e5ffd0e42009-09-11 21:30:56456 private:
[email protected]877d55d2009-11-05 21:53:08457 friend class base::RefCounted<StatementRef>;
458
459 ~StatementRef();
460
[email protected]e5ffd0e42009-09-11 21:30:56461 Connection* connection_;
462 sqlite3_stmt* stmt_;
[email protected]41a97c812013-02-07 02:35:38463 bool was_valid_;
[email protected]e5ffd0e42009-09-11 21:30:56464
465 DISALLOW_COPY_AND_ASSIGN(StatementRef);
466 };
467 friend class StatementRef;
468
469 // Executes a rollback statement, ignoring all transaction state. Used
470 // internally in the transaction management code.
471 void DoRollback();
472
473 // Called by a StatementRef when it's being created or destroyed. See
474 // open_statements_ below.
475 void StatementRefCreated(StatementRef* ref);
476 void StatementRefDeleted(StatementRef* ref);
477
[email protected]faa604e2009-09-25 22:38:59478 // Called by Statement objects when an sqlite function returns an error.
479 // The return value is the error code reflected back to client code.
480 int OnSqliteError(int err, Statement* stmt);
481
[email protected]5b96f3772010-09-28 16:30:57482 // Like |Execute()|, but retries if the database is locked.
[email protected]9fe37552011-12-23 17:07:20483 bool ExecuteWithTimeout(const char* sql, base::TimeDelta ms_timeout)
484 WARN_UNUSED_RESULT;
[email protected]5b96f3772010-09-28 16:30:57485
[email protected]2eec0a22012-07-24 01:59:58486 // Internal helper for const functions. Like GetUniqueStatement(),
487 // except the statement is not entered into open_statements_,
488 // allowing this function to be const. Open statements can block
489 // closing the database, so only use in cases where the last ref is
490 // released before close could be called (which should always be the
491 // case for const functions).
492 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const;
493
[email protected]e5ffd0e42009-09-11 21:30:56494 // The actual sqlite database. Will be NULL before Init has been called or if
495 // Init resulted in an error.
496 sqlite3* db_;
497
498 // Parameters we'll configure in sqlite before doing anything else. Zero means
499 // use the default value.
500 int page_size_;
501 int cache_size_;
502 bool exclusive_locking_;
[email protected]81a2a602013-07-17 19:10:36503 bool restrict_to_user_;
[email protected]e5ffd0e42009-09-11 21:30:56504
505 // All cached statements. Keeping a reference to these statements means that
506 // they'll remain active.
507 typedef std::map<StatementID, scoped_refptr<StatementRef> >
508 CachedStatementMap;
509 CachedStatementMap statement_cache_;
510
511 // A list of all StatementRefs we've given out. Each ref must register with
512 // us when it's created or destroyed. This allows us to potentially close
513 // any open statements when we encounter an error.
514 typedef std::set<StatementRef*> StatementRefSet;
515 StatementRefSet open_statements_;
516
517 // Number of currently-nested transactions.
518 int transaction_nesting_;
519
520 // True if any of the currently nested transactions have been rolled back.
521 // When we get to the outermost transaction, this will determine if we do
522 // a rollback instead of a commit.
523 bool needs_rollback_;
524
[email protected]35f7e5392012-07-27 19:54:50525 // True if database is open with OpenInMemory(), False if database is open
526 // with Open().
527 bool in_memory_;
528
[email protected]41a97c812013-02-07 02:35:38529 // |true| if the connection was closed using RazeAndClose(). Used
530 // to enable diagnostics to distinguish calls to never-opened
531 // databases (incorrect use of the API) from calls to once-valid
532 // databases.
533 bool poisoned_;
534
[email protected]c3881b372013-05-17 08:39:46535 ErrorCallback error_callback_;
536
[email protected]210ce0af2013-05-15 09:10:39537 // Tag for auxiliary histograms.
538 std::string histogram_tag_;
[email protected]c088e3a32013-01-03 23:59:14539
[email protected]e5ffd0e42009-09-11 21:30:56540 DISALLOW_COPY_AND_ASSIGN(Connection);
541};
542
543} // namespace sql
544
[email protected]f0a54b22011-07-19 18:40:21545#endif // SQL_CONNECTION_H_