comparison rdf/SimpleSPARQLQuery.cpp @ 440:5746c559af15

* Merge revisions 1131 to 1201 from sv-rdf-import branch
author Chris Cannam
date Thu, 18 Sep 2008 12:33:30 +0000
parents beb2948baa77
children a75edaa08d28
comparison
equal deleted inserted replaced
439:beb2948baa77 440:5746c559af15
14 */ 14 */
15 15
16 #include "SimpleSPARQLQuery.h" 16 #include "SimpleSPARQLQuery.h"
17 #include "base/ProgressReporter.h" 17 #include "base/ProgressReporter.h"
18 18
19 #ifdef USE_NEW_RASQAL_API
20 #include <rasqal/rasqal.h>
21 #else
19 #include <rasqal.h> 22 #include <rasqal.h>
23 #endif
20 24
21 #include <iostream> 25 #include <iostream>
22 26
23 using std::cerr; 27 using std::cerr;
24 using std::endl; 28 using std::endl;
29
30 #ifdef USE_NEW_RASQAL_API
31 class WrasqalWorldWrapper // wrong but wromantic, etc
32 {
33 public:
34 WrasqalWorldWrapper() : m_world(rasqal_new_world()) { }
35 ~WrasqalWorldWrapper() { rasqal_free_world(m_world); }
36
37 rasqal_world *getWorld() const { return m_world; }
38
39 private:
40 rasqal_world *m_world;
41 };
42 #endif
25 43
26 class SimpleSPARQLQuery::Impl 44 class SimpleSPARQLQuery::Impl
27 { 45 {
28 public: 46 public:
29 Impl(QString query); 47 Impl(QString query);
38 QString getErrorString() const; 56 QString getErrorString() const;
39 57
40 protected: 58 protected:
41 static void errorHandler(void *, raptor_locator *, const char *); 59 static void errorHandler(void *, raptor_locator *, const char *);
42 60
61 #ifdef USE_NEW_RASQAL_API
62 static WrasqalWorldWrapper m_www;
63 #else
43 static bool m_initialised; 64 static bool m_initialised;
65 #endif
44 66
45 QString m_query; 67 QString m_query;
46 QString m_errorString; 68 QString m_errorString;
47 ProgressReporter *m_reporter; 69 ProgressReporter *m_reporter;
48 bool m_cancelled; 70 bool m_cancelled;
49 }; 71 };
50 72
73 #ifdef USE_NEW_RASQAL_API
74 WrasqalWorldWrapper
75 SimpleSPARQLQuery::Impl::m_www;
76 #else
77 bool
78 SimpleSPARQLQuery::Impl::m_initialised = false;
79 #endif
80
51 SimpleSPARQLQuery::SimpleSPARQLQuery(QString query) : 81 SimpleSPARQLQuery::SimpleSPARQLQuery(QString query) :
52 m_impl(new Impl(query)) { } 82 m_impl(new Impl(query)) { }
53 83
54 SimpleSPARQLQuery::~SimpleSPARQLQuery() 84 SimpleSPARQLQuery::~SimpleSPARQLQuery()
55 { 85 {
83 QString 113 QString
84 SimpleSPARQLQuery::getErrorString() const 114 SimpleSPARQLQuery::getErrorString() const
85 { 115 {
86 return m_impl->getErrorString(); 116 return m_impl->getErrorString();
87 } 117 }
88
89 bool
90 SimpleSPARQLQuery::Impl::m_initialised = false;
91 118
92 SimpleSPARQLQuery::Impl::Impl(QString query) : 119 SimpleSPARQLQuery::Impl::Impl(QString query) :
93 m_query(query), 120 m_query(query),
94 m_reporter(0), 121 m_reporter(0),
95 m_cancelled(false) 122 m_cancelled(false)
96 { 123 {
97 //!!! fortunately this global stuff goes away in future rasqal versions
98 if (!m_initialised) {
99 rasqal_init();
100 }
101 } 124 }
102 125
103 SimpleSPARQLQuery::Impl::~Impl() 126 SimpleSPARQLQuery::Impl::~Impl()
104 { 127 {
105 //!!! rasqal_finish();
106 } 128 }
107 129
108 bool 130 bool
109 SimpleSPARQLQuery::Impl::isOK() const 131 SimpleSPARQLQuery::Impl::isOK() const
110 { 132 {
136 SimpleSPARQLQuery::ResultList 158 SimpleSPARQLQuery::ResultList
137 SimpleSPARQLQuery::Impl::execute() 159 SimpleSPARQLQuery::Impl::execute()
138 { 160 {
139 ResultList list; 161 ResultList list;
140 162
163 #ifdef USE_NEW_RASQAL_API
164 rasqal_query *query = rasqal_new_query(m_www.getWorld(), "sparql", NULL);
165 #else
166 if (!m_initialised) {
167 m_initialised = true;
168 rasqal_init();
169 }
141 rasqal_query *query = rasqal_new_query("sparql", NULL); 170 rasqal_query *query = rasqal_new_query("sparql", NULL);
171 #endif
142 if (!query) { 172 if (!query) {
143 m_errorString = "Failed to construct query"; 173 m_errorString = "Failed to construct query";
144 cerr << "SimpleSPARQLQuery: ERROR: " << m_errorString.toStdString() << endl; 174 cerr << "SimpleSPARQLQuery: ERROR: " << m_errorString.toStdString() << endl;
145 return list; 175 return list;
146 } 176 }
230 rasqal_free_query_results(results); 260 rasqal_free_query_results(results);
231 rasqal_free_query(query); 261 rasqal_free_query(query);
232 262
233 return list; 263 return list;
234 } 264 }
235 265
266 SimpleSPARQLQuery::Value
267 SimpleSPARQLQuery::singleResultQuery(QString query, QString binding)
268 {
269 SimpleSPARQLQuery q(query);
270 ResultList results = q.execute();
271 if (!q.isOK()) {
272 cerr << "SimpleSPARQLQuery::singleResultQuery: ERROR: "
273 << q.getErrorString().toStdString() << endl;
274 return Value();
275 }
276 if (results.empty()) {
277 return Value();
278 }
279 for (int i = 0; i < results.size(); ++i) {
280 if (results[i].find(binding) != results[i].end() &&
281 results[i][binding].type != NoValue) {
282 return results[i][binding];
283 }
284 }
285 return Value();
286 }
287
288
289