Skip to content

Commit 7b6faa3

Browse files
committed
Added OR WHERE clause functionality
1 parent b39a9ee commit 7b6faa3

6 files changed

Lines changed: 44 additions & 20 deletions

File tree

README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ You can include the Maven dependency:
5656
<dependency>
5757
<groupId>com.github.collinalpert</groupId>
5858
<artifactId>lambda2sql</artifactId>
59-
<version>1.7</version>
59+
<version>1.8</version>
6060
</dependency>
6161
```
6262

pom.xml

Lines changed: 1 addition & 9 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>1.7</version>
9+
<version>1.8</version>
1010
<packaging>jar</packaging>
1111

1212
<name>lambda2sql</name>
@@ -141,14 +141,6 @@
141141
</plugin>
142142
<plugin>
143143
<artifactId>maven-assembly-plugin</artifactId>
144-
<executions>
145-
<execution>
146-
<phase>package</phase>
147-
<goals>
148-
<goal>single</goal>
149-
</goals>
150-
</execution>
151-
</executions>
152144
<configuration>
153145
<descriptorRefs>
154146
<descriptorRef>jar-with-dependencies</descriptorRef>

src/main/java/com/github/collinalpert/lambda2sql/functions/SqlPredicate.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ default SqlPredicate<T> and(SqlPredicate<? super T> other) {
1717
Objects.requireNonNull(other);
1818
return t -> test(t) && other.test(t);
1919
}
20+
21+
default SqlPredicate<T> or(SqlPredicate<? super T> other) {
22+
Objects.requireNonNull(other);
23+
return t -> test(t) || other.test(t);
24+
}
2025
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.collinalpert.lambda2sql.test;
2+
3+
/**
4+
* @author Collin Alpert
5+
*/
6+
public interface ICar {
7+
String getModel();
8+
9+
String getColor();
10+
}

src/test/java/com/github/collinalpert/lambda2sql/test/Person.java renamed to src/test/java/com/github/collinalpert/lambda2sql/test/IPerson.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.collinalpert.lambda2sql.test;
22

3-
public interface Person {
3+
public interface IPerson {
44
long getId();
55

66
String getName();
@@ -11,6 +11,8 @@ public interface Person {
1111

1212
boolean isActive();
1313

14+
ICar getCar();
15+
1416
default boolean isAdult() {
1517
return getAge() >= 18;
1618
}

src/test/java/com/github/collinalpert/lambda2sql/test/Lambda2SqlTest.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,46 @@ void testWithVariables() {
4040

4141
@Test
4242
void testFunction() {
43-
assertFunctionEqual("name", Person::getName);
43+
assertFunctionEqual("name", IPerson::getName);
4444
assertFunctionEqual("age", person -> person.getAge());
4545
}
4646

4747
@Test
4848
void testMethodReferences() {
49-
SqlPredicate<Person> person = Person::isAdult;
50-
SqlPredicate<Person> personAnd = person.and(x -> true);
49+
SqlPredicate<IPerson> person = IPerson::isAdult;
50+
SqlPredicate<IPerson> personAnd = person.and(x -> true);
5151
assertPredicateEqual("person.isAdult AND true", personAnd);
5252
}
5353

5454
@Test
55-
void testGetById() {
56-
int id = 1;
57-
SqlPredicate<Person> personPredicate = person -> person.getId() == id;
58-
SqlPredicate<Person> personSqlPredicateAnd = personPredicate.and(x -> true);
55+
void testAndFunction() {
56+
var id = 1;
57+
SqlPredicate<IPerson> personPredicate = person -> person.getId() == id;
58+
SqlPredicate<IPerson> personSqlPredicateAnd = personPredicate.and(x -> true);
5959
assertPredicateEqual("person.id = 1 AND true", personSqlPredicateAnd);
6060
}
6161

62-
private void assertPredicateEqual(String expectedSql, SqlPredicate<Person> p) {
62+
@Test
63+
void testOrFunction() {
64+
var id = 1;
65+
SqlPredicate<IPerson> personPredicate = person -> person.getId() == id;
66+
SqlPredicate<IPerson> personSqlPredicateAnd = personPredicate.or(x -> true);
67+
assertPredicateEqual("person.id = 1 OR true", personSqlPredicateAnd);
68+
}
69+
70+
@Test
71+
void testNestedProperties() {
72+
SqlPredicate<IPerson> p = person -> person.getCar().getModel() == "Mercedes";
73+
var sql = Lambda2Sql.toSql(p, "car");
74+
Assertions.assertEquals("car.model = 'Mercedes'", sql);
75+
}
76+
77+
private void assertPredicateEqual(String expectedSql, SqlPredicate<IPerson> p) {
6378
var sql = Lambda2Sql.toSql(p, "person");
6479
Assertions.assertEquals(expectedSql, sql);
6580
}
6681

67-
private void assertFunctionEqual(String expectedSql, SqlFunction<Person, ?> function) {
82+
private void assertFunctionEqual(String expectedSql, SqlFunction<IPerson, ?> function) {
6883
var sql = Lambda2Sql.toSql(function);
6984
Assertions.assertEquals(expectedSql, sql);
7085
}

0 commit comments

Comments
 (0)