blob: 0f4ee72c3f9034c73f62b970ecfb5f42f46ca6e5 [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
8#include <string>
9
10#include "base/basictypes.h"
[email protected]d4526962011-11-10 21:40:2811#include "sql/sql_export.h"
[email protected]e5ffd0e42009-09-11 21:30:5612
13namespace sql {
14
15class Connection;
16class Statement;
17
[email protected]d4526962011-11-10 21:40:2818class SQL_EXPORT MetaTable {
[email protected]e5ffd0e42009-09-11 21:30:5619 public:
20 MetaTable();
21 ~MetaTable();
22
[email protected]24864e42011-01-10 20:38:5123 // Returns true if the 'meta' table exists.
24 static bool DoesTableExist(Connection* db);
25
[email protected]e5ffd0e42009-09-11 21:30:5626 // Initializes the MetaTableHelper, creating the meta table if necessary. For
27 // new tables, it will initialize the version number to |version| and the
[email protected]1bc85b22012-03-08 04:53:4928 // compatible version number to |compatible_version|. Versions must be
29 // greater than 0 to distinguish missing versions (see GetVersionNumber()).
[email protected]35f2094c2009-12-29 22:46:5530 bool Init(Connection* db, int version, int compatible_version);
[email protected]e5ffd0e42009-09-11 21:30:5631
[email protected]470b0dd2010-06-29 03:20:4032 // Resets this MetaTable object, making another call to Init() possible.
33 void Reset();
34
[email protected]e5ffd0e42009-09-11 21:30:5635 // The version number of the database. This should be the version number of
36 // the creator of the file. The version number will be 0 if there is no
37 // previously set version number.
38 //
39 // See also Get/SetCompatibleVersionNumber().
40 void SetVersionNumber(int version);
41 int GetVersionNumber();
42
43 // The compatible version number is the lowest version of the code that this
44 // database can be read by. If there are minor changes or additions, old
45 // versions of the code can still work with the database without failing.
46 //
47 // For example, if an optional column is added to a table in version 3, the
48 // new code will set the version to 3, and the compatible version to 2, since
49 // the code expecting version 2 databases can still read and write the table.
50 //
51 // Rule of thumb: check the version number when you're upgrading, but check
52 // the compatible version number to see if you can read the file at all. If
53 // it's larger than you code is expecting, fail.
54 //
55 // The compatible version number will be 0 if there is no previously set
56 // compatible version number.
57 void SetCompatibleVersionNumber(int version);
58 int GetCompatibleVersionNumber();
59
60 // Set the given arbitrary key with the given data. Returns true on success.
61 bool SetValue(const char* key, const std::string& value);
62 bool SetValue(const char* key, int value);
63 bool SetValue(const char* key, int64 value);
64
65 // Retrieves the value associated with the given key. This will use sqlite's
66 // type conversion rules. It will return true on success.
67 bool GetValue(const char* key, std::string* value);
68 bool GetValue(const char* key, int* value);
69 bool GetValue(const char* key, int64* value);
70
[email protected]c3ebc322012-03-03 02:07:4671 // Deletes the key from the table.
72 bool DeleteKey(const char* key);
73
[email protected]e5ffd0e42009-09-11 21:30:5674 private:
75 // Conveniences to prepare the two types of statements used by
76 // MetaTableHelper.
[email protected]eff1fa522011-12-12 23:50:5977 void PrepareSetStatement(Statement* statement, const char* key);
[email protected]e5ffd0e42009-09-11 21:30:5678 bool PrepareGetStatement(Statement* statement, const char* key);
79
80 Connection* db_;
81
[email protected]e5ffd0e42009-09-11 21:30:5682 DISALLOW_COPY_AND_ASSIGN(MetaTable);
83};
84
85} // namespace sql
86
[email protected]f0a54b22011-07-19 18:40:2187#endif // SQL_META_TABLE_H_