Commits

Michael Granger  committed 6680b39

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.

  • Participants
  • Parent commits 15bfcbe

Comments (0)

Files changed (2)

File ext/pg_connection.c

 
 static PGconn *pgconn_check( VALUE );
 static VALUE pgconn_finish( VALUE );
-
+#ifdef M17N_SUPPORTED
+static VALUE pgconn_set_default_encoding( VALUE self );
+#endif
 
 /*
  * Global functions
 	PGconn *conn = NULL;
 	VALUE conninfo;
 	VALUE error;
-#ifdef M17N_SUPPORTED	
-	rb_encoding *enc;
-	const char *encname;
-#endif
 
 	conninfo = rb_funcall2( rb_cPGconn, rb_intern("parse_connect_args"), argc, argv );
 	conn = PQconnectdb(StringValuePtr(conninfo));
 	}
 
 #ifdef M17N_SUPPORTED
-	/* If Ruby has its Encoding.default_internal set, set PostgreSQL's client_encoding 
-	 * to match */
-	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) );
-	}
+	pgconn_set_default_encoding( self );
 #endif
 
 	if (rb_block_given_p()) {
 	return encoding;
 }
 
+
+
+/*
+ * call-seq:
+ *   conn.set_default_encoding() -> Encoding
+ *
+ * If Ruby has its Encoding.default_internal set, set PostgreSQL's client_encoding
+ * to match. Returns the new Encoding, or +nil+ if the default internal encoding
+ * wasn't set.
+ */
+static VALUE
+pgconn_set_default_encoding( VALUE self )
+{
+	PGconn *conn = pg_get_pgconn( self );
+	rb_encoding *enc;
+	const char *encname;
+
+	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) );
+		return rb_enc_from_encoding( enc );
+	} else {
+		return Qnil;
+	}
+}
+
+
 #endif /* M17N_SUPPORTED */
 
 
 	rb_define_method(rb_cPGconn, "internal_encoding", pgconn_internal_encoding, 0);
 	rb_define_method(rb_cPGconn, "internal_encoding=", pgconn_internal_encoding_set, 1);
 	rb_define_method(rb_cPGconn, "external_encoding", pgconn_external_encoding, 0);
+	rb_define_method(rb_cPGconn, "set_default_encoding", pgconn_set_default_encoding, 0);
 #endif /* M17N_SUPPORTED */
 
 }

File spec/pg/connection_spec.rb

 				end
 			end
 
+			it "allows users of the async interface to set the client_encoding to the default_internal" do
+				begin
+					prev_encoding = Encoding.default_internal
+					Encoding.default_internal = Encoding::KOI8_U
+
+					@conn.set_default_encoding
+
+					@conn.internal_encoding.should == Encoding::KOI8_U
+				ensure
+					Encoding.default_internal = prev_encoding
+				end
+			end
+
 		end