mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 19:00:38 +00:00
#1172 - Refactor - remove internal unused DefaultOrmQuery logSecondaryQuery (as we use LoggedSqlCollector)
This commit is contained in:
@@ -26,16 +26,6 @@ public abstract class LoadRequest {
|
||||
*/
|
||||
public abstract Class<?> getBeanType();
|
||||
|
||||
/**
|
||||
* Log the just executed secondary query with the 'root' query if 'logSecondaryQuery' is set to
|
||||
* true. This is for testing purposes to confirm the secondary query executes etc.
|
||||
*/
|
||||
public void logSecondaryQuery(SpiQuery<?> query) {
|
||||
if (parentRequest != null && parentRequest.isLogSecondaryQuery()) {
|
||||
parentRequest.getQuery().logSecondaryQuery(query);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this is a lazy load and false if it is a secondary query.
|
||||
*/
|
||||
|
||||
@@ -440,26 +440,6 @@ public interface SpiQuery<T> extends Query<T> {
|
||||
*/
|
||||
Boolean isAutoTune();
|
||||
|
||||
/**
|
||||
* Set to true if you want to capture executed secondary queries.
|
||||
*/
|
||||
void setLogSecondaryQuery(boolean logSecondaryQuery);
|
||||
|
||||
/**
|
||||
* Return true if executed secondary queries should be captured.
|
||||
*/
|
||||
boolean isLogSecondaryQuery();
|
||||
|
||||
/**
|
||||
* Return the list of secondary queries that were executed.
|
||||
*/
|
||||
List<SpiQuery<?>> getLoggedSecondaryQueries();
|
||||
|
||||
/**
|
||||
* Log an executed secondary query.
|
||||
*/
|
||||
void logSecondaryQuery(SpiQuery<?> query);
|
||||
|
||||
/**
|
||||
* If return null then no profiling for this query. If a ProfilingListener is
|
||||
* returned this implies that profiling is turned on for this query (and all
|
||||
|
||||
@@ -84,13 +84,9 @@ public class DefaultBeanLoader {
|
||||
int batchSize = getBatchSize(batch.size());
|
||||
|
||||
SpiQuery<?> query = loadRequest.createQuery(server, batchSize);
|
||||
|
||||
executeQuery(loadRequest, query);
|
||||
|
||||
loadRequest.postLoad();
|
||||
|
||||
// log the query (for testing secondary queries)
|
||||
loadRequest.logSecondaryQuery(query);
|
||||
}
|
||||
|
||||
public void loadMany(BeanCollection<?> bc, boolean onlyIds) {
|
||||
@@ -210,9 +206,6 @@ public class DefaultBeanLoader {
|
||||
|
||||
List<?> list = executeQuery(loadRequest, query);
|
||||
loadRequest.postLoad(list);
|
||||
|
||||
// log the query (for testing secondary queries)
|
||||
loadRequest.logSecondaryQuery(query);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -551,13 +551,6 @@ public final class OrmQueryRequest<T> extends BeanRequest implements BeanQueryRe
|
||||
transaction.logSql(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the request wants to log the secondary queries (test purpose).
|
||||
*/
|
||||
public boolean isLogSecondaryQuery() {
|
||||
return query.isLogSecondaryQuery();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the batch size for lazy loading on this bean query request.
|
||||
*/
|
||||
|
||||
@@ -220,8 +220,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
*/
|
||||
private boolean autoTuned;
|
||||
|
||||
private boolean logSecondaryQuery;
|
||||
|
||||
/**
|
||||
* Root table alias. For {@link Query#alias(String)} command.
|
||||
*/
|
||||
@@ -895,31 +893,6 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
this.usageProfiling = usageProfiling;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLogSecondaryQuery(boolean logSecondaryQuery) {
|
||||
this.logSecondaryQuery = logSecondaryQuery;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLogSecondaryQuery() {
|
||||
return logSecondaryQuery;
|
||||
}
|
||||
|
||||
private List<SpiQuery<?>> loggedSecondaryQueries;
|
||||
|
||||
@Override
|
||||
public List<SpiQuery<?>> getLoggedSecondaryQueries() {
|
||||
return loggedSecondaryQueries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSecondaryQuery(SpiQuery<?> query) {
|
||||
if (loggedSecondaryQueries == null) {
|
||||
loggedSecondaryQueries = new ArrayList<>();
|
||||
}
|
||||
loggedSecondaryQueries.add(query);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setParentNode(ObjectGraphNode parentNode) {
|
||||
this.parentNode = parentNode;
|
||||
|
||||
@@ -4,18 +4,19 @@ import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.Query;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestSecondaryQueries extends BaseTestCase {
|
||||
|
||||
@@ -73,7 +74,6 @@ public class TestSecondaryQueries extends BaseTestCase {
|
||||
assertThat(trimSql(sql.get(0), 1)).contains("select t0.id, t0.name from o_customer t0 where t0.id in");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void fetchIterate() {
|
||||
|
||||
@@ -112,22 +112,18 @@ public class TestSecondaryQueries extends BaseTestCase {
|
||||
.fetch("contacts", "+query")
|
||||
.setId(custId);
|
||||
|
||||
SpiQuery<?> spiQuery = (SpiQuery<?>) query;
|
||||
spiQuery.setLogSecondaryQuery(true);
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Customer cust = query.findOne();
|
||||
assertNotNull(cust);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
Assert.assertNotNull(cust);
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
Assert.assertTrue(generatedSql.contains("from o_customer t0 where t0.id = ?"));
|
||||
assertThat(generatedSql).contains("from o_customer t0 where t0.id = ?");
|
||||
|
||||
List<SpiQuery<?>> loggedSecondaryQueries = spiQuery.getLoggedSecondaryQueries();
|
||||
Assert.assertEquals(1, loggedSecondaryQueries.size());
|
||||
|
||||
SpiQuery<?> secondaryQuery = loggedSecondaryQueries.get(0);
|
||||
String secondarySql = secondaryQuery.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(secondarySql.contains("from contact t0 where (t0.customer_id) in (?)"));
|
||||
assertEquals(2, sql.size());
|
||||
assertThat(sql.get(1)).contains("from contact t0 where (t0.customer_id) in (?)");
|
||||
}
|
||||
|
||||
|
||||
@@ -144,29 +140,28 @@ public class TestSecondaryQueries extends BaseTestCase {
|
||||
.where().eq("status", Order.Status.NEW)
|
||||
.query();
|
||||
|
||||
// .fetch("customer", "+query name, status")
|
||||
// .fetch("details", "+query(10)")
|
||||
|
||||
SpiQuery<?> spiQuery = (SpiQuery<?>) query;
|
||||
spiQuery.setLogSecondaryQuery(true);
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Order> list = query.findList();
|
||||
Assert.assertTrue(!list.isEmpty());
|
||||
assertTrue(!list.isEmpty());
|
||||
|
||||
for (Order order : list) {
|
||||
order.getCustomer().getStatus();
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
String generatedSql = sqlOf(spiQuery, 2);
|
||||
String generatedSql = sqlOf(query, 2);
|
||||
//select t0.id c0, t0.status c1, t0.kcustomer_id c2 from o_order t0 where t0.status = ? ; --bind(NEW)
|
||||
Assert.assertEquals("select t0.id, t0.status, t0.kcustomer_id from o_order t0 where t0.status = ? ", generatedSql);
|
||||
assertEquals("select t0.id, t0.status, t0.kcustomer_id from o_order t0 where t0.status = ? ", generatedSql);
|
||||
|
||||
|
||||
List<SpiQuery<?>> secondaryQueries = spiQuery.getLoggedSecondaryQueries();
|
||||
Assert.assertEquals(2, secondaryQueries.size());
|
||||
//List<SpiQuery<?>> secondaryQueries = spiQuery.getLoggedSecondaryQueries();
|
||||
assertEquals(3, sql.size());
|
||||
|
||||
SpiQuery<?> custSecondaryQuery = secondaryQueries.get(0);
|
||||
String custSecondarySql = custSecondaryQuery.getGeneratedSql();
|
||||
//SpiQuery<?> custSecondaryQuery =;
|
||||
String custSecondarySql = sql.get(1);
|
||||
|
||||
// select t0.id c0, t0.name c1, t0.status c2,
|
||||
// t1.id c3, t1.first_name c4, t1.last_name c5, t1.phone c6, t1.mobile c7, t1.email c8, t1.cretime c9, t1.updtime c10, t1.customer_id c11, t1.group_id c12
|
||||
@@ -174,19 +169,17 @@ public class TestSecondaryQueries extends BaseTestCase {
|
||||
// left join contact t1 on t1.customer_id = t0.id
|
||||
// where t0.id = ? order by t0.id; --bind(1)
|
||||
|
||||
Assert.assertTrue(custSecondarySql.contains("from o_customer t0 "));
|
||||
Assert.assertTrue(custSecondarySql.contains("left join contact t1 on t1.customer_id = t0.id "));
|
||||
Assert.assertTrue(custSecondarySql.contains("where t0.id "));
|
||||
assertThat(custSecondarySql).contains("from o_customer t0 ");
|
||||
assertThat(custSecondarySql).contains("left join contact t1 on t1.customer_id = t0.id ");
|
||||
assertThat(custSecondarySql).contains("where t0.id ");
|
||||
|
||||
|
||||
SpiQuery<?> orderDetailsSecondaryQuery = secondaryQueries.get(1);
|
||||
String ordSecondarySql = orderDetailsSecondaryQuery.getGeneratedSql();
|
||||
String ordSecondarySql = sql.get(2);
|
||||
|
||||
// select ...
|
||||
// from o_order_detail t0
|
||||
// where (t0.order_id) in (?,?,?,?,?) ; --bind(1,4,1,1,1)
|
||||
|
||||
Assert.assertTrue(ordSecondarySql.contains(" from o_order_detail t0 where t0.id > 0 and (t0.order_id) in (?"));
|
||||
assertThat(ordSecondarySql).contains(" from o_order_detail t0 where t0.id > 0 and (t0.order_id) in (?");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,16 +3,19 @@ package org.tests.query;
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.OrderShipment;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestQueryFetchManyTwoDeep extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
@@ -26,15 +29,19 @@ public class TestQueryFetchManyTwoDeep extends BaseTestCase {
|
||||
.fetch("orders")
|
||||
.fetch("orders.details");
|
||||
|
||||
SpiQuery<?> spiQuery = (SpiQuery<?>) query;
|
||||
spiQuery.setLogSecondaryQuery(true);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
Assert.assertTrue("has rows", !list.isEmpty());
|
||||
Assert.assertTrue(query.getGeneratedSql().contains("from o_customer t0 "));
|
||||
Assert.assertTrue(query.getGeneratedSql().contains("left join o_order t1 on t1.kcustomer_id = t0.id"));
|
||||
Assert.assertTrue(query.getGeneratedSql().contains("left join o_customer t2 on t2.id = t1.kcustomer_id"));
|
||||
Assert.assertFalse(query.getGeneratedSql().contains("join or_order_ship"));
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertTrue("has rows", !list.isEmpty());
|
||||
String mainSql = sqlOf(query);
|
||||
assertThat(mainSql).contains("from o_customer t0 ");
|
||||
assertThat(mainSql).contains("left join o_order t1 on t1.kcustomer_id = t0.id");
|
||||
assertThat(mainSql).contains("left join o_customer t2 on t2.id = t1.kcustomer_id");
|
||||
assertThat(mainSql).doesNotContain("join or_order_ship");
|
||||
|
||||
//select t0.id c0, t0.status c1, t0.name c2, t0.smallnote c3, t0.anniversary c4, t0.cretime c5, t0.updtime c6, t0.billing_address_id c7, t0.shipping_address_id c8, t1.id c9, t1.status c10, t1.order_date c11, t1.ship_date c12,
|
||||
// t2.name c13, t1.cretime c14, t1.updtime c15, t1.kcustomer_id c16
|
||||
@@ -44,13 +51,13 @@ public class TestQueryFetchManyTwoDeep extends BaseTestCase {
|
||||
// where t1.order_date is not null order by t0.id; --bind()
|
||||
|
||||
|
||||
List<SpiQuery<?>> secondaryQueries = spiQuery.getLoggedSecondaryQueries();
|
||||
Assert.assertNotNull(secondaryQueries);
|
||||
Assert.assertEquals(1, secondaryQueries.size());
|
||||
//List<SpiQuery<?>> secondaryQueries = spiQuery.getLoggedSecondaryQueries();
|
||||
//Assert.assertNotNull(secondaryQueries);
|
||||
assertEquals(2, sql.size());
|
||||
|
||||
SpiQuery<?> secondaryQuery = secondaryQueries.get(0);
|
||||
String secondarySql = secondaryQuery.getGeneratedSql();
|
||||
Assert.assertTrue(secondarySql.contains("from o_order_detail t0 where t0.id > 0 and (t0.order_id) in"));
|
||||
//SpiQuery<?> secondaryQuery = secondaryQueries.get(0);
|
||||
String secondarySql = sql.get(1);
|
||||
assertThat(secondarySql).contains("from o_order_detail t0 where t0.id > 0 and (t0.order_id) in");
|
||||
|
||||
// select t0.order_id c0, t0.id c1, t0.order_qty c2, t0.ship_qty c3, t0.unit_price c4, t0.cretime c5, t0.updtime c6, t0.order_id c7, t0.product_id c8
|
||||
// from o_order_detail t0
|
||||
@@ -70,7 +77,7 @@ public class TestQueryFetchManyTwoDeep extends BaseTestCase {
|
||||
.fetch("order.details");
|
||||
|
||||
List<OrderShipment> shipList = shipQuery.findList();
|
||||
Assert.assertTrue("has rows", !shipList.isEmpty());
|
||||
assertTrue("has rows", !shipList.isEmpty());
|
||||
|
||||
String generatedSql = shipQuery.getGeneratedSql();
|
||||
|
||||
@@ -81,11 +88,11 @@ public class TestQueryFetchManyTwoDeep extends BaseTestCase {
|
||||
// left join o_order_detail t2 on t2.order_id = t1.id
|
||||
// where t2.id > 0 ; --bind()
|
||||
|
||||
Assert.assertTrue(generatedSql.contains("from or_order_ship t0"));
|
||||
assertTrue(generatedSql.contains("from or_order_ship t0"));
|
||||
// Relationship from OrderShipment to Order is optional so outer join here
|
||||
Assert.assertTrue(generatedSql.contains("left join o_order t1 on t1.id = t0.order_id"));
|
||||
Assert.assertTrue(generatedSql.contains("left join o_customer t3 on t3.id = t1.kcustomer_id"));
|
||||
Assert.assertTrue(generatedSql.contains("left join o_order_detail t2 on t2.order_id = t1.id"));
|
||||
assertTrue(generatedSql.contains("left join o_order t1 on t1.id = t0.order_id"));
|
||||
assertTrue(generatedSql.contains("left join o_customer t3 on t3.id = t1.kcustomer_id"));
|
||||
assertTrue(generatedSql.contains("left join o_order_detail t2 on t2.order_id = t1.id"));
|
||||
|
||||
|
||||
// If OrderShipment to Order is not optional you get inner joins up to o_order_detail (which is a many)
|
||||
@@ -111,7 +118,7 @@ public class TestQueryFetchManyTwoDeep extends BaseTestCase {
|
||||
.fetch("customer.orders");
|
||||
|
||||
List<Contact> shipList = query.findList();
|
||||
Assert.assertTrue("has rows", !shipList.isEmpty());
|
||||
assertTrue("has rows", !shipList.isEmpty());
|
||||
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
|
||||
@@ -122,11 +129,11 @@ public class TestQueryFetchManyTwoDeep extends BaseTestCase {
|
||||
// left join o_customer t3 on t3.id = t2.kcustomer_id
|
||||
// where t2.order_date is not null ; --bind()
|
||||
|
||||
Assert.assertTrue(generatedSql.contains("from contact t0 "));
|
||||
assertThat(generatedSql).contains("from contact t0 ");
|
||||
// Relationship from Contact to Customer is mandatory so inner join here
|
||||
Assert.assertTrue(generatedSql.contains("join o_customer t1 on t1.id = t0.customer_id"));
|
||||
assertThat(generatedSql).contains("join o_customer t1 on t1.id = t0.customer_id");
|
||||
// outer join on many relationship 'orders'
|
||||
Assert.assertTrue(generatedSql.contains("left join o_order t2 on t2.kcustomer_id = t1.id"));
|
||||
assertThat(generatedSql).contains("left join o_order t2 on t2.kcustomer_id = t1.id");
|
||||
|
||||
}
|
||||
|
||||
@@ -143,7 +150,7 @@ public class TestQueryFetchManyTwoDeep extends BaseTestCase {
|
||||
.query();
|
||||
|
||||
List<Contact> list = query.findList();
|
||||
Assert.assertTrue("has rows", !list.isEmpty());
|
||||
assertTrue("has rows", !list.isEmpty());
|
||||
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
|
||||
@@ -152,9 +159,9 @@ public class TestQueryFetchManyTwoDeep extends BaseTestCase {
|
||||
// join o_customer t1 on t1.id = t0.customer_id
|
||||
// where lower(t1.name) like ? ; --bind(rob%)
|
||||
|
||||
Assert.assertTrue(generatedSql.contains("from contact t0 "));
|
||||
Assert.assertTrue(generatedSql.contains("join o_customer t1 on t1.id = t0.customer_id"));
|
||||
Assert.assertTrue(generatedSql.contains("where lower(t1.name) like "));
|
||||
assertThat(generatedSql).contains("from contact t0 ");
|
||||
assertThat(generatedSql).contains("join o_customer t1 on t1.id = t0.customer_id");
|
||||
assertThat(generatedSql).contains("where lower(t1.name) like ");
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user