AMO-Tools-Suite  v.0.9.0
Set of tools for calculating energy efficiency in industrial equipment
All Classes Namespaces Files Functions Variables Enumerations Friends Macros Pages
SQLite.h
1 #ifndef AMO_LIBRARY_SQLITEWRAPPER_H
2 #define AMO_LIBRARY_SQLITEWRAPPER_H
3 
4 #include <sqlite3.h>
5 #include <memory>
6 #include <string>
7 #include <vector>
8 #include <functional>
9 #include <calculator/losses/GasFlueGasMaterial.h>
10 #include <calculator/motor/MotorData.h>
11 #include <calculator/pump/PumpData.h>
12 
16 class GasCompositions;
18 class Atmosphere;
19 class WallLosses;
20 class MotorData;
21 class PumpData;
22 
23 class SQLiteWrapper
24 {
25 protected:
26  SQLiteWrapper(std::shared_ptr<sqlite3> const &db);
27 
28  SQLiteWrapper(std::string const &db_name, bool init_db = false);
29 
30  virtual ~SQLiteWrapper(){};
31 
32  int execute_command(std::string const &command_buffer) const;
33 
34  int prepare_statement(sqlite3_stmt *&stmt, std::string const &stmt_buffer) const;
35 
36  int bind_value(sqlite3_stmt *stmt, int const stmt_insert_col_index, std::string const &text_buffer) const;
37 
38  int bind_value(sqlite3_stmt *stmt, int const stmt_insert_col_index, int const int_to_insert) const;
39 
40  int bind_value(sqlite3_stmt *stmt, int const stmt_insert_col_index, double const double_to_insert) const;
41 
42  int bind_value(sqlite3_stmt *stmt, int const stmt_insert_col_index) const;
43 
44  int bind_value(sqlite3_stmt *stmt, int const stmt_insert_col_index, const bool value_to_insert) const;
45 
46  int bind_foreign_key(sqlite3_stmt *stmt, int const stmt_insert_col_index, int const int_to_insert) const;
47 
48  bool step_validity(int const rc) const;
49 
50  int step_command(sqlite3_stmt *stmt) const;
51 
52  int reset_command(sqlite3_stmt *stmt) const;
53 
54  void begin_transaction() const;
55 
56  void commit_transaction() const;
57 
58  static std::string convert_text(const unsigned char *text);
59 
60  static Motor::EfficiencyClass convert_motor_efficiency_class(int efficiencyClass);
61 
62  static Motor::LineFrequency convert_motor_line_frequency(int lineFrequency);
63 
64  template <typename T>
65  std::vector<T> get_all_objects(sqlite3_stmt *stmt, std::function<T(sqlite3_stmt *)> cb) const
66  {
67  std::vector<T> retVal;
68  if (m_db)
69  {
70  while (step_command(stmt) == SQLITE_ROW)
71  {
72  retVal.push_back(cb(stmt));
73  }
74  }
75  return retVal;
76  }
77 
78  template <typename T>
79  T get_object(sqlite3_stmt *stmt, int const id, std::function<T(sqlite3_stmt *)> cb) const
80  {
81  if (m_db)
82  {
83  bind_value(stmt, 1, id);
84  int rc = step_command(stmt);
85  step_validity(rc);
86  if (rc == SQLITE_ROW)
87  {
88  T retVal = cb(stmt);
89  reset_command(stmt);
90  return retVal;
91  }
92  reset_command(stmt);
93  //throw std::runtime_error("Invalid command during get_object");
94  throw std::runtime_error("Invalid command during get_object. SQLite return code: " + std::to_string(rc));
95  }
96  throw std::runtime_error("No valid database connection");
97  }
98 
99 private:
100  sqlite3 *m_connection = nullptr;
101  std::shared_ptr<sqlite3> m_db;
102 };
103 
104 class SQLite : SQLiteWrapper
105 {
106 public:
107  // Open the DB and prepare for writing data
108  // Create all of the tables on construction
109  SQLite(std::string const &db_name, bool init_db = false);
110 
111  // Close database and free prepared statements
112  virtual ~SQLite();
113 
114  std::vector<SolidLoadChargeMaterial> getSolidLoadChargeMaterials() const;
115  SolidLoadChargeMaterial getSolidLoadChargeMaterialById(int id) const;
116  std::vector<SolidLoadChargeMaterial> getCustomSolidLoadChargeMaterials() const;
117  bool insertSolidLoadChargeMaterials(SolidLoadChargeMaterial const &material);
118  bool deleteSolidLoadChargeMaterial(int id);
119  bool updateSolidLoadChargeMaterial(SolidLoadChargeMaterial const &material);
120 
121  std::vector<GasLoadChargeMaterial> getGasLoadChargeMaterials() const;
122  GasLoadChargeMaterial getGasLoadChargeMaterialById(int id) const;
123  std::vector<GasLoadChargeMaterial> getCustomGasLoadChargeMaterials() const;
124  bool insertGasLoadChargeMaterials(GasLoadChargeMaterial const &material);
125  bool deleteGasLoadChargeMaterial(int id);
126  bool updateGasLoadChargeMaterial(const GasLoadChargeMaterial &material);
127 
128  std::vector<LiquidLoadChargeMaterial> getLiquidLoadChargeMaterials() const;
129  LiquidLoadChargeMaterial getLiquidLoadChargeMaterialById(int id) const;
130  std::vector<LiquidLoadChargeMaterial> getCustomLiquidLoadChargeMaterials() const;
131  bool insertLiquidLoadChargeMaterials(LiquidLoadChargeMaterial const &material);
132  bool deleteLiquidLoadChargeMaterial(int id);
133  bool updateLiquidLoadChargeMaterial(const LiquidLoadChargeMaterial &material);
134 
135  std::vector<SolidLiquidFlueGasMaterial> getSolidLiquidFlueGasMaterials() const;
136  SolidLiquidFlueGasMaterial getSolidLiquidFlueGasMaterialById(int id) const;
137  std::vector<SolidLiquidFlueGasMaterial> getCustomSolidLiquidFlueGasMaterials() const;
138  bool insertSolidLiquidFlueGasMaterial(SolidLiquidFlueGasMaterial const &material) const;
139  bool deleteSolidLiquidFlueGasMaterial(int id);
140  bool updateSolidLiquidFlueGasMaterial(SolidLiquidFlueGasMaterial const &material);
141 
142  std::vector<GasCompositions> getGasFlueGasMaterials() const;
143  GasCompositions getGasFlueGasMaterialById(int id) const;
144  std::vector<GasCompositions> getCustomGasFlueGasMaterials() const;
145  bool insertGasFlueGasMaterial(GasCompositions const &material) const;
146  bool deleteGasFlueGasMaterial(int id);
147  bool updateGasFlueGasMaterial(GasCompositions const &material);
148 
149  std::vector<Atmosphere> getAtmosphereSpecificHeat() const;
150  Atmosphere getAtmosphereSpecificHeatById(int id) const;
151  std::vector<Atmosphere> getCustomAtmosphereSpecificHeat() const;
152  bool insertAtmosphereSpecificHeat(Atmosphere const &material);
153  bool updateAtmosphereSpecificHeat(Atmosphere const &material);
154  bool deleteAtmosphereSpecificHeat(int id);
155 
156  std::vector<WallLosses> getWallLossesSurface() const;
157  std::vector<WallLosses> getCustomWallLossesSurface() const;
158  WallLosses getWallLossesSurfaceById(int id) const;
159  bool insertWallLossesSurface(WallLosses const &material);
160  bool deleteWallLossesSurface(int id);
161  bool updateWallLossesSurface(WallLosses const &material);
162 
163  std::vector<MotorData> getMotorData() const;
164  std::vector<MotorData> getCustomMotorData() const;
165  MotorData getMotorDataById(int id) const;
166  bool insertMotorData(MotorData const &motor);
167  bool deleteMotorData(int id);
168  bool updateMotorData(MotorData const &motor);
169 
170  std::vector<PumpData> getPumpData() const;
171  std::vector<PumpData> getCustomPumpData() const;
172  PumpData getPumpDataById(int id) const;
173  bool insertPumpData(PumpData const &pump);
174  bool deletePumpData(int id);
175  bool updatePumpData(PumpData const &pump);
176 
177 private:
178  // returns true if the material id falls in the default material id range
179  inline bool isDefaultMaterial(const int id, std::size_t const defaultMaterialsSize)
180  {
181  return static_cast<std::size_t>(id) <= defaultMaterialsSize;
182  }
183 
184  sqlite3_stmt *m_solid_load_charge_materials_insert_stmt = nullptr;
185  sqlite3_stmt *m_solid_load_charge_materials_select_stmt = nullptr;
186  sqlite3_stmt *m_solid_load_charge_materials_select_single_stmt = nullptr;
187  sqlite3_stmt *m_solid_load_charge_materials_select_custom_stmt = nullptr;
188  sqlite3_stmt *m_solid_load_charge_materials_update_stmt = nullptr;
189  sqlite3_stmt *m_solid_load_charge_materials_delete_stmt = nullptr;
190 
191  sqlite3_stmt *m_gas_load_charge_materials_insert_stmt = nullptr;
192  sqlite3_stmt *m_gas_load_charge_materials_select_stmt = nullptr;
193  sqlite3_stmt *m_gas_load_charge_materials_select_single_stmt = nullptr;
194  sqlite3_stmt *m_gas_load_charge_materials_select_custom_stmt = nullptr;
195  sqlite3_stmt *m_gas_load_charge_materials_update_stmt = nullptr;
196  sqlite3_stmt *m_gas_load_charge_materials_delete_stmt = nullptr;
197 
198  sqlite3_stmt *m_liquid_load_charge_materials_insert_stmt = nullptr;
199  sqlite3_stmt *m_liquid_load_charge_materials_select_stmt = nullptr;
200  sqlite3_stmt *m_liquid_load_charge_materials_select_single_stmt = nullptr;
201  sqlite3_stmt *m_liquid_load_charge_materials_select_custom_stmt = nullptr;
202  sqlite3_stmt *m_liquid_load_charge_materials_update_stmt = nullptr;
203  sqlite3_stmt *m_liquid_load_charge_materials_delete_stmt = nullptr;
204 
205  sqlite3_stmt *m_solid_liquid_flue_gas_materials_insert_stmt = nullptr;
206  sqlite3_stmt *m_solid_liquid_flue_gas_materials_select_stmt = nullptr;
207  sqlite3_stmt *m_solid_liquid_flue_gas_materials_select_single_stmt = nullptr;
208  sqlite3_stmt *m_solid_liquid_flue_gas_materials_select_custom_stmt = nullptr;
209  sqlite3_stmt *m_solid_liquid_flue_gas_materials_update_stmt = nullptr;
210  sqlite3_stmt *m_solid_liquid_flue_gas_materials_delete_stmt = nullptr;
211 
212  sqlite3_stmt *m_gas_flue_gas_materials_insert_stmt = nullptr;
213  sqlite3_stmt *m_gas_flue_gas_materials_select_stmt = nullptr;
214  sqlite3_stmt *m_gas_flue_gas_materials_select_single_stmt = nullptr;
215  sqlite3_stmt *m_gas_flue_gas_materials_select_custom_stmt = nullptr;
216  sqlite3_stmt *m_gas_flue_gas_materials_update_stmt = nullptr;
217  sqlite3_stmt *m_gas_flue_gas_materials_delete_stmt = nullptr;
218 
219  sqlite3_stmt *m_atmosphere_specific_heat_insert_stmt = nullptr;
220  sqlite3_stmt *m_atmosphere_specific_heat_select_stmt = nullptr;
221  sqlite3_stmt *m_atmosphere_specific_heat_select_single_stmt = nullptr;
222  sqlite3_stmt *m_atmosphere_specific_heat_select_custom_stmt = nullptr;
223  sqlite3_stmt *m_atmosphere_specific_heat_update_stmt = nullptr;
224  sqlite3_stmt *m_atmosphere_specific_heat_delete_stmt = nullptr;
225 
226  sqlite3_stmt *m_wall_losses_surface_insert_stmt = nullptr;
227  sqlite3_stmt *m_wall_losses_surface_select_stmt = nullptr;
228  sqlite3_stmt *m_wall_losses_surface_select_single_stmt = nullptr;
229  sqlite3_stmt *m_wall_losses_surface_select_custom_stmt = nullptr;
230  sqlite3_stmt *m_wall_losses_surface_update_stmt = nullptr;
231  sqlite3_stmt *m_wall_losses_surface_delete_stmt = nullptr;
232 
233  sqlite3_stmt *m_motor_data_insert_stmt = nullptr;
234  sqlite3_stmt *m_motor_data_select_stmt = nullptr;
235  sqlite3_stmt *m_motor_data_select_single_stmt = nullptr;
236  sqlite3_stmt *m_motor_data_select_custom_stmt = nullptr;
237  sqlite3_stmt *m_motor_data_update_stmt = nullptr;
238  sqlite3_stmt *m_motor_data_delete_stmt = nullptr;
239 
240  sqlite3_stmt *m_pump_data_insert_stmt = nullptr;
241  sqlite3_stmt *m_pump_data_select_stmt = nullptr;
242  sqlite3_stmt *m_pump_data_select_single_stmt = nullptr;
243  sqlite3_stmt *m_pump_data_select_custom_stmt = nullptr;
244  sqlite3_stmt *m_pump_data_update_stmt = nullptr;
245  sqlite3_stmt *m_pump_data_delete_stmt = nullptr;
246 
247  void create_select_stmt();
248 
249  void create_update_and_delete_stmt();
250 
251  void create_insert_stmt();
252 
253  void create_tables();
254 
255  bool insert_solid_load_charge_materials(SolidLoadChargeMaterial const &material);
256 
257  bool insert_gas_load_charge_materials(GasLoadChargeMaterial const &material);
258 
259  bool insert_liquid_load_charge_materials(LiquidLoadChargeMaterial const &material);
260 
261  bool insert_solid_liquid_flue_gas_materials(SolidLiquidFlueGasMaterial const &material);
262 
263  bool insert_gas_flue_gas_materials(GasCompositions const &comps);
264 
265  bool insert_atmosphere_specific_heat(Atmosphere const &material);
266 
267  bool insert_wall_losses_surface(WallLosses const &surface);
268 
269  bool insert_motor_data(MotorData const &m);
270 
271  bool insert_pump_data(PumpData const &pump);
272 
273  void insert_default_data();
274 
275  std::vector<SolidLoadChargeMaterial> get_default_solid_load_charge_materials();
276 
277  std::vector<GasLoadChargeMaterial> get_default_gas_load_charge_materials();
278 
279  std::vector<LiquidLoadChargeMaterial> get_default_liquid_load_charge_materials();
280 
281  std::vector<SolidLiquidFlueGasMaterial> get_default_solid_liquid_flue_gas_materials();
282 
283  std::vector<GasCompositions> get_default_gas_flue_gas_materials();
284 
285  std::vector<Atmosphere> get_default_atmosphere_specific_heat();
286 
287  std::vector<WallLosses> get_default_wall_losses_surface();
288 
289  std::vector<MotorData> get_default_motor_data();
290 
291  std::vector<PumpData> get_default_pump_data();
292 };
293 
294 #endif //AMO_LIBRARY_SQLITEWRAPPER_H
Definition: SQLite.h:104