blob: 5bbdb972d63ffcc6a0d3e74615b4e2d6b6474761 [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
[email protected]8d409412013-07-19 18:25:3031class Recovery;
[email protected]e5ffd0e42009-09-11 21:30:5632class Statement;
33
34// Uniquely identifies a statement. There are two modes of operation:
35//
36// - In the most common mode, you will use the source file and line number to
37// identify your statement. This is a convienient way to get uniqueness for
38// a statement that is only used in one place. Use the SQL_FROM_HERE macro
39// to generate a StatementID.
40//
41// - In the "custom" mode you may use the statement from different places or
42// need to manage it yourself for whatever reason. In this case, you should
43// make up your own unique name and pass it to the StatementID. This name
44// must be a static string, since this object only deals with pointers and
45// assumes the underlying string doesn't change or get deleted.
46//
47// This object is copyable and assignable using the compiler-generated
48// operator= and copy constructor.
49class StatementID {
50 public:
51 // Creates a uniquely named statement with the given file ane line number.
52 // Normally you will use SQL_FROM_HERE instead of calling yourself.
53 StatementID(const char* file, int line)
54 : number_(line),
55 str_(file) {
56 }
57
58 // Creates a uniquely named statement with the given user-defined name.
59 explicit StatementID(const char* unique_name)
60 : number_(-1),
61 str_(unique_name) {
62 }
63
64 // This constructor is unimplemented and will generate a linker error if
65 // called. It is intended to try to catch people dynamically generating
66 // a statement name that will be deallocated and will cause a crash later.
67 // All strings must be static and unchanging!
68 explicit StatementID(const std::string& dont_ever_do_this);
69
70 // We need this to insert into our map.
71 bool operator<(const StatementID& other) const;
72
73 private:
74 int number_;
75 const char* str_;
76};
77
78#define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__)
79
[email protected]faa604e2009-09-25 22:38:5980class Connection;
81
[email protected]d4526962011-11-10 21:40:2882class SQL_EXPORT Connection {
[email protected]e5ffd0e42009-09-11 21:30:5683 private:
84 class StatementRef; // Forward declaration, see real one below.
85
86 public:
[email protected]765b44502009-10-02 05:01:4287 // The database is opened by calling Open[InMemory](). Any uncommitted
88 // transactions will be rolled back when this object is deleted.
[email protected]e5ffd0e42009-09-11 21:30:5689 Connection();
90 ~Connection();
91
92 // Pre-init configuration ----------------------------------------------------
93
[email protected]765b44502009-10-02 05:01:4294 // Sets the page size that will be used when creating a new database. This
[email protected]e5ffd0e42009-09-11 21:30:5695 // must be called before Init(), and will only have an effect on new
96 // databases.
97 //
98 // From sqlite.org: "The page size must be a power of two greater than or
99 // equal to 512 and less than or equal to SQLITE_MAX_PAGE_SIZE. The maximum
100 // value for SQLITE_MAX_PAGE_SIZE is 32768."
101 void set_page_size(int page_size) { page_size_ = page_size; }
102
103 // Sets the number of pages that will be cached in memory by sqlite. The
104 // total cache size in bytes will be page_size * cache_size. This must be
[email protected]765b44502009-10-02 05:01:42105 // called before Open() to have an effect.
[email protected]e5ffd0e42009-09-11 21:30:56106 void set_cache_size(int cache_size) { cache_size_ = cache_size; }
107
108 // Call to put the database in exclusive locking mode. There is no "back to
109 // normal" flag because of some additional requirements sqlite puts on this
[email protected]4ab952f2014-04-01 20:18:16110 // transaction (requires another access to the DB) and because we don't
[email protected]e5ffd0e42009-09-11 21:30:56111 // actually need it.
112 //
113 // Exclusive mode means that the database is not unlocked at the end of each
114 // transaction, which means there may be less time spent initializing the
115 // next transaction because it doesn't have to re-aquire locks.
116 //
[email protected]765b44502009-10-02 05:01:42117 // This must be called before Open() to have an effect.
[email protected]e5ffd0e42009-09-11 21:30:56118 void set_exclusive_locking() { exclusive_locking_ = true; }
119
[email protected]81a2a602013-07-17 19:10:36120 // Call to cause Open() to restrict access permissions of the
121 // database file to only the owner.
122 // TODO(shess): Currently only supported on OS_POSIX, is a noop on
123 // other platforms.
124 void set_restrict_to_user() { restrict_to_user_ = true; }
125
[email protected]c3881b372013-05-17 08:39:46126 // Set an error-handling callback. On errors, the error number (and
127 // statement, if available) will be passed to the callback.
128 //
129 // If no callback is set, the default action is to crash in debug
130 // mode or return failure in release mode.
[email protected]c3881b372013-05-17 08:39:46131 typedef base::Callback<void(int, Statement*)> ErrorCallback;
132 void set_error_callback(const ErrorCallback& callback) {
133 error_callback_ = callback;
134 }
[email protected]98cf3002013-07-12 01:38:56135 bool has_error_callback() const {
136 return !error_callback_.is_null();
137 }
[email protected]c3881b372013-05-17 08:39:46138 void reset_error_callback() {
139 error_callback_.Reset();
140 }
141
[email protected]210ce0af2013-05-15 09:10:39142 // Set this tag to enable additional connection-type histogramming
143 // for SQLite error codes and database version numbers.
144 void set_histogram_tag(const std::string& tag) {
145 histogram_tag_ = tag;
[email protected]c088e3a32013-01-03 23:59:14146 }
147
[email protected]210ce0af2013-05-15 09:10:39148 // Record a sparse UMA histogram sample under
149 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no
150 // histogram is recorded.
151 void AddTaggedHistogram(const std::string& name, size_t sample) const;
152
[email protected]579446c2013-12-16 18:36:52153 // Run "PRAGMA integrity_check" and post each line of
154 // results into |messages|. Returns the success of running the
155 // statement - per the SQLite documentation, if no errors are found the
156 // call should succeed, and a single value "ok" should be in messages.
157 bool FullIntegrityCheck(std::vector<std::string>* messages);
158
159 // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method,
160 // interprets the results returning true if the the statement executes
161 // without error and results in a single "ok" value.
162 bool QuickIntegrityCheck() WARN_UNUSED_RESULT;
[email protected]80abf152013-05-22 12:42:42163
[email protected]e5ffd0e42009-09-11 21:30:56164 // Initialization ------------------------------------------------------------
165
166 // Initializes the SQL connection for the given file, returning true if the
[email protected]35f2094c2009-12-29 22:46:55167 // file could be opened. You can call this or OpenInMemory.
[email protected]a3ef4832013-02-02 05:12:33168 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42169
170 // Initializes the SQL connection for a temporary in-memory database. There
171 // will be no associated file on disk, and the initial database will be
[email protected]35f2094c2009-12-29 22:46:55172 // empty. You can call this or Open.
[email protected]9fe37552011-12-23 17:07:20173 bool OpenInMemory() WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42174
[email protected]8d409412013-07-19 18:25:30175 // Create a temporary on-disk database. The database will be
176 // deleted after close. This kind of database is similar to
177 // OpenInMemory() for small databases, but can page to disk if the
178 // database becomes large.
179 bool OpenTemporary() WARN_UNUSED_RESULT;
180
[email protected]41a97c812013-02-07 02:35:38181 // Returns true if the database has been successfully opened.
[email protected]765b44502009-10-02 05:01:42182 bool is_open() const { return !!db_; }
[email protected]e5ffd0e42009-09-11 21:30:56183
184 // Closes the database. This is automatically performed on destruction for
185 // you, but this allows you to close the database early. You must not call
186 // any other functions after closing it. It is permissable to call Close on
187 // an uninitialized or already-closed database.
188 void Close();
189
[email protected]8ada10f2013-12-21 00:42:34190 // Reads the first <cache-size>*<page-size> bytes of the file to prime the
191 // filesystem cache. This can be more efficient than faulting pages
192 // individually. Since this involves blocking I/O, it should only be used if
193 // the caller will immediately read a substantial amount of data from the
194 // database.
[email protected]e5ffd0e42009-09-11 21:30:56195 //
[email protected]8ada10f2013-12-21 00:42:34196 // TODO(shess): Design a set of histograms or an experiment to inform this
197 // decision. Preloading should almost always improve later performance
198 // numbers for this database simply because it pulls operations forward, but
199 // if the data isn't actually used soon then preloading just slows down
200 // everything else.
[email protected]e5ffd0e42009-09-11 21:30:56201 void Preload();
202
[email protected]be7995f12013-07-18 18:49:14203 // Try to trim the cache memory used by the database. If |aggressively| is
204 // true, this function will try to free all of the cache memory it can. If
205 // |aggressively| is false, this function will try to cut cache memory
206 // usage by half.
207 void TrimMemory(bool aggressively);
208
[email protected]8e0c01282012-04-06 19:36:49209 // Raze the database to the ground. This approximates creating a
210 // fresh database from scratch, within the constraints of SQLite's
211 // locking protocol (locks and open handles can make doing this with
212 // filesystem operations problematic). Returns true if the database
213 // was razed.
214 //
215 // false is returned if the database is locked by some other
216 // process. RazeWithTimeout() may be used if appropriate.
217 //
218 // NOTE(shess): Raze() will DCHECK in the following situations:
219 // - database is not open.
220 // - the connection has a transaction open.
221 // - a SQLite issue occurs which is structural in nature (like the
222 // statements used are broken).
223 // Since Raze() is expected to be called in unexpected situations,
224 // these all return false, since it is unlikely that the caller
225 // could fix them.
[email protected]6d42f152012-11-10 00:38:24226 //
227 // The database's page size is taken from |page_size_|. The
228 // existing database's |auto_vacuum| setting is lost (the
229 // possibility of corruption makes it unreliable to pull it from the
230 // existing database). To re-enable on the empty database requires
231 // running "PRAGMA auto_vacuum = 1;" then "VACUUM".
232 //
233 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1,
234 // so Raze() sets auto_vacuum to 1.
235 //
236 // TODO(shess): Raze() needs a connection so cannot clear SQLITE_NOTADB.
237 // TODO(shess): Bake auto_vacuum into Connection's API so it can
238 // just pick up the default.
[email protected]8e0c01282012-04-06 19:36:49239 bool Raze();
240 bool RazeWithTimout(base::TimeDelta timeout);
241
[email protected]41a97c812013-02-07 02:35:38242 // Breaks all outstanding transactions (as initiated by
[email protected]8d409412013-07-19 18:25:30243 // BeginTransaction()), closes the SQLite database, and poisons the
244 // object so that all future operations against the Connection (or
245 // its Statements) fail safely, without side effects.
[email protected]41a97c812013-02-07 02:35:38246 //
[email protected]8d409412013-07-19 18:25:30247 // This is intended as an alternative to Close() in error callbacks.
248 // Close() should still be called at some point.
249 void Poison();
250
251 // Raze() the database and Poison() the handle. Returns the return
252 // value from Raze().
253 // TODO(shess): Rename to RazeAndPoison().
[email protected]41a97c812013-02-07 02:35:38254 bool RazeAndClose();
255
[email protected]8d2e39e2013-06-24 05:55:08256 // Delete the underlying database files associated with |path|.
257 // This should be used on a database which has no existing
258 // connections. If any other connections are open to the same
259 // database, this could cause odd results or corruption (for
260 // instance if a hot journal is deleted but the associated database
261 // is not).
262 //
263 // Returns true if the database file and associated journals no
264 // longer exist, false otherwise. If the database has never
265 // existed, this will return true.
266 static bool Delete(const base::FilePath& path);
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
[email protected]8d409412013-07-19 18:25:30283 // Rollback all outstanding transactions. Use with care, there may
284 // be scoped transactions on the stack.
285 void RollbackAllTransactions();
286
[email protected]e5ffd0e42009-09-11 21:30:56287 // Returns the current transaction nesting, which will be 0 if there are
288 // no open transactions.
289 int transaction_nesting() const { return transaction_nesting_; }
290
[email protected]8d409412013-07-19 18:25:30291 // Attached databases---------------------------------------------------------
292
293 // SQLite supports attaching multiple database files to a single
294 // handle. Attach the database in |other_db_path| to the current
295 // handle under |attachment_point|. |attachment_point| should only
296 // contain characters from [a-zA-Z0-9_].
297 //
298 // Note that calling attach or detach with an open transaction is an
299 // error.
300 bool AttachDatabase(const base::FilePath& other_db_path,
301 const char* attachment_point);
302 bool DetachDatabase(const char* attachment_point);
303
[email protected]e5ffd0e42009-09-11 21:30:56304 // Statements ----------------------------------------------------------------
305
306 // Executes the given SQL string, returning true on success. This is
307 // normally used for simple, 1-off statements that don't take any bound
308 // parameters and don't return any data (e.g. CREATE TABLE).
[email protected]9fe37552011-12-23 17:07:20309 //
[email protected]eff1fa522011-12-12 23:50:59310 // This will DCHECK if the |sql| contains errors.
[email protected]9fe37552011-12-23 17:07:20311 //
312 // Do not use ignore_result() to ignore all errors. Use
313 // ExecuteAndReturnErrorCode() and ignore only specific errors.
314 bool Execute(const char* sql) WARN_UNUSED_RESULT;
[email protected]e5ffd0e42009-09-11 21:30:56315
[email protected]eff1fa522011-12-12 23:50:59316 // Like Execute(), but returns the error code given by SQLite.
[email protected]9fe37552011-12-23 17:07:20317 int ExecuteAndReturnErrorCode(const char* sql) WARN_UNUSED_RESULT;
[email protected]eff1fa522011-12-12 23:50:59318
[email protected]e5ffd0e42009-09-11 21:30:56319 // Returns true if we have a statement with the given identifier already
320 // cached. This is normally not necessary to call, but can be useful if the
321 // caller has to dynamically build up SQL to avoid doing so if it's already
322 // cached.
323 bool HasCachedStatement(const StatementID& id) const;
324
325 // Returns a statement for the given SQL using the statement cache. It can
326 // take a nontrivial amount of work to parse and compile a statement, so
327 // keeping commonly-used ones around for future use is important for
328 // performance.
329 //
[email protected]eff1fa522011-12-12 23:50:59330 // If the |sql| has an error, an invalid, inert StatementRef is returned (and
331 // the code will crash in debug). The caller must deal with this eventuality,
332 // either by checking validity of the |sql| before calling, by correctly
333 // handling the return of an inert statement, or both.
[email protected]e5ffd0e42009-09-11 21:30:56334 //
335 // The StatementID and the SQL must always correspond to one-another. The
336 // ID is the lookup into the cache, so crazy things will happen if you use
337 // different SQL with the same ID.
338 //
339 // You will normally use the SQL_FROM_HERE macro to generate a statement
340 // ID associated with the current line of code. This gives uniqueness without
341 // you having to manage unique names. See StatementID above for more.
342 //
343 // Example:
[email protected]3273dce2010-01-27 16:08:08344 // sql::Statement stmt(connection_.GetCachedStatement(
345 // SQL_FROM_HERE, "SELECT * FROM foo"));
[email protected]e5ffd0e42009-09-11 21:30:56346 // if (!stmt)
347 // return false; // Error creating statement.
348 scoped_refptr<StatementRef> GetCachedStatement(const StatementID& id,
349 const char* sql);
350
[email protected]eff1fa522011-12-12 23:50:59351 // Used to check a |sql| statement for syntactic validity. If the statement is
352 // valid SQL, returns true.
353 bool IsSQLValid(const char* sql);
354
[email protected]e5ffd0e42009-09-11 21:30:56355 // Returns a non-cached statement for the given SQL. Use this for SQL that
356 // is only executed once or only rarely (there is overhead associated with
357 // keeping a statement cached).
358 //
359 // See GetCachedStatement above for examples and error information.
360 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql);
361
362 // Info querying -------------------------------------------------------------
363
364 // Returns true if the given table exists.
[email protected]765b44502009-10-02 05:01:42365 bool DoesTableExist(const char* table_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56366
[email protected]e2cadec82011-12-13 02:00:53367 // Returns true if the given index exists.
368 bool DoesIndexExist(const char* index_name) const;
369
[email protected]e5ffd0e42009-09-11 21:30:56370 // Returns true if a column with the given name exists in the given table.
[email protected]1ed78a32009-09-15 20:24:17371 bool DoesColumnExist(const char* table_name, const char* column_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56372
373 // Returns sqlite's internal ID for the last inserted row. Valid only
374 // immediately after an insert.
375 int64 GetLastInsertRowId() const;
376
[email protected]1ed78a32009-09-15 20:24:17377 // Returns sqlite's count of the number of rows modified by the last
378 // statement executed. Will be 0 if no statement has executed or the database
379 // is closed.
380 int GetLastChangeCount() const;
381
[email protected]e5ffd0e42009-09-11 21:30:56382 // Errors --------------------------------------------------------------------
383
384 // Returns the error code associated with the last sqlite operation.
385 int GetErrorCode() const;
386
[email protected]767718e52010-09-21 23:18:49387 // Returns the errno associated with GetErrorCode(). See
388 // SQLITE_LAST_ERRNO in SQLite documentation.
389 int GetLastErrno() const;
390
[email protected]e5ffd0e42009-09-11 21:30:56391 // Returns a pointer to a statically allocated string associated with the
392 // last sqlite operation.
393 const char* GetErrorMessage() const;
394
[email protected]92cd00a2013-08-16 11:09:58395 // Return a reproducible representation of the schema equivalent to
396 // running the following statement at a sqlite3 command-line:
397 // SELECT type, name, tbl_name, sql FROM sqlite_master ORDER BY 1, 2, 3, 4;
398 std::string GetSchema() const;
399
[email protected]74cdede2013-09-25 05:39:57400 // Clients which provide an error_callback don't see the
401 // error-handling at the end of OnSqliteError(). Expose to allow
402 // those clients to work appropriately with ScopedErrorIgnorer in
403 // tests.
404 static bool ShouldIgnoreSqliteError(int error);
405
[email protected]e5ffd0e42009-09-11 21:30:56406 private:
[email protected]8d409412013-07-19 18:25:30407 // For recovery module.
408 friend class Recovery;
409
[email protected]4350e322013-06-18 22:18:10410 // Allow test-support code to set/reset error ignorer.
411 friend class ScopedErrorIgnorer;
412
[email protected]eff1fa522011-12-12 23:50:59413 // Statement accesses StatementRef which we don't want to expose to everybody
[email protected]e5ffd0e42009-09-11 21:30:56414 // (they should go through Statement).
415 friend class Statement;
416
[email protected]765b44502009-10-02 05:01:42417 // Internal initialize function used by both Init and InitInMemory. The file
418 // name is always 8 bits since we want to use the 8-bit version of
419 // sqlite3_open. The string can also be sqlite's special ":memory:" string.
[email protected]fed734a2013-07-17 04:45:13420 //
421 // |retry_flag| controls retrying the open if the error callback
422 // addressed errors using RazeAndClose().
423 enum Retry {
424 NO_RETRY = 0,
425 RETRY_ON_POISON
426 };
427 bool OpenInternal(const std::string& file_name, Retry retry_flag);
[email protected]765b44502009-10-02 05:01:42428
[email protected]41a97c812013-02-07 02:35:38429 // Internal close function used by Close() and RazeAndClose().
430 // |forced| indicates that orderly-shutdown checks should not apply.
431 void CloseInternal(bool forced);
432
[email protected]35f7e5392012-07-27 19:54:50433 // Check whether the current thread is allowed to make IO calls, but only
434 // if database wasn't open in memory. Function is inlined to be a no-op in
435 // official build.
436 void AssertIOAllowed() {
437 if (!in_memory_)
438 base::ThreadRestrictions::AssertIOAllowed();
439 }
440
[email protected]e2cadec82011-12-13 02:00:53441 // Internal helper for DoesTableExist and DoesIndexExist.
442 bool DoesTableOrIndexExist(const char* name, const char* type) const;
443
[email protected]4350e322013-06-18 22:18:10444 // Accessors for global error-ignorer, for injecting behavior during tests.
445 // See test/scoped_error_ignorer.h.
446 typedef base::Callback<bool(int)> ErrorIgnorerCallback;
447 static ErrorIgnorerCallback* current_ignorer_cb_;
[email protected]4350e322013-06-18 22:18:10448 static void SetErrorIgnorer(ErrorIgnorerCallback* ignorer);
449 static void ResetErrorIgnorer();
450
[email protected]e5ffd0e42009-09-11 21:30:56451 // A StatementRef is a refcounted wrapper around a sqlite statement pointer.
452 // Refcounting allows us to give these statements out to sql::Statement
453 // objects while also optionally maintaining a cache of compiled statements
454 // by just keeping a refptr to these objects.
455 //
456 // A statement ref can be valid, in which case it can be used, or invalid to
457 // indicate that the statement hasn't been created yet, has an error, or has
458 // been destroyed.
459 //
460 // The Connection may revoke a StatementRef in some error cases, so callers
461 // should always check validity before using.
[email protected]601dc6a2011-11-12 01:14:23462 class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> {
[email protected]e5ffd0e42009-09-11 21:30:56463 public:
[email protected]41a97c812013-02-07 02:35:38464 // |connection| is the sql::Connection instance associated with
465 // the statement, and is used for tracking outstanding statements
466 // and for error handling. Set to NULL for invalid or untracked
467 // refs. |stmt| is the actual statement, and should only be NULL
468 // to create an invalid ref. |was_valid| indicates whether the
469 // statement should be considered valid for diagnistic purposes.
470 // |was_valid| can be true for NULL |stmt| if the connection has
471 // been forcibly closed by an error handler.
472 StatementRef(Connection* connection, sqlite3_stmt* stmt, bool was_valid);
[email protected]e5ffd0e42009-09-11 21:30:56473
474 // When true, the statement can be used.
475 bool is_valid() const { return !!stmt_; }
476
[email protected]41a97c812013-02-07 02:35:38477 // When true, the statement is either currently valid, or was
478 // previously valid but the connection was forcibly closed. Used
479 // for diagnostic checks.
480 bool was_valid() const { return was_valid_; }
481
[email protected]b4c363b2013-01-17 13:11:17482 // If we've not been linked to a connection, this will be NULL.
483 // TODO(shess): connection_ can be NULL in case of GetUntrackedStatement(),
484 // which prevents Statement::OnError() from forwarding errors.
[email protected]e5ffd0e42009-09-11 21:30:56485 Connection* connection() const { return connection_; }
486
487 // Returns the sqlite statement if any. If the statement is not active,
488 // this will return NULL.
489 sqlite3_stmt* stmt() const { return stmt_; }
490
491 // Destroys the compiled statement and marks it NULL. The statement will
[email protected]41a97c812013-02-07 02:35:38492 // no longer be active. |forced| is used to indicate if orderly-shutdown
493 // checks should apply (see Connection::RazeAndClose()).
494 void Close(bool forced);
[email protected]e5ffd0e42009-09-11 21:30:56495
[email protected]35f7e5392012-07-27 19:54:50496 // Check whether the current thread is allowed to make IO calls, but only
497 // if database wasn't open in memory.
498 void AssertIOAllowed() { if (connection_) connection_->AssertIOAllowed(); }
499
[email protected]e5ffd0e42009-09-11 21:30:56500 private:
[email protected]877d55d2009-11-05 21:53:08501 friend class base::RefCounted<StatementRef>;
502
503 ~StatementRef();
504
[email protected]e5ffd0e42009-09-11 21:30:56505 Connection* connection_;
506 sqlite3_stmt* stmt_;
[email protected]41a97c812013-02-07 02:35:38507 bool was_valid_;
[email protected]e5ffd0e42009-09-11 21:30:56508
509 DISALLOW_COPY_AND_ASSIGN(StatementRef);
510 };
511 friend class StatementRef;
512
513 // Executes a rollback statement, ignoring all transaction state. Used
514 // internally in the transaction management code.
515 void DoRollback();
516
517 // Called by a StatementRef when it's being created or destroyed. See
518 // open_statements_ below.
519 void StatementRefCreated(StatementRef* ref);
520 void StatementRefDeleted(StatementRef* ref);
521
[email protected]2f496b42013-09-26 18:36:58522 // Called when a sqlite function returns an error, which is passed
523 // as |err|. The return value is the error code to be reflected
524 // back to client code. |stmt| is non-NULL if the error relates to
525 // an sql::Statement instance. |sql| is non-NULL if the error
526 // relates to non-statement sql code (Execute, for instance). Both
527 // can be NULL, but both should never be set.
528 // NOTE(shess): Originally, the return value was intended to allow
529 // error handlers to transparently convert errors into success.
530 // Unfortunately, transactions are not generally restartable, so
531 // this did not work out.
532 int OnSqliteError(int err, Statement* stmt, const char* sql);
[email protected]faa604e2009-09-25 22:38:59533
[email protected]5b96f3772010-09-28 16:30:57534 // Like |Execute()|, but retries if the database is locked.
[email protected]9fe37552011-12-23 17:07:20535 bool ExecuteWithTimeout(const char* sql, base::TimeDelta ms_timeout)
536 WARN_UNUSED_RESULT;
[email protected]5b96f3772010-09-28 16:30:57537
[email protected]2eec0a22012-07-24 01:59:58538 // Internal helper for const functions. Like GetUniqueStatement(),
539 // except the statement is not entered into open_statements_,
540 // allowing this function to be const. Open statements can block
541 // closing the database, so only use in cases where the last ref is
542 // released before close could be called (which should always be the
543 // case for const functions).
544 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const;
545
[email protected]579446c2013-12-16 18:36:52546 bool IntegrityCheckHelper(
547 const char* pragma_sql,
548 std::vector<std::string>* messages) WARN_UNUSED_RESULT;
549
[email protected]e5ffd0e42009-09-11 21:30:56550 // The actual sqlite database. Will be NULL before Init has been called or if
551 // Init resulted in an error.
552 sqlite3* db_;
553
554 // Parameters we'll configure in sqlite before doing anything else. Zero means
555 // use the default value.
556 int page_size_;
557 int cache_size_;
558 bool exclusive_locking_;
[email protected]81a2a602013-07-17 19:10:36559 bool restrict_to_user_;
[email protected]e5ffd0e42009-09-11 21:30:56560
561 // All cached statements. Keeping a reference to these statements means that
562 // they'll remain active.
563 typedef std::map<StatementID, scoped_refptr<StatementRef> >
564 CachedStatementMap;
565 CachedStatementMap statement_cache_;
566
567 // A list of all StatementRefs we've given out. Each ref must register with
568 // us when it's created or destroyed. This allows us to potentially close
569 // any open statements when we encounter an error.
570 typedef std::set<StatementRef*> StatementRefSet;
571 StatementRefSet open_statements_;
572
573 // Number of currently-nested transactions.
574 int transaction_nesting_;
575
576 // True if any of the currently nested transactions have been rolled back.
577 // When we get to the outermost transaction, this will determine if we do
578 // a rollback instead of a commit.
579 bool needs_rollback_;
580
[email protected]35f7e5392012-07-27 19:54:50581 // True if database is open with OpenInMemory(), False if database is open
582 // with Open().
583 bool in_memory_;
584
[email protected]41a97c812013-02-07 02:35:38585 // |true| if the connection was closed using RazeAndClose(). Used
586 // to enable diagnostics to distinguish calls to never-opened
587 // databases (incorrect use of the API) from calls to once-valid
588 // databases.
589 bool poisoned_;
590
[email protected]c3881b372013-05-17 08:39:46591 ErrorCallback error_callback_;
592
[email protected]210ce0af2013-05-15 09:10:39593 // Tag for auxiliary histograms.
594 std::string histogram_tag_;
[email protected]c088e3a32013-01-03 23:59:14595
[email protected]e5ffd0e42009-09-11 21:30:56596 DISALLOW_COPY_AND_ASSIGN(Connection);
597};
598
599} // namespace sql
600
[email protected]f0a54b22011-07-19 18:40:21601#endif // SQL_CONNECTION_H_