Module: RestDelete

Included in:
ActiveOrient::OrientDB
Defined in:
lib/rest/delete.rb

Instance Method Summary collapse

Instance Method Details

#delete_class(o_class) ⇒ Object

Deletes the specified class and returns true on success todo: remove all instances of the class


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rest/delete.rb', line 37

def delete_class o_class
	cl = classname(o_class)
	return if cl.nil?
	logger.progname = 'RestDelete#DeleteClass'

	begin
		## to do: if cl contains special characters, enclose with backticks
		response = @res["/class/#{ActiveOrient.database}/#{cl}"].delete
		if response.code == 204
			logger.info{"Class #{cl} deleted."}

			ActiveOrient.database_classes.delete(cl)
		end
	rescue RestClient::InternalServerError => e
		sentence=  JSON.parse( e.response)['errors'].last['content']
		if ActiveOrient.database_classes.has_key? cl
			logger.error{"Class #{cl} still present."}
			logger.error{ sentence }
			false
		else
			logger.error{e.inspect}
			true
		end
	rescue Exception => e
		logger.error{e.message}
		logger.error{e.inspect}
	end
end

#delete_database(database:) ⇒ Object

Deletes the database and returns true on success

After the removal of the database, the working-database might be empty

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rest/delete.rb', line 10

def delete_database database:
  logger.progname = 'RestDelete#DeleteDatabase'
  old_ds = ActiveOrient.database
  change_database database
  begin
	  response = @res["/database/#{ActiveOrient.database}"].delete
	  if database == old_ds
	    change_database  'temp'
	    logger.info{"Working database deleted, switched to temp"}
	  else
	    change_database old_ds
	    logger.info{"Database #{database} deleted, working database is still #{ActiveOrient.database}"}
	  end
  rescue RestClient::InternalServerError => e
    change_database old_ds
	  logger.info{"Database #{database} NOT deleted, working database is still #{ActiveOrient.database}"}
  end
  !response.nil? && response.code == 204 ? true : false
end

#delete_property(o_class, field) ⇒ Object

PROPERTY #############


118
119
120
121
122
123
124
125
126
127
# File 'lib/rest/delete.rb', line 118

def delete_property o_class, field
  logger.progname = 'RestDelete#DeleteProperty'
  begin
	  response =   @res["/property/#{ActiveOrient.database}/#{classname(o_class)}/#{field}"].delete
	  true if response.code == 204
  rescue RestClient::InternalServerError => e
	  logger.error{"Property #{field} in  class #{classname(o_class)} NOT deleted" }
	    false
  end
end

#delete_record(*o) ⇒ Object Also known as: delete_document

Deletes a single Record when providing a single rid-link (#00:00) or a record

Deletes multible Records when providing a list of rid-links or a record
Todo: implement delete_edges after querying the database in one statement

Example:

ORD.create_class :test record = Test.new something: 'something' ORD.delete_record record

records= (1..100).map{|x| Test.create something: x } ORD.delete_record *records

delete_records provides the removal of datasets after quering the database.

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rest/delete.rb', line 84

def delete_record *o
   logger.progname = "ActiveOrient::RestDelete#DeleteRecord"
	o.map( &:to_orient ).map do |r|
		begin
			puts "RR: #{r.inspect}"
			ActiveOrient::Base.remove_rid(  ActiveOrient::Base.get_rid(r) ) 
			@res["/document/#{ActiveOrient.database}/#{r[1..-1]}"].delete
		rescue RestClient::InternalServerError => e
			logger.error{"Record #{r} NOT deleted"}
		rescue RestClient::ResourceNotFound
			logger.error{"Record #{r} does not exist in the database"}
		rescue RestClient::BadRequest => e
			logger.error{"tried to delete RID: #{r[1..-1]}, but something went wrong"}
			logger.error e.inspect
		else
			logger.info{"Record #{r} deleted"}
		end
	end
end

#delete_records(o_class, where: {}) ⇒ Object Also known as: delete_documents

Deletes records. They are defined by a query. All records which match the attributes are deleted.

An Array with freed index-values is returned

110
111
112
113
# File 'lib/rest/delete.rb', line 110

def delete_records o_class, where: {}
  logger.progname = 'RestDelete#DeleteRecords'
  get_records(from: o_class, where: where).each{|y| delete_record  y}
end