sparql-client 3.0.1 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +58 -47
- data/UNLICENSE +1 -1
- data/VERSION +1 -1
- data/lib/sparql/client/query.rb +55 -52
- data/lib/sparql/client/repository.rb +14 -14
- data/lib/sparql/client/update.rb +37 -38
- data/lib/sparql/client.rb +111 -69
- metadata +30 -31
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 91430f8e6c63118569f32b193b274fe8f19ca59728af08591177bdd5795ff05e
|
|
4
|
+
data.tar.gz: 3e0da28e7522a09004e2ac4bcd1b42e17cc943f02fd242d3a9e399597baa33e1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 21e38994cd92126304d4a481a3ee471018c9dd99206d537340b2c91eb49bd853bd76fa3bf558e795ed18110b6d639f97cb5288d2b520f5196739d7bb9cc0f095
|
|
7
|
+
data.tar.gz: 75a14ccdce74d985fb83f541853e4e8d95edfeb9c8d59a5f5fbc988cf378c985623df574585480e30945791327ce9f99ab5da2178b5c886c8cc91152d7174442
|
data/README.md
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
This is a [Ruby][] implementation of a [SPARQL][] client for [RDF.rb][].
|
|
4
4
|
|
|
5
|
-
* <
|
|
5
|
+
* <https://ruby-rdf.github.com/sparql-client/>
|
|
6
6
|
|
|
7
|
-
[](
|
|
8
|
-
[](https://badge.fury.io/rb/sparql-client)
|
|
8
|
+
[](https://github.com/ruby-rdf/sparql-client/actions?query=workflow%3ACI)
|
|
9
9
|
[](https://coveralls.io/github/ruby-rdf/sparql-client?branch=master)
|
|
10
|
+
[](https://gitter.im/ruby-rdf/rdf)
|
|
10
11
|
|
|
11
12
|
## Features
|
|
12
13
|
|
|
@@ -18,7 +19,7 @@ This is a [Ruby][] implementation of a [SPARQL][] client for [RDF.rb][].
|
|
|
18
19
|
* Supports tuple result sets in both XML, JSON, CSV and TSV formats, with JSON being
|
|
19
20
|
the preferred default for content-negotiation purposes.
|
|
20
21
|
* Supports graph results in any RDF serialization format understood by RDF.rb.
|
|
21
|
-
* Returns results using the
|
|
22
|
+
* Returns results using the RDF.rb object model.
|
|
22
23
|
* Supports accessing endpoints as read/write [`RDF::Repository`][RDF::Repository]
|
|
23
24
|
instances {SPARQL::Client::Repository}.
|
|
24
25
|
|
|
@@ -30,11 +31,20 @@ This is a [Ruby][] implementation of a [SPARQL][] client for [RDF.rb][].
|
|
|
30
31
|
require 'sparql/client'
|
|
31
32
|
sparql = SPARQL::Client.new("http://dbpedia.org/sparql")
|
|
32
33
|
```
|
|
34
|
+
|
|
35
|
+
### Querying a remote SPARQL endpoint with a custom User-Agent
|
|
36
|
+
By default, SPARQL::Client adds a `User-Agent` field to requests, but applications may choose to provide their own, using the `headers` option:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
require 'sparql/client'
|
|
40
|
+
sparql = SPARQL::Client.new("http://dbpedia.org/sparql", headers: {'User-Agent' => 'MyBotName'})
|
|
41
|
+
```
|
|
42
|
+
|
|
33
43
|
### Querying a remote SPARQL endpoint with a specified default graph
|
|
34
44
|
|
|
35
45
|
```ruby
|
|
36
46
|
require 'sparql/client'
|
|
37
|
-
sparql = SPARQL::Client.new("http://dbpedia.org/sparql", { :
|
|
47
|
+
sparql = SPARQL::Client.new("http://dbpedia.org/sparql", { graph: "http://dbpedia.org" })
|
|
38
48
|
```
|
|
39
49
|
|
|
40
50
|
|
|
@@ -114,15 +124,15 @@ sparql.delete_data(data)
|
|
|
114
124
|
|
|
115
125
|
## Dependencies
|
|
116
126
|
|
|
117
|
-
* [Ruby](
|
|
118
|
-
* [RDF.rb](
|
|
119
|
-
* [Net::HTTP::Persistent](
|
|
120
|
-
* Soft dependency on [SPARQL](
|
|
121
|
-
* Soft dependency on [Nokogiri](
|
|
127
|
+
* [Ruby](https://ruby-lang.org/) (>= 2.6)
|
|
128
|
+
* [RDF.rb](https://rubygems.org/gems/rdf) (~> 3.2)
|
|
129
|
+
* [Net::HTTP::Persistent](https://rubygems.org/gems/net-http-persistent) (~> 4.0, >= 4.0.1)
|
|
130
|
+
* Soft dependency on [SPARQL](https://rubygems.org/gems/sparql) (~> 3.2)
|
|
131
|
+
* Soft dependency on [Nokogiri](https://rubygems.org/gems/nokogiri) (>= 1.12)
|
|
122
132
|
|
|
123
133
|
## Installation
|
|
124
134
|
|
|
125
|
-
The recommended installation method is via [RubyGems](
|
|
135
|
+
The recommended installation method is via [RubyGems](https://rubygems.org/).
|
|
126
136
|
To install the latest official release of the `SPARQL::Client` gem, do:
|
|
127
137
|
|
|
128
138
|
% [sudo] gem install sparql-client
|
|
@@ -136,31 +146,31 @@ To get a local working copy of the development repository, do:
|
|
|
136
146
|
Alternatively, download the latest development version as a tarball as
|
|
137
147
|
follows:
|
|
138
148
|
|
|
139
|
-
% wget
|
|
149
|
+
% wget https://github.com/ruby-rdf/sparql-client/tarball/master
|
|
140
150
|
|
|
141
151
|
## Mailing List
|
|
142
152
|
|
|
143
|
-
* <
|
|
153
|
+
* <https://lists.w3.org/Archives/Public/public-rdf-ruby/>
|
|
144
154
|
|
|
145
155
|
## Authors
|
|
146
156
|
|
|
147
|
-
* [Arto Bendiken](
|
|
148
|
-
* [Ben Lavender](
|
|
149
|
-
* [Gregg Kellogg](
|
|
157
|
+
* [Arto Bendiken](https://github.com/artob) - <https://ar.to/>
|
|
158
|
+
* [Ben Lavender](https://github.com/bhuga) - <https://bhuga.net/>
|
|
159
|
+
* [Gregg Kellogg](https://github.com/gkellogg) - <https://greggkellogg.net/>
|
|
150
160
|
|
|
151
161
|
## Contributors
|
|
152
162
|
|
|
153
|
-
* [Christoph Badura](
|
|
154
|
-
* [James Hetherington](
|
|
155
|
-
* [Gabriel Horner](
|
|
156
|
-
* [Nicholas Humfrey](
|
|
157
|
-
* [Fumihiro Kato](
|
|
158
|
-
* [David Nielsen](
|
|
159
|
-
* [Thamaraiselvan Poomalai](
|
|
160
|
-
* [Michael Sokol](
|
|
161
|
-
* [Yves Raimond](
|
|
162
|
-
* [Thomas Feron](
|
|
163
|
-
* [Nick Gottlieb](
|
|
163
|
+
* [Christoph Badura](https://github.com/bad) - <https://github.com/bad>
|
|
164
|
+
* [James Hetherington](https://github.com/jamespjh) - <https://twitter.com/jamespjh>
|
|
165
|
+
* [Gabriel Horner](https://github.com/cldwalker) - <https://tagaholic.me/>
|
|
166
|
+
* [Nicholas Humfrey](https://github.com/njh) - <https://www.aelius.com/njh/>
|
|
167
|
+
* [Fumihiro Kato](https://github.com/fumi) - <https://fumi.me/>
|
|
168
|
+
* [David Nielsen](https://github.com/drankard) - <https://github.com/drankard>
|
|
169
|
+
* [Thamaraiselvan Poomalai](https://github.com/selvan) - <https://softonaut.blogspot.com/>
|
|
170
|
+
* [Michael Sokol](https://github.com/mikaa123) - <https://sokolmichael.com/>
|
|
171
|
+
* [Yves Raimond](https://github.com/moustaki) - <https://moustaki.org/>
|
|
172
|
+
* [Thomas Feron](https://github.com/thoferon) - <https://github.com/thoferon>
|
|
173
|
+
* [Nick Gottlieb](https://github.com/ngottlieb) - <https://www.nicholasgottlieb.com>
|
|
164
174
|
|
|
165
175
|
## Contributing
|
|
166
176
|
This repository uses [Git Flow](https://github.com/nvie/gitflow) to mange development and release activity. All submissions _must_ be on a feature branch based on the _develop_ branch to ease staging and integration.
|
|
@@ -175,31 +185,32 @@ This repository uses [Git Flow](https://github.com/nvie/gitflow) to mange develo
|
|
|
175
185
|
list in the the `README`. Alphabetical order applies.
|
|
176
186
|
* Do note that in order for us to merge any non-trivial changes (as a rule
|
|
177
187
|
of thumb, additions larger than about 15 lines of code), we need an
|
|
178
|
-
explicit [public domain dedication][PDD] on record from you
|
|
188
|
+
explicit [public domain dedication][PDD] on record from you,
|
|
189
|
+
which you will be asked to agree to on the first commit to a repo within the organization.
|
|
190
|
+
Note that the agreement applies to all repos in the [Ruby RDF](https://github.com/ruby-rdf/) organization.
|
|
179
191
|
|
|
180
192
|
## Resources
|
|
181
193
|
|
|
182
|
-
* <
|
|
183
|
-
* <
|
|
184
|
-
* <
|
|
185
|
-
* <
|
|
186
|
-
* <
|
|
194
|
+
* <https://ruby-rdf.github.com/sparql-client/>
|
|
195
|
+
* <https://github.com/ruby-rdf/sparql-client>
|
|
196
|
+
* <https://rubygems.org/gems/sparql-client>
|
|
197
|
+
* <https://raa.ruby-lang.org/project/sparql-client/>
|
|
198
|
+
* <https://www.ohloh.net/p/rdf>
|
|
187
199
|
|
|
188
200
|
## License
|
|
189
201
|
|
|
190
202
|
This is free and unencumbered public domain software. For more information,
|
|
191
|
-
see <
|
|
192
|
-
|
|
193
|
-
[Ruby]:
|
|
194
|
-
[RDF]:
|
|
195
|
-
[SPARQL]:
|
|
196
|
-
[SPARQL JSON]:
|
|
197
|
-
[RDF.rb]:
|
|
198
|
-
[RDF
|
|
199
|
-
[
|
|
200
|
-
[DSL]: http://en.wikipedia.org/wiki/Domain-specific_language
|
|
203
|
+
see <https://unlicense.org/> or the accompanying {file:UNLICENSE} file.
|
|
204
|
+
|
|
205
|
+
[Ruby]: https://ruby-lang.org/
|
|
206
|
+
[RDF]: https://www.w3.org/RDF/
|
|
207
|
+
[SPARQL]: https://en.wikipedia.org/wiki/SPARQL
|
|
208
|
+
[SPARQL JSON]: https://www.w3.org/TR/rdf-sparql-json-res/
|
|
209
|
+
[RDF.rb]: https://rubygems.org/gems/rdf
|
|
210
|
+
[RDF::Repository]: https://rubydoc.info/github/ruby-rdf/rdf/RDF/Repository
|
|
211
|
+
[DSL]: https://en.wikipedia.org/wiki/Domain-specific_language
|
|
201
212
|
"domain-specific language"
|
|
202
|
-
[YARD]:
|
|
203
|
-
[YARD-GS]:
|
|
204
|
-
[PDD]:
|
|
205
|
-
[Backports]:
|
|
213
|
+
[YARD]: https://yardoc.org/
|
|
214
|
+
[YARD-GS]: https://rubydoc.info/docs/yard/file/docs/GettingStarted.md
|
|
215
|
+
[PDD]: https://unlicense.org/#unlicensing-contributions
|
|
216
|
+
[Backports]: https://rubygems.org/gems/backports
|
data/UNLICENSE
CHANGED
|
@@ -21,4 +21,4 @@ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
21
21
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
22
|
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
23
|
|
|
24
|
-
For more information, please refer to <
|
|
24
|
+
For more information, please refer to <https://unlicense.org/>
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
3.0
|
|
1
|
+
3.2.0
|
data/lib/sparql/client/query.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require 'delegate'
|
|
2
|
+
|
|
1
3
|
class SPARQL::Client
|
|
2
4
|
##
|
|
3
5
|
# A SPARQL query builder.
|
|
@@ -10,7 +12,7 @@ class SPARQL::Client
|
|
|
10
12
|
# The form of the query.
|
|
11
13
|
#
|
|
12
14
|
# @return [:select, :ask, :construct, :describe]
|
|
13
|
-
# @see
|
|
15
|
+
# @see https://www.w3.org/TR/sparql11-query/#QueryForms
|
|
14
16
|
attr_reader :form
|
|
15
17
|
|
|
16
18
|
##
|
|
@@ -25,34 +27,33 @@ class SPARQL::Client
|
|
|
25
27
|
#
|
|
26
28
|
# @param [Hash{Symbol => Object}] options (see {#initialize})
|
|
27
29
|
# @return [Query]
|
|
28
|
-
# @see
|
|
29
|
-
def self.ask(options
|
|
30
|
-
self.new(:ask, options)
|
|
30
|
+
# @see https://www.w3.org/TR/sparql11-query/#ask
|
|
31
|
+
def self.ask(**options)
|
|
32
|
+
self.new(:ask, **options)
|
|
31
33
|
end
|
|
32
34
|
|
|
33
35
|
##
|
|
34
36
|
# Creates a tuple `SELECT` query.
|
|
35
37
|
#
|
|
36
|
-
# @example SELECT * WHERE { ?s ?p ?o . }
|
|
38
|
+
# @example `SELECT * WHERE { ?s ?p ?o . }`
|
|
37
39
|
# Query.select.where([:s, :p, :o])
|
|
38
40
|
#
|
|
39
|
-
# @example SELECT ?s WHERE {?s ?p ?o .}
|
|
41
|
+
# @example `SELECT ?s WHERE {?s ?p ?o .}`
|
|
40
42
|
# Query.select(:s).where([:s, :p, :o])
|
|
41
43
|
#
|
|
42
|
-
# @example SELECT COUNT(?uri as ?c) WHERE {?uri a owl:Class}
|
|
44
|
+
# @example `SELECT COUNT(?uri as ?c) WHERE {?uri a owl:Class}`
|
|
43
45
|
# Query.select(count: {uri: :c}).where([:uri, RDF.type, RDF::OWL.Class])
|
|
44
46
|
#
|
|
45
47
|
# @param [Array<Symbol>] variables
|
|
46
48
|
# @return [Query]
|
|
47
49
|
#
|
|
48
|
-
# @overload self.select(*variables, options)
|
|
50
|
+
# @overload self.select(*variables, **options)
|
|
49
51
|
# @param [Array<Symbol>] variables
|
|
50
52
|
# @param [Hash{Symbol => Object}] options (see {#initialize})
|
|
51
53
|
# @return [Query]
|
|
52
|
-
# @see
|
|
53
|
-
def self.select(*variables)
|
|
54
|
-
options
|
|
55
|
-
self.new(:select, options).select(*variables)
|
|
54
|
+
# @see https://www.w3.org/TR/sparql11-query/#select
|
|
55
|
+
def self.select(*variables, **options)
|
|
56
|
+
self.new(:select, **options).select(*variables)
|
|
56
57
|
end
|
|
57
58
|
|
|
58
59
|
##
|
|
@@ -64,14 +65,13 @@ class SPARQL::Client
|
|
|
64
65
|
# @param [Array<Symbol, RDF::URI>] variables
|
|
65
66
|
# @return [Query]
|
|
66
67
|
#
|
|
67
|
-
# @overload self.describe(*variables, options)
|
|
68
|
+
# @overload self.describe(*variables, **options)
|
|
68
69
|
# @param [Array<Symbol, RDF::URI>] variables
|
|
69
70
|
# @param [Hash{Symbol => Object}] options (see {#initialize})
|
|
70
71
|
# @return [Query]
|
|
71
|
-
# @see
|
|
72
|
-
def self.describe(*variables)
|
|
73
|
-
options
|
|
74
|
-
self.new(:describe, options).describe(*variables)
|
|
72
|
+
# @see https://www.w3.org/TR/sparql11-query/#describe
|
|
73
|
+
def self.describe(*variables, **options)
|
|
74
|
+
self.new(:describe, **options).describe(*variables)
|
|
75
75
|
end
|
|
76
76
|
|
|
77
77
|
##
|
|
@@ -83,19 +83,18 @@ class SPARQL::Client
|
|
|
83
83
|
# @param [Array<RDF::Query::Pattern, Array>] patterns
|
|
84
84
|
# @return [Query]
|
|
85
85
|
#
|
|
86
|
-
# @overload self.construct(*variables, options)
|
|
86
|
+
# @overload self.construct(*variables, **options)
|
|
87
87
|
# @param [Array<RDF::Query::Pattern, Array>] patterns
|
|
88
88
|
# @param [Hash{Symbol => Object}] options (see {#initialize})
|
|
89
89
|
# @return [Query]
|
|
90
|
-
# @see
|
|
91
|
-
def self.construct(*patterns)
|
|
92
|
-
options
|
|
93
|
-
self.new(:construct, options).construct(*patterns) # FIXME
|
|
90
|
+
# @see https://www.w3.org/TR/sparql11-query/#construct
|
|
91
|
+
def self.construct(*patterns, **options)
|
|
92
|
+
self.new(:construct, **options).construct(*patterns) # FIXME
|
|
94
93
|
end
|
|
95
94
|
|
|
96
95
|
##
|
|
97
96
|
# @param [Symbol, #to_s] form
|
|
98
|
-
# @overload self.construct(*variables, options)
|
|
97
|
+
# @overload self.construct(*variables, **options)
|
|
99
98
|
# @param [Symbol, #to_s] form
|
|
100
99
|
# @param [Hash{Symbol => Object}] options (see {Client#initialize})
|
|
101
100
|
# @option options [Hash{Symbol => Symbol}] :count
|
|
@@ -104,10 +103,10 @@ class SPARQL::Client
|
|
|
104
103
|
#
|
|
105
104
|
# @yield [query]
|
|
106
105
|
# @yieldparam [Query]
|
|
107
|
-
def initialize(form = :ask, options
|
|
106
|
+
def initialize(form = :ask, **options, &block)
|
|
108
107
|
@subqueries = []
|
|
109
108
|
@form = form.respond_to?(:to_sym) ? form.to_sym : form.to_s.to_sym
|
|
110
|
-
super([], options, &block)
|
|
109
|
+
super([], **options, &block)
|
|
111
110
|
end
|
|
112
111
|
|
|
113
112
|
##
|
|
@@ -115,27 +114,31 @@ class SPARQL::Client
|
|
|
115
114
|
# query.ask.where([:s, :p, :o])
|
|
116
115
|
#
|
|
117
116
|
# @return [Query]
|
|
118
|
-
# @see
|
|
117
|
+
# @see https://www.w3.org/TR/sparql11-query/#ask
|
|
119
118
|
def ask
|
|
120
119
|
@form = :ask
|
|
121
120
|
self
|
|
122
121
|
end
|
|
123
122
|
|
|
124
123
|
##
|
|
125
|
-
# @example SELECT * WHERE { ?s ?p ?o . }
|
|
124
|
+
# @example `SELECT * WHERE { ?s ?p ?o . }`
|
|
126
125
|
# query.select.where([:s, :p, :o])
|
|
127
126
|
#
|
|
128
|
-
# @example SELECT ?s WHERE {?s ?p ?o .}
|
|
127
|
+
# @example `SELECT ?s WHERE {?s ?p ?o .}`
|
|
129
128
|
# query.select(:s).where([:s, :p, :o])
|
|
130
129
|
#
|
|
131
|
-
# @example SELECT COUNT(?uri as ?c) WHERE {?uri a owl:Class}
|
|
130
|
+
# @example `SELECT COUNT(?uri as ?c) WHERE {?uri a owl:Class}`
|
|
132
131
|
# query.select(count: {uri: :c}).where([:uri, RDF.type, RDF::OWL.Class])
|
|
133
132
|
#
|
|
134
|
-
# @param [Array<Symbol
|
|
133
|
+
# @param [Array<Symbol>, Hash{Symbol => RDF::Query::Variable}] variables
|
|
135
134
|
# @return [Query]
|
|
136
|
-
# @see
|
|
135
|
+
# @see https://www.w3.org/TR/sparql11-query/#select
|
|
137
136
|
def select(*variables)
|
|
138
|
-
@values = variables.
|
|
137
|
+
@values = if variables.length == 1 && variables.first.is_a?(Hash)
|
|
138
|
+
variables.to_a
|
|
139
|
+
else
|
|
140
|
+
variables.map { |var| [var, RDF::Query::Variable.new(var)] }
|
|
141
|
+
end
|
|
139
142
|
self
|
|
140
143
|
end
|
|
141
144
|
|
|
@@ -145,7 +148,7 @@ class SPARQL::Client
|
|
|
145
148
|
#
|
|
146
149
|
# @param [Array<Symbol>] variables
|
|
147
150
|
# @return [Query]
|
|
148
|
-
# @see
|
|
151
|
+
# @see https://www.w3.org/TR/sparql11-query/#describe
|
|
149
152
|
def describe(*variables)
|
|
150
153
|
@values = variables.map { |var|
|
|
151
154
|
[var, var.is_a?(RDF::URI) ? var : RDF::Query::Variable.new(var)]
|
|
@@ -159,7 +162,7 @@ class SPARQL::Client
|
|
|
159
162
|
#
|
|
160
163
|
# @param [Array<RDF::Query::Pattern, Array>] patterns
|
|
161
164
|
# @return [Query]
|
|
162
|
-
# @see
|
|
165
|
+
# @see https://www.w3.org/TR/sparql11-query/#construct
|
|
163
166
|
def construct(*patterns)
|
|
164
167
|
options[:template] = build_patterns(patterns)
|
|
165
168
|
self
|
|
@@ -171,7 +174,7 @@ class SPARQL::Client
|
|
|
171
174
|
#
|
|
172
175
|
# @param [RDF::URI] uri
|
|
173
176
|
# @return [Query]
|
|
174
|
-
# @see
|
|
177
|
+
# @see https://www.w3.org/TR/sparql11-query/#specifyingDataset
|
|
175
178
|
def from(uri)
|
|
176
179
|
options[:from] = uri
|
|
177
180
|
self
|
|
@@ -204,7 +207,7 @@ class SPARQL::Client
|
|
|
204
207
|
# Yield form with or without argument; without an argument, evaluates within the query.
|
|
205
208
|
# @yieldparam [SPARQL::Client::Query] query Actually a delegator to query. Methods other than `#select` are evaluated against `self`. For `#select`, a new Query is created, and the result added as a subquery.
|
|
206
209
|
# @return [Query]
|
|
207
|
-
# @see
|
|
210
|
+
# @see https://www.w3.org/TR/sparql11-query/#GraphPattern
|
|
208
211
|
def where(*patterns_queries, &block)
|
|
209
212
|
subqueries, patterns = patterns_queries.partition {|pq| pq.is_a? SPARQL::Client::Query}
|
|
210
213
|
@patterns += build_patterns(patterns)
|
|
@@ -240,11 +243,11 @@ class SPARQL::Client
|
|
|
240
243
|
# query.select.where([:s, :p, :o]).order_by(:o, :p)
|
|
241
244
|
#
|
|
242
245
|
# @example SELECT * WHERE { ?s ?p ?o . } ORDER BY ASC(?o) DESC(?p)
|
|
243
|
-
# query.select.where([:s, :p, :o]).order_by(:
|
|
246
|
+
# query.select.where([:s, :p, :o]).order_by(o: :asc, p: :desc)
|
|
244
247
|
#
|
|
245
248
|
# @param [Array<Symbol, String>] variables
|
|
246
249
|
# @return [Query]
|
|
247
|
-
# @see
|
|
250
|
+
# @see https://www.w3.org/TR/sparql11-query/#modOrderBy
|
|
248
251
|
def order(*variables)
|
|
249
252
|
options[:order_by] = variables
|
|
250
253
|
self
|
|
@@ -259,7 +262,7 @@ class SPARQL::Client
|
|
|
259
262
|
#
|
|
260
263
|
# @param [Array<Symbol, String>] var
|
|
261
264
|
# @return [Query]
|
|
262
|
-
# @see
|
|
265
|
+
# @see https://www.w3.org/TR/sparql11-query/#modOrderBy
|
|
263
266
|
def asc(var)
|
|
264
267
|
(options[:order_by] ||= []) << {var => :asc}
|
|
265
268
|
self
|
|
@@ -272,7 +275,7 @@ class SPARQL::Client
|
|
|
272
275
|
#
|
|
273
276
|
# @param [Array<Symbol, String>] var
|
|
274
277
|
# @return [Query]
|
|
275
|
-
# @see
|
|
278
|
+
# @see https://www.w3.org/TR/sparql11-query/#modOrderBy
|
|
276
279
|
def desc(var)
|
|
277
280
|
(options[:order_by] ||= []) << {var => :desc}
|
|
278
281
|
self
|
|
@@ -284,7 +287,7 @@ class SPARQL::Client
|
|
|
284
287
|
#
|
|
285
288
|
# @param [Array<Symbol, String>] variables
|
|
286
289
|
# @return [Query]
|
|
287
|
-
# @see
|
|
290
|
+
# @see https://www.w3.org/TR/sparql11-query/#groupby
|
|
288
291
|
def group(*variables)
|
|
289
292
|
options[:group_by] = variables
|
|
290
293
|
self
|
|
@@ -297,7 +300,7 @@ class SPARQL::Client
|
|
|
297
300
|
# query.select(:s).distinct.where([:s, :p, :o])
|
|
298
301
|
#
|
|
299
302
|
# @return [Query]
|
|
300
|
-
# @see
|
|
303
|
+
# @see https://www.w3.org/TR/sparql11-query/#modDuplicates
|
|
301
304
|
def distinct(state = true)
|
|
302
305
|
options[:distinct] = state
|
|
303
306
|
self
|
|
@@ -308,7 +311,7 @@ class SPARQL::Client
|
|
|
308
311
|
# query.select(:s).reduced.where([:s, :p, :o])
|
|
309
312
|
#
|
|
310
313
|
# @return [Query]
|
|
311
|
-
# @see
|
|
314
|
+
# @see https://www.w3.org/TR/sparql11-query/#modDuplicates
|
|
312
315
|
def reduced(state = true)
|
|
313
316
|
options[:reduced] = state
|
|
314
317
|
self
|
|
@@ -319,7 +322,7 @@ class SPARQL::Client
|
|
|
319
322
|
# query.select.graph(:g).where([:s, :p, :o])
|
|
320
323
|
# @param [RDF::Value] graph_uri_or_var
|
|
321
324
|
# @return [Query]
|
|
322
|
-
# @see
|
|
325
|
+
# @see https://www.w3.org/TR/sparql11-query/#queryDataset
|
|
323
326
|
def graph(graph_uri_or_var)
|
|
324
327
|
options[:graph] = case graph_uri_or_var
|
|
325
328
|
when Symbol then RDF::Query::Variable.new(graph_uri_or_var)
|
|
@@ -336,7 +339,7 @@ class SPARQL::Client
|
|
|
336
339
|
#
|
|
337
340
|
# @param [Integer, #to_i] start
|
|
338
341
|
# @return [Query]
|
|
339
|
-
# @see
|
|
342
|
+
# @see https://www.w3.org/TR/sparql11-query/#modOffset
|
|
340
343
|
def offset(start)
|
|
341
344
|
slice(start, nil)
|
|
342
345
|
end
|
|
@@ -347,7 +350,7 @@ class SPARQL::Client
|
|
|
347
350
|
#
|
|
348
351
|
# @param [Integer, #to_i] length
|
|
349
352
|
# @return [Query]
|
|
350
|
-
# @see
|
|
353
|
+
# @see https://www.w3.org/TR/sparql11-query/#modResultLimit
|
|
351
354
|
def limit(length)
|
|
352
355
|
slice(nil, length)
|
|
353
356
|
end
|
|
@@ -386,7 +389,7 @@ class SPARQL::Client
|
|
|
386
389
|
#
|
|
387
390
|
# @param [string] string
|
|
388
391
|
# @return [Query]
|
|
389
|
-
# @see
|
|
392
|
+
# @see https://www.w3.org/TR/sparql11-query/#prefNames
|
|
390
393
|
def prefix(val)
|
|
391
394
|
options[:prefixes] ||= []
|
|
392
395
|
if val.kind_of? String
|
|
@@ -419,7 +422,7 @@ class SPARQL::Client
|
|
|
419
422
|
# Yield form with or without argument; without an argument, evaluates within the query.
|
|
420
423
|
# @yieldparam [SPARQL::Client::Query] query used for creating filters on the optional patterns.
|
|
421
424
|
# @return [Query]
|
|
422
|
-
# @see
|
|
425
|
+
# @see https://www.w3.org/TR/sparql11-query/#optionals
|
|
423
426
|
def optional(*patterns, &block)
|
|
424
427
|
(options[:optionals] ||= []) << build_patterns(patterns)
|
|
425
428
|
|
|
@@ -465,7 +468,7 @@ class SPARQL::Client
|
|
|
465
468
|
# Yield form with or without argument; without an argument, evaluates within the query.
|
|
466
469
|
# @yieldparam [SPARQL::Client::Query] query used for adding select clauses.
|
|
467
470
|
# @return [Query]
|
|
468
|
-
# @see
|
|
471
|
+
# @see https://www.w3.org/TR/sparql11-query/#optionals
|
|
469
472
|
def union(*patterns, &block)
|
|
470
473
|
options[:unions] ||= []
|
|
471
474
|
|
|
@@ -518,7 +521,7 @@ class SPARQL::Client
|
|
|
518
521
|
# Yield form with or without argument; without an argument, evaluates within the query.
|
|
519
522
|
# @yieldparam [SPARQL::Client::Query] query used for adding select clauses.
|
|
520
523
|
# @return [Query]
|
|
521
|
-
# @see
|
|
524
|
+
# @see https://www.w3.org/TR/sparql11-query/#optionals
|
|
522
525
|
def minus(*patterns, &block)
|
|
523
526
|
options[:minuses] ||= []
|
|
524
527
|
|
|
@@ -600,7 +603,7 @@ class SPARQL::Client
|
|
|
600
603
|
case nil_literal_or_term
|
|
601
604
|
when nil then nil
|
|
602
605
|
when String then RDF::Literal(nil_literal_or_term)
|
|
603
|
-
when RDF::Value then
|
|
606
|
+
when RDF::Value then nil_literal_or_term
|
|
604
607
|
else raise ArgumentError
|
|
605
608
|
end
|
|
606
609
|
end
|
|
@@ -727,7 +730,7 @@ class SPARQL::Client
|
|
|
727
730
|
buffer << 'ORDER BY'
|
|
728
731
|
options[:order_by].map { |elem|
|
|
729
732
|
case elem
|
|
730
|
-
# .order_by({ :
|
|
733
|
+
# .order_by({ var1: :asc, var2: :desc})
|
|
731
734
|
when Hash
|
|
732
735
|
elem.each { |key, val|
|
|
733
736
|
# check provided values
|
|
@@ -14,9 +14,9 @@ class SPARQL::Client
|
|
|
14
14
|
def initialize(uri: nil, **options, &block)
|
|
15
15
|
raise ArgumentError, "uri is a required parameter" unless uri
|
|
16
16
|
@options = options.merge(uri: uri)
|
|
17
|
-
@update_client = SPARQL::Client.new(options.delete(:update_endpoint), options) if options[:update_endpoint]
|
|
18
|
-
@client = SPARQL::Client.new(uri, options)
|
|
19
|
-
super(
|
|
17
|
+
@update_client = SPARQL::Client.new(options.delete(:update_endpoint), **options) if options[:update_endpoint]
|
|
18
|
+
@client = SPARQL::Client.new(uri, **options)
|
|
19
|
+
super(**@options, &block)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
##
|
|
@@ -115,7 +115,7 @@ class SPARQL::Client
|
|
|
115
115
|
# @see RDF::Repository#each_subject?
|
|
116
116
|
def each_subject(&block)
|
|
117
117
|
if block_given?
|
|
118
|
-
client.select(:s, :
|
|
118
|
+
client.select(:s, distinct: true).where([:s, :p, :o]).each_solution { |solution| block.call(solution[:s]) }
|
|
119
119
|
end
|
|
120
120
|
enum_subject
|
|
121
121
|
end
|
|
@@ -129,7 +129,7 @@ class SPARQL::Client
|
|
|
129
129
|
# @see RDF::Repository#each_predicate?
|
|
130
130
|
def each_predicate(&block)
|
|
131
131
|
if block_given?
|
|
132
|
-
client.select(:p, :
|
|
132
|
+
client.select(:p, distinct: true).where([:s, :p, :o]).each_solution { |solution| block.call(solution[:p]) }
|
|
133
133
|
end
|
|
134
134
|
enum_predicate
|
|
135
135
|
end
|
|
@@ -143,7 +143,7 @@ class SPARQL::Client
|
|
|
143
143
|
# @see RDF::Repository#each_object?
|
|
144
144
|
def each_object(&block)
|
|
145
145
|
if block_given?
|
|
146
|
-
client.select(:o, :
|
|
146
|
+
client.select(:o, distinct: true).where([:s, :p, :o]).each_solution { |solution| block.call(solution[:o]) }
|
|
147
147
|
end
|
|
148
148
|
enum_object
|
|
149
149
|
end
|
|
@@ -179,9 +179,7 @@ class SPARQL::Client
|
|
|
179
179
|
binding[:count].value.to_i rescue 0
|
|
180
180
|
rescue SPARQL::Client::MalformedQuery => e
|
|
181
181
|
# SPARQL 1.0 does not include support for aggregate functions:
|
|
182
|
-
count
|
|
183
|
-
each_statement { count += 1 } # TODO: optimize this
|
|
184
|
-
count
|
|
182
|
+
each_statement.count
|
|
185
183
|
end
|
|
186
184
|
end
|
|
187
185
|
|
|
@@ -262,10 +260,12 @@ class SPARQL::Client
|
|
|
262
260
|
# @return [void] ignored
|
|
263
261
|
# @see RDF::Queryable#query
|
|
264
262
|
# @see RDF::Query#execute
|
|
265
|
-
def query_execute(query, options
|
|
263
|
+
def query_execute(query, **options, &block)
|
|
266
264
|
return nil unless block_given?
|
|
267
|
-
q = SPARQL::Client::Query.
|
|
268
|
-
|
|
265
|
+
q = SPARQL::Client::Query.
|
|
266
|
+
select(query.variables, **{}).
|
|
267
|
+
where(*query.patterns)
|
|
268
|
+
client.query(q, **options).each do |solution|
|
|
269
269
|
yield solution
|
|
270
270
|
end
|
|
271
271
|
end
|
|
@@ -275,7 +275,7 @@ class SPARQL::Client
|
|
|
275
275
|
#
|
|
276
276
|
# @example
|
|
277
277
|
# repository.query([nil, RDF::DOAP.developer, nil])
|
|
278
|
-
# repository.query(:
|
|
278
|
+
# repository.query({predicate: RDF::DOAP.developer})
|
|
279
279
|
#
|
|
280
280
|
# @todo This should use basic SPARQL query mechanism.
|
|
281
281
|
#
|
|
@@ -284,7 +284,7 @@ class SPARQL::Client
|
|
|
284
284
|
# @yield [statement]
|
|
285
285
|
# @yieldparam [Statement]
|
|
286
286
|
# @return [Enumerable<Statement>]
|
|
287
|
-
def query_pattern(pattern, options
|
|
287
|
+
def query_pattern(pattern, **options, &block)
|
|
288
288
|
pattern = pattern.dup
|
|
289
289
|
pattern.subject ||= RDF::Query::Variable.new
|
|
290
290
|
pattern.predicate ||= RDF::Query::Variable.new
|