Issue #124 resolved

a humble request for set_default_encoding helper for async_connect implementations

royaltm
created an issue

I am currently developing pg client for EventMachine which provides inherited PG::Connection with a bunch of async_methods: https://github.com/royaltm/ruby-em-pg-client

The goal of the project is to provide as much of pg functionality as possible to be used with event reactor and at the same time to follow POLS and try not to alter any expected behavior of inherited class.

One of such behaviors (#33) is that non-async new() magically sets conn.internal_encoding=Encoding.default_internal after connection has been made. I'v made a hack to my async_connect() method which tries to do exactly what non-async new() does by adding the following code:

{{{

!ruby

unless Encoding.default_internal.nil? begin @client.internal_encoding = Encoding.default_internal rescue EncodingError warn "warning: Failed to set the default_internal encoding to " + "#{Encoding.default_internal}: '#{@client.error_message}'" end end }}}

The thing is, i have to maintain the code along with any future changes to PG::Connection and it might begin to work unexpectedly for various versions of pg interface in the future.

In the perfect world, I would gladly see the PG::Connection#set_default_encoding() which would do exactly the same as the code found in pgconn_init() and which is:

{{{

!c

if (( enc = rb_default_internal_encoding() )) { encname = pg_get_rb_encoding_as_pg_encoding( enc ); if ( PQsetClientEncoding(conn, encname) != 0 ) rb_warn( "Failed to set the default_internal encoding to %s: '%s'", encname, PQerrorMessage(conn) ); }

}}}

Comments (3)

  1. royaltm reporter

    No problem. I just read some of the issues you have received in the past <grin>. I think the developers should try a little bit harder to make each others life easier :)

  2. Michael Granger repo owner

    New method: PG::Connection#set_default_encoding (fixes #124)

    This pulls up the code that sets the client_encoding in synchronous connections into a public method that can be called by people using the asynchronous API to achieve the same thing once the connection is established.

    Thanks to royaltm for the suggestion.

    6680b395e6f4

  3. Log in to comment