Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expression equality in the Criteria API #1003

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ public PropertyMetamodel<?> argument() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AbstractFunction)) return false;
if (o == null || getClass() != o.getClass()) return false;
AbstractFunction<?> that = (AbstractFunction<?>) o;
return name.equals(that.name) && argument.equals(that.argument);
}
@@ -124,6 +124,20 @@ public void accept(PropertyMetamodel.Visitor visitor) {
v.visit(this);
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Count count = (Count) o;
return distinct == count.distinct;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), distinct);
}
}

class Max<PROPERTY> extends AbstractFunction<PROPERTY> {
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ public Class<?> asClass() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AbstractArithmeticExpression)) return false;
if (o == null || getClass() != o.getClass()) return false;
AbstractArithmeticExpression<?> that = (AbstractArithmeticExpression<?>) o;
return propertyMetamodel.equals(that.propertyMetamodel)
&& left.equals(that.left)
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ public Class<?> asClass() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof StringExpression.OneArgumentStringExpression)) return false;
if (o == null || getClass() != o.getClass()) return false;
OneArgumentStringExpression<?> that = (OneArgumentStringExpression<?>) o;
return argument.equals(that.argument);
}
@@ -80,7 +80,7 @@ public Class<?> asClass() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof StringExpression.TwoArgumentsStringExpression)) return false;
if (o == null || getClass() != o.getClass()) return false;
TwoArgumentsStringExpression<?> that = (TwoArgumentsStringExpression<?>) o;
return propertyMetamodel.equals(that.propertyMetamodel)
&& first.equals(that.first)
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
import org.junit.jupiter.api.Test;
import org.seasar.doma.DomaIllegalArgumentException;
import org.seasar.doma.jdbc.criteria.entity.Emp_;
import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel;

class ExpressionsTest {

@@ -229,4 +230,41 @@ void testSum() {
assertEquals("sum", sum(e.id).getName());
assertEquals(e.id, sum(e.id).argument());
}

@Test
void testEquality() {
PropertyMetamodel<?> sum = sum(e.id);
PropertyMetamodel<?> div = div(sum(e.id), 1);
PropertyMetamodel<?> mod = mod(sum(e.id), 1);
PropertyMetamodel<?> mul = mul(sum(e.id), 1);
PropertyMetamodel<?> sub = sub(sum(e.id), 1);
PropertyMetamodel<?> add = add(sum(e.id), 1);

assertEquals(sum, sum(e.id));
assertEquals(div, div(sum(e.id), 1));
assertEquals(mod, mod(sum(e.id), 1));
assertEquals(mul, mul(sum(e.id), 1));
assertEquals(sub, sub(sum(e.id), 1));
assertEquals(add, add(sum(e.id), 1));

assertNotEquals(sum, div);
assertNotEquals(sum, mod);
assertNotEquals(sum, mul);
assertNotEquals(sum, sub);
assertNotEquals(sum, add);

assertNotEquals(div, mod);
assertNotEquals(div, mul);
assertNotEquals(div, sub);
assertNotEquals(div, add);

assertNotEquals(mod, mul);
assertNotEquals(mod, sub);
assertNotEquals(mod, add);

assertNotEquals(mul, sub);
assertNotEquals(mul, add);

assertNotEquals(sub, add);
}
}