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/stats-config.h"
23 #include "ns3/simple-ref-count.h"
24 #include <sqlite3.h>
25 #include <string>
26 #ifdef HAVE_SEMAPHORE_H
27 #include <semaphore.h>
28 #else
29 #error "Can't compile this unit without <semaphore.h>"
30 #endif
31 
32 namespace ns3 {
33 
50 class SQLiteOutput : public SimpleRefCount <SQLiteOutput>
51 {
52 public:
58  SQLiteOutput (const std::string &name, const std::string &semName);
62  ~SQLiteOutput ();
63 
68  void SetJournalInMemory ();
69 
78  bool SpinExec (const std::string &cmd) const;
79 
85  bool SpinExec (sqlite3_stmt *stmt) const;
91  bool WaitExec (const std::string &cmd) const;
92 
98  bool WaitExec (sqlite3_stmt *stmt) const;
99 
106  bool WaitPrepare (sqlite3_stmt **stmt, const std::string &cmd) const;
107 
114  bool SpinPrepare (sqlite3_stmt **stmt, const std::string &cmd) const;
115 
122  template<typename T>
123  bool Bind (sqlite3_stmt *stmt, int pos, const T &value) const;
124 
130  template<typename T>
131  T RetrieveColumn (sqlite3_stmt *stmt, int pos) const;
132 
141  static int SpinStep (sqlite3_stmt *stmt);
150  static int SpinFinalize (sqlite3_stmt *stmt);
151 
159  static int SpinReset (sqlite3_stmt *stmt);
160 
161 protected:
168  int WaitExec (sqlite3 *db, const std::string &cmd) const;
175  int WaitExec (sqlite3 *db, sqlite3_stmt *stmt) const;
183  int WaitPrepare (sqlite3 *db, sqlite3_stmt **stmt, const std::string &cmd) const;
184 
191  static int SpinExec (sqlite3 *db, const std::string &cmd);
192 
193  static int SpinExec (sqlite3 *db, sqlite3_stmt *stmt);
201  static int SpinPrepare (sqlite3 *db, sqlite3_stmt **stmt, const std::string &cmd);
202 
208  [[ noreturn ]] static void Error (sqlite3 *db, const std::string &cmd);
218  static bool CheckError (sqlite3 *db, int rc, const std::string &cmd,
219  sem_t *sem, bool hardExit);
220 
221 private:
222  std::string m_dBname;
223  std::string m_semName;
224  sqlite3 *m_db {
225  nullptr
226  };
227 };
228 
229 } // namespace ns3
230 #endif
static bool CheckError(sqlite3 *db, int rc, const std::string &cmd, sem_t *sem, bool hardExit)
Check any error in the db.
A C++ interface towards an SQLITE database.
Definition: sqlite-output.h:50
static void Error(sqlite3 *db, const std::string &cmd)
Fail, printing an error message from sqlite.
SQLiteOutput(const std::string &name, const std::string &semName)
SQLiteOutput constructor.
bool SpinExec(const std::string &cmd) const
Execute a command until the return value is OK or an ERROR.
cmd
Definition: second.py:35
void SetJournalInMemory()
Instruct SQLite to store the journal in memory.
static int SpinStep(sqlite3_stmt *stmt)
Execute a step operation on a statement until the result is ok or an error.
std::string m_semName
System semaphore name.
static int SpinFinalize(sqlite3_stmt *stmt)
Finalize a statement until the result is ok or an error.
bool WaitPrepare(sqlite3_stmt **stmt, const std::string &cmd) const
Prepare a statement, waiting on a system semaphore.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool SpinPrepare(sqlite3_stmt **stmt, const std::string &cmd) const
Prepare a statement.
T RetrieveColumn(sqlite3_stmt *stmt, int pos) const
Retrieve a value from an executed statement.
~SQLiteOutput()
Destructor.
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.
sqlite3 * m_db
Database pointer.
bool WaitExec(const std::string &cmd) const
Execute a command, waiting on a system semaphore.
std::string m_dBname
Database name.
A template-based reference counting class.