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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 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 PostgreSQL
  23. def postgresql?
  24. /postgresql/i.match?(ActiveRecord::Base.connection.adapter_name)
  25. end
  26. # Returns the PostgreSQL version or nil if another DBMS is used
  27. def postgresql_version
  28. postgresql? ? ActiveRecord::Base.connection.send(:postgresql_version) : nil
  29. end
  30. # Returns true if the database is a PostgreSQL >=9.0 database with the unaccent extension installed
  31. def postgresql_unaccent?
  32. if postgresql?
  33. return @postgresql_unaccent unless @postgresql_unaccent.nil?
  34. begin
  35. sql = "SELECT name FROM pg_available_extensions WHERE installed_version IS NOT NULL and name = 'unaccent'"
  36. @postgresql_unaccent = postgresql_version >= 90000 && ActiveRecord::Base.connection.select_value(sql).present?
  37. rescue
  38. false
  39. end
  40. else
  41. false
  42. end
  43. end
  44. # Returns true if the database is MySQL
  45. def mysql?
  46. /mysql/i.match?(ActiveRecord::Base.connection.adapter_name)
  47. end
  48. # Returns a SQL statement for case/accent (if possible) insensitive match
  49. def like(left, right, options={})
  50. neg = (options[:match] == false ? 'NOT ' : '')
  51. if postgresql?
  52. if postgresql_unaccent?
  53. "unaccent(#{left}) #{neg}ILIKE unaccent(#{right})"
  54. else
  55. "#{left} #{neg}ILIKE #{right}"
  56. end
  57. else
  58. "#{left} #{neg}LIKE #{right}"
  59. end
  60. end
  61. # Returns a SQL statement to cast a timestamp column to a date given a time zone
  62. # Returns nil if not implemented for the current database
  63. def timestamp_to_date(column, time_zone)
  64. if postgresql?
  65. if time_zone
  66. identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier
  67. "(#{column}::timestamptz AT TIME ZONE '#{identifier}')::date"
  68. else
  69. "#{column}::date"
  70. end
  71. elsif mysql?
  72. if time_zone
  73. user_identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier
  74. local_identifier = ActiveSupport::TimeZone.find_tzinfo(Time.zone.name).identifier
  75. "CONVERT_TZ(DATE(#{column}),'#{local_identifier}', '#{user_identifier}')"
  76. else
  77. "DATE(#{column})"
  78. end
  79. end
  80. end
  81. # Resets database information
  82. def reset
  83. @postgresql_unaccent = nil
  84. end
  85. end
  86. end
  87. end