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

Dv::Sql::Command Class Reference

A Command represents a command, e.g. a query, for a Dv::Sql::Db. More...

#include <sqldb.h>

Collaboration diagram for Dv::Sql::Command:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Command (Db &db, const char *text=0)
 Constructor, text is initial text of command.

 ~Command ()
 Destructor also destroys any result data.

bool ok () const
 Status of command.

 operator bool () const
 Same as Dv::Sql::Command::ok().

std::string err () const
 Error message.

bool ran () const
 Return true iff the command has already been executed.

Dbdb ()
 Return database connection with which this command is associated.

std::string text ()
 Return text of command.

const std::vector< Field > & fields ()
 Return field information for query result.

unsigned int nrows ()
 Return number of rows in query result.

unsigned int ncols ()
 Return number of columns in query result.

iterator begin ()
 Return logical pointer to first row of the result.

iterator end ()
 Return logical pointer to past the end of the query result.

bool valid (int r) const
 Check whether a rowno r is valid, i.e. the r'th row is available.

Commandoperator<< (const std::string &) throw (Exception)
 Append a string to the command text; the string will be escaped and quoted.

Commandoperator<< (const char *) throw (Exception)
 Append a string to the command text.

Commandoperator<< (int) throw (Exception)
 Append an int to the command text.

Commandoperator<< (unsigned int) throw (Exception)
 Append an unsigned int to the command text.

Commandoperator<< (short) throw (Exception)
 Append a short to the command text.

Commandoperator<< (unsigned short) throw (Exception)
 Append an unsigned short to the command text.

Commandoperator<< (long) throw (Exception)
 Append a long to the command text.

Commandoperator<< (unsigned long) throw (Exception)
 Append an unsigned long to the command text.

Commandoperator<< (float) throw (Exception)
 Append a float to the command text.

Commandoperator<< (double) throw (Exception)
 Append a double to the command text.

bool exec ()
 Execute the query, status is determined by underlying DBMS.

unsigned int insertid () const
 Return auto_increment id of last insert.


Private Member Functions

void fetch (std::vector< std::string > &v, unsigned int r) throw (Exception)
 Retrieve the r'th row of the result ( 0<= r < nrows_ ).

void fetch (std::string &s, unsigned int r, unsigned int c) throw (Exception)
 Retrieve the r'th row and c'th column of the result ( 0<= r < //nrows_ ).

 Command (const Command &)
 Cctor is forbidden.

Commandoperator= (const Command &)
 Assignment is forbidden.

void nrows (unsigned int n)
void ncols (unsigned int n)
void run ()
void err (const char *s)
void ok (bool b)

Private Attributes

bool ran_
std::string err_
bool ok_
std::ostringstream text_
bool frozen_
unsigned long nrows_
 Nrows_ is # of rows in query answer, only valid if ran() && ok().

unsigned long ncols_
 Ncols_ is # of columns in query answer, only valid if ran() && ok().

Command_cmd_
 Associated dbms-dependent object.


Friends

class Db
 Friend.

class iterator
 Friend.

class Command_
 Friend.


Detailed Description

A Command represents a command, e.g. a query, for a Dv::Sql::Db.

Command is a dbms-independent proxy class for the dbms-dependent Command_. A Command object has a pointer to a companion Command_ object, which has been created using Dv::Sql::Db::command_().

This is an alternative for inheritance: instead of subclassing Command, the ``subclass part'' is available in *cmd_.

This has the advantage that only the definition of the database object ``db'' derived from Dv::Sql::Db depends on the actual DBMS used. All other database manipulatsions can be done through normal Dv::Sql::Command objects, which are initialized using a referrence to a Dv::Sql::Db. Consequently, applications are more independent of the actual underlying DBMS.

Example:

    MySql::Db db(...);

    std::string name;
    Sql::Command query("select * from person where name = ");
    query << name;
    if (q.exec())
      for (Sql::Command::iterator i=q.begin(); i!=q.end(); ++i) 
        cout << i(0) << "\t" << i(1) << endl;
Commands contain a text. This text is built using Command::operator<<(..).

Definition at line 254 of file sqldb.h.


Constructor & Destructor Documentation

Dv::Sql::Command::Command Db db,
const char *  text = 0
 

Constructor, text is initial text of command.

Example

      Sql::Command      q("insert into person values (");
      // rest of command is inserted using operator<<

Dv::Sql::Command::~Command  ) 
 

Destructor also destroys any result data.

Dv::Sql::Command::Command const Command  )  [private]
 

Cctor is forbidden.


Member Function Documentation

bool Dv::Sql::Command::ok  )  const [inline]
 

Status of command.

Definition at line 279 of file sqldb.h.

References ok_.

Dv::Sql::Command::operator bool  )  const [inline]
 

Same as Dv::Sql::Command::ok().

Definition at line 281 of file sqldb.h.

References ok_.

std::string Dv::Sql::Command::err  )  const [inline]
 

Error message.

Definition at line 283 of file sqldb.h.

References err_.

bool Dv::Sql::Command::ran  )  const [inline]
 

Return true iff the command has already been executed.

Calling Dv::Sql::Command::exec() twice is a noop.

Definition at line 287 of file sqldb.h.

References ran_.

Db & Dv::Sql::Command::db  )  [inline]
 

Return database connection with which this command is associated.

Definition at line 534 of file sqldb.h.

References cmd_, and Dv::Sql::Command_::db().

std::string Dv::Sql::Command::text  )  [inline]
 

Return text of command.

Example

    std::string name("jane");
    Sql::Command query("select * from person where name = ");
    query << name;
    cout << query.text();
will print
    select * from person where name = 'jane'

Definition at line 303 of file sqldb.h.

References frozen_, and text_.

const std::vector< Field > & Dv::Sql::Command::fields  )  [inline]
 

Return field information for query result.

Definition at line 533 of file sqldb.h.

References cmd_, and Dv::Sql::Command_::fields().

unsigned int Dv::Sql::Command::nrows  )  [inline]
 

Return number of rows in query result.

Definition at line 308 of file sqldb.h.

References nrows_.

unsigned int Dv::Sql::Command::ncols  )  [inline]
 

Return number of columns in query result.

Definition at line 310 of file sqldb.h.

References ncols_.

iterator Dv::Sql::Command::begin  ) 
 

Return logical pointer to first row of the result.

Command::iterator Dv::Sql::Command::end  )  [inline]
 

Return logical pointer to past the end of the query result.

Definition at line 535 of file sqldb.h.

References iterator.

bool Dv::Sql::Command::valid int  r  )  const
 

Check whether a rowno r is valid, i.e. the r'th row is available.

Command& Dv::Sql::Command::operator<< const std::string &   )  throw (Exception)
 

Append a string to the command text; the string will be escaped and quoted.

E.g. after

      Db::Sql::Db& db;
  
      Dv::Sql::Command  q(db,"select * from person");
      q << "where name ="  << std::string("joe's nightamere");
q.text_ will contain
      select * from person where name = 'joe\'s nightmare'

Note that Dv::Sql::Command::operator<<(const char*) does not escape or quote.

Command& Dv::Sql::Command::operator<< const char *   )  throw (Exception)
 

Append a string to the command text.

Command& Dv::Sql::Command::operator<< int   )  throw (Exception)
 

Append an int to the command text.

Command& Dv::Sql::Command::operator<< unsigned  int  )  throw (Exception)
 

Append an unsigned int to the command text.

Command& Dv::Sql::Command::operator<< short   )  throw (Exception)
 

Append a short to the command text.

Command& Dv::Sql::Command::operator<< unsigned  short  )  throw (Exception)
 

Append an unsigned short to the command text.

Command& Dv::Sql::Command::operator<< long   )  throw (Exception)
 

Append a long to the command text.

Command& Dv::Sql::Command::operator<< unsigned  long  )  throw (Exception)
 

Append an unsigned long to the command text.

Command& Dv::Sql::Command::operator<< float   )  throw (Exception)
 

Append a float to the command text.

Command& Dv::Sql::Command::operator<< double   )  throw (Exception)
 

Append a double to the command text.

bool Dv::Sql::Command::exec  )  [inline]
 

Execute the query, status is determined by underlying DBMS.

Definition at line 532 of file sqldb.h.

References cmd_, and Dv::Sql::Command_::exec().

unsigned int Dv::Sql::Command::insertid  )  const [inline]
 

Return auto_increment id of last insert.

Definition at line 536 of file sqldb.h.

References cmd_, and Dv::Sql::Command_::insertid().

void Dv::Sql::Command::fetch std::vector< std::string > &  v,
unsigned int  r
throw (Exception) [inline, private]
 

Retrieve the r'th row of the result ( 0<= r < nrows_ ).

Delegated to cmd_.

Definition at line 523 of file sqldb.h.

References cmd_, and Dv::Sql::Command_::fetch().

void Dv::Sql::Command::fetch std::string &  s,
unsigned int  r,
unsigned int  c
throw (Exception) [inline, private]
 

Retrieve the r'th row and c'th column of the result ( 0<= r < //nrows_ ).

Delegated to cmd_.

Definition at line 528 of file sqldb.h.

References cmd_, and Dv::Sql::Command_::fetch().

Command& Dv::Sql::Command::operator= const Command  )  [private]
 

Assignment is forbidden.

void Dv::Sql::Command::nrows unsigned int  n  )  [inline, private]
 

Definition at line 392 of file sqldb.h.

References nrows_.

void Dv::Sql::Command::ncols unsigned int  n  )  [inline, private]
 

Definition at line 393 of file sqldb.h.

References ncols_.

void Dv::Sql::Command::run  )  [inline, private]
 

Definition at line 394 of file sqldb.h.

References ran_.

void Dv::Sql::Command::err const char *  s  )  [inline, private]
 

Definition at line 395 of file sqldb.h.

References err_.

void Dv::Sql::Command::ok bool  b  )  [inline, private]
 

Definition at line 396 of file sqldb.h.

References ok_.


Friends And Related Function Documentation

friend class Db [friend]
 

Friend.

Definition at line 260 of file sqldb.h.

friend class iterator [friend]
 

Friend.

Definition at line 262 of file sqldb.h.

Referenced by end().

friend class Command_ [friend]
 

Friend.

Definition at line 264 of file sqldb.h.


Member Data Documentation

bool Dv::Sql::Command::ran_ [private]
 

Definition at line 360 of file sqldb.h.

Referenced by ran(), and run().

std::string Dv::Sql::Command::err_ [private]
 

Definition at line 361 of file sqldb.h.

Referenced by err().

bool Dv::Sql::Command::ok_ [private]
 

Definition at line 362 of file sqldb.h.

Referenced by ok(), and operator bool().

std::ostringstream Dv::Sql::Command::text_ [private]
 

Definition at line 363 of file sqldb.h.

Referenced by text().

bool Dv::Sql::Command::frozen_ [private]
 

Definition at line 364 of file sqldb.h.

Referenced by text().

unsigned long Dv::Sql::Command::nrows_ [private]
 

Nrows_ is # of rows in query answer, only valid if ran() && ok().

Definition at line 368 of file sqldb.h.

Referenced by nrows().

unsigned long Dv::Sql::Command::ncols_ [private]
 

Ncols_ is # of columns in query answer, only valid if ran() && ok().

Definition at line 370 of file sqldb.h.

Referenced by ncols().

Command_* Dv::Sql::Command::cmd_ [private]
 

Associated dbms-dependent object.

Many functions such as Dv::Sql::Command::exec() are delegated to cmd_->exec().

Definition at line 377 of file sqldb.h.

Referenced by db(), exec(), fetch(), fields(), and insertid().


The documentation for this class was generated from the following file:
dvmysql-0.4.11 [15 February, 2004]