Sha256: 9fe9e3d96dc495c14aa07b9bf15f1984f26556cf4ab5f75ab3fa8df53384b3ba

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

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

    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)",
          start_and_finish_not_equal_or_are_null: "(%{start_key} != %{finish_key}) OR (%{start_key} IS NULL) 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)",
          start_and_finish_not_equal_or_are_null: "(%{start_key} != %{finish_key}) OR (%{start_key} IS NULL) OR (%{finish_key} IS NULL)"
        }
      }[appropriate_db_type(klass)]
    end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
state_of_the_nation-2.0.1 lib/state_of_the_nation/query_string.rb
state_of_the_nation-2.0.0 lib/state_of_the_nation/query_string.rb
state_of_the_nation-1.1.6 lib/state_of_the_nation/query_string.rb
state_of_the_nation-1.1.5 lib/state_of_the_nation/query_string.rb