|
15 | 15 | #include "stars/ChuckNorris.hpp" |
16 | 16 |
|
17 | 17 | #include <spdlog/spdlog.h> |
18 | | -#include <sqlite3.h> |
19 | 18 |
|
20 | | -#include "Database.hpp" |
| 19 | +#include "ChuckNorrisImpl.hpp" |
21 | 20 |
|
22 | | -stars::ChuckNorris::ChuckNorris() { |
23 | | - int flags = SQLITE_OPEN_URI | SQLITE_OPEN_READONLY; |
24 | | - if (sqlite3_open_v2(STARS_DB_URI, &db, flags, nullptr) != SQLITE_OK) { |
25 | | - spdlog::critical("Can't open database: {}", sqlite3_errmsg(db)); |
26 | | - dbStatus = -1; |
27 | | - sqlite3_close(db); |
28 | | - } else { |
29 | | - dbStatus = 1; |
| 21 | +stars::ChuckNorris::ChuckNorris() : pimpl(new stars::ChuckNorrisImpl) { |
| 22 | + if (getStatus() == -1) { |
| 23 | + spdlog::error("Can't open database: {}", pimpl->getDbMessage()); |
30 | 24 | } |
31 | 25 | } |
32 | 26 |
|
33 | | -stars::ChuckNorris::~ChuckNorris() { |
34 | | - if (dbStatus == 1) { |
35 | | - spdlog::debug("Closing a database connection..."); |
36 | | - sqlite3_close(db); |
37 | | - } |
38 | | -} |
39 | | -int stars::ChuckNorris::getStatus() const { return dbStatus; } |
| 27 | +int stars::ChuckNorris::getStatus() const { return pimpl->getDbStatus(); } |
40 | 28 |
|
41 | 29 | std::string stars::ChuckNorris::getFact() { |
42 | | - if (dbStatus != 1) { |
43 | | - return ""; |
44 | | - } |
45 | | - |
46 | | - sqlite3_stmt* stmt; |
47 | | - int rc; |
48 | | - std::string res; |
49 | | - auto const q = R"(SELECT fact FROM chucknorris ORDER BY RANDOM() LIMIT 1;)"; |
50 | | - |
51 | | - rc = sqlite3_prepare_v2(db, q, -1, &stmt, nullptr); |
52 | | - if (rc != SQLITE_OK) { |
53 | | - spdlog::error("Failed to prepare database query: {}", sqlite3_errmsg(db)); |
54 | | - dbStatus = -2; |
55 | | - |
56 | | - sqlite3_finalize(stmt); |
57 | | - return ""; |
58 | | - } |
59 | | - |
60 | | - rc = sqlite3_step(stmt); |
61 | | - if (rc != SQLITE_ROW) { |
62 | | - if (rc != SQLITE_DONE) { |
63 | | - spdlog::error("Failed to select fact: {}", sqlite3_errmsg(db)); |
64 | | - dbStatus = -2; |
65 | | - } else { |
66 | | - spdlog::info("Unable to select fact: database is empty"); |
67 | | - dbStatus = -1; |
| 30 | + auto fact = pimpl->getFact(); |
| 31 | + auto stat = getStatus(); |
| 32 | + |
| 33 | + if (stat < 0) { |
| 34 | + auto message = pimpl->getDbMessage(); |
| 35 | + switch (getStatus()) { |
| 36 | + case -2: |
| 37 | + spdlog::error("Failed to prepare database query: {}", message); |
| 38 | + break; |
| 39 | + case -3: |
| 40 | + spdlog::error("Failed to select fact: {}", message); |
| 41 | + break; |
| 42 | + case -4: |
| 43 | + spdlog::error("Unable to select fact: database is empty"); |
| 44 | + break; |
| 45 | + default: |
| 46 | + spdlog::error("Something went wrong"); |
68 | 47 | } |
69 | | - |
70 | | - sqlite3_finalize(stmt); |
71 | | - return ""; |
72 | 48 | } |
73 | 49 |
|
74 | | - auto sqlite_row = sqlite3_column_text(stmt, 0); |
75 | | - auto row = reinterpret_cast<const char*>(sqlite_row); |
76 | | - res = std::string(row); |
77 | | - |
78 | | - sqlite3_finalize(stmt); |
79 | | - return res; |
| 50 | + return fact; |
80 | 51 | } |
0 commit comments