OpenFCST: The open-source Fuel Cell Simulation Toolbox
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fcst_db.h
Go to the documentation of this file.
1 //---------------------------------------------------------------------------
2 //
3 // FCST: Fuel Cell Simulation Toolbox
4 //
5 // Copyright (C) 2006-13 by Energy Systems Design Laboratory, University of Alberta
6 //
7 // This software is distributed under the MIT License.
8 // For more information, see the README file in /doc/LICENSE
9 //
10 // - Class: FCSTdatabase
11 // - Description: A class for interfacing with SQL databases,
12 // for the purpose of accessing and storing simulation results.
13 // - Developers: Philip Wardlaw
14 //
15 //---------------------------------------------------------------------------
16 
17 #ifndef FCSTDATABASE_H_
18 #define FCSTDATABASE_H_
19 
20 #include <utils/fcst_utilities.h>
21 #include <utils/logging.h>
22 //SQLITE3
23 #include <sqlite3.h>
24 
25 //STL
26 #include <iostream>
27 #include <ctime>
28 #include <string>
29 #include <cstddef> // std::size_t
30 #include <vector>
31 #include <stdexcept>
32 #include <fstream>
33 #include <chrono>
34 #include <thread>
35 #include <random>
36 
37 //Deal.ii
38 
39 
40 
41 //Boost
42 #include <boost/algorithm/string.hpp>
43 
44 namespace FcstUtilities
45 {
51  class DatabaseOC;
52 
134  class FCSTdatabase {
135  public:
139  FCSTdatabase();
143  virtual ~FCSTdatabase();
144 
149  bool connect(const std::string& db_path_, const bool& create_if_does_not_exist = false);
150 
155  bool connect();
156 
161  bool disconnect();
162 
171  bool has_data(const std::string& model_name, const DatabaseOC& OC);
172 
181  bool has_data(const std::string& model_name, const DatabaseOC& OC, const double& tolerance);
182 
193  std::vector<std::vector<double>> get_data(const std::string& model_name, const DatabaseOC& OC);
194 
206  std::vector<std::vector<double>> get_data(const std::string& model_name, const DatabaseOC& OC, const double& tolerance);
207 
218  std::vector<std::vector<double>> get_data(const std::string& model_name, const DatabaseOC& OC, const double& tolerance, const std::string& orderby);
219 
235  bool commit_data(const std::string& model_name, const DatabaseOC& OC, const std::vector<std::string>& column_names, const std::vector<std::vector<double>>& data);
236 
246  std::vector<std::vector<std::string>> request(const std::string& sqlCmd);
247 
257  bool clear_data(const std::string& model_name, const DatabaseOC& OC);
258 
265  void cleanUp();
266 
267  private:
268  /*
269  * SQLite object for all our db interactions
270  */
271  sqlite3 *db;
272 
273  /*
274  * Status variables
275  */
276  bool connected;
277  std::string db_path;
278 
279  /*
280  * Temporary variable for storing requested data
281  */
282  std::vector<std::vector<std::string>> temp;
283 
284  /*
285  * Static call back function used for interfacing with sqlite3_exec
286  * Required by public function request
287  */
288  static int callback(void *db_, int argc, char **argv, char **azColName){
289 
290  FCSTdatabase* db = reinterpret_cast<FCSTdatabase*>(db_);
291  std::vector<std::string> line_temp;
292  std::string str_temp;
293  for(int i=0; i<argc; i++){
294  str_temp = argv[i] ? argv[i] : "NULL";
295  line_temp.push_back(std::string(str_temp));
296 
297  }
298  db->temp.push_back(line_temp);
299  return 0;
300  }
301 
302  /*
303  * Function for creating new entry in HEAD table
304  * Required by public function commit_data
305  */
306  std::string make_new_head_entry(const std::string& model_name, const DatabaseOC& OC);
307 
308  /*
309  * Function for creating new entry in HEAD table
310  * Required by public function commit_data
311  */
312  bool create_table(const std::string& table_name, const std::vector<std::string>& column_names);
313 
314  /*
315  * Function for filling an empty table with data
316  * Required by public function commit_data
317  */
318  bool fill_empty_table(const std::string& table_name, const std::vector<std::vector<double>>& data, const std::vector<std::string>& column_names);
319 
320  /*
321  * Function for finding a the table name for a given model name and OC
322  * Required by public function has_data and clear_data
323  */
324  std::string find_table(const std::string& model_name, const DatabaseOC& OC);
325 
326  std::string find_table(const std::string& model_name, const DatabaseOC& OC, const double& tolerance);
327 
328 
329  bool find_matching_head_term(const std::vector<std::string>& OC, const std::vector<std::string>& head_line, const double& tolerance);
330 
331 
332  /*
333  * Private wrapper function for making requests that don't require an answer other than a yes or no
334  * The main difference between this function and request is that here callback is simply 0, i.e. we do not read the returned data
335  * Required by several public and private functions
336  */
337  bool request_no_callback(const std::string& sqlCmd);
338 
339  /*
340  * Variables associated with database locking.
341  */
342  const static int max_lock;
343  unsigned int max_milli;
344  double lock_time;
345 
346  /*
347  * Private function that implements the all request functionality.
348  * Request(sqlCmd) and request_no_callback functions rely on this function.
349  * Standard variable lockCounter is used to recursively call this function in the event of a database lock (for reattempts)
350  * Bool useCallBack determines if we read the returned data
351  */
352 
353  bool request(const std::string& sqlCmd, const bool& useCallBack, int lockCounter = max_lock);
354 
355 
359  bool create_new_db(const std::string& filePath);
360  };
361 
362 
368  class DatabaseOC{
369 
370  //FCSTdatabase is allowed to access private members of this class
371  friend class FCSTdatabase;
372 
373  public:
377  DatabaseOC();
378 
385  bool add_param(const std::string& name, const double& value);
386  bool add_param(const std::string&, const std::string& );
387 
391  inline void clear(){
392  num_param = 0;
393  param_data.clear();
394  }
395 
396 
403  bool compare(const DatabaseOC& other, const double& tolerance);
404 
405  private:
406 
407 
410  std::vector<std::vector<std::string>> param_data;
411 
412  };
413 
414 }
415 
416 
417 
418 #endif /* FCSTDATABASE_H_ */
std::string find_table(const std::string &model_name, const DatabaseOC &OC)
std::vector< std::vector< double > > get_data(const std::string &model_name, const DatabaseOC &OC)
Function for getting numerical information for a given model and operating conditions.
sqlite3 * db
Definition: fcst_db.h:271
This class is for storing a list of up to 10 parameters and is used as a way of informing the FCSTdat...
Definition: fcst_db.h:368
bool request_no_callback(const std::string &sqlCmd)
bool disconnect()
Function for disconnecting to SQL database.
bool fill_empty_table(const std::string &table_name, const std::vector< std::vector< double >> &data, const std::vector< std::string > &column_names)
virtual ~FCSTdatabase()
Destructor.
bool has_data(const std::string &model_name, const DatabaseOC &OC)
Function for testing if db that you are connected to has data for a given model and operating conditi...
unsigned int max_milli
Definition: fcst_db.h:343
void clear()
Function for clearing parameter data so one can reuse the same object more than once.
Definition: fcst_db.h:391
DatabaseOC()
Constructor.
bool connect()
Function for connecting to default SQL database, located at fcst_root/main_db.
int num_param
Definition: fcst_db.h:408
void cleanUp()
Function which performs database cleanup, removing erroneous data occurring from SQL errors...
bool create_new_db(const std::string &filePath)
Function for creating a new default db.
int max_param
Definition: fcst_db.h:409
bool connected
Definition: fcst_db.h:276
std::vector< std::vector< std::string > > request(const std::string &sqlCmd)
Wrapper Function for performing direct SQL requests.
bool clear_data(const std::string &model_name, const DatabaseOC &OC)
Function for clearing data for a given model and operating conditions.
bool commit_data(const std::string &model_name, const DatabaseOC &OC, const std::vector< std::string > &column_names, const std::vector< std::vector< double >> &data)
Function for committing numerical information for a given model and operating conditions.
static int callback(void *db_, int argc, char **argv, char **azColName)
Definition: fcst_db.h:288
bool find_matching_head_term(const std::vector< std::string > &OC, const std::vector< std::string > &head_line, const double &tolerance)
std::string make_new_head_entry(const std::string &model_name, const DatabaseOC &OC)
std::string db_path
Definition: fcst_db.h:277
std::vector< std::vector< std::string > > param_data
Definition: fcst_db.h:410
static const int max_lock
Definition: fcst_db.h:342
double lock_time
Definition: fcst_db.h:344
std::vector< std::vector< std::string > > temp
Definition: fcst_db.h:282
bool create_table(const std::string &table_name, const std::vector< std::string > &column_names)
bool add_param(const std::string &name, const double &value)
Function for adding a parameter (name and value pair) to the list of up to 5 parameters.
This class is for interfacing with SQL databases, for the purpose of accessing and storing simulation...
Definition: fcst_db.h:134
bool compare(const DatabaseOC &other, const double &tolerance)
Function for performing a tolerance based comparison of OC object with another OC object...