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?
view
orstored procedure
containing the query and usesys.sql_dependencies
on the new view/SP to get the referenced objects. It doesn't help with aliases, but may be a reliable means to get the referenced tables and views. Tip: It's helpful to tag database questions with both the appropriate software (MySQL, Oracle, DB2, ...) and version, e.g.sql-server-2014
. – RamboTSql120Parser
. Looks promising, e.g.TableReferenceWithAlias
. – Rambo