libsqlite3x  2007.10.18
sqlite3x_settings_db.hpp
00001 #ifndef s11n_net_SQLITE3X_SETTINGS_DB_INCLUDED
00002 #define s11n_net_SQLITE3X_SETTINGS_DB_INCLUDED 1
00003 
00004 #include "sqlite3x.hpp"
00005 
00006 namespace sqlite3x {
00007 
00008     /**
00009        settings_db ia a very simplistic settings-data database for
00010        use with the sqlite3x database layer.
00011 
00012        Usage:
00013 <pre>
00014        settings_db db("my.db");
00015        db.set("one", 1 );
00016        db.set("two", 2.0 );
00017        db.set("a_string", "a string" );
00018 
00019        std::string sval;
00020        assert( db.get( "a_string", sval ) );
00021 </pre>
00022 
00023     Obviously, an assert may be too harsh for what you're doing.
00024 
00025     */
00026     class settings_db
00027     {
00028     public:
00029         /**
00030            Calls open(dbname). This ctor will throw if dbname
00031            cannot be opened or if it is not a database.
00032         */
00033         explicit settings_db( std::string const & dbname );
00034         /**
00035            Creates an unopened database. You must call open()
00036            before you can use this object.
00037          */
00038         settings_db();
00039         /**
00040            Closes this database.
00041         */
00042         ~settings_db();
00043         /**
00044            Returns true if open() has succeeded.
00045         */
00046         bool is_open() const;
00047         /**
00048            Empties the database. Does not remove the db file.
00049         */
00050         void clear();
00051         /**
00052            Empties the database items matching the given WHERE
00053            clause. Does not remove the db file.
00054 
00055            'where' should be a full SQL where statement, e.g.:
00056 
00057            "WHERE KEY LIKE 'x%'"
00058 
00059            The field names in this db are KEY and VALUE.
00060         */
00061         void clear( std::string const & where );
00062 
00063         /**
00064            Sets the given key/value pair.
00065          */
00066         void set( std::string const & key, int val );
00067         /**
00068            Sets the given key/value pair.
00069          */
00070         void set( std::string const & key, sqlite_int64 val );
00071         /**
00072            Sets the given key/value pair.
00073          */
00074         void set( std::string const & key, bool val );
00075         /**
00076            Sets the given key/value pair.
00077          */
00078         void set( std::string const & key, double val );
00079         /**
00080            Sets the given key/value pair.
00081          */
00082         void set( std::string const & key, std::string const & val );
00083         /**
00084            Sets the given key/value pair.
00085          */
00086         void set( std::string const & key, char const * val );
00087 
00088         /**
00089            Fetches the given key from the db. If it is found,
00090            it is converted to the data type of val, val is
00091            assigned that value, and true is returned. If false
00092            is returned then val is unchanged.
00093         */
00094         bool get( std::string const & key, int & val );
00095         /** See get(string,int). */
00096         bool get( std::string const & key, sqlite_int64 & val );
00097         /** See get(string,int). */
00098         bool get( std::string const & key, bool & val );
00099         /** See get(string,int). */
00100         bool get( std::string const & key, double & val );
00101         /** See get(string,int). */
00102         bool get( std::string const & key, std::string & val );
00103 
00104         /**
00105            Opens the database dbname or throws on error.
00106         */
00107         void open( std::string const & dbname );
00108         /**
00109            Closes this database. Not normally necessary, as
00110            this happens during the destruction of this object.
00111         */
00112         void close();
00113 
00114         /**
00115            If you want low-level info about the db, here's the
00116            handle to it. This will be null before open() has
00117            succeeded.
00118          */
00119         sqlite3_connection * db();
00120     private:
00121         void init();
00122         sqlite3_connection * m_db;
00123     };
00124 
00125 } // namespace whnet
00126 
00127 
00128 #endif // s11n_net_SQLITE3X_SETTINGS_DB_INCLUDED