Natural Language Processing  0.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
OntologyDatabase.h
1 
8 #ifndef ONTOLOGYDATABASE_H
9 #define ONTOLOGYDATABASE_H
10 
11 #include <string>
12 #include <list>
13 #include <QtSql>
14 
15 using namespace std;
16 
17 
18 const QString DBPATH_ONT = "../../Granular-Extractor/ont_db.sqlite";
19 
27 {
28 private:
29  static QSqlDatabase mOntDB;
30  vector<string> vectorQuestionQuery(const string &qrStr);
31 
32 public:
34  void InsertionQuery(const string &qrStr);
35  void QuestionQuery (const string &qrStr, string& result);
36 
38 }; /* ----- end of class OntologyDatabase ----- */
39 
40 
41 /********************************************************************
42  * Class Implementation of OntologyDatabase *
43  ********************************************************************/
44 
45 
47 QSqlDatabase OntologyDatabase::mOntDB = QSqlDatabase::addDatabase("QSQLITE", "ONT_DB");
48 
54 {
55  mOntDB.setDatabaseName (DBPATH_ONT);
56  if( !mOntDB.open() ) {
57  qDebug() << mOntDB.lastError();
58  qFatal( "Failed to connect to database." );
59  throw std::invalid_argument("Error: Invalid database");
60  }
61 // else { qDebug() << "Debug: Database opened." << endl; }
62 }
63 
70 void OntologyDatabase::InsertionQuery(const string& qrStr)
71 {
72  QSqlQuery mLiteQr(this->mOntDB);
73  mLiteQr.prepare(qrStr.c_str());
74  if( !mLiteQr.exec() ) {
75  qDebug() << mLiteQr.lastError();
76  throw std::invalid_argument("Invalid query.");
77  }
78 }
79 
86 void OntologyDatabase::QuestionQuery(const string &qrStr, string& result)
87 {
88  vector<string> v_result = vectorQuestionQuery (qrStr);
89  result = "";
90  for(string R : v_result)
91  result += (R + ", ");
93  result = result.substr (0, result.size () - 2);
94 }
95 
96 
105 vector<string> OntologyDatabase::vectorQuestionQuery(const string &qrStr)
106 {
107  QSqlQuery mLiteQr(this->mOntDB);
108  mLiteQr.prepare (qrStr.c_str ());
109  if( !mLiteQr.exec() ) {
110  qDebug() << mLiteQr.lastError();
111  throw std::invalid_argument("Invalid query.");
112  }
113 
114  vector<string> resultList;
116  while( mLiteQr.next() ) {
117  resultList.push_back (mLiteQr.value(0).toString().toStdString());
118  }
119  return resultList;
120 }
121 
124 {
125 }
126 
127 
128 #endif /* !ONTOLOGYDATABASE_H */
void QuestionQuery(const string &qrStr, string &result)
QuestionQuery.
Definition: OntologyDatabase.h:86
The OntologyDatabase class The class acts as the interface that allows communication with database...
Definition: OntologyDatabase.h:26
~OntologyDatabase()
Upper function.
Definition: OntologyDatabase.h:123
OntologyDatabase()
lower function
Definition: OntologyDatabase.h:53
void InsertionQuery(const string &qrStr)
OntologyDatabase::InsertionQuery Given a string that is a valid SQLite form The function will proceed...
Definition: OntologyDatabase.h:70