lib/arcade/api/operations.rb in arcadedb-0.4 vs lib/arcade/api/operations.rb in arcadedb-0.5.0
- old
+ new
@@ -7,11 +7,11 @@
$ Arcade::Api.databases # returns an Array of known databases
$ Arcade::Api.create_database <a string> # returns true if succesfull
$ Arcade::Api.drop_database <a string> # returns true if successfull
$ Arcade::Api.create_document <database>, <type>, attributes
- $ Arcade::Api.execute( <database> ) { <query> }
+ $ Arcade::Api.execute( <database> [, session_id: some_session_id ]) { <query> }
$ Arcade::Api.query( <database> ) { <query> }
$ Arcade::Api.get_record <database>, rid # returns a hash
<query> is either a string
@@ -36,11 +36,11 @@
# creates a database if not present #
def self.create_database name
return if databases.include?( name.to_s )
payload = { "command" => "create database #{name}" }
post_data "server", payload
- rescue HTTPX::HTTPError => e
+ rescue Arcade::QueryError => e
logger.fatal "Create database #{name} through \"POST create/#{name}\" failed"
logger.fatal e
raise
end
@@ -48,11 +48,11 @@
# deletes the given database #
def self.drop_database name
return unless databases.include?( name.to_s )
payload = {"command" => "drop database #{name}" }
post_data "server", payload
- rescue HTTPX::HTTPError => e
+ rescue Arcade::QueryError => e
logger.fatal "Drop database #{name} through \"POST drop database/#{name}\" failed"
raise
end
# ------------------------------ create document ------------------------------------------------- #
# adds a document to the specified database table
@@ -61,19 +61,17 @@
#
# i.e Arcade::Api.create_document 'devel', 'documents', name: 'herta meyer', age: 56, sex: 'f'
#
# returns the rid of the inserted dataset
#
- def self.create_document database, type, **attributes
+ def self.create_document database, type, session_id: nil, **attributes
payload = { "@type" => type }.merge( attributes )
- logger.debug "C: #{payload}"
- options = if session.nil?
- payload
- else
- payload.merge headers: { "arcadedb-session-id" => session }
- end
- post_data "document/#{database}", options
+ if session_id.nil?
+ post_data "document/#{database}", payload
+ else
+ post_transaction "document/#{database}", payload, session_id: session_id
+ end
end
# ------------------------------ execute ------------------------------------------------- #
# executes a sql-query in the specified database
#
@@ -82,23 +80,27 @@
# returns an Array of results (if propriate)
# i.e
# Arcade::Api.execute( "devel" ) { 'select from test ' }
# =y [{"@rid"=>"#57:0", "@type"=>"test", "name"=>"Hugo"}, {"@rid"=>"#60:0", "@type"=>"test", "name"=>"Hubert"}]
#
- def self.execute database, query=nil, session_id= nil
- pl = query.nil? ? provide_payload(yield) : provide_payload(query)
- if session_id.nil? && session.nil?
+ def self.execute database, session_id: nil
+ pl = provide_payload(yield)
+ if session_id.nil?
post_data "command/#{database}" , pl
else
- post_transaction "command/#{database}" , pl, session_id || session
+ post_transaction "command/#{database}" , pl, session_id: session_id
end
end
# ------------------------------ query ------------------------------------------------- #
# same for idempotent queries
- def self.query database, query
- post_data "query/#{database}" , provide_payload(query)
+ def self.query database, query, session_id: nil
+ if session_id.nil?
+ post_data "query/#{database}" , provide_payload(query)
+ else
+ post_transaction "query/#{database}" , provide_payload(query), session_id: session_id
+ end
end
# ------------------------------ get_record ------------------------------------------------- #
# fetches a record by providing database and rid
# and returns the result as hash
@@ -131,25 +133,21 @@
#
# In case of an Error, anything is rolled back and nil is returned
#
def self.property database, type, **args
- begin_transaction database
+ s= begin_transaction database
success = args.map do | name, format |
- r= execute(database) {" create property #{type.to_s}.#{name.to_s} #{format.to_s} " } &.first
- puts "R: #{r.inspect}"
- if r.nil?
- false
- else
- r[:operation] == 'create property'
- end
+ r= execute(database, session_id: s) {" create property #{type.to_s}.#{name.to_s} #{format.to_s} " } &.first
+ r.nil? ? false : r[:operation] == 'create property'
end.uniq
if success == [true]
- commit database
+ commit database, session_id: s
true
else
- rollback database
+ rollback database log: false, session_id: s
+ false
end
end
@@ -158,13 +156,10 @@
properties = properties.map( &:to_s )
unique_requested = "unique" if properties.delete("unique")
unique_requested = "notunique" if properties.delete("notunique" )
automatic = true if
properties << name if properties.empty?
- # puts " create index #{type.to_s}[#{name.to_s}] on #{type} ( #{properties.join(',')} ) #{unique_requested}"
- # VV 22.10: providing an index-name raises an Error ( Encountered " "(" "( "" at line 1, column 44. Was expecting one of: <EOF> <SCHEMA> ... <NULL_STRATEGY> ... ";" ... "," ... )) )
- # named indices droped for now
success = execute(database) {" create index IF NOT EXISTS on #{type} (#{properties.join(', ')}) #{unique_requested}" } &.first
# puts "success: #{success}"
success[:operation] == 'create index'
end
@@ -174,13 +169,10 @@
def self.logger
Database.logger
end
- def self.session
- @session_id
- end
def self. provide_payload( the_yield, action: :post )
unless the_yield.is_a? Hash
logger.info "Q: #{the_yield}"
the_yield = { :query => the_yield }
@@ -205,53 +197,22 @@
when :serializer
if [:graph, :record].include? value.to_sym
[ :serializer, value.to_sym ]
end
when :language
- if [:sql, :cypher, :gremlin, :neo4j ].include? value.to_sym
+ if [:sql, :cypher, :gremlin, :neo4j, :sqlscript, :graphql, :mongo ].include? value.to_sym
[ :language, value.to_sym ]
end
end # case
end .to_h ) # map
end
- # returns the json-response ## retiered
- def self.analyse_result r, command
- if r.success?
- return nil if r.status == 204 # no content
- result = JSON.parse( r.response_body, symbolize_names: true )[:result]
- if result == [{}]
- []
- else
- result
- end
- elsif r.timed_out?
- raise Error "Timeout Error", caller
- []
- elsif r.response_code > 0
- logger.error "Execution Failure – Code: #{ r.response_code } – #{r.status_message} "
- error_message = JSON.parse( r.response_body, symbolize_names: true )
- logger.error "ErrorMessage: #{ error_message[:detail]} "
- if error_message[:detail] =~ /Duplicated key/
- raise IndexError, error_message[:detail]
- else
- # available fields: :detail, :exception, error
- puts error_message[:detail]
- #raise error_message[:detail], caller
- end
- end
- end
def self.auth
@a ||= { httpauth: :basic,
username: Config.admin[:user],
password: Config.admin[:pass] }
end
-# not tested
- def self.delete_data command
- result = HTTPX.delete Config.base_uri + command, auth
- analyse_result(result, command)
- end
end
end