I would like to write an API function that is modular. The problem is, modular parts feature same queries. I have suspicion, that same queries in every subfunction will affect perfomance.
Example:
MAIN_FUNCTION: base_data(customer_id, user_id) {
SELECT *
FROM get_absence(customer_id, user_id)
JOIN get_work_hours(customer_id, user_id)
USING (worker_id)
}
get_absence(customer_id, user_id) {
RETURN QUERY
SELECT *
FROM get_user_workers(customer_id, user_id)
JOIN absence_table
USING (worker_id)
}
get_work_hours(customer_id, user_id) {
RETURN QUERY
SELECT *
FROM get_user_workers(customer_id, user_id)
JOIN workhours_table
USING (worker_id)
}
Sorry for the pseudocode, but it is a bit shorter. Basically, most of the subfunctions would contain the get_user_workers query. If I would build a big and boring function, I would query workers with common table expression.
How to achieve readability and modularity, instead of countless lines of queries?