Providing a way to register middleware to the QueryBuilder class

For certain “deep” integrations, it would be helpful to be able to add middleware that interacts with the QueryBuilder class in order to modify the SQL queries before they run and then interact with the results once they are returned. I realize that there are probably not many use cases for this, but let me explain mine.

I have developed a rather alpha way to make a Nextcloud installation operate as a multitenant platform. It works by adding logic to a custom multitenant.config.php file (which isn’t at all recommended, but it works). I plan to test it with some colleagues in the next week or so.

The drawback to my approach is that it creates a completely separate database for each tenant. This is a good thing and a bad thing. On one hand, it provides isolation. On the other hand, in production, it will require enormous database resources, and I will have to write additional logic (likely “kludge”) to allow administrators to manage the separate instances.

If I could interact with the QueryBuilder class via middleware, I could simplify the entire system without having to modify the existing database or core files. I’m sure there are other use cases and deep integrations that would benefit to such an addition.

Thoughts?