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

Query Class Reference

SQL Statement execute / result. 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 string to execute.
 ~Query ()
bool Connected ()
 Check if database object is connectable.
DatabaseGetDatabase () const
 Return reference to database object.
const std::string & GetLastQuery ()
 Return string containing last query executed.
bool execute (const std::string &sql)
 execute() returns true if query is successful, does not store result.
sqlite3_stmt * get_result (const std::string &sql)
 Execute query and store result.
void free_result ()
 Free stored result, must be called after get_result() before calling execute()/get_result() again.
bool fetch_row ()
 Fetch next result row.
sqlite_int64 insert_id ()
 Get id of last insert.
long num_rows ()
 Returns 0 if there are no rows to fetch.
int num_cols ()
 Number of columns in current result.
std::string GetError ()
 Last error string.
int GetErrno ()
 Last error code.
bool is_null (int x)
 Check if column x in current row 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)
 Return column named x as a string value.
const char * getstr (int x)
 Return column x as a string value.
const char * getstr ()
 Return next column as a string value - see rowcount.
long getval (const std::string &x)
 Return column named x as a long integer.
long getval (int x)
 Return column x as a long integer.
long getval ()
 Return next column as a long integer - see rowcount.
unsigned long getuval (const std::string &x)
 Return column named x as an unsigned long integer.
unsigned long getuval (int x)
 Return column x as an unsigned long integer.
unsigned long getuval ()
 Return next column as an unsigned long integer.
int64_t getbigint (const std::string &x)
 Return column named x as a 64-bit integer value.
int64_t getbigint (int x)
 Return column x as a 64-bit integer value.
int64_t getbigint ()
 Return next column as a 64-bit integer value.
uint64_t getubigint (const std::string &x)
 Return column named x as an unsigned 64-bit integer value.
uint64_t getubigint (int x)
 Return column x as an unsigned 64-bit integer value.
uint64_t getubigint ()
 Return next column as an unsigned 64-bit integer value.
double getnum (const std::string &x)
 Return column named x as a double.
double getnum (int x)
 Return column x as a double.
double getnum ()
 Return next column as a double.

Private Member Functions

 Query (const Query &q)
 Hide the copy constructor.
Queryoperator= (const Query &)
 Hide the assignment operator.
void ViewRes ()
 Print current result to stdout.
void error (const std::string &)
 Print error to debug class.

Private Attributes

Databasem_db
 Reference to database object.
Database::OPENDBodb
 Connection pool handle.
sqlite3_stmt * res
 Stored result.
bool row
 true if fetch_row succeeded
short rowcount
 Current column pointer in result.
std::string m_tmpstr
 Used to store result in get_string() call.
std::string m_last_query
 Last query executed.
int cache_rc
 Cached result after call to get_result().
bool cache_rc_valid
 Indicates cache_rc is valid.
int m_row_count
 0 if get_result() returned no rows
std::map< std::string, int > m_nmap
 map translating column names to index
int m_num_cols
 number of columns in result

Detailed Description

SQL Statement execute / result.

Definition at line 55 of file Query.h.


Constructor & Destructor Documentation

Query::Query ( Database dbin  ) 

Constructor accepting reference to database object.

Definition at line 52 of file Query.cpp.

00053 : m_db(dbin)
00054 ,odb(dbin.grabdb())
00055 ,res(NULL)
00056 ,row(false)
00057 ,cache_rc(0)
00058 ,cache_rc_valid(false)
00059 ,m_row_count(0)
00060 ,m_num_cols(0)
00061 {
00062 }

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

Constructor accepting reference to database object and query string to execute.

Definition at line 65 of file Query.cpp.

References execute().

00066 : m_db(dbin)
00067 ,odb(dbin.grabdb())
00068 ,res(NULL)
00069 ,row(false)
00070 ,cache_rc(0)
00071 ,cache_rc_valid(false)
00072 ,m_row_count(0)
00073 ,m_num_cols(0)
00074 {
00075         execute(sql);
00076 }

Query::~Query (  ) 

Definition at line 79 of file Query.cpp.

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

00080 {
00081         if (res)
00082         {
00083                 GetDatabase().error(*this, "sqlite3_finalize in destructor");
00084                 sqlite3_finalize(res);
00085         }
00086         if (odb)
00087         {
00088                 m_db.freedb(odb);
00089         }
00090 }

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

Hide the copy constructor.

Definition at line 149 of file Query.h.

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


Member Function Documentation

bool Query::Connected (  ) 

Check if database object is connectable.

Definition at line 505 of file Query.cpp.

References odb.

00506 {
00507         return odb ? true : false;
00508 }

Database & Query::GetDatabase (  )  const

Return reference to database object.

Definition at line 93 of file Query.cpp.

References m_db.

Referenced by error(), execute(), fetch_row(), get_result(), and ~Query().

00094 {
00095         return m_db;
00096 }

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

Return string containing last query executed.

Definition at line 483 of file Query.cpp.

References m_last_query.

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

00484 {
00485         return m_last_query;
00486 }

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

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

Definition at line 103 of file Query.cpp.

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

Referenced by main(), and Query().

00104 {
00105         // query, no result
00106         m_last_query = sql;
00107         if (odb && res)
00108         {
00109                 GetDatabase().error(*this, "execute: query busy");
00110         }
00111         if (odb && !res)
00112         {
00113                 const char *s = NULL;
00114                 int rc = sqlite3_prepare(odb -> db, sql.c_str(), sql.size(), &res, &s);
00115                 if (rc != SQLITE_OK)
00116                 {
00117                         GetDatabase().error(*this, "execute: prepare query failed");
00118                         return false;
00119                 }
00120                 if (!res)
00121                 {
00122                         GetDatabase().error(*this, "execute: query failed");
00123                         return false;
00124                 }
00125                 rc = sqlite3_step(res); // execute
00126                 sqlite3_finalize(res); // deallocate statement
00127                 res = NULL;
00128                 switch (rc)
00129                 {
00130                 case SQLITE_BUSY:
00131                         GetDatabase().error(*this, "execute: database busy");
00132                         return false;
00133                 case SQLITE_DONE:
00134                 case SQLITE_ROW:
00135                         return true;
00136                 case SQLITE_ERROR:
00137                         GetDatabase().error(*this, sqlite3_errmsg(odb -> db));
00138                         return false;
00139                 case SQLITE_MISUSE:
00140                         GetDatabase().error(*this, "execute: database misuse");
00141                         return false;
00142                 }
00143                 GetDatabase().error(*this, "execute: unknown result code");
00144         }
00145         return false;
00146 }

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

Execute query and store result.

Definition at line 152 of file Query.cpp.

References cache_rc, cache_rc_valid, Database::error(), GetDatabase(), m_last_query, m_nmap, m_num_cols, m_row_count, odb, and res.

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

00153 {
00154         // query, result
00155         m_last_query = sql;
00156         if (odb && res)
00157         {
00158                 GetDatabase().error(*this, "get_result: query busy");
00159         }
00160         if (odb && !res)
00161         {
00162                 const char *s = NULL;
00163                 int rc = sqlite3_prepare(odb -> db, sql.c_str(), sql.size(), &res, &s);
00164                 if (rc != SQLITE_OK)
00165                 {
00166                         GetDatabase().error(*this, "get_result: prepare query failed");
00167                         return NULL;
00168                 }
00169                 if (!res)
00170                 {
00171                         GetDatabase().error(*this, "get_result: query failed");
00172                         return NULL;
00173                 }
00174                 // get column names from result
00175                 {
00176                         int i = 0;
00177                         do
00178                         {
00179                                 const char *p = sqlite3_column_name(res, i);
00180                                 if (!p)
00181                                         break;
00182                                 m_nmap[p] = ++i;
00183                         } while (true);
00184                         m_num_cols = i;
00185                 }
00186                 cache_rc = sqlite3_step(res);
00187                 cache_rc_valid = true;
00188                 m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0;
00189         }
00190         return res;
00191 }

void Query::free_result (  ) 

Free stored result, must be called after get_result() before calling execute()/get_result() again.

Definition at line 194 of file Query.cpp.

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

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

00195 {
00196         if (odb && res)
00197         {
00198                 sqlite3_finalize(res);
00199                 res = NULL;
00200                 row = false;
00201                 cache_rc_valid = false;
00202         }
00203         // clear column names
00204         while (m_nmap.size())
00205         {
00206                 std::map<std::string,int>::iterator it = m_nmap.begin();
00207                 m_nmap.erase(it);
00208         }
00209 }

bool Query::fetch_row (  ) 

Fetch next result row.

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

Definition at line 212 of file Query.cpp.

References cache_rc, cache_rc_valid, Database::error(), GetDatabase(), odb, res, row, and rowcount.

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

00213 {
00214         rowcount = 0;
00215         row = false;
00216         if (odb && res)
00217         {
00218                 int rc = cache_rc_valid ? cache_rc : sqlite3_step(res); // execute
00219                 cache_rc_valid = false;
00220                 switch (rc)
00221                 {
00222                 case SQLITE_BUSY:
00223                         GetDatabase().error(*this, "execute: database busy");
00224                         return false;
00225                 case SQLITE_DONE:
00226                         return false;
00227                 case SQLITE_ROW:
00228                         row = true;
00229                         return true;
00230                 case SQLITE_ERROR:
00231                         GetDatabase().error(*this, sqlite3_errmsg(odb -> db));
00232                         return false;
00233                 case SQLITE_MISUSE:
00234                         GetDatabase().error(*this, "execute: database misuse");
00235                         return false;
00236                 }
00237                 GetDatabase().error(*this, "execute: unknown result code");
00238         }
00239         return false;
00240 }

sqlite_int64 Query::insert_id (  ) 

Get id of last insert.

Definition at line 243 of file Query.cpp.

References odb.

00244 {
00245         if (odb)
00246         {
00247                 return sqlite3_last_insert_rowid(odb -> db);
00248         }
00249         else
00250         {
00251                 return 0;
00252         }
00253 }

long Query::num_rows (  ) 

Returns 0 if there are no rows to fetch.

Definition at line 256 of file Query.cpp.

References m_row_count, odb, and res.

00257 {
00258         return odb && res ? m_row_count : 0;
00259 }

int Query::num_cols (  ) 

Number of columns in current result.

Definition at line 262 of file Query.cpp.

References m_num_cols.

00263 {
00264         return m_num_cols;
00265 }

std::string Query::GetError (  ) 

Last error string.

Definition at line 489 of file Query.cpp.

References odb.

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

00490 {
00491         if (odb)
00492                 return sqlite3_errmsg(odb -> db);
00493         return "";
00494 }

int Query::GetErrno (  ) 

Last error code.

Definition at line 497 of file Query.cpp.

References odb.

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

00498 {
00499         if (odb)
00500                 return sqlite3_errcode(odb -> db);
00501         return 0;
00502 }

bool Query::is_null ( int  x  ) 

Check if column x in current row is null.

Definition at line 268 of file Query.cpp.

References odb, res, and row.

00269 {
00270         if (odb && res && row)
00271         {
00272                 if (sqlite3_column_type(res, x) == SQLITE_NULL)
00273                         return true;
00274         }
00275         return false; // ...
00276 }

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

Execute query and return first result as a string.

Definition at line 466 of file Query.cpp.

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

00467 {
00468         bool found = false;
00469         m_tmpstr = "";
00470         if (get_result(sql))
00471         {
00472                 if (fetch_row())
00473                 {
00474                         m_tmpstr = getstr();
00475                         found = true;
00476                 }
00477                 free_result();
00478         }
00479         return m_tmpstr.c_str(); // %! changed from 1.0 which didn't return NULL on failed query
00480 }

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

Execute query and return first result as a long integer.

Definition at line 453 of file Query.cpp.

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

00454 {
00455         long l = 0;
00456         if (get_result(sql))
00457         {
00458                 if (fetch_row())
00459                         l = getval();
00460                 free_result();
00461         }
00462         return l;
00463 }

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

Execute query and return first result as a double.

Definition at line 438 of file Query.cpp.

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

00439 {
00440         double l = 0;
00441         if (get_result(sql))
00442         {
00443                 if (fetch_row())
00444                 {
00445                         l = getnum();
00446                 }
00447                 free_result();
00448         }
00449         return l;
00450 }

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

Return column named x as a string value.

Definition at line 279 of file Query.cpp.

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

Referenced by main().

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

const char * Query::getstr ( int  x  ) 

Return column x as a string value.

Definition at line 289 of file Query.cpp.

References odb, res, and row.

00290 {
00291         if (odb && res && row && x < sqlite3_column_count(res) )
00292         {
00293                 const unsigned char *tmp = sqlite3_column_text(res, x);
00294                 return tmp ? (const char *)tmp : "";
00295         }
00296         return "";
00297 }

const char * Query::getstr (  ) 

Return next column as a string value - see rowcount.

Definition at line 300 of file Query.cpp.

References rowcount.

Referenced by get_string(), and getstr().

00301 {
00302         return getstr(rowcount++);
00303 }

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

Return column named x as a long integer.

Definition at line 326 of file Query.cpp.

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

Referenced by main().

00327 {
00328         int index = m_nmap[x] - 1;
00329         if (index >= 0)
00330                 return getval(index);
00331         error("Column name lookup failure: " + x);
00332         return 0;
00333 }

long Query::getval ( int  x  ) 

Return column x as a long integer.

Definition at line 336 of file Query.cpp.

References odb, res, and row.

00337 {
00338         if (odb && res && row)
00339         {
00340                 return sqlite3_column_int(res, x);
00341         }
00342         return 0;
00343 }

long Query::getval (  ) 

Return next column as a long integer - see rowcount.

Definition at line 352 of file Query.cpp.

References rowcount.

Referenced by get_count(), and getval().

00353 {
00354         return getval(rowcount++);
00355 }

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

Return column named x as an unsigned long integer.

Definition at line 358 of file Query.cpp.

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

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

unsigned long Query::getuval ( int  x  ) 

Return column x as an unsigned long integer.

Definition at line 368 of file Query.cpp.

References odb, res, and row.

00369 {
00370         unsigned long l = 0;
00371         if (odb && res && row)
00372         {
00373                 l = sqlite3_column_int(res, x);
00374         }
00375         return l;
00376 }

unsigned long Query::getuval (  ) 

Return next column as an unsigned long integer.

Definition at line 379 of file Query.cpp.

References rowcount.

Referenced by getuval().

00380 {
00381         return getuval(rowcount++);
00382 }

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

Return column named x as a 64-bit integer value.

Definition at line 385 of file Query.cpp.

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

00386 {
00387         int index = m_nmap[x] - 1;
00388         if (index >= 0)
00389                 return getbigint(index);
00390         error("Column name lookup failure: " + x);
00391         return 0;
00392 }

int64_t Query::getbigint ( int  x  ) 

Return column x as a 64-bit integer value.

Definition at line 395 of file Query.cpp.

References odb, res, and row.

00396 {
00397         if (odb && res && row)
00398         {
00399                 return sqlite3_column_int64(res, x);
00400         }
00401         return 0;
00402 }

int64_t Query::getbigint (  ) 

Return next column as a 64-bit integer value.

Definition at line 405 of file Query.cpp.

References rowcount.

Referenced by getbigint().

00406 {
00407         return getbigint(rowcount++);
00408 }

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

Return column named x as an unsigned 64-bit integer value.

Definition at line 411 of file Query.cpp.

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

00412 {
00413         int index = m_nmap[x] - 1;
00414         if (index >= 0)
00415                 return getubigint(index);
00416         error("Column name lookup failure: " + x);
00417         return 0;
00418 }

uint64_t Query::getubigint ( int  x  ) 

Return column x as an unsigned 64-bit integer value.

Definition at line 421 of file Query.cpp.

References odb, res, and row.

00422 {
00423         uint64_t l = 0;
00424         if (odb && res && row)
00425         {
00426                 l = sqlite3_column_int64(res, x);
00427         }
00428         return l;
00429 }

uint64_t Query::getubigint (  ) 

Return next column as an unsigned 64-bit integer value.

Definition at line 432 of file Query.cpp.

References rowcount.

Referenced by getubigint().

00433 {
00434         return getubigint(rowcount++);
00435 }

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

Return column named x as a double.

Definition at line 306 of file Query.cpp.

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

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

double Query::getnum ( int  x  ) 

Return column x as a double.

Definition at line 316 of file Query.cpp.

References odb, res, and row.

00317 {
00318         if (odb && res && row)
00319         {
00320                 return sqlite3_column_double(res, x);
00321         }
00322         return 0;
00323 }

double Query::getnum (  ) 

Return next column as a double.

Definition at line 346 of file Query.cpp.

References rowcount.

Referenced by get_num(), and getnum().

00347 {
00348         return getnum(rowcount++);
00349 }

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

Hide the assignment operator.

Definition at line 151 of file Query.h.

00151 { return *this; }

void Query::ViewRes (  )  [private]

Print current result to stdout.

Definition at line 513 of file Query.cpp.

References res.

00514 {
00515         if (!res)
00516         {
00517                 printf("no result stored\n");
00518                 return;
00519         }
00520         printf("result column count = %d\n", sqlite3_column_count(res));
00521         for (int i = 0; i < sqlite3_column_count(res); i++)
00522         {
00523                 printf(" %2d   type %d   name '%s'", i, sqlite3_column_type(res, i), sqlite3_column_name(res, i));
00524                 printf("  / '%s'", (char *)sqlite3_column_text(res, i));
00525                 printf("  / %d", sqlite3_column_int(res, i));
00526                 printf("  / %f", sqlite3_column_double(res, i));
00527                 printf("\n");
00528         }
00529 }

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

Print error to debug class.

Definition at line 532 of file Query.cpp.

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

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

00533 {
00534         GetDatabase().error(*this, msg);
00535 }


Member Data Documentation

Database& Query::m_db [private]

Reference to database object.

Definition at line 156 of file Query.h.

Referenced by GetDatabase(), and ~Query().

sqlite3_stmt* Query::res [private]

bool Query::row [private]

true if fetch_row succeeded

Definition at line 159 of file Query.h.

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

short Query::rowcount [private]

Current column pointer in result.

Definition at line 160 of file Query.h.

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

std::string Query::m_tmpstr [private]

Used to store result in get_string() call.

Definition at line 161 of file Query.h.

Referenced by get_string().

std::string Query::m_last_query [private]

Last query executed.

Definition at line 162 of file Query.h.

Referenced by execute(), get_result(), and GetLastQuery().

int Query::cache_rc [private]

Cached result after call to get_result().

Definition at line 163 of file Query.h.

Referenced by fetch_row(), and get_result().

bool Query::cache_rc_valid [private]

Indicates cache_rc is valid.

Definition at line 164 of file Query.h.

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

int Query::m_row_count [private]

0 if get_result() returned no rows

Definition at line 165 of file Query.h.

Referenced by get_result(), and num_rows().

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

map translating column names to index

Definition at line 167 of file Query.h.

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

int Query::m_num_cols [private]

number of columns in result

Definition at line 168 of file Query.h.

Referenced by 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