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