blob: 9b4acfe9eff2996c1e81f355babccd75360c56c1 [file] [log] [blame]
[email protected]c3ebc322012-03-03 02:07:461// 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
[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
tfarina720d4f32015-05-11 22:31:268#include <stdint.h>
[email protected]e5ffd0e42009-09-11 21:30:569#include <string>
10
Victor Costane56cc682018-12-27 01:53:4611#include "base/component_export.h"
tfarina720d4f32015-05-11 22:31:2612#include "base/macros.h"
[email protected]e5ffd0e42009-09-11 21:30:5613
14namespace sql {
15
Victor Costancfbfa602018-08-01 23:24:4616class Database;
[email protected]e5ffd0e42009-09-11 21:30:5617class Statement;
18
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();
28 ~MetaTable();
29
Carlos Knippschild46800c9f2017-09-02 02:21:4330 // Values for Get/SetMmapStatus(). |kMmapFailure| indicates that there was at
shess9bf2c672015-12-18 01:18:0831 // some point a read error and the database should not be memory-mapped, while
32 // |kMmapSuccess| indicates that the entire file was read at some point and
33 // can be memory-mapped without constraint.
Andrew Grieved1978b0e2017-07-28 15:53:4134 static constexpr int64_t kMmapFailure = -2;
35 static constexpr int64_t kMmapSuccess = -1;
shess9bf2c672015-12-18 01:18:0836
[email protected]24864e42011-01-10 20:38:5137 // Returns true if the 'meta' table exists.
Victor Costancfbfa602018-08-01 23:24:4638 static bool DoesTableExist(Database* db);
[email protected]24864e42011-01-10 20:38:5139
[email protected]fe4e3de2013-10-08 02:19:1740 // If the current version of the database is less than or equal to
Carlos Knippschild46800c9f2017-09-02 02:21:4341 // |deprecated_version|, raze the database. Must be called outside of a
42 // transaction.
43 // TODO(shess): At this time the database is razed IFF meta exists and
44 // contains a version row with value <= deprecated_version. It may make sense
45 // to also raze if meta exists but has no version row, or if meta doesn't
46 // exist. In those cases if the database is not already empty, it probably
47 // resulted from a broken initialization.
[email protected]fe4e3de2013-10-08 02:19:1748 // TODO(shess): Folding this into Init() would allow enforcing
Carlos Knippschild46800c9f2017-09-02 02:21:4349 // |deprecated_version|<|version|. But Init() is often called in a
[email protected]fe4e3de2013-10-08 02:19:1750 // transaction.
Victor Costancfbfa602018-08-01 23:24:4651 static void RazeIfDeprecated(Database* db, int deprecated_version);
[email protected]fe4e3de2013-10-08 02:19:1752
Carlos Knippschild46800c9f2017-09-02 02:21:4353 // Used to tuck some data into the meta table about mmap status. The value
shess9bf2c672015-12-18 01:18:0854 // represents how much data in bytes has successfully been read from the
55 // database, or |kMmapFailure| or |kMmapSuccess|.
Victor Costancfbfa602018-08-01 23:24:4656 static bool GetMmapStatus(Database* db, int64_t* status);
57 static bool SetMmapStatus(Database* db, int64_t status);
shess9bf2c672015-12-18 01:18:0858
Victor Costancfbfa602018-08-01 23:24:4659 // Initializes the MetaTableHelper, providing the |Database| pointer and
Carlos Knippschild46800c9f2017-09-02 02:21:4360 // creating the meta table if necessary. Must be called before any other
61 // non-static methods. For new tables, it will initialize the version number
62 // to |version| and the compatible version number to |compatible_version|.
63 // Versions must be greater than 0 to distinguish missing versions (see
64 // GetVersionNumber()). If there was no meta table (proxy for a fresh
65 // database), mmap status is set to |kMmapSuccess|.
Victor Costancfbfa602018-08-01 23:24:4666 bool Init(Database* db, int version, int compatible_version);
[email protected]e5ffd0e42009-09-11 21:30:5667
[email protected]470b0dd2010-06-29 03:20:4068 // Resets this MetaTable object, making another call to Init() possible.
69 void Reset();
70
[email protected]e5ffd0e42009-09-11 21:30:5671 // The version number of the database. This should be the version number of
72 // the creator of the file. The version number will be 0 if there is no
73 // previously set version number.
74 //
75 // See also Get/SetCompatibleVersionNumber().
76 void SetVersionNumber(int version);
77 int GetVersionNumber();
78
79 // The compatible version number is the lowest version of the code that this
80 // database can be read by. If there are minor changes or additions, old
81 // versions of the code can still work with the database without failing.
82 //
83 // For example, if an optional column is added to a table in version 3, the
84 // new code will set the version to 3, and the compatible version to 2, since
85 // the code expecting version 2 databases can still read and write the table.
86 //
87 // Rule of thumb: check the version number when you're upgrading, but check
88 // the compatible version number to see if you can read the file at all. If
89 // it's larger than you code is expecting, fail.
90 //
91 // The compatible version number will be 0 if there is no previously set
92 // compatible version number.
93 void SetCompatibleVersionNumber(int version);
94 int GetCompatibleVersionNumber();
95
96 // Set the given arbitrary key with the given data. Returns true on success.
97 bool SetValue(const char* key, const std::string& value);
98 bool SetValue(const char* key, int value);
tfarina720d4f32015-05-11 22:31:2699 bool SetValue(const char* key, int64_t value);
[email protected]e5ffd0e42009-09-11 21:30:56100
101 // Retrieves the value associated with the given key. This will use sqlite's
102 // type conversion rules. It will return true on success.
103 bool GetValue(const char* key, std::string* value);
104 bool GetValue(const char* key, int* value);
tfarina720d4f32015-05-11 22:31:26105 bool GetValue(const char* key, int64_t* value);
[email protected]e5ffd0e42009-09-11 21:30:56106
[email protected]c3ebc322012-03-03 02:07:46107 // Deletes the key from the table.
108 bool DeleteKey(const char* key);
109
[email protected]e5ffd0e42009-09-11 21:30:56110 private:
111 // Conveniences to prepare the two types of statements used by
112 // MetaTableHelper.
[email protected]eff1fa522011-12-12 23:50:59113 void PrepareSetStatement(Statement* statement, const char* key);
[email protected]e5ffd0e42009-09-11 21:30:56114 bool PrepareGetStatement(Statement* statement, const char* key);
115
Victor Costancfbfa602018-08-01 23:24:46116 Database* db_;
[email protected]e5ffd0e42009-09-11 21:30:56117
[email protected]e5ffd0e42009-09-11 21:30:56118 DISALLOW_COPY_AND_ASSIGN(MetaTable);
119};
120
121} // namespace sql
122
[email protected]f0a54b22011-07-19 18:40:21123#endif // SQL_META_TABLE_H_