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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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. else
  66. "#{left} #{neg}LIKE #{right}"
  67. end
  68. end
  69. # Returns a SQL statement to cast a timestamp column to a date given a time zone
  70. # Returns nil if not implemented for the current database
  71. def timestamp_to_date(column, time_zone)
  72. if postgresql?
  73. if time_zone
  74. identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier
  75. "(#{column}::timestamptz AT TIME ZONE '#{identifier}')::date"
  76. else
  77. "#{column}::date"
  78. end
  79. elsif mysql?
  80. if time_zone
  81. user_identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier
  82. local_identifier = ActiveSupport::TimeZone.find_tzinfo(Time.zone.name).identifier
  83. "DATE(CONVERT_TZ(#{column},'#{local_identifier}', '#{user_identifier}'))"
  84. else
  85. "DATE(#{column})"
  86. end
  87. end
  88. end
  89. # Resets database information
  90. def reset
  91. @postgresql_unaccent = nil
  92. end
  93. end
  94. end
  95. end