Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sqlite.h
1 
2 /***************************************************************************
3  * sqlite.h - Fawkes configuration stored in a SQLite database
4  *
5  * Created: Wed Dec 06 17:20:41 2006
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __CONFIG_SQLITE_H_
25 #define __CONFIG_SQLITE_H_
26 
27 #include <config/config.h>
28 #include <utils/system/hostinfo.h>
29 #include <list>
30 #include <string>
31 
32 struct sqlite3;
33 struct sqlite3_stmt;
34 
35 namespace fawkes {
36 
37 class Mutex;
38 
40 {
41  public:
43  SQLiteConfiguration(const char *sysconfdir, const char *userconfdir = NULL);
44  virtual ~SQLiteConfiguration();
45 
46  virtual void copy(Configuration *copyconf);
47 
48  virtual void load(const char *filename, const char *defaults_filename,
49  const char *tag = NULL);
50 
51  void load(const char *tag = NULL);
52 
53  virtual void tag(const char *tag);
54  virtual std::list<std::string> tags();
55 
56  virtual bool exists(const char *path);
57  virtual bool is_float(const char *path);
58  virtual bool is_uint(const char *path);
59  virtual bool is_int(const char *path);
60  virtual bool is_bool(const char *path);
61  virtual bool is_string(const char *path);
62 
63  virtual bool is_default(const char *path);
64 
65  virtual std::string get_type(const char *path);
66  virtual float get_float(const char *path);
67  virtual unsigned int get_uint(const char *path);
68  virtual int get_int(const char *path);
69  virtual bool get_bool(const char *path);
70  virtual std::string get_string(const char *path);
71  virtual ValueIterator * get_value(const char *path);
72  virtual std::string get_comment(const char *path);
73  virtual std::string get_default_comment(const char *path);
74 
75  virtual void set_float(const char *path, float f);
76  virtual void set_uint(const char *path, unsigned int uint);
77  virtual void set_int(const char *path, int i);
78  virtual void set_bool(const char *path, bool b);
79  virtual void set_string(const char *path, std::string &s);
80  virtual void set_string(const char *path, const char *s);
81  virtual void set_comment(const char *path, std::string &comment);
82  virtual void set_comment(const char *path, const char *comment);
83 
84  virtual void erase(const char *path);
85 
86  virtual void set_default_float(const char *path, float f);
87  virtual void set_default_uint(const char *path, unsigned int uint);
88  virtual void set_default_int(const char *path, int i);
89  virtual void set_default_bool(const char *path, bool b);
90  virtual void set_default_string(const char *path, std::string &s);
91  virtual void set_default_string(const char *path, const char *s);
92  virtual void set_default_comment(const char *path, const char *comment);
93  virtual void set_default_comment(const char *path, std::string &comment);
94 
95  virtual void erase_default(const char *path);
96 
97  /** Transaction type.
98  * See SQLite Documentation for BEGIN TRANSACTION.
99  */
100  typedef enum {
101  TRANSACTION_DEFERRED, /**< Deferred transaction, lock acquired late. */
102  TRANSACTION_IMMEDIATE, /**< Immediately acquire lock, reading remains possible. */
103  TRANSACTION_EXCLUSIVE /**< Immediately acquire lock, no more reading or writing possible. */
105 
107  void transaction_commit();
108  void transaction_rollback();
109 
110  public:
112  {
113  friend class SQLiteConfiguration;
114  protected:
115  SQLiteValueIterator(::sqlite3_stmt *stmt, void *p = NULL);
116  public:
117  virtual ~SQLiteValueIterator();
118  virtual bool next();
119  virtual bool valid() const;
120 
121  virtual const char * path() const;
122  virtual const char * type() const;
123 
124  virtual bool is_float() const;
125  virtual bool is_uint() const;
126  virtual bool is_int() const;
127  virtual bool is_bool() const;
128  virtual bool is_string() const;
129 
130  virtual bool is_default() const;
131 
132  virtual float get_float() const;
133  virtual unsigned int get_uint() const;
134  virtual int get_int() const;
135  virtual bool get_bool() const;
136  virtual std::string get_string() const;
137 
138  virtual std::string get_as_string() const;
139 
140  virtual std::string get_comment() const;
141 
142  std::string get_modtype() const;
143  std::string get_oldvalue() const;
144 
145  private:
146  ::sqlite3_stmt *__stmt;
147  void *__p;
148  };
149 
153  ValueIterator * search(const char *path);
154 
155  void lock();
156  bool try_lock();
157  void unlock();
158 
160 
161  void try_dump();
162 
163  private:
164  void init_dbs();
165  std::string get_type(const char *table, const char *path);
166  bool exists(const char *sql, const char *path);
167  ::sqlite3_stmt * get_value(const char *type, const char *path);
168  ::sqlite3_stmt * prepare_update(const char *sql, const char *path);
169  ::sqlite3_stmt * prepare_insert_value(const char *sql, const char *type,
170  const char *path);
171  void execute_insert_or_update(sqlite3_stmt *stmt);
172  void dump(::sqlite3 *tdb, const char *dumpfile);
173  void import(::sqlite3 *tdb, const char *dumpfile);
174  void import_default(const char *default_dump);
175  void attach_default(const char *db_file);
176 
177  private:
178  ::sqlite3 *db;
179  bool opened;
180  Mutex *mutex;
181 
182  char *__sysconfdir;
183  char *__userconfdir;
184  char *__host_file;
185  char *__default_file;
186  char *__default_sql;
187 };
188 
189 } // end namespace fawkes
190 
191 #endif