Viktor/sqlfunctions

This is a massive rewrite of a lot of stuff, but it will let us lock down the database a lot better.

I've tested everything affected afaik, but I could have missed something. We need to write real integration tests.
Use `select * from function` when you want to use a `where` clause or you want to extract multiple columns. In the case of `orders_new`, there's only one output column so you can just `select function` and it will give the same result.