The conversion from the query to sql is a two step process.
Step 1: Parse the query into an object graph. This is so that multiple implementations of the repository don’t all have to figure out how to do this; Noodb converts the query to a common format, and each repository can do as it needs.
Step 2: Convert the common graph into Sql.
Step 2 is easier. It just has to walk the graph and output sql as it goes. No big deal for the simple queries I’m starting with.
Step 1 is a lot more difficult. I’m working through the code, and it will work, but I’m not going to be happy with it on this pass. I’m manually parsing the string; there has to be a more elegant way of doing this. Unfortunately, I don’t have any experience to draw on in this case.
At this point, I’m working on parsing single-object queries. I’m only concerned with the where clause With those constraint in mind, step 2 is working.
Here’s a piece of test code:
Query simpleQuery2 = new NoodbQueryParser().Parse("from p in QueryPerson where p.LastName='Allard' && p.FirstName='Jay' || p.MiddleInitial='M'"); string simpleText2 = new SqlQueryConverter().Convert(simpleQuery2);
The resulting text:
( [p].[LastName] = 'Allard' And [p].[FirstName] = 'Jay' Or [p].[MiddleInitial] = 'M' )
Pretty simple. Again, Step 1 isn’t complete, it only does the simple stuff. But, if you skip step 1 (which convers the text to a query object), and just create the query object yourself, then you see that step 2 is more mature.
( [p].[LastName] = 'Allard' Or ( [p].[FirstName] = 'Santa' And [p].[LastName] = 'Claus' ) And [p].[random] = 'blah' )
The sql shown is the actual generated sql; I’m not reformatting it. It’s generated with the tabbing to make it more readable. If that proves to be detrimental to performace, I’ll make the formatting optional.