Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

mysqldb.h

Go to the documentation of this file.
00001 // $Id: mysqldb.h,v 1.11 2002/10/19 13:50:13 dvermeir Exp $ $Name:  $
00002 
00003 #ifndef MYSQLDB_H
00004 #define MYSQLDB_H
00005 
00006 
00007 #include        <string>
00008 #include        <vector>
00009 
00010 #include        <dvmysql/sqldb.h>
00011 #include        <dvutil/props.h>
00012 
00013 /*! \file
00014     This file declares the mysql-dependent classes.
00015 
00016     <ul>
00017     <li> Dv::MySql::Db: an open database connection, derived from Dv::Sql::Db.
00018     <li> Dv::Sql::Command_: an implementation class derived from Dv::Sql::Command_.
00019     </ul>
00020 */
00021 
00022 namespace Dv {
00023 
00024 //! The Dv::MySql namespace contains Dv::Mysql::Db and Dv::Mysql::Command_
00025 namespace MySql {
00026 
00027 //! Return string that is acceptable as a field value in a mysql statement.
00028 /*! This will insert appropriate quotes e.g. before an apostrophe etc.
00029     It will not add quotes around the string!
00030 */
00031 std::string escape(const std::string&);
00032 
00033 class Command_;
00034 
00035 //! A connection to a mysql database.
00036 class Db: public Sql::Db {
00037 //! Command_ has access to mdbms-dependent private info, e.g. mysql_.
00038 friend class Command_;
00039 public:
00040   //! Open a connection, check result with Dv::Sql::Db::ok() and friends.
00041   Db(const char* dbname=0, const char* username="root", 
00042      const char* passwd=0,const char* hostname=0);
00043   //! Get database name etc. from configuration Dv::Util::Props object.
00044   /*! The configuration file uses the following attribute names
00045       (each preceded by prefix):
00046       \code
00047       mysqldb = 
00048       mysqluser = 
00049       mysqlpasswd = 
00050       mysqlhost = 
00051       \endcode
00052       If, e.g. prefix = "abc:", this would become
00053       \code
00054       abc:mysqldb = 
00055       abc:mysqluser = 
00056       abc:mysqlpasswd = 
00057       abc:mysqlhost = 
00058       \endcode
00059       Any missing or empty attribute is equivalent to a corresponding
00060       0 argument for the other constructor. 
00061 
00062       Example usage: (from tmysql.C in the distribution)
00063       \code
00064       try {
00065         static const std::string CONFIG("tmysql.config");
00066         ifstream        ifs(CONFIG.c_str());
00067         if (!ifs)
00068           throw runtime_error(CONFIG+": cannot open");
00069         
00070         Dv::Util::Props config(ifs);
00071         std::string dictfn(config["dict"]);
00072   
00073         MySql::Db       db(config,"tmysql::");
00074         ..
00075       catch (exception& e) {
00076         cerr << e.what() << endl;
00077         return 1;
00078         }
00079       \endcode
00080       where ``tmysql.config'' may contain e.g.
00081       \code
00082       tmeta::mysqldb=mysql
00083       tmeta::mysqluser=root
00084       #tmeta::mysqlpasswd=
00085       #
00086       tmysql::mysqldb=test
00087       tmysql::mysqluser=root
00088       #mysqlpasswd=
00089       dict=/usr/dict/words
00090       \endcode
00091   */
00092   Db(const Dv::Util::Props& config,const std::string& prefix="") throw (Sql::Exception);
00093   //! Destructor.
00094   virtual ~Db();
00095   
00096   //! Return user of this database connection.
00097   std::string   user() const    { return user_; }
00098   //! Return password used to open this connection.
00099   std::string   passwd() const  { return passwd_; }
00100   //! Return name of current database of connection.
00101   std::string   database() const { return Sql::Db::database(); } 
00102   //! Switch connection to another database.
00103   bool          database(const std::string& database);
00104   //! Return the names of all databases available through this connection.
00105   std::vector<std::string>      databases();
00106   //! Return the names of all tables available in the current database.
00107   std::vector<std::string>      tables();
00108   //! Return ordered list of fields in a table.
00109   std::vector<Sql::Field>       fields(const std::string& table);
00110 
00111 
00112 private:
00113   void*         mysql_;
00114   std::string   user_;
00115   std::string   passwd_;
00116 
00117   Db(const Db&);
00118   Db&           operator=(const Db&);
00119 
00120   //! Implementation of Sql::Db pure virtual factory function.
00121   Sql::Command_*        command_(Sql::Command& cmd);
00122 };
00123 
00124 //! Implementation class for Sql::Command helper.
00125 class Command_: public Sql::Command_ {
00126 
00127 //! Only Db can e.g. construct a Command_.
00128 friend class Db;
00129 
00130 private:
00131   Db&                   db_;
00132   //! Stored result of query, opaque declaration, really MYSQL_RES*
00133   void*                 result_;
00134   //! Current row# [0..] of mrow_, -1 if none.
00135   int                   row_;   
00136   //! Lengths of fields [0..ncols()] in current row.
00137   unsigned long*        lengths_; 
00138   //! Buffer corresponding to row_; opaque declaration, really MYSQL_ROW.
00139   void*                 mrow_;
00140   //! Error code, 0 if ok.
00141   int                   errno_;
00142   //! Field list of the query result, filled on demand.
00143   std::vector<Sql::Field>       fields_;
00144 
00145   Command_(Db& db,Sql::Command& cmd);
00146   virtual ~Command_();
00147 
00148   //! Return true iff all is well.
00149   bool  check_error(); 
00150   //! Synchronize mrow_ cursor to point to row # r.
00151   void  sync(unsigned int r) throw (Sql::Exception);
00152   //! Return error code.
00153   int   error() const   { return errno_; }
00154   //! Return MySql::Db connection of this command.
00155   Db&   db()            { return db_; }
00156 
00157   //! Execute current command.
00158   /*! Part of Sql::Command_ required interface. */
00159   bool  exec() throw (Sql::Exception);
00160   //! Fetch r'th row from query result.
00161   /*! Part of Sql::Command_ required interface. */
00162   void  fetch(std::vector<std::string>&,unsigned int r) throw (Sql::Exception);
00163   //! Fetch c'th element in r'th row of query result.
00164   /*! Part of Sql::Command_ required interface. */
00165   void  fetch(std::string&,unsigned int r,unsigned int c) throw (Sql::Exception);
00166   //! Write quoted and escaped version of parameter to textstream().
00167   /*! Part of Sql::Command_ required interface. */
00168   void  escape(const std::string&);
00169   //! Return field description corresponding to query result.
00170   /*! Part of Sql::Command_ required interface. */
00171   const std::vector<Sql::Field>&        fields();
00172   /** Return auto_increment insertid of last query */
00173   unsigned int insertid() const;
00174 };
00175 }
00176 }
00177 #endif

dvmysql-0.4.11 [15 February, 2004]