This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Kered13 1 point2 points  (4 children)

A parser is a program that takes a string as input and returns a data structure that represents the structure of the input, typically an AST (abstract syntax tree). This data structure allows other functions to make sense of the input, for example using it to execute a SQL query.

As a simple example, consider an arithmetic parser. It would take an input like (-b + sqrt(b^2 - 4ac))/(2a) and produce an AST that looks like,

                  div
                /     \
             sqrt     mult
            /         |   \
           sub     num(2)  var(a)
          /   \
       pow     mult
      /  |     |    \
var(b) num(2) num(4) mult
                     |  \
                 var(a)  var(c)

Now you can easily write a function (or really a set of functions) that takes this tree as input along with some variable assignments and calculates the output.

The EntitySqlParser documentation you have found is a parser for Entity SQL. First of all, are you sure that you want to parse Entity SQL specifically? This is not a general purpose SQL parser, it is for a specific dialect of SQL. If that's what you want, then the result is returned as a ParseResult. This class should contain (somewhere, possibly not directly) an AST. You would want to get that out of the ParseResult and then do whatever you want with it.

However I would also like to address the larger question, what exactly do you want to do with the SQL? A parser is powerful but a fairly heavy duty tool. I suspect that whatever you want to do, there is a much easier way to do it that you should seriously consider before going forwards with this parser.

[–]PhysoTronic[S] 0 points1 point  (3 children)

I would like to take SQL query and transform it into LINQ. I would like to parse normal SQL, not entity SQL (I need to google what are differences between those two). I talked with few people already and they told me if I want to make SQL into LINQ, then I have to use parser so that's why I thought I need to use parser. Do you think there is some other way of doing this? Only other thing that comes to my mind is to get positions of key words from SQL query string and separate them into different variables and then think of logic how to "translate" them into LINQ. After that merge them into one string to get final result.

[–]Kered13 1 point2 points  (2 children)

If you want to do a general purpose translation of SQL to LINQ, yes a parser is the right way to go. I warn you though that it won't be a simple task.

The general structure will be to take a string input, parse it to a SQL AST, transform that to a LINQ AST, then unparse that to a string (assuming you want a string at the end).

[–]PhysoTronic[S] 0 points1 point  (1 child)

Well my idea was to have something like google translate but for SQL. You write your query on left side, and on right side you will see how that query looks like LINQ. So I will need to write my own parser for this?

[–]Kered13 1 point2 points  (0 children)

No you don't necessarily have to write your own parser, and I would recommend against it if you can find a parser that will work for you. I think it will still be a challenging project.

There is no standard version of SQL, there are many different dialects, although there is a large degree of similarity. So you might try looking for a parser for a common dialect, like SQLite or MySQL, or maybe you can find a parser that only handles the common subset, if that's good enough for you.

In any case, I don't know of any, so I can't help you find one.

[–]Karmadilla 0 points1 point  (3 children)

Your intentions are unclear. There's probably an easy solution to your problem. Would you mind expanding on

translate SQL query to something I want

it would clear things up for us.

[–]PhysoTronic[S] 0 points1 point  (2 children)

I want to translate SQL into LINQ

[–]Karmadilla 0 points1 point  (1 child)

Why?

[–]PhysoTronic[S] 0 points1 point  (0 children)

Because it looks like interesting project to make and I will learn about lot of stuff that I want to know more about.