backporting AgaviMysqliDatabase from d185d0b8267271d398d73e4ec94a7e6b94ae95ad and ca9ba934c0f896811514d5b064a73a7b406792da, closes #526