Skip to content

Commit

Permalink
Added OR WHERE clause functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinAlpert committed Oct 21, 2018
1 parent b39a9ee commit 7b6faa3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ You can include the Maven dependency:
<dependency>
<groupId>com.github.collinalpert</groupId>
<artifactId>lambda2sql</artifactId>
<version>1.7</version>
<version>1.8</version>
</dependency>
```

Expand Down
10 changes: 1 addition & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.collinalpert</groupId>
<artifactId>lambda2sql</artifactId>
<version>1.7</version>
<version>1.8</version>
<packaging>jar</packaging>

<name>lambda2sql</name>
Expand Down Expand Up @@ -141,14 +141,6 @@
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ default SqlPredicate<T> and(SqlPredicate<? super T> other) {
Objects.requireNonNull(other);
return t -> test(t) && other.test(t);
}

default SqlPredicate<T> or(SqlPredicate<? super T> other) {
Objects.requireNonNull(other);
return t -> test(t) || other.test(t);
}
}
10 changes: 10 additions & 0 deletions src/test/java/com/github/collinalpert/lambda2sql/test/ICar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.collinalpert.lambda2sql.test;

/**
* @author Collin Alpert
*/
public interface ICar {
String getModel();

String getColor();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.collinalpert.lambda2sql.test;

public interface Person {
public interface IPerson {
long getId();

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

boolean isActive();

ICar getCar();

default boolean isAdult() {
return getAge() >= 18;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,46 @@ void testWithVariables() {

@Test
void testFunction() {
assertFunctionEqual("name", Person::getName);
assertFunctionEqual("name", IPerson::getName);
assertFunctionEqual("age", person -> person.getAge());
}

@Test
void testMethodReferences() {
SqlPredicate<Person> person = Person::isAdult;
SqlPredicate<Person> personAnd = person.and(x -> true);
SqlPredicate<IPerson> person = IPerson::isAdult;
SqlPredicate<IPerson> personAnd = person.and(x -> true);
assertPredicateEqual("person.isAdult AND true", personAnd);
}

@Test
void testGetById() {
int id = 1;
SqlPredicate<Person> personPredicate = person -> person.getId() == id;
SqlPredicate<Person> personSqlPredicateAnd = personPredicate.and(x -> true);
void testAndFunction() {
var id = 1;
SqlPredicate<IPerson> personPredicate = person -> person.getId() == id;
SqlPredicate<IPerson> personSqlPredicateAnd = personPredicate.and(x -> true);
assertPredicateEqual("person.id = 1 AND true", personSqlPredicateAnd);
}

private void assertPredicateEqual(String expectedSql, SqlPredicate<Person> p) {
@Test
void testOrFunction() {
var id = 1;
SqlPredicate<IPerson> personPredicate = person -> person.getId() == id;
SqlPredicate<IPerson> personSqlPredicateAnd = personPredicate.or(x -> true);
assertPredicateEqual("person.id = 1 OR true", personSqlPredicateAnd);
}

@Test
void testNestedProperties() {
SqlPredicate<IPerson> p = person -> person.getCar().getModel() == "Mercedes";
var sql = Lambda2Sql.toSql(p, "car");
Assertions.assertEquals("car.model = 'Mercedes'", sql);
}

private void assertPredicateEqual(String expectedSql, SqlPredicate<IPerson> p) {
var sql = Lambda2Sql.toSql(p, "person");
Assertions.assertEquals(expectedSql, sql);
}

private void assertFunctionEqual(String expectedSql, SqlFunction<Person, ?> function) {
private void assertFunctionEqual(String expectedSql, SqlFunction<IPerson, ?> function) {
var sql = Lambda2Sql.toSql(function);
Assertions.assertEquals(expectedSql, sql);
}
Expand Down

0 comments on commit 7b6faa3

Please sign in to comment.