In my (C# + SQL Server) application, the user will be able to define rules over data such as:
ITEM_ID = 1
OR (ITEM_NAME LIKE 'something' AND ITEM_PRICE > 123
AND (ITEM_WEIGHT = 456 OR ITEM_HEIGHT < 789))
The set of items to validate will always be different but they are not a huge number. However, the number of rules is high (let’s say, 100000).
How can I check which rules validated (considering also into account performance) a given set of numbers?
You could use some of Microsoft’s own parsing engine for T-SQL.
You can find them in the assemblies
Microsoft.Data.Schema.ScriptDom.dllandMicrosoft.Data.Schema.ScriptDom.Sql.dll.If you don’t get any errors, the string is a valid filter expression. Though there might be some harmful expressions.
If you wish, you could run the expression trough your own visitor to detect any unwanted constructs (such as sub-queries). But be aware that you would have to override almost all of the 650 overloads, for both
Visit(...)andExplicitVisit(...). Partial classes would be good here.When you are satisfied, could then build a complete
SELECTstatement, with all of the expressions:… and turn it all back into a string:
Output:
If this expression gets too large to handle, you could always split up the rules into chunks, to run a few at the time.
Though, to be allowed to redistribute the
Microsoft.Data.Schema.ScriptDom.*.dllfiles, you have to own a licence of Visual Studio Team System (Is this included in at least VS Pro/Ultimate?)Link: http://blogs.msdn.com/b/gertd/archive/2008/08/22/redist.aspx