Fawkes API Fawkes Development Version
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
30#include <list>
31#include <string>
32
33struct sqlite3;
34struct sqlite3_stmt;
35
36namespace fawkes {
37
38class Mutex;
39
41{
42public:
44 SQLiteConfiguration(const char *sysconfdir, const char *userconfdir = NULL);
45 virtual ~SQLiteConfiguration();
46
47 virtual void copy(Configuration *copyconf);
48
49 virtual void load(const char *filename);
50
51 virtual bool exists(const char *path);
52 virtual bool is_float(const char *path);
53 virtual bool is_uint(const char *path);
54 virtual bool is_int(const char *path);
55 virtual bool is_bool(const char *path);
56 virtual bool is_string(const char *path);
57 virtual bool is_list(const char *path);
58
59 virtual bool is_default(const char *path);
60
61 virtual std::string get_type(const char *path);
62 virtual float get_float(const char *path);
63 virtual unsigned int get_uint(const char *path);
64 virtual int get_int(const char *path);
65 virtual bool get_bool(const char *path);
66 virtual std::string get_string(const char *path);
67 virtual std::vector<float> get_floats(const char *path);
68 virtual std::vector<unsigned int> get_uints(const char *path);
69 virtual std::vector<int> get_ints(const char *path);
70 virtual std::vector<bool> get_bools(const char *path);
71 virtual std::vector<std::string> get_strings(const char *path);
72 virtual ValueIterator * get_value(const char *path);
73 virtual std::string get_comment(const char *path);
74 virtual std::string get_default_comment(const char *path);
75
76 virtual void set_float(const char *path, float f);
77 virtual void set_uint(const char *path, unsigned int uint);
78 virtual void set_int(const char *path, int i);
79 virtual void set_bool(const char *path, bool b);
80 virtual void set_string(const char *path, std::string &s);
81 virtual void set_string(const char *path, const char *s);
82 virtual void set_floats(const char *path, std::vector<float> &f);
83 virtual void set_uints(const char *path, std::vector<unsigned int> &uint);
84 virtual void set_ints(const char *path, std::vector<int> &i);
85 virtual void set_bools(const char *path, std::vector<bool> &b);
86 virtual void set_strings(const char *path, std::vector<std::string> &s);
87 virtual void set_strings(const char *path, std::vector<const char *> &s);
88 virtual void set_comment(const char *path, std::string &comment);
89 virtual void set_comment(const char *path, const char *comment);
90
91 virtual void erase(const char *path);
92
93 virtual void set_default_float(const char *path, float f);
94 virtual void set_default_uint(const char *path, unsigned int uint);
95 virtual void set_default_int(const char *path, int i);
96 virtual void set_default_bool(const char *path, bool b);
97 virtual void set_default_string(const char *path, std::string &s);
98 virtual void set_default_string(const char *path, const char *s);
99 virtual void set_default_comment(const char *path, const char *comment);
100 virtual void set_default_comment(const char *path, std::string &comment);
101
102 virtual void erase_default(const char *path);
103
104 /** Transaction type.
105 * See SQLite Documentation for BEGIN TRANSACTION.
106 */
107 typedef enum {
108 TRANSACTION_DEFERRED, /**< Deferred transaction, lock acquired late. */
109 TRANSACTION_IMMEDIATE, /**< Immediately acquire lock, reading remains possible. */
110 TRANSACTION_EXCLUSIVE /**< Immediately acquire lock, no more reading or writing possible. */
112
114 void transaction_commit();
116
117public:
119 {
120 friend SQLiteConfiguration;
121
122 protected:
123 SQLiteValueIterator(::sqlite3_stmt *stmt, void *p = NULL);
124
125 public:
126 virtual ~SQLiteValueIterator();
127 virtual bool next();
128 virtual bool valid() const;
129
130 virtual const char *path() const;
131 virtual const char *type() const;
132
133 virtual bool is_float() const;
134 virtual bool is_uint() const;
135 virtual bool is_int() const;
136 virtual bool is_bool() const;
137 virtual bool is_string() const;
138 virtual bool is_list() const;
139 virtual size_t get_list_size() const;
140
141 virtual bool is_default() const;
142
143 virtual float get_float() const;
144 virtual unsigned int get_uint() const;
145 virtual int get_int() const;
146 virtual bool get_bool() const;
147 virtual std::string get_string() const;
148 virtual std::vector<float> get_floats() const;
149 virtual std::vector<unsigned int> get_uints() const;
150 virtual std::vector<int> get_ints() const;
151 virtual std::vector<bool> get_bools() const;
152 virtual std::vector<std::string> get_strings() const;
153
154 virtual std::string get_as_string() const;
155
156 virtual std::string get_comment() const;
157
158 std::string get_modtype() const;
159 std::string get_oldvalue() const;
160
161 private:
162 ::sqlite3_stmt *stmt_;
163 void * p_;
164 };
165
169 ValueIterator *search(const char *path);
170
171 void lock();
172 bool try_lock();
173 void unlock();
174
176
177 void try_dump();
178
179private:
180 void init_dbs();
181 ::sqlite3_stmt *get_typed_value(const char *path, const char *type);
182 ::sqlite3_stmt *prepare_update(const char *sql, const char *path);
183 ::sqlite3_stmt *prepare_insert_value(const char *sql, const char *type, const char *path);
184 void execute_insert_or_update(sqlite3_stmt *stmt);
185 void dump(::sqlite3 *tdb, const char *dumpfile);
186 void import(::sqlite3 *tdb, const char *dumpfile);
187 void import_default(const char *default_dump);
188 void attach_default(const char *db_file);
189
190private:
191 ::sqlite3 *db;
192 bool opened;
193 Mutex * mutex;
194
195 char *sysconfdir_;
196 char *userconfdir_;
197 char *host_file_;
198 char *default_file_;
199 char *default_sql_;
200};
201
202} // end namespace fawkes
203
204#endif
Iterator interface to iterate over config values.
Definition: config.h:75
Interface for configuration handling.
Definition: config.h:68
Mutex mutual exclusion lock.
Definition: mutex.h:33
SQLite configuration value iterator.
Definition: sqlite.h:119
virtual std::vector< float > get_floats() const
Get list of values from configuration which is of type float.
Definition: sqlite.cpp:2151
virtual std::vector< unsigned int > get_uints() const
Get list of values from configuration which is of type unsigned int.
Definition: sqlite.cpp:2157
virtual const char * path() const
Path of value.
Definition: sqlite.cpp:2038
virtual size_t get_list_size() const
Get number of elements in list value.
Definition: sqlite.cpp:2089
virtual std::string get_as_string() const
Get value as string.
Definition: sqlite.cpp:2184
virtual std::vector< std::string > get_strings() const
Get list of values from configuration which is of type string.
Definition: sqlite.cpp:2175
virtual bool next()
Check if there is another element and advance to this if possible.
Definition: sqlite.cpp:2009
virtual bool is_uint() const
Check if current value is a unsigned int.
Definition: sqlite.cpp:2059
virtual int get_int() const
Get int value.
Definition: sqlite.cpp:2127
virtual const char * type() const
Type of value.
Definition: sqlite.cpp:2047
virtual bool get_bool() const
Get bool value.
Definition: sqlite.cpp:2136
virtual bool is_default() const
Check if current value was read from the default config.
Definition: sqlite.cpp:2095
virtual float get_float() const
Get float value.
Definition: sqlite.cpp:2104
virtual bool is_int() const
Check if current value is a int.
Definition: sqlite.cpp:2065
SQLiteValueIterator(::sqlite3_stmt *stmt, void *p=NULL)
Constructor.
Definition: sqlite.cpp:1986
virtual std::string get_string() const
Get string value.
Definition: sqlite.cpp:2145
virtual unsigned int get_uint() const
Get unsigned int value.
Definition: sqlite.cpp:2113
virtual std::vector< bool > get_bools() const
Get list of values from configuration which is of type bool.
Definition: sqlite.cpp:2169
virtual std::string get_comment() const
Get comment.
Definition: sqlite.cpp:2193
virtual bool is_bool() const
Check if current value is a bool.
Definition: sqlite.cpp:2071
std::string get_oldvalue() const
Get old value (as string).
Definition: sqlite.cpp:2220
virtual bool is_string() const
Check if current value is a string.
Definition: sqlite.cpp:2077
std::string get_modtype() const
Get modification type.
Definition: sqlite.cpp:2206
virtual bool is_float() const
Check if current value is a float.
Definition: sqlite.cpp:2053
virtual bool valid() const
Check if the current element is valid.
Definition: sqlite.cpp:2029
virtual bool is_list() const
Check if a value is a list.
Definition: sqlite.cpp:2083
virtual std::vector< int > get_ints() const
Get list of values from configuration which is of type int.
Definition: sqlite.cpp:2163
Configuration storage using SQLite.
Definition: sqlite.h:41
virtual void set_floats(const char *path, std::vector< float > &f)
Set new value in configuration of type float.
Definition: sqlite.cpp:1413
virtual ValueIterator * get_value(const char *path)
Get value from configuration.
Definition: sqlite.cpp:1090
void lock()
Lock the config.
Definition: sqlite.cpp:1844
virtual std::vector< unsigned int > get_uints(const char *path)
Get list of values from configuration which is of type unsigned int.
Definition: sqlite.cpp:1066
virtual std::vector< float > get_floats(const char *path)
Get list of values from configuration which is of type float.
Definition: sqlite.cpp:1060
virtual void erase_default(const char *path)
Erase the given default value from the configuration.
Definition: sqlite.cpp:1814
virtual std::string get_comment(const char *path)
Get comment of value at given path.
Definition: sqlite.cpp:804
virtual void set_default_int(const char *path, int i)
Set new default value in configuration of type int.
Definition: sqlite.cpp:1617
ValueIterator * iterator()
Iterator for all values.
Definition: sqlite.cpp:1869
void transaction_rollback()
Rollback SQL Transaction.
Definition: sqlite.cpp:536
virtual bool is_list(const char *path)
Check if a value is a list.
Definition: sqlite.cpp:892
virtual void set_string(const char *path, std::string &s)
Set new value in configuration of type string.
Definition: sqlite.cpp:1407
virtual void erase(const char *path)
Erase the given value from the configuration.
Definition: sqlite.cpp:1492
virtual void set_uint(const char *path, unsigned int uint)
Set new value in configuration of type unsigned int.
Definition: sqlite.cpp:1206
virtual bool get_bool(const char *path)
Get value from configuration which is of type bool.
Definition: sqlite.cpp:1022
ValueIterator * iterator_hostspecific()
Iterator for all host-specific values.
Definition: sqlite.cpp:1909
virtual void set_bool(const char *path, bool b)
Set new value in configuration of type bool.
Definition: sqlite.cpp:1305
virtual int get_int(const char *path)
Get value from configuration which is of type int.
Definition: sqlite.cpp:1004
void try_dump()
Try to dump default configuration.
Definition: sqlite.cpp:376
virtual bool is_string(const char *path)
Check if a value is of type string.
Definition: sqlite.cpp:886
virtual bool is_default(const char *path)
Check if a value was read from the default config.
Definition: sqlite.cpp:898
virtual void copy(Configuration *copyconf)
Copy all values from the given configuration.
Definition: sqlite.cpp:719
virtual void set_default_bool(const char *path, bool b)
Set new default value in configuration of type bool.
Definition: sqlite.cpp:1665
virtual void set_strings(const char *path, std::vector< std::string > &s)
Set new value in configuration of type string.
Definition: sqlite.cpp:1437
virtual void set_default_uint(const char *path, unsigned int uint)
Set new default value in configuration of type unsigned int.
Definition: sqlite.cpp:1568
virtual void set_default_comment(const char *path, const char *comment)
Set new default comment for existing default configuration value.
Definition: sqlite.cpp:1772
virtual void set_uints(const char *path, std::vector< unsigned int > &uint)
Set new value in configuration of type unsigned int.
Definition: sqlite.cpp:1419
virtual bool is_float(const char *path)
Check if a value is of type float.
Definition: sqlite.cpp:862
virtual void set_int(const char *path, int i)
Set new value in configuration of type int.
Definition: sqlite.cpp:1255
virtual ~SQLiteConfiguration()
Destructor.
Definition: sqlite.cpp:231
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
Definition: sqlite.cpp:1040
virtual std::vector< std::string > get_strings(const char *path)
Get list of values from configuration which is of type string.
Definition: sqlite.cpp:1084
virtual bool is_bool(const char *path)
Check if a value is of type bool.
Definition: sqlite.cpp:880
SQLiteConfiguration()
Constructor.
Definition: sqlite.cpp:187
virtual void set_default_float(const char *path, float f)
Set new default value in configuration of type float.
Definition: sqlite.cpp:1518
virtual std::vector< int > get_ints(const char *path)
Get list of values from configuration which is of type int.
Definition: sqlite.cpp:1072
virtual std::string get_type(const char *path)
Get type of value at given path.
Definition: sqlite.cpp:771
ValueIterator * search(const char *path)
Iterator with search results.
Definition: sqlite.cpp:1951
virtual void set_float(const char *path, float f)
Set new value in configuration of type float.
Definition: sqlite.cpp:1156
virtual unsigned int get_uint(const char *path)
Get value from configuration which is of type unsigned int.
Definition: sqlite.cpp:982
void unlock()
Unlock the config.
Definition: sqlite.cpp:1863
virtual void set_bools(const char *path, std::vector< bool > &b)
Set new value in configuration of type bool.
Definition: sqlite.cpp:1431
virtual std::string get_default_comment(const char *path)
Get comment of value at given path.
Definition: sqlite.cpp:833
virtual void set_comment(const char *path, std::string &comment)
Set new comment for existing value.
Definition: sqlite.cpp:1486
void transaction_begin(transaction_type_t ttype=TRANSACTION_DEFERRED)
Begin SQL Transaction.
Definition: sqlite.cpp:507
virtual float get_float(const char *path)
Get value from configuration which is of type float.
Definition: sqlite.cpp:964
bool try_lock()
Try to lock the config.
Definition: sqlite.cpp:1854
virtual void load(const char *filename)
Load configuration.
Definition: sqlite.cpp:564
virtual std::vector< bool > get_bools(const char *path)
Get list of values from configuration which is of type bool.
Definition: sqlite.cpp:1078
virtual bool is_uint(const char *path)
Check if a value is of type unsigned int.
Definition: sqlite.cpp:868
ValueIterator * iterator_default()
Iterator for all default values.
Definition: sqlite.cpp:1889
SQLiteValueIterator * modified_iterator()
Iterator for modified values.
Definition: sqlite.cpp:1927
virtual void set_ints(const char *path, std::vector< int > &i)
Set new value in configuration of type int.
Definition: sqlite.cpp:1425
virtual bool is_int(const char *path)
Check if a value is of type int.
Definition: sqlite.cpp:874
virtual void set_default_string(const char *path, std::string &s)
Set new default value in configuration of type string.
Definition: sqlite.cpp:1766
void transaction_commit()
Commit SQL Transaction.
Definition: sqlite.cpp:524
virtual bool exists(const char *path)
Check if a given value exists.
Definition: sqlite.cpp:744
transaction_type_t
Transaction type.
Definition: sqlite.h:107
@ TRANSACTION_DEFERRED
Deferred transaction, lock acquired late.
Definition: sqlite.h:108
@ TRANSACTION_EXCLUSIVE
Immediately acquire lock, no more reading or writing possible.
Definition: sqlite.h:110
@ TRANSACTION_IMMEDIATE
Immediately acquire lock, reading remains possible.
Definition: sqlite.h:109
Fawkes library namespace.