MbDatabase connection and session management.
More...
#include <database.h>
|
| | MbDatabase (const MantisBase &app) |
| | Construct database instance bound to an application.
|
| | ~MbDatabase () |
| | Destructor (disconnects from database).
|
| bool | connect (const std::string &conn_str) |
| | Connect to database and initialize connection pool.
|
| const std::string & | connectionStr () const |
| void | disconnect () |
| | Close all database connections and destroy connection pool.
|
| bool | createSysTables () const |
| | Create system tables (mb_tables, mb_admins, etc.).
|
| std::shared_ptr< soci::session > | session () const |
| | Get a database session from the connection pool.
|
| soci::connection_pool & | connectionPool () const |
| | Get access to the underlying connection pool.
|
| bool | isConnected () const |
| | Check if database is connected.
|
MbDatabase connection and session management.
Handles database connections, connection pooling, and provides session management for executing queries. Supports SQLite (default) and PostgreSQL.
db.
connect(
"dbname=mantis user=postgres password=pass");
*
session <<
"SELECT * FROM users", soci::into(rows);
static std::unique_ptr< MantisBase > create(int argc, char **argv)
Create class instance given cmd args passed in.
Definition mantisbase.cpp:75
MbDatabase(const MantisBase &app)
Construct database instance bound to an application.
Definition database.cpp:23
std::shared_ptr< soci::session > session() const
Get a database session from the connection pool.
Definition database.cpp:474
bool connect(const std::string &conn_str)
Connect to database and initialize connection pool.
Definition database.cpp:28
◆ MbDatabase()
| mb::MbDatabase::MbDatabase |
( |
const MantisBase & | app | ) |
|
|
explicit |
Construct database instance bound to an application.
- Parameters
-
| app | The owning application; used for config (pool size, db type, data dir, dev mode). Stored by reference and must outlive this MbDatabase. |
◆ ~MbDatabase()
| mb::MbDatabase::~MbDatabase |
( |
| ) |
|
Destructor (disconnects from database).
◆ connect()
| bool mb::MbDatabase::connect |
( |
const std::string & | conn_str | ) |
|
Connect to database and initialize connection pool.
- Parameters
-
| conn_str | Connection string (format depends on database type)
- SQLite: path to database file or empty for default
- PostgreSQL: "dbname=name host=host port=5432 user=user password=pass"
|
- Returns
- true if connection successful, false otherwise
◆ connectionPool()
| soci::connection_pool & mb::MbDatabase::connectionPool |
( |
| ) |
const |
|
nodiscard |
Get access to the underlying connection pool.
- Returns
- Reference to soci::connection_pool
◆ connectionStr()
| const std::string & mb::MbDatabase::connectionStr |
( |
| ) |
const |
|
nodiscard |
- Returns
- Active connection string passed to connect.
◆ createSysTables()
| bool mb::MbDatabase::createSysTables |
( |
| ) |
const |
|
nodiscard |
Create system tables (mb_tables, mb_admins, etc.).
- Returns
- true if migration successful, false otherwise
◆ disconnect()
| void mb::MbDatabase::disconnect |
( |
| ) |
|
Close all database connections and destroy connection pool.
◆ isConnected()
| bool mb::MbDatabase::isConnected |
( |
| ) |
const |
|
nodiscard |
Check if database is connected.
- Returns
- true if connected, false otherwise
◆ session()
| std::shared_ptr< soci::session > mb::MbDatabase::session |
( |
| ) |
const |
|
nodiscard |
Get a database session from the connection pool.
- Returns
- Shared pointer to soci::session
auto sql = db.session();
*sql << "SELECT * FROM users WHERE id = :id", soci::use(id), soci::into(row);
The documentation for this class was generated from the following files:
- /home/runner/work/mantisbase/mantisbase/include/mantisbase/core/database.h
- /home/runner/work/mantisbase/mantisbase/src/core/database.cpp