Viktor/sqlfunctions

This is a massive rewrite of a lot of stuff, but it will let us lock down the database a lot better.