| 113 | | const char *s = NULL; |
| 114 | | int rc = sqlite3_prepare(odb -> db, sql.c_str(), sql.size(), &res, &s); |
| 115 | | if (rc != SQLITE_OK) |
| 116 | | { |
| 117 | | GetDatabase().error(*this, "execute: prepare query failed"); |
| 118 | | return false; |
| 119 | | } |
| 120 | | if (!res) |
| 121 | | { |
| 122 | | GetDatabase().error(*this, "execute: query failed"); |
| 123 | | return false; |
| 124 | | } |
| 125 | | rc = sqlite3_step(res); // execute |
| 126 | | sqlite3_finalize(res); // deallocate statement |
| | 117 | if (mysql_query(&odb -> mysql,sql.c_str())) |
| | 118 | { |
| | 119 | GetDatabase().error(*this,"query failed"); |
| | 120 | } |
| | 121 | else |
| | 122 | { |
| | 123 | return true; |
| | 124 | } |
| | 125 | } |
| | 126 | return false; |
| | 127 | } |
| | 128 | |
| | 129 | |
| | 130 | |
| | 131 | // methods using db specific api calls |
| | 132 | |
| | 133 | MYSQL_RES *Query::get_result(const std::string& sql) |
| | 134 | { // query, result |
| | 135 | if (odb && res) |
| | 136 | { |
| | 137 | GetDatabase().error(*this, "get_result: query busy"); |
| | 138 | } |
| | 139 | if (odb && !res) |
| | 140 | { |
| | 141 | if (execute(sql)) |
| | 142 | { |
| | 143 | res = mysql_store_result(&odb -> mysql); |
| | 144 | if (res) |
| | 145 | { |
| | 146 | MYSQL_FIELD *f = mysql_fetch_field(res); |
| | 147 | int i = 1; |
| | 148 | while (f) |
| | 149 | { |
| | 150 | if (f -> name) |
| | 151 | m_nmap[f -> name] = i; |
| | 152 | f = mysql_fetch_field(res); |
| | 153 | i++; |
| | 154 | } |
| | 155 | m_num_cols = i - 1; |
| | 156 | } |
| | 157 | } |
| | 158 | } |
| | 159 | return res; |
| | 160 | } |
| | 161 | |
| | 162 | |
| | 163 | void Query::free_result() |
| | 164 | { |
| | 165 | if (odb && res) |
| | 166 | { |
| | 167 | mysql_free_result(res); |
| 128 | | switch (rc) |
| 129 | | { |
| 130 | | case SQLITE_BUSY: |
| 131 | | GetDatabase().error(*this, "execute: database busy"); |
| 132 | | return false; |
| 133 | | case SQLITE_DONE: |
| 134 | | case SQLITE_ROW: |
| 135 | | return true; |
| 136 | | case SQLITE_ERROR: |
| 137 | | GetDatabase().error(*this, sqlite3_errmsg(odb -> db)); |
| 138 | | return false; |
| 139 | | case SQLITE_MISUSE: |
| 140 | | GetDatabase().error(*this, "execute: database misuse"); |
| 141 | | return false; |
| 142 | | } |
| 143 | | GetDatabase().error(*this, "execute: unknown result code"); |
| 144 | | } |
| 145 | | return false; |
| 146 | | } |
| 147 | | |
| 148 | | |
| 149 | | |
| 150 | | // methods using db specific api calls |
| 151 | | |
| 152 | | sqlite3_stmt *Query::get_result(const std::string& sql) |
| 153 | | { |
| 154 | | // query, result |
| 155 | | m_last_query = sql; |
| 156 | | if (odb && res) |
| 157 | | { |
| 158 | | GetDatabase().error(*this, "get_result: query busy"); |
| 159 | | } |
| 160 | | if (odb && !res) |
| 161 | | { |
| 162 | | const char *s = NULL; |
| 163 | | int rc = sqlite3_prepare(odb -> db, sql.c_str(), sql.size(), &res, &s); |
| 164 | | if (rc != SQLITE_OK) |
| 165 | | { |
| 166 | | GetDatabase().error(*this, "get_result: prepare query failed"); |
| 167 | | return NULL; |
| 168 | | } |
| 169 | | if (!res) |
| 170 | | { |
| 171 | | GetDatabase().error(*this, "get_result: query failed"); |
| 172 | | return NULL; |
| 173 | | } |
| 174 | | // get column names from result |
| 175 | | { |
| 176 | | int i = 0; |
| 177 | | do |
| 178 | | { |
| 179 | | const char *p = sqlite3_column_name(res, i); |
| 180 | | if (!p) |
| 181 | | break; |
| 182 | | m_nmap[p] = ++i; |
| 183 | | } while (true); |
| 184 | | m_num_cols = i; |
| 185 | | } |
| 186 | | cache_rc = sqlite3_step(res); |
| 187 | | cache_rc_valid = true; |
| 188 | | m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0; |
| 189 | | } |
| 190 | | return res; |
| 191 | | } |
| 192 | | |
| 193 | | |
| 194 | | void Query::free_result() |
| 195 | | { |
| 196 | | if (odb && res) |
| 197 | | { |
| 198 | | sqlite3_finalize(res); |
| 199 | | res = NULL; |
| 200 | | row = false; |
| 201 | | cache_rc_valid = false; |
| 202 | | } |
| 203 | | // clear column names |
| | 169 | row = NULL; |
| | 170 | } |
| 215 | | row = false; |
| 216 | | if (odb && res) |
| 217 | | { |
| 218 | | int rc = cache_rc_valid ? cache_rc : sqlite3_step(res); // execute |
| 219 | | cache_rc_valid = false; |
| 220 | | switch (rc) |
| 221 | | { |
| 222 | | case SQLITE_BUSY: |
| 223 | | GetDatabase().error(*this, "execute: database busy"); |
| 224 | | return false; |
| 225 | | case SQLITE_DONE: |
| 226 | | return false; |
| 227 | | case SQLITE_ROW: |
| 228 | | row = true; |
| 229 | | return true; |
| 230 | | case SQLITE_ERROR: |
| 231 | | GetDatabase().error(*this, sqlite3_errmsg(odb -> db)); |
| 232 | | return false; |
| 233 | | case SQLITE_MISUSE: |
| 234 | | GetDatabase().error(*this, "execute: database misuse"); |
| 235 | | return false; |
| 236 | | } |
| 237 | | GetDatabase().error(*this, "execute: unknown result code"); |
| 238 | | } |
| | 183 | return odb && res ? row = mysql_fetch_row(res) : NULL; |
| | 184 | } |
| | 185 | |
| | 186 | |
| | 187 | my_ulonglong Query::insert_id() |
| | 188 | { |
| | 189 | if (odb) |
| | 190 | { |
| | 191 | return mysql_insert_id(&odb -> mysql); |
| | 192 | } |
| | 193 | else |
| | 194 | { |
| | 195 | return 0; |
| | 196 | } |
| | 197 | } |
| | 198 | |
| | 199 | |
| | 200 | long Query::num_rows() |
| | 201 | { |
| | 202 | return odb && res ? mysql_num_rows(res) : 0; |
| | 203 | } |
| | 204 | |
| | 205 | |
| | 206 | int Query::num_cols() |
| | 207 | { |
| | 208 | return m_num_cols; |
| | 209 | } |
| | 210 | |
| | 211 | |
| | 212 | bool Query::is_null(int x) |
| | 213 | { |
| | 214 | if (odb && res && row) |
| | 215 | { |
| | 216 | return row[x] ? false : true; |
| | 217 | } |
| | 218 | return false; // ... |
| | 219 | } |
| | 220 | |
| | 221 | |
| | 222 | bool Query::is_null(const std::string& x) |
| | 223 | { |
| | 224 | int index = m_nmap[x] - 1; |
| | 225 | if (index >= 0) |
| | 226 | return is_null(index); |
| | 227 | error("Column name lookup failure: " + x); |
| 243 | | sqlite_int64 Query::insert_id() |
| 244 | | { |
| 245 | | if (odb) |
| 246 | | { |
| 247 | | return sqlite3_last_insert_rowid(odb -> db); |
| 248 | | } |
| 249 | | else |
| 250 | | { |
| 251 | | return 0; |
| 252 | | } |
| 253 | | } |
| 254 | | |
| 255 | | |
| 256 | | long Query::num_rows() |
| 257 | | { |
| 258 | | return odb && res ? m_row_count : 0; |
| 259 | | } |
| 260 | | |
| 261 | | |
| 262 | | int Query::num_cols() |
| 263 | | { |
| 264 | | return m_num_cols; |
| 265 | | } |
| 266 | | |
| 267 | | |
| 268 | | bool Query::is_null(int x) |
| | 232 | bool Query::is_null() |
| | 233 | { |
| | 234 | return is_null(rowcount++); |
| | 235 | } |
| | 236 | |
| | 237 | |
| | 238 | const char *Query::getstr(const std::string& x) |
| | 239 | { |
| | 240 | int index = m_nmap[x] - 1; |
| | 241 | if (index >= 0) |
| | 242 | return getstr(index); |
| | 243 | error("Column name lookup failure: " + x); |
| | 244 | return NULL; |
| | 245 | } |
| | 246 | |
| | 247 | |
| | 248 | const char *Query::getstr(int x) |
| 272 | | if (sqlite3_column_type(res, x) == SQLITE_NULL) |
| 273 | | return true; |
| 274 | | } |
| 275 | | return false; // ... |
| 276 | | } |
| 277 | | |
| 278 | | |
| 279 | | const char *Query::getstr(const std::string& x) |
| 280 | | { |
| 281 | | int index = m_nmap[x] - 1; |
| 282 | | if (index >= 0) |
| 283 | | return getstr(index); |
| 284 | | error("Column name lookup failure: " + x); |
| 285 | | return ""; |
| 286 | | } |
| 287 | | |
| 288 | | |
| 289 | | const char *Query::getstr(int x) |
| 290 | | { |
| 291 | | if (odb && res && row && x < sqlite3_column_count(res) ) |
| 292 | | { |
| 293 | | const unsigned char *tmp = sqlite3_column_text(res, x); |
| 294 | | return tmp ? (const char *)tmp : ""; |
| 295 | | } |
| 296 | | return ""; |
| | 252 | return row[x] ? row[x] : ""; |
| | 253 | } |
| | 254 | return NULL; |
| 336 | | long Query::getval(int x) |
| 337 | | { |
| 338 | | if (odb && res && row) |
| 339 | | { |
| 340 | | return sqlite3_column_int(res, x); |
| 341 | | } |
| | 318 | unsigned long Query::getuval(int x) |
| | 319 | { |
| | 320 | unsigned long l = 0; |
| | 321 | if (odb && res && row && row[x]) |
| | 322 | { |
| | 323 | l = m_db.a2ubigint(row[x]); |
| | 324 | } |
| | 325 | return l; |
| | 326 | } |
| | 327 | |
| | 328 | |
| | 329 | unsigned long Query::getuval() |
| | 330 | { |
| | 331 | return getuval(rowcount++); |
| | 332 | } |
| | 333 | |
| | 334 | |
| | 335 | int64_t Query::getbigint(const std::string& x) |
| | 336 | { |
| | 337 | int index = m_nmap[x] - 1; |
| | 338 | if (index >= 0) |
| | 339 | return getbigint(index); |
| | 340 | error("Column name lookup failure: " + x); |
| 346 | | double Query::getnum() |
| 347 | | { |
| 348 | | return getnum(rowcount++); |
| 349 | | } |
| 350 | | |
| 351 | | |
| 352 | | long Query::getval() |
| 353 | | { |
| 354 | | return getval(rowcount++); |
| 355 | | } |
| 356 | | |
| 357 | | |
| 358 | | unsigned long Query::getuval(const std::string& x) |
| 359 | | { |
| 360 | | int index = m_nmap[x] - 1; |
| 361 | | if (index >= 0) |
| 362 | | return getuval(index); |
| | 345 | int64_t Query::getbigint(int x) |
| | 346 | { |
| | 347 | return odb && res && row && row[x] ? m_db.a2bigint(row[x]) : 0; |
| | 348 | } |
| | 349 | |
| | 350 | |
| | 351 | int64_t Query::getbigint() |
| | 352 | { |
| | 353 | return getbigint(rowcount++); |
| | 354 | } |
| | 355 | |
| | 356 | |
| | 357 | uint64_t Query::getubigint(const std::string& x) |
| | 358 | { |
| | 359 | int index = m_nmap[x] - 1; |
| | 360 | if (index >= 0) |
| | 361 | return getubigint(index); |
| 368 | | unsigned long Query::getuval(int x) |
| 369 | | { |
| 370 | | unsigned long l = 0; |
| 371 | | if (odb && res && row) |
| 372 | | { |
| 373 | | l = sqlite3_column_int(res, x); |
| 374 | | } |
| 375 | | return l; |
| 376 | | } |
| 377 | | |
| 378 | | |
| 379 | | unsigned long Query::getuval() |
| 380 | | { |
| 381 | | return getuval(rowcount++); |
| 382 | | } |
| 383 | | |
| 384 | | |
| 385 | | int64_t Query::getbigint(const std::string& x) |
| 386 | | { |
| 387 | | int index = m_nmap[x] - 1; |
| 388 | | if (index >= 0) |
| 389 | | return getbigint(index); |
| 390 | | error("Column name lookup failure: " + x); |
| 391 | | return 0; |
| 392 | | } |
| 393 | | |
| 394 | | |
| 395 | | int64_t Query::getbigint(int x) |
| 396 | | { |
| 397 | | if (odb && res && row) |
| 398 | | { |
| 399 | | return sqlite3_column_int64(res, x); |
| 400 | | } |
| 401 | | return 0; |
| 402 | | } |
| 403 | | |
| 404 | | |
| 405 | | int64_t Query::getbigint() |
| 406 | | { |
| 407 | | return getbigint(rowcount++); |
| 408 | | } |
| 409 | | |
| 410 | | |
| 411 | | uint64_t Query::getubigint(const std::string& x) |
| 412 | | { |
| 413 | | int index = m_nmap[x] - 1; |
| 414 | | if (index >= 0) |
| 415 | | return getubigint(index); |
| 416 | | error("Column name lookup failure: " + x); |
| 417 | | return 0; |
| 418 | | } |
| 419 | | |
| 420 | | |
| 511 | | |
| 512 | | |
| 513 | | void Query::ViewRes() |
| 514 | | { |
| 515 | | if (!res) |
| 516 | | { |
| 517 | | printf("no result stored\n"); |
| 518 | | return; |
| 519 | | } |
| 520 | | printf("result column count = %d\n", sqlite3_column_count(res)); |
| 521 | | for (int i = 0; i < sqlite3_column_count(res); i++) |
| 522 | | { |
| 523 | | printf(" %2d type %d name '%s'", i, sqlite3_column_type(res, i), sqlite3_column_name(res, i)); |
| 524 | | printf(" / '%s'", (char *)sqlite3_column_text(res, i)); |
| 525 | | printf(" / %d", sqlite3_column_int(res, i)); |
| 526 | | printf(" / %f", sqlite3_column_double(res, i)); |
| 527 | | printf("\n"); |
| 528 | | } |
| 529 | | } |
| 530 | | |
| 531 | | |
| 532 | | void Query::error(const std::string& msg) |
| 533 | | { |
| 534 | | GetDatabase().error(*this, msg); |
| 535 | | } |
| 536 | | |
| 537 | | |
| 538 | | #ifdef SQLITEW_NAMESPACE |
| 539 | | } // namespace SQLITEW_NAMESPACE { |
| | 456 | void Query::error(const std::string& x) |
| | 457 | { |
| | 458 | m_db.error(*this, x.c_str()); |
| | 459 | } |
| | 460 | |
| | 461 | |
| | 462 | std::string Query::safestr(const std::string& x) |
| | 463 | { |
| | 464 | return m_db.safestr(x); |
| | 465 | } |
| | 466 | |
| | 467 | // Added Michael Griffin 11/15/09 |
| | 468 | // Custom 'mysql_real_escape_string()' |
| | 469 | char *Query::strip(char* Oldstr) |
| | 470 | { |
| | 471 | return m_db.strip_escape(odb,Oldstr); |
| | 472 | } |
| | 473 | |
| | 474 | |
| | 475 | #ifdef MYSQLW_NAMESPACE |
| | 476 | } // namespace MYSQLW_NAMESPACE { |