Class: Cyrel::Clause::With
Overview
Represents a WITH clause in a Cypher query. Used to project results from one part of a query to the next.
Defined Under Namespace
Classes: RawExpressionString
Instance Attribute Summary collapse
-
#distinct ⇒ Object
readonly
Allow optional WHERE after WITH.
-
#items ⇒ Object
readonly
Allow optional WHERE after WITH.
-
#where ⇒ Object
readonly
Allow optional WHERE after WITH.
Instance Method Summary collapse
-
#initialize(*items, distinct: false, where: nil) ⇒ With
constructor
Initializes a WITH clause.
-
#render(query) ⇒ String
Renders the WITH clause, including an optional subsequent WHERE.
Constructor Details
#initialize(*items, distinct: false, where: nil) ⇒ With
Initializes a WITH clause.
16 17 18 19 20 21 22 23 24 |
# File 'lib/cyrel/clause/with.rb', line 16 def initialize(*items, distinct: false, where: nil) @items = process_items(items.flatten) @distinct = distinct @where = where # Store the Where clause instance directly raise ArgumentError, 'WITH clause requires at least one item.' if @items.empty? return if where.nil? || where.is_a?(Cyrel::Clause::Where) raise ArgumentError, 'WHERE clause for WITH must be a Cyrel::Clause::Where instance.' end |
Instance Attribute Details
#distinct ⇒ Object (readonly)
Allow optional WHERE after WITH
8 9 10 |
# File 'lib/cyrel/clause/with.rb', line 8 def distinct @distinct end |
#items ⇒ Object (readonly)
Allow optional WHERE after WITH
8 9 10 |
# File 'lib/cyrel/clause/with.rb', line 8 def items @items end |
#where ⇒ Object (readonly)
Allow optional WHERE after WITH
8 9 10 |
# File 'lib/cyrel/clause/with.rb', line 8 def where @where end |
Instance Method Details
#render(query) ⇒ String
Renders the WITH clause, including an optional subsequent WHERE.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cyrel/clause/with.rb', line 29 def render(query) distinct_str = @distinct ? 'DISTINCT ' : '' # Need to handle aliases (AS keyword) properly here. # The simple RawIdentifier might not be enough if we need parsing. # Let's assume for now items can be strings like "n.name AS name". rendered_items = @items.map { |item| render_item(item, query) }.join(', ') with_part = "WITH #{distinct_str}#{rendered_items}" where_part = @where ? "\n#{@where.render(query)}" : '' # Render WHERE on new line "#{with_part}#{where_part}" end |