mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#716 - Remove createNamedQuery() and createQuery(Class<T> beanType, String query) ... use criteria API, query beans and RawSql
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestInCollectionQueryPlan extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Object> idList1 = new ArrayList<Object>();
|
||||
idList1.add(1);
|
||||
|
||||
String oq = "find customer where id in (:idList)";
|
||||
Ebean.createQuery(Customer.class, oq).setParameter("idList", idList1).findList();
|
||||
|
||||
List<Object> idList2 = new ArrayList<Object>();
|
||||
idList2.add(1);
|
||||
idList2.add(2);
|
||||
|
||||
Ebean.createQuery(Customer.class, oq).setParameter("idList", idList2).findList();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.RawSql;
|
||||
import com.avaje.ebean.RawSqlBuilder;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -17,7 +19,10 @@ public class TestMyAdHocSqlSelect extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<MyAdHoc> list = Ebean.find(MyAdHoc.class).where().gt("order_id", 0).having()
|
||||
String sql = "select order_id, count(*) as detailCount from o_order_detail group by order_id";
|
||||
RawSql rawSql = RawSqlBuilder.parse(sql).columnMapping("order_id", "order.id").create();
|
||||
|
||||
List<MyAdHoc> list = Ebean.find(MyAdHoc.class).setRawSql(rawSql).where().gt("order_id", 0).having()
|
||||
.gt("detailCount", 0).findList();
|
||||
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
|
||||
public class TestNoSpaceBracket extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Query<Order> query = Ebean.createQuery(Order.class, "find order join customer(id,name) ");
|
||||
|
||||
query.findList();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryLimitOffsetSimple extends BaseTestCase {
|
||||
|
||||
/**
|
||||
* Test the syntax of the limit offset clause.
|
||||
*/
|
||||
@Test
|
||||
public void testMe() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.createQuery(Order.class, "where status = :A limit 100 offset 3");
|
||||
query.setParameter("A", Order.Status.NEW);
|
||||
|
||||
query
|
||||
.setFirstRow(10)
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestQueryParsing extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
String oq = "find order join customer (id, name) join customer.contacts join details (+query(4),+lazy(5))";
|
||||
Query<Order> q = Ebean.createQuery(Order.class, oq);
|
||||
checkQuery(q);
|
||||
|
||||
String oq1 = "find order join customer (id, name) join customer.contacts join details ( +query(4), +lazy(5) )";
|
||||
SpiQuery<?> q1 = (SpiQuery<?>) Ebean.createQuery(Order.class, oq1);
|
||||
checkQuery(q1);
|
||||
|
||||
String oq2 = "find order join customer (id, name) join customer.contacts join details ( +query(4), +lazy(5) , *)";
|
||||
SpiQuery<?> q2 = (SpiQuery<?>) Ebean.createQuery(Order.class, oq2);
|
||||
checkQuery(q2);
|
||||
|
||||
String oq3 = "find order join customer (id, name) join customer.contacts join details (+query(4),+lazy(5),*)";
|
||||
SpiQuery<?> q3 = (SpiQuery<?>) Ebean.createQuery(Order.class, oq3);
|
||||
checkQuery(q3);
|
||||
|
||||
String oq4 = "find order join customer (id, name) join customer.contacts join details (+query(4) +lazy(5) *)";
|
||||
SpiQuery<?> q4 = (SpiQuery<?>) Ebean.createQuery(Order.class, oq4);
|
||||
checkQuery(q4);
|
||||
|
||||
}
|
||||
|
||||
private void checkQuery(Query<?> q) {
|
||||
|
||||
SpiQuery<?> sq = (SpiQuery<?>) q;
|
||||
OrmQueryDetail detail = sq.getDetail();
|
||||
assertTrue(detail.getChunk(null, false).allProperties());
|
||||
|
||||
assertNotNull(detail.getChunk("customer", false));
|
||||
assertFalse(detail.getChunk("customer", false).isQueryFetch());
|
||||
assertFalse(detail.getChunk("customer", false).isLazyFetch());
|
||||
assertFalse(detail.getChunk("customer", false).allProperties());
|
||||
assertEquals("id, name",detail.getChunk("customer", false).getProperties());
|
||||
|
||||
assertNotNull(detail.getChunk("customer.contacts", false));
|
||||
assertFalse(detail.getChunk("customer.contacts", false).isQueryFetch());
|
||||
assertFalse(detail.getChunk("customer.contacts", false).isLazyFetch());
|
||||
assertTrue(detail.getChunk("customer.contacts", false).allProperties());
|
||||
|
||||
assertNotNull(detail.getChunk("details", false));
|
||||
assertTrue(detail.getChunk("details", false).isQueryFetch());
|
||||
assertTrue(detail.getChunk("details", false).isLazyFetch());
|
||||
assertEquals(4, detail.getChunk("details", false).getQueryFetchBatch());
|
||||
assertEquals(5, detail.getChunk("details", false).getLazyFetchBatch());
|
||||
assertTrue(detail.getChunk("details", false).allProperties());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,18 +30,7 @@ public class TestSubQuery extends BaseTestCase {
|
||||
Query<Order> sq = Ebean.createQuery(Order.class).select("id").where()
|
||||
.in("details.product.id", productIds).query();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class).where().in("id", sq).findList();
|
||||
|
||||
// FIXME: need to clear out old orders..
|
||||
// Assert.assertEquals(2,list.size());
|
||||
|
||||
String oq = " find order (id, status) where id in "
|
||||
+ "(select a.id from o_order a join o_order_detail ad on ad.order_id = a.id where ad.product_id in (:prods)) ";
|
||||
|
||||
Ebean.createQuery(Order.class, oq).setParameter("prods", productIds)
|
||||
.findList();
|
||||
|
||||
|
||||
Ebean.find(Order.class).where().in("id", sq).findList();
|
||||
}
|
||||
|
||||
public void testCompositeKey() {
|
||||
|
||||
@@ -24,12 +24,5 @@ public class TestOrderByWithFunction extends BaseTestCase {
|
||||
String sql = query.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(sql.contains("order by length(t0.name)"));
|
||||
|
||||
String oq = "find customer ORDER BY LENGTH(name),name";
|
||||
query = Ebean.createQuery(Customer.class, oq);
|
||||
query.findList();
|
||||
|
||||
sql = query.getGeneratedSql();
|
||||
Assert.assertTrue(sql.contains("order by LENGTH(t0.name)"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,63 +63,5 @@ public class TestQueryConversationRowCount extends BaseTestCase {
|
||||
|
||||
Assert.assertTrue(countSql.contains("select count(*) from ( select distinct t0.id c0 from c_conversation t0 left outer join c_participation u1 on u1.conversation_id = t0.id where t0.group_id = ? and ((t0.isopen = ? and u1.user_id = ? ) or t0.isopen = ? )"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsingQueryLanguage() {
|
||||
|
||||
//"find conversation where club = :clubId and ( ( isPublic = :false and participants.user.id = :userId ) or isPublic = :true ) order by createdAt desc ";
|
||||
String qry = "find conversation where group.id = :groupId and ( ( open = :false and participants.user.id = :userId ) or open = :true ) order by whenCreated desc ";
|
||||
|
||||
Long groupId = 1L;
|
||||
Long userId = 1L;
|
||||
|
||||
Query<Conversation> query = Ebean.createQuery(Conversation.class, qry);
|
||||
query.setParameter("groupId", groupId);
|
||||
query.setParameter("userId", userId);
|
||||
query.setParameter("false", false);
|
||||
query.setParameter("true", true);
|
||||
|
||||
query.findList();
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
|
||||
// ACTUAL:
|
||||
// select distinct t0.id c0, t0.title c1, t0.open c2, t0.version c3, t0.when_created c4, t0.when_updated c5, t0.group_id c6, t0.when_created
|
||||
// from c_conversation t0
|
||||
// join c_participation t1 on t1.conversation_id = t0.id
|
||||
// where t0.group_id = ? and ( ( t0.open = ? and t1.user_id = ? ) or t0.open = ? )
|
||||
// order by t0.when_created desc; --bind(1, false, 1, true, )
|
||||
|
||||
// SHOULD BE:
|
||||
// select distinct t0.id c0, t0.title c1, t0.open c2, t0.version c3, t0.when_created c4, t0.when_updated c5, t0.group_id c6, t0.when_created
|
||||
// from c_conversation t0
|
||||
// left outer join c_participation u1 on u1.conversation_id = t0.id
|
||||
// where t0.group_id = ? and ((t0.open = ? and u1.user_id = ? ) or t0.open = ? )
|
||||
// order by t0.when_created desc;
|
||||
|
||||
Assert.assertTrue(generatedSql.contains("select distinct t0.id c0, t0.title c1, t0.isopen"));
|
||||
|
||||
|
||||
// THE NEXT ASSERT CURRENTLY FAILS:
|
||||
// Assert.assertTrue(generatedSql.contains("left outer join c_participation u1 on u1.conversation_id = t0.id"));
|
||||
// Assert.assertTrue(generatedSql.contains("where t0.group_id = ? and ((t0.open = ? and u1.user_id = ? ) or t0.open = ? )"));
|
||||
//
|
||||
//
|
||||
// LoggedSqlCollector.start();
|
||||
// query.findRowCount();
|
||||
//
|
||||
// // select count(*) from (
|
||||
// // select distinct t0.id c0
|
||||
// // from c_conversation t0
|
||||
// // left outer join c_participation u1 on u1.conversation_id = t0.id
|
||||
// // where t0.group_id = ? and ((t0.open = ? and u1.user_id = ? ) or t0.open = ? )
|
||||
// // ); --bind(1,true,1,true)
|
||||
//
|
||||
// List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
// Assert.assertEquals(1, loggedSql.size());
|
||||
//
|
||||
// String countSql = loggedSql.get(0);
|
||||
//
|
||||
// Assert.assertTrue(countSql.contains("select count(*) from ( select distinct t0.id c0 from c_conversation t0 left outer join c_participation u1 on u1.conversation_id = t0.id where t0.group_id = ? and ((t0.open = ? and u1.user_id = ? ) or t0.open = ? )"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user