Permalink
Browse files

Use a real instance variable for the :nulls option to OrderedExpression

Previously, it kept all options in a hash, but that's not the best
way to handle it.  Keep the options hash for the initialize, but
take the :nulls option out and store it in a separate instance
variable with it's own accessor for ease of use.
  • Loading branch information...
1 parent a68cb27 commit d1b7fc0221a31d0d1db8693aa3697cec7cb01dfe @jeremyevans committed Jun 29, 2010
Showing with 8 additions and 19 deletions.
  1. +2 −2 lib/sequel/dataset/sql.rb
  2. +6 −17 lib/sequel/sql.rb
@@ -354,7 +354,7 @@ def negative_boolean_constant_sql(constant)
# clause.
def ordered_expression_sql(oe)
s = "#{literal(oe.expression)} #{oe.descending ? 'DESC' : 'ASC'}"
- case oe.opts[:nulls]
+ case oe.nulls
when :first
"#{s} NULLS FIRST"
when :last
@@ -809,7 +809,7 @@ def qualified_expression(e, table)
when SQL::Identifier
SQL::QualifiedIdentifier.new(table, e)
when SQL::OrderedExpression
- SQL::OrderedExpression.new(qualified_expression(e.expression, table), e.descending)
+ SQL::OrderedExpression.new(qualified_expression(e.expression, table), e.descending, :nulls=>e.nulls)
when SQL::AliasedExpression
SQL::AliasedExpression.new(qualified_expression(e.expression, table), e.aliaz)
when SQL::CaseExpression
View
@@ -847,41 +847,30 @@ class OrderedExpression < Expression
# Whether the expression should order the result set in a descending manner
attr_reader :descending
- # Other options supported, currently :nulls
- attr_reader :opts
+ # Whether to sort NULLS FIRST/LAST
+ attr_reader :nulls
# Set the expression and descending attributes to the given values.
# Options:
#
# :nulls :: Can be :first/:last for NULLS FIRST/LAST.
def initialize(expression, descending = true, opts={})
- @expression, @descending, @opts = expression, descending, opts
+ @expression, @descending, @nulls = expression, descending, opts[:nulls]
end
# Return a copy that is ordered ASC
def asc
- OrderedExpression.new(@expression, false, @opts)
+ OrderedExpression.new(@expression, false, :nulls=>@nulls)
end
# Return a copy that is ordered DESC
def desc
- OrderedExpression.new(@expression, true, @opts)
- end
-
- # Make sure that the hash value is the same if the attributes are the same.
- # Necessary on 1.8.6 as Hash#hash is different for hashes that are
- # equal but distinct.
- def hash
- [self.class, expression, descending, opts[:nulls]].hash
+ OrderedExpression.new(@expression, true, :nulls=>@nulls)
end
# Return an inverted expression, changing ASC to DESC and NULLS FIRST to NULLS LAST.
def invert
- opts = @opts
- if nul_dir = @opts[:nulls]
- opts = opts.merge(:nulls=>INVERT_NULLS.fetch(nul_dir, nul_dir))
- end
- OrderedExpression.new(@expression, !@descending, opts)
+ OrderedExpression.new(@expression, !@descending, :nulls=>INVERT_NULLS.fetch(@nulls, @nulls))
end
to_s_method :ordered_expression_sql

0 comments on commit d1b7fc0

Please sign in to comment.