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:
-
If there is no requirement to add dynamic properties to a class, I know I
can use some thing like JoSQL on my collection. -
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 – just few recommendations and inspirations for your design:
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)
There are some interesting designs to look into:
OData API allows users to use RESTful “algebra” to query data using customer expressions OData4J handles mapping
http://code.google.com/p/odata4j/
QueryDSL:
http://blog.mysema.com/2011/01/querying-in-sql-with-querydsl.html
SQLDSL:
http://code.google.com/p/sql-dsl/
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