Module: Redmine::Database
- Defined in:
- lib/redmine/database.rb
Overview
Helper module to get information about the Redmine database
Class Method Summary collapse
-
.like(left, right, options = {}) ⇒ Object
Returns a SQL statement for case/accent (if possible) insensitive match.
-
.mysql? ⇒ Boolean
Returns true if the database is MySQL.
-
.postgresql? ⇒ Boolean
Returns true if the database is PostgreSQL.
-
.postgresql_unaccent? ⇒ Boolean
Returns true if the database is a PostgreSQL >=9.0 database with the unaccent extension installed.
-
.postgresql_version ⇒ Object
Returns the PostgreSQL version or nil if another DBMS is used.
-
.reset ⇒ Object
Resets database information.
-
.sqlite? ⇒ Boolean
Returns true if the database is SQLite.
-
.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.
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, ={}) neg = ([: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
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
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
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_version ⇒ Object
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 |
.reset ⇒ Object
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
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 (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 |