diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-11-01 23:28:45 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-11-03 13:48:37 +0100 |
commit | 32ef7ac7d9314483d08f926277aba12eed765d07 (patch) | |
tree | 28a8d39aa67a9818adedb4ab1361cb80591f6ab1 | |
parent | 1b8eb3b19a8793fb225bdc785ebc10bea63bbb36 (diff) | |
download | sink-32ef7ac7d9314483d08f926277aba12eed765d07.tar.gz sink-32ef7ac7d9314483d08f926277aba12eed765d07.zip |
Livequery
-rw-r--r-- | sinksh/CMakeLists.txt | 1 | ||||
-rw-r--r-- | sinksh/syntax_modules/sink_livequery.cpp | 133 |
2 files changed, 134 insertions, 0 deletions
diff --git a/sinksh/CMakeLists.txt b/sinksh/CMakeLists.txt index cd12c16..359dbf0 100644 --- a/sinksh/CMakeLists.txt +++ b/sinksh/CMakeLists.txt | |||
@@ -20,6 +20,7 @@ set(sink_cli_SRCS | |||
20 | syntax_modules/sink_drop.cpp | 20 | syntax_modules/sink_drop.cpp |
21 | syntax_modules/sink_upgrade.cpp | 21 | syntax_modules/sink_upgrade.cpp |
22 | syntax_modules/sink_info.cpp | 22 | syntax_modules/sink_info.cpp |
23 | syntax_modules/sink_livequery.cpp | ||
23 | sinksh_utils.cpp | 24 | sinksh_utils.cpp |
24 | repl/repl.cpp | 25 | repl/repl.cpp |
25 | repl/replStates.cpp | 26 | repl/replStates.cpp |
diff --git a/sinksh/syntax_modules/sink_livequery.cpp b/sinksh/syntax_modules/sink_livequery.cpp new file mode 100644 index 0000000..e9e85ec --- /dev/null +++ b/sinksh/syntax_modules/sink_livequery.cpp | |||
@@ -0,0 +1,133 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | |||
20 | #include <QCoreApplication> | ||
21 | #include <QDebug> | ||
22 | #include <QObject> // tr() | ||
23 | #include <QModelIndex> | ||
24 | #include <QTime> | ||
25 | |||
26 | #include "common/resource.h" | ||
27 | #include "common/storage.h" | ||
28 | #include "common/resourceconfig.h" | ||
29 | #include "common/log.h" | ||
30 | #include "common/definitions.h" | ||
31 | #include "common/store.h" | ||
32 | #include "common/propertyparser.h" | ||
33 | |||
34 | #include "sinksh_utils.h" | ||
35 | #include "state.h" | ||
36 | #include "syntaxtree.h" | ||
37 | |||
38 | namespace SinkLiveQuery | ||
39 | { | ||
40 | |||
41 | bool livequery(const QStringList &args_, State &state) | ||
42 | { | ||
43 | if (args_.isEmpty()) { | ||
44 | state.printError(QObject::tr("Options: $type [--resource $resource] [--compact] [--filter $property=$value] [--id $id] [--showall|--show $property]")); | ||
45 | return false; | ||
46 | } | ||
47 | |||
48 | auto options = SyntaxTree::parseOptions(args_); | ||
49 | |||
50 | auto type = options.positionalArguments.isEmpty() ? QString{} : options.positionalArguments.first(); | ||
51 | |||
52 | bool asLine = true; | ||
53 | |||
54 | Sink::Query query; | ||
55 | query.setId("livequery"); | ||
56 | query.setFlags(Sink::Query::LiveQuery); | ||
57 | if (!SinkshUtils::applyFilter(query, options)) { | ||
58 | state.printError(QObject::tr("Options: $type [--resource $resource] [--compact] [--filter $property=$value] [--showall|--show $property]")); | ||
59 | return false; | ||
60 | } | ||
61 | if (options.options.contains("resource")) { | ||
62 | for (const auto &f : options.options.value("resource")) { | ||
63 | query.resourceFilter(f.toLatin1()); | ||
64 | } | ||
65 | } | ||
66 | if (options.options.contains("filter")) { | ||
67 | for (const auto &f : options.options.value("filter")) { | ||
68 | auto filter = f.split("="); | ||
69 | const auto property = filter.value(0).toLatin1(); | ||
70 | query.filter(property, Sink::PropertyParser::parse(type.toLatin1(), property, filter.value(1))); | ||
71 | } | ||
72 | } | ||
73 | if (options.options.contains("id")) { | ||
74 | for (const auto &f : options.options.value("id")) { | ||
75 | query.filter(f.toUtf8()); | ||
76 | } | ||
77 | } | ||
78 | // auto compact = options.options.contains("compact"); | ||
79 | if (!options.options.contains("showall")) { | ||
80 | if (options.options.contains("show")) { | ||
81 | auto list = options.options.value("show"); | ||
82 | std::transform(list.constBegin(), list.constEnd(), std::back_inserter(query.requestedProperties), [] (const QString &s) { return s.toLatin1(); }); | ||
83 | } else { | ||
84 | query.requestedProperties = SinkshUtils::requestedProperties(type); | ||
85 | } | ||
86 | } else { | ||
87 | asLine = false; | ||
88 | } | ||
89 | |||
90 | QByteArrayList toPrint; | ||
91 | QStringList tableLine; | ||
92 | |||
93 | auto model = SinkshUtils::loadModel(query.type(), query); | ||
94 | QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) { | ||
95 | if (roles.contains(Sink::Store::ChildrenFetchedRole)) { | ||
96 | state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex()))); | ||
97 | } | ||
98 | }); | ||
99 | QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, state](const QModelIndex &index, int start, int end) { | ||
100 | for (int i = start; i <= end; i++) { | ||
101 | auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>(); | ||
102 | state.printLine("Resource: " + object->resourceInstanceIdentifier(), 1); | ||
103 | state.printLine("Identifier: " + object->identifier(), 1); | ||
104 | state.stageTableLine(QStringList() | ||
105 | << QObject::tr("Property:") | ||
106 | << QObject::tr("Value:")); | ||
107 | |||
108 | for (const auto &property : object->availableProperties()) { | ||
109 | state.stageTableLine(QStringList() | ||
110 | << property | ||
111 | << object->getProperty(property).toString()); | ||
112 | } | ||
113 | state.flushTable(); | ||
114 | } | ||
115 | }); | ||
116 | |||
117 | if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) { | ||
118 | return true; | ||
119 | } | ||
120 | |||
121 | return false; | ||
122 | } | ||
123 | |||
124 | Syntax::List syntax() | ||
125 | { | ||
126 | Syntax list("livequery", QObject::tr("Run a livequery."), &SinkLiveQuery::livequery, Syntax::EventDriven); | ||
127 | list.completer = &SinkshUtils::resourceOrTypeCompleter; | ||
128 | return Syntax::List() << list; | ||
129 | } | ||
130 | |||
131 | REGISTER_SYNTAX(SinkLiveQuery) | ||
132 | |||
133 | } | ||