From c9fa963e71258c5adfb71cf1996cd1bcb33df0bb Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 26 Feb 2023 08:54:17 +0100 Subject: Start with copy of whiteboard --- compiledsql.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 compiledsql.cpp (limited to 'compiledsql.cpp') 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 + +#include + +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(m_cs.m_db, m_cs.m_query); + } +} + +CompiledSQL::Guard::~Guard() +{ + m_cs.m_stmt->reset(); +} + -- cgit v1.2.3