blob: 85c0c82bab0c71b25acf8c6eeb5baf2b30114d05 [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
tfarina720d4f32015-05-11 22:31:2611#include "base/macros.h"
[email protected]d4526962011-11-10 21:40:2812#include "sql/sql_export.h"
[email protected]e5ffd0e42009-09-11 21:30:5613
14namespace sql {
15
16class Connection;
17class Statement;
18
[email protected]d4526962011-11-10 21:40:2819class SQL_EXPORT MetaTable {
[email protected]e5ffd0e42009-09-11 21:30:5620 public:
21 MetaTable();
22 ~MetaTable();
23
shess9bf2c672015-12-18 01:18:0824 // 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]24864e42011-01-10 20:38:5131 // Returns true if the 'meta' table exists.
32 static bool DoesTableExist(Connection* db);
33
[email protected]fe4e3de2013-10-08 02:19:1734 // 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
shess9bf2c672015-12-18 01:18:0848 // 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]e5ffd0e42009-09-11 21:30:5654 // 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]1bc85b22012-03-08 04:53:4956 // compatible version number to |compatible_version|. Versions must be
57 // greater than 0 to distinguish missing versions (see GetVersionNumber()).
shess9bf2c672015-12-18 01:18:0858 // If there was no meta table (proxy for a fresh database), mmap status is set
59 // to |kMmapSuccess|.
[email protected]35f2094c2009-12-29 22:46:5560 bool Init(Connection* db, int version, int compatible_version);
[email protected]e5ffd0e42009-09-11 21:30:5661
[email protected]470b0dd2010-06-29 03:20:4062 // Resets this MetaTable object, making another call to Init() possible.
63 void Reset();
64
[email protected]e5ffd0e42009-09-11 21:30:5665 // 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);
tfarina720d4f32015-05-11 22:31:2693 bool SetValue(const char* key, int64_t value);
[email protected]e5ffd0e42009-09-11 21:30:5694
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);
tfarina720d4f32015-05-11 22:31:2699 bool GetValue(const char* key, int64_t* value);
[email protected]e5ffd0e42009-09-11 21:30:56100
[email protected]c3ebc322012-03-03 02:07:46101 // Deletes the key from the table.
102 bool DeleteKey(const char* key);
103
[email protected]e5ffd0e42009-09-11 21:30:56104 private:
105 // Conveniences to prepare the two types of statements used by
106 // MetaTableHelper.
[email protected]eff1fa522011-12-12 23:50:59107 void PrepareSetStatement(Statement* statement, const char* key);
[email protected]e5ffd0e42009-09-11 21:30:56108 bool PrepareGetStatement(Statement* statement, const char* key);
109
110 Connection* db_;
111
[email protected]e5ffd0e42009-09-11 21:30:56112 DISALLOW_COPY_AND_ASSIGN(MetaTable);
113};
114
115} // namespace sql
116
[email protected]f0a54b22011-07-19 18:40:21117#endif // SQL_META_TABLE_H_