hello dear Bogdan,  

regarding your ideas for improvement :

what about using a parameter table  and join with this table. That will enable passing parameters at run time.

Furthermore when the query object is not sufficient due to its limitations ( like no cte, no union) an alternative is to use a SQL View and use this as a table , also with the possibility of joining with a "parameter table" that allows you to specify parameters or even select which function to execute ( in that way you can use one parameter table for multiple purposes).