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

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

- (OrderedExpression) initialize(expression, descending = true, opts = OPTS)

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

:nulls

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



1412
1413
1414
# File 'lib/sequel/sql.rb', line 1412

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

Instance Attribute Details

- (Object) descending (readonly)

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



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

def descending
  @descending
end

- (Object) expression (readonly)

The expression to order the result set by.



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

def expression
  @expression
end

- (Object) nulls (readonly)

Whether to sort NULLS FIRST/LAST



1406
1407
1408
# File 'lib/sequel/sql.rb', line 1406

def nulls
  @nulls
end

Instance Method Details

- (Object) asc

Return a copy that is ordered ASC



1417
1418
1419
# File 'lib/sequel/sql.rb', line 1417

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

- (Object) desc

Return a copy that is ordered DESC



1422
1423
1424
# File 'lib/sequel/sql.rb', line 1422

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

- (Object) invert

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



1427
1428
1429
# File 'lib/sequel/sql.rb', line 1427

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