VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
VoltMod::Database Class Reference

Async database access over Postgres, MariaDB and SQLite. More...

#include <Client.hpp>

Public Types

template<class Fn>
using JobResult = std::invoke_result_t<Fn&, SqliteConnection&>

Public Member Functions

 Database (Scheduler &scheduler)
 ~Database ()
 Database (const Database &)=delete
Database & operator= (const Database &)=delete
bool Connect (const DatabaseConfig &config)
void Disconnect (std::chrono::milliseconds stopDeadline=std::chrono::seconds(5))
template<class Fn>
void RunAsync (std::string name, Fn fn, std::move_only_function< void(Result< JobResult< Fn > >)> onDone={})
template<class Fn, class OnValue>
requires (!std::is_void_v<JobResult<Fn>> && std::invocable<OnValue&, JobResult<Fn>> && !std::invocable<OnValue&, Result<JobResult<Fn>>>)
void RunAsync (std::string name, Fn fn, OnValue onValue)
template<class Fn>
Result< JobResult< Fn > > Run (std::string name, Fn fn)
template<class Fn>
JobResult< Fn > RunOr (std::string name, Fn fn, JobResult< Fn > fallback={})
void DispatchCompletions ()
bool IsConnected () const
Driver GetDriver () const

Detailed Description

Async database access over Postgres, MariaDB and SQLite.

One worker thread owns the connection; jobs run in order and their completions run on the game thread. A job is a callable over auto& conn that returns the same type for every driver. Run blocks, so it is for load time; gameplay uses RunAsync.

Definition at line 39 of file Client.hpp.

Member Typedef Documentation

◆ JobResult

template<class Fn>
using VoltMod::Database::JobResult = std::invoke_result_t<Fn&, SqliteConnection&>

What a job returns; MakeJob makes every driver agree.

Definition at line 44 of file Client.hpp.

Constructor & Destructor Documentation

◆ Database() [1/2]

VoltMod::Database::Database ( Scheduler & scheduler)
inlineexplicit

scheduler drives per-frame completion delivery and must outlive this object.

Definition at line 47 of file Client.hpp.

Referenced by Database(), and operator=().

◆ ~Database()

VoltMod::Database::~Database ( )

Definition at line 14 of file Client.cpp.

References Disconnect().

◆ Database() [2/2]

VoltMod::Database::Database ( const Database & )
delete

References Database().

Member Function Documentation

◆ Connect()

bool VoltMod::Database::Connect ( const DatabaseConfig & config)

Start the worker and ping; false on a bad config or an unreachable database.

Definition at line 19 of file Client.cpp.

References VoltMod::CheckConfig(), Disconnect(), DispatchCompletions(), VoltMod::Log::Error(), and Run().

◆ Disconnect()

void VoltMod::Database::Disconnect ( std::chrono::milliseconds stopDeadline = std::chrono::seconds(5))

Let queued jobs finish within stopDeadline, then join the worker; later jobs fail and undelivered completions are dropped. The destructor calls it.

Definition at line 55 of file Client.cpp.

Referenced by Connect(), and ~Database().

◆ DispatchCompletions()

void VoltMod::Database::DispatchCompletions ( )
inline

Run the finished jobs' completions; Connect schedules this every frame.

Definition at line 117 of file Client.hpp.

Referenced by Connect().

◆ GetDriver()

Driver VoltMod::Database::GetDriver ( ) const
inline

The configured driver, set by Connect.

Definition at line 123 of file Client.hpp.

Referenced by VoltMod::RunMigrations().

◆ IsConnected()

bool VoltMod::Database::IsConnected ( ) const
inline

Whether the worker's last job had a live connection; a diagnostic, not a guarantee.

Definition at line 120 of file Client.hpp.

◆ operator=()

Database & VoltMod::Database::operator= ( const Database & )
delete

References Database().

◆ Run()

template<class Fn>
Result< JobResult< Fn > > VoltMod::Database::Run ( std::string name,
Fn fn )
inline

RunAsync that blocks; load time only.

Definition at line 98 of file Client.hpp.

Referenced by Connect(), and VoltMod::RunMigrations().

◆ RunAsync() [1/2]

template<class Fn, class OnValue>
requires (!std::is_void_v<JobResult<Fn>> && std::invocable<OnValue&, JobResult<Fn>> && !std::invocable<OnValue&, Result<JobResult<Fn>>>)
void VoltMod::Database::RunAsync ( std::string name,
Fn fn,
OnValue onValue )
inline

RunAsync for a callback taking the value alone; a failure is only logged.

Definition at line 79 of file Client.hpp.

References RunAsync().

◆ RunAsync() [2/2]

template<class Fn>
void VoltMod::Database::RunAsync ( std::string name,
Fn fn,
std::move_only_function< void(Result< JobResult< Fn > >)> onDone = {} )
inline

Run fn on the worker; onDone runs on the game thread later. name is a log label.

Definition at line 61 of file Client.hpp.

Referenced by RunAsync().

◆ RunOr()

template<class Fn>
JobResult< Fn > VoltMod::Database::RunOr ( std::string name,
Fn fn,
JobResult< Fn > fallback = {} )
inline

Run, falling back to fallback when the job fails.

Definition at line 110 of file Client.hpp.


The documentation for this class was generated from the following files: