Class: Cyrel::Expression::FunctionCall

Inherits:
Base
  • Object
show all
Defined in:
lib/cyrel/expression/function_call.rb

Overview

Represents a function call in Cypher (e.g., id(n), count(*), coalesce(a, b)).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#!=, #%, #&, #*, #+, #-, #/, #<, #<=, #==, #=~, #>, #>=, #^, #|

Constructor Details

#initialize(function_name, arguments = [], distinct: false) ⇒ FunctionCall



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cyrel/expression/function_call.rb', line 12

def initialize(function_name, arguments = [], distinct: false)
  @function_name = function_name.to_s # Store as string for consistency
  @arguments = Array(arguments).map do |arg|
    # Don't coerce ASTERISK or existing Expressions
    if arg == Functions::ASTERISK || arg.is_a?(Expression::Base)
      arg
    else
      Expression.coerce(arg) # Coerce only non-expression literals
    end
  end
  @distinct = distinct
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



7
8
9
# File 'lib/cyrel/expression/function_call.rb', line 7

def arguments
  @arguments
end

#distinctObject (readonly)

Returns the value of attribute distinct.



7
8
9
# File 'lib/cyrel/expression/function_call.rb', line 7

def distinct
  @distinct
end

#function_nameObject (readonly)

Returns the value of attribute function_name.



7
8
9
# File 'lib/cyrel/expression/function_call.rb', line 7

def function_name
  @function_name
end

Instance Method Details

#as(alias_name) ⇒ Cyrel::Expression::Alias

Creates an aliased version of this function call expression. Duplicates method from Base for robustness.



52
53
54
# File 'lib/cyrel/expression/function_call.rb', line 52

def as(alias_name)
  Alias.new(self, alias_name)
end

#render(query) ⇒ String

Renders the function call expression.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cyrel/expression/function_call.rb', line 28

def render(query)
  rendered_args = @arguments.map do |arg|
    case arg
    when Functions::ASTERISK
      '*'
    # Special handling for RawIdentifier when used as argument
    when Clause::Return::RawIdentifier
      arg.identifier # Render the raw identifier directly
    when Expression::Base, ->(a) { a.respond_to?(:render) } # Check if it's an Expression or renderable
      arg.render(query) # Render other expressions normally
    else
      # Parameterize other literal values
      param_key = query.register_parameter(arg)
      "$#{param_key}"
    end
  end.join(', ')
  distinct_str = @distinct ? 'DISTINCT ' : ''
  "#{@function_name}(#{distinct_str}#{rendered_args})"
end