blob: 7f22d84a15068e3e61e9ba44879b3575c189f959 [file] [log] [blame]
Avi Drissman69b874f2022-09-15 19:11:141// Copyright 2012 The Chromium Authors
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f0a54b22011-07-19 18:40:215#ifndef SQL_META_TABLE_H_
6#define SQL_META_TABLE_H_
[email protected]e5ffd0e42009-09-11 21:30:567
Victor Costane2e042f2022-03-29 21:11:348#include <cstdint>
[email protected]e5ffd0e42009-09-11 21:30:569#include <string>
10
Victor Costane56cc682018-12-27 01:53:4611#include "base/component_export.h"
Keishi Hattori0e45c022021-11-27 09:25:5212#include "base/memory/raw_ptr.h"
Victor Costane2e042f2022-03-29 21:11:3413#include "base/strings/string_piece_forward.h"
[email protected]e5ffd0e42009-09-11 21:30:5614
15namespace sql {
16
Victor Costancfbfa602018-08-01 23:24:4617class Database;
[email protected]e5ffd0e42009-09-11 21:30:5618
Carlos Knippschild46800c9f2017-09-02 02:21:4319// 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 Costane56cc682018-12-27 01:53:4625class COMPONENT_EXPORT(SQL) MetaTable {
[email protected]e5ffd0e42009-09-11 21:30:5626 public:
27 MetaTable();
Victor Costan00c76432021-07-07 16:55:5828 MetaTable(const MetaTable&) = delete;
29 MetaTable& operator=(const MetaTable&) = delete;
Andrew Paseltiner7d512ddd2022-09-16 13:36:5230 MetaTable(MetaTable&&) = delete;
31 MetaTable& operator=(MetaTable&&) = delete;
[email protected]e5ffd0e42009-09-11 21:30:5632 ~MetaTable();
33
David Van Cleve5f7a09a82021-07-13 16:28:0934 // Values for Get/SetMmapStatus(). `kMmapFailure` indicates that there was at
shess9bf2c672015-12-18 01:18:0835 // some point a read error and the database should not be memory-mapped, while
David Van Cleve5f7a09a82021-07-13 16:28:0936 // `kMmapSuccess` indicates that the entire file was read at some point and
shess9bf2c672015-12-18 01:18:0837 // can be memory-mapped without constraint.
Andrew Grieved1978b0e2017-07-28 15:53:4138 static constexpr int64_t kMmapFailure = -2;
39 static constexpr int64_t kMmapSuccess = -1;
shess9bf2c672015-12-18 01:18:0840
[email protected]24864e42011-01-10 20:38:5141 // Returns true if the 'meta' table exists.
Victor Costancfbfa602018-08-01 23:24:4642 static bool DoesTableExist(Database* db);
[email protected]24864e42011-01-10 20:38:5143
David Van Clevef7b270f2021-06-22 13:27:1944 // 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 Cleve5f7a09a82021-07-13 16:28:0949 // 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]fe4e3de2013-10-08 02:19:1766
Carlos Knippschild46800c9f2017-09-02 02:21:4367 // Used to tuck some data into the meta table about mmap status. The value
shess9bf2c672015-12-18 01:18:0868 // represents how much data in bytes has successfully been read from the
David Van Cleve5f7a09a82021-07-13 16:28:0969 // database, or `kMmapFailure` or `kMmapSuccess`.
Victor Costancfbfa602018-08-01 23:24:4670 static bool GetMmapStatus(Database* db, int64_t* status);
71 static bool SetMmapStatus(Database* db, int64_t status);
shess9bf2c672015-12-18 01:18:0872
David Van Cleve5f7a09a82021-07-13 16:28:0973 // Initializes the MetaTableHelper, providing the `Database` pointer and
Carlos Knippschild46800c9f2017-09-02 02:21:4374 // 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 Cleve5f7a09a82021-07-13 16:28:0976 // to `version` and the compatible version number to `compatible_version`.
Carlos Knippschild46800c9f2017-09-02 02:21:4377 // 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 Cleve5f7a09a82021-07-13 16:28:0979 // database), mmap status is set to `kMmapSuccess`.
Victor Costancfbfa602018-08-01 23:24:4680 bool Init(Database* db, int version, int compatible_version);
[email protected]e5ffd0e42009-09-11 21:30:5681
[email protected]470b0dd2010-06-29 03:20:4082 // Resets this MetaTable object, making another call to Init() possible.
83 void Reset();
84
[email protected]e5ffd0e42009-09-11 21:30:5685 // 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 Costan5ad7ee42020-12-12 00:53:0093 // 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]e5ffd0e42009-09-11 21:30:5698 //
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 Costan5ad7ee42020-12-12 00:53:00104 // the compatible version number to see if you can use the file at all. If
[email protected]e5ffd0e42009-09-11 21:30:56105 // 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 Costane2e042f2022-03-29 21:11:34113 bool SetValue(base::StringPiece key, const std::string& value);
114 bool SetValue(base::StringPiece key, int64_t value);
[email protected]e5ffd0e42009-09-11 21:30:56115
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 Costane2e042f2022-03-29 21:11:34118 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]e5ffd0e42009-09-11 21:30:56121
[email protected]c3ebc322012-03-03 02:07:46122 // Deletes the key from the table.
Victor Costane2e042f2022-03-29 21:11:34123 bool DeleteKey(base::StringPiece key);
[email protected]c3ebc322012-03-03 02:07:46124
[email protected]e5ffd0e42009-09-11 21:30:56125 private:
Keishi Hattori0e45c022021-11-27 09:25:52126 raw_ptr<Database> db_ = nullptr;
[email protected]e5ffd0e42009-09-11 21:30:56127};
128
129} // namespace sql
130
[email protected]f0a54b22011-07-19 18:40:21131#endif // SQL_META_TABLE_H_