I have the following problem: We have several different databases on which our program runs, so the exact database-structure is unknown during compile-time. We are now searching for a library which allows us to build query-objects, that can be translated to several SQL-dialects and gives us a plain Query-String.
We search for something similar to regular ORM-Libraries such as Hibernate. We’ve looked into some of them, but it seems that all of them except tables and columns to be defined at compile-time as a regular java-class. If I’m wrong on this, please correct me.
The basic difference between the ORM approach and ours is, that we don’t want to store java-objects in databases, but to generate queries as java-objects. To make my point more clear, I will show you some example code, that should be executable by the library.
QueryObject query = new QueryObject();
query.addSelectedTable("customers");
query.addSelectedColumn("customerID");
query.addSelectedColumn("firstname");
query.addCondition("age < 20");
query.addLimit(10);
System.out.println(query.toMySQL());
//prints SELECT customerID, firstname FROM customers WHERE age < 20 LIMIT 10;
I don’t know if such a library exists, but if you know one, I would be happy to know about it.
To be more demanding, there are several other features, that would be great. For example, it should also be able to handle subqueries and other query-types as SELECT. But that would be too much to expect 😉
Have you checked out Squiggle-SQL?