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'"\\])/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sanitized_column_names(response_body) ⇒ Object



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

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

.translate_response(response_body, result) ⇒ Object



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

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

Instance Method Details

#cypher_prop_list(props) ⇒ Object

Cypher Helper



38
39
40
41
42
43
44
45
# File 'lib/neo4j-core/cypher_translator.rb', line 38

def cypher_prop_list(props)
  return "" unless props

  properties_to_set = props.reject {|k,v| v.nil? }

  list = properties_to_set.map{|k,v| "#{k} : #{escape_value(v)}"}.join(',')
  "{#{list}}"
end

#escape_quotes(s) ⇒ Object



33
34
35
# File 'lib/neo4j-core/cypher_translator.rb', line 33

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

#escape_value(value) ⇒ Object

Cypher Helper



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

def escape_value(value)
  result = case value
    when String
      sanitized = sanitize_escape_sequences(value)
      "'#{escape_quotes(sanitized)}'"
    else
      value
  end
  result
end

#sanitize_escape_sequences(s) ⇒ Object



29
30
31
# File 'lib/neo4j-core/cypher_translator.rb', line 29

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