Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime SQL Query Builder

My question is similar to

Is there any good dynamic SQL builder library in Java?

However one important point taken from above thread:

Querydsl and jOOQ seem to be the most popular and mature choices however there's one thing to be aware of: Both rely on the concept of code generation, where meta classes are generated for database tables and fields. This facilitates a nice, clean DSL but it faces a problem when trying to create queries for databases that are only known at runtime.

Is there any way to create the queries at runtime besides just using plain JDBC + String concatenation?

What I'm looking for is a web application that can be used to build forms to query existing databases. Now if something like that already exists links to such a product would be welcome too.

like image 592
beginner_ Avatar asked Oct 15 '25 04:10

beginner_


2 Answers

While source code generation for database meta data certainly adds much value to using jOOQ, it is not a prerequisite. Many jOOQ users use jOOQ for the same use-case that you envision. This is also reflected in the jOOQ tutorials, which list using jOOQ without code generation as a perfectly valid use-case. For example:

String sql = create.select(
                        fieldByName("BOOK","TITLE"), 
                        fieldByName("AUTHOR","FIRST_NAME"), 
                        fieldByName("AUTHOR","LAST_NAME"))
                   .from(tableByName("BOOK"))
                   .join(tableByName("AUTHOR"))
                   .on(fieldByName("BOOK", "AUTHOR_ID").eq(
                        fieldByName("AUTHOR", "ID")))
                   .where(fieldByName("BOOK", "PUBLISHED_IN").eq(1948))
                   .getSQL();

In a similar fashion, bind values can be extracted from any Query using Query.getBindValues().

This approach will still beat plain JDBC + String concatenation for dynamic SQL statements, as you do not need to worry about:

  • Syntax correctness
  • Cross-database compatibility
  • SQL Injection
  • Bind variable indexing

(Disclaimer: I work for the vendor of jOOQ)

like image 176
Lukas Eder Avatar answered Oct 17 '25 16:10

Lukas Eder


SQLBuilder http://openhms.sourceforge.net/sqlbuilder/ is very useful for me. Some simple examples:

    String query1 = new InsertQuery("table1")
            .addCustomColumn("s01", "12")
            .addCustomColumn("stolbez", 19)
            .addCustomColumn("FIRSTNAME", "Alexander")
            .addCustomColumn("LASTNAME", "Ivanov")
            .toString();

    String query2 = new UpdateQuery("table2")
            .addCustomSetClause("id", 1)
            .addCustomSetClause("FIRSTNAME", "Alexander")
            .addCustomSetClause("LASTNAME", "Ivanov")
            .toString();

Results:

INSERT INTO table1 (s01,stolbez,FIRSTNAME,LASTNAME) VALUES ('12',19,'Alexander','Ivanov')
UPDATE table2 SET id = 1,FIRSTNAME = 'Alexander',LASTNAME = 'Ivanov'
like image 33
lanmaster Avatar answered Oct 17 '25 17:10

lanmaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!