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 = {}) ⇒ OrderedExpression

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

:nulls

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



1296
1297
1298
# File 'lib/sequel/sql.rb', line 1296

def initialize(expression, descending = true, 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



1287
1288
1289
# File 'lib/sequel/sql.rb', line 1287

def descending
  @descending
end

#expressionObject (readonly)

The expression to order the result set by.



1284
1285
1286
# File 'lib/sequel/sql.rb', line 1284

def expression
  @expression
end

#nullsObject (readonly)

Whether to sort NULLS FIRST/LAST



1290
1291
1292
# File 'lib/sequel/sql.rb', line 1290

def nulls
  @nulls
end

Instance Method Details

#ascObject

Return a copy that is ordered ASC



1301
1302
1303
# File 'lib/sequel/sql.rb', line 1301

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

#descObject

Return a copy that is ordered DESC



1306
1307
1308
# File 'lib/sequel/sql.rb', line 1306

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

#invertObject

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



1311
1312
1313
# File 'lib/sequel/sql.rb', line 1311

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