rdbxml 0.5 → 0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +4 -4
- data/ext/dbxml_ruby.i +54 -11
- data/lib/rdbxml.rb +80 -15
- data/test/test_rdbxml.rb +16 -2
- metadata +4 -4
data/Rakefile
CHANGED
@@ -6,6 +6,8 @@ require 'rake/rdoctask'
|
|
6
6
|
require 'rake/gempackagetask'
|
7
7
|
require 'rake/contrib/rubyforgepublisher'
|
8
8
|
|
9
|
+
GEM_VERSION = '0.6'
|
10
|
+
|
9
11
|
dbxml_dist = ENV['DBXML_DIST']
|
10
12
|
if dbxml_dist
|
11
13
|
puts "*** Using DBXML distribution in #{dbxml_dist}"
|
@@ -57,7 +59,6 @@ rd = Rake::RDocTask.new :rdoc do |rdoc|
|
|
57
59
|
rdoc.rdoc_files.include 'rake/**/*task.rb'
|
58
60
|
end
|
59
61
|
|
60
|
-
GEM_VERSION = '0.5'
|
61
62
|
GEM_FILES = rd.rdoc_files + FileList[
|
62
63
|
'Rakefile',
|
63
64
|
'ext/**/*.i',
|
@@ -71,9 +72,8 @@ spec = Gem::Specification.new do |s|
|
|
71
72
|
s.version = GEM_VERSION
|
72
73
|
s.date = Date.today.to_s
|
73
74
|
s.authors = ["Steve Sloan"]
|
74
|
-
s.summary = 'Provides wrappers for the
|
75
|
-
s.description =
|
76
|
-
END
|
75
|
+
s.summary = 'Provides wrappers for the BDB XML C++ APIs, plus pure Ruby extensions'
|
76
|
+
# s.description =
|
77
77
|
s.files = GEM_FILES.to_a.delete_if {|f| f.include?('.svn')}
|
78
78
|
s.autorequire = 'rdbxml'
|
79
79
|
s.test_files = Dir["test/test_*.rb"]
|
data/ext/dbxml_ruby.i
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
%module dbxml
|
2
|
+
%include "exception.i"
|
2
3
|
%include "std_string.i"
|
4
|
+
%include "std_except.i"
|
3
5
|
|
4
6
|
%alias XmlManager::createQueryContext "create_query_context";
|
5
7
|
|
@@ -11,25 +13,66 @@
|
|
11
13
|
%alias XmlDocument::getContent "content";
|
12
14
|
%alias XmlDocument::setContent "content=";
|
13
15
|
|
14
|
-
%alias XmlQueryContext::getNamespace "
|
15
|
-
%alias XmlQueryContext::setNamespace "
|
16
|
-
%alias XmlQueryContext::getDefaultCollection "
|
17
|
-
%alias XmlQueryContext::setDefaultCollection "
|
18
|
-
%alias XmlQueryContext::getVariableValue "[]";
|
19
|
-
%alias XmlQueryContext::setVariableValue "[]=";
|
16
|
+
%alias XmlQueryContext::getNamespace "namespace";
|
17
|
+
%alias XmlQueryContext::setNamespace "namespace=";
|
18
|
+
%alias XmlQueryContext::getDefaultCollection "collection";
|
19
|
+
%alias XmlQueryContext::setDefaultCollection "collection=";
|
20
20
|
|
21
21
|
%alias XmlValue::asString "to_s";
|
22
22
|
%alias XmlValue::asNumber "to_f";
|
23
23
|
%alias XmlValue::asDocument "to_doc";
|
24
24
|
|
25
|
+
%exceptionclass DbException;
|
26
|
+
class DbException;
|
27
|
+
|
28
|
+
%exceptionclass XmlException;
|
29
|
+
%alias XmlException::what "to_s";
|
30
|
+
%alias XmlException::getExceptionCode "err";
|
31
|
+
|
32
|
+
class XmlException : public std::exception {
|
33
|
+
public:
|
34
|
+
enum ExceptionCode {
|
35
|
+
INTERNAL_ERROR, ///< An internal error occured.
|
36
|
+
CONTAINER_OPEN, ///< The container is open.
|
37
|
+
CONTAINER_CLOSED, ///< The container is closed.
|
38
|
+
NULL_POINTER, ///< null pointer exception
|
39
|
+
INDEXER_PARSER_ERROR, ///< XML Indexer could not parse a document.
|
40
|
+
DATABASE_ERROR, ///< Berkeley DB reported a database problem.
|
41
|
+
XPATH_PARSER_ERROR, ///< The XPath parser was unable to parse the XPath expression.
|
42
|
+
DOM_PARSER_ERROR, ///< The DOM parser was unable to parse an XML document.
|
43
|
+
XPATH_EVALUATION_ERROR, ///< The XPath evaluator was unable to execute the XPath expression.
|
44
|
+
NO_VARIABLE_BINDING, ///< An XPath expression referred to variable without a value.
|
45
|
+
LAZY_EVALUATION, ///< XmlResults is lazily evaluated.
|
46
|
+
DOCUMENT_NOT_FOUND, ///< The specified document could not be found
|
47
|
+
CONTAINER_EXISTS, ///< The container already exists.
|
48
|
+
UNKNOWN_INDEX, ///< The indexing strategy name is unknown.
|
49
|
+
INVALID_VALUE, ///< An invalid parameter was passed.
|
50
|
+
VERSION_MISMATCH, ///< The container version and the dbxml library version are not compatible.
|
51
|
+
UNUSED_2, ///< Placeholder
|
52
|
+
CONTAINER_NOT_FOUND, ///< The specified container could not be found
|
53
|
+
TRANSACTION_ERROR, ///< An XmlTransaction has already been committed or aborted
|
54
|
+
UNIQUE_ERROR, ///< A uniqueness constraint has been violated
|
55
|
+
NO_MEMORY_ERROR ///< An operation was unable to allocate memory
|
56
|
+
};
|
57
|
+
|
58
|
+
explicit XmlException(const DbException &e, const char *file = 0, int line = 0);
|
59
|
+
XmlException(const XmlException &);
|
60
|
+
virtual ~XmlException() throw();
|
61
|
+
virtual const char *what() const throw();
|
62
|
+
|
63
|
+
ExceptionCode getExceptionCode() const;
|
64
|
+
int getDbErrno() const;
|
65
|
+
};
|
66
|
+
|
25
67
|
%exception {
|
26
68
|
try {
|
27
69
|
$action
|
28
70
|
} catch ( DbException &ex ) {
|
29
|
-
|
30
|
-
|
31
|
-
} catch (
|
32
|
-
|
33
|
-
|
71
|
+
%raise( SWIG_NewPointerObj(new DbException(ex), SWIGTYPE_p_DbException, SWIG_POINTER_OWN),
|
72
|
+
"BDB Exception", SWIGTYPE_p_DbException );
|
73
|
+
} catch ( XmlException &ex ) {
|
74
|
+
%raise( SWIG_NewPointerObj(new XmlException(ex), SWIGTYPE_p_XmlException, SWIG_POINTER_OWN),
|
75
|
+
"BDB XXML Exception", SWIGTYPE_p_XmlException );
|
34
76
|
}
|
35
77
|
}
|
78
|
+
|
data/lib/rdbxml.rb
CHANGED
@@ -38,9 +38,18 @@ module RDBXML
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
+
# Wraps and extends the XmlValue[http://www.sleepycat.com/xmldocs/api_cxx/XmlValue.html] class.
|
42
|
+
# === Aliases:
|
43
|
+
# to_s:: asString
|
44
|
+
# to_f:: asNumber
|
45
|
+
# to_doc:: asDocument (XmlDocument)
|
46
|
+
# to_i:: #to_f
|
41
47
|
class Dbxml::XmlValue
|
42
|
-
def to_i
|
48
|
+
def to_i # :nodoc:
|
49
|
+
self.to_f.to_i
|
50
|
+
end
|
43
51
|
|
52
|
+
# Handles Numeric/Boolean/String comparisons
|
44
53
|
def ==( that )
|
45
54
|
if isNumber
|
46
55
|
self.asNumber == that
|
@@ -52,33 +61,71 @@ class Dbxml::XmlValue
|
|
52
61
|
end
|
53
62
|
end
|
54
63
|
|
64
|
+
# Wraps the XmlResults[http://www.sleepycat.com/xmldocs/api_cxx/XmlResults_list.html] class as an
|
65
|
+
# enumerable collection of XmlValue or XmlDocument.
|
55
66
|
class Dbxml::XmlResults
|
56
67
|
include Enumerable
|
57
68
|
|
58
|
-
|
69
|
+
# Iterates across each result (as an XmlValue) in the set
|
70
|
+
def each( &block ) # :yields: value
|
59
71
|
self.reset
|
60
72
|
while self.hasNext
|
61
73
|
yield self.next
|
62
74
|
end
|
63
75
|
end
|
64
76
|
|
77
|
+
# Iterates across each result (as an XmlDocument) in the set
|
78
|
+
def each_doc( &block ) # :yields: document
|
79
|
+
self.reset
|
80
|
+
while self.hasNext
|
81
|
+
yield self.nextDocument
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns the first result as an XmlValue(or +nil+)
|
65
86
|
def first
|
66
87
|
self.reset
|
67
88
|
self.hasNext ? self.next : nil
|
68
89
|
end
|
69
90
|
|
91
|
+
# Returns the first result as an XmlDocument (or +nil+)
|
92
|
+
def first_doc
|
93
|
+
self.reset
|
94
|
+
self.hasNext ? self.nextDocument : nil
|
95
|
+
end
|
96
|
+
|
97
|
+
# Returns the result set as +newline+-joined strings
|
70
98
|
def to_s
|
71
99
|
collect { |v| v.to_s }.join "\n"
|
72
100
|
end
|
73
101
|
end
|
74
102
|
|
75
|
-
class
|
76
|
-
|
103
|
+
# Wraps and extends the XmlQueryContext[http://www.sleepycat.com/xmldocs/api_cxx/XmlQueryContext_list.html] class.
|
104
|
+
# === Aliases
|
105
|
+
# namespace:: getNamespace/setNamespace[http://www.sleepycat.com/xmldocs/api_cxx/XmlQueryContext_setNamespace.html]
|
106
|
+
# collection:: getDefaultCollection/setDefaultCollection[http://www.sleepycat.com/xmldocs/api_cxx/XmlQueryContext_setDefaultCollection.html]
|
107
|
+
class Dbxml::XmlQueryContext
|
108
|
+
# XmlQueryContext::getVariableValue[http://www.sleepycat.com/xmldocs/api_cxx/XmlQueryContext_setVariableValue.html]
|
109
|
+
def []( name )
|
110
|
+
getVariableValue name.to_s
|
111
|
+
end
|
112
|
+
# XmlQueryContext::setVariableValue[http://www.sleepycat.com/xmldocs/api_cxx/XmlQueryContext_setVariableValue.html]
|
113
|
+
def []=( name, val ) # :nodoc:
|
114
|
+
setVariableValue name.to_s, Dbxml::XmlValue.new(val)
|
115
|
+
end
|
116
|
+
end
|
77
117
|
|
118
|
+
# Wraps and extends the XmlDocument[http://www.sleepycat.com/xmldocs/api_cxx/XmlDocument_list.html] class.
|
119
|
+
# === Aliases:
|
120
|
+
# name:: getName/setName[http://www.sleepycat.com/xmldocs/api_cxx/XmlDocument_setName.html]
|
121
|
+
# content:: getContent/setContent[http://www.sleepycat.com/xmldocs/api_cxx/XmlDocument_getContent.html]
|
122
|
+
# to_s:: #content
|
123
|
+
class Dbxml::XmlDocument
|
124
|
+
# Represents the document metadata as an Enumerable collection
|
78
125
|
class MetaData
|
79
126
|
include Enumerable
|
80
127
|
|
81
|
-
def initialize(doc)
|
128
|
+
def initialize(doc) #:nodoc:
|
82
129
|
@doc = doc
|
83
130
|
end
|
84
131
|
|
@@ -92,7 +139,7 @@ class Dbxml::XmlDocument
|
|
92
139
|
val = args.pop
|
93
140
|
ns = args.shift || ''
|
94
141
|
if val
|
95
|
-
@doc.setMetaData ns, name.to_s, XmlValue.new(val)
|
142
|
+
@doc.setMetaData ns, name.to_s, Dbxml::XmlValue.new(val)
|
96
143
|
else
|
97
144
|
delete name, ns
|
98
145
|
end
|
@@ -103,7 +150,7 @@ class Dbxml::XmlDocument
|
|
103
150
|
@doc.removeMetaData ns, name.to_s
|
104
151
|
end
|
105
152
|
|
106
|
-
def each(&block)
|
153
|
+
def each(&block) # :yields: name, value, uri
|
107
154
|
i = @doc.getMetaDataIterator
|
108
155
|
while (xmd = i.next)
|
109
156
|
yield xmd.get_name.to_sym, xmd.get_value, xmd.get_uri
|
@@ -118,12 +165,16 @@ class Dbxml::XmlDocument
|
|
118
165
|
end
|
119
166
|
end
|
120
167
|
|
168
|
+
# Returns the document metadata[http://www.sleepycat.com/xmldocs/api_cxx/XmlDocument_getMetaData.html]
|
121
169
|
def meta
|
122
170
|
@Meta ||= MetaData.new(self)
|
123
171
|
end
|
124
172
|
|
125
173
|
end
|
126
174
|
|
175
|
+
# Wraps and extends the XmlContainer[http://www.sleepycat.com/xmldocs/api_cxx/XmlContainer_list.html] class.
|
176
|
+
# === Aliases
|
177
|
+
# manager:: getManager[http://www.sleepycat.com/xmldocs/api_cxx/XmlContainer_getManager.html]
|
127
178
|
class Dbxml::XmlContainer
|
128
179
|
include Enumerable
|
129
180
|
|
@@ -131,8 +182,8 @@ class Dbxml::XmlContainer
|
|
131
182
|
def []( name )
|
132
183
|
begin
|
133
184
|
getDocument name.to_s
|
134
|
-
rescue XmlException => ex
|
135
|
-
raise unless ex.
|
185
|
+
rescue Dbxml::XmlException => ex
|
186
|
+
raise unless ex.err == XmlException::DOCUMENT_NOT_FOUND
|
136
187
|
nil
|
137
188
|
end
|
138
189
|
end
|
@@ -152,32 +203,37 @@ class Dbxml::XmlContainer
|
|
152
203
|
self << doc
|
153
204
|
end
|
154
205
|
|
155
|
-
# Creates/updates the
|
206
|
+
# Creates/updates the XmlDocument +doc+.
|
156
207
|
def <<( doc )
|
157
208
|
ctx = getManager.createUpdateContext
|
158
209
|
begin
|
159
210
|
putDocument doc, ctx, 0
|
160
|
-
rescue XmlException => ex
|
161
|
-
raise unless ex.
|
211
|
+
rescue Dbxml::XmlException => ex
|
212
|
+
raise unless ex.err == XmlException::UNIQUE_ERROR
|
162
213
|
d = self[doc.name]
|
163
214
|
d.content = doc.content
|
164
215
|
updateDocument d, ctx
|
165
216
|
end
|
166
217
|
end
|
167
218
|
|
168
|
-
# Iterates over each
|
169
|
-
def each( &block )
|
219
|
+
# Iterates over each XmlDocument in the collection
|
220
|
+
def each( &block ) # :yields: document
|
170
221
|
getAllDocuments.each(block)
|
171
222
|
end
|
172
223
|
end
|
173
224
|
|
225
|
+
# Wraps and extends the XmlManager[http://www.sleepycat.com/xmldocs/api_cxx/XmlManager_list.html] class.
|
226
|
+
# === Aliases
|
227
|
+
# create_query_context:: createQueryContext
|
174
228
|
class Dbxml::XmlManager
|
175
229
|
# Opens the container named +name+, creating it if it doesn't exist.
|
176
230
|
def []( name )
|
177
231
|
openContainer name.to_s, Dbxml::DB_CREATE
|
178
232
|
end
|
179
233
|
|
180
|
-
|
234
|
+
# Runs the query +xquery+, creating a query context if necessary or using
|
235
|
+
# +opts[:ctx]+ if passed.
|
236
|
+
def query( xquery, opts = {}, &block ) # :yeilds: result
|
181
237
|
opts[:ctx] ||= create_query_context
|
182
238
|
q = self.prepare xquery, opts[:ctx]
|
183
239
|
res = q.execute( opts[:ctx], 0 )
|
@@ -186,3 +242,12 @@ class Dbxml::XmlManager
|
|
186
242
|
res
|
187
243
|
end
|
188
244
|
end
|
245
|
+
|
246
|
+
# Wraps and extends the [http://www.sleepycat.com/xmldocs/api_cxx/env_class.html] class.
|
247
|
+
class Db::DbEnv
|
248
|
+
# Convenience function to instantiate a XmlManager which attaches to
|
249
|
+
# this environment.
|
250
|
+
def manager
|
251
|
+
Dbxml::XmlManager.new self, 0
|
252
|
+
end
|
253
|
+
end
|
data/test/test_rdbxml.rb
CHANGED
@@ -55,6 +55,20 @@ class XmlValueTest < Test::Unit::TestCase
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
+
class XmlQueryContextTest < Test::Unit::TestCase
|
59
|
+
def setup
|
60
|
+
@db = RDBXML::XmlManager.new
|
61
|
+
@q = @db.create_query_context
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_get_set_variable
|
65
|
+
@q[:foo] = 'xyz'
|
66
|
+
assert_equal 'xyz', @q[:foo].to_s
|
67
|
+
assert_equal 'xyz', @db.query('$foo', :ctx => @q).to_s
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
58
72
|
class XmlResultsTest < Test::Unit::TestCase
|
59
73
|
def setup
|
60
74
|
@db = RDBXML::XmlManager.new
|
@@ -93,8 +107,8 @@ class XmlDocumentTest < Test::Unit::TestCase
|
|
93
107
|
def setup
|
94
108
|
@dir = File.join( File.dirname(__FILE__), File.basename(__FILE__, '.rb') + '.db' )
|
95
109
|
Dir.mkdir @dir unless File.exists? @dir
|
96
|
-
@env = RDBXML::env
|
97
|
-
@db =
|
110
|
+
@env = RDBXML::env @dir
|
111
|
+
@db = @env.manager
|
98
112
|
@docs = @db['test']
|
99
113
|
end
|
100
114
|
|
metadata
CHANGED
@@ -3,16 +3,16 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: rdbxml
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "0.
|
7
|
-
date: 2006-05-
|
8
|
-
summary: Provides wrappers for the
|
6
|
+
version: "0.6"
|
7
|
+
date: 2006-05-29 00:00:00 -07:00
|
8
|
+
summary: Provides wrappers for the BDB XML C++ APIs, plus pure Ruby extensions
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
- ext
|
12
12
|
email:
|
13
13
|
homepage:
|
14
14
|
rubyforge_project:
|
15
|
-
description:
|
15
|
+
description:
|
16
16
|
autorequire: rdbxml
|
17
17
|
default_executable:
|
18
18
|
bindir: bin
|