backporting AgaviMysqliDatabase from 8a9d2930834df15dbc77678e6d8fbcd97525b885 and eaa2a61e3d9e6057d6fc01df57b04905b0ba8d95, closes #601