Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * sqlite.h - Fawkes configuration stored in a SQLite database 00004 * 00005 * Created: Wed Dec 06 17:20:41 2006 00006 * Copyright 2006-2009 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #ifndef __CONFIG_SQLITE_H_ 00025 #define __CONFIG_SQLITE_H_ 00026 00027 #include <config/config.h> 00028 #include <utils/system/hostinfo.h> 00029 #include <list> 00030 #include <string> 00031 00032 struct sqlite3; 00033 struct sqlite3_stmt; 00034 00035 namespace fawkes { 00036 00037 class Mutex; 00038 00039 class SQLiteConfiguration : public Configuration 00040 { 00041 public: 00042 SQLiteConfiguration(); 00043 SQLiteConfiguration(const char *sysconfdir, const char *userconfdir = NULL); 00044 virtual ~SQLiteConfiguration(); 00045 00046 virtual void copy(Configuration *copyconf); 00047 00048 virtual void load(const char *filename, const char *defaults_filename, 00049 const char *tag = NULL); 00050 00051 void load(const char *tag = NULL); 00052 00053 virtual void tag(const char *tag); 00054 virtual std::list<std::string> tags(); 00055 00056 virtual bool exists(const char *path); 00057 virtual bool is_float(const char *path); 00058 virtual bool is_uint(const char *path); 00059 virtual bool is_int(const char *path); 00060 virtual bool is_bool(const char *path); 00061 virtual bool is_string(const char *path); 00062 00063 virtual bool is_default(const char *path); 00064 00065 virtual std::string get_type(const char *path); 00066 virtual float get_float(const char *path); 00067 virtual unsigned int get_uint(const char *path); 00068 virtual int get_int(const char *path); 00069 virtual bool get_bool(const char *path); 00070 virtual std::string get_string(const char *path); 00071 virtual ValueIterator * get_value(const char *path); 00072 virtual std::string get_comment(const char *path); 00073 virtual std::string get_default_comment(const char *path); 00074 00075 virtual void set_float(const char *path, float f); 00076 virtual void set_uint(const char *path, unsigned int uint); 00077 virtual void set_int(const char *path, int i); 00078 virtual void set_bool(const char *path, bool b); 00079 virtual void set_string(const char *path, std::string &s); 00080 virtual void set_string(const char *path, const char *s); 00081 virtual void set_comment(const char *path, std::string &comment); 00082 virtual void set_comment(const char *path, const char *comment); 00083 00084 virtual void erase(const char *path); 00085 00086 virtual void set_default_float(const char *path, float f); 00087 virtual void set_default_uint(const char *path, unsigned int uint); 00088 virtual void set_default_int(const char *path, int i); 00089 virtual void set_default_bool(const char *path, bool b); 00090 virtual void set_default_string(const char *path, std::string &s); 00091 virtual void set_default_string(const char *path, const char *s); 00092 virtual void set_default_comment(const char *path, const char *comment); 00093 virtual void set_default_comment(const char *path, std::string &comment); 00094 00095 virtual void erase_default(const char *path); 00096 00097 /** Transaction type. 00098 * See SQLite Documentation for BEGIN TRANSACTION. 00099 */ 00100 typedef enum { 00101 TRANSACTION_DEFERRED, /**< Deferred transaction, lock acquired late. */ 00102 TRANSACTION_IMMEDIATE, /**< Immediately acquire lock, reading remains possible. */ 00103 TRANSACTION_EXCLUSIVE /**< Immediately acquire lock, no more reading or writing possible. */ 00104 } transaction_type_t; 00105 00106 void transaction_begin(transaction_type_t ttype = TRANSACTION_DEFERRED); 00107 void transaction_commit(); 00108 void transaction_rollback(); 00109 00110 public: 00111 class SQLiteValueIterator : public Configuration::ValueIterator 00112 { 00113 friend class SQLiteConfiguration; 00114 protected: 00115 SQLiteValueIterator(::sqlite3_stmt *stmt, void *p = NULL); 00116 public: 00117 virtual ~SQLiteValueIterator(); 00118 virtual bool next(); 00119 virtual bool valid(); 00120 00121 virtual const char * path(); 00122 virtual const char * type(); 00123 00124 virtual bool is_float(); 00125 virtual bool is_uint(); 00126 virtual bool is_int(); 00127 virtual bool is_bool(); 00128 virtual bool is_string(); 00129 00130 virtual bool is_default(); 00131 00132 virtual float get_float(); 00133 virtual unsigned int get_uint(); 00134 virtual int get_int(); 00135 virtual bool get_bool(); 00136 virtual std::string get_string(); 00137 00138 virtual std::string get_as_string(); 00139 00140 virtual std::string get_comment(); 00141 00142 std::string get_modtype(); 00143 std::string get_oldvalue(); 00144 00145 private: 00146 ::sqlite3_stmt *__stmt; 00147 void *__p; 00148 }; 00149 00150 ValueIterator * iterator(); 00151 ValueIterator * iterator_default(); 00152 ValueIterator * iterator_hostspecific(); 00153 ValueIterator * search(const char *path); 00154 00155 void lock(); 00156 bool try_lock(); 00157 void unlock(); 00158 00159 SQLiteValueIterator * modified_iterator(); 00160 00161 void try_dump(); 00162 00163 private: 00164 void init_dbs(); 00165 std::string get_type(const char *table, const char *path); 00166 bool exists(const char *sql, const char *path); 00167 ::sqlite3_stmt * get_value(const char *type, const char *path); 00168 ::sqlite3_stmt * prepare_update(const char *sql, const char *path); 00169 ::sqlite3_stmt * prepare_insert_value(const char *sql, const char *type, 00170 const char *path); 00171 void execute_insert_or_update(sqlite3_stmt *stmt); 00172 void dump(::sqlite3 *tdb, const char *dumpfile); 00173 void import(::sqlite3 *tdb, const char *dumpfile); 00174 void import_default(const char *default_dump); 00175 void attach_default(const char *db_file); 00176 00177 private: 00178 ::sqlite3 *db; 00179 bool opened; 00180 Mutex *mutex; 00181 00182 char *__sysconfdir; 00183 char *__userconfdir; 00184 char *__host_file; 00185 char *__default_file; 00186 char *__default_sql; 00187 }; 00188 00189 } // end namespace fawkes 00190 00191 #endif