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