Main | License | Installation | FAQ | Screenshots | Contact | Links | Sf.net | Freshmeat.net | KDE-Apps.org
Requirements | Design | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

MySQLStatement Class Reference
[MySQL C++ Wrappers]

#include <mysqlStatement.hpp>

Inheritance diagram for MySQLStatement:

Inheritance graph
[legend]

Public Member Functions

 MySQLStatement (MySQLDatabase *_database, const std::string &query) throw (Error)
virtual ~MySQLStatement (void)
 Destructor frees the statement handle.
virtual int getNumCols (void) throw (Error)
virtual std::string getColAttr (int col) throw (Error)
virtual ResultRowPtr fetchRow (void) throw (Error)
 Fetch row of resulting row.

Protected Attributes

MySQLDatabasedatabase
MYSQL_RES * result
ResultRowPtr resultRow

Detailed Description

Class for executing SQL query and querying results. Call MySQLDatabase::execQuery to acquire an instance of this class.
Date:
Thu Mar 03 2005
Author:
Jan Wedekind (wedesoft@users.sourceforge.net)

Definition at line 30 of file mysqlStatement.hpp.


Constructor & Destructor Documentation

MySQLStatement::MySQLStatement ( MySQLDatabase _database,
const std::string &  query 
) throw (Error)

Constructor requiring MySQL handle. Call MySQLDatabase::execQuery to acquire an instance of this class.

See also:
MySQLDatabase::execQuery

Definition at line 22 of file mysqlStatement.cpp.

References ERRORMACRO.

00024                :
00025   database( _database ), result( NULL )
00026 {
00027   if ( !query.empty() ) {
00028 #ifndef NDEBUG
00029     // cerr << query << endl;
00030 #endif
00031     // Apparently a trailing semicolon doesn't cause any problems.
00032     ERRORMACRO( mysql_real_query( database->getConnection(), query.c_str(),
00033                                   query.size() ) == 0,
00034                 Error, , "Error executing query '" << query << "': "
00035                 << mysql_error( database->getConnection() ) );
00036 
00037     // Retrieve column-information, if applicable.
00038     if ( getNumCols() > 0 ) {
00039       result = mysql_use_result( database->getConnection() );
00040       ERRORMACRO( result != NULL, Error, ,
00041                   "Error retrieving column-information: "
00042                   << mysql_error( database->getConnection() ) );
00043     };
00044   } else
00045     database = NULL;
00046 }

MySQLStatement::~MySQLStatement ( void   )  [virtual]

Destructor frees the statement handle.

Definition at line 48 of file mysqlStatement.cpp.

References result, and resultRow.

00049 {
00050 
00051   if ( resultRow )
00052     resultRow->invalidate();
00053 
00054   if ( result != NULL ) {
00055 
00056     // Purge remaining data (see mysql-documentation!).
00057     while ( mysql_fetch_row( result ) );
00058     mysql_free_result( result );
00059 
00060   };
00061 
00062 }


Member Function Documentation

int MySQLStatement::getNumCols ( void   )  throw (Error) [virtual]

Get number of result columns.

Returns:
Number of resulting columns of a SELECT query. 0 if there is none.

Implements Statement.

Definition at line 64 of file mysqlStatement.cpp.

References database, and MySQLDatabase::getConnection().

Referenced by getColAttr().

00065 {
00066   int retVal;
00067   if ( database != NULL )
00068     retVal = mysql_field_count( database->getConnection() );
00069   else
00070     retVal = 0;
00071   return retVal;
00072 }

std::string MySQLStatement::getColAttr ( int  col  )  throw (Error) [virtual]

Meta information about column.

Parameters:
col Number of column (between 0 and getNumCols()-1).

Implements Statement.

Definition at line 74 of file mysqlStatement.cpp.

References ERRORMACRO, getNumCols(), and result.

00075 {
00076   ERRORMACRO( col >= 0 && col < getNumCols(), Error, ,
00077               "Column number of meta-column must be between 0 and "
00078               << ( getNumCols() - 1 ) << " but was " << col << "." );
00079 
00080   assert( result != NULL );
00081   mysql_field_seek( result, col );
00082   return mysql_fetch_field( result )->name;
00083 }

ResultRowPtr MySQLStatement::fetchRow ( void   )  throw (Error) [virtual]

Fetch row of resulting row.

Implements Statement.

Definition at line 85 of file mysqlStatement.cpp.

References ERRORMACRO, result, and resultRow.

00086 {
00087   assert( result != NULL );
00088 
00089   if ( resultRow ) {
00090     ERRORMACRO( resultRow.use_count() <= 1, Error, ,
00091                 "Can not fetch new row, "
00092                 "because previous row was not released yet." );
00093     assert( resultRow.use_count() == 1 );
00094   };
00095 
00096   MYSQL_ROW row = mysql_fetch_row( result );
00097   if ( row != NULL )
00098     resultRow = ResultRowPtr( new MySQLResultRow( row ) );
00099   else
00100     resultRow.reset();
00101 
00102   return resultRow;
00103 }


Field Documentation

MySQLDatabase* MySQLStatement::database [protected]

Definition at line 48 of file mysqlStatement.hpp.

Referenced by getNumCols().

MYSQL_RES* MySQLStatement::result [protected]

Definition at line 50 of file mysqlStatement.hpp.

Referenced by fetchRow(), getColAttr(), and ~MySQLStatement().

ResultRowPtr MySQLStatement::resultRow [protected]

Definition at line 52 of file mysqlStatement.hpp.

Referenced by fetchRow(), and ~MySQLStatement().


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

anymeal 0.30 - recipe management software - Make the most of your food! - © Jan Wedekind Sun Sep 14 16:01:29 2008 - GNU Free Documentation License