Module: Neo4j::Core::CypherTranslator

Included in:
QueryClauses::Clause, Label, Server::CypherNode, Server::CypherRelationship, Server::CypherSession, Server::CypherTransaction
Defined in:
lib/neo4j-core/cypher_translator.rb

Constant Summary

SANITIZE_ESCAPED_REGEXP =

Only following escape sequence characters are allowed in Cypher:

t Tab b Backspace n Newline r Carriage return f Form feed ' Single quote " Double quote \ Backslash

From: docs.neo4j.org/chunked/stable/cypher-expressions.html#_note_on_string_literals

/(?<!\\)\\(\\\\)*(?![futbnr'"\\])/
EMPTY_PROPS =
''

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) sanitized_column_names(response_body)



59
60
61
# File 'lib/neo4j-core/cypher_translator.rb', line 59

def self.sanitized_column_names(response_body)
  response_body.columns.map { |column| column[/[^\.]+$/] }
end

+ (Object) translate_response(response_body, result)



55
56
57
# File 'lib/neo4j-core/cypher_translator.rb', line 55

def self.translate_response(response_body, result)
  Hashie::Mash.new(Hash[sanitized_column_names(response_body).zip(result)])
end

Instance Method Details

- (Object) create_escape_value(value)

Like escape_value but it does not wrap the value in quotes



14
15
16
17
18
19
20
# File 'lib/neo4j-core/cypher_translator.rb', line 14

def create_escape_value(value)
  if value.is_a?(String) || value.is_a?(Symbol)
    "#{sanitize_escape_sequences(value.to_s)}"
  else
    value
  end
end

- (Object) cypher_prop_list(props)

Cypher Helper



47
48
49
50
51
# File 'lib/neo4j-core/cypher_translator.rb', line 47

def cypher_prop_list(props)
  return nil unless props
  props.reject! { |_, v| v.nil? }
  {props: props.each { |k, v|  props[k] = create_escape_value(v) }}
end

- (Object) cypher_string(labels, props)



63
64
65
# File 'lib/neo4j-core/cypher_translator.rb', line 63

def cypher_string(labels, props)
  "CREATE (n#{label_string(labels)} #{prop_identifier(props)}) RETURN ID(n)"
end

- (Object) escape_quotes(s)



42
43
44
# File 'lib/neo4j-core/cypher_translator.rb', line 42

def escape_quotes(s)
  s.gsub("'", %q(\\\'))
end

- (Object) escape_value(value)

Cypher Helper



5
6
7
8
9
10
11
# File 'lib/neo4j-core/cypher_translator.rb', line 5

def escape_value(value)
  if value.is_a?(String) || value.is_a?(Symbol)
    "'#{escape_quotes(sanitize_escape_sequences(value.to_s))}'"
  else
    value
  end
end

- (Object) label_string(labels)



67
68
69
# File 'lib/neo4j-core/cypher_translator.rb', line 67

def label_string(labels)
  labels.empty? ? '' : ":#{labels.map { |k| "`#{k}`" }.join(':')}"
end

- (Object) prop_identifier(props)



71
72
73
# File 'lib/neo4j-core/cypher_translator.rb', line 71

def prop_identifier(props)
  '{props}' unless props.nil?
end

- (Object) sanitize_escape_sequences(s)



38
39
40
# File 'lib/neo4j-core/cypher_translator.rb', line 38

def sanitize_escape_sequences(s)
  s.gsub SANITIZE_ESCAPED_REGEXP, EMPTY_PROPS
end