blob: e4f38abe722b3a1368bb1bc7f8996bca0b89db38 [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
Victor Costancfbfa602018-08-01 23:24:465#ifndef SQL_DATABASE_H_
6#define SQL_DATABASE_H_
[email protected]e5ffd0e42009-09-11 21:30:567
avi0b519202015-12-21 07:25:198#include <stddef.h>
tfarina720d4f32015-05-11 22:31:269#include <stdint.h>
mostynbd82cd9952016-04-11 20:05:3410#include <memory>
[email protected]e5ffd0e42009-09-11 21:30:5611#include <set>
[email protected]7d6aee4e2009-09-12 01:12:3312#include <string>
Victor Costan87cf8c72018-07-19 19:36:0413#include <utility>
[email protected]80abf152013-05-22 12:42:4214#include <vector>
[email protected]e5ffd0e42009-09-11 21:30:5615
[email protected]c3881b372013-05-17 08:39:4616#include "base/callback.h"
[email protected]9fe37552011-12-23 17:07:2017#include "base/compiler_specific.h"
Dmitry Skibaa9ad8fe42017-08-16 21:02:4818#include "base/containers/flat_map.h"
shessc8cd2a162015-10-22 20:30:4619#include "base/gtest_prod_util.h"
tfarina720d4f32015-05-11 22:31:2620#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1521#include "base/memory/ref_counted.h"
Victor Costan12daa3ac92018-07-19 01:05:5822#include "base/sequence_checker.h"
Etienne Pierre-Doray0400dfb62018-12-03 19:12:2523#include "base/threading/scoped_blocking_call.h"
[email protected]35f7e5392012-07-27 19:54:5024#include "base/threading/thread_restrictions.h"
Victor Costan87cf8c72018-07-19 19:36:0425#include "base/time/tick_clock.h"
Victor Costan7f6abbbe2018-07-29 02:57:2726#include "sql/internal_api_token.h"
[email protected]d4526962011-11-10 21:40:2827#include "sql/sql_export.h"
Victor Costan12daa3ac92018-07-19 01:05:5828#include "sql/statement_id.h"
[email protected]e5ffd0e42009-09-11 21:30:5629
[email protected]e5ffd0e42009-09-11 21:30:5630struct sqlite3;
31struct sqlite3_stmt;
32
[email protected]a3ef4832013-02-02 05:12:3333namespace base {
34class FilePath;
shess58b8df82015-06-03 00:19:3235class HistogramBase;
dskibab4199f82016-11-21 20:16:1336namespace trace_event {
ssid1f4e5362016-12-08 20:41:3837class ProcessMemoryDump;
Victor Costan87cf8c72018-07-19 19:36:0438} // namespace trace_event
39} // namespace base
[email protected]a3ef4832013-02-02 05:12:3340
[email protected]e5ffd0e42009-09-11 21:30:5641namespace sql {
42
Victor Costancfbfa602018-08-01 23:24:4643class DatabaseMemoryDumpProvider;
[email protected]e5ffd0e42009-09-11 21:30:5644class Statement;
45
shess58b8df82015-06-03 00:19:3246// To allow some test classes to be friended.
47namespace test {
48class ScopedCommitHook;
shess976814402016-06-21 06:56:2549class ScopedErrorExpecter;
shess58b8df82015-06-03 00:19:3250class ScopedScalarFunction;
51class ScopedMockTimeSource;
Victor Costan87cf8c72018-07-19 19:36:0452} // namespace test
shess58b8df82015-06-03 00:19:3253
Victor Costancfbfa602018-08-01 23:24:4654// Exposes private Database functionality to unit tests.
Victor Costan7f6abbbe2018-07-29 02:57:2755//
56// This class is only defined in test targets.
Victor Costancfbfa602018-08-01 23:24:4657class DatabaseTestPeer;
[email protected]faa604e2009-09-25 22:38:5958
Victor Costan87cf8c72018-07-19 19:36:0459// Handle to an open SQLite database.
60//
61// Instances of this class are thread-unsafe and DCHECK that they are accessed
62// on the same sequence.
63//
64// TODO(pwnall): This should be renamed to Database. Class instances are
65// typically named "db_" / "db", and the class' equivalents in other systems
66// used by Chrome are named LevelDB::DB and blink::IDBDatabase.
Victor Costancfbfa602018-08-01 23:24:4667class SQL_EXPORT Database {
[email protected]e5ffd0e42009-09-11 21:30:5668 private:
69 class StatementRef; // Forward declaration, see real one below.
70
71 public:
[email protected]765b44502009-10-02 05:01:4272 // The database is opened by calling Open[InMemory](). Any uncommitted
73 // transactions will be rolled back when this object is deleted.
Victor Costancfbfa602018-08-01 23:24:4674 Database();
75 ~Database();
[email protected]e5ffd0e42009-09-11 21:30:5676
77 // Pre-init configuration ----------------------------------------------------
78
[email protected]765b44502009-10-02 05:01:4279 // Sets the page size that will be used when creating a new database. This
[email protected]e5ffd0e42009-09-11 21:30:5680 // must be called before Init(), and will only have an effect on new
81 // databases.
82 //
Victor Costan7f6abbbe2018-07-29 02:57:2783 // The page size must be a power of two between 512 and 65536 inclusive.
Victor Costan87cf8c72018-07-19 19:36:0484 void set_page_size(int page_size) {
Victor Costan7f6abbbe2018-07-29 02:57:2785 DCHECK_GE(page_size, 512);
86 DCHECK_LE(page_size, 65536);
87 DCHECK(!(page_size & (page_size - 1)))
Victor Costan87cf8c72018-07-19 19:36:0488 << "page_size must be a power of two";
89
90 page_size_ = page_size;
91 }
[email protected]e5ffd0e42009-09-11 21:30:5692
Victor Costan7f6abbbe2018-07-29 02:57:2793 // The page size that will be used when creating a new database.
94 int page_size() const { return page_size_; }
95
[email protected]e5ffd0e42009-09-11 21:30:5696 // Sets the number of pages that will be cached in memory by sqlite. The
97 // total cache size in bytes will be page_size * cache_size. This must be
[email protected]765b44502009-10-02 05:01:4298 // called before Open() to have an effect.
Victor Costan87cf8c72018-07-19 19:36:0499 void set_cache_size(int cache_size) {
100 DCHECK_GE(cache_size, 0);
101
102 cache_size_ = cache_size;
103 }
[email protected]e5ffd0e42009-09-11 21:30:56104
105 // Call to put the database in exclusive locking mode. There is no "back to
106 // normal" flag because of some additional requirements sqlite puts on this
[email protected]4ab952f2014-04-01 20:18:16107 // transaction (requires another access to the DB) and because we don't
[email protected]e5ffd0e42009-09-11 21:30:56108 // actually need it.
109 //
110 // Exclusive mode means that the database is not unlocked at the end of each
111 // transaction, which means there may be less time spent initializing the
112 // next transaction because it doesn't have to re-aquire locks.
113 //
[email protected]765b44502009-10-02 05:01:42114 // This must be called before Open() to have an effect.
[email protected]e5ffd0e42009-09-11 21:30:56115 void set_exclusive_locking() { exclusive_locking_ = true; }
116
shessa62504d2016-11-07 19:26:12117 // Call to use alternative status-tracking for mmap. Usually this is tracked
118 // in the meta table, but some databases have no meta table.
119 // TODO(shess): Maybe just have all databases use the alt option?
120 void set_mmap_alt_status() { mmap_alt_status_ = true; }
121
Victor Costan87cf8c72018-07-19 19:36:04122 // Opt out of memory-mapped file I/O.
shess7dbd4dee2015-10-06 17:39:16123 void set_mmap_disabled() { mmap_disabled_ = 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.
Victor Costanc7e7f2e2018-07-18 20:07:55130 using ErrorCallback = base::RepeatingCallback<void(int, Statement*)>;
[email protected]c3881b372013-05-17 08:39:46131 void set_error_callback(const ErrorCallback& callback) {
132 error_callback_ = callback;
133 }
Victor Costan87cf8c72018-07-19 19:36:04134 bool has_error_callback() const { return !error_callback_.is_null(); }
135 void reset_error_callback() { error_callback_.Reset(); }
[email protected]c3881b372013-05-17 08:39:46136
Victor Costancfbfa602018-08-01 23:24:46137 // Set this to enable additional per-database histogramming. Must be called
shess58b8df82015-06-03 00:19:32138 // before Open().
139 void set_histogram_tag(const std::string& tag);
[email protected]c088e3a32013-01-03 23:59:14140
[email protected]210ce0af2013-05-15 09:10:39141 // Record a sparse UMA histogram sample under
142 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no
143 // histogram is recorded.
Will Harrisb8693592018-08-28 22:58:44144 void AddTaggedHistogram(const std::string& name, int sample) const;
[email protected]210ce0af2013-05-15 09:10:39145
shess58b8df82015-06-03 00:19:32146 // Track various API calls and results. Values corrospond to UMA
147 // histograms, do not modify, or add or delete other than directly
148 // before EVENT_MAX_VALUE.
149 enum Events {
150 // Number of statements run, either with sql::Statement or Execute*().
151 EVENT_STATEMENT_RUN = 0,
152
153 // Number of rows returned by statements run.
154 EVENT_STATEMENT_ROWS,
155
156 // Number of statements successfully run (all steps returned SQLITE_DONE or
157 // SQLITE_ROW).
158 EVENT_STATEMENT_SUCCESS,
159
160 // Number of statements run by Execute() or ExecuteAndReturnErrorCode().
161 EVENT_EXECUTE,
162
163 // Number of rows changed by autocommit statements.
164 EVENT_CHANGES_AUTOCOMMIT,
165
166 // Number of rows changed by statements in transactions.
167 EVENT_CHANGES,
168
169 // Count actual SQLite transaction statements (not including nesting).
170 EVENT_BEGIN,
171 EVENT_COMMIT,
172 EVENT_ROLLBACK,
173
shessd90aeea82015-11-13 02:24:31174 // Track success and failure in GetAppropriateMmapSize().
175 // GetAppropriateMmapSize() should record at most one of these per run. The
176 // case of mapping everything is not recorded.
177 EVENT_MMAP_META_MISSING, // No meta table present.
178 EVENT_MMAP_META_FAILURE_READ, // Failed reading meta table.
179 EVENT_MMAP_META_FAILURE_UPDATE, // Failed updating meta table.
180 EVENT_MMAP_VFS_FAILURE, // Failed to access VFS.
181 EVENT_MMAP_FAILED, // Failure from past run.
182 EVENT_MMAP_FAILED_NEW, // Read error in this run.
183 EVENT_MMAP_SUCCESS_NEW, // Read to EOF in this run.
184 EVENT_MMAP_SUCCESS_PARTIAL, // Read but did not reach EOF.
185 EVENT_MMAP_SUCCESS_NO_PROGRESS, // Read quota exhausted.
186
Victor Costancfbfa602018-08-01 23:24:46187 EVENT_MMAP_STATUS_FAILURE_READ, // Failure reading MmapStatus view.
188 EVENT_MMAP_STATUS_FAILURE_UPDATE, // Failure updating MmapStatus view.
shessa62504d2016-11-07 19:26:12189
shess58b8df82015-06-03 00:19:32190 // Leave this at the end.
191 // TODO(shess): |EVENT_MAX| causes compile fail on Windows.
192 EVENT_MAX_VALUE
193 };
194 void RecordEvent(Events event, size_t count);
Victor Costan87cf8c72018-07-19 19:36:04195 void RecordOneEvent(Events event) { RecordEvent(event, 1); }
shess58b8df82015-06-03 00:19:32196
[email protected]579446c2013-12-16 18:36:52197 // Run "PRAGMA integrity_check" and post each line of
198 // results into |messages|. Returns the success of running the
199 // statement - per the SQLite documentation, if no errors are found the
200 // call should succeed, and a single value "ok" should be in messages.
201 bool FullIntegrityCheck(std::vector<std::string>* messages);
202
203 // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method,
204 // interprets the results returning true if the the statement executes
205 // without error and results in a single "ok" value.
206 bool QuickIntegrityCheck() WARN_UNUSED_RESULT;
[email protected]80abf152013-05-22 12:42:42207
afakhry7c9abe72016-08-05 17:33:19208 // Meant to be called from a client error callback so that it's able to
209 // get diagnostic information about the database.
210 std::string GetDiagnosticInfo(int extended_error, Statement* statement);
211
ssid1f4e5362016-12-08 20:41:38212 // Reports memory usage into provided memory dump with the given name.
213 bool ReportMemoryUsage(base::trace_event::ProcessMemoryDump* pmd,
214 const std::string& dump_name);
dskibab4199f82016-11-21 20:16:13215
[email protected]e5ffd0e42009-09-11 21:30:56216 // Initialization ------------------------------------------------------------
217
Victor Costancfbfa602018-08-01 23:24:46218 // Initializes the SQL database for the given file, returning true if the
[email protected]35f2094c2009-12-29 22:46:55219 // file could be opened. You can call this or OpenInMemory.
[email protected]a3ef4832013-02-02 05:12:33220 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42221
Victor Costancfbfa602018-08-01 23:24:46222 // Initializes the SQL database for a temporary in-memory database. There
[email protected]765b44502009-10-02 05:01:42223 // will be no associated file on disk, and the initial database will be
[email protected]35f2094c2009-12-29 22:46:55224 // empty. You can call this or Open.
[email protected]9fe37552011-12-23 17:07:20225 bool OpenInMemory() WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42226
[email protected]8d409412013-07-19 18:25:30227 // Create a temporary on-disk database. The database will be
228 // deleted after close. This kind of database is similar to
229 // OpenInMemory() for small databases, but can page to disk if the
230 // database becomes large.
231 bool OpenTemporary() WARN_UNUSED_RESULT;
232
[email protected]41a97c812013-02-07 02:35:38233 // Returns true if the database has been successfully opened.
Victor Costan87cf8c72018-07-19 19:36:04234 bool is_open() const { return static_cast<bool>(db_); }
[email protected]e5ffd0e42009-09-11 21:30:56235
236 // Closes the database. This is automatically performed on destruction for
237 // you, but this allows you to close the database early. You must not call
238 // any other functions after closing it. It is permissable to call Close on
239 // an uninitialized or already-closed database.
240 void Close();
241
[email protected]8ada10f2013-12-21 00:42:34242 // Reads the first <cache-size>*<page-size> bytes of the file to prime the
243 // filesystem cache. This can be more efficient than faulting pages
244 // individually. Since this involves blocking I/O, it should only be used if
245 // the caller will immediately read a substantial amount of data from the
246 // database.
[email protected]e5ffd0e42009-09-11 21:30:56247 //
[email protected]8ada10f2013-12-21 00:42:34248 // TODO(shess): Design a set of histograms or an experiment to inform this
249 // decision. Preloading should almost always improve later performance
250 // numbers for this database simply because it pulls operations forward, but
251 // if the data isn't actually used soon then preloading just slows down
252 // everything else.
[email protected]e5ffd0e42009-09-11 21:30:56253 void Preload();
254
Victor Costan52bef812018-12-05 07:41:49255 // Release all non-essential memory associated with this database connection.
256 void TrimMemory();
[email protected]be7995f12013-07-18 18:49:14257
[email protected]8e0c01282012-04-06 19:36:49258 // Raze the database to the ground. This approximates creating a
259 // fresh database from scratch, within the constraints of SQLite's
260 // locking protocol (locks and open handles can make doing this with
261 // filesystem operations problematic). Returns true if the database
262 // was razed.
263 //
264 // false is returned if the database is locked by some other
Carlos Knippschild46800c9f2017-09-02 02:21:43265 // process.
[email protected]8e0c01282012-04-06 19:36:49266 //
267 // NOTE(shess): Raze() will DCHECK in the following situations:
268 // - database is not open.
Victor Costancfbfa602018-08-01 23:24:46269 // - the database has a transaction open.
[email protected]8e0c01282012-04-06 19:36:49270 // - a SQLite issue occurs which is structural in nature (like the
271 // statements used are broken).
272 // Since Raze() is expected to be called in unexpected situations,
273 // these all return false, since it is unlikely that the caller
274 // could fix them.
[email protected]6d42f152012-11-10 00:38:24275 //
276 // The database's page size is taken from |page_size_|. The
277 // existing database's |auto_vacuum| setting is lost (the
278 // possibility of corruption makes it unreliable to pull it from the
279 // existing database). To re-enable on the empty database requires
280 // running "PRAGMA auto_vacuum = 1;" then "VACUUM".
281 //
282 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1,
283 // so Raze() sets auto_vacuum to 1.
284 //
Victor Costancfbfa602018-08-01 23:24:46285 // TODO(shess): Raze() needs a database so cannot clear SQLITE_NOTADB.
286 // TODO(shess): Bake auto_vacuum into Database's API so it can
[email protected]6d42f152012-11-10 00:38:24287 // just pick up the default.
[email protected]8e0c01282012-04-06 19:36:49288 bool Raze();
[email protected]8e0c01282012-04-06 19:36:49289
[email protected]41a97c812013-02-07 02:35:38290 // Breaks all outstanding transactions (as initiated by
[email protected]8d409412013-07-19 18:25:30291 // BeginTransaction()), closes the SQLite database, and poisons the
Victor Costancfbfa602018-08-01 23:24:46292 // object so that all future operations against the Database (or
[email protected]8d409412013-07-19 18:25:30293 // its Statements) fail safely, without side effects.
[email protected]41a97c812013-02-07 02:35:38294 //
[email protected]8d409412013-07-19 18:25:30295 // This is intended as an alternative to Close() in error callbacks.
296 // Close() should still be called at some point.
297 void Poison();
298
299 // Raze() the database and Poison() the handle. Returns the return
300 // value from Raze().
301 // TODO(shess): Rename to RazeAndPoison().
[email protected]41a97c812013-02-07 02:35:38302 bool RazeAndClose();
303
Victor Costancfbfa602018-08-01 23:24:46304 // Delete the underlying database files associated with |path|. This should be
305 // used on a database which is not opened by any Database instance. Open
306 // Database instances pointing to the database can cause odd results or
307 // corruption (for instance if a hot journal is deleted but the associated
308 // database is not).
[email protected]8d2e39e2013-06-24 05:55:08309 //
310 // Returns true if the database file and associated journals no
311 // longer exist, false otherwise. If the database has never
312 // existed, this will return true.
313 static bool Delete(const base::FilePath& path);
314
[email protected]e5ffd0e42009-09-11 21:30:56315 // Transactions --------------------------------------------------------------
316
317 // Transaction management. We maintain a virtual transaction stack to emulate
318 // nested transactions since sqlite can't do nested transactions. The
319 // limitation is you can't roll back a sub transaction: if any transaction
320 // fails, all transactions open will also be rolled back. Any nested
321 // transactions after one has rolled back will return fail for Begin(). If
322 // Begin() fails, you must not call Commit or Rollback().
323 //
324 // Normally you should use sql::Transaction to manage a transaction, which
325 // will scope it to a C++ context.
326 bool BeginTransaction();
327 void RollbackTransaction();
328 bool CommitTransaction();
329
[email protected]8d409412013-07-19 18:25:30330 // Rollback all outstanding transactions. Use with care, there may
331 // be scoped transactions on the stack.
332 void RollbackAllTransactions();
333
[email protected]e5ffd0e42009-09-11 21:30:56334 // Returns the current transaction nesting, which will be 0 if there are
335 // no open transactions.
336 int transaction_nesting() const { return transaction_nesting_; }
337
[email protected]8d409412013-07-19 18:25:30338 // Attached databases---------------------------------------------------------
339
Victor Costan7f6abbbe2018-07-29 02:57:27340 // SQLite supports attaching multiple database files to a single connection.
[email protected]8d409412013-07-19 18:25:30341 //
Victor Costan7f6abbbe2018-07-29 02:57:27342 // Attach the database in |other_db_path| to the current connection under
343 // |attachment_point|. |attachment_point| must only contain characters from
344 // [a-zA-Z0-9_].
Victor Costan8a87f7e52017-11-10 01:29:30345 //
346 // On the SQLite version shipped with Chrome (3.21+, Oct 2017), databases can
347 // be attached while a transaction is opened. However, these databases cannot
Victor Costan70bedf22018-07-18 21:21:14348 // be detached until the transaction is committed or aborted.
Victor Costan7f6abbbe2018-07-29 02:57:27349 //
350 // These APIs are only exposed for use in recovery. They are extremely subtle
351 // and are not useful for features built on top of //sql.
[email protected]8d409412013-07-19 18:25:30352 bool AttachDatabase(const base::FilePath& other_db_path,
Victor Costan7f6abbbe2018-07-29 02:57:27353 const char* attachment_point,
354 InternalApiToken);
355 bool DetachDatabase(const char* attachment_point, InternalApiToken);
[email protected]8d409412013-07-19 18:25:30356
[email protected]e5ffd0e42009-09-11 21:30:56357 // Statements ----------------------------------------------------------------
358
359 // Executes the given SQL string, returning true on success. This is
360 // normally used for simple, 1-off statements that don't take any bound
361 // parameters and don't return any data (e.g. CREATE TABLE).
[email protected]9fe37552011-12-23 17:07:20362 //
[email protected]eff1fa522011-12-12 23:50:59363 // This will DCHECK if the |sql| contains errors.
[email protected]9fe37552011-12-23 17:07:20364 //
365 // Do not use ignore_result() to ignore all errors. Use
366 // ExecuteAndReturnErrorCode() and ignore only specific errors.
367 bool Execute(const char* sql) WARN_UNUSED_RESULT;
[email protected]e5ffd0e42009-09-11 21:30:56368
[email protected]eff1fa522011-12-12 23:50:59369 // Like Execute(), but returns the error code given by SQLite.
[email protected]9fe37552011-12-23 17:07:20370 int ExecuteAndReturnErrorCode(const char* sql) WARN_UNUSED_RESULT;
[email protected]eff1fa522011-12-12 23:50:59371
[email protected]e5ffd0e42009-09-11 21:30:56372 // Returns a statement for the given SQL using the statement cache. It can
373 // take a nontrivial amount of work to parse and compile a statement, so
374 // keeping commonly-used ones around for future use is important for
375 // performance.
376 //
Victor Costan613b4302018-11-20 05:32:43377 // The SQL_FROM_HERE macro is the recommended way of generating a StatementID.
378 // Code that generates custom IDs must ensure that a StatementID is never used
379 // for different SQL statements. Failing to meet this requirement results in
380 // incorrect behavior, and should be caught by a DCHECK.
381 //
382 // The SQL statement passed in |sql| must match the SQL statement reported
383 // back by SQLite. Mismatches are caught by a DCHECK, so any code that has
384 // automated test coverage or that was manually tested on a DCHECK build will
385 // not exhibit this problem. Mismatches generally imply that the statement
386 // passed in has extra whitespace or comments surrounding it, which waste
387 // storage and CPU cycles.
388 //
[email protected]eff1fa522011-12-12 23:50:59389 // If the |sql| has an error, an invalid, inert StatementRef is returned (and
390 // the code will crash in debug). The caller must deal with this eventuality,
391 // either by checking validity of the |sql| before calling, by correctly
392 // handling the return of an inert statement, or both.
[email protected]e5ffd0e42009-09-11 21:30:56393 //
[email protected]e5ffd0e42009-09-11 21:30:56394 // Example:
Victor Costancfbfa602018-08-01 23:24:46395 // sql::Statement stmt(database_.GetCachedStatement(
[email protected]3273dce2010-01-27 16:08:08396 // SQL_FROM_HERE, "SELECT * FROM foo"));
[email protected]e5ffd0e42009-09-11 21:30:56397 // if (!stmt)
398 // return false; // Error creating statement.
Victor Costan12daa3ac92018-07-19 01:05:58399 scoped_refptr<StatementRef> GetCachedStatement(StatementID id,
[email protected]e5ffd0e42009-09-11 21:30:56400 const char* sql);
401
[email protected]eff1fa522011-12-12 23:50:59402 // Used to check a |sql| statement for syntactic validity. If the statement is
403 // valid SQL, returns true.
404 bool IsSQLValid(const char* sql);
405
[email protected]e5ffd0e42009-09-11 21:30:56406 // Returns a non-cached statement for the given SQL. Use this for SQL that
407 // is only executed once or only rarely (there is overhead associated with
408 // keeping a statement cached).
409 //
410 // See GetCachedStatement above for examples and error information.
411 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql);
412
413 // Info querying -------------------------------------------------------------
414
shessa62504d2016-11-07 19:26:12415 // Returns true if the given structure exists. Instead of test-then-create,
416 // callers should almost always prefer the "IF NOT EXISTS" version of the
417 // CREATE statement.
[email protected]e2cadec82011-12-13 02:00:53418 bool DoesIndexExist(const char* index_name) const;
shessa62504d2016-11-07 19:26:12419 bool DoesTableExist(const char* table_name) const;
420 bool DoesViewExist(const char* table_name) const;
[email protected]e2cadec82011-12-13 02:00:53421
[email protected]e5ffd0e42009-09-11 21:30:56422 // Returns true if a column with the given name exists in the given table.
Victor Costan1ff47e92018-12-07 11:10:43423 //
424 // Calling this method on a VIEW returns an unspecified result.
425 //
426 // This should only be used by migration code for legacy features that do not
427 // use MetaTable, and need an alternative way of figuring out the database's
428 // current version.
[email protected]1ed78a32009-09-15 20:24:17429 bool DoesColumnExist(const char* table_name, const char* column_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56430
431 // Returns sqlite's internal ID for the last inserted row. Valid only
432 // immediately after an insert.
tfarina720d4f32015-05-11 22:31:26433 int64_t GetLastInsertRowId() const;
[email protected]e5ffd0e42009-09-11 21:30:56434
[email protected]1ed78a32009-09-15 20:24:17435 // Returns sqlite's count of the number of rows modified by the last
436 // statement executed. Will be 0 if no statement has executed or the database
437 // is closed.
438 int GetLastChangeCount() const;
439
[email protected]e5ffd0e42009-09-11 21:30:56440 // Errors --------------------------------------------------------------------
441
442 // Returns the error code associated with the last sqlite operation.
443 int GetErrorCode() const;
444
[email protected]767718e52010-09-21 23:18:49445 // Returns the errno associated with GetErrorCode(). See
446 // SQLITE_LAST_ERRNO in SQLite documentation.
447 int GetLastErrno() const;
448
[email protected]e5ffd0e42009-09-11 21:30:56449 // Returns a pointer to a statically allocated string associated with the
450 // last sqlite operation.
451 const char* GetErrorMessage() const;
452
[email protected]92cd00a2013-08-16 11:09:58453 // Return a reproducible representation of the schema equivalent to
454 // running the following statement at a sqlite3 command-line:
455 // SELECT type, name, tbl_name, sql FROM sqlite_master ORDER BY 1, 2, 3, 4;
456 std::string GetSchema() const;
457
shess976814402016-06-21 06:56:25458 // Returns |true| if there is an error expecter (see SetErrorExpecter), and
459 // that expecter returns |true| when passed |error|. Clients which provide an
460 // |error_callback| should use IsExpectedSqliteError() to check for unexpected
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52461 // errors; if one is detected, DLOG(DCHECK) is generally appropriate (see
shess976814402016-06-21 06:56:25462 // OnSqliteError implementation).
463 static bool IsExpectedSqliteError(int error);
[email protected]74cdede2013-09-25 05:39:57464
shessc8cd2a162015-10-22 20:30:46465 // Collect various diagnostic information and post a crash dump to aid
466 // debugging. Dump rate per database is limited to prevent overwhelming the
467 // crash server.
468 void ReportDiagnosticInfo(int extended_error, Statement* stmt);
469
Victor Costan87cf8c72018-07-19 19:36:04470 // Helper to return the current time from the time source.
471 base::TimeTicks NowTicks() const { return clock_->NowTicks(); }
472
473 // Intended for tests to inject a mock time source.
474 //
475 // Inlined to avoid generating code in the production binary.
476 inline void set_clock_for_testing(std::unique_ptr<base::TickClock> clock) {
477 clock_ = std::move(clock);
478 }
479
Victor Costance678e72018-07-24 10:25:00480 // Computes the path of a database's rollback journal.
481 //
482 // The journal file is created at the beginning of the database's first
483 // transaction. The file may be removed and re-created between transactions,
484 // depending on whether the database is opened in exclusive mode, and on
485 // configuration options. The journal file does not exist when the database
486 // operates in WAL mode.
487 //
488 // This is intended for internal use and tests. To preserve our ability to
489 // iterate on our SQLite configuration, features must avoid relying on
490 // the existence of specific files.
491 static base::FilePath JournalPath(const base::FilePath& db_path);
492
493 // Computes the path of a database's write-ahead log (WAL).
494 //
495 // The WAL file exists while a database is opened in WAL mode.
496 //
497 // This is intended for internal use and tests. To preserve our ability to
498 // iterate on our SQLite configuration, features must avoid relying on
499 // the existence of specific files.
500 static base::FilePath WriteAheadLogPath(const base::FilePath& db_path);
501
502 // Computes the path of a database's shared memory (SHM) file.
503 //
504 // The SHM file is used to coordinate between multiple processes using the
505 // same database in WAL mode. Thus, this file only exists for databases using
506 // WAL and not opened in exclusive mode.
507 //
508 // This is intended for internal use and tests. To preserve our ability to
509 // iterate on our SQLite configuration, features must avoid relying on
510 // the existence of specific files.
511 static base::FilePath SharedMemoryFilePath(const base::FilePath& db_path);
512
Victor Costan7f6abbbe2018-07-29 02:57:27513 // Default page size for newly created databases.
514 //
515 // Guaranteed to match SQLITE_DEFAULT_PAGE_SIZE.
516 static constexpr int kDefaultPageSize = 4096;
[email protected]8d409412013-07-19 18:25:30517
Victor Costan7f6abbbe2018-07-29 02:57:27518 // Internal state accessed by other classes in //sql.
519 sqlite3* db(InternalApiToken) const { return db_; }
520 bool poisoned(InternalApiToken) const { return poisoned_; }
521
522 private:
shess976814402016-06-21 06:56:25523 // Allow test-support code to set/reset error expecter.
524 friend class test::ScopedErrorExpecter;
[email protected]4350e322013-06-18 22:18:10525
[email protected]eff1fa522011-12-12 23:50:59526 // Statement accesses StatementRef which we don't want to expose to everybody
[email protected]e5ffd0e42009-09-11 21:30:56527 // (they should go through Statement).
528 friend class Statement;
529
Victor Costancfbfa602018-08-01 23:24:46530 friend class DatabaseTestPeer;
Victor Costan7f6abbbe2018-07-29 02:57:27531
shess58b8df82015-06-03 00:19:32532 friend class test::ScopedCommitHook;
533 friend class test::ScopedScalarFunction;
534 friend class test::ScopedMockTimeSource;
535
Victor Costancfbfa602018-08-01 23:24:46536 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, CachedStatement);
537 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, CollectDiagnosticInfo);
538 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, GetAppropriateMmapSize);
539 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, GetAppropriateMmapSizeAltStatus);
540 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, OnMemoryDump);
541 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, RegisterIntentToUpload);
shessf7fcc452017-04-19 22:10:41542 FRIEND_TEST_ALL_PREFIXES(SQLiteFeaturesTest, WALNoClose);
shessc8cd2a162015-10-22 20:30:46543
[email protected]765b44502009-10-02 05:01:42544 // Internal initialize function used by both Init and InitInMemory. The file
545 // name is always 8 bits since we want to use the 8-bit version of
546 // sqlite3_open. The string can also be sqlite's special ":memory:" string.
[email protected]fed734a2013-07-17 04:45:13547 //
548 // |retry_flag| controls retrying the open if the error callback
549 // addressed errors using RazeAndClose().
Victor Costancfbfa602018-08-01 23:24:46550 enum Retry { NO_RETRY = 0, RETRY_ON_POISON };
[email protected]fed734a2013-07-17 04:45:13551 bool OpenInternal(const std::string& file_name, Retry retry_flag);
[email protected]765b44502009-10-02 05:01:42552
[email protected]41a97c812013-02-07 02:35:38553 // Internal close function used by Close() and RazeAndClose().
554 // |forced| indicates that orderly-shutdown checks should not apply.
555 void CloseInternal(bool forced);
556
[email protected]35f7e5392012-07-27 19:54:50557 // Check whether the current thread is allowed to make IO calls, but only
558 // if database wasn't open in memory. Function is inlined to be a no-op in
559 // official build.
shessc8cd2a162015-10-22 20:30:46560 void AssertIOAllowed() const {
[email protected]35f7e5392012-07-27 19:54:50561 if (!in_memory_)
Etienne Pierre-doraya4195e592018-10-18 16:36:42562 base::AssertBlockingAllowedDeprecated();
[email protected]35f7e5392012-07-27 19:54:50563 }
564
shessa62504d2016-11-07 19:26:12565 // Internal helper for Does*Exist() functions.
566 bool DoesSchemaItemExist(const char* name, const char* type) const;
[email protected]e2cadec82011-12-13 02:00:53567
shess976814402016-06-21 06:56:25568 // Accessors for global error-expecter, for injecting behavior during tests.
569 // See test/scoped_error_expecter.h.
Victor Costanc7e7f2e2018-07-18 20:07:55570 using ErrorExpecterCallback = base::RepeatingCallback<bool(int)>;
shess976814402016-06-21 06:56:25571 static ErrorExpecterCallback* current_expecter_cb_;
572 static void SetErrorExpecter(ErrorExpecterCallback* expecter);
573 static void ResetErrorExpecter();
[email protected]4350e322013-06-18 22:18:10574
[email protected]e5ffd0e42009-09-11 21:30:56575 // A StatementRef is a refcounted wrapper around a sqlite statement pointer.
576 // Refcounting allows us to give these statements out to sql::Statement
577 // objects while also optionally maintaining a cache of compiled statements
578 // by just keeping a refptr to these objects.
579 //
580 // A statement ref can be valid, in which case it can be used, or invalid to
581 // indicate that the statement hasn't been created yet, has an error, or has
582 // been destroyed.
583 //
Victor Costancfbfa602018-08-01 23:24:46584 // The Database may revoke a StatementRef in some error cases, so callers
[email protected]e5ffd0e42009-09-11 21:30:56585 // should always check validity before using.
[email protected]601dc6a2011-11-12 01:14:23586 class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> {
[email protected]e5ffd0e42009-09-11 21:30:56587 public:
Victor Costan3b02cdf2018-07-18 00:39:56588 REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
589
Victor Costancfbfa602018-08-01 23:24:46590 // |database| is the sql::Database instance associated with
[email protected]41a97c812013-02-07 02:35:38591 // the statement, and is used for tracking outstanding statements
Victor Costanbd623112018-07-18 04:17:27592 // and for error handling. Set to nullptr for invalid or untracked
593 // refs. |stmt| is the actual statement, and should only be null
[email protected]41a97c812013-02-07 02:35:38594 // to create an invalid ref. |was_valid| indicates whether the
595 // statement should be considered valid for diagnistic purposes.
Victor Costancfbfa602018-08-01 23:24:46596 // |was_valid| can be true for a null |stmt| if the Database has
[email protected]41a97c812013-02-07 02:35:38597 // been forcibly closed by an error handler.
Victor Costancfbfa602018-08-01 23:24:46598 StatementRef(Database* database, sqlite3_stmt* stmt, bool was_valid);
[email protected]e5ffd0e42009-09-11 21:30:56599
600 // When true, the statement can be used.
601 bool is_valid() const { return !!stmt_; }
602
[email protected]41a97c812013-02-07 02:35:38603 // When true, the statement is either currently valid, or was
Victor Costancfbfa602018-08-01 23:24:46604 // previously valid but the database was forcibly closed. Used
[email protected]41a97c812013-02-07 02:35:38605 // for diagnostic checks.
606 bool was_valid() const { return was_valid_; }
607
Victor Costancfbfa602018-08-01 23:24:46608 // If we've not been linked to a database, this will be null.
Victor Costanbd623112018-07-18 04:17:27609 //
Victor Costancfbfa602018-08-01 23:24:46610 // TODO(shess): database_ can be nullptr in case of
Victor Costanbd623112018-07-18 04:17:27611 // GetUntrackedStatement(), which prevents Statement::OnError() from
612 // forwarding errors.
Victor Costancfbfa602018-08-01 23:24:46613 Database* database() const { return database_; }
[email protected]e5ffd0e42009-09-11 21:30:56614
615 // Returns the sqlite statement if any. If the statement is not active,
Victor Costanbd623112018-07-18 04:17:27616 // this will return nullptr.
[email protected]e5ffd0e42009-09-11 21:30:56617 sqlite3_stmt* stmt() const { return stmt_; }
618
Victor Costanbd623112018-07-18 04:17:27619 // Destroys the compiled statement and sets it to nullptr. The statement
620 // will no longer be active. |forced| is used to indicate if
Victor Costancfbfa602018-08-01 23:24:46621 // orderly-shutdown checks should apply (see Database::RazeAndClose()).
[email protected]41a97c812013-02-07 02:35:38622 void Close(bool forced);
[email protected]e5ffd0e42009-09-11 21:30:56623
[email protected]35f7e5392012-07-27 19:54:50624 // Check whether the current thread is allowed to make IO calls, but only
625 // if database wasn't open in memory.
Victor Costanc7e7f2e2018-07-18 20:07:55626 void AssertIOAllowed() const {
Victor Costancfbfa602018-08-01 23:24:46627 if (database_)
628 database_->AssertIOAllowed();
Victor Costanc7e7f2e2018-07-18 20:07:55629 }
[email protected]35f7e5392012-07-27 19:54:50630
[email protected]e5ffd0e42009-09-11 21:30:56631 private:
[email protected]877d55d2009-11-05 21:53:08632 friend class base::RefCounted<StatementRef>;
633
634 ~StatementRef();
635
Victor Costancfbfa602018-08-01 23:24:46636 Database* database_;
[email protected]e5ffd0e42009-09-11 21:30:56637 sqlite3_stmt* stmt_;
[email protected]41a97c812013-02-07 02:35:38638 bool was_valid_;
[email protected]e5ffd0e42009-09-11 21:30:56639
640 DISALLOW_COPY_AND_ASSIGN(StatementRef);
641 };
642 friend class StatementRef;
643
644 // Executes a rollback statement, ignoring all transaction state. Used
645 // internally in the transaction management code.
646 void DoRollback();
647
648 // Called by a StatementRef when it's being created or destroyed. See
649 // open_statements_ below.
650 void StatementRefCreated(StatementRef* ref);
651 void StatementRefDeleted(StatementRef* ref);
652
[email protected]2f496b42013-09-26 18:36:58653 // Called when a sqlite function returns an error, which is passed
654 // as |err|. The return value is the error code to be reflected
Victor Costanbd623112018-07-18 04:17:27655 // back to client code. |stmt| is non-null if the error relates to
656 // an sql::Statement instance. |sql| is non-nullptr if the error
[email protected]2f496b42013-09-26 18:36:58657 // relates to non-statement sql code (Execute, for instance). Both
Victor Costanbd623112018-07-18 04:17:27658 // can be null, but both should never be set.
[email protected]2f496b42013-09-26 18:36:58659 // NOTE(shess): Originally, the return value was intended to allow
660 // error handlers to transparently convert errors into success.
661 // Unfortunately, transactions are not generally restartable, so
662 // this did not work out.
shess9e77283d2016-06-13 23:53:20663 int OnSqliteError(int err, Statement* stmt, const char* sql) const;
[email protected]faa604e2009-09-25 22:38:59664
[email protected]5b96f3772010-09-28 16:30:57665 // Like |Execute()|, but retries if the database is locked.
Victor Costancfbfa602018-08-01 23:24:46666 bool ExecuteWithTimeout(const char* sql,
667 base::TimeDelta ms_timeout) WARN_UNUSED_RESULT;
[email protected]5b96f3772010-09-28 16:30:57668
shess9e77283d2016-06-13 23:53:20669 // Implementation helper for GetUniqueStatement() and GetUntrackedStatement().
670 // |tracking_db| is the db the resulting ref should register with for
Victor Costanbd623112018-07-18 04:17:27671 // outstanding statement tracking, which should be |this| to track or null to
shess9e77283d2016-06-13 23:53:20672 // not track.
Victor Costancfbfa602018-08-01 23:24:46673 scoped_refptr<StatementRef> GetStatementImpl(sql::Database* tracking_db,
674 const char* sql) const;
shess9e77283d2016-06-13 23:53:20675
676 // Helper for implementing const member functions. Like GetUniqueStatement(),
677 // except the StatementRef is not entered into |open_statements_|, so an
678 // outstanding StatementRef from this function can block closing the database.
679 // The StatementRef will not call OnSqliteError(), because that can call
680 // |error_callback_| which can close the database.
[email protected]2eec0a22012-07-24 01:59:58681 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const;
682
Victor Costancfbfa602018-08-01 23:24:46683 bool IntegrityCheckHelper(const char* pragma_sql,
684 std::vector<std::string>* messages)
685 WARN_UNUSED_RESULT;
[email protected]579446c2013-12-16 18:36:52686
shess58b8df82015-06-03 00:19:32687 // Record time spent executing explicit COMMIT statements.
688 void RecordCommitTime(const base::TimeDelta& delta);
689
690 // Record time in DML (Data Manipulation Language) statements such as INSERT
691 // or UPDATE outside of an explicit transaction. Due to implementation
692 // limitations time spent on DDL (Data Definition Language) statements such as
693 // ALTER and CREATE is not included.
694 void RecordAutoCommitTime(const base::TimeDelta& delta);
695
696 // Record all time spent on updating the database. This includes CommitTime()
697 // and AutoCommitTime(), plus any time spent spilling to the journal if
698 // transactions do not fit in cache.
699 void RecordUpdateTime(const base::TimeDelta& delta);
700
701 // Record all time spent running statements, including time spent doing
702 // updates and time spent on read-only queries.
703 void RecordQueryTime(const base::TimeDelta& delta);
704
705 // Record |delta| as query time if |read_only| (from sqlite3_stmt_readonly) is
706 // true, autocommit time if the database is not in a transaction, or update
707 // time if the database is in a transaction. Also records change count to
708 // EVENT_CHANGES_AUTOCOMMIT or EVENT_CHANGES_COMMIT.
709 void RecordTimeAndChanges(const base::TimeDelta& delta, bool read_only);
710
shess7dbd4dee2015-10-06 17:39:16711 // Release page-cache memory if memory-mapped I/O is enabled and the database
712 // was changed. Passing true for |implicit_change_performed| allows
713 // overriding the change detection for cases like DDL (CREATE, DROP, etc),
714 // which do not participate in the total-rows-changed tracking.
715 void ReleaseCacheMemoryIfNeeded(bool implicit_change_performed);
716
shessc8cd2a162015-10-22 20:30:46717 // Returns the results of sqlite3_db_filename(), which should match the path
718 // passed to Open().
719 base::FilePath DbPath() const;
720
721 // Helper to prevent uploading too many diagnostic dumps for a given database,
722 // since every dump will likely show the same problem. Returns |true| if this
723 // function was not previously called for this database, and the persistent
724 // storage which tracks state was updated.
725 //
726 // |false| is returned if the function was previously called for this
727 // database, even across restarts. |false| is also returned if the persistent
728 // storage cannot be updated, possibly indicating problems requiring user or
729 // admin intervention, such as filesystem corruption or disk full. |false| is
730 // also returned if the persistent storage contains invalid data or is not
731 // readable.
732 //
733 // TODO(shess): It would make sense to reset the persistent state if the
734 // database is razed or recovered, or if the diagnostic code adds new
735 // capabilities.
736 bool RegisterIntentToUpload() const;
737
738 // Helper to collect diagnostic info for a corrupt database.
739 std::string CollectCorruptionInfo();
740
741 // Helper to collect diagnostic info for errors.
742 std::string CollectErrorInfo(int error, Statement* stmt) const;
743
shessd90aeea82015-11-13 02:24:31744 // Calculates a value appropriate to pass to "PRAGMA mmap_size = ". So errors
745 // can make it unsafe to map a file, so the file is read using regular I/O,
746 // with any errors causing 0 (don't map anything) to be returned. If the
747 // entire file is read without error, a large value is returned which will
748 // allow the entire file to be mapped in most cases.
749 //
750 // Results are recorded in the database's meta table for future reference, so
751 // the file should only be read through once.
752 size_t GetAppropriateMmapSize();
753
shessa62504d2016-11-07 19:26:12754 // Helpers for GetAppropriateMmapSize().
755 bool GetMmapAltStatus(int64_t* status);
756 bool SetMmapAltStatus(int64_t status);
757
Victor Costanbd623112018-07-18 04:17:27758 // The actual sqlite database. Will be null before Init has been called or if
[email protected]e5ffd0e42009-09-11 21:30:56759 // Init resulted in an error.
760 sqlite3* db_;
761
762 // Parameters we'll configure in sqlite before doing anything else. Zero means
763 // use the default value.
764 int page_size_;
765 int cache_size_;
766 bool exclusive_locking_;
767
Victor Costanc7e7f2e2018-07-18 20:07:55768 // Holds references to all cached statements so they remain active.
769 //
770 // flat_map is appropriate here because the codebase has ~400 cached
771 // statements, and each statement is at most one insertion in the map
772 // throughout a process' lifetime.
773 base::flat_map<StatementID, scoped_refptr<StatementRef>> statement_cache_;
[email protected]e5ffd0e42009-09-11 21:30:56774
775 // A list of all StatementRefs we've given out. Each ref must register with
776 // us when it's created or destroyed. This allows us to potentially close
777 // any open statements when we encounter an error.
Victor Costanc7e7f2e2018-07-18 20:07:55778 std::set<StatementRef*> open_statements_;
[email protected]e5ffd0e42009-09-11 21:30:56779
780 // Number of currently-nested transactions.
781 int transaction_nesting_;
782
783 // True if any of the currently nested transactions have been rolled back.
784 // When we get to the outermost transaction, this will determine if we do
785 // a rollback instead of a commit.
786 bool needs_rollback_;
787
[email protected]35f7e5392012-07-27 19:54:50788 // True if database is open with OpenInMemory(), False if database is open
789 // with Open().
790 bool in_memory_;
791
Victor Costancfbfa602018-08-01 23:24:46792 // |true| if the Database was closed using RazeAndClose(). Used
[email protected]41a97c812013-02-07 02:35:38793 // to enable diagnostics to distinguish calls to never-opened
794 // databases (incorrect use of the API) from calls to once-valid
795 // databases.
796 bool poisoned_;
797
shessa62504d2016-11-07 19:26:12798 // |true| to use alternate storage for tracking mmap status.
799 bool mmap_alt_status_;
800
Victor Costancfbfa602018-08-01 23:24:46801 // |true| if SQLite memory-mapped I/O is not desired for this database.
shess7dbd4dee2015-10-06 17:39:16802 bool mmap_disabled_;
803
Victor Costancfbfa602018-08-01 23:24:46804 // |true| if SQLite memory-mapped I/O was enabled for this database.
shess7dbd4dee2015-10-06 17:39:16805 // Used by ReleaseCacheMemoryIfNeeded().
806 bool mmap_enabled_;
807
808 // Used by ReleaseCacheMemoryIfNeeded() to track if new changes have happened
809 // since memory was last released.
810 int total_changes_at_last_release_;
811
[email protected]c3881b372013-05-17 08:39:46812 ErrorCallback error_callback_;
813
[email protected]210ce0af2013-05-15 09:10:39814 // Tag for auxiliary histograms.
815 std::string histogram_tag_;
[email protected]c088e3a32013-01-03 23:59:14816
shess58b8df82015-06-03 00:19:32817 // Linear histogram for RecordEvent().
818 base::HistogramBase* stats_histogram_;
819
820 // Histogram for tracking time taken in commit.
821 base::HistogramBase* commit_time_histogram_;
822
823 // Histogram for tracking time taken in autocommit updates.
824 base::HistogramBase* autocommit_time_histogram_;
825
826 // Histogram for tracking time taken in updates (including commit and
827 // autocommit).
828 base::HistogramBase* update_time_histogram_;
829
830 // Histogram for tracking time taken in all queries.
831 base::HistogramBase* query_time_histogram_;
832
833 // Source for timing information, provided to allow tests to inject time
834 // changes.
Victor Costan87cf8c72018-07-19 19:36:04835 std::unique_ptr<base::TickClock> clock_;
shess58b8df82015-06-03 00:19:32836
ssid3be5b1ec2016-01-13 14:21:57837 // Stores the dump provider object when db is open.
Victor Costancfbfa602018-08-01 23:24:46838 std::unique_ptr<DatabaseMemoryDumpProvider> memory_dump_provider_;
ssid3be5b1ec2016-01-13 14:21:57839
Victor Costancfbfa602018-08-01 23:24:46840 DISALLOW_COPY_AND_ASSIGN(Database);
[email protected]e5ffd0e42009-09-11 21:30:56841};
842
843} // namespace sql
844
Victor Costancfbfa602018-08-01 23:24:46845#endif // SQL_DATABASE_H_