Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3152

Show
Ignore:
Timestamp:
11/21/05 18:51:27 (3 years ago)
Author:
bitsweat
Message:

MySQL: introduce :encoding option to specify the character set for client, connection, and results. Only available for MySQL 4.1 and later with the mysql-ruby driver. Do SHOW CHARACTER SET in mysql client to see available encodings. Closes #2975.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r3142 r3152  
    11*SVN* 
     2 
     3* MySQL: introduce :encoding option to specify the character set for client, connection, and results.  Only available for MySQL 4.1 and later with the mysql-ruby driver.  Do SHOW CHARACTER SET in mysql client to see available encodings.  #2975 [Shugo Maeda] 
    24 
    35* Add tasks to create, drop and rebuild the MySQL and PostgreSQL test  databases. [Marcel Molina Jr.] 
  • trunk/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

    r3148 r3152  
    3939      mysql = Mysql.init 
    4040      mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey] 
    41       ConnectionAdapters::MysqlAdapter.new(mysql.real_connect(host, username, password, database, port, socket), logger, [host, username, password, database, port, socket]) 
     41      if config[:encoding] 
     42        begin 
     43          mysql.options(Mysql::SET_CHARSET_NAME, config[:encoding]) 
     44        rescue 
     45          raise ActiveRecord::ConnectionFailed, 'The :encoding option is only available for MySQL 4.1 and later with the mysql-ruby driver.  Again, this does not work with the ruby-mysql driver or MySQL < 4.1.' 
     46        end 
     47      end 
     48 
     49      conn = mysql.real_connect(host, username, password, database, port, socket) 
     50      conn.query("SET NAMES '#{config[:encoding]}'") if config[:encoding] 
     51      ConnectionAdapters::MysqlAdapter.new(conn, logger, [host, username, password, database, port, socket], mysql) 
    4252    end 
    4353  end 
     
    8898      ] 
    8999 
    90       def initialize(connection, logger, connection_options=nil
     100      def initialize(connection, logger, connection_options=nil, mysql=Mysql
    91101        super(connection, logger) 
    92102        @connection_options = connection_options 
     103        @mysql = mysql 
    93104      end 
    94105 
     
    120131      # QUOTING ================================================== 
    121132 
     133      def quote(value, column = nil) 
     134        if value.kind_of?(String) && column && column.type == :binary 
     135          s = column.class.string_to_binary(value).unpack("H*")[0] 
     136          "x'#{s}'" 
     137        else 
     138          super 
     139        end 
     140      end 
     141 
    122142      def quote_column_name(name) #:nodoc: 
    123143        "`#{name}`" 
     
    125145 
    126146      def quote_string(string) #:nodoc: 
    127         Mysql::quote(string) 
     147        @mysql.quote(string) 
    128148      end 
    129149