Bug 8798: moving code to Koha::Database and adding tests