RADLib
RADical C++ application framework
RADDatabase.h
Go to the documentation of this file.
1 #ifndef RADDATABASE_H
2 #define RADDATABASE_H
3 
9 #pragma once
10 
11 #include <RADCore/RADCore.h>
12 
13 #include <map>
14 #include <string>
15 #include <vector>
16 
17 namespace RADDatabase {
18 
20  using RADSqlRow = std::map<std::string, RADCore::RADVariant>;
21 
22  class RADSqlStatement;
23  class RADSqlTransaction;
24 
26  class RADSqlResult {
27  public:
29  RADSqlResult() = default;
31  explicit RADSqlResult(std::vector<RADSqlRow> rows);
32 
34  size_t rowCount() const;
36  bool empty() const;
38  const RADSqlRow& row(size_t index) const;
40  const std::vector<RADSqlRow>& rows() const;
41 
42  private:
43  std::vector<RADSqlRow> rows_;
44  };
45 
48  public:
49  RADEvents:
51  RAD_EVENT(errorOccurred, (std::string));
52 
56  explicit RADSqlDatabase(std::string fileName);
58  ~RADSqlDatabase() override;
59 
61  void setFileName(std::string fileName);
63  const std::string& fileName() const;
64 
66  bool open();
68  void close();
70  bool isOpen() const;
72  std::string lastError() const;
73 
75  bool execute(const std::string& sql);
77  RADSqlResult query(const std::string& sql);
79  RADSqlStatement prepare(const std::string& sql);
83  RADCore::EventLoop& completionLoop,
84  const std::string& sql);
85 
89  bool commit();
91  bool rollback();
95  bool migrateTo(int targetVersion, const std::vector<std::pair<int, std::string>>& migrations);
96 
97  private:
98  friend class RADSqlStatement;
99  friend class RADSqlTransaction;
100  struct Impl;
101  std::shared_ptr<Impl> pImpl_;
102  };
103 
106  public:
111 
117  RADSqlStatement(RADSqlStatement&& other) noexcept;
120 
122  bool isValid() const;
124  std::string lastError() const;
128  void reset();
129 
131  bool bindNull(int index);
133  bool bind(int index, int64_t value);
135  bool bind(int index, double value);
137  bool bind(int index, const std::string& value);
139  bool bind(int index, const std::vector<uint8_t>& value);
141  bool bind(int index, const RADCore::RADVariant& value);
142 
144  bool step();
146  bool execute();
148  int columnCount() const;
150  std::string columnName(int column) const;
152  RADCore::RADVariant columnValue(int column) const;
153 
154  private:
155  friend class RADSqlDatabase;
156  struct Impl;
157  explicit RADSqlStatement(std::shared_ptr<Impl> impl);
158  std::shared_ptr<Impl> pImpl_;
159  };
160 
163  public:
165  explicit RADSqlTransaction(RADSqlDatabase& database);
168 
173 
175  bool isActive() const;
177  bool commit();
179  bool rollback();
180 
181  private:
182  RADSqlDatabase* database_ = nullptr;
183  bool active_ = false;
184  };
185 
186 } // namespace RADDatabase
187 
188 #endif
#define RAD_EVENT(Name, Signature)
Declares a typed RADCore event; example: RAD_EVENT(done, (int)).
Definition: RADCore.h:65
#define RADEvents
Marks a public event declaration block in RADObject-derived classes.
Definition: RADCore.h:43
RADCore task event loop with locked and low-latency scheduling strategies.
Definition: RADCore.h:956
Typed shared future with optional continuation support.
Definition: RADCore.h:1514
Base class for event receivers, senders, and thread-affinity aware objects.
Definition: RADCore.h:931
Simple worker thread pool that reports completion through RADFuture.
Definition: RADCore.h:1637
Small value container used by models, futures, settings, and database rows.
Definition: RADCore.h:364
SQLite connection wrapper with RADCore event reporting.
Definition: RADDatabase.h:47
RADSqlDatabase()
Creates a database object with no file assigned yet.
void close()
Closes the SQLite connection.
int schemaVersion()
Returns current schema version from the rad_schema_version table.
const std::string & fileName() const
Returns the configured SQLite database file path.
std::string lastError() const
Returns the most recent SQLite error message.
void setFileName(std::string fileName)
Sets the SQLite database file path used by open().
RADCore::Event< void(std::string) > errorOccurred
Raised when SQLite open, execute, query, or transaction handling fails.
Definition: RADDatabase.h:51
bool execute(const std::string &sql)
Executes SQL that does not need row results.
RADCore::RADFutureT< RADSqlResult > queryAsync(RADCore::RADThreadPool &pool, RADCore::EventLoop &completionLoop, const std::string &sql)
Runs query asynchronously on pool and posts completion to completionLoop.
RADSqlResult query(const std::string &sql)
Executes SQL and returns collected result rows.
bool rollback()
Rolls back the active transaction.
bool isOpen() const
Returns true when a SQLite connection is open.
bool commit()
Commits the active transaction.
~RADSqlDatabase() override
Closes the SQLite handle if still open.
RADSqlDatabase(std::string fileName)
Creates a database object targeting fileName.
bool migrateTo(int targetVersion, const std::vector< std::pair< int, std::string >> &migrations)
Applies ordered migrations until targetVersion is reached.
bool beginTransaction()
Starts a SQLite transaction.
bool open()
Opens the configured SQLite database file.
RADSqlStatement prepare(const std::string &sql)
Prepares a reusable SQL statement.
Value object containing rows returned from a SQL query.
Definition: RADDatabase.h:26
RADSqlResult(std::vector< RADSqlRow > rows)
Creates a result from already-collected rows.
const RADSqlRow & row(size_t index) const
Returns a row by index; callers must pass an index below rowCount().
size_t rowCount() const
Returns the number of rows in this result.
const std::vector< RADSqlRow > & rows() const
Returns all rows for range iteration or bulk inspection.
RADSqlResult()=default
Creates an empty result.
bool empty() const
Returns true when no rows are present.
Prepared SQLite statement with typed binding and row stepping.
Definition: RADDatabase.h:105
RADSqlStatement & operator=(RADSqlStatement &&other) noexcept
Moves statement ownership.
bool bind(int index, double value)
Binds double at 1-based index.
bool bindNull(int index)
Binds null at 1-based index.
std::string columnName(int column) const
Returns column name by zero-based index.
RADCore::RADVariant columnValue(int column) const
Returns column value by zero-based index.
void reset()
Resets statement execution state.
bool bind(int index, const RADCore::RADVariant &value)
Binds a RADVariant at 1-based index.
bool step()
Advances one row; returns true when a row is available.
~RADSqlStatement()
Finalizes the statement.
RADSqlStatement(const RADSqlStatement &)=delete
Statement ownership is movable but not copyable.
RADSqlStatement & operator=(const RADSqlStatement &)=delete
Statement ownership is movable but not copyable.
bool bind(int index, const std::vector< uint8_t > &value)
Binds bytes at 1-based index.
void clearBindings()
Clears all current bindings.
RADSqlStatement()
Creates an invalid statement.
std::string lastError() const
Returns the last statement error.
RADSqlStatement(RADSqlStatement &&other) noexcept
Moves statement ownership.
bool isValid() const
Returns true when a statement was prepared successfully.
bool execute()
Executes until done for statements that do not return rows.
bool bind(int index, int64_t value)
Binds integer at 1-based index.
bool bind(int index, const std::string &value)
Binds text at 1-based index.
int columnCount() const
Returns number of columns in the current row.
RAII transaction that rolls back unless committed.
Definition: RADDatabase.h:162
RADSqlTransaction & operator=(const RADSqlTransaction &)=delete
Transactions cannot be copied.
bool rollback()
Rolls back the transaction.
RADSqlTransaction(RADSqlDatabase &database)
Begins a transaction on database.
bool isActive() const
Returns true when begin succeeded and the transaction is active.
RADSqlTransaction(const RADSqlTransaction &)=delete
Transactions cannot be copied.
bool commit()
Commits the transaction.
~RADSqlTransaction()
Rolls back if still active.
Definition: RADDatabase.h:17
std::map< std::string, RADCore::RADVariant > RADSqlRow
One SQLite result row keyed by column name.
Definition: RADDatabase.h:20