sqlite.h

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
00030 struct sqlite3;
00031 struct sqlite3_stmt;
00032
00033 namespace fawkes {
00034
00035 class Mutex;
00036
00037 class SQLiteConfiguration : public Configuration
00038 {
00039  public:
00040   SQLiteConfiguration(const char *conf_path = NULL);
00041   virtual ~SQLiteConfiguration();
00042
00043   virtual void          copy(Configuration *copyconf);
00044
00045   virtual void          load(const char *filename, const char *defaults_filename,
00046                              const char *tag = NULL);
00047
00048   void load(const char *tag = NULL);
00049
00050   virtual void          tag(const char *tag);
00051   virtual std::list<std::string> tags();
00052
00053   virtual bool          exists(const char *path);
00054   virtual bool          is_float(const char *path);
00055   virtual bool          is_uint(const char *path);
00056   virtual bool          is_int(const char *path);
00057   virtual bool          is_bool(const char *path);
00058   virtual bool          is_string(const char *path);
00059
00060   virtual bool          is_default(const char *path);
00061
00062   virtual std::string     get_type(const char *path);
00063   virtual float           get_float(const char *path);
00064   virtual unsigned int    get_uint(const char *path);
00065   virtual int             get_int(const char *path);
00066   virtual bool            get_bool(const char *path);
00067   virtual std::string     get_string(const char *path);
00068   virtual ValueIterator * get_value(const char *path);
00069   virtual std::string     get_comment(const char *path);
00070   virtual std::string     get_default_comment(const char *path);
00071
00072   virtual void          set_float(const char *path, float f);
00073   virtual void          set_uint(const char *path, unsigned int uint);
00074   virtual void          set_int(const char *path, int i);
00075   virtual void          set_bool(const char *path, bool b);
00076   virtual void          set_string(const char *path, std::string &s);
00077   virtual void          set_string(const char *path, const char *s);
00078   virtual void          set_comment(const char *path, std::string &comment);
00079   virtual void          set_comment(const char *path, const char *comment);
00080
00081   virtual void          erase(const char *path);
00082
00083   virtual void          set_default_float(const char *path, float f);
00084   virtual void          set_default_uint(const char *path, unsigned int uint);
00085   virtual void          set_default_int(const char *path, int i);
00086   virtual void          set_default_bool(const char *path, bool b);
00087   virtual void          set_default_string(const char *path, std::string &s);
00088   virtual void          set_default_string(const char *path, const char *s);
00089   virtual void          set_default_comment(const char *path, const char *comment);
00090   virtual void          set_default_comment(const char *path, std::string &comment);
00091
00092   virtual void          erase_default(const char *path);
00093 
00094   /** Transaction type.
00095    * See SQLite Documentation for BEGIN TRANSACTION.
00096    */
00097   typedef enum {
00098     TRANSACTION_DEFERRED,       /**< Deferred transaction, lock acquired late. */
00099     TRANSACTION_IMMEDIATE,      /**< Immediately acquire lock, reading remains possible. */
00100     TRANSACTION_EXCLUSIVE       /**< Immediately acquire lock, no more reading or writing possible. */
00101   } transaction_type_t;
00102
00103   void transaction_begin(transaction_type_t ttype = TRANSACTION_DEFERRED);
00104   void transaction_commit();
00105   void transaction_rollback();
00106
00107  public:
00108  class SQLiteValueIterator : public Configuration::ValueIterator
00109   {
00110     friend class SQLiteConfiguration;
00111    protected:
00112     SQLiteValueIterator(::sqlite3_stmt *stmt, void *p = NULL);
00113    public:
00114     virtual ~SQLiteValueIterator();
00115     virtual bool          next();
00116     virtual bool          valid();
00117
00118     virtual const char *  path();
00119     virtual const char *  type();
00120
00121     virtual bool          is_float();
00122     virtual bool          is_uint();
00123     virtual bool          is_int();
00124     virtual bool          is_bool();
00125     virtual bool          is_string();
00126
00127     virtual bool          is_default();
00128
00129     virtual float         get_float();
00130     virtual unsigned int  get_uint();
00131     virtual int           get_int();
00132     virtual bool          get_bool();
00133     virtual std::string   get_string();
00134
00135     virtual std::string   get_as_string();
00136
00137     virtual std::string   get_comment();
00138
00139     std::string           get_modtype();
00140     std::string           get_oldvalue();
00141
00142    private:
00143     ::sqlite3_stmt *__stmt;
00144     void *__p;
00145   };
00146
00147   ValueIterator * iterator();
00148   ValueIterator * iterator_default();
00149   ValueIterator * iterator_hostspecific();
00150   ValueIterator * search(const char *path);
00151
00152   void lock();
00153   bool try_lock();
00154   void unlock();
00155
00156   SQLiteValueIterator * modified_iterator();
00157
00158  private:
00159   void            init_dbs();
00160   std::string     get_type(const char *table, const char *path);
00161   bool            exists(const char *sql, const char *path);
00162   ::sqlite3_stmt *  get_value(const char *type, const char *path);
00163   ::sqlite3_stmt *  prepare_update(const char *sql, const char *path);
00164   ::sqlite3_stmt *  prepare_insert_value(const char *sql, const char *type,
00165                                        const char *path);
00166   void execute_insert_or_update(sqlite3_stmt *stmt);
00167   void dump(::sqlite3 *tdb, const char *dumpfile);
00168   void import(::sqlite3 *tdb, const char *dumpfile);
00169   void import_default(const char *default_dump);
00170
00171  private:
00172   ::sqlite3 *db;
00173   const char *conf_path;
00174   bool opened;
00175   Mutex *mutex;
00176
00177   char *__host_file;
00178   char *__default_file;
00179   char *__default_dump;
00180 };
00181
00182 } // end namespace fawkes
00183
00184 #endif