Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make more basic, important queries use params #146

Merged
merged 2 commits into from Dec 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 16 additions & 20 deletions lib/neo4j-server/cypher_node.rb
Expand Up @@ -46,7 +46,7 @@ def props
if @props
@props
else
hash = @session._query_entity_data("#{match_start} RETURN n")
hash = @session._query_entity_data("#{match_start} RETURN n", nil, neo_id: neo_id)
@props = Hash[hash['data'].map{ |k, v| [k.to_sym, v] }]
end
end
Expand All @@ -58,20 +58,20 @@ def refresh
# (see Neo4j::Node#remove_property)
def remove_property(key)
refresh
@session._query_or_fail("#{match_start} REMOVE n.`#{key}`")
@session._query_or_fail("#{match_start} REMOVE n.`#{key}`", false, { neo_id: neo_id })
end

# (see Neo4j::Node#set_property)
def set_property(key,value)
refresh
@session._query_or_fail("#{match_start} SET n.`#{key}` = { value }", false, value: value)
@session._query_or_fail("#{match_start} SET n.`#{key}` = { value }", false, { value: value, neo_id: neo_id })
value
end

# (see Neo4j::Node#props=)
def props=(properties)
refresh
@session._query_or_fail("#{match_start} SET n = { props }", false, {props: properties})
@session._query_or_fail("#{match_start} SET n = { props }", false, { props: properties, neo_id: neo_id })
properties
end

Expand All @@ -80,7 +80,7 @@ def remove_properties(properties)
q = "#{match_start} REMOVE " + properties.map do |k|
"n.`#{k}`"
end.join(', ')
@session._query_or_fail(q)
@session._query_or_fail(q, false, neo_id: neo_id)
end

# (see Neo4j::Node#update_props)
Expand All @@ -92,22 +92,19 @@ def update_props(properties)
remove_properties(removed_keys) unless removed_keys.empty?
properties_to_set = properties.keys - removed_keys
return if properties_to_set.empty?
@session._query_or_fail("#{match_start} SET #{cypher_properties(properties_to_set)}", false, cypher_prop_list(properties)[:props])
props_list = cypher_prop_list(properties)[:props].merge({ neo_id: neo_id })
@session._query_or_fail("#{match_start} SET #{cypher_properties(properties_to_set)}", false, props_list)
properties
end

# (see Neo4j::Node#get_property)
def get_property(key)
if @props
@props[key.to_sym]
else
@session._query_or_fail("#{match_start} RETURN n.`#{key}`", true)
end
@props ? @props[key.to_sym] : @session._query_or_fail("#{match_start} RETURN n.`#{key}`", true, neo_id: neo_id)
end

# (see Neo4j::Node#labels)
def labels
@labels ||= @session._query_or_fail("#{match_start} RETURN labels(n) as labels", true)
@labels ||= @session._query_or_fail("#{match_start} RETURN labels(n) as labels", true, neo_id: neo_id)
@labels.map(&:to_sym)
end

Expand All @@ -116,30 +113,29 @@ def _cypher_label_list(labels)
end

def add_label(*labels)
@session._query_or_fail("#{match_start} SET n #{_cypher_label_list(labels)}")
@session._query_or_fail("#{match_start} SET n #{_cypher_label_list(labels)}", false, neo_id: neo_id)
end

def remove_label(*labels)
@session._query_or_fail("#{match_start} REMOVE n #{_cypher_label_list(labels)}")
@session._query_or_fail("#{match_start} REMOVE n #{_cypher_label_list(labels)}", false, neo_id: neo_id)
end

def set_label(*label_names)
q = "#{match_start} #{remove_labels_if_needed} #{set_labels_if_needed(label_names)}"
@session._query_or_fail(q)
@session._query_or_fail(q, false, neo_id: neo_id)
end

# (see Neo4j::Node#del)
def del
@session._query_or_fail("#{match_start} MATCH n-[r]-() DELETE r")
@session._query_or_fail("#{match_start} DELETE n")
@session._query_or_fail("#{match_start} OPTIONAL MATCH n-[r]-() DELETE n, r", false, neo_id: neo_id)
end

alias_method :delete, :del
alias_method :destroy, :del

# (see Neo4j::Node#exist?)
def exist?
@session._query("#{match_start} RETURN ID(n)").data.empty? ? false : true
@session._query("#{match_start} RETURN ID(n)", neo_id: neo_id).data.empty? ? false : true
end

# (see Neo4j::Node#node)
Expand Down Expand Up @@ -178,7 +174,7 @@ def match(clazz, returns, match={})
between_id = match[:between] ? "MATCH (p) WHERE ID(p) = #{match[:between].neo_id}" : ""
dir_func = to_dir[match[:dir] || :both]
cypher = "#{match_start} #{between_id} MATCH (n)#{dir_func.call(cypher_rel)}(p) RETURN #{returns}"
r = @session._query(cypher)
r = @session._query(cypher, neo_id: neo_id)
r.raise_error if r.error?
_map_result(r)
end
Expand Down Expand Up @@ -216,7 +212,7 @@ def ensure_single_relationship(&block)
end

def match_start(identifier = 'n')
"MATCH (#{identifier}) WHERE ID(#{identifier}) = #{neo_id}"
"MATCH (#{identifier}) WHERE ID(#{identifier}) = {neo_id}"
end
end
end
20 changes: 10 additions & 10 deletions lib/neo4j-server/cypher_relationship.rb
Expand Up @@ -34,7 +34,7 @@ def inspect

def load_resource
if resource_data.nil? || resource_data.empty?
@resource_data = @session._query_or_fail("#{match_start} RETURN n", true) # r.first_data
@resource_data = @session._query_or_fail("#{match_start} RETURN n", true, neo_id: neo_id) # r.first_data
end
end

Expand Down Expand Up @@ -69,30 +69,30 @@ def get_node_id(direction)
end

def get_property(key)
@session._query_or_fail("#{match_start} RETURN n.`#{key}`", true)
@session._query_or_fail("#{match_start} RETURN n.`#{key}`", true, neo_id: neo_id )
end

def set_property(key,value)
@session._query_or_fail("#{match_start} SET n.`#{key}` = {value}", false, {value: value})
@session._query_or_fail("#{match_start} SET n.`#{key}` = {value}", false, { value: value, neo_id: neo_id })
end

def remove_property(key)
@session._query_or_fail("#{match_start} REMOVE n.`#{key}`")
@session._query_or_fail("#{match_start} REMOVE n.`#{key}`", false, neo_id: neo_id)
end

# (see Neo4j::Relationship#props)
def props
if @props
@props
else
hash = @session._query_entity_data("#{match_start} RETURN n")
hash = @session._query_entity_data("#{match_start} RETURN n", nil, neo_id: neo_id)
@props = Hash[hash['data'].map{ |k, v| [k.to_sym, v] }]
end
end

# (see Neo4j::Relationship#props=)
def props=(properties)
@session._query_or_fail("#{match_start} SET n = { props }", false, {props: properties})
@session._query_or_fail("#{match_start} SET n = { props }", false, { props: properties, neo_id: neo_id })
properties
end

Expand All @@ -102,7 +102,7 @@ def update_props(properties)
q = "#{match_start} SET " + properties.keys.map do |k|
"n.`#{k}`= #{escape_value(properties[k])}"
end.join(',')
@session._query_or_fail(q)
@session._query_or_fail(q, false, neo_id: neo_id)
properties
end

Expand All @@ -111,21 +111,21 @@ def rel_type
end

def del
@session._query("#{match_start} DELETE n").raise_unless_response_code(200)
@session._query("#{match_start} DELETE n", neo_id: neo_id).raise_unless_response_code(200)
end
alias_method :delete, :del
alias_method :destroy, :del

def exist?
response = @session._query("#{match_start} RETURN n")
response = @session._query("#{match_start} RETURN n", neo_id: neo_id)
# binding.pry
(response.data.nil? || response.data.empty?) ? false : true
end

private

def match_start(identifier = 'n')
"MATCH (node)-[#{identifier}]-() WHERE ID(#{identifier}) = #{neo_id}"
"MATCH (node)-[#{identifier}]-() WHERE ID(#{identifier}) = {neo_id}"
end
end
end
4 changes: 2 additions & 2 deletions lib/neo4j-server/cypher_session.rb
Expand Up @@ -196,8 +196,8 @@ def _query_or_fail(q, single_row = false, params=nil)
single_row ? response.first_data : response
end

def _query_entity_data(q, id=nil)
response = _query(q)
def _query_entity_data(q, id = nil, params = nil)
response = _query(q, params)
response.raise_error if response.error?
response.entity_data(id)
end
Expand Down
14 changes: 7 additions & 7 deletions spec/neo4j-server/unit/cypher_node_unit_spec.rb
Expand Up @@ -12,7 +12,7 @@ module Neo4j::Server
double('connection')
end

let(:match_string) { "MATCH (n) WHERE ID(n) = 42" }
let(:match_string) { "MATCH (n) WHERE ID(n) = {neo_id}" }

describe 'instance methods' do

Expand Down Expand Up @@ -51,15 +51,15 @@ module Neo4j::Server

it "returns all properties" do
node = CypherNode.new(session, 42)
expect(session).to receive(:_query_entity_data).with("#{match_string} RETURN n").and_return({'data'=> {'name'=>'andreas'}})
expect(session).to receive(:_query_entity_data).with("#{match_string} RETURN n", nil, neo_id: 42).and_return({'data'=> {'name'=>'andreas'}})
expect(node.props).to eq({name: 'andreas'})
end
end

describe 'exist?' do
it "generates correct cypher" do
cypher_response = double("cypher response", error?: false)
expect(session).to receive(:_query).with("#{match_string} RETURN ID(n)").and_return(cypher_response)
expect(session).to receive(:_query).with("#{match_string} RETURN ID(n)", neo_id: 42).and_return(cypher_response)
node = CypherNode.new(session, 42)
node.init_resource_data('data', 'http://bla/42')
expect(cypher_response).to receive(:data).and_return([])
Expand All @@ -82,14 +82,14 @@ module Neo4j::Server
it 'generates correct cypher' do
node = CypherNode.new(session, 42)
response = double("cypher response", error?: false)
expect(session).to receive(:_query).with("#{match_string} SET n.`name` = { value }", { value: 'andreas' }).and_return(response)
expect(session).to receive(:_query).with("#{match_string} SET n.`name` = { value }", { value: 'andreas', neo_id: node.neo_id }).and_return(response)
node['name'] = 'andreas'
end

it 'removed property if setting it to a nil value' do
node = CypherNode.new(session, 42)
response = double("cypher response", error?: false)
expect(session).to receive(:_query).with("#{match_string} REMOVE n.`name`",nil).and_return(response)
expect(session).to receive(:_query).with("#{match_string} REMOVE n.`name`", neo_id: 42).and_return(response)
node['name'] = nil
end
end
Expand All @@ -98,11 +98,11 @@ module Neo4j::Server
it 'generates correct cypher' do
node = CypherNode.new(session, 42)
response = double('cypher response',first_data: 'andreas', error?: false)
expect(session).to receive(:_query).with("#{match_string} RETURN n.`name`",nil).and_return(response)
expect(session).to receive(:_query).with("#{match_string} RETURN n.`name`", neo_id: 42).and_return(response)
expect(node['name']).to eq('andreas')
end
end

end
end
end
end