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, requery: false) ⇒ Object

Returns an array with all names of the classes of the database. Uses a cached version if possible.

Parameters: system_classes: false|true, requery: false|true


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

def database_classes system_classes: nil, requery: false
  class_hierarchy system_classes: system_classes #requery: true
  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


67
68
69
70
71
# File 'lib/database_utils.rb', line 67

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


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

def preallocate_classes   from_model_dir= nil  # :nodoc:
	# always scan for E+V model-file 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_dataset =  true
		# 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  

			logger.info{ "#{detected_class.name} -->  Class NOT allocated"}
			ActiveOrient.database_classes[ detected_class.ref_name ] = "no model file"
			keep_the_dataset = false 
		end
		keep_the_dataset # return_value
	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"] ## added in Orentdb 3.0
	if abstract
		basic
	else
		basic + extended + v3
	end
end