sql.h
Go to the documentation of this file.
1 /* License: Apache 2.0. See LICENSE file in root directory. */
2 /* Copyright(c) 2019 Intel Corporation. All Rights Reserved. */
3 #pragma once
4 
5 #include <string>
6 #include <vector>
7 #include <stdint.h>
8 #include <functional>
9 
10 struct sqlite3;
11 struct sqlite3_stmt;
12 
13 namespace sql
14 {
16  {
17  using ptr = sqlite3*;
18 
19  static auto invalid() -> ptr { return nullptr; }
20 
21  static void close(ptr value);
22  };
23 
25  {
26  using ptr = sqlite3_stmt*;
27 
28  static auto invalid() -> ptr { return nullptr; }
29 
30  static void close(ptr value);
31  };
32 
33  template<class T>
35  {
36  typename T::ptr m_handle;
37  public:
38  scoped_handle() : m_handle(T::invalid()) {}
39  scoped_handle(scoped_handle&& other) : m_handle(other.m_handle)
40  {
41  other.m_handle = T::invalid();
42  }
44  {
45  swap(other);
46  return *this;
47  }
48  void swap(scoped_handle& other)
49  {
50  std::swap(m_handle, other.m_handle);
51  }
52 
53  typename T::ptr get() const { return m_handle; }
54  typename T::ptr* get_address() { return &m_handle; }
55 
57  {
58  if (m_handle != T::invalid())
59  {
60  T::close(m_handle);
61  m_handle = T::invalid();
62  }
63  }
64  };
65 
67 
68  class connection;
69 
71 
72  class statement
73  {
75  public:
76  statement(const connection& conn, const char * sql);
77 
78  bool step() const;
79 
80  int get_int(int column = 0) const;
81  double get_double(int column = 0) const;
82  std::string get_string(int column = 0) const;
83  std::vector<uint8_t> get_blob(int column = 0) const;
84 
85  void bind(int param, int value) const;
86  void bind(int param, double value) const;
87  void bind(int param, const char* value) const;
88  void bind(int param, const std::vector<uint8_t>& value) const;
89 
90  class iterator;
91  class row_value;
92 
94  {
96  int m_column;
97 
98  column_value(statement* owner, int column) : m_owner(owner), m_column(column) {}
99 
100  friend class row_value;
101  public:
102  std::string get_string() const { return m_owner->get_string(m_column); }
103  int get_int() const { return m_owner->get_int(m_column); }
104  double get_double() const { return m_owner->get_double(m_column); }
105  int get_bool() const { return m_owner->get_int(m_column) != 0; }
106  std::vector<uint8_t> get_blob() const { return m_owner->get_blob(m_column); }
107  };
108 
109  class row_value
110  {
112  bool m_bad;
113  row_value(statement* owner, bool bad) : m_owner(owner), m_bad(bad) {}
114  void assert_good() const;
115 
116  friend class iterator;
117  public:
119  {
120  assert_good();
121  return column_value(m_owner, column);
122  }
123  };
124 
125  class iterator
126  {
128  bool m_end;
129 
130  iterator(statement* owner) : m_owner(owner), m_end(!owner->step()) { }
131  iterator() : m_owner(nullptr), m_end(true) { }
132 
133  friend class statement;
134  public:
135  void operator++()
136  {
137  m_end = !m_owner->step();
138  }
139 
140  row_value operator*() const;
141  bool operator!=(const iterator& other) { return m_end != other.m_end; }
142  };
143 
144  iterator begin() { return { this }; }
145  iterator end() { return {}; }
146 
147  row_value operator()() { return *begin(); }
148  };
149 
151  {
153 
154  friend class statement;
155  public:
156  explicit connection(const char* filename);
157 
158  void execute(const char * command) const;
159 
160  bool table_exists(const char* name) const;
161 
162  void transaction(std::function<void()> transaction) const;
163  };
164 }
165 
const char * get_string(rs2_rs400_visual_preset value)
GLuint const GLchar * name
T::ptr m_handle
Definition: sql.h:36
iterator(statement *owner)
Definition: sql.h:130
double get_double() const
Definition: sql.h:104
row_value(statement *owner, bool bad)
Definition: sql.h:113
GLfloat value
ImVec4 operator*(const ImVec4 &color, float t)
Definition: model-views.h:68
int get_bool() const
Definition: sql.h:105
sql::statement::iterator end(sql::statement &stmt)
Definition: sql.cpp:169
GLenum GLenum GLsizei void GLsizei void * column
GLsizei const GLchar *const * string
std::vector< uint8_t > get_blob(int column=0) const
Definition: sql.cpp:126
std::string get_string(int column=0) const
Definition: sql.cpp:121
static auto invalid() -> ptr
Definition: sql.h:19
~scoped_handle()
Definition: sql.h:56
bool operator!=(const iterator &other)
Definition: sql.h:141
statement * m_owner
Definition: sql.h:95
row_value operator()()
Definition: sql.h:147
scoped_handle(scoped_handle &&other)
Definition: sql.h:39
struct sqlite3_stmt sqlite3_stmt
Definition: sqlite3.c:3429
T::ptr * get_address()
Definition: sql.h:54
sql::statement::iterator begin(sql::statement &stmt)
Definition: sql.cpp:168
static void close(ptr value)
Definition: sql.cpp:33
column_value operator[](int column) const
Definition: sql.h:118
iterator end()
Definition: sql.h:145
statement_handle m_handle
Definition: sql.h:74
statement * m_owner
Definition: sql.h:111
iterator begin()
Definition: sql.h:144
void swap(nlohmann::json &j1, nlohmann::json &j2) noexcept(is_nothrow_move_constructible< nlohmann::json >::value andis_nothrow_move_assignable< nlohmann::json >::value)
exchanges the values of two JSON objects
Definition: json.hpp:12141
sqlite3_stmt * ptr
Definition: sql.h:26
Definition: sql.cpp:11
scoped_handle & operator=(scoped_handle other)
Definition: sql.h:43
statement * m_owner
Definition: sql.h:127
GLenum GLfloat param
void swap(scoped_handle &other)
Definition: sql.h:48
connection_handle m_handle
Definition: sql.h:152
static auto invalid() -> ptr
Definition: sql.h:28
std::vector< uint8_t > get_blob() const
Definition: sql.h:106
std::string get_string() const
Definition: sql.h:102
int get_int() const
Definition: sql.h:103
bool step() const
Definition: sql.cpp:100
double get_double(int column=0) const
Definition: sql.cpp:115
int get_int(int column=0) const
Definition: sql.cpp:110
column_value(statement *owner, int column)
Definition: sql.h:98


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:41