Class: WindowExpression

Inherits:
Expression show all
Defined in:
lib/eno/expressions.rb

Constant Summary collapse

S_UNBOUNDED =
'between unbounded preceding and unbounded following'
S_WINDOW =
'(%s)'
S_PARTITION_BY =
'partition by %s '
S_ORDER_BY =
'order by %s '
S_RANGE =
'range %s '

Constants inherited from Expression

Expression::S_AND, Expression::S_DIV, Expression::S_EQ, Expression::S_GT, Expression::S_GTE, Expression::S_LT, Expression::S_LTE, Expression::S_MINUS, Expression::S_MOD, Expression::S_MUL, Expression::S_NEQ, Expression::S_OR, Expression::S_PLUS, Expression::S_TILDE

Instance Attribute Summary

Attributes inherited from Expression

#members, #props

Instance Method Summary collapse

Methods inherited from Expression

#!=, #!@, #%, #&, #*, #+, #-, #/, #<, #<=, #==, #=~, #>, #>=, #^, #as, #cast, #desc, #in, #inner_join, #join, #not_in, #not_null?, #null?, #over, #|

Constructor Details

#initialize(&block) ⇒ WindowExpression

Returns a new instance of WindowExpression.



399
400
401
# File 'lib/eno/expressions.rb', line 399

def initialize(&block)
  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym) ⇒ Object



444
445
446
447
# File 'lib/eno/expressions.rb', line 444

def method_missing(sym)
  super if sym == :to_hash
  Identifier.new(sym)
end

Instance Method Details

#_order_by_clause(sql) ⇒ Object



434
435
436
437
# File 'lib/eno/expressions.rb', line 434

def _order_by_clause(sql)
  return nil unless @order_by
  S_ORDER_BY % @order_by.map { |e| sql.quote(e) }.join(S_COMMA)
end

#_partition_by_clause(sql) ⇒ Object



429
430
431
432
# File 'lib/eno/expressions.rb', line 429

def _partition_by_clause(sql)
  return nil unless @partition_by
  S_PARTITION_BY % @partition_by.map { |e| sql.quote(e) }.join(S_COMMA)
end

#_range_clause(sql) ⇒ Object



439
440
441
442
# File 'lib/eno/expressions.rb', line 439

def _range_clause(sql)
  return nil unless @range
  S_RANGE % @range
end

#order_by(*args) ⇒ Object



407
408
409
# File 'lib/eno/expressions.rb', line 407

def order_by(*args)
  @order_by = args
end

#partition_by(*args) ⇒ Object



403
404
405
# File 'lib/eno/expressions.rb', line 403

def partition_by(*args)
  @partition_by = args
end

#range_unboundedObject



417
418
419
# File 'lib/eno/expressions.rb', line 417

def range_unbounded
  @range = S_UNBOUNDED
end

#to_sql(sql) ⇒ Object



421
422
423
424
425
426
427
# File 'lib/eno/expressions.rb', line 421

def to_sql(sql)
  S_WINDOW % [
    _partition_by_clause(sql),
    _order_by_clause(sql),
    _range_clause(sql)
  ].join.strip
end