active-orient 0.42 → 0.79
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/Gemfile +13 -5
- data/Guardfile +12 -4
- data/README.md +67 -280
- data/VERSION +1 -1
- data/active-orient.gemspec +6 -5
- data/bin/active-orient-0.6.gem +0 -0
- data/bin/active-orient-console +85 -0
- data/config/boot.rb +72 -1
- data/config/config.yml +10 -0
- data/config/connect.yml +9 -4
- data/examples/books.rb +92 -40
- data/examples/streets.rb +89 -85
- data/examples/test_commands.rb +97 -0
- data/examples/test_commands_2.rb +59 -0
- data/examples/test_commands_3.rb +55 -0
- data/examples/test_commands_4.rb +33 -0
- data/examples/time_graph.md +162 -0
- data/lib/active-orient.rb +75 -9
- data/lib/base.rb +238 -169
- data/lib/base_properties.rb +68 -60
- data/lib/class_utils.rb +226 -0
- data/lib/database_utils.rb +98 -0
- data/lib/init.rb +79 -0
- data/lib/java-api.rb +442 -0
- data/lib/jdbc.rb +211 -0
- data/lib/model/custom.rb +26 -0
- data/lib/model/edge.rb +70 -0
- data/lib/model/model.rb +134 -0
- data/lib/model/the_class.rb +607 -0
- data/lib/model/the_record.rb +266 -0
- data/lib/model/vertex.rb +236 -0
- data/lib/orientdb_private.rb +48 -0
- data/lib/other.rb +371 -0
- data/lib/railtie.rb +68 -0
- data/lib/rest/change.rb +147 -0
- data/lib/rest/create.rb +279 -0
- data/lib/rest/delete.rb +134 -0
- data/lib/rest/operations.rb +211 -0
- data/lib/rest/read.rb +171 -0
- data/lib/rest/rest.rb +112 -0
- data/lib/rest_disabled.rb +24 -0
- data/lib/support/logging.rb +38 -0
- data/lib/support/orient.rb +196 -0
- data/lib/support/orientquery.rb +469 -0
- data/rails.md +154 -0
- data/rails/activeorient.rb +32 -0
- data/rails/config.yml +10 -0
- data/rails/connect.yml +17 -0
- metadata +65 -24
- data/active-orient-0.4.gem +0 -0
- data/active-orient-0.41.gem +0 -0
- data/lib/model.rb +0 -468
- data/lib/orient.rb +0 -98
- data/lib/query.rb +0 -88
- data/lib/rest.rb +0 -1059
- data/lib/support.rb +0 -372
- data/test.rb +0 -4
- data/usecase.md +0 -91
data/rails.md
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
# Active Orient and Rails
|
2
|
+
|
3
|
+
The usage of Orientdb via ActiveOrient in Rails requires just a few steps.
|
4
|
+
Based on a Rails 5 installation
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
rvm install 2.4
|
8
|
+
rvm use 2.4
|
9
|
+
gem install bundler
|
10
|
+
gem install nokogiri
|
11
|
+
gem install rails
|
12
|
+
rails -v # Rails 5.0.1
|
13
|
+
|
14
|
+
```
|
15
|
+
|
16
|
+
create your working directory and initialize the system
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
mkdir rails_project; cd rails_project
|
20
|
+
rails new -OCT .
|
21
|
+
```
|
22
|
+
This initializes a Rails-Stack, without active-record, action-cable and the test-suite.
|
23
|
+
(We will use rspec later)
|
24
|
+
|
25
|
+
This can be checked by inspecting «/config/application.rb «
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
rails s puma
|
29
|
+
```
|
30
|
+
should work at this stage.
|
31
|
+
|
32
|
+
## Modify the Gemfile
|
33
|
+
Inform rails to use orientdb and active-orient
|
34
|
+
|
35
|
+
```
|
36
|
+
echo " gem 'active-orient' , :path => '/home/your_cloned_active_orient_path/activeorient' " >> Gemfile
|
37
|
+
# or
|
38
|
+
|
39
|
+
echo " gem 'active-orient' , :git => 'https://github.com/topofocus/active-orient.git' " >> Gemfile
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
Run the bundler
|
44
|
+
|
45
|
+
```
|
46
|
+
bundle install & bundle update
|
47
|
+
```
|
48
|
+
## Copy Initializer and Configuration Files
|
49
|
+
change to the base-dir of the gem
|
50
|
+
--> bundle show active-orient
|
51
|
+
then copy
|
52
|
+
|
53
|
+
* rails/activeorient.rb to config/initializers in the rails-project dir
|
54
|
+
* rails/connect.yml to config in the rails project
|
55
|
+
* rails/config.yml to config in the rails project
|
56
|
+
|
57
|
+
and modify the yml-files accordingly.
|
58
|
+
(Depending on your settings, you might have to adjust tab-levels)
|
59
|
+
|
60
|
+
|
61
|
+
The database is present in the rails console, and
|
62
|
+
```ruby
|
63
|
+
rails c
|
64
|
+
puts ActiveOrient:show_classes
|
65
|
+
|
66
|
+
V.count
|
67
|
+
V.first
|
68
|
+
E.count
|
69
|
+
|
70
|
+
```
|
71
|
+
should display details.
|
72
|
+
|
73
|
+
|
74
|
+
**Notice** The spring-facility is running in the background. Stop the server prior reloading
|
75
|
+
the console ( ./bin/spring stop ).
|
76
|
+
|
77
|
+
## Model-files
|
78
|
+
The final step is to generate Model-Files.
|
79
|
+
|
80
|
+
In «/config/config.yml» the «:model_dir»-var points to
|
81
|
+
the location of the model-files. The default is 'lib/orient'. Change to your needs.
|
82
|
+
Don't use the app directory, as its autoloaded too early.
|
83
|
+
|
84
|
+
Upon startup, present model-classes are destroyed and overridden by present files in the autoload directory.
|
85
|
+
|
86
|
+
After envoking the rails console, the logfile displays sucessfully loaded and missing files, ie.
|
87
|
+
|
88
|
+
```
|
89
|
+
14.01.(08:28:45) INFO->ModelClass#RequireModelFile:..:model-file not present: /home/topo/workspace/orient-rails/lib/orient/followed_by.rb
|
90
|
+
14.01.(08:28:45) INFO->ModelClass#RequireModelFile:..:/home/topo/workspace/orient-rails/lib/orient/v.rb sucessfully loaded
|
91
|
+
```
|
92
|
+
|
93
|
+
## Model-file Example
|
94
|
+
|
95
|
+
To query the GratefulDeadConcerts Database, «v.rb» hosts the essential model methods.
|
96
|
+
As always, use «def self.{method}« for class methods and simply «def {method}» for methods working on the record level.
|
97
|
+
|
98
|
+
```
|
99
|
+
1 class V
|
100
|
+
2 def self.artists **attributes
|
101
|
+
3 names 'artist', **attributes
|
102
|
+
4 end
|
103
|
+
5
|
104
|
+
6 def self.songs **attributes
|
105
|
+
7 names 'song', **attributes
|
106
|
+
8 end
|
107
|
+
9
|
108
|
+
10 def self.types
|
109
|
+
11 oo = OrientSupport::OrientQuery
|
110
|
+
12 this_query = oo.new distinct: [:type, :a ] # --> "select distinct( type ) as a "
|
111
|
+
13 query_database( this_query ).a # returns an array of types, i.e. ["artist", "song"]
|
112
|
+
14 end
|
113
|
+
15 private
|
114
|
+
16 def self.names type, sort: :asc, limit: 20, skip: 0
|
115
|
+
17 puts "in names"
|
116
|
+
18 oo = OrientSupport::OrientQuery
|
117
|
+
19 query_database oo.new( where: {type: type },
|
118
|
+
20 order: { name: sort } ,
|
119
|
+
21 limit: limit ,
|
120
|
+
22 skip: skip )
|
121
|
+
23 end
|
122
|
+
24 end
|
123
|
+
|
124
|
+
```
|
125
|
+
|
126
|
+
Now
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
V.types
|
130
|
+
V.artists limit: 15, skip: 34, sort: :desc
|
131
|
+
```
|
132
|
+
queries the database, fetches 15 artists.
|
133
|
+
|
134
|
+
## Routing
|
135
|
+
|
136
|
+
for now, restful routing has some restrictions.
|
137
|
+
|
138
|
+
Rails-routing is depending on the "id"-attribute. Even if this is remapped to rid, the ressources-entries in "config/routes.rb" have to be modified by
|
139
|
+
|
140
|
+
```ruiby
|
141
|
+
resources :{controller}, id: /[^\/]+/
|
142
|
+
```
|
143
|
+
this enables the usage of id: "xx:yy"
|
144
|
+
|
145
|
+
In the controller the record is fetched as usual:
|
146
|
+
```ruby
|
147
|
+
def show
|
148
|
+
@{your coice} = {ActiveOrient-Model-Class}.autoload_object params[:id]
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
## This is an init-script intented to be copied to
|
3
|
+
## rails-root/config/initializers
|
4
|
+
|
5
|
+
## Integrate a namespaced model
|
6
|
+
#module HC
|
7
|
+
#
|
8
|
+
#end
|
9
|
+
#
|
10
|
+
#Hc = HC
|
11
|
+
#ActiveOrient::Model.keep_models_without_file = false
|
12
|
+
#ActiveOrient::Init.define_namespace { HC }
|
13
|
+
#ActiveOrient::OrientDB.new preallocate: true
|
14
|
+
#
|
15
|
+
# class ActiveOrient::Model
|
16
|
+
# def self.namespace_prefix
|
17
|
+
# ""
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# At the end: include everything which is not yet allocated to some namespaced model
|
22
|
+
ActiveOrient::Init.connect
|
23
|
+
ActiveOrient::Model.keep_models_without_file = true
|
24
|
+
|
25
|
+
ActiveOrient::OrientDB.new preallocate: true
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
data/rails/config.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
---
|
2
|
+
:active_orient:
|
3
|
+
## Namespace: Prefix of Model-Objects. :self -> ActiveOrient::Model::{name}
|
4
|
+
## :object => No Prefix
|
5
|
+
## :active_orient => ActiveOrient::{name}
|
6
|
+
:namespace: :object
|
7
|
+
## model_dir: Path to model-files relative to the root of the application
|
8
|
+
## ie. app/model or model
|
9
|
+
:model_dir: 'lib/orient'
|
10
|
+
|
data/rails/connect.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
---
|
2
|
+
:orientdb:
|
3
|
+
:server: localhost # 172.28.50.109
|
4
|
+
:port: 2480
|
5
|
+
:logger: stdout # 'file' or 'stdout'
|
6
|
+
:database:
|
7
|
+
:development: GratefulDeadConcerts
|
8
|
+
:production: hcn_data
|
9
|
+
:test: temp
|
10
|
+
:admin:
|
11
|
+
:user: root
|
12
|
+
:pass: root
|
13
|
+
:auth:
|
14
|
+
:user: root
|
15
|
+
:pass: root
|
16
|
+
|
17
|
+
# hfx: 101
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active-orient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.79'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hartmut Bischoff
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -42,31 +42,45 @@ dependencies:
|
|
42
42
|
name: activesupport
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activemodel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rest-client
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
|
-
- - "
|
73
|
+
- - ">="
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
75
|
+
version: '0'
|
62
76
|
type: :runtime
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
|
-
- - "
|
80
|
+
- - ">="
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
69
|
-
description: Persistent ORM for OrientDB, based on ActiveModel
|
82
|
+
version: '0'
|
83
|
+
description: Persistent ORM for OrientDB(V.3), based on ActiveModel. Rails 5 compatible
|
70
84
|
email:
|
71
85
|
- topofocus@gmail.com
|
72
86
|
executables: []
|
@@ -80,23 +94,51 @@ files:
|
|
80
94
|
- LICENSE
|
81
95
|
- README.md
|
82
96
|
- VERSION
|
83
|
-
- active-orient-0.4.gem
|
84
|
-
- active-orient-0.41.gem
|
85
97
|
- active-orient.gemspec
|
98
|
+
- bin/active-orient-0.6.gem
|
99
|
+
- bin/active-orient-console
|
86
100
|
- config/boot.rb
|
101
|
+
- config/config.yml
|
87
102
|
- config/connect.yml
|
88
103
|
- examples/books.rb
|
89
104
|
- examples/streets.rb
|
105
|
+
- examples/test_commands.rb
|
106
|
+
- examples/test_commands_2.rb
|
107
|
+
- examples/test_commands_2.rb~
|
108
|
+
- examples/test_commands_3.rb
|
109
|
+
- examples/test_commands_4.rb
|
110
|
+
- examples/time_graph.md
|
90
111
|
- lib/active-orient.rb
|
91
112
|
- lib/base.rb
|
92
113
|
- lib/base_properties.rb
|
93
|
-
- lib/
|
94
|
-
- lib/
|
95
|
-
- lib/
|
96
|
-
- lib/
|
97
|
-
- lib/
|
98
|
-
-
|
99
|
-
-
|
114
|
+
- lib/class_utils.rb
|
115
|
+
- lib/database_utils.rb
|
116
|
+
- lib/init.rb
|
117
|
+
- lib/java-api.rb
|
118
|
+
- lib/jdbc.rb
|
119
|
+
- lib/model/custom.rb
|
120
|
+
- lib/model/edge.rb
|
121
|
+
- lib/model/model.rb
|
122
|
+
- lib/model/the_class.rb
|
123
|
+
- lib/model/the_record.rb
|
124
|
+
- lib/model/vertex.rb
|
125
|
+
- lib/orientdb_private.rb
|
126
|
+
- lib/other.rb
|
127
|
+
- lib/railtie.rb
|
128
|
+
- lib/rest/change.rb
|
129
|
+
- lib/rest/create.rb
|
130
|
+
- lib/rest/delete.rb
|
131
|
+
- lib/rest/operations.rb
|
132
|
+
- lib/rest/read.rb
|
133
|
+
- lib/rest/rest.rb
|
134
|
+
- lib/rest_disabled.rb
|
135
|
+
- lib/support/logging.rb
|
136
|
+
- lib/support/orient.rb
|
137
|
+
- lib/support/orientquery.rb
|
138
|
+
- rails.md
|
139
|
+
- rails/activeorient.rb
|
140
|
+
- rails/config.yml
|
141
|
+
- rails/connect.yml
|
100
142
|
homepage: https://github.com/topofocus/active-orient
|
101
143
|
licenses:
|
102
144
|
- MIT
|
@@ -109,16 +151,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
151
|
requirements:
|
110
152
|
- - ">="
|
111
153
|
- !ruby/object:Gem::Version
|
112
|
-
version: 2.
|
154
|
+
version: '2.5'
|
113
155
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
156
|
requirements:
|
115
157
|
- - ">="
|
116
158
|
- !ruby/object:Gem::Version
|
117
159
|
version: '0'
|
118
160
|
requirements: []
|
119
|
-
|
120
|
-
rubygems_version: 2.4.6
|
161
|
+
rubygems_version: 3.0.1
|
121
162
|
signing_key:
|
122
163
|
specification_version: 4
|
123
|
-
summary: Pure ruby client for OrientDB based on ActiveModel
|
164
|
+
summary: Pure ruby client for OrientDB(V.3) based on ActiveModel
|
124
165
|
test_files: []
|
data/active-orient-0.4.gem
DELETED
Binary file
|
data/active-orient-0.41.gem
DELETED
Binary file
|
data/lib/model.rb
DELETED
@@ -1,468 +0,0 @@
|
|
1
|
-
class String
|
2
|
-
def to_or
|
3
|
-
"'#{self}'"
|
4
|
-
end
|
5
|
-
end
|
6
|
-
|
7
|
-
class Numeric
|
8
|
-
def to_or
|
9
|
-
"#{self.to_s}"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
module ActiveOrient
|
13
|
-
|
14
|
-
#require 'base'
|
15
|
-
#require 'base_properties'
|
16
|
-
|
17
|
-
class Model < ActiveOrient::Base
|
18
|
-
include BaseProperties
|
19
|
-
|
20
|
-
mattr_accessor :orientdb
|
21
|
-
mattr_accessor :logger
|
22
|
-
=begin
|
23
|
-
orientdb_class is used to instantiate a ActiveOrient:Model:{class} by providing its name
|
24
|
-
todo: implement object-inherence
|
25
|
-
=end
|
26
|
-
def self.orientdb_class name:
|
27
|
-
klass = Class.new( self )
|
28
|
-
name = name.to_s.camelize
|
29
|
-
if self.send :const_defined?, name
|
30
|
-
retrieved_class = self.send :const_get, name
|
31
|
-
else
|
32
|
-
new_class = self.send :const_set , name , klass
|
33
|
-
new_class.orientdb = orientdb
|
34
|
-
new_class # return_value
|
35
|
-
end
|
36
|
-
rescue NameError => e
|
37
|
-
logger.error "Model:: Class name cannot be initialized"
|
38
|
-
puts "klass: #{klass.inspect}"
|
39
|
-
puts "name : #{name.inspect}"
|
40
|
-
puts e.inspect
|
41
|
-
end
|
42
|
-
|
43
|
-
=begin
|
44
|
-
ActiveOrient::Model.autoload_object "#00:00"
|
45
|
-
either retrieves the object from the rid_store or loads it from the DB
|
46
|
-
|
47
|
-
the rid_store is updated!
|
48
|
-
|
49
|
-
to_do: fetch for version in the db and load the object if a change is detected
|
50
|
-
=end
|
51
|
-
def self.autoload_object link
|
52
|
-
# puts "autoload_object #{link}"
|
53
|
-
link_cluster_and_record = link[1,link.size].split(':').map &:to_i
|
54
|
-
@@rid_store[link_cluster_and_record].presence || orientdb.get_document( link )
|
55
|
-
end
|
56
|
-
|
57
|
-
|
58
|
-
def self.superClass
|
59
|
-
orientdb.get_classes( 'name', 'superClass').detect{|x| x["name"].downcase == new.class.to_s.downcase.split(':')[-1].to_s
|
60
|
-
}['superClass']
|
61
|
-
end
|
62
|
-
|
63
|
-
def to_orient
|
64
|
-
link
|
65
|
-
end
|
66
|
-
def from_orient
|
67
|
-
self
|
68
|
-
end
|
69
|
-
=begin
|
70
|
-
Returns just the name of the Class
|
71
|
-
=end
|
72
|
-
def classname
|
73
|
-
self.class.to_s.split(':')[-1]
|
74
|
-
end
|
75
|
-
|
76
|
-
=begin
|
77
|
-
If a Rest::Model-Object is included in a HashWidhtIndifferentAccess-Object, only the link is stored
|
78
|
-
=end
|
79
|
-
def nested_under_indifferent_access # :nodoc:
|
80
|
-
link
|
81
|
-
end
|
82
|
-
|
83
|
-
# hard-coded orientdb-columns
|
84
|
-
# prop :cluster, :version, :record, :fieldtypes
|
85
|
-
|
86
|
-
# def default_attributes
|
87
|
-
# super.merge cluster: 0
|
88
|
-
# super.merge version: 0
|
89
|
-
# super.merge record: 0
|
90
|
-
# end
|
91
|
-
def riid # :nodoc:
|
92
|
-
[ @metadata[ :cluster ] , @metadata[ :record ] ]
|
93
|
-
end
|
94
|
-
=begin
|
95
|
-
rid is used in the where-part of sql-queries
|
96
|
-
=end
|
97
|
-
def rid
|
98
|
-
"#{@metadata[ :cluster ]}:#{@metadata[ :record ]}"
|
99
|
-
rescue
|
100
|
-
"0:0"
|
101
|
-
end
|
102
|
-
=begin
|
103
|
-
link is used in any sql-commands
|
104
|
-
eg . update #link set ...
|
105
|
-
=end
|
106
|
-
def link
|
107
|
-
"##{rid}"
|
108
|
-
end
|
109
|
-
|
110
|
-
|
111
|
-
# def method_missing method, *args, &b
|
112
|
-
# property= orientdb.get_class_properties( classname )['properties'].detect{|x| x.has_value? method.to_s }
|
113
|
-
# puts "method_missing::property"
|
114
|
-
# puts property.inspect
|
115
|
-
# if property.present?
|
116
|
-
# if property['type'] == 'LINKSET'
|
117
|
-
# attributes[method] = OrientSupport::Array.new( self )
|
118
|
-
# else
|
119
|
-
# attributes[method] = ''
|
120
|
-
# end
|
121
|
-
# else
|
122
|
-
# raise NoMethodError
|
123
|
-
# end
|
124
|
-
# end
|
125
|
-
=begin
|
126
|
-
Queries the database and fetches the count of datasets
|
127
|
-
|
128
|
-
Any parameter that qualifies a database-query is suppoerted
|
129
|
-
(see method get_documents)
|
130
|
-
=end
|
131
|
-
def self.count **args
|
132
|
-
orientdb.count_documents from: self , **args
|
133
|
-
end
|
134
|
-
=begin
|
135
|
-
Creates a new Instance of the Class with the applied attributes
|
136
|
-
and returns the freshly instantiated Object
|
137
|
-
=end
|
138
|
-
|
139
|
-
def self.create properties = {}
|
140
|
-
orientdb.create_or_update_document self, set: properties
|
141
|
-
end
|
142
|
-
|
143
|
-
def self.update_or_create set: {}, where:{} ,**args, &b
|
144
|
-
orientdb.update_or_create_documents self , set: set, where: where , **args , &b
|
145
|
-
|
146
|
-
end
|
147
|
-
|
148
|
-
# historic method
|
149
|
-
def self.new_document attributes: {} # :nodoc:
|
150
|
-
orientdb.create_or_update_document self, set: attributes
|
151
|
-
end
|
152
|
-
=begin
|
153
|
-
Create a Property in the Schema of the Class
|
154
|
-
:call-seq:
|
155
|
-
self.create_property( field (required) , type: 'string', linked_class: nil, index: nil) do
|
156
|
-
index
|
157
|
-
end
|
158
|
-
=end
|
159
|
-
|
160
|
-
def self.get_properties
|
161
|
-
object = orientdb.get_class_properties self
|
162
|
-
{:properties => object['properties'] , :indexes => object['indexes'] }
|
163
|
-
end
|
164
|
-
|
165
|
-
def self.create_property field, **keyword_arguments, &b
|
166
|
-
orientdb.create_property self, field, **keyword_arguments, &b
|
167
|
-
end
|
168
|
-
|
169
|
-
def self.create_properties argument_hash, &b
|
170
|
-
orientdb.create_properties self, argument_hash, &b
|
171
|
-
end
|
172
|
-
|
173
|
-
def self.create_link name, class_name
|
174
|
-
orientdb.create_property self, name, type: 'link', linked_class: class_name
|
175
|
-
end
|
176
|
-
def self.create_linkset name, class_name
|
177
|
-
orientdb.create_property self, name, type: 'linkset', linked_class: class_name
|
178
|
-
end
|
179
|
-
=begin
|
180
|
-
Only if the Class inherents from »E«
|
181
|
-
Instantiate a new Edge betwen two Vertices
|
182
|
-
|
183
|
-
Parameter: unique: (true) In case of an existing Edge just update its Properties.
|
184
|
-
The parameters »from« and »to« can take a list of model-records. Then subsequent edges are created.
|
185
|
-
:call-seq:
|
186
|
-
self.create_edge from: , to: , unique: false, attributes:{}
|
187
|
-
=end
|
188
|
-
def self.create_edge **keyword_arguments
|
189
|
-
o=orientdb.nexus_edge self, **keyword_arguments
|
190
|
-
[:from,:to].each{|y| keyword_arguments[y].is_a?(Array) ? keyword_arguments[y].each( &:reload! ): keyword_arguments[y].reload! }
|
191
|
-
o # return_value
|
192
|
-
end
|
193
|
-
=begin
|
194
|
-
QueryDatabase sends the Query, direct to the database.
|
195
|
-
The result is not nessessary a Object of self.
|
196
|
-
However, if the query does not return an array of Active::Model-Objects, then the entries become self
|
197
|
-
=end
|
198
|
-
def self.query_database query, set_from: true
|
199
|
-
query.from self if set_from && query.is_a?( OrientSupport::OrientQuery ) && query.from.nil?
|
200
|
-
sql_cmd = -> (command) { { type: "cmd", language: "sql", command: command } }
|
201
|
-
orientdb.execute( self.to_s.split(':')[-1] ) do
|
202
|
-
[ sql_cmd[ query.to_s ] ]
|
203
|
-
end
|
204
|
-
end
|
205
|
-
def query q
|
206
|
-
a= ActiveOrient::Query.new
|
207
|
-
a.queries << q
|
208
|
-
a.execute_queries
|
209
|
-
end
|
210
|
-
=begin
|
211
|
-
Parameter projection:
|
212
|
-
|
213
|
-
»select« is a method of enumeration, we use »projection:« to specify anything between »select« and »from«
|
214
|
-
in the query-string.
|
215
|
-
|
216
|
-
projection: a_string --> inserts the sting as it appears
|
217
|
-
an OrientSupport::OrientQuery-Object --> performs a sub-query and uses the result for further querying though the given parameters.
|
218
|
-
|
219
|
-
[ a, b, c ] --> "a , b , c " ( inserts a comma-separated list )
|
220
|
-
|
221
|
-
{ a: b, "sum(x) "=> f } --> "a as b, sum(x) as f" (renames properties and uses functions )
|
222
|
-
Parameter distinct:
|
223
|
-
|
224
|
-
Performs a Query like » select distinct( property ) [ as property ] from ...«
|
225
|
-
|
226
|
-
distinct: :property --> the result is mapped to the property »distinct«.
|
227
|
-
|
228
|
-
[ :property ] --> the result replaces the property
|
229
|
-
|
230
|
-
{ property: :some_name} --> the result is mapped to ModelInstance.some_name
|
231
|
-
|
232
|
-
Parameter Order
|
233
|
-
|
234
|
-
Sorts the result-set.
|
235
|
-
|
236
|
-
If new properties are introduced via select:, distinct: etc
|
237
|
-
|
238
|
-
Sorting takes place on these properties
|
239
|
-
|
240
|
-
order: :property
|
241
|
-
{ property: asc, property: desc }
|
242
|
-
[property, property, .. ] ( orderdirection is 'asc' )
|
243
|
-
|
244
|
-
|
245
|
-
Further supported Parameter:
|
246
|
-
group_by
|
247
|
-
skip
|
248
|
-
limit
|
249
|
-
unwind
|
250
|
-
|
251
|
-
see orientdb- documentation (https://orientdb.com/docs/last/SQL-Query.html)
|
252
|
-
|
253
|
-
Parameter query:
|
254
|
-
Instead of providing the parameter, the OrientSupport::OrientQuery can build and
|
255
|
-
tested before the method-call. The OrientQuery-Object can be provided with the query-parameter
|
256
|
-
i.e.
|
257
|
-
q= OrientSupport::OrientQuery.new
|
258
|
-
TestModel = r.open_class 'test_model'
|
259
|
-
q.from TestModel
|
260
|
-
q.where { name: 'Thomas' }
|
261
|
-
|
262
|
-
count= TestModel.count query:q
|
263
|
-
q.limit 10
|
264
|
-
0.step(count,10) do |x|
|
265
|
-
q.skip = x
|
266
|
-
puts TestModel.get_documents( query: q ).map{|x| x.adress }.join('\t')
|
267
|
-
end
|
268
|
-
prints a Table with 10 columns.
|
269
|
-
=end
|
270
|
-
|
271
|
-
def self.get_documents **args
|
272
|
-
orientdb.get_documents( from: self, **args ){ self }
|
273
|
-
|
274
|
-
end
|
275
|
-
=begin
|
276
|
-
Performs a query on the Class and returns an Array of ActiveOrient:Model-Records.
|
277
|
-
|
278
|
-
Example:
|
279
|
-
Log = r.open_class 'Log'
|
280
|
-
Log.where priority: 'high'
|
281
|
-
--> submited database-request: query/hc_database/sql/select from Log where priority = 'high'/-1
|
282
|
-
=> [ #<ActiveOrient::Model::Log:0x0000000480f7d8 @metadata={ ... }, ... ]
|
283
|
-
|
284
|
-
|
285
|
-
=end
|
286
|
-
def self.where attributes = {}
|
287
|
-
q = OrientSupport::OrientQuery.new where: attributes
|
288
|
-
query_database q
|
289
|
-
end
|
290
|
-
=begin
|
291
|
-
|
292
|
-
removes the Model-Instance from the database
|
293
|
-
|
294
|
-
returns true (successfully deleted) or false ( obj not deleted)
|
295
|
-
=end
|
296
|
-
def delete
|
297
|
-
|
298
|
-
r= if is_edge?
|
299
|
-
# returns the count of deleted edges
|
300
|
-
orientdb.delete_edge rid
|
301
|
-
else
|
302
|
-
orientdb.delete_document rid
|
303
|
-
end
|
304
|
-
ActiveOrient::Base.remove_riid self if r # removes the obj from the rid_store
|
305
|
-
r # true or false
|
306
|
-
end
|
307
|
-
=begin
|
308
|
-
An Edge is defined
|
309
|
-
* when inherented from the superclass »E» (formal definition)
|
310
|
-
* if it has an in- and an out property
|
311
|
-
|
312
|
-
Actually we just check the second term as we trust the constuctor to work properly
|
313
|
-
=end
|
314
|
-
def is_edge?
|
315
|
-
attributes.keys.include?( 'in') && attributes.keys.include?('out')
|
316
|
-
end
|
317
|
-
# get enables loading of datasets if a link is followed
|
318
|
-
# model_class.all.first.link.get
|
319
|
-
def self.get rid
|
320
|
-
orientdb.get_document rid
|
321
|
-
end
|
322
|
-
def self.all
|
323
|
-
orientdb.get_documents from: self
|
324
|
-
end
|
325
|
-
def self.first where: {}
|
326
|
-
orientdb.get_documents( from: self, where: where, limit: 1).pop
|
327
|
-
end
|
328
|
-
|
329
|
-
def self.last where: {}
|
330
|
-
# debug:: orientdb.get_documents( self, order: { "@rid" => 'desc' }, limit: 1 ){ |x| puts x }.pop
|
331
|
-
orientdb.get_documents( from: self, where: where, order: { "@rid" => 'desc' }, limit: 1 ).pop
|
332
|
-
end
|
333
|
-
=begin
|
334
|
-
Convient update of the dataset by calling sql-patch
|
335
|
-
The attributes are saved to the database.
|
336
|
-
With the optional :set argument ad-hoc attributes can be defined
|
337
|
-
obj = ActiveOrient::Model::Contracts.first
|
338
|
-
obj.name = 'new_name'
|
339
|
-
obj.update set: { yesterdays_event: 35 }
|
340
|
-
=end
|
341
|
-
def update set: {}
|
342
|
-
attributes.merge!( set ) if set.present?
|
343
|
-
result= orientdb.patch_document(rid) do
|
344
|
-
attributes.merge( { '@version' => @metadata[ :version ], '@class' => @metadata[ :class ] } )
|
345
|
-
end
|
346
|
-
# returns a new instance of ActiveOrient::Model
|
347
|
-
reload! ActiveOrient::Model.orientdb_class(name: classname).new( JSON.parse( result ))
|
348
|
-
# instantiate object, update rid_store and reassign to self
|
349
|
-
|
350
|
-
end
|
351
|
-
=begin
|
352
|
-
Overwrite the attributes with Database-Contents (or attributes provided by the updated_dataset.model-instance)
|
353
|
-
=end
|
354
|
-
def reload! updated_dataset=nil
|
355
|
-
updated_dataset = orientdb.get_document( link) if updated_dataset.nil?
|
356
|
-
@metadata[:version]= updated_dataset.version
|
357
|
-
attributes = updated_dataset.attributes
|
358
|
-
self # return_value (otherwise only the attributes would be returned)
|
359
|
-
end
|
360
|
-
|
361
|
-
def remove_item_from_property array, item=nil
|
362
|
-
logger.progname = 'ActiveOrient::Model#RemoveItemFromProperty'
|
363
|
-
execute_array = Array.new
|
364
|
-
return unless attributes.has_key? array
|
365
|
-
remove_execute_array = -> (it) do
|
366
|
-
case it
|
367
|
-
when ActiveOrient::Model
|
368
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} remove #{array} = #{it.link}"}
|
369
|
-
when String
|
370
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} remove #{array} = '#{it}'"}
|
371
|
-
when Numeric
|
372
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} remove #{array} = #{it}"}
|
373
|
-
else
|
374
|
-
logger.error { "Only Basic Formats supported . Cannot Serialize #{it.class} this way" }
|
375
|
-
logger.error { "Try to load the array from the DB, modify it and update the hole record" }
|
376
|
-
end
|
377
|
-
end
|
378
|
-
|
379
|
-
if block_given?
|
380
|
-
items = yield
|
381
|
-
items.each{|x| remove_execute_array[x]; self.attributes[array].delete( x ) }
|
382
|
-
elsif item.present?
|
383
|
-
remove_execute_array[item]
|
384
|
-
a= attributes; a.delete item
|
385
|
-
self.attributes[array].delete( item )
|
386
|
-
end
|
387
|
-
orientdb.execute do
|
388
|
-
execute_array
|
389
|
-
end
|
390
|
-
reload!
|
391
|
-
|
392
|
-
rescue RestClient::InternalServerError => e
|
393
|
-
logger.error " Could not remove item in #{array} "
|
394
|
-
logger.error e.inspect
|
395
|
-
end
|
396
|
-
|
397
|
-
=begin
|
398
|
-
Convient method for populating embedded- or linkset-properties
|
399
|
-
|
400
|
-
In both cases an array/ a collection is stored in the database.
|
401
|
-
|
402
|
-
its called via
|
403
|
-
model.add_item_to_property( linkset- or embedded property, Object_to_be_linked_to )
|
404
|
-
or
|
405
|
-
mode.add_items_to_property( linkset- or embedded property ) do
|
406
|
-
Array_of_Objects_to_be_linked_to
|
407
|
-
(actually, the objects must inherent from ActiveOrient::Model, Numeric, String)
|
408
|
-
end
|
409
|
-
|
410
|
-
to_do: use "<<" to add the item to the property
|
411
|
-
=end
|
412
|
-
def add_item_to_property array, item=nil
|
413
|
-
logger.progname = 'ActiveOrient::Model#AddItem2Property'
|
414
|
-
execute_array = Array.new
|
415
|
-
self.attributes[array] = Array.new unless attributes[array].present?
|
416
|
-
add_2_execute_array = -> (it) do
|
417
|
-
case it
|
418
|
-
when ActiveOrient::Model
|
419
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} add #{array} = #{it.link}"}
|
420
|
-
when String
|
421
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} add #{array} = '#{it}'"}
|
422
|
-
when Numeric
|
423
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} add #{array} = #{it}"}
|
424
|
-
else
|
425
|
-
logger.error { "Only Basic Formats supported . Cannot Serialize #{it.class} this way" }
|
426
|
-
logger.error { "Try to load the array from the DB, modify it and update the hole record" }
|
427
|
-
end
|
428
|
-
end
|
429
|
-
|
430
|
-
if block_given?
|
431
|
-
items = yield
|
432
|
-
items.each{|x| add_2_execute_array[x]; self.attributes[array] << x }
|
433
|
-
elsif item.present?
|
434
|
-
add_2_execute_array[item]
|
435
|
-
self.attributes[array] << item
|
436
|
-
end
|
437
|
-
orientdb.execute do
|
438
|
-
execute_array
|
439
|
-
end
|
440
|
-
reload!
|
441
|
-
|
442
|
-
rescue RestClient::InternalServerError => e
|
443
|
-
logger.error " Duplicate found in #{array} "
|
444
|
-
logger.error e.inspect
|
445
|
-
end
|
446
|
-
|
447
|
-
alias add_items_to_property add_item_to_property
|
448
|
-
## historical aliases
|
449
|
-
alias update_linkset add_item_to_property
|
450
|
-
alias update_embedded add_item_to_property
|
451
|
-
=begin
|
452
|
-
Convient method for updating a linkset-property
|
453
|
-
its called via
|
454
|
-
model.update_linkset( linkset-property, Object_to_be_linked_to )
|
455
|
-
or
|
456
|
-
mode.update_linkset( linkset-property ) do
|
457
|
-
Array_of_Objects_to_be_linked_to
|
458
|
-
end
|
459
|
-
=end
|
460
|
-
|
461
|
-
#private
|
462
|
-
def version
|
463
|
-
@metadata[ :version ]
|
464
|
-
end
|
465
|
-
|
466
|
-
end # class
|
467
|
-
|
468
|
-
end # module
|