Move query-related stff to its own file

Signed-off-by: Ian Macalinao <me@ian.pw>