Class: Neo4j::Server::CypherTransaction

Inherits:
Transaction::Base show all
Includes:
Resource
Defined in:
lib/neo4j-server/cypher_transaction.rb

Overview

The CypherTransaction object lifecycle is as follows:

  • It is initialized with the transactional endpoint URL and the connection object to use for communication. It does not communicate with the server to create this.

  • The first query within the transaction sets the commit and execution addresses, :commit_url and :query_url.

  • At any time, ‘failure` can be called to mark a transaction failed and trigger a rollback upon closure.

  • ‘close` is called to end the transaction. It calls `commit` or `delete`.

If a transaction is created and then closed without performing any queries, an OpenStruct is returned that behaves like a successfully closed query.

Constant Summary collapse

ROW_REST =
%w(row REST)
EMPTY_RESPONSE =
OpenStruct.new(status: 200, body: '')

Instance Attribute Summary collapse

Attributes included from Resource

#resource_data, #resource_url

Attributes inherited from Transaction::Base

#root, #session

Instance Method Summary collapse

Methods included from Resource

#convert_from_json_value, #expect_response_code!, #handle_response_error!, #init_resource_data, #resource_headers, #resource_url_id, #response_exception, #wrap_resource

Methods inherited from Transaction::Base

#autoclosed!, #close, #closed?, #expired?, #failed?, #initialize, #inspect, #mark_expired, #mark_failed, #root?

Constructor Details

This class inherits a constructor from Neo4j::Transaction::Base

Instance Attribute Details

#commit_urlObject (readonly)

Returns the value of attribute commit_url.



13
14
15
# File 'lib/neo4j-server/cypher_transaction.rb', line 13

def commit_url
  @commit_url
end

#query_urlObject (readonly)

Returns the value of attribute query_url.



13
14
15
# File 'lib/neo4j-server/cypher_transaction.rb', line 13

def query_url
  @query_url
end

Instance Method Details

#_query(cypher_query, params = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/neo4j-server/cypher_transaction.rb', line 27

def _query(cypher_query, params = nil)
  fail 'Transaction expired, unable to perform query' if expired?
  statement = {statement: cypher_query, parameters: params, resultDataContents: ROW_REST}
  body = {statements: [statement]}

  response = @query_url ? query(body) : start(body)

  create_cypher_response(response)
end

#base_urlObject



19
20
21
22
23
24
# File 'lib/neo4j-server/cypher_transaction.rb', line 19

def base_url
  require 'uri'
  URI(@session.instance_variable_get('@resource_url')).tap do |uri|
    uri.path = ''
  end.to_s
end

#commitObject



64
65
66
67
68
# File 'lib/neo4j-server/cypher_transaction.rb', line 64

def commit
  return EMPTY_RESPONSE if !@commit_url || expired?

  request(:post, @commit_url, 200, nil, resource_headers)
end

#connectionObject



15
16
17
# File 'lib/neo4j-server/cypher_transaction.rb', line 15

def connection
  @session.connection
end

#deleteObject



58
59
60
61
62
# File 'lib/neo4j-server/cypher_transaction.rb', line 58

def delete
  return EMPTY_RESPONSE if !@commit_url || expired?

  request(:delete, @query_url, 200, nil, resource_headers)
end

#query(body) ⇒ Object



52
53
54
# File 'lib/neo4j-server/cypher_transaction.rb', line 52

def query(body)
  request(:post, @query_url, 200, body)
end

#start(body) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/neo4j-server/cypher_transaction.rb', line 37

def start(body)
  request(:post, start_url, 201, body).tap do |response|
    @commit_url = response.body[:commit]
    @query_url = response.headers[:Location]

    fail "NO ENDPOINT URL #{connection} : HEAD: #{response.headers.inspect}" if !@query_url || @query_url.empty?

    init_resource_data(response.body, base_url)
  end
end

#start_urlObject



48
49
50
# File 'lib/neo4j-server/cypher_transaction.rb', line 48

def start_url
  @session.resource_data.fetch(:transaction) || base_url
end