libsq3  2007.10.18
sq3_settings_db.hpp
1 #ifndef s11n_net_SQ3_SETTINGS_DB_INCLUDED
2 #define s11n_net_SQ3_SETTINGS_DB_INCLUDED 1
3 // License: Public Domain
4 // Author: stephan at s11n net
5 
6 #include "sq3.hpp"
7 
8 namespace sq3 {
9 
10  /**
11  settings_db ia a very simplistic key/value pair database
12  for use with the sq3 database layer. It is intended to
13  be used as a simple config-file class.
14 
15  Usage:
16 <pre>
17  settings_db db("my.db");
18  db.set("one", 1 );
19  db.set("two", 2.0 );
20  db.set("a_string", "a string" );
21 
22  std::string sval;
23  assert( db.get( "a_string", sval ) );
24 </pre>
25 
26  Obviously, an assert may be too harsh for what you're doing.
27 
28  When doing lots of set() calls you can gain a lot of speed by
29  adding an sq3::transaction before you start and calling
30  commit() on that transaction when you're done.
31  */
32  class settings_db : public database
33  {
34  public:
35  /**
36  Calls open(dbname). Use is_open() to find out
37  if the open succeeded.
38  */
39  explicit settings_db( std::string const & dbname );
40  /**
41  Creates an unopened database. You must call open()
42  before you can use this object.
43  */
44  settings_db();
45  /**
46  Closes this database.
47  */
48  ~settings_db();
49  /**
50  Overridden to just empty the settings db.
51  Does not remove the db file.
52  Returns SQLITE_OK on success, else false.
53  */
54  virtual int clear();
55  /**
56  Empties the database items matching the given WHERE
57  clause. Does not remove the db file.
58 
59  'where' should be a full SQL where statement, e.g.:
60 
61  "WHERE KEY LIKE 'x%'"
62 
63  The field names in this db are KEY and VALUE.
64  */
65  int clear( std::string const & where );
66 
67  /**
68  Sets the given key/value pair.
69  */
70  void set( std::string const & key, int val );
71  /**
72  Sets the given key/value pair.
73  */
74  void set( std::string const & key, sqlite_int64 val );
75  /**
76  Sets the given key/value pair.
77  */
78  void set( std::string const & key, bool val );
79  /**
80  Sets the given key/value pair.
81  */
82  void set( std::string const & key, double val );
83  /**
84  Sets the given key/value pair.
85  */
86  void set( std::string const & key, std::string const & val );
87  /**
88  Sets the given key/value pair.
89  */
90  void set( std::string const & key, char const * val );
91 
92  /**
93  Fetches the given key from the db. If it is found,
94  it is converted to the data type of val, val is
95  assigned that value, and true is returned. If false
96  is returned then val is unchanged.
97  */
98  bool get( std::string const & key, int & val );
99  /** See get(string,int). */
100  bool get( std::string const & key, sqlite_int64 & val );
101  /** See get(string,int). */
102  bool get( std::string const & key, bool & val );
103  /** See get(string,int). */
104  bool get( std::string const & key, double & val );
105  /** See get(string,int). */
106  bool get( std::string const & key, std::string & val );
107 
108  private:
109  /** Reimplemented to initialize the settings table and enable some speed tweaks. */
110  virtual int on_open();
111  };
112 
113 } // namespace
114 
115 
116 #endif // s11n_net_SQ3_SETTINGS_DB_INCLUDED