Parse string to predicate

Posts   
 
    
Fab
User
Posts: 108
Joined: 20-Oct-2008
# Posted on: 26-Feb-2015 16:15:11   

Hello

I would like to know if there is an easy way to convert a string to an IPredicate (or Linq, or QuerySpec expression) ? For example, a parser that takes a string like "MyFirstField=5 AND MySecondField > '2015-02-26'" and return the corresponding IPredicateExpression or something else I can use to query database using an Adapter ?

Thank you for your help Fabrice

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 26-Feb-2015 18:43:16   

This, you have to do. . Important question, would the string come from user? How about the FieldNames, would the string come with exact match of entityField names? How it can be guaranteed?

Fab
User
Posts: 108
Joined: 20-Oct-2008
# Posted on: 26-Feb-2015 19:16:27   

Walaa wrote:

This, you have to do. . Important question, would the string come from user? How about the FieldNames, would the string come with exact match of entityField names? How it can be guaranteed?

The query will be written when configuring the customer environment. It will be written by somebody that know the system and have the knowledge on how to write it, so it'll not be written by the end user. The fieldnames will/must match the generated field name (it's not a problem if it crash when a wrong fieldname is used).

For the performance point of view, the string should be parsed once (on application load or on configuration change) and then reused. I don't expect it to change often.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 27-Feb-2015 13:57:07   

You will need to write the logic/code that digest the text and convert it into predicates.

Fab
User
Posts: 108
Joined: 20-Oct-2008
# Posted on: 27-Feb-2015 14:41:16   

So you say there is nothing that currently exist to parse a query string ?

What do you thing about the Lynq Dynamic Query ? Is it possible to make it works with the llblgen linq provider ? http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 27-Feb-2015 16:53:10   

Fab wrote:

So you say there is nothing that currently exist to parse a query string ?

That's correct, as it's not a trivial problem, but a very complicated one, hence our querysystem without strings.

Why do you need to formulate the query in string form while there are several query systems available to you without the problems related to string based queries?

What do you thing about the Lynq Dynamic Query ? Is it possible to make it works with the llblgen linq provider ? http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library

No.

That system is very limited, it does some rudimentary conversion, but will give up rather quickly if things get more complicated.

Frans Bouma | Lead developer LLBLGen Pro
Fab
User
Posts: 108
Joined: 20-Oct-2008
# Posted on: 27-Feb-2015 17:12:59   

Otis wrote:

That's correct, as it's not a trivial problem, but a very complicated one, hence our querysystem without strings.

Yes indeed everything related to query parsing is always complicated simple_smile

Otis wrote:

Why do you need to formulate the query in string form while there are several query systems available to you without the problems related to string based queries?

I need a system to configure a number of categories, and in each categories configure which items will be defined for this categories. As example, I can take your inbox: you have some categories (Today, Yesterday, This week) and i.e. for the "Today" category the query string will be "ReceiveDate='2015-02-27'". (I know in this sample there is also the complexity of setting the correct current date, but it's not the discussion heresimple_smile ) The goal is to allow our application to handle any categories, which will be defined by the customer, allowing to filter on all available fields

Otis wrote:

That system is very limited, it does some rudimentary conversion, but will give up rather quickly if things get more complicated.

I don't really needs complicated stuff, basic operations are enought

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 28-Feb-2015 06:44:09   

IMHO, you should do this in two separate steps, like Walaa mentioned:

  1. Write a process to digest your query string. This should be in a form of a simple grammar, something that the end-user can actually understand and use (i.e. gmail search keywords). It could be something simple like. At the beginning it should be short and simple. You can use grammar tools out there to help you out here. This phase should receive a string and return a set of LLBLGen predicates, or a syntax error.

  2. Use the actual predicates returned by (1) to run your LLBLGen query.

David Elizondo | LLBLGen Support Team
Fab
User
Posts: 108
Joined: 20-Oct-2008
# Posted on: 02-Mar-2015 01:41:05   

daelmo wrote:

IMHO, you should do this in two separate steps, like Walaa mentioned:

  1. Write a process to digest your query string. This should be in a form of a simple grammar, something that the end-user can actually understand and use (i.e. gmail search keywords). It could be something simple like. At the beginning it should be short and simple. You can use grammar tools out there to help you out here. This phase should receive a string and return a set of LLBLGen predicates, or a syntax error.

  2. Use the actual predicates returned by (1) to run your LLBLGen query.

Yes, my question was about the point #1, just wondering is something like this already exists. Seems not yet.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 02-Mar-2015 07:07:24   

Nothing ready to use that I can think of. But surely out there are grammars that are close to what you are looking for. You can start your own with tools like GoldParser and similar ones.

David Elizondo | LLBLGen Support Team
Fab
User
Posts: 108
Joined: 20-Oct-2008
# Posted on: 02-Mar-2015 10:14:11   

I'm trying with Irony (https://irony.codeplex.com)

I took the original SQL grammar furnished as sample, cleaned up a lot to only handle simple query. Finally I parse the tree and create predicate. After only a few hours of work I can retrieve an IPredicateExpression (and ISortExpression, and list of selected fields) from query like this

select f1, f2
where f1 > 5 and myfunc(f2) = 'test' and f3+f4 <> 5
order by f4, f5

Which is more than I expected when I started. I'm not sure I need the order by but I was in the move simple_smile

Of course it's not yet tested in real situation..

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 02-Mar-2015 20:20:01   

Glad this worked out for you.