Merge lp:~kevin-wright-1/u1db-qt/query-april-18-2013-i into lp:u1db-qt

Proposed by Kevin Wright
Status: Merged
Merged at revision: 72
Proposed branch: lp:~kevin-wright-1/u1db-qt/query-april-18-2013-i
Merge into: lp:u1db-qt
Diff against target: 180 lines (+105/-4)
3 files modified
examples/u1db-qt-example-5/u1db-qt-example-5.qml (+1/-1)
src/query.cpp (+95/-3)
src/query.h (+9/-0)
To merge this branch: bzr merge lp:~kevin-wright-1/u1db-qt/query-april-18-2013-i
Reviewer Review Type Date Requested Status
Ubuntu Phone Apps Jenkins Bot continuous-integration Approve
Cris Dywan Pending
Review via email: mp+159534@code.launchpad.net

Commit message

Modified Query to support the querying functionality. In order to support proper querying on QVariantMaps a property 'queries' was added to Query. The property is a list that includes key value pairs matching a field/sub-field with a query statement. Example: queries: [{"id":"*"},{"message":"Hel*"}]. Query supports exact match and wild cards (i.e. *). Additionaly added a queries definition to the Query element in examples/u1db-qt-example-5/u1db-qt-example-5.qml, but includes a not that the property is proposed. The 'queries' property would replace the 'query' property (which does not support query on QVariantMaps).

Description of the change

Modified Query to support the querying functionality. In order to support proper querying on QVariantMaps a property 'queries' was added to Query. The property is a list that includes key value pairs matching a field/sub-field with a query statement. Example: queries: [{"id":"*"},{"message":"Hel*"}]. Query supports exact match and wild cards (i.e. *). Additionaly added a queries definition to the Query element in examples/u1db-qt-example-5/u1db-qt-example-5.qml, but includes a not that the property is proposed. The 'queries' property would replace the 'query' property (which does not support query on QVariantMaps).

To post a comment you must log in.
73. By Kevin Wright

Modified Query to update the proposed new querying functionality. The new modification fixes a bug where an else clause was returning incorrect results. The clause was changed to an if else to check for the existance of the wildcard first and then the proper boolean value determined after that. Additionally changed the surrounding function to default to return false if none of the checks are true (previously it was set to true).

74. By Kevin Wright

Modified Query to split up the querying functionality across more reasonable and intuitive methods. Additionally changed some method names and variable names to be more intuitive (e.g. checkMapMatch to queryField)

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'examples/u1db-qt-example-5/u1db-qt-example-5.qml'
2--- examples/u1db-qt-example-5/u1db-qt-example-5.qml 2013-04-12 07:53:54 +0000
3+++ examples/u1db-qt-example-5/u1db-qt-example-5.qml 2013-04-18 09:23:51 +0000
4@@ -80,7 +80,7 @@
5 U1db.Query{
6 id: aQuery
7 index: by_helloworld
8- //query: "*" /* Note: The 'query' property is not currently suupported */
9+ queries: [{"id":"*"},{"message":"Hel*"}] /* Note: The 'queries' property is a proposed feature */
10 }
11
12 MainView {
13
14=== modified file 'src/query.cpp'
15--- src/query.cpp 2013-04-12 12:26:52 +0000
16+++ src/query.cpp 2013-04-18 09:23:51 +0000
17@@ -117,6 +117,13 @@
18 }
19 }
20
21+ generateQueryResults();
22+
23+}
24+
25+void Query::generateQueryResults()
26+{
27+
28 m_index->clearResults();
29
30 m_index->generateIndexResults();
31@@ -124,9 +131,74 @@
32 QListIterator<QVariantMap> i(m_index->getAllResults());
33
34 while (i.hasNext()) {
35- QVariantMap result = i.next();
36- m_hash.insert(m_hash.count(),result);
37- }
38+
39+ QVariantMap i_map = i.next();
40+
41+ QMapIterator<QString,QVariant> j(i_map);
42+
43+ bool match = true;
44+
45+ while(j.hasNext()){
46+ j.next();
47+
48+ if(queryField(j.key(), j.value()) == false){
49+ match = false;
50+ }
51+ }
52+
53+ if(match == true){
54+ m_hash.insert(m_hash.count(),i_map);
55+ }
56+
57+ }
58+
59+}
60+
61+bool Query::queryField(QString field, QVariant value){
62+
63+ bool match = false;
64+
65+ QString value_string = value.toString();
66+ QVariant queries = getQueries();
67+ QList<QVariant> query_list = queries.toList();
68+ QListIterator<QVariant> j(query_list);
69+
70+ while (j.hasNext()) {
71+
72+ QVariant j_value = j.next();
73+ QVariantMap value_map = j_value.toMap();
74+ QMapIterator<QString,QVariant> k(value_map);
75+
76+ while(k.hasNext()){
77+ k.next();
78+
79+ QString k_key = k.key();
80+ QVariant k_variant = k.value();
81+ QString query = k_variant.toString();
82+
83+ if(field == k_key){
84+ if(query == "*"){
85+ return true;
86+ }
87+ else if(query == value_string){
88+ return true;
89+ }
90+ else if(query.contains("*")){
91+
92+ QStringList k_string_list = query.split("*");
93+ QString k_string = k_string_list[0];
94+ match = value_string.startsWith(k_string,Qt::CaseSensitive);
95+
96+ return match;
97+
98+ }
99+
100+ }
101+ }
102+
103+ }
104+
105+ return match;
106
107 }
108
109@@ -160,6 +232,11 @@
110 return m_query;
111 }
112
113+QVariant Query::getQueries()
114+{
115+ return m_queries;
116+}
117+
118 /*!
119 Sets a range, such as ['match', false].
120 Only one of query and range is used - setting range unsets the query.
121@@ -178,6 +255,21 @@
122 onDataInvalidated();
123 }
124
125+
126+void
127+Query::setQueries(QVariant queries)
128+{
129+ if (m_queries == queries)
130+ return;
131+
132+ if (m_range.isValid())
133+ m_range = QVariant();
134+
135+ m_queries = queries;
136+ Q_EMIT queriesChanged(queries);
137+ onDataInvalidated();
138+}
139+
140 QVariant
141 Query::getRange()
142 {
143
144=== modified file 'src/query.h'
145--- src/query.h 2013-04-12 12:26:52 +0000
146+++ src/query.h 2013-04-18 09:23:51 +0000
147@@ -35,6 +35,7 @@
148 Q_PROPERTY(QT_PREPEND_NAMESPACE_U1DB(Index*) index READ getIndex WRITE setIndex NOTIFY indexChanged)
149 #endif
150 Q_PROPERTY(QVariant query READ getQuery WRITE setQuery NOTIFY queryChanged)
151+ Q_PROPERTY(QVariant queries READ getQueries WRITE setQueries NOTIFY queriesChanged)
152 Q_PROPERTY(QVariant range READ getRange WRITE setRange NOTIFY rangeChanged)
153 public:
154 Query(QObject* parent = 0);
155@@ -49,17 +50,25 @@
156 void setIndex(Index* index);
157 QVariant getQuery();
158 void setQuery(QVariant query);
159+ QVariant getQueries();
160+ void setQueries(QVariant queries);
161 QVariant getRange();
162 void setRange(QVariant range);
163+
164+ void generateQueryResults();
165+ bool queryField(QString field, QVariant value);
166+
167 Q_SIGNALS:
168 void indexChanged(Index* index);
169 void queryChanged(QVariant query);
170+ void queriesChanged(QVariant queries);
171 void rangeChanged(QVariant range);
172 private:
173 Q_DISABLE_COPY(Query)
174 Index* m_index;
175 QHash<int, QVariantMap> m_hash;
176 QVariant m_query;
177+ QVariant m_queries;
178 QVariant m_range;
179
180 void onDataInvalidated();

Subscribers

People subscribed via source and target branches

to all changes: