lib/rest/create.rb
update or insert one record is implemented as upsert.
The where-condition is merged into the set-attributes if its a hash.  
Otherwise it's taken unmodified.

The method returns the included or the updated dataset

## to do
# yield works for updated and for inserted datasets
# upsert ( ) do | what, record |
# if what == :insert 
#   do stuff with insert
#   if what ==  :update
#   do stuff with update
# end
# returns nil if no insert and no update was made, ie. the dataset is identical to the given attributes
update or insert one record is implemented as upsert.
The where-condition is merged into the set-attributes if its a hash.
Otherwise it's taken unmodified.

The method returns the included or the updated dataset

to do
yield works for updated and for inserted datasets
upsert ( ) do | what, record |
if what == :insert
do stuff with insert
if what ==  :update
do stuff with update
end
returns nil if no insert and no update was made, ie. the dataset is identical to the given attributes
update or insert one record is implemented as upsert . The where - condition is merged into the set - attributes if its a hash . Otherwise it ' s taken unmodified . The method returns the included or the updated dataset to do yield works for updated and for inserted datasets upsert ( ) do | what record | if what == : insert do stuff with insert if what == : update do stuff with update end returns nil if no insert and no update was made ie . the dataset is identical to the given attributes
def upsert o_class, set: {}, where: {}   
    logger.progname = 'RestCreate#Upsert'
    if where.blank?
      new_record = create_record(o_class, attributes: set)
      yield new_record if block_given?	  
      new_record			  
    else
      specify_return_value =  block_given? ? "" : "return after @this"
      set.merge! where if where.is_a?( Hash ) 
      command = "Update #{classname(o_class)} set #{generate_sql_list( set ){','}} upsert #{specify_return_value}  #{compose_where where}" 
      result = execute  tolerated_error_code: /found duplicated key/, raw: true do 
	[ { type: "cmd", language: 'sql', command: command}]
      end 
      result =result.pop if result.is_a? Array
      if result.is_a? Hash 
	if result.has_key?('@class')
	  the_object = ActiveOrient::Model.orientdb_class(name: result['@class']).new  result
	   ActiveOrient::Base.store_rid( the_object )   
	elsif result.has_key?('value')
	  the_record=  get_records(from: o_class, where: where, limit: 1).pop
	  if  result['value'].to_i == 1
	    yield the_record 	if block_given?
	    logger.info{ "Dataset updated" }
	  elsif result['value'].to_i == 0
	    logger.info{ "Dataset inserted"}
	  end
	  the_record  
	else
	  logger.error{ "Unexpected result form Query \n  #{command} \n Result: #{result}" }
	  raise ArgumentError
	end
      else
	logger.debug{ "No Insert or Update nessesary \n #{command} " }
    end
    end
  end
def upsert o_class , set : { } , where : { } logger . progname = 'RestCreate#Upsert' if where . blank? new_record = create_record ( o_class , attributes : set ) yield new_record if block_given? new_record else specify_return_value = block_given? ? "" : "return after @this" set . merge! where if where . is_a? ( Hash ) command = "Update #{classname(o_class)} set #{generate_sql_list( set ){','}} upsert #{specify_return_value}  #{compose_where where}" result = execute tolerated_error_code : / found duplicated key / , raw : true do [ { type : "cmd" , language : 'sql' , command : command } ] end result = result . pop if result . is_a? Array if result . is_a? Hash if result . has_key? ( '@class' ) the_object = ActiveOrient :: Model . orientdb_class ( name : result [ '@class' ] ) . new result ActiveOrient :: Base . store_rid ( the_object ) elsif result . has_key? ( 'value' ) the_record = get_records ( from : o_class , where : where , limit : 1 ) . pop if result [ 'value' ] . to_i == 1 yield the_record if block_given? logger . info { "Dataset updated" } elsif result [ 'value' ] . to_i == 0 logger . info { "Dataset inserted" } end the_record else logger . error { "Unexpected result form Query \n  #{command} \n Result: #{result}" } raise ArgumentError end else logger . debug { "No Insert or Update nessesary \n #{command} " } end end end
update or insert one record is implemented as upsert.
update or insert one record is implemented as upsert .
#    use Model#Upsert instead # in case if insert execute optional block # return_value # copy where attributes to set  #    puts "COMMAND: #{command} " # To execute commands # update cache ## process Code if a new dataset is inserted # return_value
o_class
<not_specific>
set
<not_specific>
where
<not_specific>