CuteHMI - Data Acquisition (CuteHMI.DataAcquisition.0)
HistoryTable.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_DATAACQUISITION_0_INCLUDE_CUTEHMI_DATAACQUISITION_INTERNAL_HISTORYTABLE_HPP
2 #define H_EXTENSIONS_CUTEHMI_DATAACQUISITION_0_INCLUDE_CUTEHMI_DATAACQUISITION_INTERNAL_HISTORYTABLE_HPP
3 
4 #include "common.hpp"
5 #include "TableObject.hpp"
6 #include "TagCache.hpp"
7 #include "TableNameTraits.hpp"
8 
9 #include <QHash>
10 
11 #include <limits>
12 
13 namespace cutehmi {
14 namespace dataacquisition {
15 namespace internal {
16 
17 template <typename T>
19  public TableObject
20 {
21  public:
22  typedef T type;
23 
24  struct Tuple
25  {
26  T open = T();
27  T close = T();
32  int count = 0;
33  };
34 
36 
38 
39  void insert(const TuplesContainer & tuples);
40 
41  protected:
42  struct ColumnValues
43  {
45  QVariantList open;
46  QVariantList close;
47  QVariantList min;
48  QVariantList max;
49  QVariantList openTime;
50  QVariantList closeTime;
51  QVariantList count;
52 
53  ColumnValues(const TuplesContainer & tuples);
54  };
55 
56  TagCache * tagCache() const;
57 
58  private:
59  struct Members
60  {
62  };
63 
64  MPtr<Members> m;
65 };
66 
67 template <typename T>
69  TableObject(schema, parent),
70  m(new Members{tag})
71 {
72 }
73 
74 template <typename T>
76 {
77  ColumnValues columnValues(tuples);
78  QString tableName = TableNameTraits<T>::Affixed("history");
79 
80  worker([this, columnValues, tableName](QSqlDatabase & db) {
81  if (db.driverName() == "QPSQL") {
82  QSqlQuery query(db);
83  CUTEHMI_DEBUG("Storing '" << tableName << "' values...");
84  QVariantList tagIds;
85  for (QStringList::const_iterator tagName = columnValues.tagName.begin(); tagName != columnValues.tagName.end(); ++tagName)
86  tagIds.append(tagCache()->getId(*tagName, db));
87 
88  query.prepare(QString("INSERT INTO %1.%2(tag_id, open, close, min, max, open_time, close_time, count) VALUES (:tagId, :open, :close, :min, :max, :open_time, :close_time, :count)").arg(schema()->name()).arg(tableName));
89  query.bindValue(":tagId", tagIds);
90  query.bindValue(":open", columnValues.open);
91  query.bindValue(":close", columnValues.close);
92  query.bindValue(":min", columnValues.min);
93  query.bindValue(":max", columnValues.max);
94  query.bindValue(":open_time", columnValues.openTime);
95  query.bindValue(":close_time", columnValues.closeTime);
96  query.bindValue(":count", columnValues.count);
97  query.execBatch();
98 
99  pushError(query.lastError());
100  query.finish();
101  } else if (db.driverName() == "QSQLITE") {
102  QSqlQuery query(db);
103  CUTEHMI_DEBUG("Storing '" << tableName << "' values...");
104  QVariantList tagIds;
105  for (QStringList::const_iterator tagName = columnValues.tagName.begin(); tagName != columnValues.tagName.end(); ++tagName)
106  tagIds.append(tagCache()->getId(*tagName, db));
107 
108  query.prepare(QString("INSERT INTO [%1.%2](tag_id, open, close, min, max, open_time, close_time, count) VALUES (:tagId, :open, :close, :min, :max, :open_time, :close_time, :count)").arg(schema()->name()).arg(tableName));
109  query.bindValue(":tagId", tagIds);
110  query.bindValue(":open", columnValues.open);
111  query.bindValue(":close", columnValues.close);
112  query.bindValue(":min", columnValues.min);
113  query.bindValue(":max", columnValues.max);
114  query.bindValue(":open_time", columnValues.openTime);
115  query.bindValue(":close_time", columnValues.closeTime);
116  query.bindValue(":count", columnValues.count);
117  query.execBatch();
118 
119  pushError(query.lastError());
120  query.finish();
121  } else
122  emit errored(CUTEHMI_ERROR(tr("Driver '%1' is not supported.").arg(db.driverName())));
123  })->work();
124 }
125 
126 template <typename T>
128 {
129  for (typename HistoryTable<T>::TuplesContainer::const_iterator it = tuples.begin(); it != tuples.end(); ++it) {
130  tagName.append(it.key());
131  open.append(it->open);
132  close.append(it->close);
133  min.append(it->min);
134  max.append(it->max);
135  openTime.append(it->openTime);
136  closeTime.append(it->closeTime);
137  count.append(it->count);
138  }
139 }
140 
141 template <typename T>
143 {
144  return m->tagCache;
145 }
146 
147 }
148 }
149 }
150 
151 #endif
152 
153 //(c)C: Copyright © 2020, Michał Policht <michal@policht.pl>. All rights reserved.
154 //(c)C: This file is a part of CuteHMI.
155 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
156 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
157 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
cutehmi::dataacquisition::internal::HistoryTable::HistoryTable
HistoryTable(TagCache *tagCache, Schema *schema, QObject *parent=nullptr)
Definition: HistoryTable.hpp:68
cutehmi::dataacquisition::internal::HistoryTable::Tuple::openTime
QDateTime openTime
Definition: HistoryTable.hpp:30
cutehmi::dataacquisition::internal::HistoryTable::TuplesContainer
QHash< QString, Tuple > TuplesContainer
Definition: HistoryTable.hpp:35
cutehmi::dataacquisition::internal::HistoryTable::tagCache
TagCache * tagCache() const
Definition: HistoryTable.hpp:142
cutehmi::dataacquisition::internal::HistoryTable::ColumnValues::ColumnValues
ColumnValues(const TuplesContainer &tuples)
Definition: HistoryTable.hpp:127
cutehmi::dataacquisition::internal::TagCache
Definition: TagCache.hpp:13
QHash::begin
QHash::iterator begin()
cutehmi::dataacquisition::internal::HistoryTable
Definition: HistoryTable.hpp:18
cutehmi::dataacquisition::internal::HistoryTable::ColumnValues::min
QVariantList min
Definition: HistoryTable.hpp:47
cutehmi::dataacquisition::internal::HistoryTable::ColumnValues::tagName
QStringList tagName
Definition: HistoryTable.hpp:44
cutehmi::dataacquisition::internal::TableObject
Definition: TableObject.hpp:13
QSqlDatabase
cutehmi::dataacquisition::internal::HistoryTable::ColumnValues::open
QVariantList open
Definition: HistoryTable.hpp:45
cutehmi::MPtr< Members >
cutehmi::dataacquisition::internal::HistoryTable::ColumnValues::close
QVariantList close
Definition: HistoryTable.hpp:46
QObject
cutehmi::dataacquisition::internal::HistoryTable::ColumnValues::closeTime
QVariantList closeTime
Definition: HistoryTable.hpp:50
cutehmi
QString
cutehmi::dataacquisition::internal::HistoryTable::type
T type
Definition: HistoryTable.hpp:22
cutehmi::dataacquisition::internal::TableNameTraits
Definition: TableNameTraits.hpp:11
cutehmi::dataacquisition::internal::HistoryTable::Tuple::max
T max
Definition: HistoryTable.hpp:29
cutehmi::dataacquisition::internal::HistoryTable::ColumnValues::count
QVariantList count
Definition: HistoryTable.hpp:51
std::numeric_limits::min
T min(T... args)
cutehmi::dataacquisition::internal::TableObject::schema
Schema schema
Definition: TableObject.hpp:22
cutehmi::dataacquisition::internal::HistoryTable::ColumnValues::max
QVariantList max
Definition: HistoryTable.hpp:48
cutehmi::dataacquisition::internal::HistoryTable::Tuple::min
T min
Definition: HistoryTable.hpp:28
std::internal
T internal(T... args)
QHash::const_iterator
cutehmi::dataacquisition::internal::HistoryTable::ColumnValues
Definition: HistoryTable.hpp:42
cutehmi::dataacquisition::internal::HistoryTable::Tuple::closeTime
QDateTime closeTime
Definition: HistoryTable.hpp:31
cutehmi::dataacquisition::Schema
Database schema.
Definition: Schema.hpp:13
cutehmi::dataacquisition::internal::HistoryTable::Tuple::open
T open
Definition: HistoryTable.hpp:26
QDateTime
cutehmi::dataacquisition::internal::HistoryTable::Tuple
Definition: HistoryTable.hpp:24
std::numeric_limits::max
T max(T... args)
cutehmi::dataacquisition::internal::HistoryTable::insert
void insert(const TuplesContainer &tuples)
Definition: HistoryTable.hpp:75
QHash
QSqlDatabase::driverName
QString driverName() const const
QObject::parent
QObject * parent() const const
cutehmi::dataacquisition::internal::HistoryTable::ColumnValues::openTime
QVariantList openTime
Definition: HistoryTable.hpp:49
cutehmi::dataacquisition::internal::HistoryTable::Tuple::count
int count
Definition: HistoryTable.hpp:32
QStringList
QHash::end
QHash::iterator end()
cutehmi::dataacquisition::internal::HistoryTable::Tuple::close
T close
Definition: HistoryTable.hpp:27