orientdb 1.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.
Files changed (48) hide show
  1. data/.document +5 -0
  2. data/Gemfile +8 -0
  3. data/Gemfile.lock +32 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +88 -0
  6. data/Rakefile +57 -0
  7. data/bin/orientdb_console +23 -0
  8. data/lib/jars/blueprints-core-2.2.0-SNAPSHOT.jar +0 -0
  9. data/lib/jars/blueprints-orient-graph-2.2.0-SNAPSHOT.jar +0 -0
  10. data/lib/jars/orient-commons-1.2.0.jar +0 -0
  11. data/lib/jars/orientdb-client-1.2.0.jar +0 -0
  12. data/lib/jars/orientdb-core-1.2.0.jar +0 -0
  13. data/lib/jars/orientdb-distributed-1.2.0.jar +0 -0
  14. data/lib/jars/orientdb-enterprise-1.2.0.jar +0 -0
  15. data/lib/jars/orientdb-graphdb-1.2.0.jar +0 -0
  16. data/lib/jars/orientdb-server-1.2.0.jar +0 -0
  17. data/lib/jars/orientdb-tools-1.2.0.jar +0 -0
  18. data/lib/jars/pipes-2.0.0-SNAPSHOT.jar +0 -0
  19. data/lib/orientdb.rb +29 -0
  20. data/lib/orientdb/constants.rb +57 -0
  21. data/lib/orientdb/database.rb +167 -0
  22. data/lib/orientdb/document.rb +78 -0
  23. data/lib/orientdb/ext.rb +13 -0
  24. data/lib/orientdb/oclass.rb +141 -0
  25. data/lib/orientdb/property.rb +37 -0
  26. data/lib/orientdb/record.rb +18 -0
  27. data/lib/orientdb/rid.rb +46 -0
  28. data/lib/orientdb/schema.rb +33 -0
  29. data/lib/orientdb/sql.rb +18 -0
  30. data/lib/orientdb/sql/common.rb +247 -0
  31. data/lib/orientdb/sql/delete.rb +23 -0
  32. data/lib/orientdb/sql/ext.rb +249 -0
  33. data/lib/orientdb/sql/insert.rb +37 -0
  34. data/lib/orientdb/sql/query.rb +138 -0
  35. data/lib/orientdb/sql/update.rb +57 -0
  36. data/lib/orientdb/storage.rb +51 -0
  37. data/lib/orientdb/version.rb +3 -0
  38. data/orientdb.gemspec +95 -0
  39. data/spec/database_spec.rb +111 -0
  40. data/spec/document_spec.rb +99 -0
  41. data/spec/graph_spec.rb +39 -0
  42. data/spec/orientdb_spec.rb +10 -0
  43. data/spec/spec.opts +7 -0
  44. data/spec/spec_basic_helper.rb +25 -0
  45. data/spec/spec_helper.rb +68 -0
  46. data/spec/sql_spec.rb +839 -0
  47. data/spec/tinkerpop_graph_spec.rb +32 -0
  48. metadata +165 -0
@@ -0,0 +1,10 @@
1
+ require File.expand_path("../spec_helper", __FILE__)
2
+
3
+ describe "OrientDB" do
4
+
5
+ it "should create a valid database" do
6
+ DB.should be_a_kind_of OrientDB::DocumentDatabase
7
+ DB.name.should == "test"
8
+ end
9
+
10
+ end
@@ -0,0 +1,7 @@
1
+ --colour
2
+ --format
3
+ nested
4
+ --loadby
5
+ mtime
6
+ --reverse
7
+ --backtrace
@@ -0,0 +1,25 @@
1
+ unless defined?(SPEC_BASIC_HELPER_LOADED)
2
+
3
+ GEM_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
4
+ LIB_ROOT = GEM_ROOT + '/lib'
5
+ SPEC_ROOT = GEM_ROOT + '/spec'
6
+ TEMP_DIR = SPEC_ROOT + '/tmp'
7
+ puts ">> GEM_ROOT : #{GEM_ROOT}"
8
+
9
+ $LOAD_PATH.unshift(LIB_ROOT) unless $LOAD_PATH.include?(LIB_ROOT)
10
+
11
+ require 'orientdb'
12
+ require 'rspec'
13
+ #require 'rspec/autorun'
14
+ require 'fileutils'
15
+
16
+ RSpec.configure do |config|
17
+ config.color_enabled = true
18
+ end
19
+
20
+ class Developer
21
+
22
+ end
23
+
24
+ SPEC_BASIC_HELPER_LOADED = true
25
+ end
@@ -0,0 +1,68 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+
3
+ unless defined?(SPEC_HELPER_LOADED)
4
+
5
+ require 'spec_basic_helper'
6
+
7
+ TEST_URL = ENV["ORIENTDB_TEST_URL"]
8
+ TEST_USERNAME = ENV["ORIENTDB_TEST_USERNAME"] || 'admin'
9
+ TEST_PASSWORD = ENV["ORIENTDB_TEST_PASSWORD"] || 'admin'
10
+ TEST_POOLED = ENV["ORIENTDB_TEST_POOLED"].to_s[0, 1].downcase == 't'
11
+
12
+ puts "ENV :: TEST_URL : #{TEST_URL} | TEST_USERNAME : #{TEST_USERNAME} | TEST_PASSWORD : #{TEST_PASSWORD} | TEST_POOLED : #{TEST_POOLED}"
13
+
14
+ if TEST_URL
15
+ if TEST_POOLED
16
+ puts ">> Testing [#{TEST_URL[0,TEST_URL.index(':')]}] Pooled Database :: TEST_DB URL : #{TEST_URL} : #{TEST_USERNAME} : #{TEST_PASSWORD}"
17
+ DB = OrientDB::DocumentDatabasePool.connect(TEST_URL, TEST_USERNAME, TEST_PASSWORD)
18
+ else
19
+ puts ">> Testing [#{TEST_URL[0,TEST_URL.index(':')]}] Database :: TEST_DB URL : #{TEST_URL} : #{TEST_USERNAME} : #{TEST_PASSWORD}"
20
+ DB = OrientDB::DocumentDatabase.connect(TEST_URL, TEST_USERNAME, TEST_PASSWORD)
21
+ end
22
+ else
23
+ TEST_DB_PATH = "#{TEMP_DIR}/databases/db_#{rand(999) + 1}"
24
+ require 'fileutils'
25
+ FileUtils.remove_dir "#{TEMP_DIR}/databases" rescue nil
26
+ FileUtils.mkdir_p TEST_DB_PATH
27
+ puts ">> Testing [local] Database :: TEST_DB PATH : #{TEST_DB_PATH}"
28
+ FileUtils.remove_dir "#{TEMP_DIR}/databases/"
29
+ FileUtils.mkdir_p TEST_DB_PATH
30
+ puts ">> TEST_DB PATH : #{TEST_DB_PATH}"
31
+ DB = OrientDB::DocumentDatabase.new("local:#{TEST_DB_PATH}/test").create
32
+ end
33
+
34
+ module Helpers
35
+ def create_classes
36
+ @person_class = DB.recreate_class :person, :name => :string
37
+ @customer_class = DB.recreate_class :customer,
38
+ :super => @person_class,
39
+ :tab => :float
40
+ @employee_class = DB.recreate_class :employee,
41
+ :super => @person_class,
42
+ :age => :int,
43
+ :groups => [:embedded_list, :string]
44
+ @product_class = DB.recreate_class :product,
45
+ :sku => :string,
46
+ :title => :string,
47
+ :price => :float
48
+ @line_class = DB.recreate_class :invoice_line,
49
+ :product => @product_class,
50
+ :quantity => :int,
51
+ :price => :float
52
+ @invoice_class = DB.recreate_class :invoice,
53
+ :number => {:type => :int, :mandatory => true, :index => true},
54
+ :customer => {:type => @customer_class, :not_null => true},
55
+ :sold_on => :date,
56
+ :total => {:type => :float},
57
+ :lines => [:link_list, @line_class]
58
+ end
59
+ end
60
+
61
+ RSpec.configure do |config|
62
+ include Helpers
63
+
64
+ config.color_enabled = true
65
+ end
66
+
67
+ SPEC_HELPER_LOADED = true
68
+ end
@@ -0,0 +1,839 @@
1
+ require File.expand_path("../spec_basic_helper", __FILE__)
2
+
3
+ describe "OrientDB" do
4
+
5
+ describe "SQL" do
6
+
7
+ describe "Common" do
8
+
9
+ describe "quote" do
10
+ it "should quote numeric values correctly" do
11
+ OrientDB::SQL::Query.quote(1).should == "1"
12
+ OrientDB::SQL::Query.quote(1.1).should == "1.1"
13
+ OrientDB::SQL::Query.quote(10_000_000).should == "10000000"
14
+ end
15
+
16
+ it "should quote symbols correctly" do
17
+ OrientDB::SQL::Query.quote(:name).should == "name"
18
+ end
19
+
20
+ it "should quote strings correctly" do
21
+ OrientDB::SQL::Query.quote("name").should == "'name'"
22
+ OrientDB::SQL::Query.quote("'name'").should == "'name'"
23
+ OrientDB::SQL::Query.quote("O'Brien").should == "'O\\'Brien'"
24
+ OrientDB::SQL::Query.quote("'O\\'Brien'").should == "'O\\'Brien'"
25
+ OrientDB::SQL::Query.quote("O'Brien & O'Malley").should == "'O\\'Brien & O\\'Malley'"
26
+ end
27
+
28
+ it "should quote arrays correctly" do
29
+ OrientDB::SQL::Query.quote([:a, 'b', 1]).should == "[a, 'b', 1]"
30
+ end
31
+
32
+ it "should quote regular expressions correctly" do
33
+ OrientDB::SQL::Query.quote(/\d{1,3}\.\d{1,3}/).should == "'\\d{1,3}\\.\\d{1,3}'"
34
+ end
35
+
36
+ it "should quote record attributes properly" do
37
+ OrientDB::SQL::Query.quote(:@this).should == "@this"
38
+ OrientDB::SQL::Query.quote(:@rid).should == "@rid"
39
+ OrientDB::SQL::Query.quote(:@class).should == "@class"
40
+ OrientDB::SQL::Query.quote(:@version).should == "@version"
41
+ OrientDB::SQL::Query.quote(:@size).should == "@size"
42
+ OrientDB::SQL::Query.quote(:@type).should == "@type"
43
+ end
44
+
45
+ it "should quote literal_expressions properly" do
46
+ value = OrientDB::SQL::LiteralExpression.new "@this"
47
+ OrientDB::SQL::Query.quote(:@this).should == "@this"
48
+ value = OrientDB::SQL::LiteralExpression.new :@this
49
+ OrientDB::SQL::Query.quote(:@this).should == "@this"
50
+ end
51
+ end
52
+
53
+ describe "select_single_string" do
54
+ it "should split extend and alias strings correctly" do
55
+ OrientDB::SQL::Query.select_single_string('a__b___c').should == 'a.b AS c'
56
+ end
57
+
58
+ it "should split extend strings correctly" do
59
+ OrientDB::SQL::Query.select_single_string('a__b').should == 'a.b'
60
+ end
61
+
62
+ it "should split extended strings correctly" do
63
+ OrientDB::SQL::Query.select_single_string('a__b__c').should == 'a.b.c'
64
+ end
65
+
66
+ it "should split alias strings correctly" do
67
+ OrientDB::SQL::Query.select_single_string('a___c').should == 'a AS c'
68
+ end
69
+ end
70
+
71
+ describe "field_name" do
72
+ it "should split simple names correctly" do
73
+ OrientDB::SQL::Query.field_name(:a__b).should == 'a.b'
74
+ end
75
+
76
+ it "should split longer names correctly" do
77
+ OrientDB::SQL::Query.field_name(:a__b__c).should == 'a.b.c'
78
+ end
79
+ end
80
+
81
+ end
82
+
83
+ describe "Query" do
84
+
85
+ it "should do a blank query" do
86
+ @q = OrientDB::SQL::Query.new
87
+ @q.should be_a_kind_of OrientDB::SQL::Query
88
+ @q.to_s.should == 'SELECT FROM'
89
+ end
90
+
91
+ describe "SELECT" do
92
+
93
+ before :each do
94
+ @q = OrientDB::SQL::Query.new
95
+ end
96
+
97
+ it "should select simple string columns" do
98
+ @q.select('name').to_s.should == 'SELECT name FROM'
99
+ end
100
+
101
+ it "should select with extended dots on __ separated strings" do
102
+ @q.select('name__a').to_s.should == 'SELECT name.a FROM'
103
+ end
104
+
105
+ it "should select as alias on ___ separated strings" do
106
+ @q.select('name___a').to_s.should == 'SELECT name AS a FROM'
107
+ end
108
+
109
+ it "should select as mixed alias/dots strings" do
110
+ @q.select('name__a___b').to_s.should == 'SELECT name.a AS b FROM'
111
+ end
112
+
113
+ it "should select simple symbol columns" do
114
+ @q.select(:name).to_s.should == 'SELECT name FROM'
115
+ end
116
+
117
+ it "should select simple integer columns" do
118
+ @q.select(1).to_s.should == 'SELECT 1 FROM'
119
+ end
120
+
121
+ it "should select simple hashes" do
122
+ @q.select(:name => :a).to_s.should == 'SELECT name AS a FROM'
123
+ end
124
+
125
+ it "should select simple two column arrays as aliases" do
126
+ @q.select(['name', 'a']).to_s.should == 'SELECT name AS a FROM'
127
+ end
128
+
129
+ it "should select simple n-column arrays as entries" do
130
+ @q.select(['name', 'age', 'code']).to_s.should == 'SELECT name, age, code FROM'
131
+ end
132
+
133
+ it "should select simple multiple parameters as entries" do
134
+ @q.select('name', :age, 1, [:a, :b], :x => :y).to_s.should == 'SELECT name, age, 1, a AS b, x AS y FROM'
135
+ end
136
+
137
+ it "should select simple multiple parameters as entries using columns" do
138
+ @q.columns('name', :age, 1, [:a, :b], :x => :y).to_s.should == 'SELECT name, age, 1, a AS b, x AS y FROM'
139
+ end
140
+
141
+ it "should handle concatenation" do
142
+ @q.select('name').select(:age).to_s.should == 'SELECT name, age FROM'
143
+ end
144
+
145
+ it "should handle overriding" do
146
+ @q.select('name').select!(:age).to_s.should == 'SELECT age FROM'
147
+ end
148
+ end
149
+
150
+ describe "FROM" do
151
+
152
+ before :each do
153
+ @q = OrientDB::SQL::Query.new
154
+ end
155
+
156
+ it "should use a simple string target" do
157
+ @q.from('Developer').to_s.should == 'SELECT FROM Developer'
158
+ end
159
+
160
+ it "should use a simple symbol target" do
161
+ @q.from(:Developer).to_s.should == 'SELECT FROM Developer'
162
+ end
163
+
164
+ it "should use a simple object target" do
165
+ @q.from(Developer).to_s.should == 'SELECT FROM Developer'
166
+ end
167
+
168
+ it "should use multiple simple string targets" do
169
+ @q.from('5:1', '5:3', '5:5').to_s.should == 'SELECT FROM [5:1, 5:3, 5:5]'
170
+ end
171
+
172
+ it "should handle concatenation" do
173
+ @q.from('5:1').from('5:3').from('5:5').to_s.should == 'SELECT FROM [5:1, 5:3, 5:5]'
174
+ end
175
+
176
+ it "should handle overriding" do
177
+ @q.from('5:1').from!('5:5').to_s.should == 'SELECT FROM 5:5'
178
+ end
179
+ end
180
+
181
+ describe "WHERE" do
182
+
183
+ before :each do
184
+ @q = OrientDB::SQL::Query.new
185
+ end
186
+
187
+ it "should handle simple hashes" do
188
+ @q.where(:a => 1).to_s.should == 'SELECT FROM WHERE a = 1'
189
+ end
190
+
191
+ it "should handle simple arrays" do
192
+ @q.where(['a > 1', 'b < 3', 'c = 5']).to_s.should == 'SELECT FROM WHERE a > 1 AND b < 3 AND c = 5'
193
+ end
194
+
195
+ it "should handle concatenation" do
196
+ @q.where(:a => 1).where(:b => 2).to_s.should == 'SELECT FROM WHERE a = 1 AND b = 2'
197
+ end
198
+
199
+ it "should handle overriding" do
200
+ @q.where(:a => 1).where!(:b => 2).to_s.should == 'SELECT FROM WHERE b = 2'
201
+ end
202
+
203
+ it "should handle simple joined AND conditions" do
204
+ @q.where(:a => 1).and(:b => 2).to_s.should == 'SELECT FROM WHERE a = 1 AND b = 2'
205
+ end
206
+
207
+ it "should handle simple joined OR conditions" do
208
+ @q.where(:a => 1).or(:b => 2).to_s.should == 'SELECT FROM WHERE a = 1 OR b = 2'
209
+ end
210
+
211
+ it "should handle simple joined AND NOT conditions" do
212
+ @q.where(:a => 1).and_not(:b => 2).to_s.should == 'SELECT FROM WHERE a = 1 AND NOT b = 2'
213
+ end
214
+
215
+ it "should handle simple joined OR NOT conditions" do
216
+ @q.where(:a => 1).or_not(:b => 2).to_s.should == 'SELECT FROM WHERE a = 1 OR NOT b = 2'
217
+ end
218
+
219
+ it "should handle complex joined AND conditions (1)" do
220
+ @q.where(:a => 1, :b => 2).and(:c => 3).to_s.should == 'SELECT FROM WHERE (a = 1 AND b = 2) AND c = 3'
221
+ end
222
+
223
+ it "should handle complex joined AND conditions (2)" do
224
+ @q.where(:a => 1, :b => 2).and(:c => 3, :d => 4).to_s.should == 'SELECT FROM WHERE (a = 1 AND b = 2) AND (c = 3 AND d = 4)'
225
+ end
226
+
227
+ it "should handle complex joined OR conditions (1)" do
228
+ @q.where(:a => 1, :b => 2).or(:c => 3).to_s.should == 'SELECT FROM WHERE (a = 1 AND b = 2) OR c = 3'
229
+ end
230
+
231
+ it "should handle complex joined OR conditions (2)" do
232
+ @q.where(:a => 1, :b => 2).or(:c => 3, :d => 4).to_s.should == 'SELECT FROM WHERE (a = 1 AND b = 2) OR (c = 3 AND d = 4)'
233
+ end
234
+
235
+ it "should handle complex joined AND NOT conditions (1)" do
236
+ @q.where(:a => 1, :b => 2).and_not(:c => 3).to_s.should == 'SELECT FROM WHERE (a = 1 AND b = 2) AND NOT c = 3'
237
+ end
238
+
239
+ it "should handle complex joined AND NOT conditions (2)" do
240
+ @q.where(:a => 1, :b => 2).and_not(:c => 3, :d => 4).to_s.should == 'SELECT FROM WHERE (a = 1 AND b = 2) AND NOT (c = 3 AND d = 4)'
241
+ end
242
+
243
+ it "should handle complex joined OR NOT conditions (1)" do
244
+ @q.where(:a => 1, :b => 2).or_not(:c => 3).to_s.should == 'SELECT FROM WHERE (a = 1 AND b = 2) OR NOT c = 3'
245
+ end
246
+
247
+ it "should handle complex joined OR NOT conditions (2)" do
248
+ @q.where(:a => 1, :b => 2).or_not(:c => 3, :d => 4).to_s.should == 'SELECT FROM WHERE (a = 1 AND b = 2) OR NOT (c = 3 AND d = 4)'
249
+ end
250
+ end
251
+
252
+ describe "ORDER BY" do
253
+
254
+ before :each do
255
+ @q = OrientDB::SQL::Query.new
256
+ end
257
+
258
+ it "should handle simple hashes" do
259
+ @q.order(:a => :desc).to_s.should == 'SELECT FROM ORDER BY a DESC'
260
+ end
261
+
262
+ it "should handle two column arrays" do
263
+ @q.order([:a, :desc]).to_s.should == 'SELECT FROM ORDER BY a DESC'
264
+ end
265
+
266
+ it "should handle n column arrays" do
267
+ @q.order([:a, :b, :c]).to_s.should == 'SELECT FROM ORDER BY a, b, c'
268
+ end
269
+
270
+ it "should handle simple entries" do
271
+ @q.order(:a, 'b').to_s.should == 'SELECT FROM ORDER BY a, b'
272
+ end
273
+
274
+ it "should default to ASC on unrecognized directions" do
275
+ @q.order(:a => :unknown).to_s.should == 'SELECT FROM ORDER BY a ASC'
276
+ end
277
+
278
+ it "should handle concatenation" do
279
+ @q.order(:a).order(:b).to_s.should == 'SELECT FROM ORDER BY a, b'
280
+ end
281
+
282
+ it "should handle overriding" do
283
+ @q.order(:a).order!(:b).to_s.should == 'SELECT FROM ORDER BY b'
284
+ end
285
+ end
286
+
287
+ describe "LIMIT" do
288
+
289
+ before :each do
290
+ @q = OrientDB::SQL::Query.new
291
+ end
292
+
293
+ it "should handle integers" do
294
+ @q.limit(1).to_s.should == 'SELECT FROM LIMIT 1'
295
+ end
296
+
297
+ it "should handle strings" do
298
+ @q.limit('1').to_s.should == 'SELECT FROM LIMIT 1'
299
+ end
300
+
301
+ it "should handle other types" do
302
+ @q.limit(:'1').to_s.should == 'SELECT FROM LIMIT 1'
303
+ end
304
+
305
+ it "should override on concatenation" do
306
+ @q.limit(2).limit(1).to_s.should == 'SELECT FROM LIMIT 1'
307
+ end
308
+
309
+ it "should override on bang (alias)" do
310
+ @q.limit!(2).limit!(1).to_s.should == 'SELECT FROM LIMIT 1'
311
+ end
312
+ end
313
+
314
+ describe "RANGE" do
315
+
316
+ before :each do
317
+ @q = OrientDB::SQL::Query.new
318
+ end
319
+
320
+ it "should handle lower only" do
321
+ @q.range('1:1').to_s.should == 'SELECT FROM RANGE 1:1'
322
+ end
323
+
324
+ it "should handle lower and higher" do
325
+ @q.range('1:1', '1:5').to_s.should == 'SELECT FROM RANGE 1:1, 1:5'
326
+ end
327
+
328
+ it "should override on concatenation" do
329
+ @q.range('2:1', '2:5').range('1:1').to_s.should == 'SELECT FROM RANGE 1:1'
330
+ end
331
+
332
+ it "should override on bang (alias)" do
333
+ @q.range!('2:1', '2:5').range!('1:1').to_s.should == 'SELECT FROM RANGE 1:1'
334
+ end
335
+ end
336
+
337
+ describe "Monkey Patching" do
338
+ describe Symbol do
339
+ OrientDB::SQL.monkey_patch! Symbol
340
+
341
+ describe "Order" do
342
+ it "#asc should work" do
343
+ :name.asc.should == 'name ASC'
344
+ end
345
+
346
+ it "#desc should work" do
347
+ :name.desc.should == 'name DESC'
348
+ end
349
+ end
350
+
351
+ describe "Conditional" do
352
+ it "should work" do
353
+ :name.lit.should == OrientDB::SQL::LiteralExpression.new("name")
354
+ end
355
+
356
+ it "#like should work" do
357
+ :name.like("test%").should == "name LIKE 'test%'"
358
+ end
359
+
360
+ it "#eq should work" do
361
+ :age.eq(35).should == "age = 35"
362
+ end
363
+
364
+ it "#ne should work" do
365
+ :age.ne(35).should == "age <> 35"
366
+ end
367
+
368
+ it "#lt should work" do
369
+ :age.lt(35).should == "age < 35"
370
+ end
371
+
372
+ it "#lte should work" do
373
+ :age.lte(35).should == "age <= 35"
374
+ end
375
+
376
+ it "#gt should work" do
377
+ :age.gt(35).should == "age > 35"
378
+ end
379
+
380
+ it "#gte should work" do
381
+ :age.gte(35).should == "age >= 35"
382
+ end
383
+
384
+ it "#is_null should work" do
385
+ :name.is_null.should == "name IS null"
386
+ end
387
+
388
+ it "#is_not_null should work" do
389
+ :name.is_not_null.should == "name IS NOT null"
390
+ end
391
+
392
+ it "#in should work" do
393
+ :age.in(34, 36, 38).should == "age IN [34, 36, 38]"
394
+ end
395
+
396
+ it "#contains should work" do
397
+ :name.contains(:name, "tester").should == "name contains (name = 'tester')"
398
+ end
399
+
400
+ it "#contains_all should work" do
401
+ :name.contains_all(:name, "tester").should == "name containsAll (name = 'tester')"
402
+ end
403
+
404
+ it "#contains_key should work" do
405
+ :name.contains_key("tester").should == "name containsKey 'tester'"
406
+ end
407
+
408
+ it "#contains_value should work" do
409
+ :name.contains_value("tester").should == "name containsValue 'tester'"
410
+ end
411
+
412
+ it "#contains_text should work" do
413
+ :name.contains_text("tester").should == "name containsText 'tester'"
414
+ end
415
+
416
+ it "#matches should work" do
417
+ :name.matches(/(john|mark)/).should == "name matches '(john|mark)'"
418
+ end
419
+ end
420
+
421
+ describe "Field Operators" do
422
+ it "#odb_length should work" do
423
+ :name.odb_length.should == "name.length()"
424
+ end
425
+
426
+ it "#odb_trim should work" do
427
+ :name.odb_trim.should == "name.trim()"
428
+ end
429
+
430
+ it "#to_upper_case should work" do
431
+ :name.to_upper_case.should == "name.toUpperCase()"
432
+ end
433
+
434
+ it "#to_lower_case should work" do
435
+ :name.to_lower_case.should == "name.toLowerCase()"
436
+ end
437
+
438
+ it "#odb_left should work" do
439
+ :name.odb_left(5).should == "name.left(5)"
440
+ end
441
+
442
+ it "#odb_right should work" do
443
+ :name.odb_right(5).should == "name.right(5)"
444
+ end
445
+
446
+ it "#sub_string should work" do
447
+ :name.sub_string(3).should == "name.subString(3)"
448
+ :name.sub_string(3, 5).should == "name.subString(3, 5)"
449
+ end
450
+
451
+ it "#char_at should work" do
452
+ :name.char_at(3).should == "name.charAt(3)"
453
+ end
454
+
455
+ it "#index_of should work" do
456
+ :name.index_of("test").should == "name.indexOf('test')"
457
+ :name.index_of("test", 3).should == "name.indexOf('test', 3)"
458
+ end
459
+
460
+ it "#odb_format should work" do
461
+ :name.odb_format('%-20.20s').should == "name.format('%-20.20s')"
462
+ end
463
+
464
+ it "#odb_size should work" do
465
+ :name.odb_size.should == "name.size()"
466
+ end
467
+
468
+ it "#as_string should work" do
469
+ :name.as_string.should == "name.asString()"
470
+ end
471
+
472
+ it "#as_integer should work" do
473
+ :name.as_integer.should == "name.asInteger()"
474
+ end
475
+
476
+ it "#as_float should work" do
477
+ :name.as_float.should == "name.asFloat()"
478
+ end
479
+
480
+ it "#as_date should work" do
481
+ :name.as_date.should == "name.asDate()"
482
+ end
483
+
484
+ it "#as_date_time should work" do
485
+ :name.as_date_time.should == "name.asDateTime()"
486
+ end
487
+
488
+ it "#as_boolean should work" do
489
+ :name.as_boolean.should == "name.asBoolean()"
490
+ end
491
+ end
492
+
493
+ describe "Bundled Functions" do
494
+ it "#odb_count should work" do
495
+ :name.odb_count.should == "count(name)"
496
+ end
497
+
498
+ it "#odb_min should work" do
499
+ :name.odb_min.should == "min(name)"
500
+ end
501
+
502
+ it "#odb_max should work" do
503
+ :name.odb_max.should == "max(name)"
504
+ end
505
+
506
+ it "#odb_avg should work" do
507
+ :name.odb_avg.should == "avg(name)"
508
+ end
509
+
510
+ it "#odb_sum should work" do
511
+ :name.odb_sum.should == "sum(name)"
512
+ end
513
+
514
+ it "#sysdate should work" do
515
+ :'yyyy.MM.dd'.sysdate.should == "sysdate('yyyy.MM.dd')"
516
+ end
517
+
518
+ it "#odb_format_str should work" do
519
+ :'%d - Mr. %s %s (%s)'.odb_format_str(:id, :name, :surname, :address).should == "format('%d - Mr. %s %s (%s)', id, name, surname, address)"
520
+ end
521
+ end
522
+ end
523
+
524
+ describe String do
525
+ OrientDB::SQL.monkey_patch! String
526
+
527
+ describe "Order" do
528
+ it "#asc should work" do
529
+ 'name'.asc.should == 'name ASC'
530
+ end
531
+
532
+ it "#desc should work" do
533
+ 'name'.desc.should == 'name DESC'
534
+ end
535
+ end
536
+
537
+ describe "Conditional" do
538
+ it "should work" do
539
+ 'name'.lit.should == OrientDB::SQL::LiteralExpression.new("name")
540
+ end
541
+
542
+ it "#like should work" do
543
+ 'name'.like("test%").should == "name LIKE 'test%'"
544
+ end
545
+
546
+ it "#eq should work" do
547
+ 'age'.eq(35).should == "age = 35"
548
+ end
549
+
550
+ it "#ne should work" do
551
+ 'age'.ne(35).should == "age <> 35"
552
+ end
553
+
554
+ it "#lt should work" do
555
+ 'age'.lt(35).should == "age < 35"
556
+ end
557
+
558
+ it "#lte should work" do
559
+ 'age'.lte(35).should == "age <= 35"
560
+ end
561
+
562
+ it "#gt should work" do
563
+ 'age'.gt(35).should == "age > 35"
564
+ end
565
+
566
+ it "#gte should work" do
567
+ 'age'.gte(35).should == "age >= 35"
568
+ end
569
+
570
+ it "#is_null should work" do
571
+ 'name'.is_null.should == "name IS null"
572
+ end
573
+
574
+ it "#is_not_null should work" do
575
+ 'name'.is_not_null.should == "name IS NOT null"
576
+ end
577
+
578
+ it "#in should work" do
579
+ 'age'.in(34, 36, 38).should == "age IN [34, 36, 38]"
580
+ end
581
+
582
+ it "#contains should work" do
583
+ 'name'.contains('name', "tester").should == "name contains (name = 'tester')"
584
+ end
585
+
586
+ it "#contains_all should work" do
587
+ 'name'.contains_all('name', "tester").should == "name containsAll (name = 'tester')"
588
+ end
589
+
590
+ it "#contains_key should work" do
591
+ 'name'.contains_key("tester").should == "name containsKey 'tester'"
592
+ end
593
+
594
+ it "#contains_value should work" do
595
+ 'name'.contains_value("tester").should == "name containsValue 'tester'"
596
+ end
597
+
598
+ it "#contains_text should work" do
599
+ 'name'.contains_text("tester").should == "name containsText 'tester'"
600
+ end
601
+
602
+ it "#matches should work" do
603
+ 'name'.matches(/(john|mark)/).should == "name matches '(john|mark)'"
604
+ end
605
+ end
606
+
607
+ describe "Field Operators" do
608
+ it "#odb_length should work" do
609
+ 'name'.odb_length.should == "name.length()"
610
+ end
611
+
612
+ it "#odb_trim should work" do
613
+ 'name'.odb_trim.should == "name.trim()"
614
+ end
615
+
616
+ it "#to_upper_case should work" do
617
+ 'name'.to_upper_case.should == "name.toUpperCase()"
618
+ end
619
+
620
+ it "#to_lower_case should work" do
621
+ 'name'.to_lower_case.should == "name.toLowerCase()"
622
+ end
623
+
624
+ it "#odb_left should work" do
625
+ 'name'.odb_left(5).should == "name.left(5)"
626
+ end
627
+
628
+ it "#odb_right should work" do
629
+ 'name'.odb_right(5).should == "name.right(5)"
630
+ end
631
+
632
+ it "#sub_string should work" do
633
+ 'name'.sub_string(3).should == "name.subString(3)"
634
+ 'name'.sub_string(3, 5).should == "name.subString(3, 5)"
635
+ end
636
+
637
+ it "#char_at should work" do
638
+ 'name'.char_at(3).should == "name.charAt(3)"
639
+ end
640
+
641
+ it "#index_of should work" do
642
+ 'name'.index_of("test").should == "name.indexOf('test')"
643
+ 'name'.index_of("test", 3).should == "name.indexOf('test', 3)"
644
+ end
645
+
646
+ it "#odb_format should work" do
647
+ 'name'.odb_format('%-20.20s').should == "name.format('%-20.20s')"
648
+ end
649
+
650
+ it "#odb_size should work" do
651
+ 'name'.odb_size.should == "name.size()"
652
+ end
653
+
654
+ it "#as_string should work" do
655
+ 'name'.as_string.should == "name.asString()"
656
+ end
657
+
658
+ it "#as_integer should work" do
659
+ 'name'.as_integer.should == "name.asInteger()"
660
+ end
661
+
662
+ it "#as_float should work" do
663
+ 'name'.as_float.should == "name.asFloat()"
664
+ end
665
+
666
+ it "#as_date should work" do
667
+ 'name'.as_date.should == "name.asDate()"
668
+ end
669
+
670
+ it "#as_date_time should work" do
671
+ 'name'.as_date_time.should == "name.asDateTime()"
672
+ end
673
+
674
+ it "#as_boolean should work" do
675
+ 'name'.as_boolean.should == "name.asBoolean()"
676
+ end
677
+ end
678
+
679
+ describe "Bundled Functions" do
680
+ it "#odb_count should work" do
681
+ 'name'.odb_count.should == "count(name)"
682
+ end
683
+
684
+ it "#odb_min should work" do
685
+ 'name'.odb_min.should == "min(name)"
686
+ end
687
+
688
+ it "#odb_max should work" do
689
+ 'name'.odb_max.should == "max(name)"
690
+ end
691
+
692
+ it "#odb_avg should work" do
693
+ 'name'.odb_avg.should == "avg(name)"
694
+ end
695
+
696
+ it "#odb_sum should work" do
697
+ 'name'.odb_sum.should == "sum(name)"
698
+ end
699
+
700
+ it "#sysdate should work" do
701
+ 'yyyy.MM.dd'.sysdate.should == "sysdate('yyyy.MM.dd')"
702
+ end
703
+
704
+ it "#odb_format_str should work" do
705
+ '%d - Mr. %s %s (%s)'.odb_format_str(:id, :name, :surname, :address).should == "format('%d - Mr. %s %s (%s)', id, name, surname, address)"
706
+ end
707
+ end
708
+ end
709
+ end
710
+ end
711
+ end
712
+
713
+ describe "INSERT" do
714
+
715
+ before :each do
716
+ @q = OrientDB::SQL::Insert.new
717
+ end
718
+
719
+ it "should handle basic oclass inserts" do
720
+ @q.oclass('Person').values(:name => "Martin").to_s.should == "INSERT INTO Person (name) VALUES ('Martin')"
721
+ end
722
+
723
+ it "should handle basic oclass inserts" do
724
+ @q.cluster('person').values(:name => "Martin").to_s.should == "INSERT INTO cluster:person (name) VALUES ('Martin')"
725
+ end
726
+
727
+ it "should handle complex oclass inserts" do
728
+ @q.oclass('Person').fields(:name => "Martin").values(:age => 39).to_s.should == "INSERT INTO Person (name, age) VALUES ('Martin', 39)"
729
+ end
730
+
731
+ it "should handle array oclass inserts" do
732
+ @q.oclass('Person').fields(:name, :age).values("Martin", 39).to_s.should == "INSERT INTO Person (name, age) VALUES ('Martin', 39)"
733
+ end
734
+
735
+ end
736
+
737
+ describe "UPDATE" do
738
+
739
+ before :each do
740
+ @q = OrientDB::SQL::Update.new
741
+ end
742
+
743
+ it "should handle basic oclass SET update" do
744
+ @q.oclass('Person').values(:name => "Martin").to_s.should == "UPDATE Person SET name = 'Martin'"
745
+ end
746
+
747
+ it "should handle basic cluster SET update" do
748
+ @q.cluster('person').values(:name => "Martin").to_s.should == "UPDATE cluster:person SET name = 'Martin'"
749
+ end
750
+
751
+ it "should handle complex oclass SET updates" do
752
+ @q.oclass('Person').fields(:name => "Martin").values(:age => 39).to_s.should == "UPDATE Person SET name = 'Martin', age = 39"
753
+ end
754
+
755
+ it "should handle array oclass REMOVE a field updates" do
756
+ @q.oclass('Person').action(:remove).fields(:name).to_s.should == "UPDATE Person REMOVE name"
757
+ end
758
+
759
+ it "should handle array oclass REMOVE many fields updates" do
760
+ @q.oclass('Person').action(:remove).fields(:name, :age).to_s.should == "UPDATE Person REMOVE name, age"
761
+ end
762
+
763
+ it "should handle basic oclass REMOVE a value from a collection update" do
764
+ @q.oclass('Person').action(:remove).values(:name => "Martin").to_s.should == "UPDATE Person REMOVE name = 'Martin'"
765
+ end
766
+
767
+ it "should handle basic oclass ADD a value into a collection update" do
768
+ @q.oclass('Person').action(:add).values(:name => "Martin").to_s.should == "UPDATE Person ADD name = 'Martin'"
769
+ end
770
+
771
+ it "should handle basic oclass ADD many values into a collection update" do
772
+ @q.oclass('Person').action(:add).values(:name => "Martin").values(:age => 39).to_s.should == "UPDATE Person ADD name = 'Martin', age = 39"
773
+ end
774
+
775
+ it "should handle simple hashes for conditions" do
776
+ @q.oclass('Person').values(:age => 39).where(:a => 1).to_s.should == 'UPDATE Person SET age = 39 WHERE a = 1'
777
+ end
778
+
779
+ it "should handle simple arrays for conditions" do
780
+ @q.oclass('Person').values(:age => 39).where(['a > 1', 'b < 3', 'c = 5']).to_s.should == 'UPDATE Person SET age = 39 WHERE a > 1 AND b < 3 AND c = 5'
781
+ end
782
+
783
+ it "should handle concatenation for conditions" do
784
+ @q.oclass('Person').values(:age => 39).where(:a => 1).where(:b => 2).to_s.should == 'UPDATE Person SET age = 39 WHERE a = 1 AND b = 2'
785
+ end
786
+
787
+ it "should handle overriding for conditions" do
788
+ @q.oclass('Person').values(:age => 39).where(:a => 1).where!(:b => 2).to_s.should == 'UPDATE Person SET age = 39 WHERE b = 2'
789
+ end
790
+
791
+ it "should handle simple joined AND conditions for conditions" do
792
+ @q.oclass('Person').values(:age => 39).where(:a => 1).and(:b => 2).to_s.should == 'UPDATE Person SET age = 39 WHERE a = 1 AND b = 2'
793
+ end
794
+
795
+ it "should handle simple joined OR conditions for conditions" do
796
+ @q.oclass('Person').values(:age => 39).where(:a => 1).or(:b => 2).to_s.should == 'UPDATE Person SET age = 39 WHERE a = 1 OR b = 2'
797
+ end
798
+ end
799
+
800
+ describe "DELETE" do
801
+
802
+ before :each do
803
+ @q = OrientDB::SQL::Delete.new
804
+ end
805
+
806
+ it "should handle basic oclass" do
807
+ @q.oclass('Person').to_s.should == "DELETE FROM Person"
808
+ end
809
+
810
+ it "should handle basic cluster" do
811
+ @q.cluster('person').to_s.should == "DELETE FROM cluster:person"
812
+ end
813
+
814
+ it "should handle simple hashes for conditions" do
815
+ @q.oclass('Person').where(:a => 1).to_s.should == 'DELETE FROM Person WHERE a = 1'
816
+ end
817
+
818
+ it "should handle simple arrays for conditions" do
819
+ @q.oclass('Person').where(['a > 1', 'b < 3', 'c = 5']).to_s.should == 'DELETE FROM Person WHERE a > 1 AND b < 3 AND c = 5'
820
+ end
821
+
822
+ it "should handle concatenation for conditions" do
823
+ @q.oclass('Person').where(:a => 1).where(:b => 2).to_s.should == 'DELETE FROM Person WHERE a = 1 AND b = 2'
824
+ end
825
+
826
+ it "should handle overriding for conditions" do
827
+ @q.oclass('Person').where(:a => 1).where!(:b => 2).to_s.should == 'DELETE FROM Person WHERE b = 2'
828
+ end
829
+
830
+ it "should handle simple joined AND conditions for conditions" do
831
+ @q.oclass('Person').where(:a => 1).and(:b => 2).to_s.should == 'DELETE FROM Person WHERE a = 1 AND b = 2'
832
+ end
833
+
834
+ it "should handle simple joined OR conditions for conditions" do
835
+ @q.oclass('Person').where(:a => 1).or(:b => 2).to_s.should == 'DELETE FROM Person WHERE a = 1 OR b = 2'
836
+ end
837
+ end
838
+
839
+ end