mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
This commit is contained in:
1 parent
b300db77ff
commit
f6b87ed0b4
4 files changed
+120
No files matched your search
@@ -83,6 +83,17 @@ public abstract class PBaseValueEqual<R, T> extends TQPropertyBase<R> {
|
|||||||
return _root;
|
return _root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is equal to another property.
|
||||||
|
*
|
||||||
|
* @param other the other property to compare
|
||||||
|
* @return the root query bean instance
|
||||||
|
*/
|
||||||
|
public final R eq(TQProperty<?> other) {
|
||||||
|
expr().raw(_name + " = " + other.propertyName());
|
||||||
|
return _root;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is equal to if value is non-null and otherwise no expression is added to the query.
|
* Is equal to if value is non-null and otherwise no expression is added to the query.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -135,6 +146,17 @@ public abstract class PBaseValueEqual<R, T> extends TQPropertyBase<R> {
|
|||||||
return _root;
|
return _root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is not equal to another property.
|
||||||
|
*
|
||||||
|
* @param other the other property to compare
|
||||||
|
* @return the root query bean instance
|
||||||
|
*/
|
||||||
|
public final R ne(TQProperty<?> other) {
|
||||||
|
expr().raw(_name + " <> " + other.propertyName());
|
||||||
|
return _root;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is in a list of values.
|
* Is in a list of values.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -693,6 +693,22 @@ public abstract class TQRootBean<T, R> {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add EXISTS sub-query predicate.
|
||||||
|
*/
|
||||||
|
public R exists(Query<?> subQuery) {
|
||||||
|
query.where().exists(subQuery);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add NOT EXISTS sub-query predicate.
|
||||||
|
*/
|
||||||
|
public R notExists(Query<?> subQuery) {
|
||||||
|
query.where().notExists(subQuery);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute using "for update" clause which results in the DB locking the record.
|
* Execute using "for update" clause which results in the DB locking the record.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -155,6 +155,39 @@ public class QCustomerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void equalTo_byProperty() {
|
||||||
|
Query<Customer> query = new QCustomer()
|
||||||
|
.select(QCustomer.Alias.id)
|
||||||
|
.billingAddress.city.eq(QCustomer.Alias.shippingAddress.city)
|
||||||
|
.query();
|
||||||
|
|
||||||
|
query.findList();
|
||||||
|
String generatedSql = query.getGeneratedSql();
|
||||||
|
|
||||||
|
// select t0.id from be_customer t0
|
||||||
|
// left join o_address t2 on t2.id = t0.shipping_address_id
|
||||||
|
// left join o_address t1 on t1.id = t0.billing_address_id
|
||||||
|
// where t1.city = t2.city
|
||||||
|
assertThat(generatedSql).contains("where t1.city = t2.city");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void notEqual_byProperty() {
|
||||||
|
Query<Customer> query = new QCustomer()
|
||||||
|
.select(QCustomer.Alias.id)
|
||||||
|
.billingAddress.city.ne(QCustomer.Alias.shippingAddress.city)
|
||||||
|
.query();
|
||||||
|
|
||||||
|
query.findList();
|
||||||
|
String generatedSql = query.getGeneratedSql();
|
||||||
|
|
||||||
|
// select t0.id from be_customer t0
|
||||||
|
// left join o_address t2 on t2.id = t0.shipping_address_id
|
||||||
|
// left join o_address t1 on t1.id = t0.billing_address_id
|
||||||
|
// where t1.city <> t2.city
|
||||||
|
assertThat(generatedSql).contains("where t1.city <> t2.city");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isEmpty() {
|
public void isEmpty() {
|
||||||
|
|||||||
@@ -2,12 +2,15 @@ package org.querytest;
|
|||||||
|
|
||||||
import io.ebean.DB;
|
import io.ebean.DB;
|
||||||
import io.ebean.FetchGroup;
|
import io.ebean.FetchGroup;
|
||||||
|
import io.ebean.Query;
|
||||||
import io.ebean.test.LoggedSql;
|
import io.ebean.test.LoggedSql;
|
||||||
import org.example.domain.Customer;
|
import org.example.domain.Customer;
|
||||||
import org.example.domain.Order;
|
import org.example.domain.Order;
|
||||||
|
import org.example.domain.OrderDetail;
|
||||||
import org.example.domain.otherpackage.PhoneNumber;
|
import org.example.domain.otherpackage.PhoneNumber;
|
||||||
import org.example.domain.query.QCustomer;
|
import org.example.domain.query.QCustomer;
|
||||||
import org.example.domain.query.QOrder;
|
import org.example.domain.query.QOrder;
|
||||||
|
import org.example.domain.query.QOrderDetail;
|
||||||
import org.junit.jupiter.api.AfterAll;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -214,6 +217,52 @@ public class QOrderTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void subQueryExists() {
|
||||||
|
Query<OrderDetail> subQuery = new QOrderDetail()
|
||||||
|
.alias("od")
|
||||||
|
.orderQty.gt(10)
|
||||||
|
.id.eq(QOrder.Alias.details.id)
|
||||||
|
//.where().raw("details.id = od.id")
|
||||||
|
.query();
|
||||||
|
|
||||||
|
Query<Order> query = new QOrder()
|
||||||
|
.exists(subQuery)
|
||||||
|
.orderBy("orderDate").query();
|
||||||
|
|
||||||
|
query.findList();
|
||||||
|
|
||||||
|
// select distinct t0.id, t0.status, t0.order_date, t0.ship_date, t0.version, t0.when_created, t0.when_updated, t0.customer_id, t0.shipping_address_id, t0.order_date
|
||||||
|
// from o_order t0
|
||||||
|
// left join o_order_detail t1 on t1.order_id = t0.id
|
||||||
|
// where exists (select 1 from o_order_detail od where od.order_qty > ? and od.id = t1.id) order by t0.order_date
|
||||||
|
String sql = query.getGeneratedSql();
|
||||||
|
assertThat(sql).contains(" exists (select 1 from o_order_detail od where od.order_qty > ? and od.id = t1.id)");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void subQueryNotExists() {
|
||||||
|
Query<OrderDetail> subQuery = new QOrderDetail()
|
||||||
|
.alias("od")
|
||||||
|
.orderQty.gt(10)
|
||||||
|
.id.eq(QOrder.Alias.details.id)
|
||||||
|
//.where().raw("details.id = od.id")
|
||||||
|
.query();
|
||||||
|
|
||||||
|
Query<Order> query = new QOrder()
|
||||||
|
.notExists(subQuery)
|
||||||
|
.orderBy("orderDate")
|
||||||
|
.query();
|
||||||
|
query.findList();
|
||||||
|
|
||||||
|
// select distinct t0.id, t0.status, t0.order_date, t0.ship_date, t0.version, t0.when_created, t0.when_updated, t0.customer_id, t0.shipping_address_id, t0.order_date
|
||||||
|
// from o_order t0
|
||||||
|
// left join o_order_detail t1 on t1.order_id = t0.id
|
||||||
|
// where not exists (select 1 from o_order_detail od where od.order_qty > ? and od.id = t1.id) order by t0.order_date
|
||||||
|
String sql = query.getGeneratedSql();
|
||||||
|
assertThat(sql).contains(" not exists (select 1 from o_order_detail od where od.order_qty > ? and od.id = t1.id)");
|
||||||
|
}
|
||||||
|
|
||||||
private static void setupData() {
|
private static void setupData() {
|
||||||
customer = new Customer();
|
customer = new Customer();
|
||||||
customer.name = "Fred";
|
customer.name = "Fred";
|
||||||
|
|||||||
Reference in new issue
Block a user