Use better internal query structure

Ditch the old “children” structure as it is too generic and cannot be
tested automatically

Also parameter order should be determined by the compiler, not the
query itself
add tests and hints