Skip to content

Commit

Permalink
Fix issue with like
Browse files Browse the repository at this point in the history
  • Loading branch information
xjodoin committed Oct 11, 2019
1 parent 4fd2282 commit 3c20b87
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ public OnGoingLogicalCondition notLike(String notLikeValue) {

private LikeCondition createLike(Type type, String toMatch) {
if (notLike) {
return new NotLikeCondition(type, selector, toMatch);
return new NotLikeCondition(selector, selector.generateParameter(type.wrap(toMatch)));
} else {
return new LikeCondition(type, selector, toMatch);
return new LikeCondition(selector, selector.generateParameter(type.wrap(toMatch)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@
*/
package org.torpedoquery.jpa.internal.conditions;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.torpedoquery.jpa.internal.Condition;
import org.torpedoquery.jpa.internal.Parameter;
import org.torpedoquery.jpa.internal.Selector;

public class LikeCondition implements Condition {
public class LikeCondition<T> extends SingleParameterCondition<T> {

public static enum Type {
ANY {
Expand Down Expand Up @@ -58,51 +53,24 @@ public String wrap(String toMatch) {
public abstract String wrap(String toMatch);
}

private final String toMatch;
private final Type type;
private final Selector selector;

/**
* <p>
* Constructor for LikeCondition.
* </p>
*
* @param type
* a
* {@link org.torpedoquery.jpa.internal.conditions.LikeCondition.Type}
* object.
* @param selector
* a {@link org.torpedoquery.jpa.internal.Selector} object.
* @param toMatch
* a {@link java.lang.String} object.
* @param type a
* {@link org.torpedoquery.jpa.internal.conditions.LikeCondition.Type}
* object.
* @param selector a {@link org.torpedoquery.jpa.internal.Selector} object.
* @param toMatch a {@link java.lang.String} object.
*/
public LikeCondition(Type type, Selector selector, String toMatch) {
this.type = type;
this.selector = selector;
this.toMatch = toMatch;
public LikeCondition(Selector selector, Parameter<T> parameter) {
super(selector, parameter);
}

/** {@inheritDoc} */
@Override
public String createQueryFragment(AtomicInteger incrementor) {
return selector.createQueryFragment(incrementor) + " " + getLike() + " '" + type.wrap(toMatch) + "' ";
}

/**
* <p>
* getLike.
* </p>
*
* @return a {@link java.lang.String} object.
*/
protected String getLike() {
protected String getComparator() {
return "like";
}

/** {@inheritDoc} */
@Override
public List<Parameter> getParameters() {
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
*/
package org.torpedoquery.jpa.internal.conditions;

import org.torpedoquery.jpa.internal.Parameter;
import org.torpedoquery.jpa.internal.Selector;
public class NotLikeCondition extends LikeCondition {
public class NotLikeCondition<T> extends LikeCondition<T> {

/**
* <p>Constructor for NotLikeCondition.</p>
Expand All @@ -29,14 +30,13 @@ public class NotLikeCondition extends LikeCondition {
* @param selector a {@link org.torpedoquery.jpa.internal.Selector} object.
* @param toMatch a {@link java.lang.String} object.
*/
public NotLikeCondition(Type type, Selector selector, String toMatch) {
super(type, selector, toMatch);
public NotLikeCondition(Selector selector, Parameter<T> parameter) {
super(selector,parameter);
}

/** {@inheritDoc} */
@Override
protected String getLike() {
return "not " + super.getLike();
protected String getComparator() {
return "not " + super.getComparator();
}

}
9 changes: 6 additions & 3 deletions src/test/java/org/torpedoquery/jpa/StringFunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,26 @@ public void testWhereWithStringFunction() {
Entity from = from(Entity.class);
where(lower(from.getCode())).like().any("test");
Query<Entity> select = select(from);
assertEquals("select entity_0 from Entity entity_0 where lower(entity_0.code) like '%test%'", select.getQuery());
assertEquals("select entity_0 from Entity entity_0 where lower(entity_0.code) like :function_1", select.getQuery());
assertEquals("%test%", select.getParameters().get("function_1"));
}

@Test
public void testWhereWithLikeFunction() {
Entity from = from(Entity.class);
where(lower(from.getCode())).like("%test%");
Query<Entity> select = select(from);
assertEquals("select entity_0 from Entity entity_0 where lower(entity_0.code) like '%test%'", select.getQuery());
assertEquals("select entity_0 from Entity entity_0 where lower(entity_0.code) like :function_1", select.getQuery());
assertEquals("%test%", select.getParameters().get("function_1"));
}

@Test
public void testWhereWithNotLikeFunction() {
Entity from = from(Entity.class);
where(lower(from.getCode())).notLike("%test%");
Query<Entity> select = select(from);
assertEquals("select entity_0 from Entity entity_0 where lower(entity_0.code) not like '%test%'", select.getQuery());
assertEquals("select entity_0 from Entity entity_0 where lower(entity_0.code) not like :function_1", select.getQuery());
assertEquals("%test%", select.getParameters().get("function_1"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void testLowerFunctionInCondition() {
where(condition);
Query<Entity> select = select(entity);
assertEquals(
"select entity_0 from Entity entity_0 where ( lower(entity_0.code) like '%test%' )",
"select entity_0 from Entity entity_0 where ( lower(entity_0.code) like :function_1 )",
select.getQuery());
}

Expand Down
18 changes: 12 additions & 6 deletions src/test/java/org/torpedoquery/jpa/WhereClauseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ public void test_like_any() {
where(from.getCode()).like().any("test");
Query<Entity> select = select(from);

assertEquals("select entity_0 from Entity entity_0 where entity_0.code like '%test%'", select.getQuery());
assertEquals("select entity_0 from Entity entity_0 where entity_0.code like :code_1", select.getQuery());
assertEquals("%test%", select.getParameters().get("code_1"));
}

/**
Expand All @@ -355,7 +356,8 @@ public void test_notLike_any() {
where(from.getCode()).notLike().any("test");
Query<Entity> select = select(from);

assertEquals("select entity_0 from Entity entity_0 where entity_0.code not like '%test%'", select.getQuery());
assertEquals("select entity_0 from Entity entity_0 where entity_0.code not like :code_1", select.getQuery());
assertEquals("%test%", select.getParameters().get("code_1"));
}

/**
Expand All @@ -367,7 +369,8 @@ public void test_like_startsWith() {
where(from.getCode()).like().startsWith("test");
Query<Entity> select = select(from);

assertEquals("select entity_0 from Entity entity_0 where entity_0.code like 'test%'", select.getQuery());
assertEquals("select entity_0 from Entity entity_0 where entity_0.code like :code_1", select.getQuery());
assertEquals("test%", select.getParameters().get("code_1"));
}

@Test
Expand All @@ -376,7 +379,8 @@ public void test_notLike_startsWith() {
where(from.getCode()).notLike().startsWith("test");
Query<Entity> select = select(from);

assertEquals("select entity_0 from Entity entity_0 where entity_0.code not like 'test%'", select.getQuery());
assertEquals("select entity_0 from Entity entity_0 where entity_0.code not like :code_1", select.getQuery());
assertEquals("test%", select.getParameters().get("code_1"));
}

/**
Expand All @@ -388,7 +392,8 @@ public void test_like_endsWith() {
where(from.getCode()).like().endsWith("test");
Query<Entity> select = select(from);

assertEquals("select entity_0 from Entity entity_0 where entity_0.code like '%test'", select.getQuery());
assertEquals("select entity_0 from Entity entity_0 where entity_0.code like :code_1", select.getQuery());
assertEquals("%test", select.getParameters().get("code_1"));
}

/**
Expand All @@ -400,7 +405,8 @@ public void test_notLike_endsWith() {
where(from.getCode()).notLike().endsWith("test");
Query<Entity> select = select(from);

assertEquals("select entity_0 from Entity entity_0 where entity_0.code not like '%test'", select.getQuery());
assertEquals("select entity_0 from Entity entity_0 where entity_0.code not like :code_1", select.getQuery());
assertEquals("%test", select.getParameters().get("code_1"));
}

/**
Expand Down

0 comments on commit 3c20b87

Please sign in to comment.