Class: Randumb::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/randumb/syntax.rb

Class Method Summary collapse

Class Method Details

.random_order_clause(ranking_column, opts = {}) ⇒ Object

builds the order clause to be appended in where clause



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/randumb/syntax.rb', line 6

def random_order_clause(ranking_column, opts={})
  if ranking_column.nil?
    random_for(opts)
  else
    connection = opts[:connection]
    if connection.adapter_name =~ /sqlite/i
      # computer multiplication is faster than division I was once taught...so translate here
      max_int = 9223372036854775807.0
      multiplier = 1.0 / max_int
      "(#{ranking_column} * ABS(#{random_for(opts)} * #{multiplier}) ) DESC"
    else
      "(#{ranking_column} * #{random_for(opts)}) DESC"
    end
  end
end