CuteHMI - Data Acquisition (CuteHMI.DataAcquisition.0)
EventTable.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_DATAACQUISITION_0_INCLUDE_CUTEHMI_DATAACQUISITION_INTERNAL_EVENTTABLE_HPP
2 #define H_EXTENSIONS_CUTEHMI_DATAACQUISITION_0_INCLUDE_CUTEHMI_DATAACQUISITION_INTERNAL_EVENTTABLE_HPP
3 
4 #include "common.hpp"
5 #include "TableObject.hpp"
6 #include "TagCache.hpp"
7 #include "TableNameTraits.hpp"
8 
9 #include <QHash>
10 
11 namespace cutehmi {
12 namespace dataacquisition {
13 namespace internal {
14 
15 template <typename T>
16 class EventTable:
17  public TableObject
18 {
19  public:
20  typedef T type;
21 
22  struct Tuple
23  {
24  T value = T();
26  };
27 
29 
30  void insert(const QString & tagName, const Tuple & tuple);
31 
32  protected:
33  TagCache * tagCache() const;
34 
35  private:
36  struct Members
37  {
39  };
40 
41  MPtr<Members> m;
42 };
43 
44 template <typename T>
45 EventTable<T>::EventTable(TagCache * tag, Schema * schema, QObject * parent):
46  TableObject(schema, parent),
47  m(new Members{tag})
48 {
49 }
50 
51 template <typename T>
52 void EventTable<T>::insert(const QString & tagName, const EventTable<T>::Tuple & tuple)
53 {
54  QString tableName = TableNameTraits<T>::Affixed("event");
55 
56  worker([this, tagName, tuple, tableName](QSqlDatabase & db) {
57  if (db.driverName() == "QPSQL") {
58  QSqlQuery query(db);
59  CUTEHMI_DEBUG("Storing '" << tableName << "' values...");
60 
61  query.prepare(QString("INSERT INTO %1.%2(tag_id, value, time) VALUES (:tagId, :value, :time)").arg(schema()->name()).arg(tableName));
62  query.bindValue(":tagId", tagCache()->getId(tagName, db));
63  query.bindValue(":value", tuple.value);
64  query.bindValue(":time", tuple.time);
65  query.exec();
66 
67  pushError(query.lastError());
68  query.finish();
69  } else if (db.driverName() == "QSQLITE") {
70  QSqlQuery query(db);
71  CUTEHMI_DEBUG("Storing '" << tableName << "' values...");
72 
73  query.prepare(QString("INSERT INTO [%1.%2](tag_id, value, time) VALUES (:tagId, :value, :time)").arg(schema()->name()).arg(tableName));
74  query.bindValue(":tagId", tagCache()->getId(tagName, db));
75  query.bindValue(":value", tuple.value);
76  query.bindValue(":time", tuple.time);
77  query.exec();
78 
79  pushError(query.lastError());
80  query.finish();
81  } else
82  emit errored(CUTEHMI_ERROR(tr("Driver '%1' is not supported.").arg(db.driverName())));
83  })->work();
84 }
85 
86 template <typename T>
88 {
89  return m->tagCache;
90 }
91 
92 }
93 }
94 }
95 
96 #endif
97 
98 //(c)C: Copyright © 2020, Michał Policht <michal@policht.pl>. All rights reserved.
99 //(c)C: This file is a part of CuteHMI.
100 //(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.
101 //(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.
102 //(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::EventTable::insert
void insert(const QString &tagName, const Tuple &tuple)
Definition: EventTable.hpp:52
cutehmi::dataacquisition::internal::TableNameTraits::Affixed
static QString Affixed(const QString &name)
cutehmi::dataacquisition::internal::TagCache
Definition: TagCache.hpp:13
cutehmi::dataacquisition::internal::EventTable::Tuple::value
T value
Definition: EventTable.hpp:24
cutehmi::dataacquisition::internal::TableObject
Definition: TableObject.hpp:13
QSqlDatabase
cutehmi::MPtr< Members >
QObject
cutehmi
QString
cutehmi::dataacquisition::internal::EventTable::type
T type
Definition: EventTable.hpp:20
cutehmi::dataacquisition::internal::EventTable::EventTable
EventTable(TagCache *tagCache, Schema *schema, QObject *parent=nullptr)
Definition: EventTable.hpp:45
cutehmi::dataacquisition::internal::EventTable::Tuple::time
QDateTime time
Definition: EventTable.hpp:25
cutehmi::dataacquisition::internal::EventTable::tagCache
TagCache * tagCache() const
Definition: EventTable.hpp:87
cutehmi::dataacquisition::internal::TableObject::schema
Schema schema
Definition: TableObject.hpp:22
cutehmi::dataacquisition::internal::EventTable
Definition: EventTable.hpp:16
std::internal
T internal(T... args)
cutehmi::dataacquisition::Schema
Database schema.
Definition: Schema.hpp:13
QDateTime
QSqlDatabase::driverName
QString driverName() const const
cutehmi::dataacquisition::internal::EventTable::Tuple
Definition: EventTable.hpp:22
QObject::parent
QObject * parent() const const