fmbenhassine / jql

Java code analysis and linting with SQL
https://github.com/benas/jql/wiki
MIT License
157 stars 11 forks source link

Embedded DSL / API #1

Open hrj opened 8 years ago

hrj commented 8 years ago

This is very interesting; thanks for making it.

Would it make sense to expose an API to get at the data? I would like to side-step SQL if possible.

fmbenhassine commented 8 years ago

Thank you! There is a work in progress (use-jooq branch) to add support for JOOQ. This will add support for a java DSL to query the database. Does this might help you?

In all cases, there is already an OO model for the database. You can use one of the DAOs and query the datasource with Java. I can give an example if needed.

fmbenhassine commented 8 years ago

Hi @hrj

I can give an example if needed.

Here is an example with JOOQ

        String url = "jdbc:sqlite:/path/to/jcql.db";
        try(Connection connection = DriverManager.getConnection(url)) {
            DSLContext create = DSL.using(connection, SQLDialect.SQLITE);
            Result<Record> result = create.select().from("class").fetch();
            for (Record r : result) {
                Integer id = (Integer) r.getValue("id");
                String name = (String) r.getValue("name");
                System.out.println("id: " + id + " name: " + name);
            }
        }
lukaseder commented 8 years ago

Oh, excellent! Now you could also use jOOQ's code generator, because your schema is fixed, and profit from type safety. This would turn your code snippet into:

        String url = "jdbc:sqlite:/path/to/jcql.db";
        try(Connection connection = DriverManager.getConnection(url)) {
            DSLContext create = DSL.using(connection, SQLDialect.SQLITE);
            for (ClassRecord r : create.selectFrom(CLASS)) {
                Integer id = r.getId();
                String name = r.getName();
                System.out.println("id: " + id + " name: " + name);
            }
        }
hrj commented 8 years ago

Thanks for the replies. I did have half a mind of using JOOQ initially :) I am now convinced.

Going further, what if the project used a pure Java embedded DB like H2? Apart from ease of portability, H2 has an in-memory database mode, so that would allow JCQL to be use as a side-effect free library.

fmbenhassine commented 8 years ago

what if the project used a pure Java embedded DB like H2?

It's possible to use an in-memory database, but how would you analyse the code afterwards? The goal is to create a reporting database about the code that you can analyse offline, just like other reports generated by other static analysis tools. The choice of Sqlite was made to be able to have a one-file database that is easy to share (see comment here).

that would allow JCQL to be use as a side-effect free library.

The tool is side effect free. It's read only on your code. I guess by side-effect you mean the file jcql.db created each time. If you use the maven plugin, this file will be generated in the target directory, so it will be safely deleted like other generated artefacts. The target db directory is configurable, you can set it to /tmp for instance or any other directory of your choice.