Avi Drissman | 69b874f | 2022-09-15 19:11:14 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 5 | #ifndef SQL_META_TABLE_H_ |
| 6 | #define SQL_META_TABLE_H_ |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 7 | |
Victor Costan | e2e042f | 2022-03-29 21:11:34 | [diff] [blame] | 8 | #include <cstdint> |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Victor Costan | e56cc68 | 2018-12-27 01:53:46 | [diff] [blame] | 11 | #include "base/component_export.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
Victor Costan | e2e042f | 2022-03-29 21:11:34 | [diff] [blame] | 13 | #include "base/strings/string_piece_forward.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 14 | |
| 15 | namespace sql { |
| 16 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 17 | class Database; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 18 | |
Carlos Knippschild | 46800c9f | 2017-09-02 02:21:43 | [diff] [blame] | 19 | // Creates and manages a table to store generic metadata. The features provided |
| 20 | // are: |
| 21 | // * An interface for storing multi-typed key-value data. |
| 22 | // * Helper methods to assist in database schema version control. |
| 23 | // * Historical data on past attempts to mmap the database to make it possible |
| 24 | // to avoid unconditionally retrying to load broken databases. |
Victor Costan | e56cc68 | 2018-12-27 01:53:46 | [diff] [blame] | 25 | class COMPONENT_EXPORT(SQL) MetaTable { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 26 | public: |
| 27 | MetaTable(); |
Victor Costan | 00c7643 | 2021-07-07 16:55:58 | [diff] [blame] | 28 | MetaTable(const MetaTable&) = delete; |
| 29 | MetaTable& operator=(const MetaTable&) = delete; |
Andrew Paseltiner | 7d512ddd | 2022-09-16 13:36:52 | [diff] [blame] | 30 | MetaTable(MetaTable&&) = delete; |
| 31 | MetaTable& operator=(MetaTable&&) = delete; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 32 | ~MetaTable(); |
| 33 | |
David Van Cleve | 5f7a09a8 | 2021-07-13 16:28:09 | [diff] [blame] | 34 | // Values for Get/SetMmapStatus(). `kMmapFailure` indicates that there was at |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 35 | // some point a read error and the database should not be memory-mapped, while |
David Van Cleve | 5f7a09a8 | 2021-07-13 16:28:09 | [diff] [blame] | 36 | // `kMmapSuccess` indicates that the entire file was read at some point and |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 37 | // can be memory-mapped without constraint. |
Andrew Grieve | d1978b0e | 2017-07-28 15:53:41 | [diff] [blame] | 38 | static constexpr int64_t kMmapFailure = -2; |
| 39 | static constexpr int64_t kMmapSuccess = -1; |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 40 | |
[email protected] | 24864e4 | 2011-01-10 20:38:51 | [diff] [blame] | 41 | // Returns true if the 'meta' table exists. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 42 | static bool DoesTableExist(Database* db); |
[email protected] | 24864e4 | 2011-01-10 20:38:51 | [diff] [blame] | 43 | |
David Van Cleve | f7b270f | 2021-06-22 13:27:19 | [diff] [blame] | 44 | // Deletes the 'meta' table if it exists, returning false if an internal error |
| 45 | // occurred during the deletion and true otherwise (no matter whether the |
| 46 | // table existed). |
| 47 | static bool DeleteTableForTesting(Database* db); |
| 48 | |
David Van Cleve | 5f7a09a8 | 2021-07-13 16:28:09 | [diff] [blame] | 49 | // If the current version of the database is less than |
| 50 | // `lowest_supported_version`, or the current version is less than the |
| 51 | // database's least compatible version, razes the database. To only enforce |
| 52 | // the latter, pass `kNoLowestSupportedVersion` for |
| 53 | // `lowest_supported_version`. |
| 54 | // |
| 55 | // TODO(crbug.com/1228463): At this time the database is razed IFF meta exists |
| 56 | // and contains a version row with the value not satisfying the constraints. |
| 57 | // It may make sense to also raze if meta exists but has no version row, or if |
| 58 | // meta doesn't exist. In those cases if the database is not already empty, it |
| 59 | // probably resulted from a broken initialization. |
| 60 | // TODO(crbug.com/1228463): Folding this into Init() would allow enforcing |
| 61 | // the version constraint, but Init() is often called in a transaction. |
| 62 | static constexpr int kNoLowestSupportedVersion = 0; |
| 63 | static void RazeIfIncompatible(Database* db, |
| 64 | int lowest_supported_version, |
| 65 | int current_version); |
[email protected] | fe4e3de | 2013-10-08 02:19:17 | [diff] [blame] | 66 | |
Carlos Knippschild | 46800c9f | 2017-09-02 02:21:43 | [diff] [blame] | 67 | // Used to tuck some data into the meta table about mmap status. The value |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 68 | // represents how much data in bytes has successfully been read from the |
David Van Cleve | 5f7a09a8 | 2021-07-13 16:28:09 | [diff] [blame] | 69 | // database, or `kMmapFailure` or `kMmapSuccess`. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 70 | static bool GetMmapStatus(Database* db, int64_t* status); |
| 71 | static bool SetMmapStatus(Database* db, int64_t status); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 72 | |
David Van Cleve | 5f7a09a8 | 2021-07-13 16:28:09 | [diff] [blame] | 73 | // Initializes the MetaTableHelper, providing the `Database` pointer and |
Carlos Knippschild | 46800c9f | 2017-09-02 02:21:43 | [diff] [blame] | 74 | // creating the meta table if necessary. Must be called before any other |
| 75 | // non-static methods. For new tables, it will initialize the version number |
David Van Cleve | 5f7a09a8 | 2021-07-13 16:28:09 | [diff] [blame] | 76 | // to `version` and the compatible version number to `compatible_version`. |
Carlos Knippschild | 46800c9f | 2017-09-02 02:21:43 | [diff] [blame] | 77 | // Versions must be greater than 0 to distinguish missing versions (see |
| 78 | // GetVersionNumber()). If there was no meta table (proxy for a fresh |
David Van Cleve | 5f7a09a8 | 2021-07-13 16:28:09 | [diff] [blame] | 79 | // database), mmap status is set to `kMmapSuccess`. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 80 | bool Init(Database* db, int version, int compatible_version); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 81 | |
[email protected] | 470b0dd | 2010-06-29 03:20:40 | [diff] [blame] | 82 | // Resets this MetaTable object, making another call to Init() possible. |
| 83 | void Reset(); |
| 84 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 85 | // The version number of the database. This should be the version number of |
| 86 | // the creator of the file. The version number will be 0 if there is no |
| 87 | // previously set version number. |
| 88 | // |
| 89 | // See also Get/SetCompatibleVersionNumber(). |
| 90 | void SetVersionNumber(int version); |
| 91 | int GetVersionNumber(); |
| 92 | |
Victor Costan | 5ad7ee4 | 2020-12-12 00:53:00 | [diff] [blame] | 93 | // The compatible version number is the lowest current version embedded in |
| 94 | // Chrome code that can still use this database. This is usually the same as |
| 95 | // the current version. In some limited cases, such as adding a column without |
| 96 | // a NOT NULL constraint, the SQL queries embedded in older code can still |
| 97 | // execute successfully. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 98 | // |
| 99 | // For example, if an optional column is added to a table in version 3, the |
| 100 | // new code will set the version to 3, and the compatible version to 2, since |
| 101 | // the code expecting version 2 databases can still read and write the table. |
| 102 | // |
| 103 | // Rule of thumb: check the version number when you're upgrading, but check |
Victor Costan | 5ad7ee4 | 2020-12-12 00:53:00 | [diff] [blame] | 104 | // the compatible version number to see if you can use the file at all. If |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 105 | // it's larger than you code is expecting, fail. |
| 106 | // |
| 107 | // The compatible version number will be 0 if there is no previously set |
| 108 | // compatible version number. |
| 109 | void SetCompatibleVersionNumber(int version); |
| 110 | int GetCompatibleVersionNumber(); |
| 111 | |
| 112 | // Set the given arbitrary key with the given data. Returns true on success. |
Victor Costan | e2e042f | 2022-03-29 21:11:34 | [diff] [blame] | 113 | bool SetValue(base::StringPiece key, const std::string& value); |
| 114 | bool SetValue(base::StringPiece key, int64_t value); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 115 | |
| 116 | // Retrieves the value associated with the given key. This will use sqlite's |
| 117 | // type conversion rules. It will return true on success. |
Victor Costan | e2e042f | 2022-03-29 21:11:34 | [diff] [blame] | 118 | bool GetValue(base::StringPiece key, std::string* value); |
| 119 | bool GetValue(base::StringPiece key, int* value); |
| 120 | bool GetValue(base::StringPiece key, int64_t* value); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 121 | |
[email protected] | c3ebc32 | 2012-03-03 02:07:46 | [diff] [blame] | 122 | // Deletes the key from the table. |
Victor Costan | e2e042f | 2022-03-29 21:11:34 | [diff] [blame] | 123 | bool DeleteKey(base::StringPiece key); |
[email protected] | c3ebc32 | 2012-03-03 02:07:46 | [diff] [blame] | 124 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 125 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 126 | raw_ptr<Database> db_ = nullptr; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | } // namespace sql |
| 130 | |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 131 | #endif // SQL_META_TABLE_H_ |