rdf-do 0.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.
- data/README.md +48 -0
- data/UNLICENSE +24 -0
- data/VERSION +1 -0
- data/lib/rdf/do/adapters/defaults.rb +68 -0
- data/lib/rdf/do/adapters/postgres.rb +44 -0
- data/lib/rdf/do/adapters/sqlite3.rb +25 -0
- data/lib/rdf/do.rb +369 -0
- metadata +125 -0
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# RDF::DataObjects
|
2
|
+
|
3
|
+
DataObjects-backed RDF.rb repository, aiming for a simple use case and
|
4
|
+
currently targeting SQLite and Postgres. It passes its specs with Heroku's
|
5
|
+
database.
|
6
|
+
|
7
|
+
This was written for a tutorial, and is thus a pretty naive implementation:
|
8
|
+
RDF::DataObjects stores triples in a simple subject, predicate, object, context
|
9
|
+
table. Don't try to back a big website with it yet. Nonetheless, it works.
|
10
|
+
|
11
|
+
Example:
|
12
|
+
repository = RDF::DataObjects::Repository.new "sqlite3:test.db"
|
13
|
+
repository.insert(statement)
|
14
|
+
repository.count #=> 1
|
15
|
+
repository.delete(statement)
|
16
|
+
|
17
|
+
You can use any DataObjects compatible connection options, but only SQLite3 and
|
18
|
+
Postgres are implemented for now. The different databases are *just* different
|
19
|
+
enough with their handling of unique constraints that some database-specific
|
20
|
+
work is required, but it's not much.
|
21
|
+
|
22
|
+
## Installation:
|
23
|
+
|
24
|
+
The greatly preferred installation method is via RubyGems:
|
25
|
+
|
26
|
+
$ sudo gem install rdf-do
|
27
|
+
|
28
|
+
Manual downloads are available at <http://github.com/bhuga/rdf-do/downloads>
|
29
|
+
|
30
|
+
## Connecting:
|
31
|
+
|
32
|
+
repo = RDF::DataObjects::Repository.new "postgres://localhost/database"
|
33
|
+
repo = RDF::DataObjects::Repository.new "sqlite3:test.db"
|
34
|
+
|
35
|
+
|
36
|
+
## Using:
|
37
|
+
|
38
|
+
Your repository is a fully-functional RDF.rb `RDF::Repository`. As with any
|
39
|
+
RDF.rb repository, this includes the mixins `RDF::Enumerable`, `RDF::Mutable`,
|
40
|
+
`RDF::Durable`, and `RDF::Queryable`. Please see <http://rdf.rubyforge.org> for
|
41
|
+
more information.
|
42
|
+
|
43
|
+
### Miscellany
|
44
|
+
|
45
|
+
* Author: Ben Lavender <blavender@gmail.com> <http://bhuga.net> <http://blog.datagraph.org>
|
46
|
+
* 'License': RDF::DataObjects is free and unemcumbered software released into the public domain. For more information, see the included UNLICENSE file.
|
47
|
+
|
48
|
+
|
data/UNLICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'rdf/ntriples'
|
2
|
+
|
3
|
+
module RDF::DataObjects
|
4
|
+
module Adapters
|
5
|
+
|
6
|
+
##
|
7
|
+
# Default SQL statements and methods for RDF::DataObjects::Repository::Adapters.
|
8
|
+
module Defaults
|
9
|
+
|
10
|
+
def count_sql
|
11
|
+
'select count(*) from quads'
|
12
|
+
end
|
13
|
+
|
14
|
+
def insert_sql
|
15
|
+
'REPLACE INTO `quads` (subject, predicate, object, context) VALUES (?, ?, ?, ?)'
|
16
|
+
end
|
17
|
+
|
18
|
+
def delete_sql
|
19
|
+
'DELETE FROM `quads` where (subject = ? AND predicate = ? AND object = ? AND context = ?)'
|
20
|
+
end
|
21
|
+
|
22
|
+
def each_sql
|
23
|
+
'select * from quads'
|
24
|
+
end
|
25
|
+
|
26
|
+
def each_subject_sql
|
27
|
+
'select subject from quads'
|
28
|
+
end
|
29
|
+
|
30
|
+
def each_predicate_sql
|
31
|
+
'select predicate from quads'
|
32
|
+
end
|
33
|
+
|
34
|
+
def each_object_sql
|
35
|
+
'select object from quads'
|
36
|
+
end
|
37
|
+
|
38
|
+
def each_context_sql
|
39
|
+
'select context from quads'
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# Perform a query on an RDF::DataObjects::Repository based on a hash of components.
|
44
|
+
#
|
45
|
+
# This is meant to be called by RDF::DataObjects::Repository.
|
46
|
+
#
|
47
|
+
# @example
|
48
|
+
# adapter.query(repository, :predicate => predicate)
|
49
|
+
# @return [DataObjects::Result]
|
50
|
+
def query(repository, hash = {})
|
51
|
+
return repository.result(each_sql) if hash.empty?
|
52
|
+
conditions = []
|
53
|
+
params = []
|
54
|
+
[:subject, :predicate, :object, :context].each do |resource|
|
55
|
+
unless hash[resource].nil?
|
56
|
+
conditions << "#{resource.to_s} = ?"
|
57
|
+
params << repository.serialize(hash[resource])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
where = conditions.empty? ? "" : "WHERE "
|
61
|
+
where << conditions.join(' AND ')
|
62
|
+
repository.result('select * from quads ' + where, *params)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rdf/do/adapters/defaults'
|
2
|
+
|
3
|
+
module RDF::DataObjects
|
4
|
+
module Adapters
|
5
|
+
|
6
|
+
##
|
7
|
+
# Postgres Adapter for RDF::DataObjects.
|
8
|
+
#
|
9
|
+
class Postgres
|
10
|
+
|
11
|
+
self.extend Defaults
|
12
|
+
|
13
|
+
##
|
14
|
+
# Indempotently migrate this database.
|
15
|
+
#
|
16
|
+
# @param [RDF::DataObjects::Repository]
|
17
|
+
# @return [void]
|
18
|
+
def self.migrate?(do_repository, opts = {})
|
19
|
+
begin do_repository.exec('CREATE TABLE quads (subject varchar(255), predicate varchar(255), object varchar(255), context varchar(255), UNIQUE (subject, predicate, object, context))') rescue nil end
|
20
|
+
begin do_repository.exec('CREATE INDEX quads_context_index ON quads (context)') rescue nil end
|
21
|
+
begin do_repository.exec('CREATE INDEX quads_object_index ON quads (object)') rescue nil end
|
22
|
+
begin do_repository.exec('CREATE INDEX quads_predicate_index ON quads (predicate)') rescue nil end
|
23
|
+
begin do_repository.exec('CREATE INDEX quads_subject_index ON quads (subject)') rescue nil end
|
24
|
+
do_repository.exec('CREATE OR REPLACE RULE "insert_ignore" AS ON INSERT TO quads WHERE EXISTS(SELECT true FROM quads WHERE subject = NEW.subject AND predicate = NEW.predicate AND object = NEW.object AND context = NEW.context) DO INSTEAD NOTHING;')
|
25
|
+
end
|
26
|
+
|
27
|
+
# SQL prepared statement for insertions
|
28
|
+
#
|
29
|
+
# @return [String]
|
30
|
+
def self.insert_sql
|
31
|
+
'insert into quads (subject, predicate, object, context) VALUES (?, ?, ?, ?)'
|
32
|
+
end
|
33
|
+
|
34
|
+
# SQL prepared statement for deletions
|
35
|
+
#
|
36
|
+
# @return [String]
|
37
|
+
def self.delete_sql
|
38
|
+
"DELETE FROM quads where (subject = ? AND predicate = ? AND object = ? AND context = ?)"
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rdf/do/adapters/defaults'
|
2
|
+
|
3
|
+
module RDF::DataObjects
|
4
|
+
module Adapters
|
5
|
+
class Sqlite3
|
6
|
+
|
7
|
+
self.extend Defaults
|
8
|
+
|
9
|
+
##
|
10
|
+
# Indempotently migrate this database
|
11
|
+
#
|
12
|
+
# @param [RDF::DataObjects::Repository]
|
13
|
+
# @return [void]
|
14
|
+
def self.migrate?(do_repository, opts = {})
|
15
|
+
do_repository.exec('CREATE TABLE IF NOT EXISTS quads (`subject` varchar(255), `predicate` varchar(255), `object` varchar(255), `context` varchar(255), UNIQUE (`subject`, `predicate`, `object`, `context`))')
|
16
|
+
begin do_repository.exec('CREATE INDEX `quads_context_index` ON `quads` (`context`)') rescue nil end
|
17
|
+
begin do_repository.exec('CREATE INDEX `quads_object_index` ON `quads` (`object`)') rescue nil end
|
18
|
+
begin do_repository.exec('CREATE INDEX `quads_predicate_index` ON `quads` (`predicate`)') rescue nil end
|
19
|
+
begin do_repository.exec('CREATE INDEX `quads_subject_index` ON `quads` (`subject`)') rescue nil end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
data/lib/rdf/do.rb
ADDED
@@ -0,0 +1,369 @@
|
|
1
|
+
require 'data_objects'
|
2
|
+
require 'rdf/ntriples'
|
3
|
+
require 'enumerator'
|
4
|
+
|
5
|
+
module RDF
|
6
|
+
|
7
|
+
##
|
8
|
+
# RDF::DataObjects offers an RDF::Repository which is backed by a DataObjects
|
9
|
+
# connection. All inserts and deletes to this repository are done in real
|
10
|
+
# time; no flushing is required.
|
11
|
+
#
|
12
|
+
# Each RDF::DataObjects::Repository includes an RDF::DataObjects::Adapter, such as
|
13
|
+
# RDF::DataObjects::Adapters::Sqlite3.
|
14
|
+
#
|
15
|
+
# @see RDF::DataObjects::Adapters
|
16
|
+
# @see RDF::Repository
|
17
|
+
module DataObjects
|
18
|
+
|
19
|
+
##
|
20
|
+
# RDF::DataObjects::Repository is an RDF::Repository is backed by a
|
21
|
+
# DataObjects connection.
|
22
|
+
#
|
23
|
+
class Repository < RDF::Repository
|
24
|
+
|
25
|
+
## Create a new RDF::DataObjects::Repository
|
26
|
+
#
|
27
|
+
# The `options` parameter can be anything that
|
28
|
+
# DataObjects::Connection.new accepts. The correct
|
29
|
+
# RDF::Repository::DataObjects adapter will be loaded based on the URI
|
30
|
+
# scheme of the created connection.
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
# RDF::DataObjects::Repository.new # => new Repository based on sqlite3://:memory:
|
34
|
+
# RDF::DataObjects::Repository.new 'postgres://localhost/database' # => New repository based on postgres adapter
|
35
|
+
# @param [Any] options
|
36
|
+
# @return [RDF::DataObjects::Repository]
|
37
|
+
def initialize(options = {})
|
38
|
+
begin
|
39
|
+
case options
|
40
|
+
when String
|
41
|
+
@db = ::DataObjects::Connection.new(options)
|
42
|
+
when Hash
|
43
|
+
@db = ::DataObjects::Connection.new(options[:db])
|
44
|
+
adapter = options[:adapter]
|
45
|
+
when nil
|
46
|
+
@db = ::DataObjects::Connection.new('sqlite3://:memory:')
|
47
|
+
end
|
48
|
+
adapter = @db.instance_variable_get("@uri").scheme
|
49
|
+
require 'rdf/do/adapters/' + adapter.to_s
|
50
|
+
rescue Exception => e
|
51
|
+
raise LoadError, "Could not load a DataObjects adapter for #{options}. You may need to add a 'require do_adapter', or you may be trying to use an unsupported adapter (Currently supporting postgres, sqlite3). The error message was: #{e.message}"
|
52
|
+
end
|
53
|
+
@adapter = RDF::DataObjects::Adapters::const_get(adapter.to_s.capitalize)
|
54
|
+
@adapter.migrate? self
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# Close and dispose of this connection.
|
59
|
+
#
|
60
|
+
# @return [void]
|
61
|
+
def dispose
|
62
|
+
close
|
63
|
+
@db.dispose
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Close this connection.
|
68
|
+
#
|
69
|
+
# @return [void]
|
70
|
+
def close
|
71
|
+
@db.close
|
72
|
+
@adapter = nil
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Returns true if this repository is empty.
|
77
|
+
#
|
78
|
+
# @see RDF::Enumerable#empty
|
79
|
+
# @return [Boolean]
|
80
|
+
def empty?
|
81
|
+
count == 0
|
82
|
+
end
|
83
|
+
|
84
|
+
##
|
85
|
+
# Insert a single statement into this repository.
|
86
|
+
#
|
87
|
+
# @see RDF::Mutable#insert_statement
|
88
|
+
# @param [RDF::Statement]
|
89
|
+
# @return [void]
|
90
|
+
def insert_statement(statement)
|
91
|
+
insert *[statement]
|
92
|
+
end
|
93
|
+
|
94
|
+
##
|
95
|
+
# Delete a single statement from this repository.
|
96
|
+
#
|
97
|
+
# @see RDF::Mutable#delete_statement
|
98
|
+
# @param [RDF::Statement]
|
99
|
+
# @return [void]
|
100
|
+
def delete_statement(statement)
|
101
|
+
delete *[statement]
|
102
|
+
end
|
103
|
+
|
104
|
+
def insert(*statements)
|
105
|
+
query = @adapter.insert_sql
|
106
|
+
statements.each do |s|
|
107
|
+
exec(query,serialize(s. subject),serialize(s.predicate), serialize(s.object), serialize(s.context))
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def delete(*statements)
|
112
|
+
query = @adapter.delete_sql
|
113
|
+
statements.each do |s|
|
114
|
+
exec(query,serialize(s. subject),serialize(s.predicate), serialize(s.object), serialize(s.context))
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
##
|
119
|
+
# Serialize an RDF::Value into N-triples format.
|
120
|
+
# Nil values will be encoded as 'nil' to avoid problems with SQL
|
121
|
+
# implementations considering null values as distinct from one another.
|
122
|
+
#
|
123
|
+
# @see RDF::DataObjects::Repository#unserialize
|
124
|
+
# @param [RDF::Value]
|
125
|
+
# @return [String]
|
126
|
+
def serialize(value)
|
127
|
+
value.nil? ? 'nil' : RDF::NTriples::Writer.serialize(value)
|
128
|
+
end
|
129
|
+
|
130
|
+
##
|
131
|
+
# Unserialize an RDF::Value from N-triples format.
|
132
|
+
# Expects nil values to be encoded as 'nil'.
|
133
|
+
#
|
134
|
+
# @see RDF::DataObjects::Repository#serialize
|
135
|
+
# @param [String]
|
136
|
+
# @return [RDF::Value]
|
137
|
+
def unserialize(value)
|
138
|
+
value == 'nil' ? nil : RDF::NTriples::Reader.unserialize(value)
|
139
|
+
end
|
140
|
+
|
141
|
+
##
|
142
|
+
# Execute the given non-query SQL with the given arguments against this
|
143
|
+
# repository's DataObjects::Connection.
|
144
|
+
#
|
145
|
+
# If the given sql is in a prepared statement format, it will be executed
|
146
|
+
# with the given *args.
|
147
|
+
#
|
148
|
+
# @param [String] sql
|
149
|
+
# @param [*RDF::Value] args
|
150
|
+
def exec(sql, *args)
|
151
|
+
@db.create_command(sql).execute_non_query(*args)
|
152
|
+
end
|
153
|
+
|
154
|
+
##
|
155
|
+
# Execute the given query SQL with the given arguments against this
|
156
|
+
# repository's DataObjects::Connection.
|
157
|
+
#
|
158
|
+
# If the given sql is in a prepared statement format, it will be executed
|
159
|
+
# with the given *args.
|
160
|
+
#
|
161
|
+
# @param [String] sql
|
162
|
+
# @param [*RDF::Value] args
|
163
|
+
def result(sql, *args)
|
164
|
+
@db.create_command(sql).execute_reader(*args)
|
165
|
+
end
|
166
|
+
|
167
|
+
##
|
168
|
+
# Helper method to handle the block/enumerator handling for each/each_subject/each_object, etc.
|
169
|
+
#
|
170
|
+
# Will call the given method with the given block is one is given.
|
171
|
+
# Will return an enumerator with the given method called as a block if no block is given.
|
172
|
+
#
|
173
|
+
# @param [Symbol] method
|
174
|
+
# @param [Proc] &block
|
175
|
+
# @return [Enumerable::Enumerator, void]
|
176
|
+
# @nodoc
|
177
|
+
# @private
|
178
|
+
def each_or_enumerator(method, &block)
|
179
|
+
if block_given?
|
180
|
+
self.send(method, &block)
|
181
|
+
else
|
182
|
+
::Enumerable::Enumerator.new(self,:each_or_enumerator)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
##
|
187
|
+
# Implementation of #each
|
188
|
+
#
|
189
|
+
# @nodoc
|
190
|
+
# @private
|
191
|
+
# @return [void]
|
192
|
+
# @see RDF::Enumerable#each
|
193
|
+
# @see RDF::DataObjects::Repository#each_or_enumerator
|
194
|
+
def each_block(&block)
|
195
|
+
reader = result(@adapter.each_sql)
|
196
|
+
while reader.next!
|
197
|
+
block.call(RDF::Statement.new(
|
198
|
+
:subject => unserialize(reader.values[0]),
|
199
|
+
:predicate => unserialize(reader.values[1]),
|
200
|
+
:object => unserialize(reader.values[2]),
|
201
|
+
:context => unserialize(reader.values[3])))
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
##
|
206
|
+
# Iterate over all RDF::Statements in this repository.
|
207
|
+
#
|
208
|
+
# @see RDF::Enumerable#each
|
209
|
+
# @param [Proc] &block
|
210
|
+
# @return [Enumerable::Enumerator, void]
|
211
|
+
def each(&block)
|
212
|
+
each_or_enumerator(:each_block, &block)
|
213
|
+
end
|
214
|
+
|
215
|
+
##
|
216
|
+
# Iterate over all RDF::Resource subjects in this repository.
|
217
|
+
#
|
218
|
+
# @see RDF::Enumerable#each_subject
|
219
|
+
# @param [Proc] &block
|
220
|
+
# @return [Enumerable::Enumerator, void]
|
221
|
+
def each_subject(&block)
|
222
|
+
each_or_enumerator(:subject_block, &block)
|
223
|
+
end
|
224
|
+
|
225
|
+
##
|
226
|
+
# Implementation of #each_subject
|
227
|
+
#
|
228
|
+
# @nodoc
|
229
|
+
# @private
|
230
|
+
# @return [void]
|
231
|
+
# @see RDF::Enumerable#each_subject
|
232
|
+
# @see RDF::DataObjects::Repository#each_or_enumerator
|
233
|
+
def subject_block(&block)
|
234
|
+
reader = result(@adapter.each_subject_sql)
|
235
|
+
while reader.next!
|
236
|
+
block.call(unserialize(reader.values[0]))
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
##
|
241
|
+
# Iterate over all RDF::Resource predicates in this repository.
|
242
|
+
#
|
243
|
+
# @see RDF::Enumerable#each_predicate
|
244
|
+
# @param [Proc] &block
|
245
|
+
# @return [Enumerable::Enumerator, void]
|
246
|
+
def each_predicate(&block)
|
247
|
+
each_or_enumerator(:predicate_block, &block)
|
248
|
+
end
|
249
|
+
|
250
|
+
##
|
251
|
+
# Implementation of #each_predicate
|
252
|
+
#
|
253
|
+
# @nodoc
|
254
|
+
# @private
|
255
|
+
# @return [void]
|
256
|
+
# @see RDF::Enumerable#each_predicate
|
257
|
+
# @see RDF::DataObjects::Repository#each_or_enumerator
|
258
|
+
def predicate_block(&block)
|
259
|
+
reader = result(@adapter.each_predicate_sql)
|
260
|
+
while reader.next!
|
261
|
+
block.call(unserialize(reader.values[0]))
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
##
|
266
|
+
# Iterate over all RDF::Value objects in this repository.
|
267
|
+
#
|
268
|
+
# @see RDF::Enumerable#each_object
|
269
|
+
# @param [Proc] &block
|
270
|
+
# @return [Enumerable::Enumerator, void]
|
271
|
+
def each_object(&block)
|
272
|
+
each_or_enumerator(:object_block, &block)
|
273
|
+
end
|
274
|
+
|
275
|
+
##
|
276
|
+
# Implementation of #each_object
|
277
|
+
#
|
278
|
+
# @nodoc
|
279
|
+
# @private
|
280
|
+
# @return [void]
|
281
|
+
# @see RDF::Enumerable#each_object
|
282
|
+
# @see RDF::DataObjects::Repository#each_or_enumerator
|
283
|
+
def object_block(&block)
|
284
|
+
reader = result(@adapter.each_object_sql)
|
285
|
+
while reader.next!
|
286
|
+
block.call(unserialize(reader.values[0]))
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
##
|
291
|
+
# Iterate over all RDF::Resource contexts in this repository.
|
292
|
+
#
|
293
|
+
# @see RDF::Enumerable#each_context
|
294
|
+
# @param [Proc] &block
|
295
|
+
# @return [Enumerable::Enumerator, void]
|
296
|
+
def each_context(&block)
|
297
|
+
each_or_enumerator(:context_block, &block)
|
298
|
+
end
|
299
|
+
|
300
|
+
##
|
301
|
+
# Implementation of #each_context
|
302
|
+
#
|
303
|
+
# @nodoc
|
304
|
+
# @private
|
305
|
+
# @return [void]
|
306
|
+
# @see RDF::Enumerable#each_context
|
307
|
+
# @see RDF::DataObjects::Repository#each_or_enumerator
|
308
|
+
def context_block(&block)
|
309
|
+
reader = result(@adapter.each_context_sql)
|
310
|
+
while reader.next!
|
311
|
+
context = unserialize(reader.values[0])
|
312
|
+
block.call(context) unless context.nil?
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
## Implementation of RDF::Queryable#query
|
317
|
+
#
|
318
|
+
# This implementation will do well for statements and hashes, and not so
|
319
|
+
# well for RDF::Query objects.
|
320
|
+
#
|
321
|
+
# Accepts a query pattern argument as in RDF::Queryable. See
|
322
|
+
# {RDF::Queryable} for more information.
|
323
|
+
#
|
324
|
+
# @param [RDF::Statement, Hash, Array] pattern
|
325
|
+
# @return [RDF::Enumerable, void]
|
326
|
+
# @see RDF::Queryable#query
|
327
|
+
def query(pattern, &block)
|
328
|
+
case pattern
|
329
|
+
when RDF::Statement
|
330
|
+
query(pattern.to_hash)
|
331
|
+
when Array
|
332
|
+
query(RDF::Statement.new(*pattern))
|
333
|
+
when Hash
|
334
|
+
statements = []
|
335
|
+
reader = @adapter.query(self,pattern)
|
336
|
+
while reader.next!
|
337
|
+
statements << RDF::Statement.new(
|
338
|
+
:subject => unserialize(reader.values[0]),
|
339
|
+
:predicate => unserialize(reader.values[1]),
|
340
|
+
:object => unserialize(reader.values[2]),
|
341
|
+
:context => unserialize(reader.values[3]))
|
342
|
+
end
|
343
|
+
case block_given?
|
344
|
+
when true
|
345
|
+
statements.each(&block)
|
346
|
+
else
|
347
|
+
statements.extend(RDF::Enumerable, RDF::Queryable)
|
348
|
+
end
|
349
|
+
else
|
350
|
+
super(pattern)
|
351
|
+
end
|
352
|
+
|
353
|
+
end
|
354
|
+
|
355
|
+
##
|
356
|
+
# The number of statements in this repository
|
357
|
+
#
|
358
|
+
# @see RDF::Enumerable#count
|
359
|
+
# @return [Integer]
|
360
|
+
def count
|
361
|
+
result = result(@adapter.count_sql)
|
362
|
+
result.next!
|
363
|
+
result.values.first
|
364
|
+
end
|
365
|
+
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rdf-do
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Ben Lavender
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-02 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rdf
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 1
|
30
|
+
- 3
|
31
|
+
version: 0.1.3
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rdf-spec
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 1
|
44
|
+
- 0
|
45
|
+
version: 0.1.0
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rspec
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 1
|
57
|
+
- 3
|
58
|
+
- 0
|
59
|
+
version: 1.3.0
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: yard
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
- 5
|
72
|
+
- 3
|
73
|
+
version: 0.5.3
|
74
|
+
type: :development
|
75
|
+
version_requirements: *id004
|
76
|
+
description: RDF.rb plugin for DataObjects-backed repositories
|
77
|
+
email: blavender@gmail.com
|
78
|
+
executables: []
|
79
|
+
|
80
|
+
extensions: []
|
81
|
+
|
82
|
+
extra_rdoc_files: []
|
83
|
+
|
84
|
+
files:
|
85
|
+
- README.md
|
86
|
+
- UNLICENSE
|
87
|
+
- VERSION
|
88
|
+
- lib/rdf/do/adapters/defaults.rb
|
89
|
+
- lib/rdf/do/adapters/postgres.rb
|
90
|
+
- lib/rdf/do/adapters/sqlite3.rb
|
91
|
+
- lib/rdf/do.rb
|
92
|
+
has_rdoc: false
|
93
|
+
homepage: http://rdf.rubyforge.org/
|
94
|
+
licenses:
|
95
|
+
- Public Domain
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
segments:
|
106
|
+
- 1
|
107
|
+
- 8
|
108
|
+
- 2
|
109
|
+
version: 1.8.2
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
version: "0"
|
117
|
+
requirements: []
|
118
|
+
|
119
|
+
rubyforge_project: rdf
|
120
|
+
rubygems_version: 1.3.6
|
121
|
+
signing_key:
|
122
|
+
specification_version: 3
|
123
|
+
summary: RDF.rb plugin for DataObjects-backed repositories
|
124
|
+
test_files: []
|
125
|
+
|