Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
sql.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <stdint.h>
6 #include <functional>
7 
8 struct sqlite3;
9 struct sqlite3_stmt;
10 
11 namespace sql
12 {
14  {
15  using ptr = sqlite3*;
16 
17  static auto invalid() -> ptr { return nullptr; }
18 
19  static void close(ptr value);
20  };
21 
23  {
24  using ptr = sqlite3_stmt*;
25 
26  static auto invalid() -> ptr { return nullptr; }
27 
28  static void close(ptr value);
29  };
30 
31  template<class T>
33  {
34  typename T::ptr m_handle;
35  public:
36  scoped_handle() : m_handle(T::invalid()) {}
37  scoped_handle(scoped_handle&& other) : m_handle(other.m_handle)
38  {
39  other.m_handle = T::invalid();
40  }
42  {
43  swap(other);
44  return *this;
45  }
46  void swap(scoped_handle& other)
47  {
48  std::swap(m_handle, other.m_handle);
49  }
50 
51  typename T::ptr get() const { return m_handle; }
52  typename T::ptr* get_address() { return &m_handle; }
53 
55  {
56  if (m_handle != T::invalid())
57  {
58  T::close(m_handle);
59  m_handle = T::invalid();
60  }
61  }
62  };
63 
65 
66  class connection;
67 
69 
70  class statement
71  {
72  statement_handle m_handle;
73  public:
74  statement(const connection& conn, const char * sql);
75 
76  bool step() const;
77 
78  int get_int(int column = 0) const;
79  double get_double(int column = 0) const;
80  std::string get_string(int column = 0) const;
81  std::vector<uint8_t> get_blob(int column = 0) const;
82 
83  void bind(int param, int value) const;
84  void bind(int param, double value) const;
85  void bind(int param, const char* value) const;
86  void bind(int param, const std::vector<uint8_t>& value) const;
87 
88  class iterator;
89  class row_value;
90 
92  {
93  statement* m_owner;
94  int m_column;
95 
96  column_value(statement* owner, int column) : m_owner(owner), m_column(column) {}
97 
98  friend class row_value;
99  public:
100  std::string get_string() const { return m_owner->get_string(m_column); }
101  int get_int() const { return m_owner->get_int(m_column); }
102  double get_double() const { return m_owner->get_double(m_column); }
103  int get_bool() const { return m_owner->get_int(m_column) != 0; }
104  std::vector<uint8_t> get_blob() const { return m_owner->get_blob(m_column); }
105  };
106 
107  class row_value
108  {
109  statement* m_owner;
110  bool m_bad;
111  row_value(statement* owner, bool bad) : m_owner(owner), m_bad(bad) {}
112  void assert_good() const;
113 
114  friend class iterator;
115  public:
116  column_value operator[](int column) const
117  {
118  assert_good();
119  return column_value(m_owner, column);
120  }
121  };
122 
123  class iterator
124  {
125  statement* m_owner;
126  bool m_end;
127 
128  iterator(statement* owner) : m_owner(owner), m_end(!owner->step()) { }
129  iterator() : m_owner(nullptr), m_end(true) { }
130 
131  friend class statement;
132  public:
133  void operator++()
134  {
135  m_end = !m_owner->step();
136  }
137 
138  row_value operator*() const;
139  bool operator!=(const iterator& other) { return m_end != other.m_end; }
140  };
141 
142  iterator begin() { return { this }; }
143  iterator end() { return {}; }
144 
145  row_value operator()() { return *begin(); }
146  };
147 
149  {
150  connection_handle m_handle;
151 
152  friend class statement;
153  public:
154  explicit connection(const char* filename);
155 
156  void execute(const char * command) const;
157 
158  bool table_exists(const char* name) const;
159 
160  void transaction(std::function<void()> transaction) const;
161  };
162 }
163 
void execute(const char *command) const
Definition: backend.h:347
Definition: backend.h:351
sqlite3 * ptr
Definition: sql.h:15
Definition: sql.h:70
std::string get_string(int column=0) const
Definition: sql.h:123
Definition: sql.h:91
static void close(ptr value)
sql::statement::iterator end(sql::statement &stmt)
row_value operator*() const
static auto invalid() -> ptr
Definition: sql.h:17
~scoped_handle()
Definition: sql.h:54
bool operator!=(const iterator &other)
Definition: sql.h:139
double get_double(int column=0) const
scoped_handle()
Definition: sql.h:36
std::vector< uint8_t > get_blob(int column=0) const
row_value operator()()
Definition: sql.h:145
std::vector< uint8_t > get_blob() const
Definition: sql.h:104
bool step() const
scoped_handle(scoped_handle &&other)
Definition: sql.h:37
T::ptr * get_address()
Definition: sql.h:52
sql::statement::iterator begin(sql::statement &stmt)
column_value operator[](int column) const
Definition: sql.h:116
bool table_exists(const char *name) const
iterator end()
Definition: sql.h:143
int get_int() const
Definition: sql.h:101
static void close(ptr value)
iterator begin()
Definition: sql.h:142
statement(const connection &conn, const char *sql)
sqlite3_stmt * ptr
Definition: sql.h:24
Definition: sql.h:11
scoped_handle & operator=(scoped_handle other)
Definition: sql.h:41
void operator++()
Definition: sql.h:133
Definition: sql.h:22
Definition: sql.h:107
double get_double() const
Definition: sql.h:102
void swap(scoped_handle &other)
Definition: sql.h:46
int get_bool() const
Definition: sql.h:103
void transaction(std::function< void()> transaction) const
std::string get_string() const
Definition: sql.h:100
int get_int(int column=0) const
void bind(int param, int value) const
static auto invalid() -> ptr
Definition: sql.h:26
Definition: sql.h:32
Definition: sql.h:148
Definition: sql.h:13
connection(const char *filename)