Skip to content

jguest/petrol

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Petrol

Because maintaining strings of SQL leads to 💩

Warning: this library is under active development and is not ready for production use.

Install

Maven + Jitpack:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.github.jguest</groupId>
        <artifactId>petrol</artifactId>
        <version>v0.1.0</version>
        <exclusions>
            <exclusion>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
                <groupId>org.hibernate.javax.persistence</groupId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

Use It

As a query builder

new Query().select("*").from("table").toPlainString();

will produce the following query:

SELECT * FROM table

"Oh, that's even longer than writing the SQL! Why would you want that?"

Oh, because:

String sql = "select s.* " +
   "from flarbs f " + // <-- eww multiline concatenation
   "inner join scrabs s on f.id = s.flarbs_id " + // <-- accidentally miss a space and you die
   "where f.teacher_type_id = 42";

😳 🔫

And that was the neatest string of SQL I could find.

Here's the same query built with petrol:

String sql = new Query()
   .select("s.*")
   .from("flarbs f", table ->
      table.innerJoin("scrabs s").on("f.id = s.flarbs_id"))
   .where("f.teacher_type_id = 42")
   .toPlainString();

"That's nice, but why not use an ORM?" I like ORMs for writes but prefer plain ol' SQL for reads.

As a JPA interface

Instantiate Petrol, bringing your own javax.persistence.EntityManager. Petrol's #stream method accepts a class (entity) and query function and will return a java.util.stream.Stream. Use built in Java 8 methods to act on the result. Here's a complete example:

// instantiate petrol
Petrol db = new Petrol(myEntityManager);

// build the query and execute it
db.stream(Sloth.class, query ->
   query.select("*")
      .from("sloths")
      .where("s.name = 'Kristen Bell'")
   )
   .findFirst() // Optional<Sloth>

Incoming Features

  • parameter substitution
  • better support for sub-queries
  • group by
  • count
  • distinct
  • better documentation

About

java 8 query builder

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages