DatabaseBase::query - autogenerate $fname if not passed

Backporting #4753