A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sqlite-output.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Natale Patriciello <natale.patriciello@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
18#ifndef SQLITE_OUTPUT_H
19#define SQLITE_OUTPUT_H
20
21#include "ns3/simple-ref-count.h"
22
23#include <mutex>
24#include <sqlite3.h>
25#include <string>
26
27namespace ns3
28{
29
30/**
31 * \ingroup stats
32 *
33 * \brief A C++ interface towards an SQLITE database
34 *
35 * The class is able to execute commands, and retrieve results, from an SQLITE
36 * database. The methods with the "Spin" prefix, in case of concurrent access
37 * to the database, will spin until the operation is applied. The methods with
38 * the "Wait" prefix will wait on a mutex.
39 *
40 * If you run multiple simulations that write on the same database, it is
41 * recommended to use the "Wait" prefixed methods. Otherwise, if the access to
42 * the database is unique, using "Spin" methods will speed up database access.
43 *
44 * The database is opened in the constructor, and closed in the deconstructor.
45 */
46class SQLiteOutput : public SimpleRefCount<SQLiteOutput>
47{
48 public:
49 /**
50 * \brief SQLiteOutput constructor
51 * \param name database name
52 */
53 SQLiteOutput(const std::string& name);
54
55 /**
56 * Destructor
57 */
59
60 /**
61 * \brief Instruct SQLite to store the journal in memory. May lead to data losses
62 * in case of unexpected program exits.
63 */
64 void SetJournalInMemory();
65
66 /**
67 * \brief Execute a command until the return value is OK or an ERROR
68 *
69 * Ignore errors due to concurrency, the method will repeat the command instead
70 *
71 * \param cmd Command
72 * \return true in case of success
73 */
74 bool SpinExec(const std::string& cmd) const;
75
76 /**
77 * \brief Execute a command until the return value is OK or an ERROR
78 * \param stmt SQLite statement
79 * \return true in case of success
80 */
81 bool SpinExec(sqlite3_stmt* stmt) const;
82
83 /**
84 * \brief Execute a command, waiting on a mutex
85 * \param cmd Command to be executed
86 * \return true in case of success
87 */
88 bool WaitExec(const std::string& cmd) const;
89
90 /**
91 * \brief Execute a command, waiting on a mutex
92 * \param stmt Sqlite3 statement to be executed
93 * \return true in case of success
94 */
95 bool WaitExec(sqlite3_stmt* stmt) const;
96
97 /**
98 * \brief Prepare a statement, waiting on a mutex
99 * \param stmt Sqlite statement
100 * \param cmd Command to prepare inside the statement
101 * \return true in case of success
102 */
103 bool WaitPrepare(sqlite3_stmt** stmt, const std::string& cmd) const;
104
105 /**
106 * \brief Prepare a statement
107 * \param stmt Sqlite statement
108 * \param cmd Command to prepare inside the statement
109 * \return true in case of success
110 */
111 bool SpinPrepare(sqlite3_stmt** stmt, const std::string& cmd) const;
112
113 /**
114 * \brief Bind a value to a sqlite statement
115 * \param stmt Sqlite statement
116 * \param pos Position of the bind argument inside the statement
117 * \param value Value to bind
118 * \return true on success, false otherwise
119 */
120 template <typename T>
121 bool Bind(sqlite3_stmt* stmt, int pos, const T& value) const;
122
123 /**
124 * \brief Retrieve a value from an executed statement
125 * \param stmt sqlite statement
126 * \param pos Column position
127 * \return the value from the executed statement
128 */
129 template <typename T>
130 T RetrieveColumn(sqlite3_stmt* stmt, int pos) const;
131
132 /**
133 * \brief Execute a step operation on a statement until the result is ok or an error
134 *
135 * Ignores concurrency errors; it will retry instead of failing.
136 *
137 * \param stmt Statement
138 * \return Sqlite error core
139 */
140
141 static int SpinStep(sqlite3_stmt* stmt);
142 /**
143 * \brief Finalize a statement until the result is ok or an error
144 *
145 * Ignores concurrency errors; it will retry instead of failing.
146 *
147 * \param stmt Statement
148 * \return Sqlite error code
149 */
150 static int SpinFinalize(sqlite3_stmt* stmt);
151
152 /**
153 * \brief Reset a statement until the result is ok or an error
154 *
155 * Ignores concurrency errors: it will retry instead of failing.
156 * \param stmt Statement
157 * \return the return code of reset
158 */
159 static int SpinReset(sqlite3_stmt* stmt);
160
161 protected:
162 /**
163 * \brief Execute a command, waiting on a mutex
164 * \param db Database
165 * \param cmd Command
166 * \return Sqlite error code
167 */
168 int WaitExec(sqlite3* db, const std::string& cmd) const;
169
170 /**
171 * \brief Execute a statement, waiting on a mutex
172 * \param db Database
173 * \param stmt Statement
174 * \return Sqlite error code
175 */
176 int WaitExec(sqlite3* db, sqlite3_stmt* stmt) const;
177
178 /**
179 * \brief Prepare a statement, waiting on a mutex
180 * \param db Database
181 * \param stmt Statement
182 * \param cmd Command to prepare
183 * \return Sqlite error code
184 */
185 int WaitPrepare(sqlite3* db, sqlite3_stmt** stmt, const std::string& cmd) const;
186
187 /**
188 * \brief Execute a command ignoring concurrency problems, retrying instead
189 * \param db Database
190 * \param cmd Command
191 * \return Sqlite error code
192 */
193 static int SpinExec(sqlite3* db, const std::string& cmd);
194
195 /**
196 * \brief Execute a Prepared Statement Object ignoring concurrency problems, retrying instead
197 * \param db Database
198 * \param stmt Prepared Statement Object
199 * \return Sqlite error code
200 */
201 static int SpinExec(sqlite3* db, sqlite3_stmt* stmt);
202
203 /**
204 * \brief Preparing a command ignoring concurrency problems, retrying instead
205 * \param db Database
206 * \param stmt Statement
207 * \param cmd Command to prepare
208 * \return Sqlite error code
209 */
210 static int SpinPrepare(sqlite3* db, sqlite3_stmt** stmt, const std::string& cmd);
211
212 /**
213 * \brief Fail, printing an error message from sqlite
214 * \param db Database
215 * \param cmd Command
216 */
217 [[noreturn]] static void Error(sqlite3* db, const std::string& cmd);
218
219 /**
220 * \brief Check any error in the db
221 * \param db Database
222 * \param rc Sqlite return code
223 * \param cmd Command
224 * \param hardExit if true, will exit the program
225 * \return true in case of error, false otherwise
226 */
227 static bool CheckError(sqlite3* db, int rc, const std::string& cmd, bool hardExit);
228
229 private:
230 std::string m_dBname; //!< Database name
231 mutable std::mutex m_mutex; //!< Mutex
232 sqlite3* m_db{nullptr}; //!< Database pointer
233};
234
235} // namespace ns3
236#endif
A C++ interface towards an SQLITE database.
Definition: sqlite-output.h:47
sqlite3 * m_db
Database pointer.
std::string m_dBname
Database name.
bool SpinExec(const std::string &cmd) const
Execute a command until the return value is OK or an ERROR.
static bool CheckError(sqlite3 *db, int rc, const std::string &cmd, bool hardExit)
Check any error in the db.
~SQLiteOutput()
Destructor.
static int SpinStep(sqlite3_stmt *stmt)
Execute a step operation on a statement until the result is ok or an error.
void SetJournalInMemory()
Instruct SQLite to store the journal in memory.
T RetrieveColumn(sqlite3_stmt *stmt, int pos) const
Retrieve a value from an executed statement.
bool Bind(sqlite3_stmt *stmt, int pos, const T &value) const
Bind a value to a sqlite statement.
static int SpinReset(sqlite3_stmt *stmt)
Reset a statement until the result is ok or an error.
static int SpinFinalize(sqlite3_stmt *stmt)
Finalize a statement until the result is ok or an error.
std::mutex m_mutex
Mutex.
bool WaitExec(const std::string &cmd) const
Execute a command, waiting on a mutex.
static void Error(sqlite3 *db, const std::string &cmd)
Fail, printing an error message from sqlite.
bool SpinPrepare(sqlite3_stmt **stmt, const std::string &cmd) const
Prepare a statement.
bool WaitPrepare(sqlite3_stmt **stmt, const std::string &cmd) const
Prepare a statement, waiting on a mutex.
A template-based reference counting class.
Every class exported by the ns3 library is enclosed in the ns3 namespace.