DAQ  1.0.0
Data Acquisition System (basic demo)
sqlitewrapper.h
Go to the documentation of this file.
1 /*! \brief This file have the interface for SQLiteWrapper class.
2  \file sqlitewrapper.h
3  \author Alvaro Denis <denisacostaq@gmail.com>
4  \date 6/19/2019
5 
6  \copyright
7  \attention <h1><center><strong>COPYRIGHT &copy; 2019 </strong>
8  [<strong>denisacostaq</strong>][denisacostaq-URL].
9  All rights reserved.</center></h1>
10  \attention This file is part of [<strong>DAQs</strong>][DAQs-URL].
11 
12  Redistribution and use in source and binary forms, with or without
13  modification, are permitted provided that the following conditions
14  are met:
15  - 1. Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17  - 2. Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20  - 3. Neither the name of the University nor the names of its contributors
21  may be used to endorse or promote products derived from this software
22  without specific prior written permission.
23 
24  THIS PRODUCT IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  THIS PRODUCT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 
35  [denisacostaq-URL]: https://about.me/denisacostaq "Alvaro Denis Acosta"
36  [DAQs-URL]: https://github.com/denisacostaq/DAQs "DAQs"
37  */
38 #ifndef DATABASESERVER_DATAMODEL_SQLITEWRAPPER_H
39 #define DATABASESERVER_DATAMODEL_SQLITEWRAPPER_H
40 
41 #include <string>
42 
43 #include "src/database-server/data-source/idatasource.h"
44 
45 class sqlite3;
46 class SQLiteWrapper : public IDataSource {
47  public:
48  /**
49  * @brief SQLiteWrapper create a sqlite3 connection.
50  * @throw exception if any error happen.
51  * @param db_path the path to the sqlite3 db file
52  * @sa IDataSource
53  */
54  explicit SQLiteWrapper(const std::string& db_path);
55 
56  /**
57  * @brief ~SQLiteWrapper release the sqlite3 connection.
58  * @sa SQLiteWrapper
59  */
60  ~SQLiteWrapper() override;
61 
62  /**
63  * @brief create_scheme creates the database schema for sqlite3
64  * @details a table with posible variables, a table with variable values and a
65  * relation betwen the two (a variable have many variable values).
66  * @return Err::Ok on success.
67  * @sa IDataSource::create_scheme
68  */
70 
71  /**
72  * @brief add_variable add an entry to the variable table.
73  * @param var variable info.
74  * @return Err::Ok on success.
75  * @sa IDataSource::add_variable
76  */
78 
79  /**
80  * @brief add_variable_value add an entry ti the vriable value table, related
81  * to an entry in the variable table.
82  * @param var variable value info to be inserted.
83  * @return Err::Ok on success.
84  * @sa IDataSource::add_variable_value
85  */
87 
88  /**
89  * @brief fetch_variables get all variables.
90  * @param send_vale the variables will be send in this callback, one at a
91  * time, index is the current value index.
92  * @return Err::Ok on succes
93  */
95  const std::function<void(const Variable& var, size_t index)>&
97 
98  /**
99  * @brief fetch_variable_values get all values related to a variable.
100  * @param var_name variable to get related values from.
101  * @param send_vale get values one at a time from this callback.
102  * @return Err::Ok on success.
103  * @sa IDataSource::fetch_variable_values
104  */
106  const std::string& var_name,
107  const std::function<void(const VarValue&, size_t index)>&
109 
110  /**
111  * @brief count_variable_values count the number of values for a given
112  * variable.
113  * @param var_name variable to count related values from.
114  * @param send_count get the values amount from this callback.
115  * @return Err::Ok on success.
116  */
118  const std::string& var_name,
120 
121  /**
122  * @brief fetch_variable_values get all values related to a variable in a date
123  * range.
124  * @param var_name variable to get related values from.
125  * @param start_data begin of the date range.
126  * @param end_date end of the date range.
127  * @param send_vale get values one at a time from this callback.
128  * @return Err::Ok on success.
129  */
131  const std::string& var_name,
134  const std::function<void(const VarValue& val, size_t index)>&
136 
137  /**
138  * @brief count_variable_values count the number of values for a given
139  * variable in a date range.
140  * @param var_name variable to count related values from.
141  * @param start_data begin of the date range.
142  * @param end_date end of the date range.
143  * @param send_count get the values amount from this callback.
144  * @return Err::Ok on success.
145  */
146  virtual Err count_variable_values(
147  const std::string& var_name,
151 
152  private:
153  sqlite3* db_;
154 };
155 
156 #endif // DATABASESERVER_DATAMODEL_SQLITEWRAPPER_H
Err create_scheme() noexceptoverride
create_scheme creates the database schema for sqlite3
Definition: sqlitewrapper.cc:66
VarValue(Variable &&variable, double &&val, std::uint64_t &&ts)
Definition: varvalue.cc:45
Definition: variable.h:44
Err
Definition: idatasource.h:57
SQLiteWrapper(const std::string &db_path)
SQLiteWrapper create a sqlite3 connection.
Definition: sqlitewrapper.cc:53
~SQLiteWrapper() override
~SQLiteWrapper release the sqlite3 connection.
Definition: sqlitewrapper.cc:64
Err add_variable_value(const VarValue &var) noexceptoverride
add_variable_value add an entry ti the vriable value table, related to an entry in the variable table...
Definition: sqlitewrapper.cc:113
virtual Err count_variable_values(const std::string &var_name, const std::chrono::system_clock::time_point &start_date, const std::chrono::system_clock::time_point &end_date, const std::function< void(size_t count)> &send_count) noexceptoverride
count_variable_values count the number of values for a given variable in a date range.
Definition: sqlitewrapper.cc:342
Err fetch_variables(const std::function< void(const Variable &var, size_t index)> &send_vale) noexceptoverride
fetch_variables get all variables.
Definition: sqlitewrapper.cc:131
Err fetch_variable_values(const std::string &var_name, const std::function< void(const VarValue &, size_t index)> &send_vale) noexceptoverride
fetch_variable_values get all values related to a variable.
virtual Err count_variable_values(const std::string &var_name, const std::chrono::system_clock::time_point &start_date, const std::chrono::system_clock::time_point &end_date, const std::function< void(size_t count)> &send_count) noexcept=0
count_variable_values count all values of a given variable
Err count_variable_values(const std::string &var_name, const std::function< void(size_t count)> &send_count) noexceptoverride
count_variable_values count the number of values for a given variable.
Definition: sqlitewrapper.cc:235
Err add_variable(const Variable &var) noexceptoverride
add_variable add an entry to the variable table.
Definition: sqlitewrapper.cc:98
sqlite3 * db_
Definition: sqlitewrapper.h:153