OpenFCST: The open-source Fuel Cell Simulation Toolbox
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
FcstUtilities::FCSTdatabase Class Reference

This class is for interfacing with SQL databases, for the purpose of accessing and storing simulation results. More...

#include <fcst_db.h>

Public Member Functions

 FCSTdatabase ()
 Constructor. More...
 
virtual ~FCSTdatabase ()
 Destructor. More...
 
bool connect (const std::string &db_path_, const bool &create_if_does_not_exist=false)
 Function for connecting to SQL database. More...
 
bool connect ()
 Function for connecting to default SQL database, located at fcst_root/main_db. More...
 
bool disconnect ()
 Function for disconnecting to SQL database. More...
 
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 conditions. More...
 
bool has_data (const std::string &model_name, const DatabaseOC &OC, const double &tolerance)
 Function for testing if db that you are connected to has data for a given model and operating conditions. More...
 
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. More...
 
std::vector< std::vector
< double > > 
get_data (const std::string &model_name, const DatabaseOC &OC, const double &tolerance)
 Function for getting numerical information for a given model and operating conditions. More...
 
std::vector< std::vector
< double > > 
get_data (const std::string &model_name, const DatabaseOC &OC, const double &tolerance, const std::string &orderby)
 Function for getting numerical information for a given model and operating conditions. More...
 
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. More...
 
std::vector< std::vector
< std::string > > 
request (const std::string &sqlCmd)
 Wrapper Function for performing direct SQL requests. More...
 
bool clear_data (const std::string &model_name, const DatabaseOC &OC)
 Function for clearing data for a given model and operating conditions. More...
 
void cleanUp ()
 Function which performs database cleanup, removing erroneous data occurring from SQL errors. More...
 

Private Member Functions

std::string make_new_head_entry (const std::string &model_name, const DatabaseOC &OC)
 
bool create_table (const std::string &table_name, const std::vector< std::string > &column_names)
 
bool fill_empty_table (const std::string &table_name, const std::vector< std::vector< double >> &data, const std::vector< std::string > &column_names)
 
std::string find_table (const std::string &model_name, const DatabaseOC &OC)
 
std::string find_table (const std::string &model_name, const DatabaseOC &OC, const double &tolerance)
 
bool find_matching_head_term (const std::vector< std::string > &OC, const std::vector< std::string > &head_line, const double &tolerance)
 
bool request_no_callback (const std::string &sqlCmd)
 
bool request (const std::string &sqlCmd, const bool &useCallBack, int lockCounter=max_lock)
 
bool create_new_db (const std::string &filePath)
 Function for creating a new default db. More...
 

Static Private Member Functions

static int callback (void *db_, int argc, char **argv, char **azColName)
 

Private Attributes

sqlite3 * db
 
bool connected
 
std::string db_path
 
std::vector< std::vector
< std::string > > 
temp
 
unsigned int max_milli
 
double lock_time
 

Static Private Attributes

static const int max_lock
 

Detailed Description

This class is for interfacing with SQL databases, for the purpose of accessing and storing simulation results.

Usage details

This class provides a database interface for storing generic numerical data to disc. It provides tolerance based selection of data based upon explicit operating conditions.

*
* //Create the database interface and address
* std::string address = "/some/location"
*
* //Connect to that database
* testDb.connect(address);
*
* //Alternatively if the following line will create the database if it does not exist
* bool create_if_does_not_exit = true;
* testDb.connect(address, create_if_does_not_exit);
*
* //We want to get some data from the database
* //First we must describe the model and operating conditions
* //for which we would like some data
* std::string model_name = "test";
* OC.add_param("OCV", 1);
* OC.add_param("lambda", 3);
*
* //Data is stored in tables, i.e. 2d array or a vector of vector of doubles
* std::vector<std::vector<double>> data;
*
* //Check if the database had data matching our OC within a 10% tolerance
* if (testDb.has_data(model_name, OC, 0.1)){
* //If it does retrieve this data
* data= testDb.get_data(model_name, OC, 0.1);
* }
*
* //We wish to store data to the database
* //First we create the relevant name and OC
* std::string new_model_name = "test";
* new_OC.add_param("OCV", 4);
* new_OC.add_param("lambda",12);
*
*
* //Create some data we wish to save.
* std::vector<std::vector<double>> data_to_save = create_some_data();
*
* //Column names correspond to the data we are saving,
* //in this case our data table looks like this:
* //
* // |_x_|_phi_M_|
* // |0.1| 0.1 |
* // |0.3| 0.2 |
* // |0.5| 0.4 |
* // |0.6| 0.4 |
* // |1.1| 0.5 |
* // |2.1| 0.7 |
* //
* //To submit the data we must also let the database know what the column names are.
*
* std::vector<std::string> columnNames;
* columnNames.push_back("x");
* columnNames.push_back("phi_M");
*
* //Now we are ready to store our data in the database.
* testDb.commit_data(new_model_name, new_OC, columnNames, data_to_save);
*
* //It is good practice to always close the database connection
* //once we are done submitting/retrieving data.
* testDb.close()
*
*
Author
Philip Wardlaw
Date
2014

Constructor & Destructor Documentation

FcstUtilities::FCSTdatabase::FCSTdatabase ( )

Constructor.

virtual FcstUtilities::FCSTdatabase::~FCSTdatabase ( )
virtual

Destructor.

Member Function Documentation

static int FcstUtilities::FCSTdatabase::callback ( void *  db_,
int  argc,
char **  argv,
char **  azColName 
)
inlinestaticprivate

References db, and temp.

void FcstUtilities::FCSTdatabase::cleanUp ( )

Function which performs database cleanup, removing erroneous data occurring from SQL errors.

Currently the function removes invalid HEAD entries (entries which are missing corresponding data tables).

bool FcstUtilities::FCSTdatabase::clear_data ( const std::string &  model_name,
const DatabaseOC OC 
)

Function for clearing data for a given model and operating conditions.

Takes model name and operating conditions as arguments. OC is a 2d array (vector vector) in which for a given line the first element is the operating condition name and the second element is the value.

Returns true if data is successfully deleted.

See Unit test "testCommitData" for usage demo.

bool FcstUtilities::FCSTdatabase::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.

Takes model name, operating conditions, data column titles, and data as arguments. OC is a 2d array (vector vector) in which for a given line the first element is the operating condition name and the second element is the value. Column_names is a list of column that pertains to the numerical data stored in argument data. Data is a 2d array (vector vector) of doubles containing the data we whish to store.

Returns an empty vector(vector(double)) if no data exists.

Throws exceptions if invalid column names are provided (only characters "123456789abcdefghijklmnopqrstuvwxyz " are permitted, i.e. alpha numeric and whitespace).

Throws exception if data or column_name vectors are empty or do not match in dimension.

See Unit test "testCommitData" for usage demo.

bool FcstUtilities::FCSTdatabase::connect ( const std::string &  db_path_,
const bool &  create_if_does_not_exist = false 
)

Function for connecting to SQL database.

Takes database file path as argument. Returns true if connection successful

bool FcstUtilities::FCSTdatabase::connect ( )

Function for connecting to default SQL database, located at fcst_root/main_db.

Returns true if connection successful

bool FcstUtilities::FCSTdatabase::create_new_db ( const std::string &  filePath)
private

Function for creating a new default db.

bool FcstUtilities::FCSTdatabase::create_table ( const std::string &  table_name,
const std::vector< std::string > &  column_names 
)
private
bool FcstUtilities::FCSTdatabase::disconnect ( )

Function for disconnecting to SQL database.

Returns true if disconnection successful

bool FcstUtilities::FCSTdatabase::fill_empty_table ( const std::string &  table_name,
const std::vector< std::vector< double >> &  data,
const std::vector< std::string > &  column_names 
)
private
bool FcstUtilities::FCSTdatabase::find_matching_head_term ( const std::vector< std::string > &  OC,
const std::vector< std::string > &  head_line,
const double &  tolerance 
)
private
std::string FcstUtilities::FCSTdatabase::find_table ( const std::string &  model_name,
const DatabaseOC OC 
)
private
std::string FcstUtilities::FCSTdatabase::find_table ( const std::string &  model_name,
const DatabaseOC OC,
const double &  tolerance 
)
private
std::vector<std::vector<double> > FcstUtilities::FCSTdatabase::get_data ( const std::string &  model_name,
const DatabaseOC OC 
)

Function for getting numerical information for a given model and operating conditions.

Takes model name and operating conditions as arguments. OC is an object of DatabaseOC type.

N.b: returns an potentially unordered (by rows) list of results. See third overloading of this function.

Returns an empty vector(vector(double)) if no data exists.

See Unit test "testGetData" for usage demo.

std::vector<std::vector<double> > FcstUtilities::FCSTdatabase::get_data ( const std::string &  model_name,
const DatabaseOC OC,
const double &  tolerance 
)

Function for getting numerical information for a given model and operating conditions.

Takes model name, operating conditions, and a tolerance as arguments. OC is an object of DatabaseOC type. Differences in operating condition values will be compared with provided tolerance (0.0 - 1.0).

N.b: returns an potentially unordered (by rows) list of results. See third overloading of this function.

Returns an empty vector(vector(double)) if no data exists.

See Unit test "testGetData" for usage demo.

std::vector<std::vector<double> > FcstUtilities::FCSTdatabase::get_data ( const std::string &  model_name,
const DatabaseOC OC,
const double &  tolerance,
const std::string &  orderby 
)

Function for getting numerical information for a given model and operating conditions.

Takes model name, operating conditions, orderby string,and a tolerance as arguments. OC is an object of DatabaseOC type. Differences in operating condition values will be compared with provided tolerance (0.0 - 1.0). Orderby is the name of the column that the results will be ordered by before being returned.

Returns an empty vector(vector(double)) if no data exists.

See Unit test "testGetData" for usage demo.

bool FcstUtilities::FCSTdatabase::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 conditions.

Takes model name and operating conditions as arguments. OC is an object of DatabaseOC type.

Returns true if data exists.

See Unit test "testHasData" for usage demo.

bool FcstUtilities::FCSTdatabase::has_data ( const std::string &  model_name,
const DatabaseOC OC,
const double &  tolerance 
)

Function for testing if db that you are connected to has data for a given model and operating conditions.

Takes model name, operating conditions, and a tolerance as arguments. OC is an object of DatabaseOC type. Differences in operating condition values will be compared with provided tolerance (0.0 - 1.0). Returns true if data exists.

See Unit test "testHasData" for usage demo.

std::string FcstUtilities::FCSTdatabase::make_new_head_entry ( const std::string &  model_name,
const DatabaseOC OC 
)
private
std::vector<std::vector<std::string> > FcstUtilities::FCSTdatabase::request ( const std::string &  sqlCmd)

Wrapper Function for performing direct SQL requests.

Takes a SQL statement as argument. Returns the results of the request in a 2d array (vector vector) of strings.

Note: If you don't know SQL then this is probably not the function for you, try another.

See Unit test "testRequest" for usage demo.

bool FcstUtilities::FCSTdatabase::request ( const std::string &  sqlCmd,
const bool &  useCallBack,
int  lockCounter = max_lock 
)
private
bool FcstUtilities::FCSTdatabase::request_no_callback ( const std::string &  sqlCmd)
private

Member Data Documentation

bool FcstUtilities::FCSTdatabase::connected
private
sqlite3* FcstUtilities::FCSTdatabase::db
private

Referenced by callback().

std::string FcstUtilities::FCSTdatabase::db_path
private
double FcstUtilities::FCSTdatabase::lock_time
private
const int FcstUtilities::FCSTdatabase::max_lock
staticprivate
unsigned int FcstUtilities::FCSTdatabase::max_milli
private
std::vector<std::vector<std::string> > FcstUtilities::FCSTdatabase::temp
private

Referenced by callback().


The documentation for this class was generated from the following file: