Class: Neo4j::Server::CypherSession
Constant Summary
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
-
#_query(q, params = nil) ⇒ Object
-
#_query_data(q) ⇒ Object
-
#_query_entity_data(q, id = nil, params = nil) ⇒ Object
-
#_query_or_fail(q, single_row = false, params = nil) ⇒ Object
-
#begin_tx ⇒ Object
-
#close ⇒ Object
-
#create_label(name) ⇒ Object
-
#create_node(props = nil, labels = []) ⇒ Object
-
#db_type ⇒ Object
-
#find_all_nodes(label_name) ⇒ Object
-
#find_nodes(label_name, key, value) ⇒ Object
-
#indexes(label) ⇒ Object
-
#initialize(data_url, connection, auth_obj = nil) ⇒ CypherSession
constructor
A new instance of CypherSession.
-
#initialize_resource(data_url) ⇒ Object
-
#inspect ⇒ Object
-
#load_entity(clazz, cypher_response) ⇒ Object
-
#load_node(neo_id) ⇒ Object
-
#load_relationship(neo_id) ⇒ Object
-
#map_column(key, map, data) ⇒ Object
-
#query(*args) ⇒ Object
-
#schema_properties(query_string) ⇒ Object
-
#search_result_to_enumerable_first_column(response) ⇒ Object
-
#search_result_to_enumerable_first_column_with_tx(response) ⇒ Object
-
#search_result_to_enumerable_first_column_without_tx(response) ⇒ Object
-
#super_query ⇒ Object
-
#to_s ⇒ Object
-
#uniqueness_constraints(label) ⇒ Object
-
#version ⇒ Object
#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
_listeners, _notify_listeners, add_listener, #auto_commit?, create_session, current, current!, inspect, named, on_session_available, open_named, query, register, register_db, #running, set_current, #shutdown, #start, unregister, user_agent_string
Constructor Details
#initialize(data_url, connection, auth_obj = nil) ⇒ CypherSession
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
#auth ⇒ Object
Returns the value of attribute auth.
13
14
15
|
# File 'lib/neo4j-server/cypher_session.rb', line 13
def auth
@auth
end
|
#connection ⇒ Object
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
.create_connection(params) ⇒ Faraday
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 :json, content_type: 'application/json'
b.use Faraday::Adapter::NetHttpPersistent
end
conn. = {'Content-Type' => 'application/json', 'User-Agent' => ::Neo4j::Session.user_agent_string}
conn
end
|
.establish_session(root_data, connection, auth_obj) ⇒ Object
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
|
.open(endpoint_url = nil, params = {}) ⇒ Object
Opens a session to the database
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 = {})
(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
#_query(q, params = nil) ⇒ Object
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)
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
|
#_query_data(q) ⇒ Object
189
190
191
192
193
|
# File 'lib/neo4j-server/cypher_session.rb', line 189
def _query_data(q)
r = _query_or_fail(q, true)
Neo4j::Transaction.current ? r : r['data']
end
|
#_query_entity_data(q, id = nil, params = nil) ⇒ Object
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
|
#_query_or_fail(q, single_row = false, params = nil) ⇒ Object
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
|
#create_label(name) ⇒ Object
144
145
146
|
# File 'lib/neo4j-server/cypher_session.rb', line 144
def create_label(name)
CypherLabel.new(self, name)
end
|
#create_node(props = nil, labels = []) ⇒ Object
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
|
#db_type ⇒ Object
73
74
75
|
# File 'lib/neo4j-server/cypher_session.rb', line 73
def db_type
:server_db
end
|
#find_all_nodes(label_name) ⇒ Object
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
|
#find_nodes(label_name, key, value) ⇒ Object
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
|
#indexes(label) ⇒ Object
152
153
154
|
# File 'lib/neo4j-server/cypher_session.rb', line 152
def indexes(label)
schema_properties("#{@resource_url}schema/index/#{label}")
end
|
#initialize_resource(data_url) ⇒ Object
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
init_resource_data(data_resource, data_url)
end
|
#inspect ⇒ Object
81
82
83
|
# File 'lib/neo4j-server/cypher_session.rb', line 81
def inspect
"#{self} version: '#{version}'"
end
|
#load_entity(clazz, cypher_response) ⇒ Object
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/ return nil
else
clazz.new(self, data)
end
end
|
#load_node(neo_id) ⇒ Object
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
|
#load_relationship(neo_id) ⇒ Object
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
|
#map_column(key, map, data) ⇒ Object
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
|
#query(*args) ⇒ Object
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
|
#schema_properties(query_string) ⇒ Object
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
|
#search_result_to_enumerable_first_column(response) ⇒ Object
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
|
#search_result_to_enumerable_first_column_with_tx(response) ⇒ Object
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
|
#search_result_to_enumerable_first_column_without_tx(response) ⇒ Object
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
|
#super_query ⇒ Object
12
|
# File 'lib/neo4j-server/cypher_session.rb', line 12
alias_method :super_query, :query
|
#to_s ⇒ Object
77
78
79
|
# File 'lib/neo4j-server/cypher_session.rb', line 77
def to_s
"#{self.class} url: '#{@resource_url}'"
end
|
#uniqueness_constraints(label) ⇒ Object
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
|
#version ⇒ Object
85
86
87
|
# File 'lib/neo4j-server/cypher_session.rb', line 85
def version
resource_data ? resource_data['neo4j_version'] : ''
end
|