orientdb4r 0.2.1 → 0.2.2

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.rdoc CHANGED
@@ -15,33 +15,35 @@ see Wiki page for more sample at https://github.com/veny/orientdb4r/wiki
15
15
 
16
16
  client.connect :database => 'temp', :user => 'admin', :password => 'admin'
17
17
 
18
-
19
18
  client.create_class(CLASS) do |c|
20
- c.property 'prop1', :integer
21
- c.property 'prop2', :string, :mandatory => true, :notnull => :true, :min => 1, :max => 99
19
+ c.property 'prop1', :integer, :notnull => true, :min => 1, :max => 99
20
+ c.property 'prop2', :string, :mandatory => true
21
+ c.link 'users', :linkset, 'OUser' # by default: :mandatory => false, :notnull => false
22
22
  end
23
23
 
24
-
25
- 1.upto(10) { |i| client.command "INSERT INTO #{CLASS} (prop1, prop2) VALUES (#{i}, 'string#{i}')" }
26
-
24
+ admin = client.query("SELECT FROM OUser WHERE name = 'admin'")[0]
25
+ 1.upto(5) do |i|
26
+ # insert link to admin only to first two
27
+ client.command "INSERT INTO #{CLASS} (prop1, prop2, users) VALUES (#{i}, 'text#{i}', [#{admin['@rid'] if i<3}])"
28
+ end
27
29
 
28
30
  puts client.query "SELECT FROM #{CLASS}"
29
- > {"@type"=>"d", "@rid"=>"#6:0", "@version"=>0, "@class"=>"testing", "prop1"=>1, "prop2"=>"string1"}
30
- > {"@type"=>"d", "@rid"=>"#6:1", "@version"=>0, "@class"=>"testing", "prop1"=>2, "prop2"=>"string2"}
31
- > {"@type"=>"d", "@rid"=>"#6:2", "@version"=>0, "@class"=>"testing", "prop1"=>3, "prop2"=>"string3"}
32
- > {"@type"=>"d", "@rid"=>"#6:3", "@version"=>0, "@class"=>"testing", "prop1"=>4, "prop2"=>"string4"}
33
- > {"@type"=>"d", "@rid"=>"#6:4", "@version"=>0, "@class"=>"testing", "prop1"=>5, "prop2"=>"string5"}
34
- > {"@type"=>"d", "@rid"=>"#6:5", "@version"=>0, "@class"=>"testing", "prop1"=>6, "prop2"=>"string6"}
35
- > {"@type"=>"d", "@rid"=>"#6:6", "@version"=>0, "@class"=>"testing", "prop1"=>7, "prop2"=>"string7"}
36
- > {"@type"=>"d", "@rid"=>"#6:7", "@version"=>0, "@class"=>"testing", "prop1"=>8, "prop2"=>"string8"}
37
- > {"@type"=>"d", "@rid"=>"#6:8", "@version"=>0, "@class"=>"testing", "prop1"=>9, "prop2"=>"string9"}
38
- > {"@type"=>"d", "@rid"=>"#6:9", "@version"=>0, "@class"=>"testing", "prop1"=>10, "prop2"=>"string10"}
31
+ > {"@type"=>"d", "@rid"=>"#6:0", "@version"=>0, "@class"=>"myclass", "prop1"=>1, "prop2"=>"text1", "users"=>["#4:0"]}
32
+ > {"@type"=>"d", "@rid"=>"#6:1", "@version"=>0, "@class"=>"myclass", "prop1"=>2, "prop2"=>"text2", "users"=>["#4:0"]}
33
+ > {"@type"=>"d", "@rid"=>"#6:2", "@version"=>0, "@class"=>"myclass", "prop1"=>3, "prop2"=>"text3", "users"=>[]}
34
+ > {"@type"=>"d", "@rid"=>"#6:3", "@version"=>0, "@class"=>"myclass", "prop1"=>4, "prop2"=>"text4", "users"=>[]}
35
+ > {"@type"=>"d", "@rid"=>"#6:4", "@version"=>0, "@class"=>"myclass", "prop1"=>5, "prop2"=>"text5", "users"=>[]}
36
+
39
37
  puts client.query "SELECT count(*) FROM #{CLASS}"
40
- > {"@type"=>"d", "@version"=>0, "count"=>10, "@fieldTypes"=>"count=l"}
38
+ > {"@type"=>"d", "@version"=>0, "count"=>5, "@fieldTypes"=>"count=l"}
39
+
41
40
  puts client.query "SELECT max(prop1) FROM #{CLASS}"
42
- > {"@type"=>"d", "@version"=>0, "max"=>10}
43
- puts client.query "SELECT FROM #{CLASS} WHERE prop2 LIKE '%10'"
44
- > {"@type"=>"d", "@rid"=>"#6:9", "@version"=>0, "@class"=>"testing", "prop1"=>10, "prop2"=>"string10"}
41
+ > {"@type"=>"d", "@version"=>0, "max"=>5}
42
+
43
+ puts client.query "TRAVERSE any() FROM (SELECT FROM #{CLASS} WHERE prop1 = 1)"
44
+ > {"@type"=>"d", "@rid"=>"#6:0", "@version"=>0, "@class"=>"myclass", "prop1"=>1, "prop2"=>"text1", "users"=>["#4:0"]}
45
+ > {"@type"=>"d", "@rid"=>"#4:0", "@version"=>0, "@class"=>"OUser", "name"=>"admin", "password"=>"{SHA-256}8C6976E5B5410415BDE908BD4DEE15DFB167A9C873FC4BB8A81F6F2AB448A918", "status"=>"ACTIVE", "roles"=>["#3:0"]}
46
+ > {"@type"=>"d", "@rid"=>"#3:0", "@version"=>0, "@class"=>"ORole", "name"=>"admin", "mode"=>1, "rules"=>{}, "@fieldTypes"=>"mode=b"}
45
47
 
46
48
 
47
49
  client.drop_class CLASS
@@ -55,8 +57,9 @@ see Wiki page for more sample at https://github.com/veny/orientdb4r/wiki
55
57
  * gem published on http://rubygems.org/gems/orientdb4r
56
58
 
57
59
  === Important Upgrade Notice
58
- 2012-06-19 [v0.2.1]: Client#create_class - parameter given to a block has new method 'link' to define a linked property
59
- 2012-06-17 [v0.2.0]: Client#get_class raises Orientdb4r::NotFoundError instead of ArgumentError
60
+
61
+ * 2012-06-19 [v0.2.1]: Client#create_class - parameter given to a block has new method 'link' to define a linked property
62
+ * 2012-06-17 [v0.2.0]: Client#get_class raises Orientdb4r::NotFoundError instead of ArgumentError
60
63
 
61
64
 
62
65
 
@@ -3,6 +3,11 @@ module Orientdb4r
3
3
  class Client
4
4
  include Utils
5
5
 
6
+ DEFAULT_SERVER_VERSION = '1.0.0--'
7
+ SERVER_VERSION_PATTERN = /^\d+\.\d+\.\d+/
8
+
9
+ attr_reader :server_version
10
+
6
11
  ###
7
12
  # Constructor.
8
13
  def initialize
@@ -155,25 +160,6 @@ module Orientdb4r
155
160
  end
156
161
 
157
162
 
158
- ###
159
- # Creates links between two or more records of type Document.
160
- # You need to create the class before.
161
- # def create_link(clazz, options={})
162
- # raise ArgumentError, "class name is blank" if blank?(clazz)
163
- # opt_pattern = {
164
- # :name => :optional, :type => :optional,
165
- # :source_property => :mandatory, :destination_class => :mandatory, :destination_property => :mandatory
166
- # }
167
- # verify_options(options, opt_pattern)
168
- #
169
- # cmd = 'CREATE LINK '
170
- # cmd << "#{options[:name]} " if options.include? :name
171
- # cmd << "TYPE #{options[:type]} " if options.include? :type
172
- # cmd << "FROM #{clazz}.#{options[:source_property]} TO #{options[:destination_class]}.#{options[:destination_property]}"
173
- #puts cmd
174
- # command cmd
175
- # end
176
-
177
163
  # ----------------------------------------------------------------- DOCUMENT
178
164
 
179
165
  ###
@@ -3,7 +3,7 @@ module Orientdb4r
3
3
  class RestClient < Client
4
4
  include Aop2
5
5
 
6
- before [:query, :command, :get_class], :assert_connected
6
+ before [:create_database, :get_class, :query, :command], :assert_connected
7
7
  around [:query, :command], :time_around
8
8
 
9
9
  attr_reader :host, :port, :ssl, :user, :password
@@ -34,6 +34,15 @@ module Orientdb4r
34
34
 
35
35
  decorate_classes_with_model(rslt['classes'])
36
36
 
37
+ # try to read server version
38
+ if rslt.include? 'server'
39
+ @server_version = rslt['server']['version']
40
+ else
41
+ @server_version = DEFAULT_SERVER_VERSION
42
+ end
43
+ raise OrientdbError, "bad version format, version=#{server_version}" unless server_version =~ SERVER_VERSION_PATTERN
44
+ Orientdb4r::logger.debug "successfully connected to server, version=#{server_version}"
45
+
37
46
  @connected = true
38
47
  rescue
39
48
  @connected = false
@@ -80,16 +89,24 @@ module Orientdb4r
80
89
  def get_class(name) #:nodoc:
81
90
  raise ArgumentError, "class name is blank" if blank?(name)
82
91
 
83
- # there seems to be a bug in REST API, only data are returned
84
- #response = @resource["class/#{@database}/#{name}"].get
85
- #rslt = process_response(response)
92
+ if compare_versions(server_version, '1.1.0') >= 0
93
+ begin
94
+ response = @resource["class/#{@database}/#{name}"].get
95
+ rescue
96
+ raise NotFoundError
97
+ end
98
+ rslt = process_response(response, :mode => :strict)
99
+ classes = [rslt]
100
+ else
101
+ # there is bug in REST API [v1.0.0, fixed in r5902], only data are returned
102
+ # workaround - use metadate delivered by 'connect'
103
+ response = @resource["connect/#{@database}"].get
104
+ connect_info = process_response(response, :mode => :strict)
86
105
 
87
- # workaround - use metadate delivered by 'connect'
88
- response = @resource["connect/#{@database}"].get
89
- connect_info = process_response(response, :mode => :strict)
106
+ classes = connect_info['classes'].select { |i| i['name'] == name }
107
+ raise NotFoundError, "class not found, name=#{name}" unless 1 == classes.size
108
+ end
90
109
 
91
- classes = connect_info['classes'].select { |i| i['name'] == name }
92
- raise NotFoundError, "class not found, name=#{name}" unless 1 == classes.size
93
110
  decorate_classes_with_model(classes)
94
111
  clazz = classes[0]
95
112
  clazz.extend Orientdb4r::HashExtension
@@ -101,7 +118,6 @@ module Orientdb4r
101
118
  end
102
119
  end
103
120
 
104
-
105
121
  clazz
106
122
  end
107
123
 
@@ -252,6 +268,7 @@ module Orientdb4r
252
268
 
253
269
  def decorate_classes_with_model(classes)
254
270
  classes.each do |clazz|
271
+ #puts "OOO #{classes}"
255
272
  clazz.extend Orientdb4r::HashExtension
256
273
  clazz.extend Orientdb4r::OClass
257
274
  unless clazz['properties'].nil? # there can be a class without properties
@@ -46,6 +46,22 @@ module Orientdb4r
46
46
  (0...len).map{65.+(rand(25)).chr}.join
47
47
  end
48
48
 
49
+ ###
50
+ # Compares two given versions.
51
+ # ==== Returns
52
+ # * 1 if first > second
53
+ # * 0 if first == second
54
+ # * -1 if first < second
55
+ def compare_versions(first, second)
56
+ raise ArgumentError, "bad version format, version=#{first}" unless first =~ Orientdb4r::Client::SERVER_VERSION_PATTERN
57
+ raise ArgumentError, "bad version format, version=#{second}" unless second =~ Orientdb4r::Client::SERVER_VERSION_PATTERN
58
+
59
+ firstv = /^(?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)/.match(first)[0]
60
+ secondv = /^(?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)/.match(second)[0]
61
+
62
+ return 0 if firstv == secondv
63
+ return firstv > secondv ? 1 : -1
64
+ end
49
65
 
50
66
  class Proxy
51
67
 
@@ -2,6 +2,9 @@ module Orientdb4r
2
2
 
3
3
  # Version history.
4
4
  VERSION_HISTORY = [
5
+ # https://groups.google.com/forum/?fromgroups#!topic/orient-database/jK4EZd068AE
6
+ # https://groups.google.com/forum/?fromgroups#!topic/orient-database/nJOAsgwSnKI
7
+ ['0.2.2', '2012-06-23', "Added support for server version detection [r5913]"],
5
8
  ['0.2.1', '2012-06-19', "Fixed linked property definition"],
6
9
  ['0.2.0', '2012-06-12', "Introduces document's CRUD operations"],
7
10
  ['0.1.2', '2012-06-10', 'Introduces new OClass module'],
@@ -0,0 +1,35 @@
1
+ require 'orientdb4r'
2
+
3
+ CLASS = 'myclass'
4
+
5
+ client = Orientdb4r.client # equivalent for :host => 'localhost', :port => 2480, :ssl => false
6
+
7
+ client.connect :database => 'temp', :user => 'admin', :password => 'admin'
8
+
9
+ client.create_class(CLASS) do |c|
10
+ c.property 'prop1', :integer, :notnull => true, :min => 1, :max => 99
11
+ c.property 'prop2', :string, :mandatory => true
12
+ c.link 'users', :linkset, 'OUser' # by default: :mandatory => false, :notnull => false
13
+ end
14
+
15
+ admin = client.query("SELECT FROM OUser WHERE name = 'admin'")[0]
16
+ 1.upto(5) do |i|
17
+ # insert link to admin only to first two
18
+ client.command "INSERT INTO #{CLASS} (prop1, prop2, users) VALUES (#{i}, 'text#{i}', [#{admin['@rid'] if i<3}])"
19
+ end
20
+
21
+ puts client.query "SELECT FROM #{CLASS}"
22
+ #>
23
+
24
+ puts client.query "SELECT count(*) FROM #{CLASS}"
25
+ #>
26
+
27
+ puts client.query "SELECT max(prop1) FROM #{CLASS}"
28
+ #>
29
+
30
+ puts client.query "TRAVERSE any() FROM (SELECT FROM #{CLASS} WHERE prop1 = 1)"
31
+ #>
32
+
33
+
34
+ client.drop_class CLASS
35
+ client.disconnect
@@ -22,6 +22,7 @@ class TestDatabase < Test::Unit::TestCase
22
22
  assert_instance_of Hash, rslt
23
23
  assert rslt.size > 0
24
24
  assert rslt.include? 'classes'
25
+ assert_not_nil @client.server_version
25
26
 
26
27
  # connection refused
27
28
  client = Orientdb4r.client :port => 2840, :instance => :new
@@ -43,7 +44,9 @@ class TestDatabase < Test::Unit::TestCase
43
44
  ###
44
45
  # DISCONNECT
45
46
  def test_disconnect
46
- @client.disconnect
47
+ @client.connect :database => 'temp', :user => 'admin', :password => 'admin'
48
+ assert @client.connected?
49
+ assert_nothing_thrown do @client.disconnect; end
47
50
  assert !@client.connected?
48
51
  # unable to query after disconnect
49
52
  assert_raise Orientdb4r::ConnectionError do @client.query 'SELECT FROM OUser'; end
data/test/test_dmo.rb CHANGED
@@ -31,7 +31,7 @@ class TestDmo < Test::Unit::TestCase
31
31
 
32
32
  ###
33
33
  # INSERT INTO
34
- def xtest_insert
34
+ def test_insert
35
35
  assert_nothing_thrown do
36
36
  1.upto(10) do |i|
37
37
  @client.command "INSERT INTO #{CLASS} (prop1, prop2, friends) VALUES (#{i}, '#{random_string}', [#{@admin['@rid']}])"
data/test/test_utils.rb CHANGED
@@ -27,4 +27,25 @@ class TestDmo < Test::Unit::TestCase
27
27
  assert_equal 'B', options[:b]
28
28
  end
29
29
 
30
+ def test_compare_versions
31
+ assert_raise ArgumentError do compare_versions 'foo', 'bar'; end
32
+ assert_raise ArgumentError do compare_versions nil, 'bar'; end
33
+ assert_raise ArgumentError do compare_versions 'foo', nil; end
34
+ assert_raise ArgumentError do compare_versions '1.0.0', 'bar'; end
35
+ assert_raise ArgumentError do compare_versions 'foo', '1.0.0'; end
36
+ assert_nothing_thrown do compare_versions '1.0.0', '1.1.0'; end
37
+
38
+ assert_equal 0, compare_versions('1.0.0', '1.0.0')
39
+ assert_equal 0, compare_versions('1.2.0', '1.2.0')
40
+ assert_equal 0, compare_versions('1.2.3', '1.2.3')
41
+
42
+ assert_equal 1, compare_versions('1.0.1', '1.0.0')
43
+ assert_equal 1, compare_versions('1.1.0', '1.0.0')
44
+ assert_equal 1, compare_versions('2.0.0', '1.0.0')
45
+
46
+ assert_equal -1, compare_versions('1.0.0', '1.0.1')
47
+ assert_equal -1, compare_versions('1.0.0', '1.1.0')
48
+ assert_equal -1, compare_versions('1.0.0', '2.0.0')
49
+ end
50
+
30
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orientdb4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-19 00:00:00.000000000 Z
12
+ date: 2012-06-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -48,6 +48,7 @@ files:
48
48
  - lib/orientdb4r/utils.rb
49
49
  - lib/orientdb4r/version.rb
50
50
  - orientdb4r.gemspec
51
+ - test/readme_sample.rb
51
52
  - test/test_database.rb
52
53
  - test/test_ddo.rb
53
54
  - test/test_dmo.rb
@@ -74,11 +75,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
75
  version: 1.3.1
75
76
  requirements: []
76
77
  rubyforge_project:
77
- rubygems_version: 1.8.19
78
+ rubygems_version: 1.8.23
78
79
  signing_key:
79
80
  specification_version: 3
80
81
  summary: Ruby binding for Orient DB.
81
82
  test_files:
83
+ - test/readme_sample.rb
82
84
  - test/test_database.rb
83
85
  - test/test_ddo.rb
84
86
  - test/test_dmo.rb