libsq3 2007.10.18
sq3_settings_db.hpp
00001 #ifndef s11n_net_SQ3_SETTINGS_DB_INCLUDED
00002 #define s11n_net_SQ3_SETTINGS_DB_INCLUDED 1
00003 // License: Public Domain
00004 // Author: stephan at s11n net
00005 
00006 #include "sq3.hpp"
00007 
00008 namespace sq3 {
00009 
00010     /**
00011        settings_db ia a very simplistic key/value pair database
00012        for use with the sq3 database layer. It is intended to
00013        be used as a simple config-file class.
00014 
00015        Usage:
00016 <pre>
00017        settings_db db("my.db");
00018        db.set("one", 1 );
00019        db.set("two", 2.0 );
00020        db.set("a_string", "a string" );
00021 
00022        std::string sval;
00023        assert( db.get( "a_string", sval ) );
00024 </pre>
00025 
00026     Obviously, an assert may be too harsh for what you're doing.
00027 
00028     When doing lots of set() calls you can gain a lot of speed by
00029     adding an sq3::transaction before you start and calling
00030     commit() on that transaction when you're done.
00031     */
00032     class settings_db : public database
00033     {
00034     public:
00035         /**
00036            Calls open(dbname). Use is_open() to find out
00037            if the open succeeded.
00038         */
00039         explicit settings_db( std::string const & dbname );
00040         /**
00041            Creates an unopened database. You must call open()
00042            before you can use this object.
00043          */
00044         settings_db();
00045         /**
00046            Closes this database.
00047         */
00048         ~settings_db();
00049         /**
00050            Overridden to just empty the settings db.
00051            Does not remove the db file.
00052            Returns SQLITE_OK on success, else false.
00053         */
00054         virtual int clear();
00055         /**
00056            Empties the database items matching the given WHERE
00057            clause. Does not remove the db file.
00058 
00059            'where' should be a full SQL where statement, e.g.:
00060 
00061            "WHERE KEY LIKE 'x%'"
00062 
00063            The field names in this db are KEY and VALUE.
00064         */
00065         int clear( std::string const & where );
00066 
00067         /**
00068            Sets the given key/value pair.
00069          */
00070         void set( std::string const & key, int val );
00071         /**
00072            Sets the given key/value pair.
00073          */
00074         void set( std::string const & key, sqlite_int64 val );
00075         /**
00076            Sets the given key/value pair.
00077          */
00078         void set( std::string const & key, bool val );
00079         /**
00080            Sets the given key/value pair.
00081          */
00082         void set( std::string const & key, double val );
00083         /**
00084            Sets the given key/value pair.
00085          */
00086         void set( std::string const & key, std::string const & val );
00087         /**
00088            Sets the given key/value pair.
00089          */
00090         void set( std::string const & key, char const * val );
00091 
00092         /**
00093            Fetches the given key from the db. If it is found,
00094            it is converted to the data type of val, val is
00095            assigned that value, and true is returned. If false
00096            is returned then val is unchanged.
00097         */
00098         bool get( std::string const & key, int & val );
00099         /** See get(string,int). */
00100         bool get( std::string const & key, sqlite_int64 & val );
00101         /** See get(string,int). */
00102         bool get( std::string const & key, bool & val );
00103         /** See get(string,int). */
00104         bool get( std::string const & key, double & val );
00105         /** See get(string,int). */
00106         bool get( std::string const & key, std::string & val );
00107 
00108     private:
00109         /** Reimplemented to initialize the settings table and enable some speed tweaks. */
00110         virtual int on_open();
00111     };
00112 
00113 } // namespace
00114 
00115 
00116 #endif // s11n_net_SQ3_SETTINGS_DB_INCLUDED