Logo
~Sockets~
~Examples~
~Contact~


Query Class Reference

#include <Query.h>

Collaboration diagram for Query:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Query ()
 Query (Database *dbin)
 Query (Database *dbin, const char *sql)
virtual ~Query ()
short execute (const char *sql)
short execute (const std::string &)
bool get_result (const char *sql, char **attrs=NULL)
bool get_result (const std::string &)
void free_result ()
bool fetch_row ()
long insert_id ()
long num_rows ()
char * get_string (const char *sql)
double get_num (const char *sql)
long get_count (const char *sql)
char * get_string (const std::string &)
double get_num (const std::string &)
long get_count (const std::string &)
char * getstr (int x)
double getnum (int x)
long getval (int x)
char * getstr ()
double getnum ()
long getval ()
void debug (short val)
void syslog (short val)
char * fetch_fieldname ()
bool GotMore ()
char * allocsafestr (const char *s)
void safestr (const char *src, char *dest)
char * gettmpstr (int)
void safestr (std::string &)
std::string XMLSafe (const std::string &)
void ReturnRowAsXML (std::string &, const std::string &="", long=DQ_XML_ENDTAG)
int num_cols ()
bool getcol (std::string &colname, std::string &coltype, int &width, int &dec, int &nullable)
bool getcol (int x, std::string &colname, std::string &coltype, int &width, int &dec, int &nullable)

Private Member Functions

void unsafestr (char *s)

Private Attributes

Databasedb
Sessionodb
char ** res
char * row [256]
TMPSTRm_tmpstrbase
short rowcount
short _debug
short _syslog
int m_fetchrows
int m_results
int m_ncolumns

Detailed Description

Definition at line 42 of file Query.h.


Constructor & Destructor Documentation

Query::Query (  ) 

Definition at line 46 of file Query.cpp.

00047 :db(NULL)
00048 ,odb(NULL)
00049 ,res(NULL)
00050 ,m_tmpstrbase(NULL)
00051 ,rowcount(0)
00052 ,_debug(0)
00053 ,_syslog(0)
00054 ,m_fetchrows(0)
00055 ,m_results(0)
00056 ,m_ncolumns(0)
00057 {
00058 }

Query::Query ( Database dbin  ) 

Definition at line 60 of file Query.cpp.

00061 :db(dbin)
00062 ,odb(dbin -> grabdb())
00063 ,res(NULL)
00064 ,m_tmpstrbase(NULL)
00065 ,rowcount(0)
00066 ,_debug(dbin -> debug())
00067 ,_syslog(0)
00068 ,m_fetchrows(0)
00069 ,m_results(0)
00070 ,m_ncolumns(0)
00071 {
00072 }

Query::Query ( Database dbin,
const char *  sql 
)

Definition at line 74 of file Query.cpp.

References execute().

00075 :db(dbin)
00076 ,odb(dbin -> grabdb())
00077 ,res(NULL)
00078 ,m_tmpstrbase(NULL)
00079 ,rowcount(0)
00080 ,_debug(dbin -> debug())
00081 ,_syslog(0)
00082 ,m_fetchrows(0)
00083 ,m_results(0)
00084 ,m_ncolumns(0)
00085 {
00086         if (!execute(sql))
00087                 fprintf(stderr,"query failed: '%s'\n",sql);
00088 }

Query::~Query (  )  [virtual]

Definition at line 92 of file Query.cpp.

References tmpstrstruct::buffer, db, m_tmpstrbase, odb, and res.

00093 {
00094         TMPSTR *tmpstr;
00095 
00096         while (m_tmpstrbase)
00097         {
00098                 tmpstr = m_tmpstrbase -> next;
00099                 delete m_tmpstrbase->buffer;
00100                 delete m_tmpstrbase;
00101                 m_tmpstrbase = tmpstr;
00102         }
00103 //printf("~Query()\n");
00104         if (res)
00105         {
00106                 SQLITE::sqlite_free_table(res);
00107                 res = NULL;
00108         }
00109         if (odb)
00110                 db -> freedb(odb);
00111 }


Member Function Documentation

short Query::execute ( const char *  sql  ) 

Definition at line 120 of file Query.cpp.

References db, free_result(), m_fetchrows, m_ncolumns, m_results, odb, and res.

Referenced by execute(), NukeHandler::InitDB(), Query(), db::Sender::save(), and db::Mailinst::save().

00121 {               // query, no result
00122         if (db && odb && res)
00123                 fprintf(stderr,"query busy\n");
00124         if (db && odb && !res)
00125         {
00126                 SQLITE::sqlite *db = static_cast<SQLITE::sqlite *>(odb -> GetHandle());
00127                 char *errmsg;
00128                 m_results = 0;
00129                 m_fetchrows = 1;
00130                 m_ncolumns = 0;
00131                 if (SQLITE::sqlite_get_table(db,sql,&res,&m_results,&m_ncolumns,&errmsg) != SQLITE_OK)
00132                 {
00133                         fprintf(stderr,"query failed: '%s'  because: '%s'\n",sql,errmsg);
00134                 }
00135                 else
00136                 {
00137                         free_result();
00138                         return 1;
00139                 }
00140         }
00141         return 0;
00142 }

short Query::execute ( const std::string &   ) 

Definition at line 114 of file Query.cpp.

References execute().

00115 {
00116         return execute(sql.c_str());
00117 }

bool Query::get_result ( const char *  sql,
char **  attrs = NULL 
)

Definition at line 152 of file Query.cpp.

References db, m_fetchrows, m_ncolumns, m_results, odb, and res.

Referenced by get_count(), get_num(), get_result(), get_string(), db::Sender::Spawn(), and db::Mailinst::Spawn().

00153 {       // query, result
00154         if (attrs); // UNUSED_ALWAYS
00155         if (db && odb && res)
00156                 fprintf(stderr,"query busy\n");
00157         if (db && odb && !res)
00158         {
00159                 SQLITE::sqlite *db = static_cast<SQLITE::sqlite *>(odb -> GetHandle());
00160                 char *errmsg;
00161                 m_results = 0;
00162                 m_fetchrows = 1;
00163                 m_ncolumns = 0;
00164                 if (SQLITE::sqlite_get_table(db,sql,&res,&m_results,&m_ncolumns,&errmsg) != SQLITE_OK)
00165                 {
00166                         fprintf(stderr,"query failed: '%s'  because: '%s'\n",sql,errmsg);
00167                 }
00168                 else
00169                 {
00170                         return 1;
00171                 }
00172         }
00173         return false;
00174 }

bool Query::get_result ( const std::string &   ) 

Definition at line 146 of file Query.cpp.

References get_result().

00147 {
00148         return get_result(sql.c_str());
00149 }

void Query::free_result (  ) 

Definition at line 176 of file Query.cpp.

References db, odb, and res.

Referenced by execute(), get_count(), get_num(), get_string(), db::Sender::Spawn(), and db::Mailinst::Spawn().

00177 {
00178         if (db && odb && res)
00179         {
00180                 SQLITE::sqlite_free_table(res);
00181                 res = NULL;
00182         }
00183 }

bool Query::fetch_row (  ) 

Definition at line 185 of file Query.cpp.

References db, m_fetchrows, m_ncolumns, m_results, odb, res, row, and rowcount.

Referenced by get_count(), get_num(), get_string(), db::Sender::Spawn(), and db::Mailinst::Spawn().

00186 {
00187         rowcount = 0;
00188         if (db && odb && res)
00189         {
00190                 if (m_fetchrows <= m_results)
00191                 {
00192                         for (int i = 0; i < m_ncolumns; i++)
00193                                 row[i] = res[m_fetchrows * m_ncolumns + i];
00194                         m_fetchrows++;
00195                         return true;
00196                 }
00197         }
00198         return false;
00199 }

long Query::insert_id (  ) 

Definition at line 201 of file Query.cpp.

References db, and odb.

Referenced by db::Sender::save(), and db::Mailinst::save().

00202 {
00203         if (db && odb)
00204         {
00205                 SQLITE::sqlite *db = static_cast<SQLITE::sqlite *>(odb -> GetHandle());
00206                 return SQLITE::sqlite_last_insert_rowid(db);
00207         }
00208         else
00209         {
00210                 return -1;
00211         }
00212 }

long Query::num_rows (  ) 

Definition at line 214 of file Query.cpp.

References db, m_results, odb, and res.

00215 {
00216         return db && odb && res ? m_results : 0;
00217 }

char * Query::get_string ( const char *  sql  ) 

Definition at line 293 of file Query.cpp.

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

Referenced by get_string().

00294 {
00295 static  char slask[999];
00296         *slask = 0;
00297         if (Query::get_result(sql))
00298         {
00299                 if (Query::fetch_row())
00300                         strcpy(slask,Query::getstr());
00301                 Query::free_result();
00302         }
00303         return slask;
00304 }

double Query::get_num ( const char *  sql  ) 

Definition at line 259 of file Query.cpp.

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

Referenced by get_num().

00260 {
00261         double l = 0;
00262         if (Query::get_result(sql))
00263         {
00264                 if (Query::fetch_row())
00265                         l = Query::getnum();
00266                 Query::free_result();
00267         }
00268         return l;
00269 }

long Query::get_count ( const char *  sql  ) 

Definition at line 276 of file Query.cpp.

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

Referenced by get_count().

00277 {
00278         long l = 0;
00279         if (Query::get_result(sql))
00280         {
00281                 if (Query::fetch_row())
00282                         l = Query::getval();
00283                 Query::free_result();
00284         }
00285         return l;
00286 }

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

Definition at line 288 of file Query.cpp.

References get_string().

00289 {
00290         return get_string(sql.c_str());
00291 }

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

Definition at line 254 of file Query.cpp.

References get_num().

00255 {
00256         return get_num(sql.c_str());
00257 }

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

Definition at line 271 of file Query.cpp.

References get_count().

00272 {
00273         return get_count(sql.c_str());
00274 }

char * Query::getstr ( int  x  ) 

Definition at line 221 of file Query.cpp.

References db, odb, res, and row.

Referenced by db::Sender::Spawn(), and db::Mailinst::Spawn().

00222 {
00223         if (db && odb && res && row)
00224                 return row[x] ? row[x] : (char *)"";
00225         else
00226                 return NULL;
00227 }

double Query::getnum ( int  x  ) 

Definition at line 234 of file Query.cpp.

References db, odb, res, and row.

00235 {
00236         return db && odb && res && row && row[x] ? atof(row[x]) : 0;
00237 }

long Query::getval ( int  x  ) 

Definition at line 239 of file Query.cpp.

References db, odb, res, and row.

Referenced by db::Sender::Spawn(), and db::Mailinst::Spawn().

00240 {
00241         return db && odb && res && row && row[x] ? atol(row[x]) : 0;
00242 }

char * Query::getstr (  ) 

Definition at line 229 of file Query.cpp.

References rowcount.

Referenced by get_string().

00230 {
00231         return Query::getstr(rowcount++);
00232 }

double Query::getnum (  ) 

Definition at line 244 of file Query.cpp.

References rowcount.

Referenced by get_num().

00245 {
00246         return getnum(rowcount++);
00247 }

long Query::getval (  ) 

Definition at line 249 of file Query.cpp.

References rowcount.

Referenced by get_count().

00250 {
00251         return getval(rowcount++);
00252 }

void Query::debug ( short  val  ) 

Definition at line 306 of file Query.cpp.

References _debug.

00307 { 
00308         _debug = val; 
00309 }

void Query::syslog ( short  val  ) 

Definition at line 311 of file Query.cpp.

References _syslog.

00312 { 
00313         _syslog = val; 
00314 }

char * Query::fetch_fieldname (  ) 

Definition at line 324 of file Query.cpp.

References res, and rowcount.

00325 {
00326         if (res)
00327                 return res[rowcount]; // rowcount is colcount actually
00328         return NULL;
00329 }

bool Query::GotMore (  ) 

Definition at line 366 of file Query.cpp.

References m_fetchrows, and m_results.

00367 {
00368   return (m_fetchrows < m_results) ? true : false;
00369 }

char * Query::allocsafestr ( const char *  s  ) 

Definition at line 383 of file Query.cpp.

References safestr().

00384 {
00385   char                  *str = new char[strlen(s) * 2 + 4];
00386 
00387   safestr(s, str);
00388   return str;
00389 } // allocsafestr

void Query::safestr ( const char *  src,
char *  dest 
)

Definition at line 403 of file Query.cpp.

Referenced by allocsafestr().

00404 {
00405   int register          i;
00406   int register          j = 0;
00407 
00408   *s2 = 0;
00409   for (i = 0; i < (int)strlen(s); i++)
00410   {
00411     switch (s[i])
00412     {
00413     case '\'':
00414     case '\\':
00415     case 34:
00416       s2[j++] = '\\';
00417     default:
00418       s2[j++] = s[i];
00419     }
00420   }
00421   s2[j] = 0;
00422 } // safestr

char * Query::gettmpstr ( int   ) 

Definition at line 476 of file Query.cpp.

References tmpstrstruct::buffer, m_tmpstrbase, tmpstrstruct::next, and tmpstrstruct::temporary.

00477 {
00478   TMPSTR                *tmp = new TMPSTR;
00479 
00480   tmp->next = m_tmpstrbase;
00481   tmp->buffer = new char[l + 1];
00482   tmp->temporary = 0;
00483   m_tmpstrbase = tmp;
00484   return tmp->buffer;
00485 } // gettmpstr

void Query::safestr ( std::string &   ) 

Definition at line 425 of file Query.cpp.

00426 {
00427         std::string s2;
00428         for (size_t i = 0; i < str.size(); i++)
00429         {
00430                 switch (str[i])
00431                 {
00432                 case '\'':
00433                 case '\\':
00434                 case 34:
00435                         s2 += '\\';
00436                 default:
00437                         s2 += str[i];
00438                 }
00439         }
00440         str = s2;
00441 }

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

void Query::ReturnRowAsXML ( std::string &  ,
const std::string &  = "",
long  = DQ_XML_ENDTAG 
) [inline]

Definition at line 100 of file Query.h.

00100 {}

int Query::num_cols (  )  [inline]

Definition at line 102 of file Query.h.

References m_ncolumns.

00102 { return m_ncolumns; }

bool Query::getcol ( std::string &  colname,
std::string &  coltype,
int &  width,
int &  dec,
int &  nullable 
)

Definition at line 607 of file Query.cpp.

References res, and rowcount.

00608 {
00609         if (res)
00610         {
00611                 colname = res[rowcount];
00612                 return true;
00613         }
00614         return false;
00615 }

bool Query::getcol ( int  x,
std::string &  colname,
std::string &  coltype,
int &  width,
int &  dec,
int &  nullable 
)

Definition at line 617 of file Query.cpp.

References m_ncolumns, and res.

00618 {
00619         if (res && x >= 0 && x < m_ncolumns)
00620         {
00621                 colname = res[x];
00622                 return true;
00623         }
00624         return false;
00625 }

void Query::unsafestr ( char *  s  )  [private]

Definition at line 455 of file Query.cpp.

00456 {
00457   int                   i;
00458 
00459   for (i = 0; i < (int)strlen(s); i++)
00460     if (s[i] == '\\')
00461       memmove(s + i,s + i + 1,strlen(s + i) + 1);
00462 } // unsafestr


Member Data Documentation

char** Query::res [private]

char* Query::row[256] [private]

Definition at line 114 of file Query.h.

Referenced by fetch_row(), getnum(), getstr(), and getval().

Definition at line 116 of file Query.h.

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

short Query::rowcount [private]

Definition at line 117 of file Query.h.

Referenced by fetch_fieldname(), fetch_row(), getcol(), getnum(), getstr(), and getval().

short Query::_debug [private]

Definition at line 118 of file Query.h.

Referenced by debug().

short Query::_syslog [private]

Definition at line 119 of file Query.h.

Referenced by syslog().

int Query::m_fetchrows [private]

Definition at line 120 of file Query.h.

Referenced by execute(), fetch_row(), get_result(), and GotMore().

int Query::m_results [private]

Definition at line 121 of file Query.h.

Referenced by execute(), fetch_row(), get_result(), GotMore(), and num_rows().

int Query::m_ncolumns [private]

Definition at line 122 of file Query.h.

Referenced by execute(), fetch_row(), get_result(), getcol(), 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
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4