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 ResultOf = 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< ResultOf< Fn > >)> onDone={})
 
template<class Fn , class OnValue >
requires (!std::is_void_v<ResultOf<Fn>> && std::invocable<OnValue&, ResultOf<Fn>> && !std::invocable<OnValue&, Result<ResultOf<Fn>>>)
void RunAsync (std::string name, Fn fn, OnValue onValue)
 
template<class Fn >
Result< ResultOf< Fn > > Run (std::string name, Fn fn)
 
template<class Fn >
ResultOf< Fn > RunOr (std::string name, Fn fn, ResultOf< 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

◆ ResultOf

template<class Fn >
using VoltMod::Database::ResultOf = 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.

◆ ~Database()

VoltMod::Database::~Database ( )

Definition at line 14 of file Client.cpp.

References Disconnect().

◆ Database() [2/2]

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

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(), VoltMod::Scheduler::EveryFrame(), VoltMod::ReadFile(), 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.

References VoltMod::GameThreadQueue::Clear(), VoltMod::ReadFile(), and VoltMod::Subscription::Reset().

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.

References VoltMod::GameThreadQueue::RunAll().

Referenced by Connect().

◆ GetDriver()

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

The configured driver, set by Connect.

Definition at line 123 of file Client.hpp.

◆ 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

◆ Run()

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

RunAsync that blocks; load time only.

Definition at line 98 of file Client.hpp.

References VoltMod::ReadFile().

Referenced by Connect().

◆ RunAsync() [1/2]

template<class Fn , class OnValue >
requires (!std::is_void_v<ResultOf<Fn>> && std::invocable<OnValue&, ResultOf<Fn>> && !std::invocable<OnValue&, Result<ResultOf<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 VoltMod::ReadFile(), and RunAsync().

◆ RunAsync() [2/2]

template<class Fn >
void VoltMod::Database::RunAsync ( std::string  name,
Fn  fn,
std::move_only_function< void(Result< ResultOf< 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 >
ResultOf< Fn > VoltMod::Database::RunOr ( std::string  name,
Fn  fn,
ResultOf< 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: