Module: DatabaseUtils

Included in:
ActiveOrient::API, ActiveOrient::OrientDB
Defined in:
lib/database_utils.rb

Instance Method Summary collapse

Instance Method Details

#class_hierarchy(base_class: '', system_classes: nil) ⇒ Object

Returns the class_hierachy

To fetch all Vertices:

class_hieararchy(base_class: 'V').flatten

To fetch all Edges:

class_hierarchy(base_class: 'E').flatten

– notice: To retrieve the class hierarchy from Objects avoid calling ‘ORD.classname (obj)`, because it depends on class_hierarchy.



35
36
37
38
39
40
41
42
43
44
# File 'lib/database_utils.rb', line 35

def class_hierarchy base_class: '',  system_classes: nil
#		@actual_class_hash = get_classes('name', 'superClass') #if requery || @all_classes.blank?
	fv = ->( s )	{ 	@actual_class_hash.find_all{|x| x['superClass']== s}.map{|v| v['name']} }
	fx = ->( v )  {		fv[v.strip].map{|x| ar = fx[x]; ar.empty? ? x : [x, ar]} }
	if system_classes.present?
		fx[ base_class.to_s ]
	else
		fx[ base_class.to_s ] - system_classes()  - [ ["OIdentity", ["ORole", "OUser"]]] - [ ["OShape",["OGeometryCollection","OLineString", "OMultiLineString", "OMultiPoint", "OMultiPolygon", "OPoint", "OPolygon", "ORectangle"]  ] ]
	end
end

#database_classes(system_classes: nil) ⇒ Object

Returns an array with all names of the classes of the database.

Reads the database-structure and updates the @actual_class_hash (used by class_hierachy and get_db_superclass )



55
56
57
58
59
60
61
62
63
# File 'lib/database_utils.rb', line 55

def database_classes system_classes: nil
@actual_class_hash = get_classes('name', 'superClass') 
  all_classes = get_classes('name').map(&:values).sort.flatten
  all_user_classes =  all_classes - system_classes()

  all_user_classes.each{|x| ActiveOrient.database_classes[x] = "unset" unless ActiveOrient.database_classes.has_key?(x) }
  
  ActiveOrient.database_classes.keys  # return an array of database-classnames
end

#get_db_superclass(name) ⇒ Object

Service-Method for Model#OrientdbClass



69
70
71
72
73
# File 'lib/database_utils.rb', line 69

def get_db_superclass name   #:nodoc:
#    @actual_class_hash = get_classes( 'name', 'superClass') if @actual_class_hash.nil? 
 z= @actual_class_hash.find{|x,y|  x['name'] == name.to_s } 
 z['superClass'] unless z.nil?
end

#preallocate_classes(from_model_dir = nil) ⇒ Object

preallocate classes reads any class from the @classes-Array and allocates adequat Ruby-Objects



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/database_utils.rb', line 78

def preallocate_classes   from_model_dir= nil  # :nodoc:
	# always scan for E+V model-files and include
	[E,V].each{|y| y.require_model_file(from_model_dir) }

	ActiveOrient.database_classes.each do | db_name, the_class |
		allocate_class_in_ruby( db_name ) do |detected_class|
			# keep the class if it is already noted in database_classes 
       if [E,V].include?(detected_class) ||
					 ActiveOrient.database_classes.key( detected_class) || 
					 detected_class.require_model_file(from_model_dir) ||
					 ActiveOrient::Model.keep_models_without_file  
				true	#  return_value
			else
				logger.info{ "#{detected_class.name} -->  Class NOT allocated"}
				ActiveOrient.database_classes[ detected_class.ref_name ] = "no model file"
				false 	#  return_value
			end
		end  # block
	end  # each iteration
end

#system_classes(abstract: false) ⇒ Object

returns the classes set by OrientDB

Parameter:

abstract: true|false

if abstract: true is given, only basic classes (Abstact-Classes) are returend



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/database_utils.rb', line 10

def system_classes abstract: false

	basic=   [  "ORestricted", "OSchedule", "OTriggered", "OSequence"]
	## "ORid" dropped in V2.2
	extended = ["OIdentity","ORole",  "OUser", "OFunction", "_studio"]
	v3 = ["OGeometryCollection", "OLineString", "OMultiLineString", "OMultiPoint", "OMultiPolygon",
	"OPoint", "OPolygon", "ORectangle", "OShape", 'OSecurityPolicy'] ## added in Orentdb 3.0 and 3.1
	if abstract
		basic
	else
		basic + extended + v3
	end
end