CuteHMI - Data Acquisition (CuteHMI.DataAcquisition.0)
RecencyTable.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_DATAACQUISITION_0_INCLUDE_CUTEHMI_DATAACQUISITION_INTERNAL_RECENCYTABLE_HPP
2 #define H_EXTENSIONS_CUTEHMI_DATAACQUISITION_0_INCLUDE_CUTEHMI_DATAACQUISITION_INTERNAL_RECENCYTABLE_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 value = T();
28  };
29 
31 
33 
34  void update(const TuplesContainer & tuples);
35 
36  protected:
37  struct ColumnValues
38  {
40  QVariantList value;
41  QVariantList time;
42 
43  ColumnValues(const TuplesContainer & tuples);
44  };
45 
46  TagCache * tagCache() const;
47 
48  private:
49  struct Members
50  {
52  };
53 
54  MPtr<Members> m;
55 };
56 
57 template <typename T>
59  TableObject(schema, parent),
60  m(new Members{tag})
61 {
62 }
63 
64 template <typename T>
66 {
67  ColumnValues columnValues(tuples);
68  QString tableName = TableNameTraits<T>::Affixed("recency");
69 
70  worker([this, columnValues, tableName](QSqlDatabase & db) {
71  if (db.driverName() == "QPSQL") {
72  QSqlQuery query(db);
73  CUTEHMI_DEBUG("Storing '" << tableName << "' values...");
74  QVariantList tagIds;
75  for (QStringList::const_iterator tagName = columnValues.tagName.begin(); tagName != columnValues.tagName.end(); ++tagName)
76  tagIds.append(tagCache()->getId(*tagName, db));
77 
78  query.prepare(QString("INSERT INTO %1.%2 (tag_id, value, time) VALUES (:tagId, :value, :time)"
79  " ON CONFLICT (tag_id) DO UPDATE SET(value, time) = (:value, :time) WHERE %1.%2.tag_id = :tagId").arg(schema()->name()).arg(tableName));
80  query.bindValue(":tagId", tagIds);
81  query.bindValue(":value", columnValues.value);
82  query.bindValue(":time", columnValues.time);
83  query.execBatch();
84 
85  pushError(query.lastError());
86  query.finish();
87  } else if (db.driverName() == "QSQLITE") {
88  QSqlQuery query(db);
89  CUTEHMI_DEBUG("Storing '" << tableName << "' values...");
90  QVariantList tagIds;
91  for (QStringList::const_iterator tagName = columnValues.tagName.begin(); tagName != columnValues.tagName.end(); ++tagName)
92  tagIds.append(tagCache()->getId(*tagName, db));
93 
94  query.prepare(QString("INSERT INTO [%1.%2] (tag_id, value, time) VALUES (:tagId, :value, :time)"
95  " ON CONFLICT (tag_id) DO UPDATE SET(value, time) = (:value, :time)").arg(schema()->name()).arg(tableName));
96  query.bindValue(":tagId", tagIds);
97  query.bindValue(":value", columnValues.value);
98  query.bindValue(":time", columnValues.time);
99  query.execBatch();
100 
101  pushError(query.lastError());
102  query.finish();
103  } else
104  emit errored(CUTEHMI_ERROR(tr("Driver '%1' is not supported.").arg(db.driverName())));
105  })->work();
106 }
107 
108 template <typename T>
110 {
111  for (typename RecencyTable<T>::TuplesContainer::const_iterator it = tuples.begin(); it != tuples.end(); ++it) {
112  tagName.append(it.key());
113  value.append(it->value);
114  time.append(it->time);
115  }
116 }
117 
118 template <typename T>
120 {
121  return m->tagCache;
122 }
123 
124 }
125 }
126 }
127 
128 #endif
129 
130 //(c)C: Copyright © 2020, Michał Policht <michal@policht.pl>. All rights reserved.
131 //(c)C: This file is a part of CuteHMI.
132 //(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.
133 //(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.
134 //(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/>.
QList::append
void append(const T &value)
cutehmi::dataacquisition::internal::TableNameTraits::Affixed
static QString Affixed(const QString &name)
cutehmi::dataacquisition::internal::TagCache
Definition: TagCache.hpp:13
cutehmi::dataacquisition::internal::RecencyTable::ColumnValues::time
QVariantList time
Definition: RecencyTable.hpp:41
QHash::begin
QHash::iterator begin()
cutehmi::dataacquisition::internal::RecencyTable::Tuple
Definition: RecencyTable.hpp:24
cutehmi::dataacquisition::internal::RecencyTable::tagCache
TagCache * tagCache() const
Definition: RecencyTable.hpp:119
cutehmi::dataacquisition::internal::RecencyTable::update
void update(const TuplesContainer &tuples)
Definition: RecencyTable.hpp:65
cutehmi::dataacquisition::internal::TableObject
Definition: TableObject.hpp:13
QSqlDatabase
cutehmi::MPtr< Members >
QObject
cutehmi
QString
cutehmi::dataacquisition::internal::RecencyTable::RecencyTable
RecencyTable(TagCache *tagCache, Schema *schema, QObject *parent=nullptr)
Definition: RecencyTable.hpp:58
cutehmi::dataacquisition::internal::RecencyTable::Tuple::time
QDateTime time
Definition: RecencyTable.hpp:27
cutehmi::dataacquisition::internal::RecencyTable::TuplesContainer
QHash< QString, Tuple > TuplesContainer
Definition: RecencyTable.hpp:30
cutehmi::dataacquisition::internal::RecencyTable
Definition: RecencyTable.hpp:18
cutehmi::dataacquisition::internal::TableObject::schema
Schema schema
Definition: TableObject.hpp:22
cutehmi::dataacquisition::internal::RecencyTable::ColumnValues::tagName
QStringList tagName
Definition: RecencyTable.hpp:39
std::internal
T internal(T... args)
QHash::const_iterator
cutehmi::dataacquisition::internal::RecencyTable::ColumnValues::ColumnValues
ColumnValues(const TuplesContainer &tuples)
Definition: RecencyTable.hpp:109
cutehmi::dataacquisition::Schema
Database schema.
Definition: Schema.hpp:13
cutehmi::dataacquisition::internal::RecencyTable::Tuple::value
T value
Definition: RecencyTable.hpp:26
QDateTime
cutehmi::dataacquisition::internal::RecencyTable::ColumnValues
Definition: RecencyTable.hpp:37
cutehmi::dataacquisition::internal::RecencyTable::type
T type
Definition: RecencyTable.hpp:22
QHash
QSqlDatabase::driverName
QString driverName() const const
cutehmi::dataacquisition::internal::RecencyTable::ColumnValues::value
QVariantList value
Definition: RecencyTable.hpp:40
QObject::parent
QObject * parent() const const
QStringList
QHash::end
QHash::iterator end()