diff options
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-x | activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 34 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | 21 |
2 files changed, 36 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 177d201259..76ea6e6530 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -38,17 +38,7 @@ module ActiveRecord mysql = Mysql.init mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey] - if config[:encoding] - begin - mysql.options(Mysql::SET_CHARSET_NAME, config[:encoding]) - rescue - 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.' - end - end - - conn = mysql.real_connect(host, username, password, database, port, socket) - conn.query("SET NAMES '#{config[:encoding]}'") if config[:encoding] - ConnectionAdapters::MysqlAdapter.new(conn, logger, [host, username, password, database, port, socket], mysql) + ConnectionAdapters::MysqlAdapter.new(mysql, logger, [host, username, password, database, port, socket], config) end end @@ -97,10 +87,11 @@ module ActiveRecord "MySQL server has gone away" ] - def initialize(connection, logger, connection_options=nil, mysql=Mysql) + def initialize(connection, logger, connection_options=nil, config={}) super(connection, logger) @connection_options = connection_options - @mysql = mysql + @config = config + connect end def adapter_name #:nodoc: @@ -144,7 +135,7 @@ module ActiveRecord end def quote_string(string) #:nodoc: - @mysql.quote(string) + @connection.quote(string) end def quoted_true @@ -170,7 +161,7 @@ module ActiveRecord @connection.ping else @connection.close rescue nil - @connection.real_connect(*@connection_options) + connect end end @@ -318,6 +309,19 @@ module ActiveRecord private + def connect + encoding = @config[:encoding] + if encoding + begin + @connection.options(Mysql::SET_CHARSET_NAME, encoding) + rescue + 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.' + end + end + @connection.real_connect(*@connection_options) + execute("SET NAMES '#{encoding}'") if encoding + end + def select(sql, name = nil) @connection.query_with_result = true result = execute(sql, name) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index e1793ba460..3f826edfe2 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -12,7 +12,6 @@ module ActiveRecord username = config[:username].to_s password = config[:password].to_s - encoding = config[:encoding] min_messages = config[:min_messages] if config.has_key?(:database) @@ -22,12 +21,10 @@ module ActiveRecord end pga = ConnectionAdapters::PostgreSQLAdapter.new( - PGconn.connect(host, port, "", "", database, username, password), logger + PGconn.connect(host, port, "", "", database, username, password), logger, config ) pga.schema_search_path = config[:schema_search_path] || config[:schema_order] - pga.execute("SET client_encoding TO '#{encoding}'") if encoding - pga.execute("SET client_min_messages TO '#{min_messages}'") if min_messages pga end @@ -52,6 +49,12 @@ module ActiveRecord 'PostgreSQL' end + def initialize(connection, logger, config = {}) + super(connection, logger) + @config = config + configure_connection + end + # Is this connection alive and ready for queries? def active? # TODO: postgres-pr doesn't have PGconn#status. @@ -67,6 +70,7 @@ module ActiveRecord # TODO: postgres-pr doesn't have PGconn#reset. if @connection.respond_to?(:reset) @connection.reset + configure_connection end end @@ -327,6 +331,15 @@ module ActiveRecord private BYTEA_COLUMN_TYPE_OID = 17 + def configure_connection + if @config[:encoding] + execute("SET client_encoding TO '#{@config[:encoding]}'") + end + if @config[:min_messages] + execute("SET client_min_messages TO '#{@config[:min_messages]}'") + end + end + def last_insert_id(table, sequence_name) Integer(select_value("SELECT currval('#{sequence_name}')")) end |