Refactoring of query builder