blob: 6e4334ea776c234708d781a4330cb2b35f61642c [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"
Victor Costane56cc682018-12-27 01:53:4618#include "base/component_export.h"
Dmitry Skibaa9ad8fe42017-08-16 21:02:4819#include "base/containers/flat_map.h"
shessc8cd2a162015-10-22 20:30:4620#include "base/gtest_prod_util.h"
tfarina720d4f32015-05-11 22:31:2621#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1522#include "base/memory/ref_counted.h"
Etienne Pierre-Doraya71d7af2019-02-07 02:07:5423#include "base/optional.h"
Victor Costan12daa3ac92018-07-19 01:05:5824#include "base/sequence_checker.h"
Etienne Pierre-Doray0400dfb62018-12-03 19:12:2525#include "base/threading/scoped_blocking_call.h"
Victor Costan7f6abbbe2018-07-29 02:57:2726#include "sql/internal_api_token.h"
Victor Costan12daa3ac92018-07-19 01:05:5827#include "sql/statement_id.h"
[email protected]e5ffd0e42009-09-11 21:30:5628
[email protected]e5ffd0e42009-09-11 21:30:5629struct sqlite3;
30struct sqlite3_stmt;
31
[email protected]a3ef4832013-02-02 05:12:3332namespace base {
33class FilePath;
shess58b8df82015-06-03 00:19:3234class HistogramBase;
dskibab4199f82016-11-21 20:16:1335namespace trace_event {
ssid1f4e5362016-12-08 20:41:3836class ProcessMemoryDump;
Victor Costan87cf8c72018-07-19 19:36:0437} // namespace trace_event
38} // namespace base
[email protected]a3ef4832013-02-02 05:12:3339
[email protected]e5ffd0e42009-09-11 21:30:5640namespace sql {
41
Victor Costancfbfa602018-08-01 23:24:4642class DatabaseMemoryDumpProvider;
[email protected]e5ffd0e42009-09-11 21:30:5643class Statement;
44
shess58b8df82015-06-03 00:19:3245namespace test {
shess976814402016-06-21 06:56:2546class ScopedErrorExpecter;
Victor Costan87cf8c72018-07-19 19:36:0447} // namespace test
shess58b8df82015-06-03 00:19:3248
Victor Costan87cf8c72018-07-19 19:36:0449// Handle to an open SQLite database.
50//
51// Instances of this class are thread-unsafe and DCHECK that they are accessed
52// on the same sequence.
Victor Costane56cc682018-12-27 01:53:4653class COMPONENT_EXPORT(SQL) Database {
[email protected]e5ffd0e42009-09-11 21:30:5654 private:
55 class StatementRef; // Forward declaration, see real one below.
56
57 public:
[email protected]765b44502009-10-02 05:01:4258 // The database is opened by calling Open[InMemory](). Any uncommitted
59 // transactions will be rolled back when this object is deleted.
Victor Costancfbfa602018-08-01 23:24:4660 Database();
61 ~Database();
[email protected]e5ffd0e42009-09-11 21:30:5662
Ken Rockot01687422020-08-17 18:00:5963 // Allows mmapping to be disabled globally by default in the calling process.
64 // Must be called before any threads attempt to create a Database.
65 //
66 // TODO(crbug.com/1117049): Remove this global configuration.
67 static void DisableMmapByDefault();
68
[email protected]e5ffd0e42009-09-11 21:30:5669 // Pre-init configuration ----------------------------------------------------
70
[email protected]765b44502009-10-02 05:01:4271 // Sets the page size that will be used when creating a new database. This
[email protected]e5ffd0e42009-09-11 21:30:5672 // must be called before Init(), and will only have an effect on new
73 // databases.
74 //
Victor Costan7f6abbbe2018-07-29 02:57:2775 // The page size must be a power of two between 512 and 65536 inclusive.
Victor Costan87cf8c72018-07-19 19:36:0476 void set_page_size(int page_size) {
Victor Costan7f6abbbe2018-07-29 02:57:2777 DCHECK_GE(page_size, 512);
78 DCHECK_LE(page_size, 65536);
79 DCHECK(!(page_size & (page_size - 1)))
Victor Costan87cf8c72018-07-19 19:36:0480 << "page_size must be a power of two";
81
82 page_size_ = page_size;
83 }
[email protected]e5ffd0e42009-09-11 21:30:5684
Victor Costan7f6abbbe2018-07-29 02:57:2785 // The page size that will be used when creating a new database.
86 int page_size() const { return page_size_; }
87
[email protected]e5ffd0e42009-09-11 21:30:5688 // Sets the number of pages that will be cached in memory by sqlite. The
89 // total cache size in bytes will be page_size * cache_size. This must be
[email protected]765b44502009-10-02 05:01:4290 // called before Open() to have an effect.
Victor Costan87cf8c72018-07-19 19:36:0491 void set_cache_size(int cache_size) {
92 DCHECK_GE(cache_size, 0);
93
94 cache_size_ = cache_size;
95 }
[email protected]e5ffd0e42009-09-11 21:30:5696
Shubham Aggarwalbe4f97ce2020-06-19 15:58:5797 // Returns whether a database will be opened in WAL mode.
98 bool UseWALMode() const;
99
100 // Enables/disables WAL mode (https://www.sqlite.org/wal.html) when
101 // opening a new database.
102 //
103 // WAL mode is currently not fully supported on FuchsiaOS. It will only be
104 // turned on if the database is also using exclusive locking mode.
105 // (https://crbug.com/1082059)
106 //
107 // Note: Changing page size is not supported when in WAL mode. So running
108 // 'PRAGMA page_size = <new-size>' or using set_page_size will result in
109 // no-ops.
110 //
111 // This must be called before Open() to have an effect.
112 void want_wal_mode(bool enabled) { want_wal_mode_ = enabled; }
113
Victor Costanb2230792020-10-09 08:35:14114 // Makes database accessible by only one process at a time.
[email protected]e5ffd0e42009-09-11 21:30:56115 //
Victor Costanb2230792020-10-09 08:35:14116 // TODO(https://crbug.com/1120969): This should be the default mode. The
117 // "NORMAL" mode should be opt-in.
[email protected]e5ffd0e42009-09-11 21:30:56118 //
Victor Costanb2230792020-10-09 08:35:14119 // SQLite supports a locking protocol that allows multiple processes to safely
120 // operate on the same database at the same time. The locking protocol is used
121 // on every transaction, and comes with a small performance penalty.
122 //
123 // Calling this method causes the locking protocol to be used once, when the
124 // database is opened. No other process will be able to access the database at
125 // the same time.
126 //
127 // This method must be called before Open() to have an effect.
128 //
129 // More details at https://www.sqlite.org/pragma.html#pragma_locking_mode
130 //
131 // SQLite's locking protocol is summarized at
132 // https://www.sqlite.org/c3ref/io_methods.html
[email protected]e5ffd0e42009-09-11 21:30:56133 void set_exclusive_locking() { exclusive_locking_ = true; }
134
shessa62504d2016-11-07 19:26:12135 // Call to use alternative status-tracking for mmap. Usually this is tracked
136 // in the meta table, but some databases have no meta table.
137 // TODO(shess): Maybe just have all databases use the alt option?
138 void set_mmap_alt_status() { mmap_alt_status_ = true; }
139
Victor Costan87cf8c72018-07-19 19:36:04140 // Opt out of memory-mapped file I/O.
shess7dbd4dee2015-10-06 17:39:16141 void set_mmap_disabled() { mmap_disabled_ = true; }
142
[email protected]c3881b372013-05-17 08:39:46143 // Set an error-handling callback. On errors, the error number (and
144 // statement, if available) will be passed to the callback.
145 //
146 // If no callback is set, the default action is to crash in debug
147 // mode or return failure in release mode.
Victor Costanc7e7f2e2018-07-18 20:07:55148 using ErrorCallback = base::RepeatingCallback<void(int, Statement*)>;
[email protected]c3881b372013-05-17 08:39:46149 void set_error_callback(const ErrorCallback& callback) {
150 error_callback_ = callback;
151 }
Victor Costan87cf8c72018-07-19 19:36:04152 bool has_error_callback() const { return !error_callback_.is_null(); }
153 void reset_error_callback() { error_callback_.Reset(); }
[email protected]c3881b372013-05-17 08:39:46154
Victor Costancfbfa602018-08-01 23:24:46155 // Set this to enable additional per-database histogramming. Must be called
shess58b8df82015-06-03 00:19:32156 // before Open().
157 void set_histogram_tag(const std::string& tag);
[email protected]c088e3a32013-01-03 23:59:14158
[email protected]210ce0af2013-05-15 09:10:39159 // Record a sparse UMA histogram sample under
160 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no
161 // histogram is recorded.
Will Harrisb8693592018-08-28 22:58:44162 void AddTaggedHistogram(const std::string& name, int sample) const;
[email protected]210ce0af2013-05-15 09:10:39163
Etienne Bergerone7681c72020-01-17 00:51:20164 // Track various API calls and results. Values correspond to UMA
shess58b8df82015-06-03 00:19:32165 // histograms, do not modify, or add or delete other than directly
166 // before EVENT_MAX_VALUE.
167 enum Events {
168 // Number of statements run, either with sql::Statement or Execute*().
Victor Costan5e785e32019-02-26 20:39:31169 EVENT_STATEMENT_RUN_DEPRECATED = 0,
shess58b8df82015-06-03 00:19:32170
171 // Number of rows returned by statements run.
Victor Costan5e785e32019-02-26 20:39:31172 EVENT_STATEMENT_ROWS_DEPRECATED,
shess58b8df82015-06-03 00:19:32173
174 // Number of statements successfully run (all steps returned SQLITE_DONE or
175 // SQLITE_ROW).
Victor Costan5e785e32019-02-26 20:39:31176 EVENT_STATEMENT_SUCCESS_DEPRECATED,
shess58b8df82015-06-03 00:19:32177
178 // Number of statements run by Execute() or ExecuteAndReturnErrorCode().
Victor Costan5e785e32019-02-26 20:39:31179 EVENT_EXECUTE_DEPRECATED,
shess58b8df82015-06-03 00:19:32180
181 // Number of rows changed by autocommit statements.
Victor Costan5e785e32019-02-26 20:39:31182 EVENT_CHANGES_AUTOCOMMIT_DEPRECATED,
shess58b8df82015-06-03 00:19:32183
184 // Number of rows changed by statements in transactions.
Victor Costan5e785e32019-02-26 20:39:31185 EVENT_CHANGES_DEPRECATED,
shess58b8df82015-06-03 00:19:32186
187 // Count actual SQLite transaction statements (not including nesting).
Victor Costan5e785e32019-02-26 20:39:31188 EVENT_BEGIN_DEPRECATED,
189 EVENT_COMMIT_DEPRECATED,
190 EVENT_ROLLBACK_DEPRECATED,
shess58b8df82015-06-03 00:19:32191
shessd90aeea82015-11-13 02:24:31192 // Track success and failure in GetAppropriateMmapSize().
193 // GetAppropriateMmapSize() should record at most one of these per run. The
194 // case of mapping everything is not recorded.
Victor Costan5e785e32019-02-26 20:39:31195 EVENT_MMAP_META_MISSING, // No meta table present.
196 EVENT_MMAP_META_FAILURE_READ, // Failed reading meta table.
197 EVENT_MMAP_META_FAILURE_UPDATE, // Failed updating meta table.
198 EVENT_MMAP_VFS_FAILURE, // Failed to access VFS.
199 EVENT_MMAP_FAILED, // Failure from past run.
200 EVENT_MMAP_FAILED_NEW, // Read error in this run.
201 EVENT_MMAP_SUCCESS_NEW_DEPRECATED, // Read to EOF in this run.
202 EVENT_MMAP_SUCCESS_PARTIAL_DEPRECATED, // Read but did not reach EOF.
203 EVENT_MMAP_SUCCESS_NO_PROGRESS_DEPRECATED, // Read quota exhausted.
shessd90aeea82015-11-13 02:24:31204
Victor Costancfbfa602018-08-01 23:24:46205 EVENT_MMAP_STATUS_FAILURE_READ, // Failure reading MmapStatus view.
206 EVENT_MMAP_STATUS_FAILURE_UPDATE, // Failure updating MmapStatus view.
shessa62504d2016-11-07 19:26:12207
shess58b8df82015-06-03 00:19:32208 // Leave this at the end.
209 // TODO(shess): |EVENT_MAX| causes compile fail on Windows.
Victor Costan5e785e32019-02-26 20:39:31210 EVENT_MAX_VALUE,
shess58b8df82015-06-03 00:19:32211 };
212 void RecordEvent(Events event, size_t count);
Victor Costan87cf8c72018-07-19 19:36:04213 void RecordOneEvent(Events event) { RecordEvent(event, 1); }
shess58b8df82015-06-03 00:19:32214
[email protected]579446c2013-12-16 18:36:52215 // Run "PRAGMA integrity_check" and post each line of
216 // results into |messages|. Returns the success of running the
217 // statement - per the SQLite documentation, if no errors are found the
218 // call should succeed, and a single value "ok" should be in messages.
219 bool FullIntegrityCheck(std::vector<std::string>* messages);
220
221 // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method,
222 // interprets the results returning true if the the statement executes
223 // without error and results in a single "ok" value.
224 bool QuickIntegrityCheck() WARN_UNUSED_RESULT;
[email protected]80abf152013-05-22 12:42:42225
afakhry7c9abe72016-08-05 17:33:19226 // Meant to be called from a client error callback so that it's able to
227 // get diagnostic information about the database.
228 std::string GetDiagnosticInfo(int extended_error, Statement* statement);
229
ssid1f4e5362016-12-08 20:41:38230 // Reports memory usage into provided memory dump with the given name.
231 bool ReportMemoryUsage(base::trace_event::ProcessMemoryDump* pmd,
232 const std::string& dump_name);
dskibab4199f82016-11-21 20:16:13233
[email protected]e5ffd0e42009-09-11 21:30:56234 // Initialization ------------------------------------------------------------
235
Victor Costancfbfa602018-08-01 23:24:46236 // Initializes the SQL database for the given file, returning true if the
[email protected]35f2094c2009-12-29 22:46:55237 // file could be opened. You can call this or OpenInMemory.
[email protected]a3ef4832013-02-02 05:12:33238 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42239
Victor Costancfbfa602018-08-01 23:24:46240 // Initializes the SQL database for a temporary in-memory database. There
[email protected]765b44502009-10-02 05:01:42241 // will be no associated file on disk, and the initial database will be
[email protected]35f2094c2009-12-29 22:46:55242 // empty. You can call this or Open.
[email protected]9fe37552011-12-23 17:07:20243 bool OpenInMemory() WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42244
[email protected]8d409412013-07-19 18:25:30245 // Create a temporary on-disk database. The database will be
246 // deleted after close. This kind of database is similar to
247 // OpenInMemory() for small databases, but can page to disk if the
248 // database becomes large.
249 bool OpenTemporary() WARN_UNUSED_RESULT;
250
[email protected]41a97c812013-02-07 02:35:38251 // Returns true if the database has been successfully opened.
Victor Costan87cf8c72018-07-19 19:36:04252 bool is_open() const { return static_cast<bool>(db_); }
[email protected]e5ffd0e42009-09-11 21:30:56253
254 // Closes the database. This is automatically performed on destruction for
255 // you, but this allows you to close the database early. You must not call
256 // any other functions after closing it. It is permissable to call Close on
257 // an uninitialized or already-closed database.
258 void Close();
259
[email protected]8ada10f2013-12-21 00:42:34260 // Reads the first <cache-size>*<page-size> bytes of the file to prime the
261 // filesystem cache. This can be more efficient than faulting pages
262 // individually. Since this involves blocking I/O, it should only be used if
263 // the caller will immediately read a substantial amount of data from the
264 // database.
[email protected]e5ffd0e42009-09-11 21:30:56265 //
[email protected]8ada10f2013-12-21 00:42:34266 // TODO(shess): Design a set of histograms or an experiment to inform this
267 // decision. Preloading should almost always improve later performance
268 // numbers for this database simply because it pulls operations forward, but
269 // if the data isn't actually used soon then preloading just slows down
270 // everything else.
[email protected]e5ffd0e42009-09-11 21:30:56271 void Preload();
272
Victor Costan52bef812018-12-05 07:41:49273 // Release all non-essential memory associated with this database connection.
274 void TrimMemory();
[email protected]be7995f12013-07-18 18:49:14275
[email protected]8e0c01282012-04-06 19:36:49276 // Raze the database to the ground. This approximates creating a
277 // fresh database from scratch, within the constraints of SQLite's
278 // locking protocol (locks and open handles can make doing this with
279 // filesystem operations problematic). Returns true if the database
280 // was razed.
281 //
282 // false is returned if the database is locked by some other
Carlos Knippschild46800c9f2017-09-02 02:21:43283 // process.
[email protected]8e0c01282012-04-06 19:36:49284 //
285 // NOTE(shess): Raze() will DCHECK in the following situations:
286 // - database is not open.
Victor Costancfbfa602018-08-01 23:24:46287 // - the database has a transaction open.
[email protected]8e0c01282012-04-06 19:36:49288 // - a SQLite issue occurs which is structural in nature (like the
289 // statements used are broken).
290 // Since Raze() is expected to be called in unexpected situations,
291 // these all return false, since it is unlikely that the caller
292 // could fix them.
[email protected]6d42f152012-11-10 00:38:24293 //
294 // The database's page size is taken from |page_size_|. The
295 // existing database's |auto_vacuum| setting is lost (the
296 // possibility of corruption makes it unreliable to pull it from the
297 // existing database). To re-enable on the empty database requires
298 // running "PRAGMA auto_vacuum = 1;" then "VACUUM".
299 //
300 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1,
301 // so Raze() sets auto_vacuum to 1.
302 //
Victor Costancfbfa602018-08-01 23:24:46303 // TODO(shess): Raze() needs a database so cannot clear SQLITE_NOTADB.
304 // TODO(shess): Bake auto_vacuum into Database's API so it can
[email protected]6d42f152012-11-10 00:38:24305 // just pick up the default.
[email protected]8e0c01282012-04-06 19:36:49306 bool Raze();
[email protected]8e0c01282012-04-06 19:36:49307
[email protected]41a97c812013-02-07 02:35:38308 // Breaks all outstanding transactions (as initiated by
[email protected]8d409412013-07-19 18:25:30309 // BeginTransaction()), closes the SQLite database, and poisons the
Victor Costancfbfa602018-08-01 23:24:46310 // object so that all future operations against the Database (or
[email protected]8d409412013-07-19 18:25:30311 // its Statements) fail safely, without side effects.
[email protected]41a97c812013-02-07 02:35:38312 //
[email protected]8d409412013-07-19 18:25:30313 // This is intended as an alternative to Close() in error callbacks.
314 // Close() should still be called at some point.
315 void Poison();
316
317 // Raze() the database and Poison() the handle. Returns the return
318 // value from Raze().
319 // TODO(shess): Rename to RazeAndPoison().
[email protected]41a97c812013-02-07 02:35:38320 bool RazeAndClose();
321
Victor Costancfbfa602018-08-01 23:24:46322 // Delete the underlying database files associated with |path|. This should be
323 // used on a database which is not opened by any Database instance. Open
324 // Database instances pointing to the database can cause odd results or
325 // corruption (for instance if a hot journal is deleted but the associated
326 // database is not).
[email protected]8d2e39e2013-06-24 05:55:08327 //
328 // Returns true if the database file and associated journals no
329 // longer exist, false otherwise. If the database has never
330 // existed, this will return true.
331 static bool Delete(const base::FilePath& path);
332
[email protected]e5ffd0e42009-09-11 21:30:56333 // Transactions --------------------------------------------------------------
334
335 // Transaction management. We maintain a virtual transaction stack to emulate
336 // nested transactions since sqlite can't do nested transactions. The
337 // limitation is you can't roll back a sub transaction: if any transaction
338 // fails, all transactions open will also be rolled back. Any nested
339 // transactions after one has rolled back will return fail for Begin(). If
340 // Begin() fails, you must not call Commit or Rollback().
341 //
342 // Normally you should use sql::Transaction to manage a transaction, which
343 // will scope it to a C++ context.
344 bool BeginTransaction();
345 void RollbackTransaction();
346 bool CommitTransaction();
347
[email protected]8d409412013-07-19 18:25:30348 // Rollback all outstanding transactions. Use with care, there may
349 // be scoped transactions on the stack.
350 void RollbackAllTransactions();
351
[email protected]e5ffd0e42009-09-11 21:30:56352 // Returns the current transaction nesting, which will be 0 if there are
353 // no open transactions.
354 int transaction_nesting() const { return transaction_nesting_; }
355
[email protected]8d409412013-07-19 18:25:30356 // Attached databases---------------------------------------------------------
357
Victor Costan7f6abbbe2018-07-29 02:57:27358 // SQLite supports attaching multiple database files to a single connection.
[email protected]8d409412013-07-19 18:25:30359 //
Victor Costan7f6abbbe2018-07-29 02:57:27360 // Attach the database in |other_db_path| to the current connection under
361 // |attachment_point|. |attachment_point| must only contain characters from
362 // [a-zA-Z0-9_].
Victor Costan8a87f7e52017-11-10 01:29:30363 //
364 // On the SQLite version shipped with Chrome (3.21+, Oct 2017), databases can
365 // be attached while a transaction is opened. However, these databases cannot
Victor Costan70bedf22018-07-18 21:21:14366 // be detached until the transaction is committed or aborted.
Victor Costan7f6abbbe2018-07-29 02:57:27367 //
368 // These APIs are only exposed for use in recovery. They are extremely subtle
369 // and are not useful for features built on top of //sql.
[email protected]8d409412013-07-19 18:25:30370 bool AttachDatabase(const base::FilePath& other_db_path,
Victor Costan7f6abbbe2018-07-29 02:57:27371 const char* attachment_point,
372 InternalApiToken);
373 bool DetachDatabase(const char* attachment_point, InternalApiToken);
[email protected]8d409412013-07-19 18:25:30374
[email protected]e5ffd0e42009-09-11 21:30:56375 // Statements ----------------------------------------------------------------
376
377 // Executes the given SQL string, returning true on success. This is
378 // normally used for simple, 1-off statements that don't take any bound
379 // parameters and don't return any data (e.g. CREATE TABLE).
[email protected]9fe37552011-12-23 17:07:20380 //
[email protected]eff1fa522011-12-12 23:50:59381 // This will DCHECK if the |sql| contains errors.
[email protected]9fe37552011-12-23 17:07:20382 //
383 // Do not use ignore_result() to ignore all errors. Use
384 // ExecuteAndReturnErrorCode() and ignore only specific errors.
385 bool Execute(const char* sql) WARN_UNUSED_RESULT;
[email protected]e5ffd0e42009-09-11 21:30:56386
[email protected]eff1fa522011-12-12 23:50:59387 // Like Execute(), but returns the error code given by SQLite.
[email protected]9fe37552011-12-23 17:07:20388 int ExecuteAndReturnErrorCode(const char* sql) WARN_UNUSED_RESULT;
[email protected]eff1fa522011-12-12 23:50:59389
[email protected]e5ffd0e42009-09-11 21:30:56390 // Returns a statement for the given SQL using the statement cache. It can
391 // take a nontrivial amount of work to parse and compile a statement, so
392 // keeping commonly-used ones around for future use is important for
393 // performance.
394 //
Victor Costan613b4302018-11-20 05:32:43395 // The SQL_FROM_HERE macro is the recommended way of generating a StatementID.
396 // Code that generates custom IDs must ensure that a StatementID is never used
397 // for different SQL statements. Failing to meet this requirement results in
398 // incorrect behavior, and should be caught by a DCHECK.
399 //
400 // The SQL statement passed in |sql| must match the SQL statement reported
401 // back by SQLite. Mismatches are caught by a DCHECK, so any code that has
402 // automated test coverage or that was manually tested on a DCHECK build will
403 // not exhibit this problem. Mismatches generally imply that the statement
404 // passed in has extra whitespace or comments surrounding it, which waste
405 // storage and CPU cycles.
406 //
[email protected]eff1fa522011-12-12 23:50:59407 // If the |sql| has an error, an invalid, inert StatementRef is returned (and
408 // the code will crash in debug). The caller must deal with this eventuality,
409 // either by checking validity of the |sql| before calling, by correctly
410 // handling the return of an inert statement, or both.
[email protected]e5ffd0e42009-09-11 21:30:56411 //
[email protected]e5ffd0e42009-09-11 21:30:56412 // Example:
Victor Costancfbfa602018-08-01 23:24:46413 // sql::Statement stmt(database_.GetCachedStatement(
[email protected]3273dce2010-01-27 16:08:08414 // SQL_FROM_HERE, "SELECT * FROM foo"));
[email protected]e5ffd0e42009-09-11 21:30:56415 // if (!stmt)
416 // return false; // Error creating statement.
Victor Costan12daa3ac92018-07-19 01:05:58417 scoped_refptr<StatementRef> GetCachedStatement(StatementID id,
[email protected]e5ffd0e42009-09-11 21:30:56418 const char* sql);
419
[email protected]eff1fa522011-12-12 23:50:59420 // Used to check a |sql| statement for syntactic validity. If the statement is
421 // valid SQL, returns true.
422 bool IsSQLValid(const char* sql);
423
[email protected]e5ffd0e42009-09-11 21:30:56424 // Returns a non-cached statement for the given SQL. Use this for SQL that
425 // is only executed once or only rarely (there is overhead associated with
426 // keeping a statement cached).
427 //
428 // See GetCachedStatement above for examples and error information.
429 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql);
430
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57431 // Performs a passive checkpoint on the main attached database if it is in
432 // WAL mode. Returns true if the checkpoint was successful and false in case
433 // of an error. It is a no-op if the database is not in WAL mode.
434 //
435 // Note: Checkpointing is a very slow operation and will block any writes
436 // until it is finished. Please use with care.
437 bool CheckpointDatabase();
438
[email protected]e5ffd0e42009-09-11 21:30:56439 // Info querying -------------------------------------------------------------
440
shessa62504d2016-11-07 19:26:12441 // Returns true if the given structure exists. Instead of test-then-create,
442 // callers should almost always prefer the "IF NOT EXISTS" version of the
443 // CREATE statement.
[email protected]e2cadec82011-12-13 02:00:53444 bool DoesIndexExist(const char* index_name) const;
shessa62504d2016-11-07 19:26:12445 bool DoesTableExist(const char* table_name) const;
446 bool DoesViewExist(const char* table_name) const;
[email protected]e2cadec82011-12-13 02:00:53447
[email protected]e5ffd0e42009-09-11 21:30:56448 // Returns true if a column with the given name exists in the given table.
Victor Costan1ff47e92018-12-07 11:10:43449 //
450 // Calling this method on a VIEW returns an unspecified result.
451 //
452 // This should only be used by migration code for legacy features that do not
453 // use MetaTable, and need an alternative way of figuring out the database's
454 // current version.
[email protected]1ed78a32009-09-15 20:24:17455 bool DoesColumnExist(const char* table_name, const char* column_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56456
457 // Returns sqlite's internal ID for the last inserted row. Valid only
458 // immediately after an insert.
tfarina720d4f32015-05-11 22:31:26459 int64_t GetLastInsertRowId() const;
[email protected]e5ffd0e42009-09-11 21:30:56460
[email protected]1ed78a32009-09-15 20:24:17461 // Returns sqlite's count of the number of rows modified by the last
462 // statement executed. Will be 0 if no statement has executed or the database
463 // is closed.
464 int GetLastChangeCount() const;
465
Victor Costand6e73252020-10-14 21:11:25466 // Approximates the amount of memory used by SQLite for this database.
467 //
468 // This measures the memory used for the page cache (most likely the biggest
469 // consumer), database schema, and prepared statements.
470 //
471 // The memory used by the page cache can be recovered by calling TrimMemory(),
472 // which will cause SQLite to drop the page cache.
473 int GetMemoryUsage();
474
[email protected]e5ffd0e42009-09-11 21:30:56475 // Errors --------------------------------------------------------------------
476
477 // Returns the error code associated with the last sqlite operation.
478 int GetErrorCode() const;
479
[email protected]767718e52010-09-21 23:18:49480 // Returns the errno associated with GetErrorCode(). See
481 // SQLITE_LAST_ERRNO in SQLite documentation.
482 int GetLastErrno() const;
483
[email protected]e5ffd0e42009-09-11 21:30:56484 // Returns a pointer to a statically allocated string associated with the
485 // last sqlite operation.
486 const char* GetErrorMessage() const;
487
[email protected]92cd00a2013-08-16 11:09:58488 // Return a reproducible representation of the schema equivalent to
489 // running the following statement at a sqlite3 command-line:
490 // SELECT type, name, tbl_name, sql FROM sqlite_master ORDER BY 1, 2, 3, 4;
491 std::string GetSchema() const;
492
shess976814402016-06-21 06:56:25493 // Returns |true| if there is an error expecter (see SetErrorExpecter), and
494 // that expecter returns |true| when passed |error|. Clients which provide an
495 // |error_callback| should use IsExpectedSqliteError() to check for unexpected
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52496 // errors; if one is detected, DLOG(DCHECK) is generally appropriate (see
shess976814402016-06-21 06:56:25497 // OnSqliteError implementation).
498 static bool IsExpectedSqliteError(int error);
[email protected]74cdede2013-09-25 05:39:57499
Victor Costance678e72018-07-24 10:25:00500 // Computes the path of a database's rollback journal.
501 //
502 // The journal file is created at the beginning of the database's first
503 // transaction. The file may be removed and re-created between transactions,
504 // depending on whether the database is opened in exclusive mode, and on
505 // configuration options. The journal file does not exist when the database
506 // operates in WAL 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 JournalPath(const base::FilePath& db_path);
512
513 // Computes the path of a database's write-ahead log (WAL).
514 //
515 // The WAL file exists while a database is opened in WAL mode.
516 //
517 // This is intended for internal use and tests. To preserve our ability to
518 // iterate on our SQLite configuration, features must avoid relying on
519 // the existence of specific files.
520 static base::FilePath WriteAheadLogPath(const base::FilePath& db_path);
521
522 // Computes the path of a database's shared memory (SHM) file.
523 //
524 // The SHM file is used to coordinate between multiple processes using the
525 // same database in WAL mode. Thus, this file only exists for databases using
526 // WAL and not opened in exclusive mode.
527 //
528 // This is intended for internal use and tests. To preserve our ability to
529 // iterate on our SQLite configuration, features must avoid relying on
530 // the existence of specific files.
531 static base::FilePath SharedMemoryFilePath(const base::FilePath& db_path);
532
Victor Costan7f6abbbe2018-07-29 02:57:27533 // Default page size for newly created databases.
534 //
535 // Guaranteed to match SQLITE_DEFAULT_PAGE_SIZE.
536 static constexpr int kDefaultPageSize = 4096;
[email protected]8d409412013-07-19 18:25:30537
Victor Costan7f6abbbe2018-07-29 02:57:27538 // Internal state accessed by other classes in //sql.
539 sqlite3* db(InternalApiToken) const { return db_; }
540 bool poisoned(InternalApiToken) const { return poisoned_; }
541
542 private:
shess976814402016-06-21 06:56:25543 // Allow test-support code to set/reset error expecter.
544 friend class test::ScopedErrorExpecter;
[email protected]4350e322013-06-18 22:18:10545
[email protected]eff1fa522011-12-12 23:50:59546 // Statement accesses StatementRef which we don't want to expose to everybody
[email protected]e5ffd0e42009-09-11 21:30:56547 // (they should go through Statement).
548 friend class Statement;
549
Victor Costancfbfa602018-08-01 23:24:46550 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, CachedStatement);
551 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, CollectDiagnosticInfo);
552 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, GetAppropriateMmapSize);
553 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, GetAppropriateMmapSizeAltStatus);
554 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, OnMemoryDump);
555 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, RegisterIntentToUpload);
shessf7fcc452017-04-19 22:10:41556 FRIEND_TEST_ALL_PREFIXES(SQLiteFeaturesTest, WALNoClose);
shessc8cd2a162015-10-22 20:30:46557
[email protected]765b44502009-10-02 05:01:42558 // Internal initialize function used by both Init and InitInMemory. The file
559 // name is always 8 bits since we want to use the 8-bit version of
560 // sqlite3_open. The string can also be sqlite's special ":memory:" string.
[email protected]fed734a2013-07-17 04:45:13561 //
562 // |retry_flag| controls retrying the open if the error callback
563 // addressed errors using RazeAndClose().
Victor Costancfbfa602018-08-01 23:24:46564 enum Retry { NO_RETRY = 0, RETRY_ON_POISON };
[email protected]fed734a2013-07-17 04:45:13565 bool OpenInternal(const std::string& file_name, Retry retry_flag);
[email protected]765b44502009-10-02 05:01:42566
[email protected]41a97c812013-02-07 02:35:38567 // Internal close function used by Close() and RazeAndClose().
568 // |forced| indicates that orderly-shutdown checks should not apply.
569 void CloseInternal(bool forced);
570
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54571 // Construct a ScopedBlockingCall to annotate IO calls, but only if
Etienne Bergerone7681c72020-01-17 00:51:20572 // database wasn't open in memory. ScopedBlockingCall uses |from_here| to
573 // declare its blocking execution scope (see https://www.crbug/934302).
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54574 void InitScopedBlockingCall(
Etienne Bergerone7681c72020-01-17 00:51:20575 const base::Location& from_here,
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54576 base::Optional<base::ScopedBlockingCall>* scoped_blocking_call) const {
[email protected]35f7e5392012-07-27 19:54:50577 if (!in_memory_)
Etienne Bergerone7681c72020-01-17 00:51:20578 scoped_blocking_call->emplace(from_here, base::BlockingType::MAY_BLOCK);
[email protected]35f7e5392012-07-27 19:54:50579 }
580
shessa62504d2016-11-07 19:26:12581 // Internal helper for Does*Exist() functions.
582 bool DoesSchemaItemExist(const char* name, const char* type) const;
[email protected]e2cadec82011-12-13 02:00:53583
shess976814402016-06-21 06:56:25584 // Accessors for global error-expecter, for injecting behavior during tests.
585 // See test/scoped_error_expecter.h.
Victor Costanc7e7f2e2018-07-18 20:07:55586 using ErrorExpecterCallback = base::RepeatingCallback<bool(int)>;
shess976814402016-06-21 06:56:25587 static ErrorExpecterCallback* current_expecter_cb_;
588 static void SetErrorExpecter(ErrorExpecterCallback* expecter);
589 static void ResetErrorExpecter();
[email protected]4350e322013-06-18 22:18:10590
[email protected]e5ffd0e42009-09-11 21:30:56591 // A StatementRef is a refcounted wrapper around a sqlite statement pointer.
592 // Refcounting allows us to give these statements out to sql::Statement
593 // objects while also optionally maintaining a cache of compiled statements
594 // by just keeping a refptr to these objects.
595 //
596 // A statement ref can be valid, in which case it can be used, or invalid to
597 // indicate that the statement hasn't been created yet, has an error, or has
598 // been destroyed.
599 //
Victor Costancfbfa602018-08-01 23:24:46600 // The Database may revoke a StatementRef in some error cases, so callers
[email protected]e5ffd0e42009-09-11 21:30:56601 // should always check validity before using.
Victor Costane56cc682018-12-27 01:53:46602 class COMPONENT_EXPORT(SQL) StatementRef
603 : public base::RefCounted<StatementRef> {
[email protected]e5ffd0e42009-09-11 21:30:56604 public:
Victor Costan3b02cdf2018-07-18 00:39:56605 REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
606
Victor Costancfbfa602018-08-01 23:24:46607 // |database| is the sql::Database instance associated with
[email protected]41a97c812013-02-07 02:35:38608 // the statement, and is used for tracking outstanding statements
Victor Costanbd623112018-07-18 04:17:27609 // and for error handling. Set to nullptr for invalid or untracked
610 // refs. |stmt| is the actual statement, and should only be null
[email protected]41a97c812013-02-07 02:35:38611 // to create an invalid ref. |was_valid| indicates whether the
Etienne Bergeron95a01c2a2019-02-26 21:32:50612 // statement should be considered valid for diagnostic purposes.
Victor Costancfbfa602018-08-01 23:24:46613 // |was_valid| can be true for a null |stmt| if the Database has
[email protected]41a97c812013-02-07 02:35:38614 // been forcibly closed by an error handler.
Victor Costancfbfa602018-08-01 23:24:46615 StatementRef(Database* database, sqlite3_stmt* stmt, bool was_valid);
[email protected]e5ffd0e42009-09-11 21:30:56616
617 // When true, the statement can be used.
618 bool is_valid() const { return !!stmt_; }
619
[email protected]41a97c812013-02-07 02:35:38620 // When true, the statement is either currently valid, or was
Victor Costancfbfa602018-08-01 23:24:46621 // previously valid but the database was forcibly closed. Used
[email protected]41a97c812013-02-07 02:35:38622 // for diagnostic checks.
623 bool was_valid() const { return was_valid_; }
624
Victor Costancfbfa602018-08-01 23:24:46625 // If we've not been linked to a database, this will be null.
Victor Costanbd623112018-07-18 04:17:27626 //
Victor Costancfbfa602018-08-01 23:24:46627 // TODO(shess): database_ can be nullptr in case of
Victor Costanbd623112018-07-18 04:17:27628 // GetUntrackedStatement(), which prevents Statement::OnError() from
629 // forwarding errors.
Victor Costancfbfa602018-08-01 23:24:46630 Database* database() const { return database_; }
[email protected]e5ffd0e42009-09-11 21:30:56631
632 // Returns the sqlite statement if any. If the statement is not active,
Victor Costanbd623112018-07-18 04:17:27633 // this will return nullptr.
[email protected]e5ffd0e42009-09-11 21:30:56634 sqlite3_stmt* stmt() const { return stmt_; }
635
Victor Costanbd623112018-07-18 04:17:27636 // Destroys the compiled statement and sets it to nullptr. The statement
637 // will no longer be active. |forced| is used to indicate if
Victor Costancfbfa602018-08-01 23:24:46638 // orderly-shutdown checks should apply (see Database::RazeAndClose()).
[email protected]41a97c812013-02-07 02:35:38639 void Close(bool forced);
[email protected]e5ffd0e42009-09-11 21:30:56640
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54641 // Construct a ScopedBlockingCall to annotate IO calls, but only if
Etienne Bergerone7681c72020-01-17 00:51:20642 // database wasn't open in memory. ScopedBlockingCall uses |from_here| to
643 // declare its blocking execution scope (see https://www.crbug/934302).
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54644 void InitScopedBlockingCall(
Etienne Bergerone7681c72020-01-17 00:51:20645 const base::Location& from_here,
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54646 base::Optional<base::ScopedBlockingCall>* scoped_blocking_call) const {
Victor Costancfbfa602018-08-01 23:24:46647 if (database_)
Etienne Bergerone7681c72020-01-17 00:51:20648 database_->InitScopedBlockingCall(from_here, scoped_blocking_call);
Victor Costanc7e7f2e2018-07-18 20:07:55649 }
[email protected]35f7e5392012-07-27 19:54:50650
[email protected]e5ffd0e42009-09-11 21:30:56651 private:
[email protected]877d55d2009-11-05 21:53:08652 friend class base::RefCounted<StatementRef>;
653
654 ~StatementRef();
655
Victor Costancfbfa602018-08-01 23:24:46656 Database* database_;
[email protected]e5ffd0e42009-09-11 21:30:56657 sqlite3_stmt* stmt_;
[email protected]41a97c812013-02-07 02:35:38658 bool was_valid_;
[email protected]e5ffd0e42009-09-11 21:30:56659
660 DISALLOW_COPY_AND_ASSIGN(StatementRef);
661 };
662 friend class StatementRef;
663
664 // Executes a rollback statement, ignoring all transaction state. Used
665 // internally in the transaction management code.
666 void DoRollback();
667
668 // Called by a StatementRef when it's being created or destroyed. See
669 // open_statements_ below.
670 void StatementRefCreated(StatementRef* ref);
671 void StatementRefDeleted(StatementRef* ref);
672
[email protected]2f496b42013-09-26 18:36:58673 // Called when a sqlite function returns an error, which is passed
674 // as |err|. The return value is the error code to be reflected
Victor Costanbd623112018-07-18 04:17:27675 // back to client code. |stmt| is non-null if the error relates to
676 // an sql::Statement instance. |sql| is non-nullptr if the error
[email protected]2f496b42013-09-26 18:36:58677 // relates to non-statement sql code (Execute, for instance). Both
Victor Costanbd623112018-07-18 04:17:27678 // can be null, but both should never be set.
[email protected]2f496b42013-09-26 18:36:58679 // NOTE(shess): Originally, the return value was intended to allow
680 // error handlers to transparently convert errors into success.
681 // Unfortunately, transactions are not generally restartable, so
682 // this did not work out.
shess9e77283d2016-06-13 23:53:20683 int OnSqliteError(int err, Statement* stmt, const char* sql) const;
[email protected]faa604e2009-09-25 22:38:59684
[email protected]5b96f3772010-09-28 16:30:57685 // Like |Execute()|, but retries if the database is locked.
Victor Costancfbfa602018-08-01 23:24:46686 bool ExecuteWithTimeout(const char* sql,
687 base::TimeDelta ms_timeout) WARN_UNUSED_RESULT;
[email protected]5b96f3772010-09-28 16:30:57688
shess9e77283d2016-06-13 23:53:20689 // Implementation helper for GetUniqueStatement() and GetUntrackedStatement().
690 // |tracking_db| is the db the resulting ref should register with for
Victor Costanbd623112018-07-18 04:17:27691 // outstanding statement tracking, which should be |this| to track or null to
shess9e77283d2016-06-13 23:53:20692 // not track.
Victor Costancfbfa602018-08-01 23:24:46693 scoped_refptr<StatementRef> GetStatementImpl(sql::Database* tracking_db,
694 const char* sql) const;
shess9e77283d2016-06-13 23:53:20695
696 // Helper for implementing const member functions. Like GetUniqueStatement(),
697 // except the StatementRef is not entered into |open_statements_|, so an
698 // outstanding StatementRef from this function can block closing the database.
699 // The StatementRef will not call OnSqliteError(), because that can call
700 // |error_callback_| which can close the database.
[email protected]2eec0a22012-07-24 01:59:58701 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const;
702
Victor Costancfbfa602018-08-01 23:24:46703 bool IntegrityCheckHelper(const char* pragma_sql,
704 std::vector<std::string>* messages)
705 WARN_UNUSED_RESULT;
[email protected]579446c2013-12-16 18:36:52706
shess7dbd4dee2015-10-06 17:39:16707 // Release page-cache memory if memory-mapped I/O is enabled and the database
708 // was changed. Passing true for |implicit_change_performed| allows
709 // overriding the change detection for cases like DDL (CREATE, DROP, etc),
710 // which do not participate in the total-rows-changed tracking.
711 void ReleaseCacheMemoryIfNeeded(bool implicit_change_performed);
712
shessc8cd2a162015-10-22 20:30:46713 // Returns the results of sqlite3_db_filename(), which should match the path
714 // passed to Open().
715 base::FilePath DbPath() const;
716
shessc8cd2a162015-10-22 20:30:46717 // Helper to collect diagnostic info for a corrupt database.
718 std::string CollectCorruptionInfo();
719
720 // Helper to collect diagnostic info for errors.
721 std::string CollectErrorInfo(int error, Statement* stmt) const;
722
shessd90aeea82015-11-13 02:24:31723 // Calculates a value appropriate to pass to "PRAGMA mmap_size = ". So errors
724 // can make it unsafe to map a file, so the file is read using regular I/O,
725 // with any errors causing 0 (don't map anything) to be returned. If the
726 // entire file is read without error, a large value is returned which will
727 // allow the entire file to be mapped in most cases.
728 //
729 // Results are recorded in the database's meta table for future reference, so
730 // the file should only be read through once.
731 size_t GetAppropriateMmapSize();
732
shessa62504d2016-11-07 19:26:12733 // Helpers for GetAppropriateMmapSize().
734 bool GetMmapAltStatus(int64_t* status);
735 bool SetMmapAltStatus(int64_t status);
736
Victor Costanbd623112018-07-18 04:17:27737 // The actual sqlite database. Will be null before Init has been called or if
[email protected]e5ffd0e42009-09-11 21:30:56738 // Init resulted in an error.
739 sqlite3* db_;
740
741 // Parameters we'll configure in sqlite before doing anything else. Zero means
742 // use the default value.
743 int page_size_;
744 int cache_size_;
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57745
[email protected]e5ffd0e42009-09-11 21:30:56746 bool exclusive_locking_;
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57747 bool want_wal_mode_;
[email protected]e5ffd0e42009-09-11 21:30:56748
Victor Costanc7e7f2e2018-07-18 20:07:55749 // Holds references to all cached statements so they remain active.
750 //
751 // flat_map is appropriate here because the codebase has ~400 cached
752 // statements, and each statement is at most one insertion in the map
753 // throughout a process' lifetime.
754 base::flat_map<StatementID, scoped_refptr<StatementRef>> statement_cache_;
[email protected]e5ffd0e42009-09-11 21:30:56755
756 // A list of all StatementRefs we've given out. Each ref must register with
757 // us when it's created or destroyed. This allows us to potentially close
758 // any open statements when we encounter an error.
Victor Costanc7e7f2e2018-07-18 20:07:55759 std::set<StatementRef*> open_statements_;
[email protected]e5ffd0e42009-09-11 21:30:56760
761 // Number of currently-nested transactions.
762 int transaction_nesting_;
763
764 // True if any of the currently nested transactions have been rolled back.
765 // When we get to the outermost transaction, this will determine if we do
766 // a rollback instead of a commit.
767 bool needs_rollback_;
768
[email protected]35f7e5392012-07-27 19:54:50769 // True if database is open with OpenInMemory(), False if database is open
770 // with Open().
771 bool in_memory_;
772
Victor Costancfbfa602018-08-01 23:24:46773 // |true| if the Database was closed using RazeAndClose(). Used
[email protected]41a97c812013-02-07 02:35:38774 // to enable diagnostics to distinguish calls to never-opened
775 // databases (incorrect use of the API) from calls to once-valid
776 // databases.
777 bool poisoned_;
778
shessa62504d2016-11-07 19:26:12779 // |true| to use alternate storage for tracking mmap status.
780 bool mmap_alt_status_;
781
Victor Costancfbfa602018-08-01 23:24:46782 // |true| if SQLite memory-mapped I/O is not desired for this database.
shess7dbd4dee2015-10-06 17:39:16783 bool mmap_disabled_;
784
Victor Costancfbfa602018-08-01 23:24:46785 // |true| if SQLite memory-mapped I/O was enabled for this database.
shess7dbd4dee2015-10-06 17:39:16786 // Used by ReleaseCacheMemoryIfNeeded().
787 bool mmap_enabled_;
788
789 // Used by ReleaseCacheMemoryIfNeeded() to track if new changes have happened
790 // since memory was last released.
791 int total_changes_at_last_release_;
792
[email protected]c3881b372013-05-17 08:39:46793 ErrorCallback error_callback_;
794
[email protected]210ce0af2013-05-15 09:10:39795 // Tag for auxiliary histograms.
796 std::string histogram_tag_;
[email protected]c088e3a32013-01-03 23:59:14797
shess58b8df82015-06-03 00:19:32798 // Linear histogram for RecordEvent().
799 base::HistogramBase* stats_histogram_;
800
ssid3be5b1ec2016-01-13 14:21:57801 // Stores the dump provider object when db is open.
Victor Costancfbfa602018-08-01 23:24:46802 std::unique_ptr<DatabaseMemoryDumpProvider> memory_dump_provider_;
ssid3be5b1ec2016-01-13 14:21:57803
Victor Costancfbfa602018-08-01 23:24:46804 DISALLOW_COPY_AND_ASSIGN(Database);
[email protected]e5ffd0e42009-09-11 21:30:56805};
806
807} // namespace sql
808
Victor Costancfbfa602018-08-01 23:24:46809#endif // SQL_DATABASE_H_