lib/rest/operations.rb
Executes a list of commands and returns the result-array (if present)

(External use)

If soley a string is provided in the block, a minimal database-console is realized.
i.e.

  ORD.execute{ 'select from #25:0' }

(Internal Use)

Structure of the provided block:
  [{type: "cmd", language: "sql",  command: "create class Person extends V"}, (...)]
--
  It was first used by ActiveOrient::Query.execute_queries
  Later I (topofocus) discovered that some Queries are not interpretated correctly by #GetRecords but are submitted without Error via batch-processing.
  For instance, this valid query
   select expand(first_list[5].second_list[9]) from base where label = 9
  can only be submitted via batch
++
Parameters:
 
 transaction:  true|false   Perform the batch as transaction
 tolerate_error_code: /a regular expression/   
 Statements to execute are provided via block
 These statements are translated to json and transmitted to the database. Example:

    	{ type: "cmd",
          language: 'sql',
          command: "CREATE EDGE #{classname(o_class)} FROM #{from.to_orient} TO #{to.to_orient}"}

Multible statements are transmitted at once if the Block provides an Array of statements.


Executes a list of commands and returns the result-array (if present)
(External use)

If soley a string is provided in the block, a minimal database-console is realized.

ORD.execute{ 'select from #25:0' }

(Internal Use)



It was first used by ActiveOrient::Query.execute_queries
Later I (topofocus) discovered that some Queries are not interpretated correctly by #GetRecords but are submitted without Error via batch-processing.
For instance, this valid query
select expand(first_list[5].second_list[9]) from base where label = 9
can only be submitted via batch
Parameters.

true|false   Perform the batch as transaction
tolerate_error_code: /a regular expression
Statements to execute are provided via block
These statements are translated to json and transmitted to the database. Example.



Multible statements are transmitted at once if the Block provides an Array of statements.
Executes a list of commands and returns the result - array ( if present ) ( External use ) If soley a string is provided in the block a minimal database - console is realized . ORD . execute { ' select from #25 : 0 ' } ( Internal Use ) It was first used by ActiveOrient :: Query . execute_queries Later I ( topofocus ) discovered that some Queries are not interpretated correctly by #GetRecords but are submitted without Error via batch - processing . For instance this valid query select expand ( first_list [ 5 ] . second_list [ 9 ] ) from base where label = 9 can only be submitted via batch Parameters . true|false Perform the batch as transaction tolerate_error_code : / a regular expression Statements to execute are provided via block These statements are translated to json and transmitted to the database . Example . Multible statements are transmitted at once if the Block provides an Array of statements .
def execute transaction: true, tolerated_error_code: nil, process_error: true, raw: nil
    batch = {transaction: transaction, operations: yield}
    logger.progname= "Execute"
    unless batch[:operations].blank?
      batch[:operations] = {:type=>"cmd", :language=>"sql", :command=> batch[:operations]} if batch[:operations].is_a? String
      batch[:operations] = [batch[:operations]] unless batch[:operations].is_a? Array
      batch[:operations].compact!
      begin
	logger.debug{ batch[:operations].map{|y|y[:command]}.join("; ") } 
        response = @res["/batch/#{ActiveOrient.database}"].post batch.to_json
      rescue RestClient::BadRequest => f
	sentence=  JSON.parse( f.response)['errors'].last['content']
	logger.fatal{ " BadRequest --> #{sentence.split("\n")[1]} " }
	puts "Query not recognized"
	puts sentence
	raise
      rescue RestClient::InternalServerError => e
        logger.progname = 'RestOperations#Execute'
	sentence=  JSON.parse( e.response)['errors'].last['content']
	if tolerated_error_code.present? &&  e.response =~ tolerated_error_code
	  logger.info{ "tolerated_error::#{e.message}"}
	else
	  if process_error
	  logger.error{sentence}
	  logger.error{ e.backtrace.map {|l| "  #{l}\n"}.join  }
	  else 
	    raise
	  end
	end 
      rescue Errno::EADDRNOTAVAIL => e
	sleep(2)
	retry
      end
      if response.present? && response.code == 200
        if response.body['result'].present?
          result=JSON.parse(response.body)['result']
	  return result if raw.present?
          result.map do |x|
            if x.is_a? Hash
              if x.has_key?('@class')
		the_object = ActiveOrient::Model.orientdb_class( name: x['@class'] ).new x
		ActiveOrient::Base.store_rid( the_object )   
              elsif x.has_key?('value')
                x['value']
              else   
                ActiveOrient::Model.orientdb_class(name: 'query' ).new x
              end
            end
          end.compact 
        else
          response.body
        end
      else
        nil
      end
    end
  end
def execute transaction : true , tolerated_error_code : nil , process_error : true , raw : nil batch = { transaction : transaction , operations : yield } logger . progname = "Execute" unless batch [ :operations ] . blank? batch [ :operations ] = { :type => "cmd" , :language => "sql" , :command => batch [ :operations ] } if batch [ :operations ] . is_a? String batch [ :operations ] = [ batch [ :operations ] ] unless batch [ :operations ] . is_a? Array batch [ :operations ] . compact! begin logger . debug { batch [ :operations ] . map { | y | y [ :command ] } . join ( "; " ) } response = @res [ "/batch/#{ActiveOrient.database}" ] . post batch . to_json rescue RestClient :: BadRequest => f sentence = JSON . parse ( f . response ) [ 'errors' ] . last [ 'content' ] logger . fatal { " BadRequest --> #{sentence.split("\n")[1]} " } puts "Query not recognized" puts sentence raise rescue RestClient :: InternalServerError => e logger . progname = 'RestOperations#Execute' sentence = JSON . parse ( e . response ) [ 'errors' ] . last [ 'content' ] if tolerated_error_code . present? && e . response =~ tolerated_error_code logger . info { "tolerated_error::#{e.message}" } else if process_error logger . error { sentence } logger . error { e . backtrace . map { | l | "  #{l}\n" } . join } else raise end end rescue Errno :: EADDRNOTAVAIL => e sleep ( 2 ) retry end if response . present? && response . code == 200 if response . body [ 'result' ] . present? result = JSON . parse ( response . body ) [ 'result' ] return result if raw . present? result . map do | x | if x . is_a? Hash if x . has_key? ( '@class' ) the_object = ActiveOrient :: Model . orientdb_class ( name : x [ '@class' ] ) . new x ActiveOrient :: Base . store_rid ( the_object ) elsif x . has_key? ( 'value' ) x [ 'value' ] else ActiveOrient :: Model . orientdb_class ( name : 'query' ) . new x end end end . compact else response . body end else nil end end end
Executes a list of commands and returns the result-array (if present)
(External use)
Executes a list of commands and returns the result - array ( if present ) ( External use )
#    puts "batch: #{batch[:operations]}" # transaction is true only for multible statements #      batch[:transaction] = transaction & batch[:operations].size >1 # extract the misspelled query in logfile and abort #	    puts batch.to_json #	  logger.error{e.response} #	  logger.error{e.message.to_s} # update cache # create a dummy class and fill with attributes from result-set # return_value
transaction
<not_specific>
tolerated_error_code
<not_specific>
process_error
<not_specific>
raw
<not_specific>