Module: Redmine::Database

Defined in:
lib/redmine/database.rb

Overview

Helper module to get information about the Redmine database

Class Method Summary collapse

Class Method Details

.like(left, right, options = {}) ⇒ Object

Returns a SQL statement for case/accent (if possible) insensitive match



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/redmine/database.rb', line 53

def like(left, right, options={})
  neg = (options[:match] == false ? 'NOT ' : '')

  if postgresql?
    if postgresql_unaccent?
      "unaccent(#{left}) #{neg}ILIKE unaccent(#{right})"
    else
      "#{left} #{neg}ILIKE #{right}"
    end
  else
    "#{left} #{neg}LIKE #{right}"
  end
end

.mysql?Boolean

Returns true if the database is MySQL

Returns:

  • (Boolean)


48
49
50
# File 'lib/redmine/database.rb', line 48

def mysql?
  (ActiveRecord::Base.connection.adapter_name =~ /mysql/i).present?
end

.postgresql?Boolean

Returns true if the database is PostgreSQL

Returns:

  • (Boolean)


23
24
25
# File 'lib/redmine/database.rb', line 23

def postgresql?
  (ActiveRecord::Base.connection.adapter_name =~ /postgresql/i).present?
end

.postgresql_unaccent?Boolean

Returns true if the database is a PostgreSQL >=9.0 database with the unaccent extension installed

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/redmine/database.rb', line 33

def postgresql_unaccent?
  if postgresql?
    return @postgresql_unaccent unless @postgresql_unaccent.nil?
    begin
      sql = "SELECT name FROM pg_available_extensions WHERE installed_version IS NOT NULL and name = 'unaccent'"
      @postgresql_unaccent = postgresql_version >= 90000 && ActiveRecord::Base.connection.select_value(sql).present?
    rescue
      false
    end
  else
    false
  end
end

.postgresql_versionObject

Returns the PostgreSQL version or nil if another DBMS is used



28
29
30
# File 'lib/redmine/database.rb', line 28

def postgresql_version
  postgresql? ? ActiveRecord::Base.connection.send(:postgresql_version) : nil
end

.resetObject

Resets database information



68
69
70
# File 'lib/redmine/database.rb', line 68

def reset
  @postgresql_unaccent = nil
end