libsq3  2007.10.18
Public Member Functions | List of all members
sq3::settings_db Class Reference

#include <sq3_settings_db.hpp>

Inheritance diagram for sq3::settings_db:
sq3::database

Public Member Functions

 settings_db (std::string const &dbname)
 Calls open(dbname). More...
 
 settings_db ()
 Creates an unopened database. More...
 
 ~settings_db ()
 Closes this database.
 
virtual int clear ()
 Overridden to just empty the settings db. More...
 
int clear (std::string const &where)
 Empties the database items matching the given WHERE clause. More...
 
void set (std::string const &key, int val)
 Sets the given key/value pair.
 
void set (std::string const &key, sqlite_int64 val)
 Sets the given key/value pair.
 
void set (std::string const &key, bool val)
 Sets the given key/value pair.
 
void set (std::string const &key, double val)
 Sets the given key/value pair.
 
void set (std::string const &key, std::string const &val)
 Sets the given key/value pair.
 
void set (std::string const &key, char const *val)
 Sets the given key/value pair.
 
bool get (std::string const &key, int &val)
 Fetches the given key from the db. More...
 
bool get (std::string const &key, sqlite_int64 &val)
 See get(string,int). More...
 
bool get (std::string const &key, bool &val)
 See get(string,int). More...
 
bool get (std::string const &key, double &val)
 See get(string,int). More...
 
bool get (std::string const &key, std::string &val)
 See get(string,int). More...
 
- Public Member Functions inherited from sq3::database
sqlite3 * handle () const
 The low-level handle to the sqlite db. More...
 
 database ()
 Creates an unopened database. More...
 
void take_handle (sqlite3 *dbh, std::string const &name="")
 dbh is assumed to be an opened, valid db handle. More...
 
sqlite3 * take_handle ()
 Transfers ownership of this->handle() to the caller. More...
 
 database (std::string const &filename)
 Opens/creates the given db file. More...
 
virtual ~database ()
 Closes this db.
 
bool is_open () const
 Returns true if this db is opened. More...
 
std::string name () const
 Returns the name of the db file.
 
std::string errormsg () const
 Returns the last error message from sqlite, or an empty string if this object is not opened. More...
 
virtual int open (char const *, long flags=0)
 Creates/opens the given db file. More...
 
int open (std::string const &, long flags=0)
 Functionally identical to open(char const*,long). More...
 
int close (bool force=false)
 "Closes" this db. More...
 
int64_t insertid ()
 Returns the rowid of the most recently inserted row on this db.
 
int changes ()
 Returns the number of database rows that were changed (or inserted or deleted) by the most recently completed INSERT, UPDATE, or DELETE statement. More...
 
int setbusytimeout (int ms)
 See sqlite3_busy_timeout().
 
int execute (const std::string &sql)
 Functionally identical to execute(char const *).
 
int execute (char const *sql)
 Executes a statement which is assumed to have a single step and a void result. More...
 
int execute (std::string const &sql, int &tgt)
 Executes a statement which is assumed to have a single result step and an integer result. More...
 
int execute (char const *sql, int &tgt)
 See execute( std::string, int & );.
 
int execute (char const *sql, int64_t &tgt)
 See execute( std::string, int & );.
 
int execute (std::string const &sql, int64_t &tgt)
 See execute( std::string, int & );.
 
int execute (char const *sql, double &tgt)
 See execute( std::string, int & );.
 
int execute (std::string const &sql, double &tgt)
 See execute( std::string, int & );.
 
int execute (char const *sql, std::string &tgt)
 See execute( std::string, int & );.
 
int execute (std::string const &sql, std::string &tgt)
 See execute( std::string, int & );.
 
int execute (char const *sql, sqlite3_text_char_t const **tgt, int &len)
 See execute( std::string, int & );. More...
 
int execute (std::string const &sql, sqlite3_text_char_t const **tgt, int &len)
 See execute( char const *, sqlite3_text_char_t const **, int & ). More...
 
int execute (std::string const &sql, void const **tgt, int &sz)
 See execute( char const *, sqlite3_text_char_t **, int & ). More...
 
int execute (char const *sql, void const **tgt, int &sz)
 See execute( char const *, sqlite3_text_char_t **, int & ). More...
 
int execute (std::string const &sql, sqlite3_callback callback, void *data, std::string &errmsg)
 Executes the given query, calling the given callback function for each row of the result set. More...
 
int execute (char const *sql, sqlite3_callback callback, void *data, std::string &errmsg)
 Identical to the execute(std::string,sqlite3_callback,void*,std::string&).
 
int execute (std::string const &sql, sqlite3_callback callback, void *data=0)
 Convenience overload which has a default data value of 0 and ignores any error string passed back by sqlite3_exec().
 
int execute (char const *sql, sqlite3_callback callback, void *data=0)
 Convenience overload which has a default data value of 0 and ignores any error string passed back by sqlite3_exec().
 
int pragma (char const *code)
 This is a convenience wrapper for execute( "pragma ..." ). More...
 
int vacuum ()
 Convenience wrapper around execute("vacuum"). More...
 

Additional Inherited Members

Detailed Description

   settings_db ia a very simplistic key/value pair database
   for use with the sq3 database layer. It is intended to
   be used as a simple config-file class.

   Usage:
       settings_db db("my.db");
       db.set("one", 1 );
       db.set("two", 2.0 );
       db.set("a_string", "a string" );
       std::string sval;
       assert( db.get( "a_string", sval ) );
Obviously, an assert may be too harsh for what you're doing.

When doing lots of set() calls you can gain a lot of speed by
adding an sq3::transaction before you start and calling
commit() on that transaction when you're done.

Definition at line 32 of file sq3_settings_db.hpp.

Constructor & Destructor Documentation

◆ settings_db() [1/2]

sq3::settings_db::settings_db ( std::string const &  dbname)
explicit

Calls open(dbname).

Use is_open() to find out if the open succeeded.

Definition at line 12 of file sq3_settings_db.cpp.

References sq3::database::open().

◆ settings_db() [2/2]

sq3::settings_db::settings_db ( )

Creates an unopened database.

You must call open() before you can use this object.

Definition at line 7 of file sq3_settings_db.cpp.

Member Function Documentation

◆ clear() [1/2]

int sq3::settings_db::clear ( )
virtual

Overridden to just empty the settings db.

Does not remove the db file. Returns SQLITE_OK on success, else false.

Reimplemented from sq3::database.

Definition at line 22 of file sq3_settings_db.cpp.

References sq3::database::execute().

◆ clear() [2/2]

int sq3::settings_db::clear ( std::string const &  where)

Empties the database items matching the given WHERE clause.

Does not remove the db file.

'where' should be a full SQL where statement, e.g.:

"WHERE KEY LIKE 'x%'"

The field names in this db are KEY and VALUE.

Definition at line 27 of file sq3_settings_db.cpp.

References sq3::database::execute().

◆ get() [1/5]

bool sq3::settings_db::get ( std::string const &  key,
int &  val 
)

Fetches the given key from the db.

If it is found, it is converted to the data type of val, val is assigned that value, and true is returned. If false is returned then val is unchanged.

Definition at line 93 of file sq3_settings_db.cpp.

References sq3::statement::bind(), sq3::statement::execute(), and sq3::rc_is_okay().

◆ get() [2/5]

bool sq3::settings_db::get ( std::string const &  key,
sqlite_int64 &  val 
)

See get(string,int).

Definition at line 108 of file sq3_settings_db.cpp.

References sq3::statement::bind(), sq3::statement::execute(), and sq3::rc_is_okay().

◆ get() [3/5]

bool sq3::settings_db::get ( std::string const &  key,
bool &  val 
)

See get(string,int).

Definition at line 122 of file sq3_settings_db.cpp.

References sq3::statement::bind(), sq3::statement::execute(), and sq3::rc_is_okay().

◆ get() [4/5]

bool sq3::settings_db::get ( std::string const &  key,
double &  val 
)

See get(string,int).

Definition at line 142 of file sq3_settings_db.cpp.

References sq3::statement::bind(), sq3::statement::execute(), and sq3::rc_is_okay().

◆ get() [5/5]

bool sq3::settings_db::get ( std::string const &  key,
std::string &  val 
)

See get(string,int).

Definition at line 156 of file sq3_settings_db.cpp.

References sq3::statement::bind(), sq3::statement::execute(), and sq3::rc_is_okay().


The documentation for this class was generated from the following files: