mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
auomatically append "order by id" when paging and query.orderById = true (#1275)
Add ServerConfig.setDefaultOrderById() and query.orderById(boolean) ... to control automatically append "order by id" with paging query
This commit is contained in:
committed by
Rob Bygrave
parent
0cba0664f8
commit
a712be2917
@@ -10,6 +10,8 @@ import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
@@ -67,6 +69,13 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
// check also select count(*)
|
||||
LoggedSqlCollector.start();
|
||||
assertThat(query.findCount()).isEqualTo(list.size());
|
||||
List<String>sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql.get(0)).startsWith("select count(*) from ( select r.id, r.name from o_customer r");
|
||||
assertThat(sql.get(0)).doesNotContain("order by");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -104,7 +113,6 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
|
||||
query.setFirstRow(1);
|
||||
query.setMaxRows(2);
|
||||
query.order().asc("id");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
|
||||
@@ -170,12 +178,54 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
|
||||
if (isSqlServer()) {
|
||||
assertThat(query.getGeneratedSql()).contains("top 100 ");
|
||||
assertThat(query.getGeneratedSql()).contains("order by o.ship_date desc");
|
||||
assertThat(query.getGeneratedSql()).contains("order by o.ship_date desc ");
|
||||
} else if (isOracle()) {
|
||||
assertThat(query.getGeneratedSql()).contains("a where rownum <= 100 )");
|
||||
} else {
|
||||
assertThat(query.getGeneratedSql()).contains("order by o.ship_date desc limit 100");
|
||||
}
|
||||
|
||||
// check also select count(*)
|
||||
LoggedSqlCollector.start();
|
||||
query.findCount();
|
||||
List<String>sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql.get(0)).startsWith("select count(*) from ( select o.id, o.order_date, o.ship_date from o_order o");
|
||||
assertThat(sql.get(0)).doesNotContain("order by");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaging_with_existingRawSqlOrderBy_expect_id_appendToOrderBy_with_id() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse("select o.id, o.order_date, o.ship_date from o_order o order by o.ship_date desc")
|
||||
.columnMapping("o.id", "id")
|
||||
.columnMapping("o.order_date", "orderDate")
|
||||
.columnMapping("o.ship_date", "shipDate")
|
||||
.create();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.orderById(true);
|
||||
|
||||
query.setMaxRows(100);
|
||||
query.findList();
|
||||
|
||||
if (isSqlServer()) {
|
||||
assertThat(query.getGeneratedSql()).contains("top 100 ");
|
||||
assertThat(query.getGeneratedSql()).contains("order by o.ship_date desc, o.id ");
|
||||
} else if (isOracle()) {
|
||||
assertThat(query.getGeneratedSql()).contains("a where rownum <= 100 )");
|
||||
} else {
|
||||
assertThat(query.getGeneratedSql()).contains("order by o.ship_date desc, o.id limit 100");
|
||||
}
|
||||
|
||||
// check also select count(*)
|
||||
LoggedSqlCollector.start();
|
||||
query.findCount();
|
||||
List<String>sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql.get(0)).startsWith("select count(*) from ( select o.id, o.order_date, o.ship_date from o_order o");
|
||||
assertThat(sql.get(0)).doesNotContain("order by");
|
||||
}
|
||||
|
||||
@IgnorePlatform(Platform.ORACLE)
|
||||
@@ -199,7 +249,7 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
query.order("coalesce(shipDate, getdate()) desc");
|
||||
query.findList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("order by coalesce(o.ship_date, getdate()) desc");
|
||||
assertThat(sqlOf(query)).contains("order by coalesce(o.ship_date, getdate()) desc ");
|
||||
assertThat(sqlOf(query)).contains("select top 100");
|
||||
|
||||
} else {
|
||||
@@ -210,6 +260,39 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@IgnorePlatform(Platform.ORACLE)
|
||||
@Test
|
||||
public void testPaging_when_setOrderBy_expect_id_appendToOrderBy_with_id() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse("select o.id, o.order_date, o.ship_date from o_order o order by o.ship_date desc nulls last")
|
||||
.columnMapping("o.id", "id")
|
||||
.columnMapping("o.order_date", "orderDate")
|
||||
.columnMapping("o.ship_date", "shipDate")
|
||||
.create();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
query.setMaxRows(100);
|
||||
query.orderById(true);
|
||||
|
||||
if (isSqlServer()) {
|
||||
query.order("coalesce(shipDate, getdate()) desc");
|
||||
query.findList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("order by coalesce(o.ship_date, getdate()) desc, o.id");
|
||||
assertThat(sqlOf(query)).contains("select top 100");
|
||||
|
||||
} else {
|
||||
query.order("coalesce(shipDate, now()) desc");
|
||||
query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("order by coalesce(o.ship_date, now()) desc, o.id limit 100");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaging_when_setOrderBy_containsId_expect_leaveAsIs() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user