summaryrefslogtreecommitdiffhomepage
path: root/compiledsql.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'compiledsql.cpp')
-rw-r--r--compiledsql.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/compiledsql.cpp b/compiledsql.cpp
new file mode 100644
index 0000000..b8d6d70
--- /dev/null
+++ b/compiledsql.cpp
@@ -0,0 +1,46 @@
+#include "compiledsql.h"
+
+#include <iostream>
+
+#include <boost/algorithm/string/predicate.hpp>
+
+CompiledSQL::CompiledSQL(SQLite::Database& db, const std::string& stmt):
+ m_db{db},
+ m_query{stmt},
+ m_stmt{},
+ m_isSelect{}
+{
+ if (
+#if __cplusplus >= 202002
+ stmt.starts_with("SELECT ")
+#else
+ boost::algorithm::starts_with(stmt, "SELECT ")
+#endif
+ ) {
+ m_isSelect = true;
+ } else {
+ m_isSelect = false;
+ }
+}
+
+bool CompiledSQL::execute()
+{
+ if (m_isSelect) {
+ return m_stmt->executeStep();
+ } else {
+ return m_stmt->exec();
+ }
+}
+
+CompiledSQL::Guard::Guard(CompiledSQL& cs): m_cs{cs}
+{
+ if (!m_cs.m_stmt) {
+ m_cs.m_stmt = std::make_shared<SQLite::Statement>(m_cs.m_db, m_cs.m_query);
+ }
+}
+
+CompiledSQL::Guard::~Guard()
+{
+ m_cs.m_stmt->reset();
+}
+