Class: Sequel::SQL::OrderedExpression

Inherits:
Expression show all
Defined in:
lib/sequel/sql.rb,
lib/sequel/extensions/eval_inspect.rb

Overview

Represents a column/expression to order the result set by.

Constant Summary collapse

INVERT_NULLS =
{:first=>:last, :last=>:first}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#==, attr_reader, #clone, #eql?, #hash, inherited, #inspect

Constructor Details

#initialize(expression, descending = true, opts = OPTS) ⇒ OrderedExpression

Set the expression and descending attributes to the given values. Options:

:nulls

Can be :first/:last for NULLS FIRST/LAST.



1673
1674
1675
1676
1677
1678
# File 'lib/sequel/sql.rb', line 1673

def initialize(expression, descending = true, opts=OPTS)
  @expression = expression
  @descending = descending
  @nulls = opts[:nulls]
  freeze
end

Instance Attribute Details

#descendingObject (readonly)

Whether the expression should order the result set in a descending manner



1664
1665
1666
# File 'lib/sequel/sql.rb', line 1664

def descending
  @descending
end

#expressionObject (readonly)

The expression to order the result set by.



1661
1662
1663
# File 'lib/sequel/sql.rb', line 1661

def expression
  @expression
end

#nullsObject (readonly)

Whether to sort NULLS FIRST/LAST



1667
1668
1669
# File 'lib/sequel/sql.rb', line 1667

def nulls
  @nulls
end

Instance Method Details

#ascObject

Return a copy that is ordered ASC



1681
1682
1683
# File 'lib/sequel/sql.rb', line 1681

def asc
  OrderedExpression.new(@expression, false, :nulls=>@nulls)
end

#descObject

Return a copy that is ordered DESC



1686
1687
1688
# File 'lib/sequel/sql.rb', line 1686

def desc
  OrderedExpression.new(@expression, true, :nulls=>@nulls)
end

#invertObject

Return an inverted expression, changing ASC to DESC and NULLS FIRST to NULLS LAST.



1691
1692
1693
# File 'lib/sequel/sql.rb', line 1691

def invert
  OrderedExpression.new(@expression, !@descending, :nulls=>INVERT_NULLS.fetch(@nulls, @nulls))
end