Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::database::Row Class Reference

Represents a single row of a database result set. More...

#include <ResultSet.hpp>

Public Member Functions

 Row ()=default
 
 Row (std::vector< std::string > values, std::shared_ptr< std::unordered_map< std::string, size_t > > col_map)
 
 Row (std::vector< std::optional< std::string > > values, std::shared_ptr< std::unordered_map< std::string, size_t > > col_map)
 Values may be std::nullopt for SQL NULL.
 
std::optional< std::string > get (size_t index) const
 Gets a column value by index.
 
std::optional< std::string > get (const std::string &col_name) const
 Gets a column value by column name.
 
template<typename T >
std::optional< T > get_as (size_t index) const
 Gets a column converted to T (bool, an integer type, a floating point type or std::string).
 
template<typename T >
std::optional< T > get_as (const std::string &col_name) const
 
template<typename T >
T value_or (const std::string &col_name, T fallback) const
 get_as<T>(), or fallback when that is std::nullopt.
 
bool is_null (const std::string &col_name) const
 True if the column exists and is SQL NULL.
 
int as_int (const std::string &col_name, int default_val=0) const
 Helper to get a column value as an integer.
 
nlohmann::json to_json () const
 Serializes the row to a JSON object automatically. Tries to parse numbers and booleans from strings if possible (Task 52).
 

Private Attributes

std::vector< std::optional< std::string > > values_
 
std::shared_ptr< std::unordered_map< std::string, size_t > > col_map_
 

Detailed Description

Represents a single row of a database result set.

Constructor & Destructor Documentation

◆ Row() [1/3]

orbit::database::Row::Row ( )
default

◆ Row() [2/3]

orbit::database::Row::Row ( std::vector< std::string >  values,
std::shared_ptr< std::unordered_map< std::string, size_t > >  col_map 
)
inline

◆ Row() [3/3]

orbit::database::Row::Row ( std::vector< std::optional< std::string > >  values,
std::shared_ptr< std::unordered_map< std::string, size_t > >  col_map 
)
inline

Values may be std::nullopt for SQL NULL.

Member Function Documentation

◆ as_int()

int orbit::database::Row::as_int ( const std::string &  col_name,
int  default_val = 0 
) const
inline

Helper to get a column value as an integer.

◆ get() [1/2]

std::optional< std::string > orbit::database::Row::get ( const std::string &  col_name) const
inline

Gets a column value by column name.

Parameters
col_nameThe name of the column.
Returns
An optional containing the string value, or std::nullopt if not found or NULL.

◆ get() [2/2]

std::optional< std::string > orbit::database::Row::get ( size_t  index) const
inline

Gets a column value by index.

Parameters
indexThe zero-based column index.
Returns
An optional containing the string value, or std::nullopt if index is out of bounds or value is NULL.

◆ get_as() [1/2]

template<typename T >
std::optional< T > orbit::database::Row::get_as ( const std::string &  col_name) const
inline

◆ get_as() [2/2]

template<typename T >
std::optional< T > orbit::database::Row::get_as ( size_t  index) const
inline

Gets a column converted to T (bool, an integer type, a floating point type or std::string).

Returns
std::nullopt if the column is missing, NULL, or does not convert exactly (out of range, trailing characters).

◆ is_null()

bool orbit::database::Row::is_null ( const std::string &  col_name) const
inline

True if the column exists and is SQL NULL.

◆ to_json()

nlohmann::json orbit::database::Row::to_json ( ) const
inline

Serializes the row to a JSON object automatically. Tries to parse numbers and booleans from strings if possible (Task 52).

◆ value_or()

template<typename T >
T orbit::database::Row::value_or ( const std::string &  col_name,
T  fallback 
) const
inline

get_as<T>(), or fallback when that is std::nullopt.

Member Data Documentation

◆ col_map_

std::shared_ptr<std::unordered_map<std::string, size_t> > orbit::database::Row::col_map_
private

◆ values_

std::vector<std::optional<std::string> > orbit::database::Row::values_
private

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