Module: ChiliProject::Database

Defined in:
lib/chili_project/database.rb

Overview

This module provides some information about the currently used database adapter. It can be used to write code specific to certain database vendors which, while not not encouraged, is sometimes necessary due to syntax differences.

Class Method Summary collapse

Class Method Details

.adapter_nameObject

Get the raw name of the currently used database adapter. This string is set by the used adapter gem.


36
37
38
# File 'lib/chili_project/database.rb', line 36

def self.adapter_name
  ActiveRecord::Base.connection.adapter_name
end

.nameObject

returns the identifier of the currently used database type


41
42
43
44
45
# File 'lib/chili_project/database.rb', line 41

def self.name
  supported_adapters.find(proc{ [:unknown, //] }) { |adapter, regex|
    self.adapter_name =~ regex
  }[0]
end

.supported_adaptersObject

This method returns a hash which maps the identifier of the supported adapter to a regex matching the adapter_name.


26
27
28
29
30
31
32
# File 'lib/chili_project/database.rb', line 26

def self.supported_adapters
  @adapters ||= ({
    :mysql => /mysql/i,
    :postgresql => /postgres/i,
    :sqlite => /sqlite/i
  })
end

.version(raw = false) ⇒ Object

Return the version of the underlying database engine. Set the raw argument to true to return the unmangled string from the database.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chili_project/database.rb', line 58

def self.version(raw = false)
  case self.name
  when :mysql
    version = ActiveRecord::Base.connection.select_value('SELECT VERSION()')
  when :postgresql
    version = ActiveRecord::Base.connection.select_value('SELECT version()')
    raw ? version : version.match(/^PostgreSQL (\S+)/i)[1]
  when :sqlite
    if Object.const_defined?('RUBY_ENGINE') && ::RUBY_ENGINE == 'jruby'
      Jdbc::SQLite3::VERSION
    else
      if SQLite3.const_defined? 'SQLITE_VERSION'
        SQLite3::SQLITE_VERSION
      else
        SQLite3::Driver::Native::API.sqlite3_libversion
      end
    end
  end
end