summaryrefslogtreecommitdiffhomepage
path: root/compiledsql.cpp
blob: b8d6d70296162f5ca3376b99c548811efd2a958b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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();
}