Ensure query bindings are counted correctly

Fixes #202.

Still WIP because tests have not been added for the issue nor for making sure that compile time optimizations work.

/cc @josevalim