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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/redmine/database.rb', line 65

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
  elsif mysql?
    "#{left} #{neg}LIKE #{right}"
  else
    "#{left} #{neg}LIKE #{right} ESCAPE '\\'"
  end
end

.mysql?Boolean

Returns true if the database is MySQL

Returns:

  • (Boolean)


60
61
62
# File 'lib/redmine/database.rb', line 60

def mysql?
  /mysql/i.match?(ActiveRecord::Base.connection.adapter_name)
end

.postgresql?Boolean

Returns true if the database is PostgreSQL

Returns:

  • (Boolean)


30
31
32
# File 'lib/redmine/database.rb', line 30

def postgresql?
  /postgresql/i.match?(ActiveRecord::Base.connection.adapter_name)
end

.postgresql_unaccent?Boolean

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

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/redmine/database.rb', line 40

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



35
36
37
# File 'lib/redmine/database.rb', line 35

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

.resetObject

Resets database information



103
104
105
# File 'lib/redmine/database.rb', line 103

def reset
  @postgresql_unaccent = nil
end

.sqlite?Boolean

Returns true if the database is SQLite

Returns:

  • (Boolean)


25
26
27
# File 'lib/redmine/database.rb', line 25

def sqlite?
  ActiveRecord::Base.connection.adapter_name =~ /sqlite/i
end

.timestamp_to_date(column, time_zone) ⇒ Object

Returns a SQL statement to cast a timestamp column to a date given a time zone Returns nil if not implemented for the current database



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/redmine/database.rb', line 83

def timestamp_to_date(column, time_zone)
  if postgresql?
    if time_zone
      identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier
      "(#{column}::timestamptz AT TIME ZONE '#{identifier}')::date"
    else
      "#{column}::date"
    end
  elsif mysql?
    if time_zone
      user_identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier
      local_identifier = ActiveSupport::TimeZone.find_tzinfo(Time.zone.name).identifier
      "DATE(CONVERT_TZ(#{column},'#{local_identifier}', '#{user_identifier}'))"
    else
      "DATE(#{column})"
    end
  end
end