Query Class ReferenceSQL Statement execute / result. More...
Collaboration diagram for Query:
Detailed DescriptionSQL Statement execute / result.
Definition at line 55 of file Query.h. Constructor & Destructor Documentation
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 }
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 }
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 }
Member Function Documentation
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 }
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 }
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 }
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 }
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 }
Fetch next result row.
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 }
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 }
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 }
Last error string.
Definition at line 489 of file Query.cpp. References odb. Referenced by SysLog::error(), and StderrLog::error().
Last error code.
Definition at line 497 of file Query.cpp. References odb. Referenced by SysLog::error(), and StderrLog::error().
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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
Reference to database object.
Definition at line 156 of file Query.h. Referenced by GetDatabase(), and ~Query().
Connection pool handle.
Definition at line 157 of file Query.h. Referenced by Connected(), execute(), fetch_row(), free_result(), get_result(), getbigint(), GetErrno(), GetError(), getnum(), getstr(), getubigint(), getuval(), getval(), insert_id(), is_null(), num_rows(), and ~Query().
Stored result.
Definition at line 158 of file Query.h. Referenced by execute(), fetch_row(), free_result(), get_result(), getbigint(), getnum(), getstr(), getubigint(), getuval(), getval(), is_null(), num_rows(), ViewRes(), and ~Query().
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().
Current column pointer in result.
Definition at line 160 of file Query.h. Referenced by fetch_row(), getbigint(), getnum(), getstr(), getubigint(), getuval(), and getval().
Used to store result in get_string() call.
Definition at line 161 of file Query.h. Referenced by get_string().
Last query executed.
Definition at line 162 of file Query.h. Referenced by execute(), get_result(), and GetLastQuery().
Cached result after call to get_result().
Definition at line 163 of file Query.h. Referenced by fetch_row(), and get_result().
Indicates cache_rc is valid.
Definition at line 164 of file Query.h. Referenced by fetch_row(), free_result(), and get_result().
0 if get_result() returned no rows
Definition at line 165 of file Query.h. Referenced by get_result(), and num_rows().
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().
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: |