Class: Neo4j::Core::Query
- Inherits:
-
Object
- Object
- Neo4j::Core::Query
- Includes:
- Enumerable, QueryClauses, QueryFindInBatches
- Defined in:
- lib/neo4j-core/query.rb
Overview
Allows for generation of cypher queries via ruby method calls (inspired by ActiveRecord / arel syntax)
Can be used to express cypher queries in ruby nicely, or to more easily generate queries programatically.
Also, queries can be passed around an application to progressively build a query across different concerns
See also the following link for full cypher language documentation: docs.neo4j.org/chunked/milestone/cypher-query-lang.html
Defined Under Namespace
Classes: PartitionedClauses
Constant Summary collapse
- DEFINED_CLAUSES =
{}
- METHODS =
DELETE clause
%w(start match optional_match using where create create_unique merge set on_create_set on_match_set remove unwind delete with return order skip limit)
- BREAK_METHODS =
%(with)
- CLAUSIFY_CLAUSE =
proc do |method| const_get(method.to_s.split('_').map(&:capitalize).join + 'Clause') end
- CLAUSES =
METHODS.map(&CLAUSIFY_CLAUSE)
- EMPTY =
Returns a CYPHER query string from the object query representation
' '
- NEWLINE =
"\n"
- MEMOIZED_INSTANCE_VARIABLES =
[:response, :merge_params]
Class Attribute Summary collapse
-
.pretty_cypher ⇒ Object
Returns the value of attribute pretty_cypher.
Instance Attribute Summary collapse
-
#clauses ⇒ Object
Returns the value of attribute clauses.
Instance Method Summary collapse
- #&(other) ⇒ Object
-
#break ⇒ Object
Allows what’s been built of the query so far to be frozen and the rest built anew.
- #clause?(method) ⇒ Boolean
- #copy ⇒ Object
- #count(var = nil) ⇒ Object
-
#create(*args) ⇒ Query
CREATE clause.
-
#create_unique(*args) ⇒ Query
CREATE UNIQUE clause.
-
#delete(*args) ⇒ Query
DELETE clause.
- #each ⇒ Object
-
#exec ⇒ Boolean
Executes a query without returning the result.
-
#initialize(options = {}) ⇒ Query
constructor
A new instance of Query.
- #inspect ⇒ Object
-
#limit(*args) ⇒ Query
LIMIT clause.
-
#match(*args) ⇒ Query
MATCH clause.
- #match_nodes(hash, optional_match = false) ⇒ Object
-
#merge(*args) ⇒ Query
MERGE clause.
-
#on_create_set(*args) ⇒ Query
ON CREATE SET clause.
-
#on_match_set(*args) ⇒ Query
ON MATCH SET clause.
-
#optional_match(*args) ⇒ Query
OPTIONAL MATCH clause.
- #optional_match_nodes(hash) ⇒ Object
-
#order(*args) ⇒ Query
(also: #order_by)
ORDER BY clause.
-
#params(args) ⇒ Object
Allows for the specification of values for params specified in query.
-
#pluck(*columns) ⇒ Object
Return the specified columns as an array.
- #print_cypher ⇒ Object
-
#remove(*args) ⇒ Query
REMOVE clause.
-
#reorder(*args) ⇒ Object
Clears out previous order clauses and allows only for those specified by args.
- #response ⇒ Object
-
#return(*args) ⇒ Query
RETURN clause.
- #return_query(columns) ⇒ Object
-
#set(*args) ⇒ Query
SET clause.
-
#set_props(*args) ⇒ Object
Works the same as the #set method, but when given a nested array it will set properties rather than setting entire objects.
-
#skip(*args) ⇒ Query
(also: #offset)
SKIP clause.
-
#start(*args) ⇒ Query
START clause.
-
#to_a ⇒ Array
Class is Enumerable.
- #to_cypher(options = {}) ⇒ Object
-
#union_cypher(other, options = {}) ⇒ String
Returns a CYPHER query specifying the union of the callee object’s query and the argument’s query.
-
#unwind(*args) ⇒ Query
UNWIND clause.
- #unwrapped ⇒ Object
- #unwrapped? ⇒ Boolean
-
#using(*args) ⇒ Query
USING clause.
-
#where(*args) ⇒ Query
WHERE clause.
-
#where_not(*args) ⇒ Object
Works the same as the #where method, but the clause is surrounded by a Cypher NOT() function.
-
#with(*args) ⇒ Query
WITH clause.
Methods included from QueryFindInBatches
Constructor Details
Class Attribute Details
.pretty_cypher ⇒ Object
Returns the value of attribute pretty_cypher.
22 23 24 |
# File 'lib/neo4j-core/query.rb', line 22 def pretty_cypher @pretty_cypher end |
Instance Attribute Details
#clauses ⇒ Object
Returns the value of attribute clauses.
19 20 21 |
# File 'lib/neo4j-core/query.rb', line 19 def clauses @clauses end |
Instance Method Details
#&(other) ⇒ Object
326 327 328 329 330 331 332 333 |
# File 'lib/neo4j-core/query.rb', line 326 def &(other) fail "Sessions don't match!" if @session != other.session self.class.new(session: @session).tap do |new_query| new_query. = .merge(other.) new_query.clauses = clauses + other.clauses end.params(other._params) end |
#break ⇒ Object
Allows what’s been built of the query so far to be frozen and the rest built anew. Can be called multiple times in a string of method calls
162 163 164 |
# File 'lib/neo4j-core/query.rb', line 162 def break build_deeper_query(nil) end |
#clause?(method) ⇒ Boolean
344 345 346 347 348 349 |
# File 'lib/neo4j-core/query.rb', line 344 def clause?(method) clause_class = DEFINED_CLAUSES[method] || CLAUSIFY_CLAUSE.call(method) clauses.any? do |clause| clause.is_a?(clause_class) end end |
#copy ⇒ Object
336 337 338 339 340 341 342 |
# File 'lib/neo4j-core/query.rb', line 336 def copy dup.tap do |query| MEMOIZED_INSTANCE_VARIABLES.each do |var| query.instance_variable_set("@#{var}", nil) end end end |
#count(var = nil) ⇒ Object
220 221 222 223 |
# File 'lib/neo4j-core/query.rb', line 220 def count(var = nil) v = var.nil? ? '*' : var pluck("count(#{v})").first end |
#delete(*args) ⇒ Query
DELETE clause
113 |
# File 'lib/neo4j-core/query.rb', line 113 METHODS = %w(start match optional_match using where create create_unique merge set on_create_set on_match_set remove unwind delete with return order skip limit) |
#each ⇒ Object
225 226 227 228 229 230 231 232 233 |
# File 'lib/neo4j-core/query.rb', line 225 def each response = self.response if response.is_a?(Neo4j::Server::CypherResponse) response.unwrapped! if unwrapped? response.to_node_enumeration else Neo4j::Embedded::ResultWrapper.new(response, to_cypher, unwrapped?) end.each { |object| yield object } end |
#exec ⇒ Boolean
Executes a query without returning the result
244 245 246 247 248 |
# File 'lib/neo4j-core/query.rb', line 244 def exec response true end |
#inspect ⇒ Object
33 34 35 |
# File 'lib/neo4j-core/query.rb', line 33 def inspect "#<Query CYPHER: #{ANSI::YELLOW}#{to_cypher.inspect}#{ANSI::CLEAR}>" end |
#match_nodes(hash, optional_match = false) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/neo4j-core/query.rb', line 202 def match_nodes(hash, optional_match = false) hash.inject(self) do |query, (variable, node_object)| neo_id = if node_object.respond_to?(:neo_id) node_object.neo_id else node_object end match_method = optional_match ? :optional_match : :match query.send(match_method, variable).where(variable => {neo_id: neo_id}) end end |
#optional_match_nodes(hash) ⇒ Object
214 215 216 |
# File 'lib/neo4j-core/query.rb', line 214 def optional_match_nodes(hash) match_nodes(hash, true) end |
#order(*args) ⇒ Query Also known as: order_by
ORDER BY clause
|
# File 'lib/neo4j-core/query.rb', line 61
|
#params(args) ⇒ Object
Allows for the specification of values for params specified in query
173 174 175 176 177 |
# File 'lib/neo4j-core/query.rb', line 173 def params(args) @_params = @_params.merge(args) self end |
#pluck(*columns) ⇒ Object
Return the specified columns as an array. If one column is specified, a one-dimensional array is returned with the values of that column If two columns are specified, a n-dimensional array is returned with the values of those columns
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/neo4j-core/query.rb', line 259 def pluck(*columns) fail ArgumentError, 'No columns specified for Query#pluck' if columns.size.zero? query = return_query(columns) columns = query.response.columns case columns.size when 1 column = columns[0] query.map { |row| row[column] } else query.map do |row| columns.map do |column| row[column] end end end end |
#print_cypher ⇒ Object
308 309 310 |
# File 'lib/neo4j-core/query.rb', line 308 def print_cypher puts to_cypher(pretty: true).gsub(/\e[^m]+m/, '') end |
#reorder(*args) ⇒ Object
Clears out previous order clauses and allows only for those specified by args
137 138 139 140 141 142 |
# File 'lib/neo4j-core/query.rb', line 137 def reorder(*args) query = copy query.remove_clause_class(OrderClause) query.order(*args) end |
#response ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/neo4j-core/query.rb', line 188 def response return @response if @response cypher = to_cypher pretty_cypher = to_cypher(pretty: true) if self.class.pretty_cypher @response = @session._query(cypher, merge_params, context: @options[:context], pretty_cypher: pretty_cypher) if !response.respond_to?(:error?) || !response.error? response else response.raise_cypher_error end end |
#return_query(columns) ⇒ Object
278 279 280 281 282 283 |
# File 'lib/neo4j-core/query.rb', line 278 def return_query(columns) query = copy query.remove_clause_class(ReturnClause) query.return(*columns) end |
#set_props(*args) ⇒ Object
Works the same as the #set method, but when given a nested array it will set properties rather than setting entire objects
154 155 156 |
# File 'lib/neo4j-core/query.rb', line 154 def set_props(*args) build_deeper_query(SetClause, args, set_props: true) end |
#to_a ⇒ Array
Class is Enumerable. Each yield is a Hash with the key matching the variable returned and the value being the value for that key from the response
|
# File 'lib/neo4j-core/query.rb', line 235
|
#to_cypher(options = {}) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/neo4j-core/query.rb', line 292 def to_cypher( = {}) cypher_string = PartitionedClauses.new(@clauses).map do |clauses| clauses_by_class = clauses.group_by(&:class) cypher_parts = CLAUSES.map do |clause_class| clause_class.to_cypher(clauses, ) if clauses = clauses_by_class[clause_class] end cypher_parts.compact! cypher_parts.join([:pretty] ? NEWLINE : EMPTY).tap(&:strip!) end.join([:pretty] ? NEWLINE : EMPTY) cypher_string = "CYPHER #{@options[:parser]} #{cypher_string}" if @options[:parser] cypher_string.tap(&:strip!) end |
#union_cypher(other, options = {}) ⇒ String
Returns a CYPHER query specifying the union of the callee object’s query and the argument’s query
322 323 324 |
# File 'lib/neo4j-core/query.rb', line 322 def union_cypher(other, = {}) "#{to_cypher} UNION#{[:all] ? ' ALL' : ''} #{other.to_cypher}" end |
#unwrapped ⇒ Object
179 180 181 182 |
# File 'lib/neo4j-core/query.rb', line 179 def unwrapped @_unwrapped_obj = true self end |
#unwrapped? ⇒ Boolean
184 185 186 |
# File 'lib/neo4j-core/query.rb', line 184 def unwrapped? !!@_unwrapped_obj end |
#where_not(*args) ⇒ Object
Works the same as the #where method, but the clause is surrounded by a Cypher NOT() function
146 147 148 |
# File 'lib/neo4j-core/query.rb', line 146 def where_not(*args) build_deeper_query(WhereClause, args, not: true) end |