(5 secs) Doing this properly is actually more work than you might think.
Here's a simplified version pulled from Sequel 2.0's MySQL adapter. This is the simplest thing that works, but it has some unfortunate drawbacks.
http://github.com/jeremyevans/sequel/commit/494f2172920521e284a6970cf0de40288acffc52