diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-11 21:30:56 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-11 21:30:56 +0000 |
commit | e5ffd0e471417e75ddcd5af20c3254c0ec2f1f5d (patch) | |
tree | 60e8d7c1de4ee33cc063cfa98a168e8fe9fcf27b /app/sql/meta_table.h | |
parent | 92c3dc6b3fffbaf9fa2fa409120ca051bf317234 (diff) | |
download | chromium_src-e5ffd0e471417e75ddcd5af20c3254c0ec2f1f5d.zip chromium_src-e5ffd0e471417e75ddcd5af20c3254c0ec2f1f5d.tar.gz chromium_src-e5ffd0e471417e75ddcd5af20c3254c0ec2f1f5d.tar.bz2 |
Add a new wrapper for sqlite. This is mostly a large cleanup of the existing
one, combined with the statement cache in a nice way. It is designed to
entirely wrap sqlite so that we can catch corrupt errors in the future and
"do something" when we get them without having to change all the calling code.
There is also a new meta_table file which is almost exactly like the old one
but which uses the new sql interface.
This patch changes Chrome's history TextDatabase to use this new wrapper as a
proof of concept, because this usage is relatively well-confined.
Review URL: http://codereview.chromium.org/199047
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26022 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/sql/meta_table.h')
-rw-r--r-- | app/sql/meta_table.h | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/app/sql/meta_table.h b/app/sql/meta_table.h new file mode 100644 index 0000000..6ccee17 --- /dev/null +++ b/app/sql/meta_table.h @@ -0,0 +1,87 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef APP_SQL_META_TABLE_H_ +#define APP_SQL_META_TABLE_H_ + +#include <string> + +#include "base/basictypes.h" + +namespace sql { + +class Connection; +class Statement; + +class MetaTable { + public: + MetaTable(); + ~MetaTable(); + + // Initializes the MetaTableHelper, creating the meta table if necessary. For + // new tables, it will initialize the version number to |version| and the + // compatible version number to |compatible_version|. + // + // The name of the database in the sqlite connection (for tables named with + // the "db_name.table_name" scheme is given in |db_name|. If empty, it is + // assumed there is no database name. + bool Init(Connection* db, + int version, + int compatible_version); + + // The version number of the database. This should be the version number of + // the creator of the file. The version number will be 0 if there is no + // previously set version number. + // + // See also Get/SetCompatibleVersionNumber(). + void SetVersionNumber(int version); + int GetVersionNumber(); + + // The compatible version number is the lowest version of the code that this + // database can be read by. If there are minor changes or additions, old + // versions of the code can still work with the database without failing. + // + // For example, if an optional column is added to a table in version 3, the + // new code will set the version to 3, and the compatible version to 2, since + // the code expecting version 2 databases can still read and write the table. + // + // Rule of thumb: check the version number when you're upgrading, but check + // the compatible version number to see if you can read the file at all. If + // it's larger than you code is expecting, fail. + // + // The compatible version number will be 0 if there is no previously set + // compatible version number. + void SetCompatibleVersionNumber(int version); + int GetCompatibleVersionNumber(); + + // Set the given arbitrary key with the given data. Returns true on success. + bool SetValue(const char* key, const std::string& value); + bool SetValue(const char* key, int value); + bool SetValue(const char* key, int64 value); + + // Retrieves the value associated with the given key. This will use sqlite's + // type conversion rules. It will return true on success. + bool GetValue(const char* key, std::string* value); + bool GetValue(const char* key, int* value); + bool GetValue(const char* key, int64* value); + + private: + // Conveniences to prepare the two types of statements used by + // MetaTableHelper. + bool PrepareSetStatement(Statement* statement, const char* key); + bool PrepareGetStatement(Statement* statement, const char* key); + + Connection* db_; + + // Name of the database within the connection, if there is one. When empty, + // there is no special database name and the table name can be used + // unqualified. + std::string db_name_; + + DISALLOW_COPY_AND_ASSIGN(MetaTable); +}; + +} // namespace sql + +#endif // APP_SQL_META_TABLE_H_ |