mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#919 - io.ebean package initial
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.SqlUpdate;
|
||||
import org.tests.idkeys.db.AuditLog;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestInsertSqlLogging extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
if (isMsSqlServer()) return;
|
||||
|
||||
Ebean.delete(AuditLog.class, 10000);
|
||||
|
||||
String sql = "insert into audit_log (id, description, modified_description) values (?,?,?)";
|
||||
SqlUpdate sqlUpdate = Ebean.createSqlUpdate(sql);
|
||||
sqlUpdate.setParameter(1, 10000);
|
||||
sqlUpdate.setParameter(2, "hello");
|
||||
sqlUpdate.setParameter(3, "rob");
|
||||
|
||||
sqlUpdate.execute();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.OrderAggregate;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestOrderReportTotal extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = getRawSql();
|
||||
|
||||
Query<OrderAggregate> query = Ebean.createQuery(OrderAggregate.class);
|
||||
|
||||
List<OrderAggregate> list = query.setRawSql(rawSql).findList();
|
||||
assertNotNull(list);
|
||||
|
||||
Query<OrderAggregate> q2 = Ebean.createQuery(OrderAggregate.class).setRawSql(rawSql);
|
||||
q2.where().gt("id", 1);
|
||||
q2.having().gt("totalItems", 1);
|
||||
|
||||
List<OrderAggregate> l2 = q2.findList();
|
||||
assertNotNull(l2);
|
||||
|
||||
}
|
||||
|
||||
private RawSql getRawSql() {
|
||||
String sql =
|
||||
"select order_id, count(*) as totalItems, sum(order_qty*unit_price) as totalAmount \n" +
|
||||
"from o_order_detail \n" +
|
||||
"group by order_id";
|
||||
|
||||
return RawSqlBuilder.parse(sql).columnMapping("order_id", "order.id").create();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrderDetailCount() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
int detailsCount = Ebean.find(OrderDetail.class)
|
||||
.where()
|
||||
.gt("order.id", 2)
|
||||
.istartsWith("order.customer.name", "rob")
|
||||
.findCount();
|
||||
|
||||
assertThat(detailsCount).isGreaterThan(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestRawSqlAsKeyword extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
// Make this false to run this test ... as the pipe string concatenation syntax is DB specific
|
||||
boolean skipTestAsDBSpecficSQL = true;
|
||||
|
||||
if (skipTestAsDBSpecficSQL) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
|
||||
// try valid query where spaces in the formula ...
|
||||
RawSql rawSql =
|
||||
RawSqlBuilder
|
||||
.parse("select r.id, r.name || 'hello' as name from o_customer r ")
|
||||
.create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
|
||||
// try valid query with no spaces
|
||||
rawSql =
|
||||
RawSqlBuilder
|
||||
.parse("select r.id, r.name||'hello' as name from o_customer r ")
|
||||
.create();
|
||||
|
||||
query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
rawSql =
|
||||
RawSqlBuilder
|
||||
.parse("select r.id, r.name||'hello' name from o_customer r ")
|
||||
.create();
|
||||
query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
// this will barf - expecting the AS keyword now
|
||||
rawSql =
|
||||
RawSqlBuilder
|
||||
.parse("select r.id, r.name || 'hello' name from o_customer r ")
|
||||
.create();
|
||||
query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.CustomerAggregate;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestRawSqlCustomerAggregate extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.parse(
|
||||
"select c.customer_id, count(*) as totalContacts from contact c group by c.customer_id")
|
||||
.columnMapping("c.customer_id", "customer.id").create();
|
||||
|
||||
Query<CustomerAggregate> query = Ebean.find(CustomerAggregate.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ge("customer.id", 1);
|
||||
|
||||
List<CustomerAggregate> list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TestRawSqlMasterDetail extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String rs = "select t0.id, t0.status, t1.id, t1.name, " +
|
||||
" t2.id, t2.order_qty, t3.id, t3.name " +
|
||||
"from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id " +
|
||||
"join o_order_detail t2 on t2.order_id = t0.id " +
|
||||
"join o_product t3 on t3.id = t2.product_id " +
|
||||
"where t0.id <= :maxOrderId and t3.id = :productId " +
|
||||
"order by t0.id, t2.id asc";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs)
|
||||
.columnMapping("t0.id", "id")
|
||||
.columnMapping("t0.status", "status")
|
||||
.columnMapping("t1.id", "customer.id")
|
||||
.columnMapping("t1.name", "customer.name")
|
||||
.columnMapping("t2.id", "details.id")
|
||||
.columnMapping("t2.order_qty", "details.orderQty")
|
||||
.columnMapping("t3.id", "details.product.id")
|
||||
.columnMapping("t3.name", "details.product.name")
|
||||
.create();
|
||||
|
||||
List<Order> ordersFromRaw = Ebean.find(Order.class)
|
||||
.setRawSql(rawSql)
|
||||
.setParameter("maxOrderId", 2)
|
||||
.setParameter("productId", 1)
|
||||
.findList();
|
||||
|
||||
printOrders(ordersFromRaw, "using RawSql");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForeignKeyColumn() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// billing_address_id fk column is automatically
|
||||
// mapped to the logical path: billingAddress.id
|
||||
|
||||
String rs = "select c.id, c.name, c.billing_address_id, c.updtime " +
|
||||
"from o_customer c " +
|
||||
"order by c.id";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs).create();
|
||||
|
||||
List<Customer> customers = Ebean.find(Customer.class)
|
||||
.setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
assertThat(customers).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleNestedColumn() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
|
||||
String rs = "select c.id, c.name, c.billing_address_id, ba.line_1, ba.city, c.updtime " +
|
||||
"from o_customer c " +
|
||||
" left join o_address ba on ba.id = c.billing_address_id " +
|
||||
"order by c.id";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs)
|
||||
.tableAliasMapping("ba", "billingAddress")
|
||||
.create();
|
||||
|
||||
List<Customer> customers = Ebean.find(Customer.class)
|
||||
.setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
assertThat(customers).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleJoinColumn() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String rs = "select c.id, c.name, c.billing_address_id, ba.line_1, ba.city, c.updtime, sa.id, sa.line_1, sa.city " +
|
||||
"from o_customer c " +
|
||||
" left join o_address ba on ba.id = c.billing_address_id " +
|
||||
" left join o_address sa on sa.id = c.shipping_address_id " +
|
||||
"order by c.id";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs)
|
||||
.tableAliasMapping("ba", "billingAddress")
|
||||
.tableAliasMapping("sa", "shippingAddress")
|
||||
.create();
|
||||
|
||||
List<Customer> customers = Ebean.find(Customer.class)
|
||||
.setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
assertThat(customers).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithTableAliasMapping() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String rs = "select o.id, o.status, c.id, c.name, " +
|
||||
" d.id, d.order_qty, p.id, p.name " +
|
||||
"from o_order o join o_customer c on c.id = o.kcustomer_id " +
|
||||
"join o_order_detail d on d.order_id = o.id " +
|
||||
"join o_product p on p.id = d.product_id " +
|
||||
"where o.id <= :maxOrderId and p.id = :productId " +
|
||||
"order by o.id, d.id asc";
|
||||
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs)
|
||||
.tableAliasMapping("c", "customer")
|
||||
.tableAliasMapping("d", "details")
|
||||
.tableAliasMapping("p", "details.product")
|
||||
.create();
|
||||
|
||||
List<Order> ordersFromRaw = Ebean.find(Order.class)
|
||||
.setRawSql(rawSql)
|
||||
.setParameter("maxOrderId", 2)
|
||||
.setParameter("productId", 1)
|
||||
.findList();
|
||||
|
||||
printOrders(ordersFromRaw, "using RawSql with tableAlias mapping");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithNoIdPropertyWithInsert() {
|
||||
|
||||
String name = "RawSql-NoIdTest" + new Random().nextInt();
|
||||
EBasic basic = new EBasic();
|
||||
basic.setName(name);
|
||||
basic.setStatus(EBasic.Status.ACTIVE);
|
||||
|
||||
Ebean.save(basic);
|
||||
|
||||
String rs = "select b.status, b.name from e_basic b ";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs).create();
|
||||
|
||||
List<EBasic> list = Ebean.find(EBasic.class)
|
||||
.setRawSql(rawSql)
|
||||
.where().eq("name", name)
|
||||
.findList();
|
||||
|
||||
assertEquals(1, list.size());
|
||||
EBasic basic1 = list.get(0);
|
||||
basic1.setDescription("insertAfterRawFetch");
|
||||
|
||||
Ebean.save(basic1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithNoIdProperty() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String rs = "select o.status, o.order_date from o_order o ";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs)
|
||||
.create();
|
||||
|
||||
List<Order> ordersFromRaw = Ebean.find(Order.class)
|
||||
.setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
assertNotNull(ordersFromRaw);
|
||||
assertFalse(ordersFromRaw.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTableAlias_with_rootTypeMappedNotMapped() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String rs = "select o.id, o.status " +
|
||||
"from o_order o order by o.id asc";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs)
|
||||
.create();
|
||||
|
||||
List<Order> ordersFromRaw = Ebean.find(Order.class)
|
||||
.setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
for (Order order : ordersFromRaw) {
|
||||
assertThat(order.getId()).isNotNull();
|
||||
assertThat(order.getStatus()).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTableAlias_with_rootTypeMappedToNullPath() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String rs = "select o.id, o.status " +
|
||||
"from o_order o order by o.id asc";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs)
|
||||
.tableAliasMapping("o", null)
|
||||
.create();
|
||||
|
||||
List<Order> ordersFromRaw = Ebean.find(Order.class)
|
||||
.setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
for (Order order : ordersFromRaw) {
|
||||
assertThat(order.getId()).isNotNull();
|
||||
assertThat(order.getStatus()).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithMultipleManys() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String rs = "select o.id, o.status, c.id, c.name, " +
|
||||
" d.id, d.order_qty, p.id, p.name " +
|
||||
"from o_order o join o_customer c on c.id = o.kcustomer_id " +
|
||||
"join o_order_detail d on d.order_id = o.id " +
|
||||
"join o_product p on p.id = d.product_id " +
|
||||
"order by o.id, d.id asc";
|
||||
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs)
|
||||
.tableAliasMapping("c", "customer")
|
||||
.tableAliasMapping("d", "details")
|
||||
.tableAliasMapping("p", "details.product")
|
||||
.create();
|
||||
|
||||
List<Order> ordersFromRaw = Ebean.find(Order.class)
|
||||
.setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
printOrders(ordersFromRaw, "using RawSql with tableAlias mapping");
|
||||
|
||||
}
|
||||
|
||||
private void printOrders(List<Order> orders, String heading) {
|
||||
|
||||
for (Order order : orders) {
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
order.getCustomer().getName();
|
||||
for (OrderDetail detail : details) {
|
||||
detail.getProduct().getId();
|
||||
detail.getOrderQty();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestRawSqlNamedParams extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.parse("select r.id, r.name from o_customer r where r.id > :id and r.name like :name")
|
||||
.columnMapping("r.id", "id").columnMapping("r.name", "name").create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.setParameter("name", "R%");
|
||||
query.setParameter("id", 0);
|
||||
query.where().lt("id", 1000);
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
|
||||
Assert.assertNotNull(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.FutureRowCount;
|
||||
import io.ebean.PagedList;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testNamed() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.createNamedQuery(Order.class, "myRawTest");
|
||||
query.setParameter("orderStatus", Order.Status.NEW);
|
||||
query.setMaxRows(10);
|
||||
List<Order> list = query.findList();
|
||||
for (Order order : list) {
|
||||
order.getCretime();
|
||||
}
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
assertThat(sql).contains("select o.id, o.status, o.ship_date, c.id, c.name, a.id, a.line_1, a.line_2, a.city from o_order o");
|
||||
assertThat(sql).contains("join o_customer c on o.kcustomer_id = c.id ");
|
||||
assertThat(sql).contains("where o.status = ? order by c.name, c.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse("select r.id, r.name from o_customer r ")
|
||||
.columnMapping("r.id", "id")
|
||||
.columnMapping("r.name", "name").create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
query.fetch("contacts", new FetchConfig().query());
|
||||
query.filterMany("contacts").gt("lastName", "b");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFirstRowsMaxRows() throws InterruptedException, ExecutionException {
|
||||
|
||||
if (isMsSqlServer()) return;
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql =
|
||||
RawSqlBuilder
|
||||
.parse("select r.id, r.name from o_customer r ")
|
||||
.columnMapping("r.id", "id")
|
||||
.columnMapping("r.name", "name")
|
||||
.create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
int initialRowCount = query.findCount();
|
||||
|
||||
query.setFirstRow(1);
|
||||
query.setMaxRows(2);
|
||||
List<Customer> list = query.findList();
|
||||
|
||||
int rowCount = query.findCount();
|
||||
FutureRowCount<Customer> futureRowCount = query.findFutureCount();
|
||||
|
||||
Assert.assertEquals(initialRowCount, rowCount);
|
||||
Assert.assertEquals(initialRowCount, futureRowCount.get().intValue());
|
||||
|
||||
// check that lazy loading still executes
|
||||
for (Customer customer : list) {
|
||||
customer.getCretime();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaging() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse("select r.id, r.name from o_customer r ")
|
||||
.columnMapping("r.id", "id")
|
||||
.columnMapping("r.name", "name")
|
||||
.create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
int initialRowCount = query.findCount();
|
||||
|
||||
PagedList<Customer> page = query.setMaxRows(2).findPagedList();
|
||||
|
||||
List<Customer> list = page.getList();
|
||||
int rowCount = page.getTotalCount();
|
||||
|
||||
Assert.assertEquals(2, list.size());
|
||||
Assert.assertEquals(initialRowCount, rowCount);
|
||||
|
||||
// check that lazy loading executes
|
||||
for (Customer customer : list) {
|
||||
customer.getCretime();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaging_with_existingRawSqlOrderBy_expect_id_appendToOrderBy() {
|
||||
|
||||
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.setMaxRows(100);
|
||||
query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("order by o.ship_date desc, o.id limit 100");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaging_when_setOrderBy_expect_id_appendToOrderBy() {
|
||||
|
||||
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.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() {
|
||||
|
||||
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.setMaxRows(100);
|
||||
query.order("id desc");
|
||||
PagedList<Order> pagedList = query.findPagedList();
|
||||
pagedList.getList();
|
||||
pagedList.getTotalCount();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("order by o.id desc limit 100");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestRawSqlOrmQueryDistinct extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql0 = RawSqlBuilder.parse(" select distinct r.id, r.name from o_customer r ")
|
||||
.create();
|
||||
|
||||
Query<Customer> query0 = Ebean.find(Customer.class);
|
||||
query0.setRawSql(rawSql0);
|
||||
query0.where().ilike("name", "r%");
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(" select distinct r.id, r.name from o_customer r ")
|
||||
.columnMapping("r.id", "id").columnMapping("r.name", "name").create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
query.fetch("contacts", new FetchConfig().query());
|
||||
query.filterMany("contacts").gt("lastName", "b");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderAggregate;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestRawSqlOrmWrapper extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String sql = " select order_id, o.status, c.id, c.name, sum(d.order_qty*d.unit_price) as totalAmount"
|
||||
+ " from o_order o"
|
||||
+ " join o_customer c on c.id = o.kcustomer_id "
|
||||
+ " join o_order_detail d on d.order_id = o.id "
|
||||
+ " group by order_id, o.status, c.id, c.name ";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(sql).columnMapping("order_id", "order.id")
|
||||
.columnMapping("o.status", "order.status").columnMapping("c.id", "order.customer.id")
|
||||
.columnMapping("c.name", "order.customer.name")
|
||||
// .columnMapping("sum(d.order_qty*d.unit_price)", "totalAmount")
|
||||
.create();
|
||||
|
||||
Query<OrderAggregate> query = Ebean.find(OrderAggregate.class);
|
||||
query.setRawSql(rawSql)
|
||||
// .fetch("order.details", new FetchConfig().query())
|
||||
.where().gt("order.id", 0).having().gt("totalAmount", 20);
|
||||
|
||||
List<OrderAggregate> list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
output(list);
|
||||
|
||||
List<OrderAggregate> list2 = Ebean.find(OrderAggregate.class).setRawSql(rawSql)
|
||||
// .fetch("order.details", new FetchConfig().query())
|
||||
.where().gt("order.id", 2).having().gt("totalAmount", 10).findList();
|
||||
|
||||
output(list2);
|
||||
|
||||
}
|
||||
|
||||
private void output(List<OrderAggregate> list) {
|
||||
|
||||
for (OrderAggregate oa : list) {
|
||||
|
||||
Order order = oa.getOrder();
|
||||
order.getId();
|
||||
order.getStatus();
|
||||
oa.getTotalAmount();
|
||||
|
||||
Customer c = order.getCustomer();
|
||||
c.getId();
|
||||
c.getName();
|
||||
|
||||
// invoke lazy loading as this property
|
||||
// has not populated originally
|
||||
// order.getOrderDate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderAggregate;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestRawSqlOrmWrapper2 extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String sql = " select order_id, 'ignoreMe', sum(d.order_qty*d.unit_price) as totalAmount "
|
||||
+ " from o_order_detail d group by order_id ";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(sql).columnMapping("order_id", "order.id")
|
||||
.columnMappingIgnore("'ignoreMe'")
|
||||
// don't need this when using column alias
|
||||
// .columnMapping("sum(d.order_qty*d.unit_price)", "totalAmount")
|
||||
.create();
|
||||
|
||||
Query<OrderAggregate> query = Ebean.find(OrderAggregate.class);
|
||||
query.setRawSql(rawSql).fetch("order", "status,orderDate", new FetchConfig().query())
|
||||
.fetch("order.customer", "name").where().gt("order.id", 0).having().gt("totalAmount", 20)
|
||||
.order().desc("totalAmount").setMaxRows(10);
|
||||
|
||||
List<OrderAggregate> list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
for (OrderAggregate oa : list) {
|
||||
oa.getTotalAmount();
|
||||
Order order = oa.getOrder();
|
||||
order.getId();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderAggregate;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestRawSqlOrmWrapper3 extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String sql = " select order_id, o.status, c.id, c.name, sum(d.order_qty*d.unit_price) as totalAmount"
|
||||
+ " from o_order o"
|
||||
+ " join o_customer c on c.id = o.kcustomer_id "
|
||||
+ " join o_order_detail d on d.order_id = o.id "
|
||||
+ " group by order_id, o.status, c.id, c.name ";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(sql).columnMapping("order_id", "order.id")
|
||||
.columnMapping("o.status", "order.status").columnMapping("c.id", "order.customer.id")
|
||||
.columnMapping("c.name", "order.customer.name")
|
||||
// .columnMapping("sum(d.order_qty*d.unit_price)", "totalAmount")
|
||||
.create();
|
||||
|
||||
List<OrderAggregate> list2 = Ebean.find(OrderAggregate.class).setRawSql(rawSql)
|
||||
.fetch("order", new FetchConfig().query())
|
||||
.fetch("order.details", new FetchConfig().query())
|
||||
.where().gt("order.id", 2)
|
||||
.having().gt("totalAmount", 10)
|
||||
.filterMany("order.details").gt("unitPrice", 2d)
|
||||
.findList();
|
||||
|
||||
output(list2);
|
||||
|
||||
}
|
||||
|
||||
private void output(List<OrderAggregate> list) {
|
||||
|
||||
for (OrderAggregate oa : list) {
|
||||
oa.getTotalAmount();
|
||||
Order order = oa.getOrder();
|
||||
order.getId();
|
||||
order.getStatus();
|
||||
|
||||
Customer c = order.getCustomer();
|
||||
c.getId();
|
||||
c.getName();
|
||||
|
||||
// invoke lazy loading as this property
|
||||
// has not populated originally
|
||||
// order.getOrderDate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestRawSqlPositionedParams extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.parse("select r.id, r.name from o_customer r where r.id >= ? and r.name like ?")
|
||||
.create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.setParameter(1, 1);
|
||||
query.setParameter(2, "R%");
|
||||
query.where().lt("id", 2001);
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
|
||||
assertNotNull(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_unparsed() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select r.id, r.name from o_customer r where r.id >= ? and r.name like ?")
|
||||
.columnMapping("r.id", "id")
|
||||
.columnMapping("r.name", "name").create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.setParameter(1, 1);
|
||||
query.setParameter(2, "R%");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
|
||||
assertNotNull(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestRawSqlUnparsedQuery extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testDoubleUnparsedQuery() {
|
||||
|
||||
// Checks for RawSql caching issue https://github.com/ebean-orm/avaje-ebeanorm/issues/259
|
||||
ResetBasicData.reset();
|
||||
|
||||
test();
|
||||
test();
|
||||
}
|
||||
|
||||
private static void test() {
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select r.id, r.name from o_customer r where r.id >= :a and r.name like :b")
|
||||
.columnMapping("r.id", "id").columnMapping("r.name", "name").create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.setParameter("a", 1);
|
||||
query.setParameter("b", "R%");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
Assert.assertNotNull(list);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.Transaction;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
public class TestRawSqlWithResultSet extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() throws SQLException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// Transaction supplies our jdbc Connection
|
||||
Transaction txn = Ebean.beginTransaction();
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
|
||||
try {
|
||||
pstmt = txn.getConnection().prepareStatement("select id, name, billing_address_id from o_customer");
|
||||
|
||||
// ResultSet will be closed by Ebean
|
||||
ResultSet resultSet = pstmt.executeQuery();
|
||||
|
||||
RawSql rawSql = new RawSql(resultSet, "id", "name", "billingAddress.id");
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class)
|
||||
.setRawSql(rawSql)
|
||||
// also test a secondary query join
|
||||
.fetch("billingAddress", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
for (Customer customer : list) {
|
||||
customer.getId();
|
||||
customer.getName();
|
||||
customer.getBillingAddress();
|
||||
}
|
||||
|
||||
} finally {
|
||||
close(pstmt);
|
||||
txn.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void close(Statement stmt) {
|
||||
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.tests.rawsql.inherit;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.avaje.test.model.rawsql.inherit.ChildA;
|
||||
import org.avaje.test.model.rawsql.inherit.ChildB;
|
||||
import org.avaje.test.model.rawsql.inherit.Data;
|
||||
import org.avaje.test.model.rawsql.inherit.Parent;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class ParentQueryTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void QueryParentCollectionFetch() {
|
||||
|
||||
List<Data> exampleData = new ArrayList<>();
|
||||
exampleData.add(new Data(0));
|
||||
exampleData.add(new Data(1));
|
||||
exampleData.add(new Data(2));
|
||||
|
||||
ChildA a = new ChildA(0, "PA");
|
||||
a.setData(exampleData);
|
||||
Ebean.save(a);
|
||||
|
||||
ChildB b = new ChildB(1, "PB");
|
||||
b.setData(exampleData);
|
||||
Ebean.save(b);
|
||||
|
||||
ChildA c = new ChildA(2, "PC");
|
||||
c.setData(exampleData);
|
||||
Ebean.save(c);
|
||||
|
||||
List<Parent> partial = Ebean.find(Parent.class).where().ge("val", 1).findList();
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertEquals(partial.get(0).getData().get(0).getVal().intValue(), 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
package org.tests.rawsql.inherit;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.avaje.test.model.rawsql.inherit.ChildA;
|
||||
import org.avaje.test.model.rawsql.inherit.ChildB;
|
||||
import org.avaje.test.model.rawsql.inherit.Data;
|
||||
import org.avaje.test.model.rawsql.inherit.EUncle;
|
||||
import org.avaje.test.model.rawsql.inherit.Parent;
|
||||
import org.avaje.test.model.rawsql.inherit.ParentAggregate;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ParentRawSqlTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void RawSqlParentLoad() {
|
||||
|
||||
List<Data> exampleData = new ArrayList<>();
|
||||
exampleData.add(new Data(0));
|
||||
exampleData.add(new Data(1));
|
||||
exampleData.add(new Data(2));
|
||||
|
||||
ChildA a = new ChildA(0, "PA");
|
||||
a.setData(exampleData);
|
||||
Ebean.save(a);
|
||||
|
||||
ChildB b = new ChildB(1, "PB");
|
||||
b.setData(exampleData);
|
||||
Ebean.save(b);
|
||||
|
||||
ChildA c = new ChildA(2, "PC");
|
||||
c.setData(exampleData);
|
||||
Ebean.save(c);
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.parse("select type, id, val from rawinherit_parent where val > 1")
|
||||
.create();
|
||||
|
||||
List<Parent> partial = Ebean.find(Parent.class)
|
||||
.setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertEquals(partial.get(0).getData().get(0).getVal().intValue(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RawSqlParentFetch() {
|
||||
|
||||
List<Data> exampleData = new ArrayList<>();
|
||||
exampleData.add(new Data(0));
|
||||
exampleData.add(new Data(1));
|
||||
exampleData.add(new Data(2));
|
||||
|
||||
ChildA a = new ChildA(0, "PA");
|
||||
a.setData(exampleData);
|
||||
Ebean.save(a);
|
||||
|
||||
ChildB b = new ChildB(1, "PB");
|
||||
b.setData(exampleData);
|
||||
Ebean.save(b);
|
||||
|
||||
ChildA c = new ChildA(2, "PC");
|
||||
c.setData(exampleData);
|
||||
Ebean.save(c);
|
||||
|
||||
EUncle e1 = new EUncle();
|
||||
e1.setParent(b);
|
||||
e1.setName("fester");
|
||||
Ebean.save(e1);
|
||||
|
||||
joinToInheritanceHierarchy_withAliasMapping();
|
||||
joinToInheritanceHierarchy_bug416();
|
||||
joinToInheritanceHierarchy_with_queryJoin();
|
||||
joinToInheritanceHierarchy_withIgnore();
|
||||
|
||||
useColumnMappingIgnore();
|
||||
useColumnMappingWithDiscriminator();
|
||||
useExtraColumnMappingIgnore();
|
||||
}
|
||||
|
||||
|
||||
private void joinToInheritanceHierarchy_bug416() {
|
||||
|
||||
// For bug 416 we need the parent.more to trigger it
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select u.id, u.name, p.type as p_type, p.id as p_id, p.more as p_more from rawinherit_uncle u join rawinherit_parent p on p.id = u.parent_id")
|
||||
.columnMapping("id", "id")
|
||||
.columnMapping("name", "name")
|
||||
.columnMapping("p_type", "parent.type")
|
||||
.columnMapping("p_id", "parent.id")
|
||||
.columnMapping("p_more", "parent.more")
|
||||
.create();
|
||||
|
||||
List<EUncle> uncles = Ebean.find(EUncle.class).setRawSql(rawSql).findList();
|
||||
|
||||
assertNotNull(uncles.get(0));
|
||||
Parent parent = uncles.get(0).getParent();
|
||||
assertTrue(parent instanceof ChildB);
|
||||
}
|
||||
|
||||
private void joinToInheritanceHierarchy_with_queryJoin() {
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select u.id, u.name, p.type as p_type, p.id as p_id from rawinherit_uncle u join rawinherit_parent p on p.id = u.parent_id")
|
||||
.columnMapping("id", "id")
|
||||
.columnMapping("name", "name")
|
||||
.columnMapping("p_type", "parent.type")
|
||||
.columnMapping("p_id", "parent.id")
|
||||
.create();
|
||||
|
||||
List<EUncle> uncles = Ebean.find(EUncle.class).setRawSql(rawSql)
|
||||
.fetch("parent", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
assertNotNull(uncles.get(0));
|
||||
Parent parent = uncles.get(0).getParent();
|
||||
assertTrue(parent instanceof ChildB);
|
||||
}
|
||||
|
||||
private void joinToInheritanceHierarchy_withIgnore() {
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select u.id, u.name, p.type as ptype, p.id as pid from rawinherit_uncle u join rawinherit_parent p on p.id = u.parent_id")
|
||||
.columnMapping("id", "id")
|
||||
.columnMapping("name", "name")
|
||||
.columnMappingIgnore("ptype")
|
||||
.columnMapping("pid", "parent.id")
|
||||
.create();
|
||||
|
||||
List<EUncle> uncles = Ebean.find(EUncle.class).setRawSql(rawSql)
|
||||
.fetch("parent", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
assertNotNull(uncles.get(0));
|
||||
Parent parent = uncles.get(0).getParent();
|
||||
assertTrue(parent instanceof ChildB);
|
||||
}
|
||||
|
||||
private void joinToInheritanceHierarchy_withAliasMapping() {
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.parse("select u.id, u.name, p.type, p.id from rawinherit_uncle u join rawinherit_parent p on p.id = u.parent_id")
|
||||
.tableAliasMapping("p", "parent")
|
||||
.create();
|
||||
|
||||
List<EUncle> uncles = Ebean.find(EUncle.class).setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
assertNotNull(uncles.get(0));
|
||||
Parent parent = uncles.get(0).getParent();
|
||||
assertTrue(parent instanceof ChildB);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the discriminator column - columnMapping("type", "parent.type").
|
||||
*/
|
||||
private void useColumnMappingWithDiscriminator() {
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select type, id from rawinherit_parent where val > 1")
|
||||
.columnMapping("type", "parent.type") // can map the discriminator column 'type'
|
||||
.columnMapping("id", "parent.id")
|
||||
.create();
|
||||
|
||||
List<ParentAggregate> aggregates = Ebean.find(ParentAggregate.class).setRawSql(rawSql)
|
||||
.fetch("parent", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
List<Parent> partial = new ArrayList<>();
|
||||
for (ParentAggregate aggregate : aggregates) {
|
||||
partial.add(aggregate.parent);
|
||||
}
|
||||
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertEquals(partial.get(0).getData().get(0).getVal().intValue(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use columnMappingIgnore() to ignore the discriminator column.
|
||||
*/
|
||||
private void useColumnMappingIgnore() {
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select type, id from rawinherit_parent where val > 1")
|
||||
.columnMappingIgnore("type") // can ignore the discriminator 'type'
|
||||
.columnMapping("id", "parent.id")
|
||||
.create();
|
||||
|
||||
List<ParentAggregate> aggregates = Ebean.find(ParentAggregate.class).setRawSql(rawSql)
|
||||
.fetch("parent", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
List<Parent> partial = new ArrayList<>();
|
||||
for (ParentAggregate aggregate : aggregates) {
|
||||
partial.add(aggregate.parent);
|
||||
}
|
||||
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertEquals(partial.get(0).getData().get(0).getVal().intValue(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extra columnMappingIgnore() before and after.
|
||||
*/
|
||||
private void useExtraColumnMappingIgnore() {
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select 'a', type, id, 'b' from rawinherit_parent where val > 1")
|
||||
.columnMappingIgnore("a") // extra ignore before
|
||||
.columnMappingIgnore("type")
|
||||
.columnMapping("id", "parent.id")
|
||||
.columnMappingIgnore("b") // extra ignore after
|
||||
.create();
|
||||
|
||||
List<ParentAggregate> aggregates = Ebean.find(ParentAggregate.class).setRawSql(rawSql)
|
||||
.fetch("parent", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
List<Parent> partial = new ArrayList<>();
|
||||
for (ParentAggregate aggregate : aggregates) {
|
||||
partial.add(aggregate.parent);
|
||||
}
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertEquals(partial.get(0).getData().get(0).getVal().intValue(), 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package org.tests.rawsql.nativesql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.EbeanServer;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestNativeSqlBasic extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void noBindParams() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
|
||||
String nativeSql = "select id, name from o_customer";
|
||||
|
||||
Query<Customer> query = server.findNative(Customer.class, nativeSql);
|
||||
|
||||
List<Customer> customers = query.findList();
|
||||
assertThat(customers).isNotEmpty();
|
||||
|
||||
BeanState beanState = server.getBeanState(customers.get(0));
|
||||
assertThat(beanState.getLoadedProps()).contains("id", "name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectAll() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
|
||||
String nativeSql = "select * from o_customer";
|
||||
Query<Customer> query = server.findNative(Customer.class, nativeSql);
|
||||
|
||||
List<Customer> customers = query.findList();
|
||||
assertThat(customers).isNotEmpty();
|
||||
|
||||
BeanState beanState = server.getBeanState(customers.get(0));
|
||||
assertThat(beanState.getLoadedProps().size()).isGreaterThan(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectWithAssocOne() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
|
||||
String nativeSql = "select c.*, b.city from o_customer c join o_address b on b.id = c.billing_address_id";
|
||||
Query<Customer> query = server.findNative(Customer.class, nativeSql);
|
||||
|
||||
List<Customer> customers = query.findList();
|
||||
assertThat(customers).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void namedParam() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
|
||||
String nativeSql = "select id, name from o_customer where id > :some";
|
||||
|
||||
Query<Customer> query = server.findNative(Customer.class, nativeSql);
|
||||
query.setParameter("some", 1);
|
||||
|
||||
List<Customer> customers = query.findList();
|
||||
assertThat(customers).isNotEmpty();
|
||||
|
||||
Query<Customer> query2 = server.findNative(Customer.class, nativeSql);
|
||||
query2.setParameter("some", 2);
|
||||
|
||||
List<Customer> customers2 = query2.findList();
|
||||
assertThat(customers2).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void partialAndLazyLoad() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
|
||||
String nativeSql = "select id, name from o_customer where id > ?";
|
||||
|
||||
List<Customer> customers = server.findNative(Customer.class, nativeSql)
|
||||
.setParameter(1, 1)
|
||||
.findList();
|
||||
|
||||
for (Customer customer : customers) {
|
||||
customer.getStatus();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void partialAssoc() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
|
||||
String nativeSql = "select o.id, o.status, c.id, c.name, c.version from o_order o join o_customer c on c.id = o.kcustomer_id ";
|
||||
|
||||
List<Order> orders = server.findNative(Order.class, nativeSql)
|
||||
.findList();
|
||||
|
||||
for (Order order : orders) {
|
||||
order.getStatus();
|
||||
order.getCustomer().getName();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchQuery() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select * from o_customer where id > ?";
|
||||
List<Customer> customers =
|
||||
Ebean.findNative(Customer.class, nativeSql)
|
||||
.setParameter(1, 1)
|
||||
.fetchQuery("contacts")
|
||||
.findList();
|
||||
|
||||
assertThat(customers).isNotEmpty();
|
||||
|
||||
BeanState beanState = Ebean.getBeanState(customers.get(0));
|
||||
assertThat(beanState.getLoadedProps().size()).isGreaterThan(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void columnAlias() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select c.id, 'foo' as name from o_customer c";
|
||||
List<Customer> customers =
|
||||
Ebean.findNative(Customer.class, nativeSql)
|
||||
.findList();
|
||||
|
||||
assertThat(customers).isNotEmpty();
|
||||
|
||||
BeanState beanState = Ebean.getBeanState(customers.get(0));
|
||||
assertThat(beanState.getLoadedProps()).contains("id", "name");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user