summaryrefslogtreecommitdiff
path: root/src/database/count_query.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/database/count_query.hpp')
-rw-r--r--src/database/count_query.hpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/database/count_query.hpp b/src/database/count_query.hpp
new file mode 100644
index 0000000..b7bbf51
--- /dev/null
+++ b/src/database/count_query.hpp
@@ -0,0 +1,35 @@
+#pragma once
+
+#include <database/query.hpp>
+#include <database/table.hpp>
+
+#include <string>
+
+#include <sqlite3.h>
+
+struct CountQuery: public Query
+{
+ CountQuery(std::string name):
+ Query("SELECT count(*) FROM ")
+ {
+ this->body += std::move(name);
+ }
+
+ int64_t execute(sqlite3* db)
+ {
+ auto statement = this->prepare(db);
+ int64_t res = 0;
+ if (sqlite3_step(statement.get()) == SQLITE_ROW)
+ res = sqlite3_column_int64(statement.get(), 0);
+ else
+ {
+ log_error("Count request didn’t return a result");
+ return 0;
+ }
+ if (sqlite3_step(statement.get()) != SQLITE_DONE)
+ log_warning("Count request returned more than one result.");
+
+ log_debug("Returning count: ", res);
+ return res;
+ }
+};