Skip to content

Commit 0d63e50

Browse files
committed
Added support for variables
1 parent 6c7d2c6 commit 0d63e50

File tree

5 files changed

+58
-66
lines changed

5 files changed

+58
-66
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ apply plugin: 'java'
22
apply plugin: 'eclipse'
33
apply plugin: 'maven'
44

5-
sourceCompatibility = 1.8
6-
targetCompatibility = 1.8
5+
sourceCompatibility = 1.9
6+
targetCompatibility = 1.9
77

88
version = '0.1'
99
group = "ajermakovics"
@@ -15,7 +15,7 @@ repositories {
1515
}
1616

1717
dependencies {
18-
compile 'com.trigersoft:jaque:2.0.4'
18+
compile 'com.trigersoft:jaque:2.1.3'
1919
testCompile group: 'junit', name: 'junit', version: '4.+'
2020
}
2121

src/main/java/lambda2sql/Lambda2Sql.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,22 @@
11
package lambda2sql;
22

3-
import static java.lang.System.getProperty;
4-
import static java.nio.file.Files.createTempDirectory;
5-
import static java.util.Objects.requireNonNull;
3+
import com.trigersoft.jaque.expression.LambdaExpression;
64

7-
import java.io.IOException;
85
import java.util.function.Predicate;
96

10-
import com.trigersoft.jaque.expression.LambdaExpression;
11-
127
/**
138
* A utility class for converting java lambdas to SQL.
14-
* It must be initializes with {@link #init()} before any lambdas are created.
159
*/
1610
public class Lambda2Sql {
1711

18-
private static final String DUMP_CLASSES_PROP = "jdk.internal.lambda.dumpProxyClasses";
19-
20-
/**
21-
* Initializes the jdk.internal.lambda.dumpProxyClasses system property with a temporary directory.
22-
* See https://bugs.openjdk.java.net/browse/JDK-8023524
23-
*/
24-
public static void init() {
25-
try {
26-
if( System.getProperty(DUMP_CLASSES_PROP) == null )
27-
System.setProperty(DUMP_CLASSES_PROP, createTempDirectory("lambda").toString());
28-
} catch (IOException e) {
29-
throw new IllegalStateException(e);
30-
}
31-
}
32-
3312
/**
34-
* Converts a predicate lambda to SQL. Make sure to {@link Lamda2Sql#init()} before creating the predicate.<br/>
13+
* Converts a predicate lambda to SQL.
3514
* <pre>{@code person -> person.getAge() > 50 && person.isActive() }</pre>
3615
* Becomes a string:
3716
* <pre>{@code "age > 50 AND active" }</pre>
3817
* Supported operators: >,>=,<,<=,=,!=,&&,||,!
3918
*/
40-
public static <T> String toSql(Predicate<T> predicate) {
41-
requireNonNull(getProperty(DUMP_CLASSES_PROP), "Call init() before creating the predicate.");
19+
public static <T> String toSql(SqlPredicate<T> predicate) {
4220
LambdaExpression<Predicate<T>> lambdaExpression = LambdaExpression.parse(predicate);
4321
return lambdaExpression.accept(new ToSqlVisitor()).toString();
4422
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package lambda2sql;
2+
3+
import java.io.Serializable;
4+
import java.util.function.Predicate;
5+
6+
/**
7+
* @author Collin Alpert
8+
*/
9+
public interface SqlPredicate<T> extends Predicate<T>, Serializable {
10+
}

src/main/java/lambda2sql/ToSqlVisitor.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
package lambda2sql;
22

3-
import static com.trigersoft.jaque.expression.ExpressionType.Equal;
4-
import static com.trigersoft.jaque.expression.ExpressionType.LogicalAnd;
5-
import static com.trigersoft.jaque.expression.ExpressionType.LogicalOr;
6-
73
import com.trigersoft.jaque.expression.*;
84

5+
import java.util.ArrayList;
6+
7+
import static com.trigersoft.jaque.expression.ExpressionType.*;
8+
99
public class ToSqlVisitor implements ExpressionVisitor<StringBuilder> {
1010

1111
private StringBuilder sb = new StringBuilder();
1212
private Expression body;
1313
private ArrayList<ConstantExpression> parameters = new ArrayList<>();
1414

15+
private static String toSqlOp(int expressionType) {
16+
switch (expressionType) {
17+
case Equal:
18+
return "=";
19+
case LogicalAnd:
20+
return "AND";
21+
case LogicalOr:
22+
return "OR";
23+
}
24+
return ExpressionType.toString(expressionType);
25+
}
26+
1527
@Override
1628
public StringBuilder visit(BinaryExpression e) {
1729
boolean quote = e != body && e.getExpressionType() == LogicalOr;
1830

19-
if( quote ) sb.append('(');
31+
if (quote) sb.append('(');
2032

2133
e.getFirst().accept(this);
2234
sb.append(' ').append(toSqlOp(e.getExpressionType())).append(' ');
2335
e.getSecond().accept(this);
2436

25-
if( quote ) sb.append(')');
37+
if (quote) sb.append(')');
2638

2739
return sb;
2840
}
2941

30-
public static String toSqlOp(int expressionType) {
31-
switch(expressionType) {
32-
case Equal: return "=";
33-
case LogicalAnd: return "AND";
34-
case LogicalOr: return "OR";
35-
}
36-
return ExpressionType.toString(expressionType);
37-
}
38-
3942
@Override
4043
public StringBuilder visit(ConstantExpression e) {
44+
if (e.getValue() instanceof String) {
45+
return sb.append("'").append(e.getValue().toString()).append("'");
46+
}
4147
return sb.append(e.getValue().toString());
4248
}
4349

@@ -56,7 +62,9 @@ public StringBuilder visit(LambdaExpression<?> e) {
5662
@Override
5763
public StringBuilder visit(MemberExpression e) {
5864
String name = e.getMember().getName();
59-
name = name.replaceAll("^(get|is)", "").toLowerCase();
65+
name = name.replaceAll("^(get)", "");
66+
name = name.substring(0, 1).toLowerCase() + name.substring(1);
67+
6068
return sb.append(name);
6169
}
6270

@@ -72,4 +80,4 @@ public StringBuilder visit(UnaryExpression e) {
7280
return e.getFirst().accept(this);
7381
}
7482

75-
}
83+
}
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
package lambda2sql;
22

3-
import static org.junit.Assert.assertEquals;
4-
5-
import java.util.function.Predicate;
6-
7-
import org.junit.BeforeClass;
3+
import org.junit.Assert;
84
import org.junit.Test;
95

10-
116
public class Lambda2SqlTest {
127

13-
@BeforeClass
14-
public static void init() throws Exception {
15-
Lambda2Sql.init();
16-
}
17-
188
@Test
19-
public void testComparisons() throws Exception {
9+
public void testComparisons() {
2010
assertEqual("age = 1", e -> e.getAge() == 1);
2111
assertEqual("age > 1", e -> e.getAge() > 1);
2212
assertEqual("age < 1", e -> e.getAge() < 1);
@@ -26,20 +16,26 @@ public void testComparisons() throws Exception {
2616
}
2717

2818
@Test
29-
public void testLogicalOps() throws Exception {
30-
assertEqual("!active", e -> ! e.isActive() );
31-
assertEqual("age < 100 AND height > 200", e -> e.getAge() < 100 && e.getHeight() > 200 );
32-
assertEqual("age < 100 OR height > 200", e -> e.getAge() < 100 || e.getHeight() > 200 );
19+
public void testLogicalOps() {
20+
assertEqual("!isActive", e -> !e.isActive());
21+
assertEqual("age < 100 AND height > 200", e -> e.getAge() < 100 && e.getHeight() > 200);
22+
assertEqual("age < 100 OR height > 200", e -> e.getAge() < 100 || e.getHeight() > 200);
23+
}
24+
25+
@Test
26+
public void testMultipleLogicalOps() {
27+
assertEqual("isActive AND (age < 100 OR height > 200)", e -> e.isActive() && (e.getAge() < 100 || e.getHeight() > 200));
28+
assertEqual("(age < 100 OR height > 200) AND isActive", e -> (e.getAge() < 100 || e.getHeight() > 200) && e.isActive());
3329
}
3430

3531
@Test
36-
public void testMultipleLogicalOps() throws Exception {
37-
assertEqual("active AND (age < 100 OR height > 200)", e -> e.isActive() && (e.getAge() < 100 || e.getHeight() > 200) );
38-
assertEqual("(age < 100 OR height > 200) AND active", e -> (e.getAge() < 100 || e.getHeight() > 200) && e.isActive() );
32+
public void testWithVariables() {
33+
String name = "Donald";
34+
assertEqual("name = 'Donald'", person -> person.getName() == name);
3935
}
4036

41-
private void assertEqual(String expectedSql, Predicate<Person> p) {
37+
private void assertEqual(String expectedSql, SqlPredicate<Person> p) {
4238
String sql = Lambda2Sql.toSql(p);
43-
assertEquals(expectedSql, sql);
39+
Assert.assertEquals(expectedSql, sql);
4440
}
4541
}

0 commit comments

Comments
 (0)