Roll src/third_party/WebKit 3c1b001:57aef96 (svn 201977:201978)
[chromium-blink-merge.git] / sql / meta_table.h
blob0ae76bd23e7afaf5cfcffcd1299b5963f8039ef9
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SQL_META_TABLE_H_
6 #define SQL_META_TABLE_H_
8 #include <stdint.h>
9 #include <string>
11 #include "base/macros.h"
12 #include "sql/sql_export.h"
14 namespace sql {
16 class Connection;
17 class Statement;
19 class SQL_EXPORT MetaTable {
20 public:
21 MetaTable();
22 ~MetaTable();
24 // Returns true if the 'meta' table exists.
25 static bool DoesTableExist(Connection* db);
27 // If the current version of the database is less than or equal to
28 // |deprecated_version|, raze the database. Must be called outside
29 // of a transaction.
30 // TODO(shess): At this time the database is razed IFF meta exists
31 // and contains a version row with value <= deprecated_version. It
32 // may make sense to also raze if meta exists but has no version
33 // row, or if meta doesn't exist. In those cases if the database is
34 // not already empty, it probably resulted from a broken
35 // initialization.
36 // TODO(shess): Folding this into Init() would allow enforcing
37 // |deprecated_version|<|version|. But Init() is often called in a
38 // transaction.
39 static void RazeIfDeprecated(Connection* db, int deprecated_version);
41 // Initializes the MetaTableHelper, creating the meta table if necessary. For
42 // new tables, it will initialize the version number to |version| and the
43 // compatible version number to |compatible_version|. Versions must be
44 // greater than 0 to distinguish missing versions (see GetVersionNumber()).
45 bool Init(Connection* db, int version, int compatible_version);
47 // Resets this MetaTable object, making another call to Init() possible.
48 void Reset();
50 // The version number of the database. This should be the version number of
51 // the creator of the file. The version number will be 0 if there is no
52 // previously set version number.
54 // See also Get/SetCompatibleVersionNumber().
55 void SetVersionNumber(int version);
56 int GetVersionNumber();
58 // The compatible version number is the lowest version of the code that this
59 // database can be read by. If there are minor changes or additions, old
60 // versions of the code can still work with the database without failing.
62 // For example, if an optional column is added to a table in version 3, the
63 // new code will set the version to 3, and the compatible version to 2, since
64 // the code expecting version 2 databases can still read and write the table.
66 // Rule of thumb: check the version number when you're upgrading, but check
67 // the compatible version number to see if you can read the file at all. If
68 // it's larger than you code is expecting, fail.
70 // The compatible version number will be 0 if there is no previously set
71 // compatible version number.
72 void SetCompatibleVersionNumber(int version);
73 int GetCompatibleVersionNumber();
75 // Set the given arbitrary key with the given data. Returns true on success.
76 bool SetValue(const char* key, const std::string& value);
77 bool SetValue(const char* key, int value);
78 bool SetValue(const char* key, int64_t value);
80 // Retrieves the value associated with the given key. This will use sqlite's
81 // type conversion rules. It will return true on success.
82 bool GetValue(const char* key, std::string* value);
83 bool GetValue(const char* key, int* value);
84 bool GetValue(const char* key, int64_t* value);
86 // Deletes the key from the table.
87 bool DeleteKey(const char* key);
89 private:
90 // Conveniences to prepare the two types of statements used by
91 // MetaTableHelper.
92 void PrepareSetStatement(Statement* statement, const char* key);
93 bool PrepareGetStatement(Statement* statement, const char* key);
95 Connection* db_;
97 DISALLOW_COPY_AND_ASSIGN(MetaTable);
100 } // namespace sql
102 #endif // SQL_META_TABLE_H_