blob: eea13801f8db614b73b43ea9ef405bf69f2f293b [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"
Shubham Aggarwal7b60fe6e2020-10-15 06:00:2820#include "base/feature_list.h"
shessc8cd2a162015-10-22 20:30:4621#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1522#include "base/memory/ref_counted.h"
Victor Costan12daa3ac92018-07-19 01:05:5823#include "base/sequence_checker.h"
Victor Costan83d940d62021-07-13 00:15:2024#include "base/strings/string_piece.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"
Shubham Aggarwal7b60fe6e2020-10-15 06:00:2827#include "sql/sql_features.h"
Victor Costan12daa3ac92018-07-19 01:05:5828#include "sql/statement_id.h"
Anton Bikineev3ac3d302021-05-15 17:54:0129#include "third_party/abseil-cpp/absl/types/optional.h"
[email protected]e5ffd0e42009-09-11 21:30:5630
[email protected]e5ffd0e42009-09-11 21:30:5631struct sqlite3;
32struct sqlite3_stmt;
33
[email protected]a3ef4832013-02-02 05:12:3334namespace base {
35class FilePath;
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:3246namespace test {
shess976814402016-06-21 06:56:2547class ScopedErrorExpecter;
Victor Costan87cf8c72018-07-19 19:36:0448} // namespace test
shess58b8df82015-06-03 00:19:3249
Shubham Aggarwal7b60fe6e2020-10-15 06:00:2850struct COMPONENT_EXPORT(SQL) DatabaseOptions {
51 // Default page size for newly created databases.
52 //
53 // Guaranteed to match SQLITE_DEFAULT_PAGE_SIZE.
54 static constexpr int kDefaultPageSize = 4096;
55
56 // If true, the database can only be opened by one process at a time.
57 //
Shubham Aggarwalb30a0cee2021-01-28 15:11:2358 // SQLite supports a locking protocol that allows multiple processes to safely
59 // operate on the same database at the same time. The locking protocol is used
60 // on every transaction, and comes with a small performance penalty.
61 //
62 // Setting this to true causes the locking protocol to be used once, when the
63 // database is opened. No other process will be able to access the database at
64 // the same time.
65 //
66 // More details at https://www.sqlite.org/pragma.html#pragma_locking_mode
67 //
68 // SQLite's locking protocol is summarized at
69 // https://www.sqlite.org/c3ref/io_methods.html
70 //
Shubham Aggarwal7b60fe6e2020-10-15 06:00:2871 // Exclusive mode is strongly recommended. It reduces the I/O cost of setting
72 // up a transaction. It also removes the need of handling transaction failures
73 // due to lock contention.
74 bool exclusive_locking = true;
75
76 // If true, enables SQLite's Write-Ahead Logging (WAL).
77 //
78 // WAL integration is under development, and should not be used in shipping
79 // Chrome features yet. In particular, our custom database recovery code does
80 // not support the WAL log file.
81 //
Shubham Aggarwalb30a0cee2021-01-28 15:11:2382 // WAL mode is currently not fully supported on FuchsiaOS. It will only be
83 // turned on if the database is also using exclusive locking mode.
84 // (https://crbug.com/1082059)
85 //
86 // Note: Changing page size is not supported when in WAL mode. So running
87 // 'PRAGMA page_size = <new-size>' will result in no-ops.
88 //
Shubham Aggarwal7b60fe6e2020-10-15 06:00:2889 // More details at https://www.sqlite.org/wal.html
90 bool wal_mode =
91 base::FeatureList::IsEnabled(sql::features::kEnableWALModeByDefault);
92
93 // Database page size.
94 //
Victor Costan9d1c8754b2021-07-13 02:53:2995 // New Chrome features should set an explicit page size in their
96 // DatabaseOptions initializers, even if they use the default page size. This
97 // makes it easier to track the page size used by the databases on the users'
98 // devices.
99 //
100 // The value in this option is only applied to newly created databases. In
101 // other words, changing the value doesn't impact the databases that have
102 // already been created on the users' devices. So, changing the value in the
103 // code without a lot of work (re-creating existing databases) will result in
104 // inconsistent page sizes across the fleet of user devices, which will make
105 // it (even) more difficult to reason about database performance.
106 //
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28107 // Larger page sizes result in shallower B-trees, because they allow an inner
108 // page to hold more keys. On the flip side, larger page sizes may result in
109 // more I/O when making small changes to existing records.
Shubham Aggarwalb30a0cee2021-01-28 15:11:23110 //
111 // Must be a power of two between 512 and 65536 inclusive.
Victor Costan9d1c8754b2021-07-13 02:53:29112 //
113 // TODO(pwnall): Replace the default with an invalid value after all
114 // sql::Database users explicitly initialize page_size.
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28115 int page_size = kDefaultPageSize;
116
117 // The size of in-memory cache, in pages.
118 //
Victor Costan9d1c8754b2021-07-13 02:53:29119 // New Chrome features should set an explicit cache size in their
120 // DatabaseOptions initializers, even if they use the default cache size. This
121 // makes it easier to track the cache size used by the databases on the users'
122 // devices. The default page size of 4,096 bytes results in a cache size of
123 // 500 pages.
124 //
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28125 // SQLite's database cache will take up at most (`page_size` * `cache_size`)
126 // bytes of RAM.
127 //
128 // 0 invokes SQLite's default, which is currently to size up the cache to use
129 // exactly 2,048,000 bytes of RAM.
Victor Costan9d1c8754b2021-07-13 02:53:29130 //
131 // TODO(pwnall): Replace the default with an invalid value after all
132 // sql::Database users explicitly initialize page_size.
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28133 int cache_size = 0;
134};
135
Victor Costan87cf8c72018-07-19 19:36:04136// Handle to an open SQLite database.
137//
138// Instances of this class are thread-unsafe and DCHECK that they are accessed
139// on the same sequence.
Victor Costan9d1c8754b2021-07-13 02:53:29140//
141// When a Database instance goes out of scope, any uncommitted transactions are
142// rolled back.
Victor Costane56cc682018-12-27 01:53:46143class COMPONENT_EXPORT(SQL) Database {
[email protected]e5ffd0e42009-09-11 21:30:56144 private:
145 class StatementRef; // Forward declaration, see real one below.
146
147 public:
Victor Costan9d1c8754b2021-07-13 02:53:29148 // Creates an instance that can receive Open() / OpenInMemory() calls.
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28149 //
Victor Costan9d1c8754b2021-07-13 02:53:29150 // Some `options` members are only applied to newly created databases.
151 //
152 // Most operations on the new instance will fail until Open() / OpenInMemory()
153 // is called.
154 explicit Database(DatabaseOptions options);
155
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28156 // This constructor is deprecated.
Victor Costan9d1c8754b2021-07-13 02:53:29157 //
158 // When transitioning away from this default constructor, consider setting
159 // DatabaseOptions::explicit_locking to true. For historical reasons, this
160 // constructor results in DatabaseOptions::explicit_locking set to false.
161 //
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28162 // TODO(crbug.com/1126968): Remove this constructor after migrating all
163 // uses to the explicit constructor below.
Victor Costancfbfa602018-08-01 23:24:46164 Database();
Victor Costan9d1c8754b2021-07-13 02:53:29165
Victor Costan00c76432021-07-07 16:55:58166 Database(const Database&) = delete;
167 Database& operator=(const Database&) = delete;
Victor Costancfbfa602018-08-01 23:24:46168 ~Database();
[email protected]e5ffd0e42009-09-11 21:30:56169
Ken Rockot01687422020-08-17 18:00:59170 // Allows mmapping to be disabled globally by default in the calling process.
171 // Must be called before any threads attempt to create a Database.
172 //
173 // TODO(crbug.com/1117049): Remove this global configuration.
174 static void DisableMmapByDefault();
175
[email protected]e5ffd0e42009-09-11 21:30:56176 // Pre-init configuration ----------------------------------------------------
177
Victor Costan7f6abbbe2018-07-29 02:57:27178 // The page size that will be used when creating a new database.
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28179 int page_size() const { return options_.page_size; }
Victor Costan7f6abbbe2018-07-29 02:57:27180
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57181 // Returns whether a database will be opened in WAL mode.
182 bool UseWALMode() const;
183
shessa62504d2016-11-07 19:26:12184 // Call to use alternative status-tracking for mmap. Usually this is tracked
185 // in the meta table, but some databases have no meta table.
186 // TODO(shess): Maybe just have all databases use the alt option?
187 void set_mmap_alt_status() { mmap_alt_status_ = true; }
188
Victor Costan87cf8c72018-07-19 19:36:04189 // Opt out of memory-mapped file I/O.
shess7dbd4dee2015-10-06 17:39:16190 void set_mmap_disabled() { mmap_disabled_ = true; }
191
[email protected]c3881b372013-05-17 08:39:46192 // Set an error-handling callback. On errors, the error number (and
193 // statement, if available) will be passed to the callback.
194 //
195 // If no callback is set, the default action is to crash in debug
196 // mode or return failure in release mode.
Victor Costanc7e7f2e2018-07-18 20:07:55197 using ErrorCallback = base::RepeatingCallback<void(int, Statement*)>;
[email protected]c3881b372013-05-17 08:39:46198 void set_error_callback(const ErrorCallback& callback) {
199 error_callback_ = callback;
200 }
Victor Costan87cf8c72018-07-19 19:36:04201 bool has_error_callback() const { return !error_callback_.is_null(); }
202 void reset_error_callback() { error_callback_.Reset(); }
[email protected]c3881b372013-05-17 08:39:46203
Victor Costan90dae262021-06-01 21:01:08204 // Developer-friendly database ID used in logging output and memory dumps.
shess58b8df82015-06-03 00:19:32205 void set_histogram_tag(const std::string& tag);
[email protected]c088e3a32013-01-03 23:59:14206
[email protected]579446c2013-12-16 18:36:52207 // Run "PRAGMA integrity_check" and post each line of
208 // results into |messages|. Returns the success of running the
209 // statement - per the SQLite documentation, if no errors are found the
210 // call should succeed, and a single value "ok" should be in messages.
211 bool FullIntegrityCheck(std::vector<std::string>* messages);
212
213 // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method,
214 // interprets the results returning true if the the statement executes
215 // without error and results in a single "ok" value.
216 bool QuickIntegrityCheck() WARN_UNUSED_RESULT;
[email protected]80abf152013-05-22 12:42:42217
afakhry7c9abe72016-08-05 17:33:19218 // Meant to be called from a client error callback so that it's able to
219 // get diagnostic information about the database.
220 std::string GetDiagnosticInfo(int extended_error, Statement* statement);
221
ssid1f4e5362016-12-08 20:41:38222 // Reports memory usage into provided memory dump with the given name.
223 bool ReportMemoryUsage(base::trace_event::ProcessMemoryDump* pmd,
224 const std::string& dump_name);
dskibab4199f82016-11-21 20:16:13225
[email protected]e5ffd0e42009-09-11 21:30:56226 // Initialization ------------------------------------------------------------
227
Victor Costancfbfa602018-08-01 23:24:46228 // Initializes the SQL database for the given file, returning true if the
[email protected]35f2094c2009-12-29 22:46:55229 // file could be opened. You can call this or OpenInMemory.
[email protected]a3ef4832013-02-02 05:12:33230 bool Open(const base::FilePath& path) WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42231
Victor Costancfbfa602018-08-01 23:24:46232 // Initializes the SQL database for a temporary in-memory database. There
[email protected]765b44502009-10-02 05:01:42233 // will be no associated file on disk, and the initial database will be
[email protected]35f2094c2009-12-29 22:46:55234 // empty. You can call this or Open.
[email protected]9fe37552011-12-23 17:07:20235 bool OpenInMemory() WARN_UNUSED_RESULT;
[email protected]765b44502009-10-02 05:01:42236
[email protected]8d409412013-07-19 18:25:30237 // Create a temporary on-disk database. The database will be
238 // deleted after close. This kind of database is similar to
239 // OpenInMemory() for small databases, but can page to disk if the
240 // database becomes large.
241 bool OpenTemporary() WARN_UNUSED_RESULT;
242
[email protected]41a97c812013-02-07 02:35:38243 // Returns true if the database has been successfully opened.
Victor Costan87cf8c72018-07-19 19:36:04244 bool is_open() const { return static_cast<bool>(db_); }
[email protected]e5ffd0e42009-09-11 21:30:56245
246 // Closes the database. This is automatically performed on destruction for
247 // you, but this allows you to close the database early. You must not call
248 // any other functions after closing it. It is permissable to call Close on
249 // an uninitialized or already-closed database.
250 void Close();
251
[email protected]8ada10f2013-12-21 00:42:34252 // Reads the first <cache-size>*<page-size> bytes of the file to prime the
253 // filesystem cache. This can be more efficient than faulting pages
254 // individually. Since this involves blocking I/O, it should only be used if
255 // the caller will immediately read a substantial amount of data from the
256 // database.
[email protected]e5ffd0e42009-09-11 21:30:56257 //
[email protected]8ada10f2013-12-21 00:42:34258 // TODO(shess): Design a set of histograms or an experiment to inform this
259 // decision. Preloading should almost always improve later performance
260 // numbers for this database simply because it pulls operations forward, but
261 // if the data isn't actually used soon then preloading just slows down
262 // everything else.
[email protected]e5ffd0e42009-09-11 21:30:56263 void Preload();
264
Victor Costan52bef812018-12-05 07:41:49265 // Release all non-essential memory associated with this database connection.
266 void TrimMemory();
[email protected]be7995f12013-07-18 18:49:14267
[email protected]8e0c01282012-04-06 19:36:49268 // Raze the database to the ground. This approximates creating a
269 // fresh database from scratch, within the constraints of SQLite's
270 // locking protocol (locks and open handles can make doing this with
271 // filesystem operations problematic). Returns true if the database
272 // was razed.
273 //
274 // false is returned if the database is locked by some other
Carlos Knippschild46800c9f2017-09-02 02:21:43275 // process.
[email protected]8e0c01282012-04-06 19:36:49276 //
277 // NOTE(shess): Raze() will DCHECK in the following situations:
278 // - database is not open.
Victor Costancfbfa602018-08-01 23:24:46279 // - the database has a transaction open.
[email protected]8e0c01282012-04-06 19:36:49280 // - a SQLite issue occurs which is structural in nature (like the
281 // statements used are broken).
282 // Since Raze() is expected to be called in unexpected situations,
283 // these all return false, since it is unlikely that the caller
284 // could fix them.
[email protected]6d42f152012-11-10 00:38:24285 //
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28286 // The database's page size is taken from |options_.page_size|. The
[email protected]6d42f152012-11-10 00:38:24287 // existing database's |auto_vacuum| setting is lost (the
288 // possibility of corruption makes it unreliable to pull it from the
289 // existing database). To re-enable on the empty database requires
290 // running "PRAGMA auto_vacuum = 1;" then "VACUUM".
291 //
292 // NOTE(shess): For Android, SQLITE_DEFAULT_AUTOVACUUM is set to 1,
293 // so Raze() sets auto_vacuum to 1.
294 //
Victor Costancfbfa602018-08-01 23:24:46295 // TODO(shess): Raze() needs a database so cannot clear SQLITE_NOTADB.
296 // TODO(shess): Bake auto_vacuum into Database's API so it can
[email protected]6d42f152012-11-10 00:38:24297 // just pick up the default.
[email protected]8e0c01282012-04-06 19:36:49298 bool Raze();
[email protected]8e0c01282012-04-06 19:36:49299
[email protected]41a97c812013-02-07 02:35:38300 // Breaks all outstanding transactions (as initiated by
[email protected]8d409412013-07-19 18:25:30301 // BeginTransaction()), closes the SQLite database, and poisons the
Victor Costancfbfa602018-08-01 23:24:46302 // object so that all future operations against the Database (or
[email protected]8d409412013-07-19 18:25:30303 // its Statements) fail safely, without side effects.
[email protected]41a97c812013-02-07 02:35:38304 //
[email protected]8d409412013-07-19 18:25:30305 // This is intended as an alternative to Close() in error callbacks.
306 // Close() should still be called at some point.
307 void Poison();
308
309 // Raze() the database and Poison() the handle. Returns the return
310 // value from Raze().
311 // TODO(shess): Rename to RazeAndPoison().
[email protected]41a97c812013-02-07 02:35:38312 bool RazeAndClose();
313
Victor Costancfbfa602018-08-01 23:24:46314 // Delete the underlying database files associated with |path|. This should be
315 // used on a database which is not opened by any Database instance. Open
316 // Database instances pointing to the database can cause odd results or
317 // corruption (for instance if a hot journal is deleted but the associated
318 // database is not).
[email protected]8d2e39e2013-06-24 05:55:08319 //
320 // Returns true if the database file and associated journals no
321 // longer exist, false otherwise. If the database has never
322 // existed, this will return true.
323 static bool Delete(const base::FilePath& path);
324
[email protected]e5ffd0e42009-09-11 21:30:56325 // Transactions --------------------------------------------------------------
326
327 // Transaction management. We maintain a virtual transaction stack to emulate
328 // nested transactions since sqlite can't do nested transactions. The
329 // limitation is you can't roll back a sub transaction: if any transaction
330 // fails, all transactions open will also be rolled back. Any nested
331 // transactions after one has rolled back will return fail for Begin(). If
332 // Begin() fails, you must not call Commit or Rollback().
333 //
334 // Normally you should use sql::Transaction to manage a transaction, which
335 // will scope it to a C++ context.
336 bool BeginTransaction();
337 void RollbackTransaction();
338 bool CommitTransaction();
339
[email protected]8d409412013-07-19 18:25:30340 // Rollback all outstanding transactions. Use with care, there may
341 // be scoped transactions on the stack.
342 void RollbackAllTransactions();
343
[email protected]e5ffd0e42009-09-11 21:30:56344 // Returns the current transaction nesting, which will be 0 if there are
345 // no open transactions.
346 int transaction_nesting() const { return transaction_nesting_; }
347
[email protected]8d409412013-07-19 18:25:30348 // Attached databases---------------------------------------------------------
349
Victor Costan7f6abbbe2018-07-29 02:57:27350 // SQLite supports attaching multiple database files to a single connection.
[email protected]8d409412013-07-19 18:25:30351 //
Victor Costan7f6abbbe2018-07-29 02:57:27352 // Attach the database in |other_db_path| to the current connection under
353 // |attachment_point|. |attachment_point| must only contain characters from
354 // [a-zA-Z0-9_].
Victor Costan8a87f7e52017-11-10 01:29:30355 //
356 // On the SQLite version shipped with Chrome (3.21+, Oct 2017), databases can
357 // be attached while a transaction is opened. However, these databases cannot
Victor Costan70bedf22018-07-18 21:21:14358 // be detached until the transaction is committed or aborted.
Victor Costan7f6abbbe2018-07-29 02:57:27359 //
360 // These APIs are only exposed for use in recovery. They are extremely subtle
361 // and are not useful for features built on top of //sql.
[email protected]8d409412013-07-19 18:25:30362 bool AttachDatabase(const base::FilePath& other_db_path,
Victor Costan83d940d62021-07-13 00:15:20363 base::StringPiece attachment_point,
Victor Costan7f6abbbe2018-07-29 02:57:27364 InternalApiToken);
Victor Costan83d940d62021-07-13 00:15:20365 bool DetachDatabase(base::StringPiece attachment_point, InternalApiToken);
[email protected]8d409412013-07-19 18:25:30366
[email protected]e5ffd0e42009-09-11 21:30:56367 // Statements ----------------------------------------------------------------
368
369 // Executes the given SQL string, returning true on success. This is
370 // normally used for simple, 1-off statements that don't take any bound
371 // parameters and don't return any data (e.g. CREATE TABLE).
[email protected]9fe37552011-12-23 17:07:20372 //
[email protected]eff1fa522011-12-12 23:50:59373 // This will DCHECK if the |sql| contains errors.
[email protected]9fe37552011-12-23 17:07:20374 //
375 // Do not use ignore_result() to ignore all errors. Use
376 // ExecuteAndReturnErrorCode() and ignore only specific errors.
377 bool Execute(const char* sql) WARN_UNUSED_RESULT;
[email protected]e5ffd0e42009-09-11 21:30:56378
[email protected]eff1fa522011-12-12 23:50:59379 // Like Execute(), but returns the error code given by SQLite.
[email protected]9fe37552011-12-23 17:07:20380 int ExecuteAndReturnErrorCode(const char* sql) WARN_UNUSED_RESULT;
[email protected]eff1fa522011-12-12 23:50:59381
[email protected]e5ffd0e42009-09-11 21:30:56382 // Returns a statement for the given SQL using the statement cache. It can
383 // take a nontrivial amount of work to parse and compile a statement, so
384 // keeping commonly-used ones around for future use is important for
385 // performance.
386 //
Victor Costan613b4302018-11-20 05:32:43387 // The SQL_FROM_HERE macro is the recommended way of generating a StatementID.
388 // Code that generates custom IDs must ensure that a StatementID is never used
389 // for different SQL statements. Failing to meet this requirement results in
390 // incorrect behavior, and should be caught by a DCHECK.
391 //
392 // The SQL statement passed in |sql| must match the SQL statement reported
393 // back by SQLite. Mismatches are caught by a DCHECK, so any code that has
394 // automated test coverage or that was manually tested on a DCHECK build will
395 // not exhibit this problem. Mismatches generally imply that the statement
396 // passed in has extra whitespace or comments surrounding it, which waste
397 // storage and CPU cycles.
398 //
[email protected]eff1fa522011-12-12 23:50:59399 // If the |sql| has an error, an invalid, inert StatementRef is returned (and
400 // the code will crash in debug). The caller must deal with this eventuality,
401 // either by checking validity of the |sql| before calling, by correctly
402 // handling the return of an inert statement, or both.
[email protected]e5ffd0e42009-09-11 21:30:56403 //
[email protected]e5ffd0e42009-09-11 21:30:56404 // Example:
Victor Costancfbfa602018-08-01 23:24:46405 // sql::Statement stmt(database_.GetCachedStatement(
[email protected]3273dce2010-01-27 16:08:08406 // SQL_FROM_HERE, "SELECT * FROM foo"));
[email protected]e5ffd0e42009-09-11 21:30:56407 // if (!stmt)
408 // return false; // Error creating statement.
Victor Costan12daa3ac92018-07-19 01:05:58409 scoped_refptr<StatementRef> GetCachedStatement(StatementID id,
[email protected]e5ffd0e42009-09-11 21:30:56410 const char* sql);
411
[email protected]eff1fa522011-12-12 23:50:59412 // Used to check a |sql| statement for syntactic validity. If the statement is
413 // valid SQL, returns true.
414 bool IsSQLValid(const char* sql);
415
[email protected]e5ffd0e42009-09-11 21:30:56416 // Returns a non-cached statement for the given SQL. Use this for SQL that
417 // is only executed once or only rarely (there is overhead associated with
418 // keeping a statement cached).
419 //
420 // See GetCachedStatement above for examples and error information.
421 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql);
422
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57423 // Performs a passive checkpoint on the main attached database if it is in
424 // WAL mode. Returns true if the checkpoint was successful and false in case
425 // of an error. It is a no-op if the database is not in WAL mode.
426 //
427 // Note: Checkpointing is a very slow operation and will block any writes
428 // until it is finished. Please use with care.
429 bool CheckpointDatabase();
430
[email protected]e5ffd0e42009-09-11 21:30:56431 // Info querying -------------------------------------------------------------
432
shessa62504d2016-11-07 19:26:12433 // Returns true if the given structure exists. Instead of test-then-create,
434 // callers should almost always prefer the "IF NOT EXISTS" version of the
435 // CREATE statement.
Victor Costan83d940d62021-07-13 00:15:20436 bool DoesIndexExist(base::StringPiece index_name) const;
437 bool DoesTableExist(base::StringPiece table_name) const;
438 bool DoesViewExist(base::StringPiece table_name) const;
[email protected]e2cadec82011-12-13 02:00:53439
[email protected]e5ffd0e42009-09-11 21:30:56440 // Returns true if a column with the given name exists in the given table.
Victor Costan1ff47e92018-12-07 11:10:43441 //
442 // Calling this method on a VIEW returns an unspecified result.
443 //
444 // This should only be used by migration code for legacy features that do not
445 // use MetaTable, and need an alternative way of figuring out the database's
446 // current version.
[email protected]1ed78a32009-09-15 20:24:17447 bool DoesColumnExist(const char* table_name, const char* column_name) const;
[email protected]e5ffd0e42009-09-11 21:30:56448
449 // Returns sqlite's internal ID for the last inserted row. Valid only
450 // immediately after an insert.
tfarina720d4f32015-05-11 22:31:26451 int64_t GetLastInsertRowId() const;
[email protected]e5ffd0e42009-09-11 21:30:56452
[email protected]1ed78a32009-09-15 20:24:17453 // Returns sqlite's count of the number of rows modified by the last
454 // statement executed. Will be 0 if no statement has executed or the database
455 // is closed.
456 int GetLastChangeCount() const;
457
Victor Costand6e73252020-10-14 21:11:25458 // Approximates the amount of memory used by SQLite for this database.
459 //
460 // This measures the memory used for the page cache (most likely the biggest
461 // consumer), database schema, and prepared statements.
462 //
463 // The memory used by the page cache can be recovered by calling TrimMemory(),
464 // which will cause SQLite to drop the page cache.
465 int GetMemoryUsage();
466
[email protected]e5ffd0e42009-09-11 21:30:56467 // Errors --------------------------------------------------------------------
468
469 // Returns the error code associated with the last sqlite operation.
470 int GetErrorCode() const;
471
[email protected]767718e52010-09-21 23:18:49472 // Returns the errno associated with GetErrorCode(). See
473 // SQLITE_LAST_ERRNO in SQLite documentation.
474 int GetLastErrno() const;
475
[email protected]e5ffd0e42009-09-11 21:30:56476 // Returns a pointer to a statically allocated string associated with the
477 // last sqlite operation.
478 const char* GetErrorMessage() const;
479
[email protected]92cd00a2013-08-16 11:09:58480 // Return a reproducible representation of the schema equivalent to
481 // running the following statement at a sqlite3 command-line:
482 // SELECT type, name, tbl_name, sql FROM sqlite_master ORDER BY 1, 2, 3, 4;
483 std::string GetSchema() const;
484
shess976814402016-06-21 06:56:25485 // Returns |true| if there is an error expecter (see SetErrorExpecter), and
486 // that expecter returns |true| when passed |error|. Clients which provide an
487 // |error_callback| should use IsExpectedSqliteError() to check for unexpected
Sigurdur Asgeirsson8d82bd02017-09-25 21:05:52488 // errors; if one is detected, DLOG(DCHECK) is generally appropriate (see
shess976814402016-06-21 06:56:25489 // OnSqliteError implementation).
490 static bool IsExpectedSqliteError(int error);
[email protected]74cdede2013-09-25 05:39:57491
Victor Costance678e72018-07-24 10:25:00492 // Computes the path of a database's rollback journal.
493 //
494 // The journal file is created at the beginning of the database's first
495 // transaction. The file may be removed and re-created between transactions,
496 // depending on whether the database is opened in exclusive mode, and on
497 // configuration options. The journal file does not exist when the database
498 // operates in WAL mode.
499 //
500 // This is intended for internal use and tests. To preserve our ability to
501 // iterate on our SQLite configuration, features must avoid relying on
502 // the existence of specific files.
503 static base::FilePath JournalPath(const base::FilePath& db_path);
504
505 // Computes the path of a database's write-ahead log (WAL).
506 //
507 // The WAL file exists while a database is opened in WAL mode.
508 //
509 // This is intended for internal use and tests. To preserve our ability to
510 // iterate on our SQLite configuration, features must avoid relying on
511 // the existence of specific files.
512 static base::FilePath WriteAheadLogPath(const base::FilePath& db_path);
513
514 // Computes the path of a database's shared memory (SHM) file.
515 //
516 // The SHM file is used to coordinate between multiple processes using the
517 // same database in WAL mode. Thus, this file only exists for databases using
518 // WAL and not opened in exclusive mode.
519 //
520 // This is intended for internal use and tests. To preserve our ability to
521 // iterate on our SQLite configuration, features must avoid relying on
522 // the existence of specific files.
523 static base::FilePath SharedMemoryFilePath(const base::FilePath& db_path);
524
Victor Costan7f6abbbe2018-07-29 02:57:27525 // Internal state accessed by other classes in //sql.
526 sqlite3* db(InternalApiToken) const { return db_; }
527 bool poisoned(InternalApiToken) const { return poisoned_; }
528
529 private:
shess976814402016-06-21 06:56:25530 // Allow test-support code to set/reset error expecter.
531 friend class test::ScopedErrorExpecter;
[email protected]4350e322013-06-18 22:18:10532
[email protected]eff1fa522011-12-12 23:50:59533 // Statement accesses StatementRef which we don't want to expose to everybody
[email protected]e5ffd0e42009-09-11 21:30:56534 // (they should go through Statement).
535 friend class Statement;
536
Victor Costancfbfa602018-08-01 23:24:46537 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, CachedStatement);
538 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, CollectDiagnosticInfo);
539 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, GetAppropriateMmapSize);
540 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, GetAppropriateMmapSizeAltStatus);
541 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, OnMemoryDump);
542 FRIEND_TEST_ALL_PREFIXES(SQLDatabaseTest, RegisterIntentToUpload);
shessf7fcc452017-04-19 22:10:41543 FRIEND_TEST_ALL_PREFIXES(SQLiteFeaturesTest, WALNoClose);
shessc8cd2a162015-10-22 20:30:46544
[email protected]765b44502009-10-02 05:01:42545 // Internal initialize function used by both Init and InitInMemory. The file
546 // name is always 8 bits since we want to use the 8-bit version of
547 // sqlite3_open. The string can also be sqlite's special ":memory:" string.
[email protected]fed734a2013-07-17 04:45:13548 //
549 // |retry_flag| controls retrying the open if the error callback
550 // addressed errors using RazeAndClose().
Victor Costancfbfa602018-08-01 23:24:46551 enum Retry { NO_RETRY = 0, RETRY_ON_POISON };
[email protected]fed734a2013-07-17 04:45:13552 bool OpenInternal(const std::string& file_name, Retry retry_flag);
[email protected]765b44502009-10-02 05:01:42553
[email protected]41a97c812013-02-07 02:35:38554 // Internal close function used by Close() and RazeAndClose().
555 // |forced| indicates that orderly-shutdown checks should not apply.
556 void CloseInternal(bool forced);
557
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54558 // Construct a ScopedBlockingCall to annotate IO calls, but only if
Etienne Bergerone7681c72020-01-17 00:51:20559 // database wasn't open in memory. ScopedBlockingCall uses |from_here| to
560 // declare its blocking execution scope (see https://www.crbug/934302).
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54561 void InitScopedBlockingCall(
Etienne Bergerone7681c72020-01-17 00:51:20562 const base::Location& from_here,
Anton Bikineev3ac3d302021-05-15 17:54:01563 absl::optional<base::ScopedBlockingCall>* scoped_blocking_call) const {
[email protected]35f7e5392012-07-27 19:54:50564 if (!in_memory_)
Etienne Bergerone7681c72020-01-17 00:51:20565 scoped_blocking_call->emplace(from_here, base::BlockingType::MAY_BLOCK);
[email protected]35f7e5392012-07-27 19:54:50566 }
567
shessa62504d2016-11-07 19:26:12568 // Internal helper for Does*Exist() functions.
Victor Costan83d940d62021-07-13 00:15:20569 bool DoesSchemaItemExist(base::StringPiece name,
570 base::StringPiece type) const;
[email protected]e2cadec82011-12-13 02:00:53571
shess976814402016-06-21 06:56:25572 // Accessors for global error-expecter, for injecting behavior during tests.
573 // See test/scoped_error_expecter.h.
Victor Costanc7e7f2e2018-07-18 20:07:55574 using ErrorExpecterCallback = base::RepeatingCallback<bool(int)>;
shess976814402016-06-21 06:56:25575 static ErrorExpecterCallback* current_expecter_cb_;
576 static void SetErrorExpecter(ErrorExpecterCallback* expecter);
577 static void ResetErrorExpecter();
[email protected]4350e322013-06-18 22:18:10578
[email protected]e5ffd0e42009-09-11 21:30:56579 // A StatementRef is a refcounted wrapper around a sqlite statement pointer.
580 // Refcounting allows us to give these statements out to sql::Statement
581 // objects while also optionally maintaining a cache of compiled statements
582 // by just keeping a refptr to these objects.
583 //
584 // A statement ref can be valid, in which case it can be used, or invalid to
585 // indicate that the statement hasn't been created yet, has an error, or has
586 // been destroyed.
587 //
Victor Costancfbfa602018-08-01 23:24:46588 // The Database may revoke a StatementRef in some error cases, so callers
[email protected]e5ffd0e42009-09-11 21:30:56589 // should always check validity before using.
Victor Costane56cc682018-12-27 01:53:46590 class COMPONENT_EXPORT(SQL) StatementRef
591 : public base::RefCounted<StatementRef> {
[email protected]e5ffd0e42009-09-11 21:30:56592 public:
Victor Costan3b02cdf2018-07-18 00:39:56593 REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
594
Victor Costancfbfa602018-08-01 23:24:46595 // |database| is the sql::Database instance associated with
[email protected]41a97c812013-02-07 02:35:38596 // the statement, and is used for tracking outstanding statements
Victor Costanbd623112018-07-18 04:17:27597 // and for error handling. Set to nullptr for invalid or untracked
598 // refs. |stmt| is the actual statement, and should only be null
[email protected]41a97c812013-02-07 02:35:38599 // to create an invalid ref. |was_valid| indicates whether the
Etienne Bergeron95a01c2a2019-02-26 21:32:50600 // statement should be considered valid for diagnostic purposes.
Victor Costancfbfa602018-08-01 23:24:46601 // |was_valid| can be true for a null |stmt| if the Database has
[email protected]41a97c812013-02-07 02:35:38602 // been forcibly closed by an error handler.
Victor Costancfbfa602018-08-01 23:24:46603 StatementRef(Database* database, sqlite3_stmt* stmt, bool was_valid);
[email protected]e5ffd0e42009-09-11 21:30:56604
Victor Costan00c76432021-07-07 16:55:58605 StatementRef(const StatementRef&) = delete;
606 StatementRef& operator=(const StatementRef&) = delete;
607
[email protected]e5ffd0e42009-09-11 21:30:56608 // When true, the statement can be used.
609 bool is_valid() const { return !!stmt_; }
610
[email protected]41a97c812013-02-07 02:35:38611 // When true, the statement is either currently valid, or was
Victor Costancfbfa602018-08-01 23:24:46612 // previously valid but the database was forcibly closed. Used
[email protected]41a97c812013-02-07 02:35:38613 // for diagnostic checks.
614 bool was_valid() const { return was_valid_; }
615
Victor Costancfbfa602018-08-01 23:24:46616 // If we've not been linked to a database, this will be null.
Victor Costanbd623112018-07-18 04:17:27617 //
Victor Costancfbfa602018-08-01 23:24:46618 // TODO(shess): database_ can be nullptr in case of
Victor Costanbd623112018-07-18 04:17:27619 // GetUntrackedStatement(), which prevents Statement::OnError() from
620 // forwarding errors.
Victor Costancfbfa602018-08-01 23:24:46621 Database* database() const { return database_; }
[email protected]e5ffd0e42009-09-11 21:30:56622
623 // Returns the sqlite statement if any. If the statement is not active,
Victor Costanbd623112018-07-18 04:17:27624 // this will return nullptr.
[email protected]e5ffd0e42009-09-11 21:30:56625 sqlite3_stmt* stmt() const { return stmt_; }
626
Victor Costanbd623112018-07-18 04:17:27627 // Destroys the compiled statement and sets it to nullptr. The statement
628 // will no longer be active. |forced| is used to indicate if
Victor Costancfbfa602018-08-01 23:24:46629 // orderly-shutdown checks should apply (see Database::RazeAndClose()).
[email protected]41a97c812013-02-07 02:35:38630 void Close(bool forced);
[email protected]e5ffd0e42009-09-11 21:30:56631
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54632 // Construct a ScopedBlockingCall to annotate IO calls, but only if
Etienne Bergerone7681c72020-01-17 00:51:20633 // database wasn't open in memory. ScopedBlockingCall uses |from_here| to
634 // declare its blocking execution scope (see https://www.crbug/934302).
Etienne Pierre-Doraya71d7af2019-02-07 02:07:54635 void InitScopedBlockingCall(
Etienne Bergerone7681c72020-01-17 00:51:20636 const base::Location& from_here,
Anton Bikineev3ac3d302021-05-15 17:54:01637 absl::optional<base::ScopedBlockingCall>* scoped_blocking_call) const {
Victor Costancfbfa602018-08-01 23:24:46638 if (database_)
Etienne Bergerone7681c72020-01-17 00:51:20639 database_->InitScopedBlockingCall(from_here, scoped_blocking_call);
Victor Costanc7e7f2e2018-07-18 20:07:55640 }
[email protected]35f7e5392012-07-27 19:54:50641
[email protected]e5ffd0e42009-09-11 21:30:56642 private:
[email protected]877d55d2009-11-05 21:53:08643 friend class base::RefCounted<StatementRef>;
644
645 ~StatementRef();
646
Victor Costancfbfa602018-08-01 23:24:46647 Database* database_;
[email protected]e5ffd0e42009-09-11 21:30:56648 sqlite3_stmt* stmt_;
[email protected]41a97c812013-02-07 02:35:38649 bool was_valid_;
[email protected]e5ffd0e42009-09-11 21:30:56650 };
651 friend class StatementRef;
652
653 // Executes a rollback statement, ignoring all transaction state. Used
654 // internally in the transaction management code.
655 void DoRollback();
656
657 // Called by a StatementRef when it's being created or destroyed. See
658 // open_statements_ below.
659 void StatementRefCreated(StatementRef* ref);
660 void StatementRefDeleted(StatementRef* ref);
661
[email protected]2f496b42013-09-26 18:36:58662 // Called when a sqlite function returns an error, which is passed
663 // as |err|. The return value is the error code to be reflected
Victor Costanbd623112018-07-18 04:17:27664 // back to client code. |stmt| is non-null if the error relates to
665 // an sql::Statement instance. |sql| is non-nullptr if the error
[email protected]2f496b42013-09-26 18:36:58666 // relates to non-statement sql code (Execute, for instance). Both
Victor Costanbd623112018-07-18 04:17:27667 // can be null, but both should never be set.
[email protected]2f496b42013-09-26 18:36:58668 // NOTE(shess): Originally, the return value was intended to allow
669 // error handlers to transparently convert errors into success.
670 // Unfortunately, transactions are not generally restartable, so
671 // this did not work out.
shess9e77283d2016-06-13 23:53:20672 int OnSqliteError(int err, Statement* stmt, const char* sql) const;
[email protected]faa604e2009-09-25 22:38:59673
[email protected]5b96f3772010-09-28 16:30:57674 // Like |Execute()|, but retries if the database is locked.
Victor Costancfbfa602018-08-01 23:24:46675 bool ExecuteWithTimeout(const char* sql,
676 base::TimeDelta ms_timeout) WARN_UNUSED_RESULT;
[email protected]5b96f3772010-09-28 16:30:57677
shess9e77283d2016-06-13 23:53:20678 // Implementation helper for GetUniqueStatement() and GetUntrackedStatement().
679 // |tracking_db| is the db the resulting ref should register with for
Victor Costanbd623112018-07-18 04:17:27680 // outstanding statement tracking, which should be |this| to track or null to
shess9e77283d2016-06-13 23:53:20681 // not track.
Victor Costancfbfa602018-08-01 23:24:46682 scoped_refptr<StatementRef> GetStatementImpl(sql::Database* tracking_db,
683 const char* sql) const;
shess9e77283d2016-06-13 23:53:20684
685 // Helper for implementing const member functions. Like GetUniqueStatement(),
686 // except the StatementRef is not entered into |open_statements_|, so an
687 // outstanding StatementRef from this function can block closing the database.
688 // The StatementRef will not call OnSqliteError(), because that can call
689 // |error_callback_| which can close the database.
[email protected]2eec0a22012-07-24 01:59:58690 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const;
691
Victor Costancfbfa602018-08-01 23:24:46692 bool IntegrityCheckHelper(const char* pragma_sql,
693 std::vector<std::string>* messages)
694 WARN_UNUSED_RESULT;
[email protected]579446c2013-12-16 18:36:52695
shess7dbd4dee2015-10-06 17:39:16696 // Release page-cache memory if memory-mapped I/O is enabled and the database
697 // was changed. Passing true for |implicit_change_performed| allows
698 // overriding the change detection for cases like DDL (CREATE, DROP, etc),
699 // which do not participate in the total-rows-changed tracking.
700 void ReleaseCacheMemoryIfNeeded(bool implicit_change_performed);
701
shessc8cd2a162015-10-22 20:30:46702 // Returns the results of sqlite3_db_filename(), which should match the path
703 // passed to Open().
704 base::FilePath DbPath() const;
705
shessc8cd2a162015-10-22 20:30:46706 // Helper to collect diagnostic info for a corrupt database.
707 std::string CollectCorruptionInfo();
708
709 // Helper to collect diagnostic info for errors.
710 std::string CollectErrorInfo(int error, Statement* stmt) const;
711
shessd90aeea82015-11-13 02:24:31712 // Calculates a value appropriate to pass to "PRAGMA mmap_size = ". So errors
713 // can make it unsafe to map a file, so the file is read using regular I/O,
714 // with any errors causing 0 (don't map anything) to be returned. If the
715 // entire file is read without error, a large value is returned which will
716 // allow the entire file to be mapped in most cases.
717 //
718 // Results are recorded in the database's meta table for future reference, so
719 // the file should only be read through once.
720 size_t GetAppropriateMmapSize();
721
shessa62504d2016-11-07 19:26:12722 // Helpers for GetAppropriateMmapSize().
723 bool GetMmapAltStatus(int64_t* status);
724 bool SetMmapAltStatus(int64_t status);
725
Victor Costanbd623112018-07-18 04:17:27726 // The actual sqlite database. Will be null before Init has been called or if
[email protected]e5ffd0e42009-09-11 21:30:56727 // Init resulted in an error.
Shubham Aggarwale2d6b60d2020-10-22 04:41:48728 sqlite3* db_ = nullptr;
[email protected]e5ffd0e42009-09-11 21:30:56729
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28730 // TODO([email protected]): Make `options_` const after removing all
731 // setters.
732 DatabaseOptions options_;
[email protected]e5ffd0e42009-09-11 21:30:56733
Victor Costanc7e7f2e2018-07-18 20:07:55734 // Holds references to all cached statements so they remain active.
735 //
736 // flat_map is appropriate here because the codebase has ~400 cached
737 // statements, and each statement is at most one insertion in the map
738 // throughout a process' lifetime.
739 base::flat_map<StatementID, scoped_refptr<StatementRef>> statement_cache_;
[email protected]e5ffd0e42009-09-11 21:30:56740
741 // A list of all StatementRefs we've given out. Each ref must register with
742 // us when it's created or destroyed. This allows us to potentially close
743 // any open statements when we encounter an error.
Victor Costanc7e7f2e2018-07-18 20:07:55744 std::set<StatementRef*> open_statements_;
[email protected]e5ffd0e42009-09-11 21:30:56745
746 // Number of currently-nested transactions.
Shubham Aggarwale2d6b60d2020-10-22 04:41:48747 int transaction_nesting_ = 0;
[email protected]e5ffd0e42009-09-11 21:30:56748
749 // True if any of the currently nested transactions have been rolled back.
750 // When we get to the outermost transaction, this will determine if we do
751 // a rollback instead of a commit.
Shubham Aggarwale2d6b60d2020-10-22 04:41:48752 bool needs_rollback_ = false;
[email protected]e5ffd0e42009-09-11 21:30:56753
[email protected]35f7e5392012-07-27 19:54:50754 // True if database is open with OpenInMemory(), False if database is open
755 // with Open().
Shubham Aggarwale2d6b60d2020-10-22 04:41:48756 bool in_memory_ = false;
[email protected]35f7e5392012-07-27 19:54:50757
Victor Costancfbfa602018-08-01 23:24:46758 // |true| if the Database was closed using RazeAndClose(). Used
[email protected]41a97c812013-02-07 02:35:38759 // to enable diagnostics to distinguish calls to never-opened
760 // databases (incorrect use of the API) from calls to once-valid
761 // databases.
Shubham Aggarwale2d6b60d2020-10-22 04:41:48762 bool poisoned_ = false;
[email protected]41a97c812013-02-07 02:35:38763
shessa62504d2016-11-07 19:26:12764 // |true| to use alternate storage for tracking mmap status.
Shubham Aggarwale2d6b60d2020-10-22 04:41:48765 bool mmap_alt_status_ = false;
shessa62504d2016-11-07 19:26:12766
Victor Costancfbfa602018-08-01 23:24:46767 // |true| if SQLite memory-mapped I/O is not desired for this database.
shess7dbd4dee2015-10-06 17:39:16768 bool mmap_disabled_;
769
Victor Costancfbfa602018-08-01 23:24:46770 // |true| if SQLite memory-mapped I/O was enabled for this database.
shess7dbd4dee2015-10-06 17:39:16771 // Used by ReleaseCacheMemoryIfNeeded().
Shubham Aggarwale2d6b60d2020-10-22 04:41:48772 bool mmap_enabled_ = false;
shess7dbd4dee2015-10-06 17:39:16773
774 // Used by ReleaseCacheMemoryIfNeeded() to track if new changes have happened
775 // since memory was last released.
Shubham Aggarwale2d6b60d2020-10-22 04:41:48776 int total_changes_at_last_release_ = 0;
shess7dbd4dee2015-10-06 17:39:16777
[email protected]c3881b372013-05-17 08:39:46778 ErrorCallback error_callback_;
779
Victor Costan90dae262021-06-01 21:01:08780 // Developer-friendly database ID used in logging output and memory dumps.
[email protected]210ce0af2013-05-15 09:10:39781 std::string histogram_tag_;
[email protected]c088e3a32013-01-03 23:59:14782
ssid3be5b1ec2016-01-13 14:21:57783 // Stores the dump provider object when db is open.
Victor Costancfbfa602018-08-01 23:24:46784 std::unique_ptr<DatabaseMemoryDumpProvider> memory_dump_provider_;
[email protected]e5ffd0e42009-09-11 21:30:56785};
786
787} // namespace sql
788
Victor Costancfbfa602018-08-01 23:24:46789#endif // SQL_DATABASE_H_