Skip to content

Convert Java 8 lambdas to SQL statements. Build type safe and readable queries.

License

Notifications You must be signed in to change notification settings

CollinAlpert/lambda2sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lambda2sql (lambda) -> "sql"

Release

Convert Java 8 lambdas to SQL statements.

For example, the following Predicate:

 person -> person.getAge() < 100 && person.getHeight() > 200

is converted to a string:

  age < 100 AND height > 200

allowing you to write readable queries in a type safe way.

See Lambda2SqlTest for more examples or the Wiki.

Usage

Lambda2Sql.init(); // initialize on program start, before predicates are created

Predicate<Person> predicate = person -> person.getAge() < 100 && person.getHeight() > 200;

String sql = Lambda2Sql.toSql( predicate ); // age < 100 AND height > 200

How it works

It uses JaQue to build an expression tree for a lambda. The expression tree is then traversed and converted to a SQL statement.

Under the hood JaQue depends on the following system property: jdk.internal.lambda.dumpProxyClasses See https://bugs.openjdk.java.net/browse/JDK-8023524.

When the property is enabled, JVM generated classes for lambdas are saved to disk. JaQue then uses ASM to read the .class files and creates expression trees.

Limitations

Current version only works with predicates and supports the following operators: >, >=, <, <=, =, !=, &&, ||, !

Install

You can get the library using JitPack https://jitpack.io/#ajermakovics/lambda2sql/v0.1

repositories {
    maven { url "https://jitpack.io" }
}
dependencies {
    compile 'com.github.ajermakovics:lambda2sql:v0.1'
}	

Build

gradle jar or gradle fatjar to include dependencies.

or you can just download the fat jar.

About

Convert Java 8 lambdas to SQL statements. Build type safe and readable queries.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%