scclib-sqlite
Stable Cloud Computing Sqlite Library
sqld.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_SQLD_H
32 #define _SCC_SQLD_H
33 
34 #include <string>
35 #include <sstream>
36 #include <vector>
37 
38 struct sqlite3; // forward declaration
39 struct sqlite3_stmt;
40 
41 namespace scc::sqld
42 {
43 
61 class Conn
62 {
63  sqlite3* m_db;
64  friend class Req;
65 
66  void close();
67 public:
70  Conn(const std::string& = "file:mem?mode=memory&cache=shared");
71  virtual ~Conn();
72 
73  Conn(const Conn&) = delete;
74  Conn& operator=(const Conn&) = delete;
75  Conn(Conn&&) = delete;
76  Conn& operator=(Conn&&) = delete;
77 
82  void reopen(const std::string& = "file:mem?mode=memory&cache=shared");
83 };
84 
89 class Trans
90 {
91  Conn& m_conn;
92  bool m_active;
93 public:
94  Trans(Conn&);
95  virtual ~Trans();
96 
101  void begin();
102 
107  void commit();
108 
113  void abort();
114 
116  bool is_active() const { return m_active; }
117 };
118 
128 class Req
129 {
130  Conn& m_conn;
131  sqlite3_stmt* m_stmt;
132 
133  std::ostringstream m_sql;
134  std::string::size_type m_pos;
135  int m_cols;
136 
137  void finalize();
138  void prepare();
139 public:
140  Req(Conn&);
141  virtual ~Req();
142 
147  void clear();
148 
154  void reset();
155 
166  int exec_select();
167 
175  int next_row();
176 
179  void exec();
180 
184  std::string col_name(int);
185 
189  void col_name(int, std::string&);
190 
194  std::string col_text(int);
195 
199  void col_text(int, std::string&);
200 
204  int col_int(int);
205 
209  int64_t col_int64(int);
210 
214  double col_real(int);
215 
219  void col_blob(int, std::vector<char>&);
220 
230  std::ostringstream& sql()
231  {
232  return m_sql;
233  }
234 };
235 
237 }
238 
239 #endif
scc::sqld::Req::clear
void clear()
Clear the request and the sql() stream.
Definition: sqld.cc:146
scc::sqld::Conn
Database connection.
Definition: sqld.h:61
scc::sqld::Req::col_text
std::string col_text(int)
Return UTF-8 TEXT.
Definition: sqld.cc:315
scc::sqld::Trans
Database transaction.
Definition: sqld.h:89
scc::sqld::Req::col_int
int col_int(int)
Return 32-bit INTEGER.
Definition: sqld.cc:351
scc::sqld::Req::sql
std::ostringstream & sql()
Sql streamer.
Definition: sqld.h:230
scc::sqld::Req::col_blob
void col_blob(int, std::vector< char > &)
Return BLOB unstructured data.
Definition: sqld.cc:405
scc::sqld::Req
Database request.
Definition: sqld.h:128
scc::sqld::Trans::abort
void abort()
ROLLBACK (abort) the transaction.
Definition: sqld.cc:116
scc::sqld::Conn::Conn
Conn(const std::string &="file:mem?mode=memory&cache=shared")
Constructs and open a sqlite in-memory database connection.
Definition: sqld.cc:46
scc::sqld::Req::exec
void exec()
Execute all statements, ignoring all row data.
Definition: sqld.cc:228
scc::sqld::Req::col_real
double col_real(int)
Return 64-bit REAL.
Definition: sqld.cc:387
scc::sqld::Trans::begin
void begin()
BEGIN the transaction.
Definition: sqld.cc:92
scc::sqld::Req::col_int64
int64_t col_int64(int)
Return 64-bit INTEGER.
Definition: sqld.cc:369
scc::sqld::Req::exec_select
int exec_select()
Executes in select mode.
Definition: sqld.cc:192
scc::sqld::Req::reset
void reset()
Reset the request without clearing the sql() stream.
Definition: sqld.cc:155
scc::sqld::Trans::is_active
bool is_active() const
Is this transaction active?
Definition: sqld.h:116
scc::sqld::Trans::commit
void commit()
COMMIT the transaction.
Definition: sqld.cc:104
scc::sqld::Req::col_name
std::string col_name(int)
Return column name.
Definition: sqld.cc:279
scc::sqld::Conn::reopen
void reopen(const std::string &="file:mem?mode=memory&cache=shared")
Reopen the connection.
Definition: sqld.cc:69
scc::sqld::Req::next_row
int next_row()
Get the next row.
Definition: sqld.cc:252