main.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. raise "Do not directly load this library." unless defined?(ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter)
  2. module ActiveRecord::ConnectionAdapters
  3. class ColumnDefinition
  4. module CharsetSupport
  5. attr_accessor :charset, :collation
  6. end
  7. prepend CharsetSupport
  8. end
  9. class TableDefinition
  10. module CharsetSupport
  11. def new_column_definition(name, type, options)
  12. column = super
  13. column.charset = options[:charset]
  14. column.collation = options[:collation]
  15. column
  16. end
  17. end
  18. prepend CharsetSupport
  19. end
  20. class AbstractMysqlAdapter
  21. module CharsetSupport
  22. def prepare_column_options(column, types)
  23. spec = super
  24. spec[:charset] = column.charset.inspect if column.charset && column.charset != charset
  25. spec[:collation] = column.collation.inspect if column.collation && column.collation != collation
  26. spec
  27. end
  28. def migration_keys
  29. super + [:charset, :collation]
  30. end
  31. def utf8mb4_supported?
  32. if @utf8mb4_supported.nil?
  33. @utf8mb4_supported = !select("show character set like 'utf8mb4'").empty?
  34. else
  35. @utf8mb4_supported
  36. end
  37. end
  38. def charset_collation(charset, collation)
  39. [charset, collation].map { |name|
  40. case name
  41. when nil
  42. nil
  43. when /\A(utf8mb4(_\w*)?)\z/
  44. if utf8mb4_supported?
  45. $1
  46. else
  47. "utf8#{$2}"
  48. end
  49. else
  50. name.to_s
  51. end
  52. }
  53. end
  54. def create_database(name, options = {})
  55. # utf8mb4 is used in column definitions; use utf8 for
  56. # databases.
  57. [:charset, :collation].each { |key|
  58. case options[key]
  59. when /\A(utf8mb4(_\w*)?)\z/
  60. options = options.merge(key => "utf8#{$2}")
  61. end
  62. }
  63. super(name, options)
  64. end
  65. end
  66. prepend CharsetSupport
  67. class SchemaCreation
  68. module CharsetSupport
  69. def column_options(o)
  70. column_options = super
  71. column_options[:charset] = o.charset unless o.charset.nil?
  72. column_options[:collation] = o.collation unless o.collation.nil?
  73. column_options
  74. end
  75. def add_column_options!(sql, options)
  76. charset, collation = @conn.charset_collation(options[:charset], options[:collation])
  77. if charset
  78. sql << " CHARACTER SET #{charset}"
  79. end
  80. if collation
  81. sql << " COLLATE #{collation}"
  82. end
  83. super
  84. end
  85. end
  86. prepend CharsetSupport
  87. end
  88. class Column
  89. module CharsetSupport
  90. attr_reader :charset
  91. def initialize(*args)
  92. super
  93. @charset = @collation[/\A[^_]+/] unless @collation.nil?
  94. end
  95. end
  96. prepend CharsetSupport
  97. end
  98. end
  99. end