move all sql stuff to separate module, get rid of macros