A C++ interface towards an SQLITE database. More...
#include "sqlite-output.h"
Public Member Functions | |
SQLiteOutput (const std::string &name) | |
SQLiteOutput constructor. | |
~SQLiteOutput () | |
Destructor. | |
template<> | |
bool | Bind (sqlite3_stmt *stmt, int pos, const double &value) const |
Bind a value to a sqlite statement. | |
template<> | |
bool | Bind (sqlite3_stmt *stmt, int pos, const int &value) const |
Bind a value to a sqlite statement. | |
template<> | |
bool | Bind (sqlite3_stmt *stmt, int pos, const long &value) const |
Bind a value to a sqlite statement. | |
template<> | |
bool | Bind (sqlite3_stmt *stmt, int pos, const long long &value) const |
Bind a value to a sqlite statement. | |
template<> | |
bool | Bind (sqlite3_stmt *stmt, int pos, const std::string &value) const |
Bind a value to a sqlite statement. | |
template<typename T > | |
bool | Bind (sqlite3_stmt *stmt, int pos, const T &value) const |
Bind a value to a sqlite statement. | |
template<> | |
bool | Bind (sqlite3_stmt *stmt, int pos, const Time &value) const |
Bind a value to a sqlite statement. | |
template<> | |
bool | Bind (sqlite3_stmt *stmt, int pos, const uint16_t &value) const |
Bind a value to a sqlite statement. | |
template<> | |
bool | Bind (sqlite3_stmt *stmt, int pos, const uint32_t &value) const |
Bind a value to a sqlite statement. | |
template<> | |
bool | Bind (sqlite3_stmt *stmt, int pos, const uint8_t &value) const |
Bind a value to a sqlite statement. | |
template<> | |
int | RetrieveColumn (sqlite3_stmt *stmt, int pos) const |
Retrieve a value from an executed statement. | |
template<> | |
uint32_t | RetrieveColumn (sqlite3_stmt *stmt, int pos) const |
Retrieve a value from an executed statement. | |
template<> | |
double | RetrieveColumn (sqlite3_stmt *stmt, int pos) const |
Retrieve a value from an executed statement. | |
template<typename T > | |
T | RetrieveColumn (sqlite3_stmt *stmt, int pos) const |
Retrieve a value from an executed statement. | |
void | SetJournalInMemory () |
Instruct SQLite to store the journal in memory. | |
bool | SpinExec (const std::string &cmd) const |
Execute a command until the return value is OK or an ERROR. | |
bool | SpinExec (sqlite3_stmt *stmt) const |
Execute a command until the return value is OK or an ERROR. | |
bool | SpinPrepare (sqlite3_stmt **stmt, const std::string &cmd) const |
Prepare a statement. | |
bool | WaitExec (const std::string &cmd) const |
Execute a command, waiting on a mutex. | |
bool | WaitExec (sqlite3_stmt *stmt) const |
Execute a command, waiting on a mutex. | |
bool | WaitPrepare (sqlite3_stmt **stmt, const std::string &cmd) const |
Prepare a statement, waiting on a mutex. | |
![]() | |
SimpleRefCount () | |
Default constructor. | |
SimpleRefCount (const SimpleRefCount &o) | |
Copy constructor. | |
uint32_t | GetReferenceCount () const |
Get the reference count of the object. | |
SimpleRefCount & | operator= (const SimpleRefCount &o) |
Assignment operator. | |
void | Ref () const |
Increment the reference count. | |
void | Unref () const |
Decrement the reference count. | |
Static Public Member Functions | |
static int | SpinFinalize (sqlite3_stmt *stmt) |
Finalize a statement until the result is ok or an error. | |
static int | SpinReset (sqlite3_stmt *stmt) |
Reset a statement until the result is ok or an error. | |
static int | SpinStep (sqlite3_stmt *stmt) |
Execute a step operation on a statement until the result is ok or an error. | |
Protected Member Functions | |
int | WaitExec (sqlite3 *db, const std::string &cmd) const |
Execute a command, waiting on a mutex. | |
int | WaitExec (sqlite3 *db, sqlite3_stmt *stmt) const |
Execute a statement, waiting on a mutex. | |
int | WaitPrepare (sqlite3 *db, sqlite3_stmt **stmt, const std::string &cmd) const |
Prepare a statement, waiting on a mutex. | |
Static Protected Member Functions | |
static bool | CheckError (sqlite3 *db, int rc, const std::string &cmd, bool hardExit) |
Check any error in the db. | |
static void | Error (sqlite3 *db, const std::string &cmd) |
Fail, printing an error message from sqlite. | |
static int | SpinExec (sqlite3 *db, const std::string &cmd) |
Execute a command ignoring concurrency problems, retrying instead. | |
static int | SpinExec (sqlite3 *db, sqlite3_stmt *stmt) |
Execute a Prepared Statement Object ignoring concurrency problems, retrying instead. | |
static int | SpinPrepare (sqlite3 *db, sqlite3_stmt **stmt, const std::string &cmd) |
Preparing a command ignoring concurrency problems, retrying instead. | |
Private Attributes | |
sqlite3 * | m_db {nullptr} |
Database pointer. | |
std::string | m_dBname |
Database name. | |
std::mutex | m_mutex |
Mutex. | |
A C++ interface towards an SQLITE database.
The class is able to execute commands, and retrieve results, from an SQLITE database. The methods with the "Spin" prefix, in case of concurrent access to the database, will spin until the operation is applied. The methods with the "Wait" prefix will wait on a mutex.
If you run multiple simulations that write on the same database, it is recommended to use the "Wait" prefixed methods. Otherwise, if the access to the database is unique, using "Spin" methods will speed up database access.
The database is opened in the constructor, and closed in the deconstructor.
Definition at line 35 of file sqlite-output.h.
ns3::SQLiteOutput::SQLiteOutput | ( | const std::string & | name | ) |
SQLiteOutput constructor.
name | database name |
Definition at line 21 of file sqlite-output.cc.
References m_db, and NS_ABORT_MSG_UNLESS.
ns3::SQLiteOutput::~SQLiteOutput | ( | ) |
Destructor.
Definition at line 28 of file sqlite-output.cc.
References m_db, and NS_ABORT_MSG_UNLESS.
bool ns3::SQLiteOutput::Bind | ( | sqlite3_stmt * | stmt, |
int | pos, | ||
const double & | value ) const |
Bind a value to a sqlite statement.
stmt | Sqlite statement |
pos | Position of the bind argument inside the statement |
value | Value to bind |
Definition at line 131 of file sqlite-output.cc.
bool ns3::SQLiteOutput::Bind | ( | sqlite3_stmt * | stmt, |
int | pos, | ||
const int & | value ) const |
Bind a value to a sqlite statement.
stmt | Sqlite statement |
pos | Position of the bind argument inside the statement |
value | Value to bind |
Definition at line 179 of file sqlite-output.cc.
bool ns3::SQLiteOutput::Bind | ( | sqlite3_stmt * | stmt, |
int | pos, | ||
const long & | value ) const |
Bind a value to a sqlite statement.
stmt | Sqlite statement |
pos | Position of the bind argument inside the statement |
value | Value to bind |
Definition at line 147 of file sqlite-output.cc.
bool ns3::SQLiteOutput::Bind | ( | sqlite3_stmt * | stmt, |
int | pos, | ||
const long long & | value ) const |
Bind a value to a sqlite statement.
stmt | Sqlite statement |
pos | Position of the bind argument inside the statement |
value | Value to bind |
Definition at line 155 of file sqlite-output.cc.
bool ns3::SQLiteOutput::Bind | ( | sqlite3_stmt * | stmt, |
int | pos, | ||
const std::string & | value ) const |
Bind a value to a sqlite statement.
stmt | Sqlite statement |
pos | Position of the bind argument inside the statement |
value | Value to bind |
Definition at line 187 of file sqlite-output.cc.
bool ns3::SQLiteOutput::Bind | ( | sqlite3_stmt * | stmt, |
int | pos, | ||
const T & | value ) const |
Bind a value to a sqlite statement.
stmt | Sqlite statement |
pos | Position of the bind argument inside the statement |
value | Value to bind |
Definition at line 114 of file sqlite-output.cc.
References NS_FATAL_ERROR.
bool ns3::SQLiteOutput::Bind | ( | sqlite3_stmt * | stmt, |
int | pos, | ||
const Time & | value ) const |
Bind a value to a sqlite statement.
stmt | Sqlite statement |
pos | Position of the bind argument inside the statement |
value | Value to bind |
Definition at line 123 of file sqlite-output.cc.
bool ns3::SQLiteOutput::Bind | ( | sqlite3_stmt * | stmt, |
int | pos, | ||
const uint16_t & | value ) const |
Bind a value to a sqlite statement.
stmt | Sqlite statement |
pos | Position of the bind argument inside the statement |
value | Value to bind |
Definition at line 163 of file sqlite-output.cc.
bool ns3::SQLiteOutput::Bind | ( | sqlite3_stmt * | stmt, |
int | pos, | ||
const uint32_t & | value ) const |
Bind a value to a sqlite statement.
stmt | Sqlite statement |
pos | Position of the bind argument inside the statement |
value | Value to bind |
Definition at line 139 of file sqlite-output.cc.
bool ns3::SQLiteOutput::Bind | ( | sqlite3_stmt * | stmt, |
int | pos, | ||
const uint8_t & | value ) const |
Bind a value to a sqlite statement.
stmt | Sqlite statement |
pos | Position of the bind argument inside the statement |
value | Value to bind |
Definition at line 171 of file sqlite-output.cc.
|
staticprotected |
Check any error in the db.
db | Database |
rc | Sqlite return code |
cmd | Command |
hardExit | if true, will exit the program |
Definition at line 267 of file sqlite-output.cc.
References Error().
Referenced by SpinExec(), SpinExec(), SpinExec(), WaitExec(), WaitExec(), WaitExec(), and WaitPrepare().
|
staticprotected |
Fail, printing an error message from sqlite.
db | Database |
cmd | Command |
Definition at line 261 of file sqlite-output.cc.
References NS_ABORT_MSG.
Referenced by CheckError().
int ns3::SQLiteOutput::RetrieveColumn | ( | sqlite3_stmt * | stmt, |
int | pos ) const |
Retrieve a value from an executed statement.
stmt | sqlite statement |
pos | Column position |
Definition at line 91 of file sqlite-output.cc.
uint32_t ns3::SQLiteOutput::RetrieveColumn | ( | sqlite3_stmt * | stmt, |
int | pos ) const |
Retrieve a value from an executed statement.
stmt | sqlite statement |
pos | Column position |
Definition at line 99 of file sqlite-output.cc.
double ns3::SQLiteOutput::RetrieveColumn | ( | sqlite3_stmt * | stmt, |
int | pos ) const |
Retrieve a value from an executed statement.
stmt | sqlite statement |
pos | Column position |
Definition at line 107 of file sqlite-output.cc.
T ns3::SQLiteOutput::RetrieveColumn | ( | sqlite3_stmt * | stmt, |
int | pos ) const |
Retrieve a value from an executed statement.
stmt | sqlite statement |
pos | Column position |
Definition at line 83 of file sqlite-output.cc.
References NS_FATAL_ERROR.
void ns3::SQLiteOutput::SetJournalInMemory | ( | ) |
Instruct SQLite to store the journal in memory.
May lead to data losses in case of unexpected program exits.
Definition at line 37 of file sqlite-output.cc.
References NS_LOG_FUNCTION, and SpinExec().
bool ns3::SQLiteOutput::SpinExec | ( | const std::string & | cmd | ) | const |
Execute a command until the return value is OK or an ERROR.
Ignore errors due to concurrency, the method will repeat the command instead
cmd | Command |
Definition at line 44 of file sqlite-output.cc.
References m_db, and SpinExec().
Referenced by SetJournalInMemory(), SpinExec(), and SpinExec().
|
staticprotected |
Execute a command ignoring concurrency problems, retrying instead.
db | Database |
cmd | Command |
Definition at line 285 of file sqlite-output.cc.
References CheckError(), SpinFinalize(), SpinPrepare(), and SpinStep().
|
staticprotected |
Execute a Prepared Statement Object ignoring concurrency problems, retrying instead.
db | Database |
stmt | Prepared Statement Object |
Definition at line 311 of file sqlite-output.cc.
References CheckError(), SpinFinalize(), and SpinStep().
bool ns3::SQLiteOutput::SpinExec | ( | sqlite3_stmt * | stmt | ) | const |
Execute a command until the return value is OK or an ERROR.
stmt | SQLite statement |
Definition at line 50 of file sqlite-output.cc.
References CheckError(), m_db, and SpinExec().
|
static |
Finalize a statement until the result is ok or an error.
Ignores concurrency errors; it will retry instead of failing.
stmt | Statement |
Definition at line 236 of file sqlite-output.cc.
Referenced by ns3::SqliteDataOutput::SqliteOutputCallback::~SqliteOutputCallback(), ns3::SqliteDataOutput::Output(), SpinExec(), SpinExec(), WaitExec(), and WaitExec().
|
staticprotected |
Preparing a command ignoring concurrency problems, retrying instead.
db | Database |
stmt | Statement |
cmd | Command to prepare |
Definition at line 212 of file sqlite-output.cc.
bool ns3::SQLiteOutput::SpinPrepare | ( | sqlite3_stmt ** | stmt, |
const std::string & | cmd ) const |
Prepare a statement.
stmt | Sqlite statement |
cmd | Command to prepare inside the statement |
Definition at line 76 of file sqlite-output.cc.
References m_db, and SpinPrepare().
Referenced by SpinExec(), SpinPrepare(), and WaitExec().
|
static |
Reset a statement until the result is ok or an error.
Ignores concurrency errors: it will retry instead of failing.
stmt | Statement |
Definition at line 248 of file sqlite-output.cc.
Referenced by ns3::SqliteDataOutput::Output(), ns3::SqliteDataOutput::SqliteOutputCallback::OutputSingleton(), ns3::SqliteDataOutput::SqliteOutputCallback::OutputSingleton(), ns3::SqliteDataOutput::SqliteOutputCallback::OutputSingleton(), ns3::SqliteDataOutput::SqliteOutputCallback::OutputSingleton(), and ns3::SqliteDataOutput::SqliteOutputCallback::OutputSingleton().
|
static |
Execute a step operation on a statement until the result is ok or an error.
Ignores concurrency errors; it will retry instead of failing.
stmt | Statement |
Definition at line 224 of file sqlite-output.cc.
Referenced by ns3::SqliteDataOutput::Output(), ns3::SqliteDataOutput::SqliteOutputCallback::OutputSingleton(), ns3::SqliteDataOutput::SqliteOutputCallback::OutputSingleton(), ns3::SqliteDataOutput::SqliteOutputCallback::OutputSingleton(), ns3::SqliteDataOutput::SqliteOutputCallback::OutputSingleton(), ns3::SqliteDataOutput::SqliteOutputCallback::OutputSingleton(), SpinExec(), SpinExec(), WaitExec(), and WaitExec().
bool ns3::SQLiteOutput::WaitExec | ( | const std::string & | cmd | ) | const |
Execute a command, waiting on a mutex.
cmd | Command to be executed |
Definition at line 57 of file sqlite-output.cc.
References CheckError(), m_db, and WaitExec().
Referenced by WaitExec(), and WaitExec().
|
protected |
Execute a command, waiting on a mutex.
db | Database |
cmd | Command |
Definition at line 347 of file sqlite-output.cc.
References CheckError(), m_mutex, SpinFinalize(), SpinPrepare(), and SpinStep().
|
protected |
Execute a statement, waiting on a mutex.
db | Database |
stmt | Statement |
Definition at line 326 of file sqlite-output.cc.
References CheckError(), m_mutex, SpinFinalize(), and SpinStep().
bool ns3::SQLiteOutput::WaitExec | ( | sqlite3_stmt * | stmt | ) | const |
Execute a command, waiting on a mutex.
stmt | Sqlite3 statement to be executed |
Definition at line 64 of file sqlite-output.cc.
References m_db, and WaitExec().
|
protected |
Prepare a statement, waiting on a mutex.
db | Database |
stmt | Statement |
cmd | Command to prepare |
Definition at line 193 of file sqlite-output.cc.
References CheckError(), and m_mutex.
bool ns3::SQLiteOutput::WaitPrepare | ( | sqlite3_stmt ** | stmt, |
const std::string & | cmd ) const |
Prepare a statement, waiting on a mutex.
stmt | Sqlite statement |
cmd | Command to prepare inside the statement |
Definition at line 70 of file sqlite-output.cc.
References m_db, and WaitPrepare().
Referenced by WaitPrepare().
|
private |
Database pointer.
Definition at line 221 of file sqlite-output.h.
Referenced by SQLiteOutput(), ~SQLiteOutput(), SpinExec(), SpinExec(), SpinPrepare(), WaitExec(), WaitExec(), and WaitPrepare().
|
private |
Database name.
Definition at line 219 of file sqlite-output.h.
|
mutableprivate |
Mutex.
Definition at line 220 of file sqlite-output.h.
Referenced by WaitExec(), WaitExec(), and WaitPrepare().