Class: ApacheAge::Cypher

Inherits:
Object
  • Object
show all
Defined in:
lib/apache_age/cypher.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph_name = 'age_schema') ⇒ Cypher

Returns a new instance of Cypher.



7
8
9
10
11
# File 'lib/apache_age/cypher.rb', line 7

def initialize(graph_name = 'age_schema')
  @graph_name = graph_name
  @query = ""
  @as_type = "result agtype"
end

Class Attribute Details

.model_classObject

Returns the value of attribute model_class.



4
5
6
# File 'lib/apache_age/cypher.rb', line 4

def model_class
  @model_class
end

Instance Method Details

#as(type) ⇒ Object



81
82
83
84
# File 'lib/apache_age/cypher.rb', line 81

def as(type)
  @as_type = type
  self
end

#create(node) ⇒ Object



46
47
48
49
# File 'lib/apache_age/cypher.rb', line 46

def create(node)
  @query += "CREATE #{node} "
  self
end

#delete(entity) ⇒ Object



61
62
63
64
# File 'lib/apache_age/cypher.rb', line 61

def delete(entity)
  @query += "DELETE #{entity} "
  self
end

#executeObject



90
91
92
93
# File 'lib/apache_age/cypher.rb', line 90

def execute
  cypher_sql = to_cypher
  ActiveRecord::Base.connection.execute(cypher_sql)
end

#limit(count) ⇒ Object



76
77
78
79
# File 'lib/apache_age/cypher.rb', line 76

def limit(count)
  @query += "LIMIT #{count} "
  self
end

#match(pattern) ⇒ Object



13
14
15
16
# File 'lib/apache_age/cypher.rb', line 13

def match(pattern)
  @query += "MATCH #{pattern} "
  self
end

#merge(pattern) ⇒ Object



66
67
68
69
# File 'lib/apache_age/cypher.rb', line 66

def merge(pattern)
  @query += "MERGE #{pattern} "
  self
end

#order_by(*conditions) ⇒ Object

ORDER BY n.age DESC, n.name ASC



35
36
37
38
# File 'lib/apache_age/cypher.rb', line 35

def order_by(*conditions)
  @query += "ORDER BY #{variables.join(', ')} "
  self
end

#remove(property) ⇒ Object



56
57
58
59
# File 'lib/apache_age/cypher.rb', line 56

def remove(property)
  @query += "REMOVE #{property} "
  self
end

#return(*variables) ⇒ Object

can use full names n.name or aliases (with) name



41
42
43
44
# File 'lib/apache_age/cypher.rb', line 41

def return(*variables)
  @query += "RETURN #{variables.join(', ')} "
  self
end

#set(properties) ⇒ Object



51
52
53
54
# File 'lib/apache_age/cypher.rb', line 51

def set(properties)
  @query += "SET #{properties} "
  self
end

#skip(count) ⇒ Object



71
72
73
74
# File 'lib/apache_age/cypher.rb', line 71

def skip(count)
  @query += "SKIP #{count} "
  self
end

#to_cypherObject



86
87
88
# File 'lib/apache_age/cypher.rb', line 86

def to_cypher
  "SELECT * FROM cypher('#{@graph_name}', $$ #{@query.strip} $$) AS (#{@as_type});"
end

#where(*conditions) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/apache_age/cypher.rb', line 26

def where(*conditions)
  condition_str = conditions.join(' AND ')
  # @query += "WHERE #{condition_str} "
  # If there's already a WHERE clause in the query, append to it (they need to be adjacent!)
  @query += (@query.include?("WHERE") ? " AND #{condition_str} " : "WHERE #{condition_str} ")
  self
end

#with(*conditions) ⇒ Object

WITH n.name as name, n.age as age WITH otherPerson, count(*) AS foaf WHERE foaf > 1 with has a lot of cases - see docs



21
22
23
24
# File 'lib/apache_age/cypher.rb', line 21

def with(*conditions)
  @query += "WITH #{variables.join(', ')} "
  self
end