blob: ae78e11549ac3c2055a9fb7b696ca9781b79a05e [file] [log] [blame]
[email protected]e5ffd0e42009-09-11 21:30:561// Copyright (c) 2009 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 APP_SQL_META_TABLE_H_
6#define APP_SQL_META_TABLE_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11
12namespace sql {
13
14class Connection;
15class Statement;
16
17class MetaTable {
18 public:
[email protected]35f2094c2009-12-29 22:46:5519 // Returns true if the 'meta' table exists.
20 static bool DoesTableExist(Connection* db);
21
[email protected]e5ffd0e42009-09-11 21:30:5622 MetaTable();
23 ~MetaTable();
24
25 // Initializes the MetaTableHelper, creating the meta table if necessary. For
26 // new tables, it will initialize the version number to |version| and the
27 // compatible version number to |compatible_version|.
[email protected]35f2094c2009-12-29 22:46:5528 bool Init(Connection* db, int version, int compatible_version);
[email protected]e5ffd0e42009-09-11 21:30:5629
30 // The version number of the database. This should be the version number of
31 // the creator of the file. The version number will be 0 if there is no
32 // previously set version number.
33 //
34 // See also Get/SetCompatibleVersionNumber().
35 void SetVersionNumber(int version);
36 int GetVersionNumber();
37
38 // The compatible version number is the lowest version of the code that this
39 // database can be read by. If there are minor changes or additions, old
40 // versions of the code can still work with the database without failing.
41 //
42 // For example, if an optional column is added to a table in version 3, the
43 // new code will set the version to 3, and the compatible version to 2, since
44 // the code expecting version 2 databases can still read and write the table.
45 //
46 // Rule of thumb: check the version number when you're upgrading, but check
47 // the compatible version number to see if you can read the file at all. If
48 // it's larger than you code is expecting, fail.
49 //
50 // The compatible version number will be 0 if there is no previously set
51 // compatible version number.
52 void SetCompatibleVersionNumber(int version);
53 int GetCompatibleVersionNumber();
54
55 // Set the given arbitrary key with the given data. Returns true on success.
56 bool SetValue(const char* key, const std::string& value);
57 bool SetValue(const char* key, int value);
58 bool SetValue(const char* key, int64 value);
59
60 // Retrieves the value associated with the given key. This will use sqlite's
61 // type conversion rules. It will return true on success.
62 bool GetValue(const char* key, std::string* value);
63 bool GetValue(const char* key, int* value);
64 bool GetValue(const char* key, int64* value);
65
66 private:
67 // Conveniences to prepare the two types of statements used by
68 // MetaTableHelper.
69 bool PrepareSetStatement(Statement* statement, const char* key);
70 bool PrepareGetStatement(Statement* statement, const char* key);
71
72 Connection* db_;
73
[email protected]e5ffd0e42009-09-11 21:30:5674 DISALLOW_COPY_AND_ASSIGN(MetaTable);
75};
76
77} // namespace sql
78
79#endif // APP_SQL_META_TABLE_H_