0

I have the following requirement, I need to expose an API where the user can send a free form 'query' like expression and I need to return true/false. ex: For a car object, if the query is

" (make = 'FORD' AND year IN (1990,1991)) OR type = 'SUV') ".

However, 'make' 'year' or 'type' comes from a table which is populated externally.

TABLE CAR_PROPERTIES (
 propertyName VARCHAR2(40),
 propertyValue VARCHAR2(10)
)

There are other join tables which I traverse first to load the properties.

So, if there is a new property (and corresponding value), I need to support in the query.

As of now, this is what I did:

  1. If there is no requirement to add dynamic properties to a class, I know I can use some thing like JoSQL on my collection.

  2. I looked into creating the class dynamically once the application starts using Java Tools API or javaassist.

Client is insisting on a solution which doesn't require adding new properties and deploying the code (though the 'caller' of this API does need code changes in order to use the new properties in the query).

I don't like creating new Class dynamically. Looking for any pointers or solutions.

Raj
  • 42
  • 6

1 Answers1

2

Raj - just few recommendations and inspirations for your design:

  1. Would not even do a full O/R mapping. I would considering translating your domain specific language into SQL. I've done such a thing in Python. It is much easier because you are doing language-to-language translation between two easy languages (custom DSL and SQL). With robust regular expressions you can have a very concise and powerful framework for handling this kinds of interactions. (see Regex Table Lexer and other patterns)

  2. There are some interesting designs to look into:

  3. For dynamic relationship and property expressions I suggest (embedded) graph data structures Here is a helpful tutorial that shows rel-to-graph mappings:
    http://py2neo.org/tutorials/tables_to_graphs
    and just a tutorial on Neo4J which I use for similar purposes:
    http://docs.neo4j.org/chunked/stable/tutorials.html

Edmon
  • 4,752
  • 4
  • 32
  • 42
  • Thx for quick reply. I am not trying to do any ORM and I can ask the users to follow any syntax for the query. My main problem is how to create java objects which can be queried. Specifically, I have car class, but it doesn't have 'make' , 'year' as properties when the application starts. These come from a table which I load. – Raj Aug 02 '12 at 02:01
  • 1
    I see. Not trying to get too fancy here :-) but I would then consider a graph-like structure, perhaps embedded Neo4J or simpler, where you can add some attributes after the fact, have aliases, etc and query these. Maybe too buys but also gives you flexibility: http://stackoverflow.com/questions/1754628/graph-database-in-java-other-than-neo4j – Edmon Aug 02 '12 at 02:11
  • 1
    Here are some examples similar to what you are looking for: http://py2neo.org/tutorials/tables_to_graphs – Edmon Aug 02 '12 at 02:13