Represents a connection to a sqlite database. More...
#include <mostlyharmless_DatabaseState.h>
Public Member Functions | |
DatabaseState (const DatabaseState &)=delete | |
DatabaseState (DatabaseState &&other) noexcept | |
DatabaseState & | operator= (const DatabaseState &)=delete |
DatabaseState & | operator= (DatabaseState &&other) noexcept |
~DatabaseState () noexcept | |
template<DatabaseStorageType T> | |
auto | set (std::string_view name, const T &toSet) -> void |
template<DatabaseStorageType T> | |
auto | get (std::string_view name) -> std::optional< T > |
auto | duplicate () const -> std::optional< DatabaseState > |
Static Public Member Functions | |
static auto | tryCreate (const std::filesystem::path &location, const std::vector< std::pair< std::string, DatabaseValueVariant > > &initialValues) -> std::optional< DatabaseState > |
Represents a connection to a sqlite database.
The rationale behind this class' existence is to provide a unified "standard" layout for a plugin's global state - things like settings, statistics or anything else you might need to share between all instances. It doesn't directly support any kind of IPC notifiers, and is assumed that if a user wants synchronisation between instances using this class, they'll either poll the values they need on a timer, or set up their own filewatching mechanism.
The database schema itself is fairly generic, a single DATA
table, with NAME
, TEXT_VALUE
, BOOL_VALUE
, INT_VALUE
, FLOAT_VALUE
and `DOUBLE_VALUE fields. get and set are templated, and will get or set the field associated with the given template type - so it's important to ensure you retrieve the correct value type for a field you've previously set. The fields default to "", false, 0, 0.0f and 0.0 respectively. The connection is set to WAL mode, to allow concurrent access.
|
delete |
Non Copyable, as the database connection pointer will be closed on destruction...
|
inlinenoexcept |
Moveable, nulls other
's connection pointer
other | The moved-from DatabaseState instance |
|
inlinenoexcept |
The internal database handle is closed if not null.
|
inlinenodiscard |
Tries to create a copy of the current database connection. This is useful as database connections are only safe to use on a single thread - so for polling for changes on a background thread for example, a new connection must be used.
Will not work with an in-memory database created with filename = ":memory:".
|
inlinenodiscard |
Attempts to retrieve the value of a named field in the database.
T | A std::string, bool, any int, float, or double. |
name | The name of the database field to retrieve |
|
delete |
Non Copyable, as the database connection pointer will be closed on destruction...
|
inlinenoexcept |
Moveable, nulls other
's connection pointer
other | The moved-from DatabaseState instance |
|
inline |
Sets the value of a database entry with a matching name. If not found, will first create the entry.
T | A std::string, bool, any int, float, or double |
name | The name of the field to set. |
toSet | The value to set. |
|
inlinestaticnodiscard |
Attempts to create a new DatabaseState instance. On construction, will attempt to open the database passed to location, and establish a connection to it if it exists. If it doesn't exist, creates the database, and a table to store user data in.
location | A path to the database to create or open. |
initialValues | A vector containing the initial values to add to the database if it didn't exist. If the database DID exist, but any of the keys in the vector aren't present, they'll be added with the values specified and existing items will be skipped. |