Class: StateOfTheNation::QueryString

Inherits:
Object
  • Object
show all
Defined in:
lib/state_of_the_nation/query_string.rb

Class Method Summary collapse

Class Method Details

.appropriate_db_type(klass) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/state_of_the_nation/query_string.rb', line 22

def self.appropriate_db_type(klass)
  case klass.connection.adapter_name
  when /PostgreSQL/
    :postgresql
  else
    :mysql
  end
end

.database_appropriate_types(klass) ⇒ Object



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

def self.database_appropriate_types(klass)
  return {
    postgresql: {
      active_scope: "(%{finish_key} IS NULL OR %{finish_key} > ?::timestamp) AND %{start_key} <= ?::timestamp",
      less_than: "(%{start_key} < ?::timestamp)",
      greater_than_or_null: "(%{finish_key} > ?::timestamp) OR (%{finish_key} IS NULL)",
    },
    mysql: {
      active_scope: "(%{finish_key} IS NULL OR %{finish_key} > ?) AND %{start_key} <= ?",
      less_than: "(%{start_key} < ?)",
      greater_than_or_null: "(%{finish_key} > ?) OR (%{finish_key} IS NULL)"
    }
  }[appropriate_db_type(klass)]
end

.query_for(type, klass) ⇒ Object



3
4
5
# File 'lib/state_of_the_nation/query_string.rb', line 3

def self.query_for(type, klass)
  database_appropriate_types(klass)[type] % { finish_key: klass.finish_key, start_key: klass.start_key }
end