Generally, I find that if you do anything too complicated with SQL it won't be scalable because the optimizer will start making bad decisions and you should just do it in the app tier where you can tune the algorithm more easily.
Thus, if you need abstraction, you probably are doing it wrong already.
Thus, if you need abstraction, you probably are doing it wrong already.