Class: Neo4j::Server::CypherSession

Inherits:
Neo4j::Session show all
Includes:
Core::CypherTranslator, Resource
Defined in:
lib/neo4j-server/cypher_session.rb

Constant Summary

Constant Summary

Constants included from Core::CypherTranslator

Core::CypherTranslator::EMPTY_PROPS, Core::CypherTranslator::SANITIZE_ESCAPED_REGEXP

Instance Attribute Summary (collapse)

Attributes included from Resource

#resource_data, #resource_url

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Core::CypherTranslator

#create_escape_value, #cypher_prop_list, #cypher_string, #escape_quotes, #escape_value, #label_string, #prop_identifier, #sanitize_escape_sequences, sanitized_column_names, translate_response

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 Neo4j::Session

#auto_commit?, current, current!, inspect, named, on_session_available, open_named, query, #running, set_current, #shutdown, #start, user_agent_string

Constructor Details

- (CypherSession) initialize(data_url, connection, auth_obj = nil)

Returns a new instance of CypherSession



15
16
17
18
19
20
21
# File 'lib/neo4j-server/cypher_session.rb', line 15

def initialize(data_url, connection, auth_obj = nil)
  @connection = connection
  @auth = auth_obj if auth_obj
  Neo4j::Session.register(self)
  initialize_resource(data_url)
  Neo4j::Session._notify_listeners(:session_available, self)
end

Instance Attribute Details

- (Object) auth (readonly)

Returns the value of attribute auth



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

def auth
  @auth
end

- (Object) connection (readonly)

Returns the value of attribute connection



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

def connection
  @connection
end

Class Method Details

+ (Faraday) create_connection(params)

Parameters:

  • params (Hash)

    could be empty or contain basic authentication user and password

Returns:

  • (Faraday)

See Also:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/neo4j-server/cypher_session.rb', line 26

def self.create_connection(params)
  init_params = params[:initialize] && params.delete(:initialize)
  conn = Faraday.new(init_params) do |b|
    b.request :basic_auth, params[:basic_auth][:username], params[:basic_auth][:password] if params[:basic_auth]
    b.request :json
    # b.response :logger
    b.response :json, content_type: 'application/json'
    # b.use Faraday::Response::RaiseError
    b.use Faraday::Adapter::NetHttpPersistent
    # b.adapter  Faraday.default_adapter
  end
  conn.headers = {'Content-Type' => 'application/json', 'User-Agent' => ::Neo4j::Session.user_agent_string}
  conn
end

+ (Object) establish_session(root_data, connection, auth_obj)



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

def self.establish_session(root_data, connection, auth_obj)
  data_url = root_data['data']
  data_url << '/' unless data_url.nil? || data_url.end_with?('/')
  CypherSession.new(data_url, connection, auth_obj)
end

+ (Object) open(endpoint_url = nil, params = {})

Opens a session to the database

Parameters:

  • endpoint_url (String) (defaults to: nil)
  • params (Hash) (defaults to: {})

    faraday params, see #create_connection or an already created faraday connection

See Also:

  • Neo4j::Session#open


46
47
48
49
50
51
52
53
54
55
# File 'lib/neo4j-server/cypher_session.rb', line 46

def self.open(endpoint_url = nil, params = {})
  extract_basic_auth(endpoint_url, params)
  connection = params[:connection] || create_connection(params)
  url = endpoint_url || 'http://localhost:7474'
  auth_obj = CypherAuthentication.new(url, connection, params)
  auth_obj.authenticate
  response = connection.get(url)
  fail "Server not available on #{url} (response code #{response.status})" unless response.status == 200
  establish_session(response.body, connection, auth_obj)
end

Instance Method Details

- (Object) _query(q, params = nil)



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/neo4j-server/cypher_session.rb', line 207

def _query(q, params = nil)
  # puts "q #{q}"
  curr_tx = Neo4j::Transaction.current
  if curr_tx
    curr_tx._query(q, params)
  else
    url = resource_url('cypher')
    q = params.nil? ? {'query' => q} : {'query' => q, 'params' => params}
    response = @connection.post(url, q)
    CypherResponse.create_with_no_tx(response)
  end
end

- (Object) _query_data(q)



189
190
191
192
193
# File 'lib/neo4j-server/cypher_session.rb', line 189

def _query_data(q)
  r = _query_or_fail(q, true)
  # the response is different if we have a transaction or not
  Neo4j::Transaction.current ? r : r['data']
end

- (Object) _query_entity_data(q, id = nil, params = nil)



201
202
203
204
205
# File 'lib/neo4j-server/cypher_session.rb', line 201

def _query_entity_data(q, id = nil, params = nil)
  response = _query(q, params)
  response.raise_error if response.error?
  response.entity_data(id)
end

- (Object) _query_or_fail(q, single_row = false, params = nil)



195
196
197
198
199
# File 'lib/neo4j-server/cypher_session.rb', line 195

def _query_or_fail(q, single_row = false, params = nil)
  response = _query(q, params)
  response.raise_error if response.error?
  single_row ? response.first_data : response
end

- (Object) begin_tx



103
104
105
106
107
108
109
110
111
# File 'lib/neo4j-server/cypher_session.rb', line 103

def begin_tx
  if Neo4j::Transaction.current
    # Handle nested transaction "placebo transaction"
    Neo4j::Transaction.current.push_nested!
  else
    wrap_resource(@connection)
  end
  Neo4j::Transaction.current
end

- (Object) close



98
99
100
101
# File 'lib/neo4j-server/cypher_session.rb', line 98

def close
  super
  Neo4j::Transaction.unregister_current
end

- (Object) create_label(name)



144
145
146
# File 'lib/neo4j-server/cypher_session.rb', line 144

def create_label(name)
  CypherLabel.new(self, name)
end

- (Object) create_node(props = nil, labels = [])



113
114
115
116
117
# File 'lib/neo4j-server/cypher_session.rb', line 113

def create_node(props = nil, labels = [])
  id = _query_or_fail(cypher_string(labels, props), true, cypher_prop_list(props))
  value = props.nil? ? id : {'id' => id, 'metadata' => {'labels' => labels}, 'data' => props}
  CypherNode.new(self, value)
end

- (Object) db_type



73
74
75
# File 'lib/neo4j-server/cypher_session.rb', line 73

def db_type
  :server_db
end

- (Object) find_all_nodes(label_name)



162
163
164
# File 'lib/neo4j-server/cypher_session.rb', line 162

def find_all_nodes(label_name)
  search_result_to_enumerable_first_column(_query_or_fail("MATCH (n:`#{label_name}`) RETURN ID(n)"))
end

- (Object) find_nodes(label_name, key, value)



166
167
168
169
170
171
172
173
174
175
# File 'lib/neo4j-server/cypher_session.rb', line 166

def find_nodes(label_name, key, value)
  value = "'#{value}'" if value.is_a? String

  response = _query_or_fail <<-CYPHER
    MATCH (n:`#{label_name}`)
    WHERE n.#{key} = #{value}
    RETURN ID(n)
  CYPHER
  search_result_to_enumerable_first_column(response)
end

- (Object) indexes(label)



152
153
154
# File 'lib/neo4j-server/cypher_session.rb', line 152

def indexes(label)
  schema_properties("#{@resource_url}schema/index/#{label}")
end

- (Object) initialize_resource(data_url)



89
90
91
92
93
94
95
96
# File 'lib/neo4j-server/cypher_session.rb', line 89

def initialize_resource(data_url)
  response = @connection.get(data_url)
  expect_response_code(response, 200)
  data_resource = response.body
  fail "No data_resource for #{response.body}" unless data_resource
  # store the resource data
  init_resource_data(data_resource, data_url)
end

- (Object) inspect



81
82
83
# File 'lib/neo4j-server/cypher_session.rb', line 81

def inspect
  "#{self} version: '#{version}'"
end

- (Object) load_entity(clazz, cypher_response)



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/neo4j-server/cypher_session.rb', line 127

def load_entity(clazz, cypher_response)
  return nil if cypher_response.data.nil? || cypher_response.data[0].nil?
  data  = if cypher_response.transaction_response?
            cypher_response.rest_data_with_id
          else
            cypher_response.first_data
          end

  if cypher_response.error?
    cypher_response.raise_error
  elsif cypher_response.error_msg =~ /not found/  # Ugly that the Neo4j API gives us this error message
    return nil
  else
    clazz.new(self, data)
  end
end

- (Object) load_node(neo_id)



119
120
121
# File 'lib/neo4j-server/cypher_session.rb', line 119

def load_node(neo_id)
  load_entity(CypherNode, _query("MATCH (n) WHERE ID(n) = #{neo_id} RETURN n"))
end

- (Object) load_relationship(neo_id)



123
124
125
# File 'lib/neo4j-server/cypher_session.rb', line 123

def load_relationship(neo_id)
  load_entity(CypherRelationship, _query("MATCH (n)-[r]-() WHERE ID(r) = #{neo_id} RETURN r"))
end

- (Object) map_column(key, map, data)



247
248
249
250
251
252
253
254
255
# File 'lib/neo4j-server/cypher_session.rb', line 247

def map_column(key, map, data)
  if map[key] == :node
    CypherNode.new(self, data).wrapper
  elsif map[key] == :rel || map[:key] || :relationship
    CypherRelationship.new(self, data)
  else
    data
  end
end

- (Object) query(*args)



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/neo4j-server/cypher_session.rb', line 177

def query(*args)
  if [[String], [String, Hash]].include?(args.map(&:class))
    query, params = args[0, 2]
    response = _query(query, params)
    response.raise_error if response.error?
    response.to_node_enumeration(query)
  else
    options = args[0] || {}
    Neo4j::Core::Query.new(options.merge(session: self))
  end
end

- (Object) schema_properties(query_string)



156
157
158
159
160
# File 'lib/neo4j-server/cypher_session.rb', line 156

def schema_properties(query_string)
  response = @connection.get(query_string)
  expect_response_code(response, 200)
  {property_keys: response.body.map { |row| row['property_keys'].map(&:to_sym) }}
end

- (Object) search_result_to_enumerable_first_column(response)



220
221
222
223
224
225
226
227
# File 'lib/neo4j-server/cypher_session.rb', line 220

def search_result_to_enumerable_first_column(response)
  return [] unless response.data
  if Neo4j::Transaction.current
    search_result_to_enumerable_first_column_with_tx(response)
  else
    search_result_to_enumerable_first_column_without_tx(response)
  end
end

- (Object) search_result_to_enumerable_first_column_with_tx(response)



229
230
231
232
233
234
235
236
237
# File 'lib/neo4j-server/cypher_session.rb', line 229

def search_result_to_enumerable_first_column_with_tx(response)
  Enumerator.new do |yielder|
    response.data.each do |data|
      data['row'].each do |id|
        yielder << CypherNode.new(self, id).wrapper
      end
    end
  end
end

- (Object) search_result_to_enumerable_first_column_without_tx(response)



239
240
241
242
243
244
245
# File 'lib/neo4j-server/cypher_session.rb', line 239

def search_result_to_enumerable_first_column_without_tx(response)
  Enumerator.new do |yielder|
    response.data.each do |data|
      yielder << CypherNode.new(self, data[0]).wrapper
    end
  end
end

- (Object) super_query



12
# File 'lib/neo4j-server/cypher_session.rb', line 12

alias_method :super_query, :query

- (Object) to_s



77
78
79
# File 'lib/neo4j-server/cypher_session.rb', line 77

def to_s
  "#{self.class} url: '#{@resource_url}'"
end

- (Object) uniqueness_constraints(label)



148
149
150
# File 'lib/neo4j-server/cypher_session.rb', line 148

def uniqueness_constraints(label)
  schema_properties("#{@resource_url}schema/constraint/#{label}/uniqueness")
end

- (Object) version



85
86
87
# File 'lib/neo4j-server/cypher_session.rb', line 85

def version
  resource_data ? resource_data['neo4j_version'] : ''
end