Forums  +  C++ and Sockets  +  C++ and SQL: MySQL, sqlite, ODBC  +  Miscellaneous Projects
Logo
~Database~
~ C++ ~
~Contact~

Query Class Reference

SQL Statement execute / result set helper class. More...

#include <Query.h>

Collaboration diagram for Query:

Collaboration graph
List of all members.

Public Member Functions

 Query (Database &dbin)
 Constructor accepting reference to database object.
 Query (Database &dbin, const std::string &sql)
 Constructor accepting reference to database object and query to execute.
 ~Query ()
bool Connected ()
 Check to see if database object is connectable.
DatabaseGetDatabase () const
 Return reference to database object.
const std::string & GetLastQuery ()
 Return string of last query executed.
bool execute (const std::string &sql)
 execute() returns true if query is successful, does not store result
MYSQL_RES * get_result (const std::string &sql)
 execute query and store result.
void free_result ()
 free stored result, must be called after get_result()
MYSQL_ROW fetch_row ()
 Fetch next result row.
my_ulonglong insert_id ()
 Get id of last insert.
long num_rows ()
 Returns number of rows returned by last select call.
int num_cols ()
 Number of columns in current result.
std::string GetError ()
 Last error string.
int GetErrno ()
 Last error code.
bool is_null (const std::string &x)
 Check if column x in current row is null.
bool is_null (int x)
bool is_null ()
const char * get_string (const std::string &sql)
 Execute query and return first result as a string.
long get_count (const std::string &sql)
 Execute query and return first result as a long integer.
double get_num (const std::string &sql)
 Execute query and return first result as a double.
const char * getstr (const std::string &x)
const char * getstr (int x)
const char * getstr ()
long getval (const std::string &x)
long getval (int x)
long getval ()
unsigned long getuval (const std::string &x)
unsigned long getuval (int x)
unsigned long getuval ()
int64_t getbigint (const std::string &x)
int64_t getbigint (int x)
int64_t getbigint ()
uint64_t getubigint (const std::string &x)
uint64_t getubigint (int x)
uint64_t getubigint ()
double getnum (const std::string &x)
double getnum (int x)
double getnum ()
std::string safestr (const std::string &x)

Protected Member Functions

 Query (Database *dbin)
 Query.cpp.
 Query (Database *dbin, const std::string &sql)

Private Member Functions

 Query (const Query &q)
Queryoperator= (const Query &)
void error (const std::string &)

Private Attributes

Databasem_db
Database::OPENDBodb
MYSQL_RES * res
MYSQL_ROW row
short rowcount
std::string m_tmpstr
std::string m_last_query
std::map< std::string, int > m_nmap
int m_num_cols

Detailed Description

SQL Statement execute / result set helper class.

Definition at line 55 of file Query.h.


Constructor & Destructor Documentation

Query::Query ( Database dbin  ) 

Constructor accepting reference to database object.

Definition at line 67 of file Query.cpp.

00067                            : m_db(dbin),odb(dbin.grabdb()),res(NULL),row(NULL)
00068 ,m_num_cols(0)
00069 {
00070 }

Query::Query ( Database dbin,
const std::string &  sql 
)

Constructor accepting reference to database object and query to execute.

Definition at line 81 of file Query.cpp.

References execute().

00081                                                 : m_db(dbin),odb(dbin.grabdb()),res(NULL),row(NULL)
00082 ,m_num_cols(0)
00083 {
00084         execute(sql); // returns 0 if fail
00085 }

Query::~Query (  ) 

Definition at line 88 of file Query.cpp.

References Database::error(), Database::freedb(), GetDatabase(), m_db, odb, and res.

00089 {
00090         if (res)
00091         {
00092                 GetDatabase().error(*this, "mysql_free_result in destructor");
00093                 mysql_free_result(res);
00094         }
00095         if (odb)
00096         {
00097                 m_db.freedb(odb);
00098         }
00099 }

Query::Query ( Database dbin  )  [protected]

Query.cpp.

Published / author: 2001-02-15 / grymse@alhem.net

Definition at line 57 of file Query.cpp.

00058 :m_db(*dbin)
00059 ,odb(dbin ? dbin -> grabdb() : NULL)
00060 ,res(NULL)
00061 ,row(NULL)
00062 ,m_num_cols(0)
00063 {
00064 }

Query::Query ( Database dbin,
const std::string &  sql 
) [protected]

Definition at line 73 of file Query.cpp.

References execute().

00073                                                 : m_db(*dbin)
00074 ,odb(dbin ? dbin -> grabdb() : NULL),res(NULL),row(NULL)
00075 ,m_num_cols(0)
00076 {
00077         execute(sql);
00078 }

Query::Query ( const Query q  )  [inline, private]

Definition at line 131 of file Query.h.

00131 : m_db(q.GetDatabase()) {}


Member Function Documentation

bool Query::Connected (  ) 

Check to see if database object is connectable.

Definition at line 442 of file Query.cpp.

References Database::error(), GetDatabase(), and odb.

00443 {
00444         if (odb)
00445         {
00446                 if (mysql_ping(&odb -> mysql))
00447                 {
00448                         GetDatabase().error(*this, "mysql_ping() failed");
00449                         return false;
00450                 }
00451         }
00452         return odb ? true : false;
00453 }

Database & Query::GetDatabase (  )  const

Return reference to database object.

Definition at line 102 of file Query.cpp.

References m_db.

Referenced by Connected(), execute(), get_result(), and ~Query().

00103 {
00104         return m_db;
00105 }

const std::string & Query::GetLastQuery (  ) 

Return string of last query executed.

Definition at line 424 of file Query.cpp.

References m_last_query.

Referenced by SysLog::error(), and StderrLog::error().

00425 {
00426         return m_last_query;
00427 }

bool Query::execute ( const std::string &  sql  ) 

execute() returns true if query is successful, does not store result

Definition at line 108 of file Query.cpp.

References Database::error(), GetDatabase(), m_last_query, odb, and res.

Referenced by get_result(), and Query().

00109 {               // query, no result
00110         m_last_query = sql;
00111         if (odb && res)
00112         {
00113                 GetDatabase().error(*this, "execute: query busy");
00114         }
00115         if (odb && !res)
00116         {
00117                 if (mysql_query(&odb -> mysql,sql.c_str()))
00118                 {
00119                         GetDatabase().error(*this,"query failed");
00120                 }
00121                 else
00122                 {
00123                         return true;
00124                 }
00125         }
00126         return false;
00127 }

MYSQL_RES * Query::get_result ( const std::string &  sql  ) 

execute query and store result.

Definition at line 133 of file Query.cpp.

References Database::error(), execute(), GetDatabase(), m_nmap, m_num_cols, odb, and res.

Referenced by get_count(), get_num(), and get_string().

00134 {       // query, result
00135         if (odb && res)
00136         {
00137                 GetDatabase().error(*this, "get_result: query busy");
00138         }
00139         if (odb && !res)
00140         {
00141                 if (execute(sql))
00142                 {
00143                         res = mysql_store_result(&odb -> mysql);
00144                         if (res)
00145                         {
00146                                 MYSQL_FIELD *f = mysql_fetch_field(res);
00147                                 int i = 1;
00148                                 while (f)
00149                                 {
00150                                         if (f -> name)
00151                                                 m_nmap[f -> name] = i;
00152                                         f = mysql_fetch_field(res);
00153                                         i++;
00154                                 }
00155                                 m_num_cols = i - 1;
00156                         }
00157                 }
00158         }
00159         return res;
00160 }

void Query::free_result (  ) 

free stored result, must be called after get_result()

Definition at line 163 of file Query.cpp.

References m_nmap, m_num_cols, odb, res, and row.

Referenced by get_count(), get_num(), and get_string().

00164 {
00165         if (odb && res)
00166         {
00167                 mysql_free_result(res);
00168                 res = NULL;
00169                 row = NULL;
00170         }
00171         while (m_nmap.size())
00172         {
00173                 std::map<std::string,int>::iterator it = m_nmap.begin();
00174                 m_nmap.erase(it);
00175         }
00176         m_num_cols = 0;
00177 }

MYSQL_ROW Query::fetch_row (  ) 

Fetch next result row.

Returns:
false if there was no row to fetch (end of rows)

Definition at line 180 of file Query.cpp.

References odb, res, row, and rowcount.

Referenced by get_count(), get_num(), and get_string().

00181 {
00182         rowcount = 0;
00183         return odb && res ? row = mysql_fetch_row(res) : NULL;
00184 }

my_ulonglong Query::insert_id (  ) 

Get id of last insert.

Definition at line 187 of file Query.cpp.

References odb.

00188 {
00189         if (odb)
00190         {
00191                 return mysql_insert_id(&odb -> mysql);
00192         }
00193         else
00194         {
00195                 return 0;
00196         }
00197 }

long Query::num_rows (  ) 

Returns number of rows returned by last select call.

Definition at line 200 of file Query.cpp.

References odb, and res.

00201 {
00202         return odb && res ? mysql_num_rows(res) : 0;
00203 }

int Query::num_cols (  ) 

Number of columns in current result.

Definition at line 206 of file Query.cpp.

References m_num_cols.

00207 {
00208         return m_num_cols;
00209 }

std::string Query::GetError (  ) 

Last error string.

Definition at line 430 of file Query.cpp.

References odb.

Referenced by SysLog::error(), and StderrLog::error().

00431 {
00432         return odb ? mysql_error(&odb -> mysql) : "";
00433 }

int Query::GetErrno (  ) 

Last error code.

Definition at line 436 of file Query.cpp.

References odb.

Referenced by SysLog::error(), and StderrLog::error().

00437 {
00438         return odb ? mysql_errno(&odb -> mysql) : 0;
00439 }

bool Query::is_null ( const std::string &  x  ) 

Check if column x in current row is null.

Definition at line 222 of file Query.cpp.

References error(), is_null(), and m_nmap.

00223 {
00224         int index = m_nmap[x] - 1;
00225         if (index >= 0)
00226                 return is_null(index);
00227         error("Column name lookup failure: " + x);
00228         return false;
00229 }

bool Query::is_null ( int  x  ) 

Definition at line 212 of file Query.cpp.

References odb, res, and row.

00213 {
00214         if (odb && res && row)
00215         {
00216                 return row[x] ? false : true;
00217         }
00218         return false; // ...
00219 }

bool Query::is_null (  ) 

Definition at line 232 of file Query.cpp.

References rowcount.

Referenced by is_null().

00233 {
00234         return is_null(rowcount++);
00235 }

const char * Query::get_string ( const std::string &  sql  ) 

Execute query and return first result as a string.

Definition at line 407 of file Query.cpp.

References fetch_row(), free_result(), get_result(), getstr(), and m_tmpstr.

00408 {
00409         bool found = false;
00410         m_tmpstr = "";
00411         if (get_result(sql))
00412         {
00413                 if (fetch_row())
00414                 {
00415                         m_tmpstr = getstr();
00416                         found = true;
00417                 }
00418                 free_result();
00419         }
00420         return m_tmpstr.c_str(); // %! changed from 1.0 which didn't return NULL on failed query
00421 }

long Query::get_count ( const std::string &  sql  ) 

Execute query and return first result as a long integer.

Definition at line 394 of file Query.cpp.

References fetch_row(), free_result(), get_result(), and getval().

00395 {
00396         long l = 0;
00397         if (get_result(sql))
00398         {
00399                 if (fetch_row())
00400                         l = getval();
00401                 free_result();
00402         }
00403         return l;
00404 }

double Query::get_num ( const std::string &  sql  ) 

Execute query and return first result as a double.

Definition at line 379 of file Query.cpp.

References fetch_row(), free_result(), get_result(), and getnum().

00380 {
00381         double l = 0;
00382         if (get_result(sql))
00383         {
00384                 if (fetch_row())
00385                 {
00386                         l = getnum();
00387                 }
00388                 free_result();
00389         }
00390         return l;
00391 }

const char * Query::getstr ( const std::string &  x  ) 

Definition at line 238 of file Query.cpp.

References error(), getstr(), and m_nmap.

00239 {
00240         int index = m_nmap[x] - 1;
00241         if (index >= 0)
00242                 return getstr(index);
00243         error("Column name lookup failure: " + x);
00244         return NULL;
00245 }

const char * Query::getstr ( int  x  ) 

Definition at line 248 of file Query.cpp.

References odb, res, and row.

00249 {
00250         if (odb && res && row)
00251         {
00252                 return row[x] ? row[x] : "";
00253         }
00254         return NULL;
00255 }

const char * Query::getstr (  ) 

Definition at line 258 of file Query.cpp.

References rowcount.

Referenced by get_string(), and getstr().

00259 {
00260         return getstr(rowcount++);
00261 }

long Query::getval ( const std::string &  x  ) 

Definition at line 280 of file Query.cpp.

References error(), getval(), and m_nmap.

00281 {
00282         int index = m_nmap[x] - 1;
00283         if (index >= 0)
00284                 return getval(index);
00285         error("Column name lookup failure: " + x);
00286         return 0;
00287 }

long Query::getval ( int  x  ) 

Definition at line 290 of file Query.cpp.

References odb, res, and row.

00291 {
00292         return odb && res && row && row[x] ? atol(row[x]) : 0;
00293 }

long Query::getval (  ) 

Definition at line 302 of file Query.cpp.

References rowcount.

Referenced by get_count(), and getval().

00303 {
00304         return getval(rowcount++);
00305 }

unsigned long Query::getuval ( const std::string &  x  ) 

Definition at line 308 of file Query.cpp.

References error(), getuval(), and m_nmap.

00309 {
00310         int index = m_nmap[x] - 1;
00311         if (index >= 0)
00312                 return getuval(index);
00313         error("Column name lookup failure: " + x);
00314         return 0;
00315 }

unsigned long Query::getuval ( int  x  ) 

Definition at line 318 of file Query.cpp.

References Database::a2ubigint(), m_db, odb, res, and row.

00319 {
00320         unsigned long l = 0;
00321         if (odb && res && row && row[x])
00322         {
00323                 l = m_db.a2ubigint(row[x]);
00324         }
00325         return l;
00326 }

unsigned long Query::getuval (  ) 

Definition at line 329 of file Query.cpp.

References rowcount.

Referenced by getuval().

00330 {
00331         return getuval(rowcount++);
00332 }

int64_t Query::getbigint ( const std::string &  x  ) 

Definition at line 335 of file Query.cpp.

References error(), getbigint(), and m_nmap.

00336 {
00337         int index = m_nmap[x] - 1;
00338         if (index >= 0)
00339                 return getbigint(index);
00340         error("Column name lookup failure: " + x);
00341         return 0;
00342 }

int64_t Query::getbigint ( int  x  ) 

Definition at line 345 of file Query.cpp.

References Database::a2bigint(), m_db, odb, res, and row.

00346 {
00347         return odb && res && row && row[x] ? m_db.a2bigint(row[x]) : 0;
00348 }

int64_t Query::getbigint (  ) 

Definition at line 351 of file Query.cpp.

References rowcount.

Referenced by getbigint().

00352 {
00353         return getbigint(rowcount++);
00354 }

uint64_t Query::getubigint ( const std::string &  x  ) 

Definition at line 357 of file Query.cpp.

References error(), getubigint(), and m_nmap.

00358 {
00359         int index = m_nmap[x] - 1;
00360         if (index >= 0)
00361                 return getubigint(index);
00362         error("Column name lookup failure: " + x);
00363         return 0;
00364 }

uint64_t Query::getubigint ( int  x  ) 

Definition at line 367 of file Query.cpp.

References Database::a2ubigint(), m_db, odb, res, and row.

00368 {
00369         return odb && res && row && row[x] ? m_db.a2ubigint(row[x]) : 0;
00370 }

uint64_t Query::getubigint (  ) 

Definition at line 373 of file Query.cpp.

References rowcount.

Referenced by getubigint().

00374 {
00375         return getubigint(rowcount++);
00376 }

double Query::getnum ( const std::string &  x  ) 

Definition at line 264 of file Query.cpp.

References error(), getnum(), and m_nmap.

00265 {
00266         int index = m_nmap[x] - 1;
00267         if (index >= 0)
00268                 return getnum(index);
00269         error("Column name lookup failure: " + x);
00270         return 0;
00271 }

double Query::getnum ( int  x  ) 

Definition at line 274 of file Query.cpp.

References odb, res, and row.

00275 {
00276         return odb && res && row && row[x] ? atof(row[x]) : 0;
00277 }

double Query::getnum (  ) 

Definition at line 296 of file Query.cpp.

References rowcount.

Referenced by get_num(), and getnum().

00297 {
00298         return getnum(rowcount++);
00299 }

std::string Query::safestr ( const std::string &  x  ) 

Definition at line 462 of file Query.cpp.

References m_db, and Database::safestr().

00463 {
00464         return m_db.safestr(x);
00465 }

Query& Query::operator= ( const Query  )  [inline, private]

Definition at line 132 of file Query.h.

00132 { return *this; }

void Query::error ( const std::string &   )  [private]

Definition at line 456 of file Query.cpp.

References Database::error(), and m_db.

Referenced by getbigint(), getnum(), getstr(), getubigint(), getuval(), getval(), and is_null().

00457 {
00458         m_db.error(*this, x.c_str());
00459 }


Member Data Documentation

Database& Query::m_db [private]

Definition at line 134 of file Query.h.

Referenced by error(), getbigint(), GetDatabase(), getubigint(), getuval(), safestr(), and ~Query().

MYSQL_ROW Query::row [private]

Definition at line 137 of file Query.h.

Referenced by fetch_row(), free_result(), getbigint(), getnum(), getstr(), getubigint(), getuval(), getval(), and is_null().

short Query::rowcount [private]

Definition at line 138 of file Query.h.

Referenced by fetch_row(), getbigint(), getnum(), getstr(), getubigint(), getuval(), getval(), and is_null().

std::string Query::m_tmpstr [private]

Definition at line 139 of file Query.h.

Referenced by get_string().

std::string Query::m_last_query [private]

Definition at line 140 of file Query.h.

Referenced by execute(), and GetLastQuery().

std::map<std::string,int> Query::m_nmap [private]

Definition at line 141 of file Query.h.

Referenced by free_result(), get_result(), getbigint(), getnum(), getstr(), getubigint(), getuval(), getval(), and is_null().

int Query::m_num_cols [private]

Definition at line 142 of file Query.h.

Referenced by free_result(), get_result(), and num_cols().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2006 by Anders Hedström