QNLP  v1.0
db_helper.hpp
Go to the documentation of this file.
1 
9 #ifndef QNLP_DBHELPER
10 #define QNLP_DBHELPER
11 
12 #include <iostream>
13 #include <sqlite3.h>
14 #include <string>
15 
16 //#############################################################################
17 
18 //Return-type checking macro for SQL statements; compares the result x with that of y, and exits with the respective error-code if they differ. Macro wraps inline to return linenumber and filename for resultant cause.
19 #define SQL_RETURN_CHECK(x,y) sql_return_check(x, y, __FILE__, __LINE__)
20 inline void sql_return_check( const int ret_code,
21  const int expected_code,
22  const char* fileName,
23  const std::size_t lineNum){
24 #ifdef SQL_ERR_CHECK
25  if ( ret_code != expected_code ) {
26  std::cerr << "SQL error: FILE: " << fileName << "\t LINE: " << lineNum << "\t RETURN_CODE: " << ret_code << std::endl;
27  std::exit(ret_code);
28  }
29 #endif
30 }
31 
32 //#############################################################################
33 
34 namespace QNLP{
35  class DBHelper{
36  public:
37 
38  DBHelper();
39  DBHelper(const std::string filename) ;
40  ~DBHelper();
41 
48  int openDB(const std::string filename);
49 
53  void closeDB();
54 
60  sqlite3* getDBRef();
61 
67  std::string getFilename();
68 
69  private:
70  //Pointer to database object
71  sqlite3* db_ptr;
72  std::string db_filename;
73  };
74 }
75 #endif
std::string db_filename
Definition: db_helper.hpp:72
void closeDB()
Closes the database if the DB pointer is open.
Definition: db_helper.cpp:23
void sql_return_check(const int ret_code, const int expected_code, const char *fileName, const std::size_t lineNum)
Definition: db_helper.hpp:20
sqlite3 * getDBRef()
Returns a pointer to the DB object.
Definition: db_helper.cpp:47
int openDB(const std::string filename)
Opens the requested database, and gives the return code of the operation. If the DB pointer has alrea...
Definition: db_helper.cpp:32
std::string getFilename()
Get the name of the opened database.
Definition: db_helper.cpp:51
sqlite3 * db_ptr
Definition: db_helper.hpp:71