Skip to content

Commit 4979539

Browse files
committed
Added support for more SQL functions.
1 parent f9ab7cb commit 4979539

File tree

7 files changed

+224
-98
lines changed

7 files changed

+224
-98
lines changed

README.MD

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,53 @@ String sql = Lambda2Sql.toSql(predicate); // person.age < 100 AND person.height
3131
How it works
3232
---------
3333

34-
It uses [JaQue](https://github.com/TrigerSoft/jaque) to build an expression tree for a lambda. The expression tree is then traversed and converted to a SQL statement.
35-
36-
Under the hood, JaQue depends on the system property `jdk.internal.lambda.dumpProxyClasses`, if the lambda expression is not serialized:
37-
See [https://bugs.openjdk.java.net/browse/JDK-8023524](https://bugs.openjdk.java.net/browse/JDK-8023524).
38-
39-
When the property is enabled, JVM generated classes for lambdas are saved to disk. JaQue then uses [ASM](http://asm.ow2.org/) to read the .class files and creates expression trees.
40-
41-
Since the functional interfaces included in this project are automatically serialized, there is no need to set this property.
42-
The interfaces ``SqlPredicate<T>`` and ``SqlFunction<T>`` can be used exactly like the original functional interfaces.
34+
It uses [Expressions](https://github.com/CollinAlpert/Expressions) (based
35+
upon [JaQue](https://github.com/TrigerSoft/jaque)) to build an expression tree for a lambda. The expression tree is then
36+
traversed and converted to an SQL WHERE clause.
37+
38+
**The following is only for explanatory purposes. You do not need to set this property anywhere, as long as you use the
39+
interfaces ``SqlPredicate<T>`` and ``SqlFunction<T>``**:\
40+
Under the hood, JaQue depends on the system property `jdk.internal.lambda.dumpProxyClasses`, if the lambda expression is
41+
not serialized:
42+
See [https://bugs.openjdk.java.net/browse/JDK-8023524](https://bugs.openjdk.java.net/browse/JDK-8023524). \
43+
When the property is enabled, JVM generated classes for lambdas are saved to disk. JaQue then
44+
uses [ASM](http://asm.ow2.org/) to read the .class files and creates expression trees.
45+
46+
Since the functional interfaces included in this project are automatically serialized, there is no need to set this
47+
property. The interfaces ``SqlPredicate<T>`` and ``SqlFunction<T>`` can be used exactly like the original functional
48+
interfaces.
4349

4450
Features
4551
---------
4652

47-
Current version works with predicates, functions and supports the following operators: >, >=, <, <=, =, !=, &&, ||, !. The DateTime API introduced in Java 8 is also supported.
53+
Current version works with predicates, functions and supports the following operators: >, >=, <, <=, =, !=, &&, ||, !.
54+
The DateTime API introduced in Java 8 is also supported (`.isBefore()`/`.isAfter()`).
4855

49-
It is also possible to achieve ``LIKE`` operations using the String ``startsWith``, ``endsWith`` and ``contains`` methods.
50-
For example, the lambda expression\
56+
It is also possible to achieve ``LIKE`` operations using the String ``startsWith``, ``endsWith`` and ``contains``
57+
methods. For example, the lambda expression\
5158
``person -> person.getAge() > 18 && person.getName().startsWith("Steve")``\
5259
would translate to:\
5360
``person.age > 18 AND person.name LIKE 'Steve%'``
5461

55-
Lambda2Sql also automatically escapes table names and columns with backticks (\`). If you do not wish this, you can specify it as an argument in the `Lambda2Sql.toSql()` method.
56-
62+
Most common SQL functions are supported as well. For example, ``person -> person.getBirthDate().getYear()`` will
63+
yield `YEAR(person.birthDate)`.\
64+
``person -> SqlFunctions.sum(person.getAge())`` will yield `SUM(person.age)`
65+
66+
Lambda2Sql also automatically escapes table names and columns with backticks (\`). If you do not wish this, you can
67+
specify it as an argument in the `Lambda2Sql.toSql()` method.
68+
5769
Feel free to open an issue with any requests you might have.
5870

5971
Install
6072
-------
6173

6274
You can include the Maven dependency:
75+
6376
```xml
6477
<dependency>
6578
<groupId>com.github.collinalpert</groupId>
6679
<artifactId>lambda2sql</artifactId>
67-
<version>2.2.2</version>
80+
<version>2.4.0</version>
6881
</dependency>
6982
```
7083

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.collinalpert</groupId>
88
<artifactId>lambda2sql</artifactId>
9-
<version>2.3.0</version>
9+
<version>2.4.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>lambda2sql</name>
@@ -61,7 +61,7 @@
6161
<dependency>
6262
<groupId>com.github.collinalpert</groupId>
6363
<artifactId>expressions</artifactId>
64-
<version>2.6.0</version>
64+
<version>2.6.1</version>
6565
</dependency>
6666

6767
<dependency>

src/main/java/com/github/collinalpert/lambda2sql/LinkedListStack.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ private class Node {
8181
/**
8282
* Reference to the next element in the stack.
8383
*/
84-
private Node next;
85-
private T value;
84+
private final Node next;
85+
private final T value;
8686

8787
/**
8888
* Constructor for creating an element with a value and a reference to the next element in the stack.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.collinalpert.lambda2sql;
2+
3+
/**
4+
* @author Collin Alpert
5+
*/
6+
public class SqlFunctions {
7+
8+
public static <T> T sum(T t) {
9+
return null;
10+
}
11+
12+
public static <T> T max(T t) {
13+
return null;
14+
}
15+
16+
public static <T> T min(T t) {
17+
return null;
18+
}
19+
}

0 commit comments

Comments
 (0)