added logic to generate sql and refined code.