Flow query refactor

Better flow query refactor without HSTORE

replaces https://github.com/Tahi-project/tahi/pull/717