make Query reuseable

fixes #1545
refactored scalar query functions to share common code
Merge pull request #1560 from cebe/1545-query-reuse

make Query reuseable