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 //
4 // FCST: Fuel Cell Simulation Toolbox
5 //
6 // Copyright (C) 2006-13 by Energy Systems Design Laboratory, University of Alberta
7 //
8 // This software is distributed under the MIT License.
9 // For more information, see the README file in /doc/LICENSE
10 //
11 // - Class: FCSTdatabase
12 // - Description: A class for interfacing with SQL databases,
13 // for the purpose of accessing and storing simulation results.
14 // - Developers: Philip Wardlaw
15 //
16 //---------------------------------------------------------------------------
17 
18 #ifndef FCSTDATABASE_H_
19 #define FCSTDATABASE_H_
20 #include <iostream>
21 #include <ctime>
22 #include <fcst_utilities.h>
23 #include <string>
24 #include <sqlite3.h>
25 #include <vector>
26 #include <stdexcept>
27 #include <fstream>
28 
29 namespace FcstUtilities
30 {
36  class DatabaseOC;
37 
42  class FCSTdatabase {
43  public:
47  FCSTdatabase();
51  virtual ~FCSTdatabase();
52 
57  bool connect(const std::string& db_path);
58 
63  bool connect();
64 
69  bool disconnect();
70 
79  bool has_data(const std::string& model_name, const DatabaseOC& OC);
80 
89  bool has_data(const std::string& model_name, const DatabaseOC& OC, const double& tolerance);
90 
101  std::vector<std::vector<double>> get_data(const std::string& model_name, const DatabaseOC& OC);
102 
114  std::vector<std::vector<double>> get_data(const std::string& model_name, const DatabaseOC& OC, const double& tolerance);
115 
126  std::vector<std::vector<double>> get_data(const std::string& model_name, const DatabaseOC& OC, const double& tolerance, const std::string& orderby);
127 
139  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);
140 
150  std::vector<std::vector<std::string>> request(const std::string& sqlCmd);
151 
161  bool clear_data(const std::string& model_name, const DatabaseOC& OC);
162 
163  private:
164  /*
165  * SQLite object for all our db interactions
166  */
167  sqlite3 *db;
168 
169  /*
170  * Status variables
171  */
172  bool connected;
173  std::string db_path;
174 
175  /*
176  * Temporary variable for storing requested data
177  */
178  std::vector<std::vector<std::string>> temp;
179 
180  /*
181  * Static call back function used for interfacing with sqlite3_exec
182  * Required by public function request
183  */
184  static int callback(void *db_, int argc, char **argv, char **azColName){
185 
186  FCSTdatabase* db = reinterpret_cast<FCSTdatabase*>(db_);
187  std::vector<std::string> line_temp;
188  std::string str_temp;
189  for(int i=0; i<argc; i++){
190  str_temp = argv[i] ? argv[i] : "NULL";
191  line_temp.push_back(std::string(str_temp));
192 
193  }
194  db->temp.push_back(line_temp);
195  return 0;
196  }
197 
198  /*
199  * Function for creating new entry in HEAD table
200  * Required by public function commit_data
201  */
202  std::string make_new_head_entry(const std::string& model_name, const DatabaseOC& OC);
203 
204  /*
205  * Function for creating new entry in HEAD table
206  * Required by public function commit_data
207  */
208  bool create_table(const std::string& table_name, const std::vector<std::string>& column_names);
209 
210  /*
211  * Function for filling an empty table with data
212  * Required by public function commit_data
213  */
214  bool fill_empty_table(const std::string& table_name, const std::vector<std::vector<double>>& data, const std::vector<std::string>& column_names);
215 
216  /*
217  * Function for finding a the table name for a given model name and OC
218  * Required by public function has_data and clear_data
219  */
220  std::string find_table(const std::string& model_name, const DatabaseOC& OC);
221 
222  std::string find_table(const std::string& model_name, const DatabaseOC& OC, const double& tolerance);
223 
224 
225  bool find_matching_head_term(const std::vector<std::string>& OC, const std::vector<std::string>& head_line, const double& tolerance);
226 
227 
228  /*
229  * Private wrapper function for making requests that don't require an answer other than a yes or no
230  * The main difference between this function and request is that here callback is simply 0, i.e. we do not read the returned data
231  * Required by several public and private functions
232  */
233  bool request_no_callback(const std::string& sqlCmd);
234 
235  /*
236  * Private function that implements the all request functionality.
237  * Request(sqlCmd) and request_no_callback functions rely on this function.
238  * Standard variable lockCounter is used to recursively call this function in the event of a database lock (for reattempts)
239  * Bool useCallBack determines if we read the returned data
240  */
241  bool request(const std::string& sqlCmd, const bool& useCallBack, int lockCounter =3);
242  };
243 
244 
250  class DatabaseOC{
251 
252  //FCSTdatabase is allowed to access private members of this class
253  friend class FCSTdatabase;
254 
255  public:
259  DatabaseOC();
260 
267  bool add_param(const std::string& name, const double& value);
268 
269 
270  private:
271 
272  bool add_param(const std::string&, const std::string& );
275  std::vector<std::vector<std::string>> param_data;
276 
277  };
278 
279 }
280 
281 
282 
283 #endif /* FCSTDATABASE_H_ */