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

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