3 #include <ChangeListener.hpp>
4 #include <Configuration.hpp>
5 #include <databases/DatabaseManager.hpp>
6 #include <databases/DatabaseUtils.hpp>
7 #include <Showcase.hpp>
11 #include <boost/expected/expected.hpp>
15 template <
typename GeneratorType>
16 class SHOWCASE_API DatabaseGenerator :
public GeneratorType
20 virtual ~DatabaseGenerator();
22 virtual QString protocol()
const override;
23 virtual bool init(
const GraphNode & node)
override;
24 virtual bool isValidId(
const NodeId &
id) = 0;
25 ChangeListenerPtr changeListener();
27 void doKeyValueQuery(
const QString & command, PropertyMap & props,
28 const QStringList & columns);
29 boost::expected<QString> getTheme()
const;
31 QString connectionName();
37 QString m_connectionName;
38 ChangeListener::Key m_key;
44 template <
typename GeneratorType>
45 DatabaseGenerator<GeneratorType>::DatabaseGenerator()
49 template <
typename GeneratorType>
50 DatabaseGenerator<GeneratorType>::~DatabaseGenerator()
52 auto listener = changeListener();
54 listener->unsubscribeToChanges(m_key);
57 template <
typename GeneratorType>
58 QString DatabaseGenerator<GeneratorType>::protocol()
const
60 QString proto = Utils::protocol(targetNode());
66 template <
typename GeneratorType>
67 bool DatabaseGenerator<GeneratorType>::init(
const GraphNode & node)
69 if(!GeneratorType::init(node))
71 if (!isValidId(node.id))
76 m_connectionName = DB::getServer(node.id);
78 ChangeListener::NotifiableWeakPtr me =
79 std::dynamic_pointer_cast<Notifiable>(this->shared_from_this());
81 auto listener = changeListener();
83 m_key = listener->subscribeToChanges(DB::getBaseNodeId(node.id), me);
88 template <
typename GeneratorType>
89 ChangeListenerPtr DatabaseGenerator<GeneratorType>::changeListener()
91 if(
auto s = Showcase::weakInstance().lock()) {
92 auto remoteControl = s->remoteControl();
93 return remoteControl->changeListener();
99 template <
typename GeneratorType>
100 void DatabaseGenerator<GeneratorType>::doKeyValueQuery(
const QString & command, PropertyMap & props,
101 const QStringList & columns)
104 QSqlQuery query(databaseConnection);
105 query.prepare(command);
106 QList<QVariantMap> q = DB::query(query, columns);
108 for (
const auto & r : q) {
109 QByteArray key = r.value(columns.at(0)).toByteArray();
110 QVariant value = r.value(columns.at(1));
115 template <
typename GeneratorType>
116 boost::expected<QString> DatabaseGenerator<GeneratorType>::getTheme()
const
118 RunConfigurationPtr config = Showcase::instance()->runConfiguration();
120 auto theme = config->theme.id;
121 if (!theme.isEmpty())
122 return QString(theme);
124 return boost::make_unexpected(
"No valid theme");
127 template <
typename GeneratorType>
128 QString DatabaseGenerator<GeneratorType>::connectionName()
130 return m_connectionName;
133 template <
typename GeneratorType>
134 NodeId DatabaseGenerator<GeneratorType>::targetNode()
const
139 template<
typename GeneratorType>
140 NodeId DatabaseGenerator<GeneratorType>::listenerId()
const