Simple, hook on the query
filter and structure that “unstructured” sql data. Parsing SQL is annoying but can’t be that difficult.
Of course some queries are ran before plugins are loaded as you do need to get the list of active plugins and active theme before including their file. And if you need to access a DB which is different from mysql or ignores the dbname/user/password settings from wp_config.php
file, you need to do override $wpdb
before the first query is sent.