rdf-do 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,23 +1,28 @@
1
1
  # RDF::DataObjects
2
2
 
3
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.
4
+ currently targeting SQLite and Postgres.
6
5
 
7
- This was written for a tutorial, and is thus a pretty naive implementation:
6
+ * <http://github.com/bhuga/rdf-do>
7
+ * <http://lists.w3.org/Archives/Public/public-rdf-ruby>
8
+
9
+ This was written for a tutorial, and is thus a pretty naive implementation so far.
8
10
  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.
11
+ table. Don't try to back a big website with it yet. Nonetheless, it works,
12
+ and it passes all its tests on Heroku as well.
10
13
 
11
14
  Example:
15
+
12
16
  repository = RDF::DataObjects::Repository.new "sqlite3:test.db"
13
17
  repository.insert(statement)
14
18
  repository.count #=> 1
15
19
  repository.delete(statement)
16
20
 
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
+ You can use any DataObjects compatible connection options to create a new
22
+ repository, but only SQLite3 and Postgres are implemented for now. The
23
+ different databases are *just* different enough with their handling of unique
24
+ constraints that some database-specific work is required for a new adapter, but
25
+ it's not much.
21
26
 
22
27
  ## Installation:
23
28
 
@@ -28,7 +33,8 @@ The greatly preferred installation method is via RubyGems:
28
33
  Manual downloads are available at <http://github.com/bhuga/rdf-do/downloads>
29
34
 
30
35
  ## Connecting:
31
-
36
+ require 'do_postgres'
37
+ require 'do_sqlite3'
32
38
  repo = RDF::DataObjects::Repository.new "postgres://localhost/database"
33
39
  repo = RDF::DataObjects::Repository.new "sqlite3:test.db"
34
40
 
@@ -37,12 +43,68 @@ Manual downloads are available at <http://github.com/bhuga/rdf-do/downloads>
37
43
 
38
44
  Your repository is a fully-functional RDF.rb `RDF::Repository`. As with any
39
45
  RDF.rb repository, this includes the mixins `RDF::Enumerable`, `RDF::Mutable`,
40
- `RDF::Durable`, and `RDF::Queryable`. Please see <http://rdf.rubyforge.org> for
46
+ `RDF::Durable`, and `RDF::Queryable`. Please see <http://rdf.rubyforge.org/RDF/Repository.html> for
41
47
  more information.
42
48
 
49
+ Example:
50
+
51
+ require 'rdf'
52
+ require 'rdf/ntriples'
53
+ require 'data_objects'
54
+ require 'do_sqlite3'
55
+ require 'rdf/do'
56
+
57
+ repo = RDF::DataObjects::Repository.new('sqlite3:test.db')
58
+ # repo = RDF::DataObjects::Repository.new 'postgres://postgres@server/database'
59
+ # heroku_repo = RDF::DataObjects::Repository.new(ENV['DATABASE_URL'])
60
+ repo.load('http://datagraph.org/jhacker/foaf.nt')
61
+
62
+ # How many statements did we have?
63
+ repo.count
64
+ #=> 10
65
+
66
+ # Get the URI of the first subject
67
+ jhacker = repo.first.subject
68
+ #=> #<RDF::URI(http://datagraph.org/jhacker/foaf)>
69
+
70
+ # Delete everything to do with it
71
+ jhacker_statements = repo.query(:subject => jhacker)
72
+ repo.delete *jhacker_statements
73
+ repo.count
74
+ #=> 7
75
+
76
+ # with Postgres, we could have done this, but SQLite gives us a locking error:
77
+ # repo.delete(*repo.query(:subject => jhacker))
78
+
79
+ # Changed our mind--bring it back
80
+ repo.insert *jhacker_statements
81
+ repo.count
82
+ #=> 10
83
+
84
+
85
+
86
+ ### Developing
87
+
88
+ The main project page is on Github, at <http://github.com/bhuga/rdf-do>. You
89
+ can get a working copy of the source tree with:
90
+
91
+ $ git clone git://github.com/bhuga/rdf-do.git
92
+
93
+ Or with:
94
+
95
+ $ wget http://github.com/bhuga/rdf-do/tarball/master
96
+
97
+ ### Support
98
+
99
+ The preferred method to report issues is the issue queue at
100
+ <http://github.com/bhuga/rdf-do/issues>. You'll get the the most attention if
101
+ you submit a failing test for a bug, or a pending test for a feature.
102
+
103
+ We'd also like to hear from you on the mailing list:
104
+ <http://lists.w3.org/Archives/Public/public-rdf-ruby>
105
+
43
106
  ### Miscellany
44
107
 
45
- * Author: Ben Lavender <blavender@gmail.com> <http://bhuga.net> <http://blog.datagraph.org>
108
+ * Author: Ben Lavender | <blavender@gmail.com> | <http://bhuga.net> | <http://blog.datagraph.org>
46
109
  * 'License': RDF::DataObjects is free and unemcumbered software released into the public domain. For more information, see the included UNLICENSE file.
47
110
 
48
-
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -24,19 +24,19 @@ module RDF::DataObjects
24
24
  end
25
25
 
26
26
  def each_subject_sql
27
- 'select subject from quads'
27
+ 'select distinct subject from quads'
28
28
  end
29
29
 
30
30
  def each_predicate_sql
31
- 'select predicate from quads'
31
+ 'select distinct predicate from quads'
32
32
  end
33
33
 
34
34
  def each_object_sql
35
- 'select object from quads'
35
+ 'select distinct object from quads'
36
36
  end
37
37
 
38
38
  def each_context_sql
39
- 'select context from quads'
39
+ 'select distinct context from quads'
40
40
  end
41
41
 
42
42
  ##
@@ -31,6 +31,15 @@ module RDF::DataObjects
31
31
  'insert into quads (subject, predicate, object, context) VALUES (?, ?, ?, ?)'
32
32
  end
33
33
 
34
+ # SQL prepared statement for multiple insertion
35
+ #
36
+ # @param [Integer] The number of statements to be inserted
37
+ # @return [String]
38
+ def self.multiple_insert_sql(count)
39
+ sql = 'insert into quads (subject, predicate, object, context) VALUES '
40
+ sql + (1..count).map { "(?, ?, ?, ?)" }.join(',')
41
+ end
42
+
34
43
  # SQL prepared statement for deletions
35
44
  #
36
45
  # @return [String]
@@ -0,0 +1,23 @@
1
+ module RDF::DataObjects
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 3
5
+ TINY = 0
6
+ EXTRA = nil
7
+
8
+ STRING = [MAJOR, MINOR, TINY].join('.')
9
+ STRING << ".#{EXTRA}" if EXTRA
10
+
11
+ ##
12
+ # @return [String]
13
+ def self.to_s() STRING end
14
+
15
+ ##
16
+ # @return [String]
17
+ def self.to_str() STRING end
18
+
19
+ ##
20
+ # @return [Array(Integer, Integer, Integer)]
21
+ def self.to_a() [MAJOR, MINOR, TINY] end
22
+ end
23
+ end
data/lib/rdf/do.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'data_objects'
2
2
  require 'rdf/ntriples'
3
3
  require 'enumerator'
4
+ require 'rdf/do/version'
4
5
 
5
6
  module RDF
6
7
 
@@ -15,7 +16,6 @@ module RDF
15
16
  # @see RDF::DataObjects::Adapters
16
17
  # @see RDF::Repository
17
18
  module DataObjects
18
-
19
19
  ##
20
20
  # RDF::DataObjects::Repository is an RDF::Repository is backed by a
21
21
  # DataObjects connection.
@@ -88,7 +88,7 @@ module RDF
88
88
  # @param [RDF::Statement]
89
89
  # @return [void]
90
90
  def insert_statement(statement)
91
- insert *[statement]
91
+ insert_statements [statement]
92
92
  end
93
93
 
94
94
  ##
@@ -98,20 +98,44 @@ module RDF
98
98
  # @param [RDF::Statement]
99
99
  # @return [void]
100
100
  def delete_statement(statement)
101
- delete *[statement]
101
+ delete_statements [statement]
102
102
  end
103
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))
104
+ ##
105
+ # Insert multiple statements into this repository
106
+ #
107
+ # @see RDF::Mutable#insert_statements
108
+ # @param [Array]
109
+ # @return [void]
110
+ def insert_statements(statements)
111
+ if @adapter.respond_to?(:multiple_insert_sql)
112
+ each = statements.respond_to?(:each_statement) ? :each_statement : :each
113
+ args = []
114
+ count = 0
115
+ statements.__send__(each) do |s|
116
+ count += 1
117
+ args += [serialize(s. subject),serialize(s.predicate), serialize(s.object), serialize(s.context)]
118
+ end
119
+ query = @adapter.multiple_insert_sql(count)
120
+ exec(query,*(args.flatten))
121
+ else
122
+ query = @adapter.insert_sql
123
+ statements.each do |s|
124
+ exec(query, serialize(s. subject),serialize(s.predicate), serialize(s.object), serialize(s.context))
125
+ end
108
126
  end
109
127
  end
110
128
 
111
- def delete(*statements)
129
+ ##
130
+ # Remove multiple statements from this repository
131
+ #
132
+ # @see RDF::Mutable#delete_statements
133
+ # @param [Array]
134
+ # @return [void]
135
+ def delete_statements(statements)
112
136
  query = @adapter.delete_sql
113
137
  statements.each do |s|
114
- exec(query,serialize(s. subject),serialize(s.predicate), serialize(s.object), serialize(s.context))
138
+ exec(query, serialize(s. subject), serialize(s.predicate), serialize(s.object), serialize(s.context))
115
139
  end
116
140
  end
117
141
 
@@ -165,72 +189,31 @@ module RDF
165
189
  end
166
190
 
167
191
  ##
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.
192
+ # Iterate over all RDF::Statements in this repository.
172
193
  #
173
- # @param [Symbol] method
194
+ # @see RDF::Enumerable#each
174
195
  # @param [Proc] &block
175
196
  # @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)
197
+ def each(&block)
198
+ return ::Enumerable::Enumerator.new(self,:each) unless block_given?
195
199
  reader = result(@adapter.each_sql)
196
200
  while reader.next!
197
201
  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
+ :subject => unserialize(reader.values[0]),
203
+ :predicate => unserialize(reader.values[1]),
204
+ :object => unserialize(reader.values[2]),
205
+ :context => unserialize(reader.values[3])))
202
206
  end
203
207
  end
204
208
 
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
209
  ##
216
210
  # Iterate over all RDF::Resource subjects in this repository.
217
211
  #
218
212
  # @see RDF::Enumerable#each_subject
219
213
  # @param [Proc] &block
220
214
  # @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)
215
+ def each_subject(&block)
216
+ return ::Enumerable::Enumerator.new(self,:each_subject) unless block_given?
234
217
  reader = result(@adapter.each_subject_sql)
235
218
  while reader.next!
236
219
  block.call(unserialize(reader.values[0]))
@@ -244,18 +227,7 @@ module RDF
244
227
  # @param [Proc] &block
245
228
  # @return [Enumerable::Enumerator, void]
246
229
  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)
230
+ return ::Enumerable::Enumerator.new(self,:each_predicate) unless block_given?
259
231
  reader = result(@adapter.each_predicate_sql)
260
232
  while reader.next!
261
233
  block.call(unserialize(reader.values[0]))
@@ -269,18 +241,7 @@ module RDF
269
241
  # @param [Proc] &block
270
242
  # @return [Enumerable::Enumerator, void]
271
243
  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)
244
+ return ::Enumerable::Enumerator.new(self,:each_object) unless block_given?
284
245
  reader = result(@adapter.each_object_sql)
285
246
  while reader.next!
286
247
  block.call(unserialize(reader.values[0]))
@@ -294,18 +255,7 @@ module RDF
294
255
  # @param [Proc] &block
295
256
  # @return [Enumerable::Enumerator, void]
296
257
  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)
258
+ return ::Enumerable::Enumerator.new(self,:each_context) unless block_given?
309
259
  reader = result(@adapter.each_context_sql)
310
260
  while reader.next!
311
261
  context = unserialize(reader.values[0])
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Lavender
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-02 00:00:00 +02:00
17
+ date: 2010-04-17 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -27,8 +27,8 @@ dependencies:
27
27
  segments:
28
28
  - 0
29
29
  - 1
30
- - 3
31
- version: 0.1.3
30
+ - 7
31
+ version: 0.1.7
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
@@ -55,8 +55,8 @@ dependencies:
55
55
  segments:
56
56
  - 0
57
57
  - 1
58
- - 0
59
- version: 0.1.0
58
+ - 6
59
+ version: 0.1.6
60
60
  type: :development
61
61
  version_requirements: *id003
62
62
  - !ruby/object:Gem::Dependency
@@ -103,6 +103,7 @@ files:
103
103
  - lib/rdf/do/adapters/defaults.rb
104
104
  - lib/rdf/do/adapters/postgres.rb
105
105
  - lib/rdf/do/adapters/sqlite3.rb
106
+ - lib/rdf/do/version.rb
106
107
  - lib/rdf/do.rb
107
108
  has_rdoc: false
108
109
  homepage: http://rdf.rubyforge.org/