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, #eql?, #hash, inherited, #inspect, #lit, #sql_literal

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.



1409
1410
1411
# File 'lib/sequel/sql.rb', line 1409

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

Instance Attribute Details

#descendingObject (readonly)

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



1400
1401
1402
# File 'lib/sequel/sql.rb', line 1400

def descending
  @descending
end

#expressionObject (readonly)

The expression to order the result set by.



1397
1398
1399
# File 'lib/sequel/sql.rb', line 1397

def expression
  @expression
end

#nullsObject (readonly)

Whether to sort NULLS FIRST/LAST



1403
1404
1405
# File 'lib/sequel/sql.rb', line 1403

def nulls
  @nulls
end

Instance Method Details

#ascObject

Return a copy that is ordered ASC



1414
1415
1416
# File 'lib/sequel/sql.rb', line 1414

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

#descObject

Return a copy that is ordered DESC



1419
1420
1421
# File 'lib/sequel/sql.rb', line 1419

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

#invertObject

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



1424
1425
1426
# File 'lib/sequel/sql.rb', line 1424

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