[email protected] | c3ebc32 | 2012-03-03 02:07:46 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[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 | |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 8 | #include <stdint.h> |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 9 | #include <string> |
| 10 | |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | d452696 | 2011-11-10 21:40:28 | [diff] [blame] | 12 | #include "sql/sql_export.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 13 | |
| 14 | namespace sql { |
| 15 | |
| 16 | class Connection; |
| 17 | class Statement; |
| 18 | |
[email protected] | d452696 | 2011-11-10 21:40:28 | [diff] [blame] | 19 | class SQL_EXPORT MetaTable { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 20 | public: |
| 21 | MetaTable(); |
| 22 | ~MetaTable(); |
| 23 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 24 | // Values for Get/SetMmapStatus(). |kMmapFailure| indicates that there was at |
| 25 | // some point a read error and the database should not be memory-mapped, while |
| 26 | // |kMmapSuccess| indicates that the entire file was read at some point and |
| 27 | // can be memory-mapped without constraint. |
| 28 | static int64_t kMmapFailure; |
| 29 | static int64_t kMmapSuccess; |
| 30 | |
[email protected] | 24864e4 | 2011-01-10 20:38:51 | [diff] [blame] | 31 | // Returns true if the 'meta' table exists. |
| 32 | static bool DoesTableExist(Connection* db); |
| 33 | |
[email protected] | fe4e3de | 2013-10-08 02:19:17 | [diff] [blame] | 34 | // If the current version of the database is less than or equal to |
| 35 | // |deprecated_version|, raze the database. Must be called outside |
| 36 | // of a transaction. |
| 37 | // TODO(shess): At this time the database is razed IFF meta exists |
| 38 | // and contains a version row with value <= deprecated_version. It |
| 39 | // may make sense to also raze if meta exists but has no version |
| 40 | // row, or if meta doesn't exist. In those cases if the database is |
| 41 | // not already empty, it probably resulted from a broken |
| 42 | // initialization. |
| 43 | // TODO(shess): Folding this into Init() would allow enforcing |
| 44 | // |deprecated_version|<|version|. But Init() is often called in a |
| 45 | // transaction. |
| 46 | static void RazeIfDeprecated(Connection* db, int deprecated_version); |
| 47 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 48 | // Used to tuck some data into the meta table about mmap status. The value |
| 49 | // represents how much data in bytes has successfully been read from the |
| 50 | // database, or |kMmapFailure| or |kMmapSuccess|. |
| 51 | static bool GetMmapStatus(Connection* db, int64_t* status); |
| 52 | static bool SetMmapStatus(Connection* db, int64_t status); |
| 53 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 54 | // Initializes the MetaTableHelper, creating the meta table if necessary. For |
| 55 | // new tables, it will initialize the version number to |version| and the |
[email protected] | 1bc85b2 | 2012-03-08 04:53:49 | [diff] [blame] | 56 | // compatible version number to |compatible_version|. Versions must be |
| 57 | // greater than 0 to distinguish missing versions (see GetVersionNumber()). |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 58 | // If there was no meta table (proxy for a fresh database), mmap status is set |
| 59 | // to |kMmapSuccess|. |
[email protected] | 35f2094c | 2009-12-29 22:46:55 | [diff] [blame] | 60 | bool Init(Connection* db, int version, int compatible_version); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 61 | |
[email protected] | 470b0dd | 2010-06-29 03:20:40 | [diff] [blame] | 62 | // Resets this MetaTable object, making another call to Init() possible. |
| 63 | void Reset(); |
| 64 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 65 | // The version number of the database. This should be the version number of |
| 66 | // the creator of the file. The version number will be 0 if there is no |
| 67 | // previously set version number. |
| 68 | // |
| 69 | // See also Get/SetCompatibleVersionNumber(). |
| 70 | void SetVersionNumber(int version); |
| 71 | int GetVersionNumber(); |
| 72 | |
| 73 | // The compatible version number is the lowest version of the code that this |
| 74 | // database can be read by. If there are minor changes or additions, old |
| 75 | // versions of the code can still work with the database without failing. |
| 76 | // |
| 77 | // For example, if an optional column is added to a table in version 3, the |
| 78 | // new code will set the version to 3, and the compatible version to 2, since |
| 79 | // the code expecting version 2 databases can still read and write the table. |
| 80 | // |
| 81 | // Rule of thumb: check the version number when you're upgrading, but check |
| 82 | // the compatible version number to see if you can read the file at all. If |
| 83 | // it's larger than you code is expecting, fail. |
| 84 | // |
| 85 | // The compatible version number will be 0 if there is no previously set |
| 86 | // compatible version number. |
| 87 | void SetCompatibleVersionNumber(int version); |
| 88 | int GetCompatibleVersionNumber(); |
| 89 | |
| 90 | // Set the given arbitrary key with the given data. Returns true on success. |
| 91 | bool SetValue(const char* key, const std::string& value); |
| 92 | bool SetValue(const char* key, int value); |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 93 | bool SetValue(const char* key, int64_t value); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 94 | |
| 95 | // Retrieves the value associated with the given key. This will use sqlite's |
| 96 | // type conversion rules. It will return true on success. |
| 97 | bool GetValue(const char* key, std::string* value); |
| 98 | bool GetValue(const char* key, int* value); |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 99 | bool GetValue(const char* key, int64_t* value); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 100 | |
[email protected] | c3ebc32 | 2012-03-03 02:07:46 | [diff] [blame] | 101 | // Deletes the key from the table. |
| 102 | bool DeleteKey(const char* key); |
| 103 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 104 | private: |
| 105 | // Conveniences to prepare the two types of statements used by |
| 106 | // MetaTableHelper. |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 107 | void PrepareSetStatement(Statement* statement, const char* key); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 108 | bool PrepareGetStatement(Statement* statement, const char* key); |
| 109 | |
| 110 | Connection* db_; |
| 111 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 112 | DISALLOW_COPY_AND_ASSIGN(MetaTable); |
| 113 | }; |
| 114 | |
| 115 | } // namespace sql |
| 116 | |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 117 | #endif // SQL_META_TABLE_H_ |