# Extends Rails to define a charset and collation when you create your database (default = utf8) module ActiveRecord module ConnectionAdapters # class MysqlAdapter < AbstractAdapter # Create a new MySQL database allowing you to specify the charset and collation, by default the database is created with utf8 charset and utf8_bin collation # usage: ActiveRecord::Base.connection.create_database('charset_plugin_test', {:charset => 'latin1', :collation => 'latin1_bin'}) def create_database(name, options = {}) if options[:collation] execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] ||= 'utf8'}` COLLATE `#{options[:collation]}`" else execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] ||= 'utf8'}` " end end # Returns the database character set. def charset show_variable 'character_set_database' end # Returns the database collation strategy. def collation show_variable 'collation_database' end # SHOW VARIABLES LIKE 'name' def show_variable(name) variables = select_all("SHOW VARIABLES LIKE '#{name}'") variables.first['Value'] unless variables.empty? end end end end