Unify handling of legacy and v2 statements
Add support for query history (fixes #98)