Modified constructSelectQuery function