Refactored the changes for the patch to be consistent with db/models/sql/query.py code.