Skip to content

Commit

Permalink
Fix excessive NULL where clause generation
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
glebm committed Mar 21, 2018
1 parent a742995 commit 36c7fa8
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/order_query/sql/where.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ def where_tie(col)
# @param [:before or :after] side
# @return [query, params] return query fragment for column values
# before / after the current one.
def where_side(col, side, strict = true, value = point.value(col))
def where_side(col, side, strict, value = point.value(col))
if col.order_enum
where_in col, col.enum_side(value, side, strict)
elsif value.nil?
where_null col, side, strict
else
where_ray col, value, side, strict
end
Expand Down Expand Up @@ -126,24 +128,23 @@ def where_eq(col, value = point.value(col))
RAY_OP = { asc: '>', desc: '<' }.freeze
NULLS_ORD = { first: 'IS NOT NULL', last: 'IS NULL' }.freeze

# rubocop:disable Metrics/AbcSize

def where_ray(col, from, mode, strict = true)
reverse = (mode == :before)
if from.nil?
["#{col.column_name} #{NULLS_ORD[col.nulls_direction(reverse)]}", []]
def where_null(col, mode, strict)
if strict && col.nulls_direction(mode == :before) != :last
["#{col.column_name} IS NOT NULL", []]
else
["#{col.column_name} " \
"#{RAY_OP[col.direction(reverse)]}#{'=' unless strict} ?",
[from]].tap do |ray|
if col.nullable? && col.nulls_direction(reverse) == :last
ray[0] += " OR #{col.column_name} IS NULL"
ray[0] = "(#{ray[0]})"
end
WHERE_IDENTITY
end
end

def where_ray(col, from, mode, strict)
["#{col.column_name} "\
"#{RAY_OP[col.direction(mode == :before)]}#{'=' unless strict} ?",
[from]].tap do |ray|
if col.nullable? && col.nulls_direction(mode == :before) == :last
ray[0] = "(#{ray[0]} OR #{col.column_name} IS NULL)"
end
end
end
# rubocop:enable Metrics/AbcSize

WHERE_IDENTITY = ['', [].freeze].freeze

Expand Down

0 comments on commit 36c7fa8

Please sign in to comment.