blob: 6ccee17ef32b182fec2cfab53f3e98fd8bdc5b9e [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:
19 MetaTable();
20 ~MetaTable();
21
22 // Initializes the MetaTableHelper, creating the meta table if necessary. For
23 // new tables, it will initialize the version number to |version| and the
24 // compatible version number to |compatible_version|.
25 //
26 // The name of the database in the sqlite connection (for tables named with
27 // the "db_name.table_name" scheme is given in |db_name|. If empty, it is
28 // assumed there is no database name.
29 bool Init(Connection* db,
30 int version,
31 int compatible_version);
32
33 // The version number of the database. This should be the version number of
34 // the creator of the file. The version number will be 0 if there is no
35 // previously set version number.
36 //
37 // See also Get/SetCompatibleVersionNumber().
38 void SetVersionNumber(int version);
39 int GetVersionNumber();
40
41 // The compatible version number is the lowest version of the code that this
42 // database can be read by. If there are minor changes or additions, old
43 // versions of the code can still work with the database without failing.
44 //
45 // For example, if an optional column is added to a table in version 3, the
46 // new code will set the version to 3, and the compatible version to 2, since
47 // the code expecting version 2 databases can still read and write the table.
48 //
49 // Rule of thumb: check the version number when you're upgrading, but check
50 // the compatible version number to see if you can read the file at all. If
51 // it's larger than you code is expecting, fail.
52 //
53 // The compatible version number will be 0 if there is no previously set
54 // compatible version number.
55 void SetCompatibleVersionNumber(int version);
56 int GetCompatibleVersionNumber();
57
58 // Set the given arbitrary key with the given data. Returns true on success.
59 bool SetValue(const char* key, const std::string& value);
60 bool SetValue(const char* key, int value);
61 bool SetValue(const char* key, int64 value);
62
63 // Retrieves the value associated with the given key. This will use sqlite's
64 // type conversion rules. It will return true on success.
65 bool GetValue(const char* key, std::string* value);
66 bool GetValue(const char* key, int* value);
67 bool GetValue(const char* key, int64* value);
68
69 private:
70 // Conveniences to prepare the two types of statements used by
71 // MetaTableHelper.
72 bool PrepareSetStatement(Statement* statement, const char* key);
73 bool PrepareGetStatement(Statement* statement, const char* key);
74
75 Connection* db_;
76
77 // Name of the database within the connection, if there is one. When empty,
78 // there is no special database name and the table name can be used
79 // unqualified.
80 std::string db_name_;
81
82 DISALLOW_COPY_AND_ASSIGN(MetaTable);
83};
84
85} // namespace sql
86
87#endif // APP_SQL_META_TABLE_H_