libsqlite3x  2007.10.18
sqlite3x_settings_db.hpp
1 #ifndef s11n_net_SQLITE3X_SETTINGS_DB_INCLUDED
2 #define s11n_net_SQLITE3X_SETTINGS_DB_INCLUDED 1
3 
4 #include "sqlite3x.hpp"
5 
6 namespace sqlite3x {
7 
8  /**
9  settings_db ia a very simplistic settings-data database for
10  use with the sqlite3x database layer.
11 
12  Usage:
13 <pre>
14  settings_db db("my.db");
15  db.set("one", 1 );
16  db.set("two", 2.0 );
17  db.set("a_string", "a string" );
18 
19  std::string sval;
20  assert( db.get( "a_string", sval ) );
21 </pre>
22 
23  Obviously, an assert may be too harsh for what you're doing.
24 
25  */
27  {
28  public:
29  /**
30  Calls open(dbname). This ctor will throw if dbname
31  cannot be opened or if it is not a database.
32  */
33  explicit settings_db( std::string const & dbname );
34  /**
35  Creates an unopened database. You must call open()
36  before you can use this object.
37  */
38  settings_db();
39  /**
40  Closes this database.
41  */
42  ~settings_db();
43  /**
44  Returns true if open() has succeeded.
45  */
46  bool is_open() const;
47  /**
48  Empties the database. Does not remove the db file.
49  */
50  void clear();
51  /**
52  Empties the database items matching the given WHERE
53  clause. Does not remove the db file.
54 
55  'where' should be a full SQL where statement, e.g.:
56 
57  "WHERE KEY LIKE 'x%'"
58 
59  The field names in this db are KEY and VALUE.
60  */
61  void clear( std::string const & where );
62 
63  /**
64  Sets the given key/value pair.
65  */
66  void set( std::string const & key, int val );
67  /**
68  Sets the given key/value pair.
69  */
70  void set( std::string const & key, sqlite_int64 val );
71  /**
72  Sets the given key/value pair.
73  */
74  void set( std::string const & key, bool val );
75  /**
76  Sets the given key/value pair.
77  */
78  void set( std::string const & key, double val );
79  /**
80  Sets the given key/value pair.
81  */
82  void set( std::string const & key, std::string const & val );
83  /**
84  Sets the given key/value pair.
85  */
86  void set( std::string const & key, char const * val );
87 
88  /**
89  Fetches the given key from the db. If it is found,
90  it is converted to the data type of val, val is
91  assigned that value, and true is returned. If false
92  is returned then val is unchanged.
93  */
94  bool get( std::string const & key, int & val );
95  /** See get(string,int). */
96  bool get( std::string const & key, sqlite_int64 & val );
97  /** See get(string,int). */
98  bool get( std::string const & key, bool & val );
99  /** See get(string,int). */
100  bool get( std::string const & key, double & val );
101  /** See get(string,int). */
102  bool get( std::string const & key, std::string & val );
103 
104  /**
105  Opens the database dbname or throws on error.
106  */
107  void open( std::string const & dbname );
108  /**
109  Closes this database. Not normally necessary, as
110  this happens during the destruction of this object.
111  */
112  void close();
113 
114  /**
115  If you want low-level info about the db, here's the
116  handle to it. This will be null before open() has
117  succeeded.
118  */
120  private:
121  void init();
122  sqlite3_connection * m_db;
123  };
124 
125 } // namespace whnet
126 
127 
128 #endif // s11n_net_SQLITE3X_SETTINGS_DB_INCLUDED
settings_db()
Creates an unopened database.
settings_db ia a very simplistic settings-data database for use with the sqlite3x database layer...
bool is_open() const
Returns true if open() has succeeded.
This namespace encapsulates a C++ API wrapper for sqlite3 databases.
Definition: sqlite3x.hpp:120
Represents a connection to an sqlite3 database.
Definition: sqlite3x.hpp:148
void clear()
Empties the database.
void close()
Closes this database.
~settings_db()
Closes this database.
void open(std::string const &dbname)
Opens the database dbname or throws on error.
sqlite3_connection * db()
If you want low-level info about the db, here&#39;s the handle to it.