Class: Hotdog::Expression::FuncallNode

Inherits:
ExpressionNode show all
Defined in:
lib/hotdog/expression/semantics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ExpressionNode

#==, #compact

Constructor Details

#initialize(function, args, options = {}) ⇒ FuncallNode

Returns a new instance of FuncallNode.



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/hotdog/expression/semantics.rb', line 498

def initialize(function, args, options={})
  # FIXME: smart argument handling (e.g. arity & type checking)
  case function.to_s
  when "HEAD", "head"
    @function = :HEAD
  when "GROUP_BY", "group_by"
    @function = :GROUP_BY
  when "LIMIT", "limit"
    @function = :HEAD
  when "ORDER_BY", "order_by"
    @function = :ORDER_BY
  when "REVERSE", "reverse"
    @function = :REVERSE
  when "SAMPLE", "sample"
    @function = :HEAD
    args[0] = FuncallNode.new("SHUFFLE", [args[0]])
  when "SHUFFLE", "shuffle"
    @function = :SHUFFLE
  when "SLICE", "slice"
    @function = :SLICE
  when "SORT", "sort"
    @function = :ORDER_BY
  when "TAIL", "tail"
    @function = :TAIL
  else
    raise(SyntaxError.new("unknown function call: #{function}"))
  end
  @args = args
  @options = options
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



496
497
498
# File 'lib/hotdog/expression/semantics.rb', line 496

def args
  @args
end

#functionObject (readonly)

Returns the value of attribute function.



496
497
498
# File 'lib/hotdog/expression/semantics.rb', line 496

def function
  @function
end

Instance Method Details

#dump(options = {}) ⇒ Object



529
530
531
532
533
534
535
536
537
538
# File 'lib/hotdog/expression/semantics.rb', line 529

def dump(options={})
  args = @args.map { |arg|
    if ExpressionNode === arg
      arg.dump(options)
    else
      arg
    end
  }
  {funcall: @function.to_s, args: args}
end

#evaluate(environment, options = {}) ⇒ Object



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/hotdog/expression/semantics.rb', line 575

def evaluate(environment, options={})
  case function
  when :HEAD
    args[0].evaluate(environment, options).take(args[1] || 1)
  when :GROUP_BY
    intermediate = args[0].evaluate(environment, options)
    q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
          "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
          "WHERE tags.name = ? AND hosts_tags.host_id IN (%s) " \
          "GROUP BY tags.value;" % intermediate.map { "?" }.join(", ")
    QueryExpressionNode.new(q, [args[1]] + intermediate).evaluate(environment, options)
  when :ORDER_BY
    intermediate = args[0].evaluate(environment, options)
    if args[1]
      q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
            "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
            "WHERE tags.name = ? AND hosts_tags.host_id IN (%s) " \
            "ORDER BY tags.value;" % intermediate.map { "?" }.join(", ")
      QueryExpressionNode.new(q, [args[1]] + intermediate).evaluate(environment, options)
    else
      q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
            "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
            "WHERE hosts_tags.host_id IN (%s) " \
            "ORDER BY hosts_tags.host_id;" % intermediate.map { "?" }.join(", ")
      QueryExpressionNode.new(q, intermediate).evaluate(environment, options)
    end
  when :REVERSE
    args[0].evaluate(environment, options).reverse()
  when :SHUFFLE
    args[0].evaluate(environment, options).shuffle()
  when :SLICE
    args[0].evaluate(environment, options).slice(args[1], args[2] || 1)
  when :TAIL
    args[0].evaluate(environment, options).last(args[1] || 1)
  else
    []
  end
end

#optimize(options = {}) ⇒ Object



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/hotdog/expression/semantics.rb', line 540

def optimize(options={})
  case function
  when :GROUP_BY
    o_args = [@args[0].optimize(options)]
    if TagExpressionNode === args[1]
      # workaround for expressions like `ORDER_BY((environment:development),role)`
      o_args << @args[1].tagname
    else
      o_args << @args[1]
    end
  when :ORDER_BY
    o_args = [@args[0].optimize(options)]
    if @args[1]
      if TagExpressionNode === @args[1]
        # workaround for expressions like `ORDER_BY((environment:development),role)`
        o_args << @args[1].tagname
      else
        o_args << @args[1]
      end
    end
  else
    o_args = @args.map { |arg|
      if ExpressionNode === arg
        arg.optimize(options)
      else
        arg
      end
    }
  end
  FuncallNode.new(
    @function,
    o_args,
  )
end