active-orient 0.4 → 0.80

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.graphs.txt.swp +0 -0
  4. data/Gemfile +9 -5
  5. data/Guardfile +12 -4
  6. data/README.md +70 -281
  7. data/VERSION +1 -1
  8. data/active-orient.gemspec +9 -7
  9. data/bin/active-orient-0.6.gem +0 -0
  10. data/bin/active-orient-console +97 -0
  11. data/changelog.md +60 -0
  12. data/config/boot.rb +70 -17
  13. data/config/config.yml +10 -0
  14. data/config/connect.yml +11 -6
  15. data/examples/books.rb +154 -65
  16. data/examples/streets.rb +89 -85
  17. data/graphs.txt +70 -0
  18. data/lib/active-orient.rb +78 -6
  19. data/lib/base.rb +266 -168
  20. data/lib/base_properties.rb +76 -65
  21. data/lib/class_utils.rb +187 -0
  22. data/lib/database_utils.rb +99 -0
  23. data/lib/init.rb +80 -0
  24. data/lib/java-api.rb +442 -0
  25. data/lib/jdbc.rb +211 -0
  26. data/lib/model/custom.rb +29 -0
  27. data/lib/model/e.rb +6 -0
  28. data/lib/model/edge.rb +114 -0
  29. data/lib/model/model.rb +134 -0
  30. data/lib/model/the_class.rb +657 -0
  31. data/lib/model/the_record.rb +313 -0
  32. data/lib/model/vertex.rb +371 -0
  33. data/lib/orientdb_private.rb +48 -0
  34. data/lib/other.rb +423 -0
  35. data/lib/railtie.rb +68 -0
  36. data/lib/rest/change.rb +150 -0
  37. data/lib/rest/create.rb +287 -0
  38. data/lib/rest/delete.rb +150 -0
  39. data/lib/rest/operations.rb +222 -0
  40. data/lib/rest/read.rb +189 -0
  41. data/lib/rest/rest.rb +120 -0
  42. data/lib/rest_disabled.rb +24 -0
  43. data/lib/support/conversions.rb +42 -0
  44. data/lib/support/default_formatter.rb +7 -0
  45. data/lib/support/errors.rb +41 -0
  46. data/lib/support/logging.rb +38 -0
  47. data/lib/support/orient.rb +305 -0
  48. data/lib/support/orientquery.rb +647 -0
  49. data/lib/support/query.rb +92 -0
  50. data/rails.md +154 -0
  51. data/rails/activeorient.rb +32 -0
  52. data/rails/config.yml +10 -0
  53. data/rails/connect.yml +17 -0
  54. metadata +89 -30
  55. data/lib/model.rb +0 -461
  56. data/lib/orient.rb +0 -98
  57. data/lib/query.rb +0 -88
  58. data/lib/rest.rb +0 -1036
  59. data/lib/support.rb +0 -347
  60. data/test.rb +0 -4
  61. data/usecase.md +0 -91
@@ -0,0 +1,48 @@
1
+ module OrientDbPrivate
2
+ private
3
+
4
+ def translate_property_hash field, type: nil, linked_class: nil, **args
5
+ type = type.presence || args[:propertyType].presence || args[:property_type]
6
+ linked_class = linked_class.presence || args[:linkedClass] || args[:other_class]
7
+ if type.present?
8
+ if linked_class.nil?
9
+ {field => {propertyType: type.to_s.upcase}}
10
+ else
11
+ {field => {propertyType: type.to_s.upcase, linkedClass: classname(linked_class)}}
12
+ end
13
+ end
14
+ end
15
+
16
+ def property_uri this_classname
17
+ if block_given?
18
+ "property/#{@database}/#{this_classname}/" << yield
19
+ else
20
+ "property/#{@database}/#{this_classname}"
21
+ end
22
+ end
23
+
24
+ def self.simple_uri *names
25
+ names.each do |name|
26
+ m_name = ("#{name.to_s}_uri").to_sym
27
+ define_method(m_name) do |&b|
28
+ if b
29
+ "#{name.to_s}/#{@database}/#{b.call}"
30
+ else
31
+ "#{name.to_s}/#{@database}"
32
+ end # branch
33
+ end
34
+ end
35
+ end
36
+
37
+ def self.sql_uri *names
38
+ names.each do |name|
39
+ define_method(("#{name.to_s}_sql_uri").to_sym) do
40
+ "#{name.to_s}/#{@database}/sql/"
41
+ end
42
+ end
43
+ end
44
+
45
+ simple_uri :database, :document, :class, :batch, :function
46
+ sql_uri :command, :query
47
+
48
+ end
@@ -0,0 +1,423 @@
1
+
2
+ class Array
3
+
4
+ @@accepted_methods = []
5
+ ## dummy for refining
6
+
7
+ def method_missing method, *args, &b
8
+ return if [:to_hash, :to_str].include? method
9
+ if @@accepted_methods.include? method
10
+ self.map{|x| x.public_send(method, *args, &b)}
11
+ else
12
+ raise ArgumentError.new("Method #{method} does not exist")
13
+ end
14
+ end
15
+
16
+
17
+ # Class extentions to manage to_orient and from_orient
18
+ def to_orient
19
+ if all?{ |x| x.respond_to?(:rid?)} && any?( &:rid? )
20
+ "["+ map{|x| x.rid? ? x.rid : x.to_or }.join(', ') + ']'
21
+ else
22
+ map(&:to_orient) # .join(',')
23
+ end
24
+ end
25
+
26
+ def to_or
27
+ "["+ map( &:to_or).join(', ')+"]"
28
+ end
29
+
30
+ def from_orient
31
+ map &:from_orient
32
+ end
33
+
34
+ def to_human
35
+ map &:to_human
36
+ end
37
+
38
+ # used to enable
39
+ # def abc *key
40
+ # where key is a Range, an comma separated List or an item
41
+ # aimed to support #compose_where
42
+ def analyse # :nodoc:
43
+ if first.is_a?(Range)
44
+ first
45
+ elsif size ==1
46
+ first
47
+ else
48
+ self
49
+ end
50
+ end
51
+
52
+ def orient_flatten
53
+ while( first.is_a?(Array) )
54
+ self.flatten!(1)
55
+ end
56
+ self.compact!
57
+ self ## return object
58
+ end
59
+ end
60
+
61
+ class Symbol
62
+ def to_a
63
+ [ self ]
64
+ end
65
+ # symbols are masked with ":{symbol}:"
66
+ def to_orient
67
+ ":"+self.to_s+":"
68
+ end
69
+ def to_or
70
+ "'"+self.to_orient+"'"
71
+ end
72
+
73
+ # inserted to prevent error message while initializing a model-recorda
74
+ =begin
75
+ 2.6.1 :008 > ii.first.attributes
76
+ => {:zwoebelkuchen=>[7, 9, 9, 7, 8, 8, 3, 6, 9, ":zt:", ":zt:", ":zg:", ":zg:", ":tzu:", ":grotte:"], :created_at=>Wed, 27 Mar 2019 14:59:45 +0000}
77
+ 2.6.1 :009 > ii.first.send :zwoebelkuchen
78
+ key zwoebelkuchen
79
+ iv [7, 9, 9, 7, 8, 8, 3, 6, 9, ":zt:", ":zt:", ":zg:", ":zg:", ":tzu:", ":grotte:"]
80
+ 27.03.(15:00:36)ERROR->MethodMissing -> Undefined method: coerce -- Args: [Wed, 27 Mar 2019 14:59:45 +0000]
81
+ 27.03.(15:00:36)ERROR-> The Message undefined method `coerce' for :zt:Symbol
82
+ 27.03.(15:00:36)ERROR-> /home/dev/topo/activeorient/lib/support/orient.rb:129:in `block in method_missing'
83
+ /home/dev/topo/activeorient/lib/support/orient.rb:129:in `map'
84
+ /home/dev/topo/activeorient/lib/support/orient.rb:129:in `method_missing'
85
+ /home/ubuntu/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.2.1/lib/active_support/core_ext/date/calculations.rb:140:in `<=>'
86
+ /home/ubuntu/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.2.1/lib/active_support/core_ext/date/calculations.rb:140:in `compare_with_coercion'
87
+ /home/ubuntu/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.2.1/lib/active_support/core_ext/date_time/calculations.rb:208:in `<=>'
88
+ /home/dev/topo/activeorient/lib/support/orient.rb:36:in `=='
89
+ /home/dev/topo/activeorient/lib/support/orient.rb:36:in `key'
90
+ /home/dev/topo/activeorient/lib/support/orient.rb:36:in `initialize'
91
+ /home/dev/topo/activeorient/lib/base.rb:216:in `new'
92
+ /home/dev/topo/activeorient/lib/base.rb:216:in `[]'
93
+ /home/dev/topo/activeorient/lib/base_properties.rb:110:in `block in define_property_methods'
94
+ (irb):9:in `irb_binding'
95
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb/workspace.rb:85:in `eval'
96
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb/workspace.rb:85:in `evaluate'
97
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb/context.rb:385:in `evaluate'
98
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb.rb:493:in `block (2 levels) in eval_input'
99
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb.rb:647:in `signal_status'
100
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb.rb:490:in `block in eval_input'
101
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb/ruby-lex.rb:246:in `block (2 levels) in each_top_level_statement'
102
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb/ruby-lex.rb:232:in `loop'
103
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb/ruby-lex.rb:232:in `block in each_top_level_statement'
104
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb/ruby-lex.rb:231:in `catch'
105
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb/ruby-lex.rb:231:in `each_top_level_statement'
106
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb.rb:489:in `eval_input'
107
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb.rb:428:in `block in run'
108
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb.rb:427:in `catch'
109
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb.rb:427:in `run'
110
+ /home/ubuntu/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/irb.rb:383:in `start'
111
+ ./active-orient-console:51:in `<main>'
112
+ =end
113
+
114
+ #def coerce a #nodoc#
115
+ # nil
116
+ #end
117
+ end
118
+
119
+ class Object
120
+ def from_orient
121
+ self
122
+ end
123
+
124
+ def to_orient
125
+ self
126
+ end
127
+ end
128
+
129
+
130
+
131
+ class Time
132
+ def to_or
133
+ "date(\'#{self.strftime("%Y%m%d%H%M%S")}\',\'yyyyMMddHHmmss\')"
134
+ end
135
+ end
136
+
137
+ class Object
138
+ def to_or
139
+ self
140
+ end
141
+ end
142
+ class NilClass
143
+ def to_or
144
+ "NULL"
145
+ end
146
+ end
147
+ class Date
148
+ def to_orient
149
+ if RUBY_PLATFORM == 'java'
150
+ java.util.Date.new( year-1900, month-1, day , 0, 0 , 0 ) ## Jahr 0 => 1900
151
+ else
152
+ self
153
+ end
154
+ end
155
+ def to_or
156
+ "date(\'#{self.to_s}\',\'yyyy-MM-dd\')"
157
+ end
158
+ end
159
+
160
+ class DateTime
161
+ def to_or
162
+ "date(\'#{self.strftime("%Y%m%d%H%M%S")}\',\'yyyyMMddHHmmss\')"
163
+ end
164
+ end
165
+
166
+ class Numeric
167
+
168
+ def to_or
169
+ # "#{self.to_s}"
170
+ self
171
+ end
172
+
173
+ def to_a
174
+ [ self ]
175
+ end
176
+ end
177
+
178
+ class String
179
+ def capitalize_first_letter
180
+ self.sub(/^(.)/) { $1.capitalize }
181
+ end
182
+ ### as_json has unexpected side-effects, needs further consideration
183
+ # def as_json o=nil
184
+ # if rid?
185
+ # rid
186
+ # else
187
+ # super o
188
+ # end
189
+ # end
190
+
191
+ def where **args
192
+ if rid?
193
+ from_orient.where **args
194
+ end
195
+ end
196
+
197
+ # from orient translates the database response into active-orient objects
198
+ #
199
+ # symbols are representated via ":{something]:}"
200
+ #
201
+ # database records respond to the "rid"-method
202
+ #
203
+ # other values are not modified
204
+ def from_orient
205
+ if rid?
206
+ ActiveOrient::Model.autoload_object self
207
+ elsif self =~ /^:.*:$/
208
+ # symbol-representation in the database
209
+ self[1..-2].to_sym
210
+ else
211
+ self
212
+ end
213
+ end
214
+
215
+ alias expand from_orient
216
+ # if the string contains "#xx:yy" omit quotes
217
+ def to_orient
218
+ rid? ? "#"+rid : self # return the string (not the quoted string. this is to_or)
219
+ end
220
+
221
+ # a rid is either #nn:nn or nn:nn
222
+ def rid?
223
+ self =~ /\A[#]{,1}[0-9]{1,}:[0-9]{1,}\z/
224
+ end
225
+
226
+ # return a valid rid (format: "nn:mm") or nil
227
+ def rid
228
+ self["#"].nil? ? self : self[1..-1] if rid?
229
+ end
230
+ alias rrid rid
231
+
232
+ def to_classname
233
+ if self[0] == '$'
234
+ self[1..-1]
235
+ else
236
+ self
237
+ end
238
+ end
239
+
240
+ def to_or
241
+ quote
242
+ end
243
+
244
+ def to_a
245
+ [ self ]
246
+ end
247
+
248
+ def quote
249
+ str = self.dup
250
+ if str[0, 1] == "'" && str[-1, 1] == "'"
251
+ self
252
+ else
253
+ last_pos = 0
254
+ while (pos = str.index("'", last_pos))
255
+ str.insert(pos, "\\") if pos > 0 && str[pos - 1, 1] != "\\"
256
+ last_pos = pos + 1
257
+ end
258
+ "'#{str}'"
259
+ end
260
+ end
261
+
262
+ # def coerce a #nodoc#
263
+ # nil
264
+ # end
265
+
266
+ def to_human
267
+ self
268
+ end
269
+ end
270
+
271
+ class Hash
272
+
273
+ # converts :abc => {anything} to "abc" => {anything}
274
+ #
275
+ # converts nn => {anything} to nn => {anything}
276
+ #
277
+ # leaves "abc" => {anything} untouched
278
+ def to_orient # converts hast from activeorient to db
279
+ map do | k, v|
280
+ orient_k = case k
281
+ when Numeric
282
+ k
283
+ when Symbol, String
284
+ k.to_s
285
+ else
286
+ raise "not supported key: #[k} -- must a sting, symbol or number"
287
+ end
288
+ [orient_k, v.to_orient]
289
+ end.to_h
290
+ end
291
+
292
+ # converts hash from db to activeorient
293
+ #
294
+ #
295
+ def from_orient
296
+ #puts "here hash.from_orient --> #{self.inspect}"
297
+ if keys.include?("@class" )
298
+ ActiveOrient::Model.orientdb_class( name: self["@class"] ).new self
299
+ # create a dummy class and fill with attributes from result-set
300
+ elsif keys.include?("@type") && self["@type"] == 'd'
301
+ ActiveOrient::Model.orientdb_class(name: 'query' ).new self
302
+ else
303
+ # populate the hash by converting keys: stings to symbols, values: proprocess through :from_orient
304
+ map do |k,v|
305
+ orient_k = if k.to_s.to_i.to_s == k.to_s
306
+ k.to_i
307
+ else
308
+ k.to_sym
309
+ end
310
+
311
+ [orient_k, v.from_orient]
312
+ end.to_h
313
+ end
314
+ end
315
+
316
+ # converts a hash to a string appropiate to include in raw queries
317
+ def to_or
318
+ "{ " + to_orient.map{|k,v| "#{k.to_or}: #{v.to_or}"}.join(',') + "}"
319
+ end
320
+ end
321
+
322
+ #class RecordList
323
+ # def from_orient
324
+ # map &:from_orient
325
+ # end
326
+ #end
327
+ if RUBY_PLATFORM == 'java'
328
+
329
+ ## JavaMath:.BigDecimal does not premit mathematical operations
330
+ ## We convert it to RubyBigDecimal to represent it (if present in the DB) and upon loading from the DB
331
+
332
+ class Java::JavaMath::BigDecimal
333
+ def to_f
334
+ BigDecimal.new self.to_s
335
+ end
336
+
337
+ def from_orient
338
+ BigDecimal.new self.to_s
339
+ end
340
+
341
+ end
342
+ class Java::ComOrientechnologiesOrientCoreDbRecordRidbag::ORidBag
343
+ def from_orient
344
+ to_a.from_orient
345
+ end
346
+ end
347
+
348
+ class Java::ComOrientechnologiesOrientCoreDbRecord::OTrackedList
349
+ # class RecordList
350
+ # Basisklasse
351
+ # Java::ComOrientechnologiesOrientCoreDbRecord::ORecordLazyList
352
+ # Methode get(Index): gibt das Dokument (Java::ComOrientechnologiesOrientCoreRecordImpl::ODocument) zurück
353
+ ## base = ActiveOrient::Model::Base.first
354
+ ## base.document.first_list
355
+ # => #<OrientDB::RecordList:[#21:0, #22:0, #23:0, #24:0, #21:1, #22:1, #23:1, #24:1, #21:2, #22:2]>
356
+ ## base.first_list.get(3)
357
+ # => <OrientDB::Document:first_list:#24:0 second_list:#<OrientDB::RecordList:[#27:17, #28:17, #25:18, #26:18, #27:18, #28:18, #25:19, #26:19, #27:19, #28:19]> label:3>
358
+ ## base.first_list[3]
359
+ # => #<ActiveOrient::Model::FirstList:0x18df26a1 (...)
360
+ ## base.first_list[3].second_list[5]
361
+ # => #<ActiveOrient::Model::SecondList: (...)
362
+ ## base.first_list.get(3).second_list.get(5)
363
+ # => <OrientDB::Document:second_list:#28:18 label:5>
364
+ #
365
+ def from_orient
366
+ map &:from_orient
367
+ self
368
+ end
369
+ def to_orient
370
+ self
371
+ end
372
+
373
+ #def add value
374
+ # puts "ASDSD"
375
+ #end
376
+ #
377
+ def to_a
378
+ super.map &:from_orient
379
+ end
380
+ def first
381
+ super.from_orient
382
+ end
383
+ def last
384
+ super.from_orient
385
+ end
386
+ def [] val
387
+ super.from_orient
388
+
389
+ end
390
+
391
+ def << value
392
+ #put "I will perform the insert"
393
+ value = value.document if value.is_a?( ActiveOrient::Model ) && value.document.present?
394
+ add value
395
+ #save
396
+
397
+ end
398
+ end
399
+
400
+ class Java::ComOrientechnologiesOrientCoreDbRecord::OTrackedMap
401
+ def from_orient
402
+
403
+ # puts self.inspect
404
+ # puts self.keys.inspect
405
+ HashWithIndifferentAccess.new(self)
406
+ # Kernel.exit
407
+ # map &:from_orient
408
+ # to_a.from_orient
409
+ end
410
+
411
+ end
412
+ class Java::JavaUtil::Date
413
+ def from_orient
414
+ Date.new(year+1900, month+1, date )
415
+ end
416
+ def to_orient
417
+ self
418
+ end
419
+ end
420
+
421
+
422
+ end
423
+