| 378 | }; |
| 379 | |
| 380 | class Statement : NonCopyable { |
| 381 | SQLiteDB& db; |
| 382 | sqlite3_stmt* stmt; |
| 383 | |
| 384 | public: |
| 385 | Statement(SQLiteDB& db, const char* sql) : db(db), stmt(nullptr) { |
| 386 | db.checkError("prepare", sqlite3_prepare_v2(db.db, sql, -1, &stmt, nullptr)); |
| 387 | } |
| 388 | ~Statement() { |
| 389 | try { |
| 390 | db.checkError("finalize", sqlite3_finalize(stmt)); |
| 391 | } catch (...) { |
| 392 | } |
| 393 | } |
| 394 | Statement& reset() { |
| 395 | db.checkError("reset", sqlite3_reset(stmt)); |
| 396 | return *this; |
| 397 | } |
| 398 | Statement& param(int i, StringRef value) { |
| 399 | db.checkError("bind", sqlite3_bind_blob(stmt, i, value.begin(), value.size(), SQLITE_STATIC)); |
| 400 | return *this; |
| 401 | } |
| 402 | Statement& param(int i, int value) { |
| 403 | db.checkError("bind", sqlite3_bind_int(stmt, i, value)); |
| 404 | return *this; |
| 405 | } |
| 406 | Statement& execute() { |
| 407 | int r = sqlite3_step(stmt); |
| 408 | if (r == SQLITE_ROW) |
| 409 | db.checkError("execute called on statement that returns rows", r); |
| 410 | if (r != SQLITE_DONE) |
| 411 | db.checkError("execute", r); |
| 412 | return *this; |
| 413 | } |
| 414 | bool nextRow() { |
| 415 | int r = sqlite3_step(stmt); |
| 416 | if (r == SQLITE_ROW) |
| 417 | return true; |
| 418 | if (r == SQLITE_DONE) |
| 419 | return false; |
| 420 | db.checkError("nextRow", r); |
| 421 | __assume(false); // NOT REACHED |
| 422 | } |
| 423 | StringRef column(int i) { |
| 424 | return StringRef((const uint8_t*)sqlite3_column_blob(stmt, i), sqlite3_column_bytes(stmt, i)); |
| 425 | } |
| 426 | }; |
| 427 | |
| 428 | void hexdump(FILE* fout, StringRef val) { |
| 429 | int buflen = val.size(); |
no test coverage detected