Module: Neo4j::Core::CypherTranslator

Constant Summary collapse

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

.sanitized_column_names(response_body) ⇒ Object



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

.translate_response(response_body, result) ⇒ Object



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

#create_escape_value(value) ⇒ Object

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

#cypher_prop_list(props) ⇒ Object

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

#cypher_string(labels, props) ⇒ Object



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

#escape_quotes(s) ⇒ Object



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

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

#escape_value(value) ⇒ Object

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

#label_string(labels) ⇒ Object



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

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

#prop_identifier(props) ⇒ Object



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

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

#sanitize_escape_sequences(s) ⇒ Object



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