SlHelpers
SQLConn.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #pragma once
4 
5 #include <filesystem>
6 #include <optional>
7 #include <string>
8 #include <variant>
9 #include <vector>
10 
11 #include "../helpers/LastError.h"
12 #include "SQLiteSmart.h"
13 
14 namespace SlSqlite {
15 
19 enum OpenFlags : unsigned {
20  CREATE = 1 << 0,
21  NO_FOREIGN_KEY = 1 << 1,
22  ERROR_ON_UNIQUE_CONSTRAINT = 1 << 2,
23 };
24 
28 enum struct TransactionType {
29  DEFERRED,
30  IMMEDIATE,
31  EXCLUSIVE,
32 };
33 
34 class Select;
35 class SQLConn;
36 
41 public:
42  AutoTransaction() = delete;
44  AutoTransaction(const SQLConn &conn,
45  TransactionType type = TransactionType::DEFERRED);
46  ~AutoTransaction() { end(); }
47 
48  AutoTransaction(const AutoTransaction &) = delete;
49  AutoTransaction &operator=(const AutoTransaction &) = delete;
50 
52  AutoTransaction(AutoTransaction &&other) noexcept : m_conn(other.m_conn) {
53  other.m_conn = nullptr;
54  }
57  if (this != &other) {
58  end();
59  m_conn = other.m_conn;
60  other.m_conn = nullptr;
61  }
62 
63  return *this;
64  }
65 
67  void end();
68 
70  bool operator!() const { return !m_conn; }
72  operator bool() const { return m_conn; }
73 private:
74  const SQLConn *m_conn;
75 };
76 
80 class SQLConn {
81  friend class Select;
82 public:
83  virtual ~SQLConn() = default;
84 
86  SQLConn(SQLConn &&) = default;
88  SQLConn &operator=(SQLConn &&) = default;
89 
96  bool open(const std::filesystem::path &dbFile, unsigned int flags = 0)
97  {
98  return openDB(dbFile, flags) && createDB() && prepDB();
99  }
100 
107  bool openDB(const std::filesystem::path &dbFile, unsigned int flags = 0) noexcept;
108 
115  virtual bool createDB() { return true; }
116 
123  virtual bool prepDB() { return true; }
124 
130  bool exec(const std::string &sql) const noexcept { return exec(sql, "exec failed"); }
131 
138  bool attach(const std::filesystem::path &dbFile,
139  std::string_view dbName) const noexcept;
140 
146  bool begin(TransactionType type = TransactionType::DEFERRED) const noexcept;
147 
152  bool end() const noexcept { return exec("END;", "db END failed"); }
153 
159  AutoTransaction beginAuto(TransactionType type = TransactionType::DEFERRED) const noexcept {
160  return AutoTransaction(*this, type);
161  }
162 
164  std::string lastError() const { return m_lastError.lastError(); }
166  int lastErrorCode() const { return m_lastError.get<0>(); }
168  int lastErrorCodeExt() const { return m_lastError.get<1>(); }
169 
170 protected:
171  SQLConn() : m_flags(0) {}
172 
174  using BindVal = std::variant<std::monostate, int, unsigned, std::string, std::string_view>;
176  using Binding = std::vector<std::pair<std::string, BindVal>>;
178  using Column = std::variant<std::monostate, int, std::string>;
180  using Row = std::vector<Column>;
182  using SelectResult = std::vector<Row>;
183 
185  enum TableFlags : unsigned {
186  TABLE_TEMPORARY = 1u << 0,
187  };
188 
190  struct TableEntry {
192  std::string name;
194  std::vector<std::string> columns;
196  unsigned flags = 0u;
197  };
198 
200  using Tables = std::vector<TableEntry>;
202  using Indices = std::vector<std::pair<std::string, std::string>>;
204  using Triggers = Indices;
206  using Views = Indices;
208  using Statements = std::vector<std::pair<std::reference_wrapper<SQLStmtHolder>, std::string>>;
209 
211  bool createTables(const Tables &tables) const noexcept;
213  bool createIndices(const Indices &indices) const noexcept;
215  bool createTriggers(const Triggers &triggers) const noexcept;
217  bool createViews(const Views &views) const noexcept;
218 
220  bool prepareStatement(std::string_view sql, SQLStmtHolder &stmt) const noexcept;
222  bool prepareStatements(const Statements &stmts) const noexcept;
223 
225  bool bind(const SQLStmtHolder &ins, const std::string &key,
226  const BindVal &val, bool transient = false) const noexcept;
228  bool bind(const SQLStmtHolder &ins, const Binding &binding,
229  bool transient = false) const noexcept;
231  bool step(const SQLStmtHolder &ins, uint64_t *affected = nullptr,
232  bool *uniqueError = nullptr) const noexcept;
234  bool insert(const SQLStmtHolder &ins, const Binding &binding = {},
235  uint64_t *affected = nullptr) const noexcept;
236 
238  std::optional<SQLConn::SelectResult>
239  select(const SQLStmtHolder &sel, const Binding &binding) const noexcept;
240 
242  static BindVal valueOrNull(bool cond, BindVal val) {
243  return cond ? std::move(val) : std::monostate();
244  }
245 
248 
250  LastError &setError(int ret, std::string_view error, bool errmsg = false) const;
251 
255  unsigned int m_flags;
258 private:
259  bool exec(const std::string &SQL, std::string_view errorMsg,
260  bool includeSQL = false) const noexcept;
261 
262  static constexpr bool isUniqueConstraint(int sqlExtError) noexcept;
263  static int busyHandler(void *, int count);
264  void dumpBinding(const Binding &binding) const noexcept;
265 };
266 
267 inline AutoTransaction::AutoTransaction(const SQLConn &conn, TransactionType type)
268  : m_conn(conn.begin(type) ? &conn : nullptr)
269 {
270 }
271 
272 inline void AutoTransaction::end()
273 {
274  if (m_conn) {
275  m_conn->end();
276  m_conn = nullptr;
277  }
278 }
279 
280 }
bool prepareStatement(std::string_view sql, SQLStmtHolder &stmt) const noexcept
Prepate one sql string into stmt.
Begin a transaction in the constructor and end in the destructor.
Definition: SQLConn.h:40
bool createTables(const Tables &tables) const noexcept
Create tables in the DB as specified in tables.
std::string lastError() const
Return the last error string if some.
Definition: SQLConn.h:164
Indices Triggers
Triggers to be created by createTriggers()
Definition: SQLConn.h:204
virtual bool createDB()
Creates tables, views, triggers and such.
Definition: SQLConn.h:115
void end()
End the transaction manually.
Definition: SQLConn.h:272
SQLite3 connection (the core class)
Definition: SQLConn.h:80
TableFlags
Flags for TableEntry::flags.
Definition: SQLConn.h:185
Wrapper around std::unique_ptr for easier re-definition of free.
Definition: Unique.h:41
bool openDB(const std::filesystem::path &dbFile, unsigned int flags=0) noexcept
Just open a database file.
static BindVal valueOrNull(bool cond, BindVal val)
A helper to build a null BindVal if cond does not hold, val otherwise.
Definition: SQLConn.h:242
std::vector< Row > SelectResult
Complete SELECT result.
Definition: SQLConn.h:182
bool end() const noexcept
End a transaction.
Definition: SQLConn.h:152
AutoTransaction(AutoTransaction &&other) noexcept
Move constructor.
Definition: SQLConn.h:52
std::tuple_element_t< idx, Tuple > & get() noexcept
Get n-th error member.
Definition: LastError.h:22
bool exec(const std::string &sql) const noexcept
Execute sql.
Definition: SQLConn.h:130
int lastErrorCode() const
Return the last error number.
Definition: SQLConn.h:166
LastError m_lastError
The last error + error code + extended error code.
Definition: SQLConn.h:257
Definition: SQLConn.h:14
std::vector< std::pair< std::string, BindVal > > Binding
Bind name -> bind value.
Definition: SQLConn.h:176
bool createViews(const Views &views) const noexcept
Create views in the DB as specified in views.
bool prepareStatements(const Statements &stmts) const noexcept
Prepate all statements as specified in stmts.
bool createTriggers(const Triggers &triggers) const noexcept
Create triggers in the DB as specified in triggers.
AutoTransaction beginAuto(TransactionType type=TransactionType::DEFERRED) const noexcept
Begin a transaction which is automatically ended when the returned object dies.
Definition: SQLConn.h:159
LastError & setError(int ret, std::string_view error, bool errmsg=false) const
Set last error to ret and error.
A table to be created by createTables()
Definition: SQLConn.h:190
virtual bool prepDB()
Prepares statements.
Definition: SQLConn.h:123
int lastErrorCodeExt() const
Return the last extended error number.
Definition: SQLConn.h:168
bool attach(const std::filesystem::path &dbFile, std::string_view dbName) const noexcept
Attach another database using ATTACH.
std::optional< SQLConn::SelectResult > select(const SQLStmtHolder &sel, const Binding &binding) const noexcept
Perform one SELECT (sel), using the passed binding.
Indices Views
Views to be created by createViews()
Definition: SQLConn.h:206
std::vector< std::pair< std::reference_wrapper< SQLStmtHolder >, std::string > > Statements
Statements to be prepared by prepareStatements()
Definition: SQLConn.h:208
std::variant< std::monostate, int, unsigned, std::string, std::string_view > BindVal
Bind value (SQL&#39;s null, number, string)
Definition: SQLConn.h:174
std::vector< std::string > columns
List of columns.
Definition: SQLConn.h:194
bool bind(const SQLStmtHolder &ins, const std::string &key, const BindVal &val, bool transient=false) const noexcept
Bind one value val into key of the statement ins.
std::string name
Name of the table.
Definition: SQLConn.h:192
std::vector< TableEntry > Tables
Tables to be created by createTables()
Definition: SQLConn.h:200
std::vector< Column > Row
One row returned by SELECT (ie. list of Columns)
Definition: SQLConn.h:180
bool begin(TransactionType type=TransactionType::DEFERRED) const noexcept
Begin a transaction.
bool operator!() const
Test whether AutoTransaction is valid.
Definition: SQLConn.h:70
unsigned flags
See TableFlags.
Definition: SQLConn.h:196
bool insert(const SQLStmtHolder &ins, const Binding &binding={}, uint64_t *affected=nullptr) const noexcept
Bind, step, and reset the statement ins, using the passed binding.
std::vector< std::pair< std::string, std::string > > Indices
Indices to be created by createIndices()
Definition: SQLConn.h:202
bool createIndices(const Indices &indices) const noexcept
Create indices in the DB as specified in indices.
AutoTransaction & operator=(AutoTransaction &&other) noexcept
Move assignment.
Definition: SQLConn.h:56
SQLHolder sqlHolder
The DB connection.
Definition: SQLConn.h:253
std::variant< std::monostate, int, std::string > Column
One column returned by SELECT.
Definition: SQLConn.h:178
std::string lastError() const noexcept
Obtain the stored string.
Definition: LastError.h:112
bool step(const SQLStmtHolder &ins, uint64_t *affected=nullptr, bool *uniqueError=nullptr) const noexcept
Do one step of the statement ins.
bool open(const std::filesystem::path &dbFile, unsigned int flags=0)
Open a database connection (openDB() + createDB() + prepDB())
Definition: SQLConn.h:96
SQLConn & operator=(SQLConn &&)=default
Move assignment.
unsigned int m_flags
OpenFlags.
Definition: SQLConn.h:255