All of my tenanted tables in sql server have a field customer_id. I'm attempting to write an interceptor for NPoco that will extract all table and alias names from the query. match it to an exception list of tables that are not tenanted, and modify the select to check the tenant key of all tenanted tables in the where clause.
I'm having a really hard time finding a sql parser that can extract table names and aliases from a sql statement.
A good solution would parse the following statement.
SELECT fis.OrderDateKey, SUM(fis.SalesAmount) AS TotalSales
FROM FactInternetSales fis
Join product on fis.productid = product.productid
where p.name like 'prefix%'
GROUP BY fis.OrderDateKey
HAVING fis.OrderDateKey > 20010000
ORDER BY fis.OrderDateKey;
Would provide enough information for me to reliably construct a dictionary with 2 items {"FactInternetSales", "fis"} and {"product", "product"}
So far I have only found tokenizers that only know types that are too generic for me to rely upon. (ie keyword, identifier, operator, text). Is there anything out there that can do this sort of work in .net. Or are there better strategies to append these tenant checks to every query ran?