You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

database.rb 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. # Helper module to get information about the Redmine database
  20. module Database
  21. class << self
  22. # Returns true if the database is SQLite
  23. def sqlite?
  24. ActiveRecord::Base.connection.adapter_name =~ /sqlite/i
  25. end
  26. # Returns true if the database is PostgreSQL
  27. def postgresql?
  28. /postgresql/i.match?(ActiveRecord::Base.connection.adapter_name)
  29. end
  30. # Returns the PostgreSQL version or nil if another DBMS is used
  31. def postgresql_version
  32. postgresql? ? ActiveRecord::Base.connection.send(:postgresql_version) : nil
  33. end
  34. # Returns true if the database is a PostgreSQL >=9.0 database with the unaccent extension installed
  35. def postgresql_unaccent?
  36. if postgresql?
  37. return @postgresql_unaccent unless @postgresql_unaccent.nil?
  38. begin
  39. sql =
  40. "SELECT name FROM pg_available_extensions " \
  41. "WHERE installed_version IS NOT NULL and name = 'unaccent'"
  42. @postgresql_unaccent =
  43. postgresql_version >= 90000 &&
  44. ActiveRecord::Base.connection.select_value(sql).present?
  45. rescue
  46. false
  47. end
  48. else
  49. false
  50. end
  51. end
  52. # Returns true if the database is MySQL
  53. def mysql?
  54. /mysql/i.match?(ActiveRecord::Base.connection.adapter_name)
  55. end
  56. def mysql_version
  57. mysql? ? ActiveRecord::Base.connection.select_value("SELECT VERSION()") : nil
  58. end
  59. # Returns a SQL statement for case/accent (if possible) insensitive match
  60. def like(left, right, options={})
  61. neg = (options[:match] == false ? 'NOT ' : '')
  62. if postgresql?
  63. if postgresql_unaccent?
  64. "unaccent(#{left}) #{neg}ILIKE unaccent(#{right})"
  65. else
  66. "#{left} #{neg}ILIKE #{right}"
  67. end
  68. elsif mysql?
  69. "#{left} #{neg}LIKE #{right}"
  70. else
  71. "#{left} #{neg}LIKE #{right} ESCAPE '\\'"
  72. end
  73. end
  74. # Returns a SQL statement to cast a timestamp column to a date given a time zone
  75. # Returns nil if not implemented for the current database
  76. def timestamp_to_date(column, time_zone)
  77. if postgresql?
  78. if time_zone
  79. identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier
  80. "(#{column}::timestamptz AT TIME ZONE '#{identifier}')::date"
  81. else
  82. "#{column}::date"
  83. end
  84. elsif mysql?
  85. if time_zone
  86. user_identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier
  87. local_identifier = ActiveSupport::TimeZone.find_tzinfo(Time.zone.name).identifier
  88. "DATE(CONVERT_TZ(#{column},'#{local_identifier}', '#{user_identifier}'))"
  89. else
  90. "DATE(#{column})"
  91. end
  92. end
  93. end
  94. # Resets database information
  95. def reset
  96. @postgresql_unaccent = nil
  97. end
  98. end
  99. end
  100. end