Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to write a Query Language Parser from C#
Query string should be something like this.

Input:
C#
string query = "Site = 'Location 1'

Output:
SQL
Select * from Table1
where TagName = 'Site' 
and TagValue = 'Location 1'

Is there any dll, to this or How could do something like this. Please help me someone. Thanks

What I have tried:

I have tried that by reading each character and checking each character with the operators given.
I know thats wrong.
Posted
Updated 2-Nov-20 13:34pm
v2
Comments
PIEBALDconsult 2-Nov-20 22:45pm    
I'd start with Regular Expressions and then open a bottle of tequila.
Parsing the pieces and using them to set parameter values may be all you require -- and avoid the injection Griff mentioned.
Other than that, you haven't provided enough information to assist further.

What you are trying to do is pretty complicated, and is going to take a fair amount of work - mostly because the query you are processing contains hardly any information, and the rest (table name, column names, and so forth) has to either be inferred from the information you are given, or hard coded, which will result in an inefficient and inflexible database design.

And as the complexity of the query you want to process rises, the complexity of the output query increases exponentially. That doesn't mean it's an impossible task, but it's is decidedly non-trivial!

Start by reading up on tokenization and expression parsing, define your "input language" in good detail, and then start writing a parser for it. When you have that validated, you can start on the SQL expression generation side of things, which is going to be even less trivial - particularly if you want your DB to remain undamaged. The damage problem comes from SQL Injection, which your current output query is wide open to - you need to consider how to prevent any form of string concatenation and using parameterised queries here, or your DB is going to get deleted pretty quickly!

Me? I'd say that the effort involved doesn't warrant the risk you introduce: I would abandon the project!
 
Share this answer
 
Go to: https://www.codeproject.com/Articles/code-witch#Article[^] and look at the section under "Parsers". Honey has written some brilliant articles on the subject.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900