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

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