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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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. # Returns a SQL statement for case/accent (if possible) insensitive match
  57. def like(left, right, options={})
  58. neg = (options[:match] == false ? 'NOT ' : '')
  59. if postgresql?
  60. if postgresql_unaccent?
  61. "unaccent(#{left}) #{neg}ILIKE unaccent(#{right})"
  62. else
  63. "#{left} #{neg}ILIKE #{right}"
  64. end
  65. elsif mysql?
  66. "#{left} #{neg}LIKE #{right}"
  67. else
  68. "#{left} #{neg}LIKE #{right} ESCAPE '\\'"
  69. end
  70. end
  71. # Returns a SQL statement to cast a timestamp column to a date given a time zone
  72. # Returns nil if not implemented for the current database
  73. def timestamp_to_date(column, time_zone)
  74. if postgresql?
  75. if time_zone
  76. identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier
  77. "(#{column}::timestamptz AT TIME ZONE '#{identifier}')::date"
  78. else
  79. "#{column}::date"
  80. end
  81. elsif mysql?
  82. if time_zone
  83. user_identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier
  84. local_identifier = ActiveSupport::TimeZone.find_tzinfo(Time.zone.name).identifier
  85. "DATE(CONVERT_TZ(#{column},'#{local_identifier}', '#{user_identifier}'))"
  86. else
  87. "DATE(#{column})"
  88. end
  89. end
  90. end
  91. # Resets database information
  92. def reset
  93. @postgresql_unaccent = nil
  94. end
  95. end
  96. end
  97. end