ged/ruby-pg — pull request #6 by larskanis (2014-09-29 16:04:55+00:00)
Title: Add type mapping and performance improvements

This pull request adds the possibility to type cast values to and from the database in native code. That means, that data received from the database is directly converted to proper Ruby objects when retrieved by the user and that Ruby objects sent to the database are converted to wire format without additional object allocations. Simple types and complex types like arrays or hstore can be converted in-line or alternatively to/from their string representation. Not only built-in PostgreSQL types can be converted, but also extension / user defined types. Type casting can be applied to all value transmission- and retrieval methods including copy in and copy out, even for the binary formats.

There are several different strategies implemented already, to type cast values based on different criteria. One of them is PG::TypeMapAllStrings, which represents the default and current behavior of ruby-pg. There is also a pool of encoders and decoders to choose the desired conversion for a given value.

The pull request also improves the overall performance by reducing a lot of duplicated checks and hash lookups in the code paths outside of the type casting. Enabling type casts usually improves the performance even more.

I tried to implement all interesting use cases first and foremost, to avoid later API changes when there are many encoders or decoders. That's why there are a lot of PostgreSQL types with no representation in ruby-pg as yet, but they can now be implemented quite straightforward.

I also implemented a [patch for rails](https://github.com/larskanis/rails/compare/type_cast_via_ruby_pg) to make use of native type casts. Unfortunately it's no longer up to date in respect to both rails and ruby-pg. I'll update it in the next weeks.