A Discrete-Event Network Simulator
API
sqlite-output.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2018 Natale Patriciello <natale.patriciello@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19 #ifndef SQLITE_OUTPUT_H
20 #define SQLITE_OUTPUT_H
21 
22 #include "ns3/simple-ref-count.h"
23 #include <sqlite3.h>
24 #include <string>
25 #include <mutex>
26 
27 namespace ns3 {
28 
45 class SQLiteOutput : public SimpleRefCount <SQLiteOutput>
46 {
47 public:
52  SQLiteOutput (const std::string &name);
53 
57  ~SQLiteOutput ();
58 
63  void SetJournalInMemory ();
64 
73  bool SpinExec (const std::string &cmd) const;
74 
80  bool SpinExec (sqlite3_stmt *stmt) const;
81 
87  bool WaitExec (const std::string &cmd) const;
88 
94  bool WaitExec (sqlite3_stmt *stmt) const;
95 
102  bool WaitPrepare (sqlite3_stmt **stmt, const std::string &cmd) const;
103 
110  bool SpinPrepare (sqlite3_stmt **stmt, const std::string &cmd) const;
111 
119  template<typename T>
120  bool Bind (sqlite3_stmt *stmt, int pos, const T &value) const;
121 
128  template<typename T>
129  T RetrieveColumn (sqlite3_stmt *stmt, int pos) const;
130 
140  static int SpinStep (sqlite3_stmt *stmt);
149  static int SpinFinalize (sqlite3_stmt *stmt);
150 
158  static int SpinReset (sqlite3_stmt *stmt);
159 
160 protected:
167  int WaitExec (sqlite3 *db, const std::string &cmd) const;
168 
175  int WaitExec (sqlite3 *db, sqlite3_stmt *stmt) const;
176 
184  int WaitPrepare (sqlite3 *db, sqlite3_stmt **stmt, const std::string &cmd) const;
185 
192  static int SpinExec (sqlite3 *db, const std::string &cmd);
193 
200  static int SpinExec (sqlite3 *db, sqlite3_stmt *stmt);
201 
209  static int SpinPrepare (sqlite3 *db, sqlite3_stmt **stmt, const std::string &cmd);
210 
216  [[ noreturn ]] static void Error (sqlite3 *db, const std::string &cmd);
217 
226  static bool CheckError (sqlite3 *db, int rc, const std::string &cmd,
227  bool hardExit);
228 
229 private:
230  std::string m_dBname;
231  mutable std::mutex m_mutex;
232  sqlite3 *m_db {
233  nullptr
234  };
235 };
236 
237 } // namespace ns3
238 #endif
A C++ interface towards an SQLITE database.
Definition: sqlite-output.h:46
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.
SQLiteOutput(const std::string &name)
SQLiteOutput constructor.
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.
cmd
Definition: second.py:35