Skip to content

Unexpected behavior: #union_cypher returns String object rather than Query object #308

@jorroll

Description

@jorroll

While attempting to create a query involving the union of three sub-queries, I discovered that union_cypher expects a query object but returns a string rather than another query object. This prevents me from performing a union query on a union query. Given that all the other query methods return a query object, is there a reason for this behavior? I guess it kinda explains the name union_cypher

# @example
# # Generates cypher: MATCH (n:Person) UNION MATCH (o:Person) WHERE o.age = 10
# q = Neo4j::Core::Query.new.match(o: :Person).where(o: {age: 10})
# result = Neo4j::Core::Query.new.match(n: :Person).union_cypher(q)
#
# @param other [Query] Second half of UNION
# @param options [Hash] Specify {all: true} to use UNION ALL
# @return [String] Resulting UNION cypher query string
def union_cypher(other, options = {})
"#{to_cypher} UNION#{options[:all] ? ' ALL' : ''} #{other.to_cypher}"
end

How would you feel about a PR to either create a new query method union that returns another query object for further chaining?

Alternatively (and easier to implement), union_cypher could be updated so that it tests to see if its argument is already a string and, if so, simply inserts it without first calling to_cypher on the argument.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions