summaryrefslogtreecommitdiffhomepage
path: root/compiledsql.h
blob: bb1062c3fbef84f90ffb0833c9a622e6f80344d2 (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
// Helper Class for SQLite backed storage

#pragma once

#include <memory>

#include <SQLiteCpp/SQLiteCpp.h>

class CompiledSQL
{
public:
 CompiledSQL(SQLite::Database& db, const std::string& stmt);
 
 // index 1-based as in SQLite
 template<typename T>
 void bind(int index, T value)
 {
  m_stmt->bind(index, value);
 }

 bool execute();

 // index 0-based as in SQLite
 template<typename T>
 T getColumn(const int index)
 {
  return m_stmt->getColumn(index);
 }

 class Guard
 {
 public:
  Guard(CompiledSQL& cs);
  ~Guard();
 private:
  CompiledSQL& m_cs;
 };

private:
 SQLite::Database& m_db;
 std::string m_query;
 std::shared_ptr<SQLite::Statement> m_stmt;
 bool m_isSelect; // In SQLite, SELECT statements will be handled w/ executeStep(), others w/ exec()
};