mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
@@ -0,0 +1,30 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.annotation.Sql;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@Sql
|
||||
public class A2Customer {
|
||||
|
||||
private long custId;
|
||||
|
||||
private String customerName;
|
||||
|
||||
public long getCustId() {
|
||||
return custId;
|
||||
}
|
||||
|
||||
public void setCustId(long custId) {
|
||||
this.custId = custId;
|
||||
}
|
||||
|
||||
public String getCustomerName() {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public void setCustomerName(String customerName) {
|
||||
this.customerName = customerName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.annotation.Sql;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@Sql
|
||||
public class ACustomer {
|
||||
|
||||
private long custId;
|
||||
|
||||
@Column(name = "customerName")
|
||||
private String custName;
|
||||
|
||||
|
||||
public long getCustId() {
|
||||
return custId;
|
||||
}
|
||||
|
||||
public void setCustId(long custId) {
|
||||
this.custId = custId;
|
||||
}
|
||||
|
||||
public String getCustName() {
|
||||
return custName;
|
||||
}
|
||||
|
||||
public void setCustName(String custName) {
|
||||
this.custName = custName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.SqlUpdate;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.idkeys.db.AuditLog;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
public class TestInsertSqlLogging extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
DB.find(AuditLog.class).where().ge("id", 10000).delete();
|
||||
|
||||
String sql = "insert into audit_log (id, description, modified_description) values (?,?,?)";
|
||||
SqlUpdate insert = DB.sqlUpdate(sql);
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
txn.setBatchMode(true);
|
||||
txn.setBatchSize(2);
|
||||
|
||||
insert.setParameter(10000);
|
||||
insert.setParameter("hello");
|
||||
insert.setParameter("rob");
|
||||
insert.execute();
|
||||
|
||||
insert.setParameter(10001);
|
||||
insert.setParameter("goodbye");
|
||||
insert.setParameter("rob");
|
||||
insert.execute();
|
||||
|
||||
insert.setParameter(10002);
|
||||
insert.setParameter("chow");
|
||||
insert.setParameter("bob");
|
||||
insert.execute();
|
||||
|
||||
txn.commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addBatch_executeBatch() {
|
||||
|
||||
DB.find(AuditLog.class).where().ge("id", 10000).delete();
|
||||
|
||||
String sql = "insert into audit_log (id, description, modified_description) values (?,?,?)";
|
||||
SqlUpdate insert = DB.sqlUpdate(sql);
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
|
||||
insert.setParameter(10000);
|
||||
insert.setParameter("hello");
|
||||
insert.setParameter("rob");
|
||||
insert.addBatch();
|
||||
|
||||
insert.setParameter(10001);
|
||||
insert.setParameter("goodbye");
|
||||
insert.setParameter("rob");
|
||||
insert.addBatch();
|
||||
|
||||
insert.setParameter(10002);
|
||||
insert.setParameter("chow");
|
||||
insert.setParameter("bob");
|
||||
insert.addBatch();
|
||||
|
||||
int[] rows = insert.executeBatch();
|
||||
System.out.println("Rows was " + Arrays.toString(rows));
|
||||
|
||||
txn.commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addBatch_namedParams() {
|
||||
|
||||
DB.find(AuditLog.class).where().ge("id", 10000).delete();
|
||||
|
||||
String sql = "insert into audit_log (id, description, modified_description) values (:id, :desc, :modDesc)";
|
||||
SqlUpdate insert = DB.sqlUpdate(sql);
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
|
||||
insert.setParameter("id", 20000);
|
||||
insert.setParameter("desc", "hello");
|
||||
insert.setParameter("modDesc", "rob");
|
||||
insert.addBatch();
|
||||
|
||||
insert.setParameter("id", 20001);
|
||||
insert.setParameter("desc", "goodbye");
|
||||
insert.setParameter("modDesc", "rob");
|
||||
insert.addBatch();
|
||||
|
||||
insert.setParameter("id", 20002);
|
||||
insert.setParameter("desc", "chow");
|
||||
insert.setParameter("modDesc", "bob");
|
||||
insert.addBatch();
|
||||
|
||||
int[] rows = insert.executeBatch();
|
||||
System.out.println("Rows was " + Arrays.toString(rows));
|
||||
|
||||
txn.commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_trim_leadingSpaces_eventFound() {
|
||||
|
||||
Transaction transaction = DB.beginTransaction();
|
||||
try {
|
||||
String upd = " update audit_log set description='junk' where id = 10001";
|
||||
SqlUpdate update = DB.sqlUpdate(upd);
|
||||
update.execute();
|
||||
transaction.commit();
|
||||
|
||||
assertFalse(((SpiTransaction) transaction).getEvent().getEventTables().isEmpty());
|
||||
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
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.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestOrderReportTotal extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = getRawSql();
|
||||
|
||||
Query<OrderAggregate> query = DB.createQuery(OrderAggregate.class);
|
||||
|
||||
List<OrderAggregate> list = query.setRawSql(rawSql).findList();
|
||||
assertNotNull(list);
|
||||
|
||||
Query<OrderAggregate> q2 = DB.createQuery(OrderAggregate.class).setRawSql(rawSql);
|
||||
q2.where().gt("id", 1);
|
||||
q2.having().gt("totalItems", 1);
|
||||
|
||||
List<OrderAggregate> l2 = q2.findList();
|
||||
assertNotNull(l2);
|
||||
|
||||
Query<OrderAggregate> q3 = DB.createQuery(OrderAggregate.class).setRawSql(rawSql);
|
||||
q3.where().eq("order_id", 1);
|
||||
|
||||
OrderAggregate orderAggregate = q3.findOne();
|
||||
assertNotNull(orderAggregate);
|
||||
}
|
||||
|
||||
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 = DB.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.DB;
|
||||
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.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
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 = DB.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
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 = DB.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
list = query.findList();
|
||||
assertNotNull(list);
|
||||
|
||||
rawSql =
|
||||
RawSqlBuilder
|
||||
.parse("select r.id, r.name||'hello' name from o_customer r ")
|
||||
.create();
|
||||
query = DB.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
list = query.findList();
|
||||
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 = DB.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
list = query.findList();
|
||||
assertNotNull(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
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.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
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 = DB.find(CustomerAggregate.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ge("customer.id", 1);
|
||||
|
||||
List<CustomerAggregate> list = query.findList();
|
||||
assertNotNull(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
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.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
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 = DB.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 = DB.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 = DB.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 = DB.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 = DB.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);
|
||||
|
||||
DB.save(basic);
|
||||
|
||||
String rs = "select b.status, b.name from e_basic b ";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs).create();
|
||||
|
||||
List<EBasic> list = DB.find(EBasic.class)
|
||||
.setRawSql(rawSql)
|
||||
.where().eq("name", name)
|
||||
.findList();
|
||||
|
||||
assertEquals(1, list.size());
|
||||
EBasic basic1 = list.get(0);
|
||||
basic1.setDescription("insertAfterRawFetch");
|
||||
|
||||
DB.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 = DB.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 = DB.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 = DB.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 = DB.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,83 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.CallableSql;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.ForPlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
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 = DB.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.setParameter("name", "R%");
|
||||
query.setParameter("id", 0);
|
||||
query.where().lt("id", 1000);
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
assertNotNull(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform(Platform.MYSQL)
|
||||
public void testMySqlColonEquals() throws SQLException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Transaction transaction = DB.beginTransaction();
|
||||
|
||||
try {
|
||||
if ("MariaDB connector/J".equals(transaction.connection().getMetaData().getDriverName())) {
|
||||
return; // MariaDb only supports callable statements in the form "? = call function x(?)"
|
||||
}
|
||||
CallableSql callableSql = DB.createCallableSql("set @total = 0");
|
||||
DB.getDefault().execute(callableSql);
|
||||
|
||||
String sql = "select id, @total := 0 + id as total_items from o_order";
|
||||
|
||||
Query<Order> query1 = DB.findNative(Order.class, sql);
|
||||
List<Order> list = query1.findList();
|
||||
|
||||
assertThat(list.get(0).getTotalItems()).isNotNull();
|
||||
|
||||
DB.getDefault().execute(callableSql);
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.parse(sql)
|
||||
.create();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.setRawSql(rawSql);
|
||||
|
||||
List<Order> list1 = query.findList();
|
||||
assertThat(list1.get(0).getTotalItems()).isNotNull();
|
||||
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
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 io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
|
||||
@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 = DB.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
query.fetchQuery("contacts");
|
||||
query.filterMany("contacts").gt("lastName", "b");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
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
|
||||
@IgnorePlatform({Platform.MYSQL, Platform.MARIADB, Platform.SQLSERVER})
|
||||
public void test_upperCaseSql() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse("select id, NAME from O_CUSTOMER ").create();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
assertNotNull(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFirstRowsMaxRows() throws InterruptedException, ExecutionException {
|
||||
|
||||
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 = DB.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
int initialRowCount = query.findCount();
|
||||
|
||||
query.setFirstRow(1);
|
||||
query.setMaxRows(2);
|
||||
query.order("id");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
|
||||
int rowCount = query.findCount();
|
||||
FutureRowCount<Customer> futureRowCount = query.findFutureCount();
|
||||
|
||||
assertEquals(initialRowCount, rowCount);
|
||||
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 = DB.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();
|
||||
|
||||
assertEquals(2, list.size());
|
||||
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 = DB.find(Order.class);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
query.setMaxRows(100);
|
||||
query.findList();
|
||||
|
||||
if (isSqlServer()) {
|
||||
assertSql(query).contains("top 100 ");
|
||||
assertSql(query).contains("order by o.ship_date desc");
|
||||
} else if (isOracle()) {
|
||||
assertSql(query).contains("fetch next 100 rows only");
|
||||
} else {
|
||||
assertSql(query).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 = DB.find(Order.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.orderById(true);
|
||||
|
||||
query.setMaxRows(100);
|
||||
query.findList();
|
||||
|
||||
if (isSqlServer()) {
|
||||
assertSql(query).contains("top 100 ");
|
||||
assertSql(query).contains("order by o.ship_date desc, o.id");
|
||||
} else if (isOracle()) {
|
||||
assertSql(query).contains("fetch next 100 rows only");
|
||||
} else {
|
||||
assertSql(query).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)
|
||||
@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 = DB.find(Order.class);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
query.setMaxRows(100);
|
||||
|
||||
if (isSqlServer()) {
|
||||
query.order("coalesce(shipDate, getdate()) desc");
|
||||
query.findList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("order by coalesce(o.ship_date, getdate()) desc");
|
||||
assertThat(sqlOf(query)).contains("select top 100");
|
||||
|
||||
} else {
|
||||
query.order("coalesce(shipDate, now()) desc");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("order by coalesce(o.ship_date, now()) desc limit 100");
|
||||
}
|
||||
}
|
||||
|
||||
@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 = DB.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();
|
||||
|
||||
assertSql(query).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 = DB.find(Order.class);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
query.setMaxRows(100);
|
||||
query.order("id desc");
|
||||
PagedList<Order> pagedList = query.findPagedList();
|
||||
pagedList.getList();
|
||||
pagedList.getTotalCount();
|
||||
|
||||
if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("select top 100 ");
|
||||
assertThat(sqlOf(query)).contains("order by o.id desc");
|
||||
} else if (isOracle()) {
|
||||
assertThat(sqlOf(query)).contains("fetch next 100 rows only");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("order by o.id desc limit 100");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
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 = DB.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 = DB.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
query.where().ilike("name", "r%");
|
||||
|
||||
query.fetchQuery("contacts");
|
||||
query.filterMany("contacts").gt("lastName", "b");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
assertNotNull(list);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
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.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
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 = DB.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();
|
||||
assertNotNull(list);
|
||||
|
||||
output(list);
|
||||
|
||||
List<OrderAggregate> list2 = DB.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.DB;
|
||||
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.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
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 = DB.find(OrderAggregate.class);
|
||||
query.setRawSql(rawSql).fetchQuery("order", "status,orderDate")
|
||||
.fetch("order.customer", "name").where().gt("order.id", 0).having().gt("totalAmount", 20)
|
||||
.order().desc("totalAmount").setMaxRows(10);
|
||||
|
||||
List<OrderAggregate> list = query.findList();
|
||||
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.DB;
|
||||
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.jupiter.api.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 = DB.find(OrderAggregate.class).setRawSql(rawSql)
|
||||
.fetchQuery("order")
|
||||
.fetchQuery("order.details")
|
||||
.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,55 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestRawSqlPositionedParams extends BaseTestCase {
|
||||
|
||||
private static final RawSql RAWSQL_1 = RawSqlBuilder
|
||||
.parse("select r.id, r.name from o_customer r where r.id >= ? and r.name like ?")
|
||||
.create();
|
||||
|
||||
private static final RawSql RAW_SQL_2 = 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();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> list = DB.find(Customer.class)
|
||||
.setRawSql(RAWSQL_1)
|
||||
.setParameter(1)
|
||||
.setParameter("R%")
|
||||
.where().lt("id", 2001)
|
||||
.findList();
|
||||
|
||||
assertThat(list).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_unparsed() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> list = DB.find(Customer.class)
|
||||
.setRawSql(RAW_SQL_2)
|
||||
.setParameter(1)
|
||||
.setParameter("R%")
|
||||
.findList();
|
||||
|
||||
assertThat(list).isNotNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestRawSqlUnparsedQuery extends BaseTestCase {
|
||||
|
||||
private static final 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();
|
||||
|
||||
@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() {
|
||||
|
||||
List<Customer> list = DB.find(Customer.class)
|
||||
.setRawSql(rawSql)
|
||||
.setParameter("a", 1)
|
||||
.setParameter("b", "R%")
|
||||
.findList();
|
||||
|
||||
assertThat(list).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package org.tests.rawsql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import io.ebean.Transaction;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.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();
|
||||
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
|
||||
// Transaction supplies our jdbc Connection
|
||||
Transaction txn = DB.beginTransaction();
|
||||
try {
|
||||
pstmt = txn.connection().prepareStatement("select id, name, billing_address_id from o_customer");
|
||||
|
||||
// ResultSet will be closed by Ebean
|
||||
ResultSet resultSet = pstmt.executeQuery();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.resultSet(resultSet, "id", "name", "billingAddress.id");
|
||||
|
||||
List<Customer> list = DB.find(Customer.class)
|
||||
.setRawSql(rawSql)
|
||||
// also test a secondary query join
|
||||
.fetchQuery("billingAddress")
|
||||
.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,47 @@
|
||||
package org.tests.rawsql.inherit;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import org.tests.inherit.ChildA;
|
||||
import org.tests.inherit.ChildB;
|
||||
import org.tests.inherit.Data;
|
||||
import org.tests.inherit.Parent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.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(1000, "PQT-PA");
|
||||
a.setData(exampleData);
|
||||
DB.save(a);
|
||||
|
||||
ChildB b = new ChildB(1001, "PQT-PB");
|
||||
b.setData(exampleData);
|
||||
DB.save(b);
|
||||
|
||||
ChildA c = new ChildA(1002, "PQT-PC");
|
||||
c.setData(exampleData);
|
||||
DB.save(c);
|
||||
|
||||
List<Parent> partial = DB.find(Parent.class).where().ge("val", 1001).findList();
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertThat(partial.get(0).getMore()).startsWith("PQT-");
|
||||
assertEquals(partial.get(0).getData().get(0).getVal().intValue(), 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
package org.tests.rawsql.inherit;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.RawSql;
|
||||
import io.ebean.RawSqlBuilder;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.inherit.ChildA;
|
||||
import org.tests.inherit.ChildB;
|
||||
import org.tests.inherit.Data;
|
||||
import org.tests.inherit.EUncle;
|
||||
import org.tests.inherit.Parent;
|
||||
import org.tests.inherit.ParentAggregate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ParentRawSqlTest extends BaseTestCase {
|
||||
|
||||
@BeforeEach
|
||||
public void clearDb() {
|
||||
DB.deleteAll(DB.find(Data.class).findList());
|
||||
DB.deleteAll(DB.find(Parent.class).findList());
|
||||
//@rob: this does not work as it does not clear the ManyToMany relations.
|
||||
// DB.find(Data.class).delete();
|
||||
// DB.find(Parent.class).delete();
|
||||
}
|
||||
|
||||
@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);
|
||||
DB.save(a);
|
||||
|
||||
ChildB b = new ChildB(1, "PB");
|
||||
b.setData(exampleData);
|
||||
DB.save(b);
|
||||
|
||||
ChildA c = new ChildA(2, "PC");
|
||||
c.setData(exampleData);
|
||||
DB.save(c);
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.parse("select type, id, val from rawinherit_parent where val > 1")
|
||||
.create();
|
||||
|
||||
List<Parent> partial = DB.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);
|
||||
DB.save(a);
|
||||
|
||||
ChildB b = new ChildB(1, "PB");
|
||||
b.setData(exampleData);
|
||||
DB.save(b);
|
||||
|
||||
ChildA c = new ChildA(2, "PC");
|
||||
c.setData(exampleData);
|
||||
DB.save(c);
|
||||
|
||||
EUncle e1 = new EUncle();
|
||||
e1.setParent(b);
|
||||
e1.setName("fester");
|
||||
DB.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 = DB.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 = DB.find(EUncle.class).setRawSql(rawSql)
|
||||
.fetchQuery("parent")
|
||||
.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 = DB.find(EUncle.class).setRawSql(rawSql)
|
||||
.fetchQuery("parent")
|
||||
.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 = DB.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 = DB.find(ParentAggregate.class).setRawSql(rawSql)
|
||||
.fetchQuery("parent")
|
||||
.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 = DB.find(ParentAggregate.class).setRawSql(rawSql)
|
||||
.fetchQuery("parent")
|
||||
.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 = DB.find(ParentAggregate.class).setRawSql(rawSql)
|
||||
.fetchQuery("parent")
|
||||
.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,215 @@
|
||||
package org.tests.rawsql.nativesql;
|
||||
|
||||
import io.ebean.*;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestNativeSqlBasic extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void noBindParams() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select id, name from o_customer";
|
||||
|
||||
Query<Customer> query = DB.findNative(Customer.class, nativeSql);
|
||||
|
||||
List<Customer> customers = query.findList();
|
||||
assertThat(customers).isNotEmpty();
|
||||
|
||||
BeanState beanState = DB.beanState(customers.get(0));
|
||||
assertThat(beanState.loadedProps()).contains("id", "name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectAll() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select * from o_customer";
|
||||
Query<Customer> query = DB.findNative(Customer.class, nativeSql);
|
||||
|
||||
List<Customer> customers = query.findList();
|
||||
assertThat(customers).isNotEmpty();
|
||||
|
||||
BeanState beanState = DB.beanState(customers.get(0));
|
||||
assertThat(beanState.loadedProps().size()).isGreaterThan(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectWithAssocOne() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select c.*, b.city from o_customer c join o_address b on b.id = c.billing_address_id";
|
||||
Query<Customer> query = DB.findNative(Customer.class, nativeSql);
|
||||
|
||||
List<Customer> customers = query.findList();
|
||||
assertThat(customers).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void namedParam() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select id, name from o_customer where id > :some";
|
||||
|
||||
Query<Customer> query = DB.findNative(Customer.class, nativeSql);
|
||||
query.setParameter("some", 1);
|
||||
|
||||
List<Customer> customers = query.findList();
|
||||
assertThat(customers).isNotEmpty();
|
||||
|
||||
Query<Customer> query2 = DB.findNative(Customer.class, nativeSql);
|
||||
query2.setParameter("some", 2);
|
||||
|
||||
List<Customer> customers2 = query2.findList();
|
||||
assertThat(customers2).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withMaxRows() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select id, name from o_customer where id > :some";
|
||||
|
||||
Query<Customer> query = DB.findNative(Customer.class, nativeSql)
|
||||
.setParameter("some", 1)
|
||||
.setMaxRows(10);
|
||||
|
||||
query.findList();
|
||||
|
||||
if (isH2() || isPostgres()) {
|
||||
assertThat(sqlOf(query)).contains(" limit 10");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withFirstRowsMaxRows() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select id, name from o_customer where id > :some";
|
||||
|
||||
Query<Customer> query = DB.findNative(Customer.class, nativeSql)
|
||||
.setParameter("some", 1)
|
||||
.setFirstRow(20)
|
||||
.setMaxRows(10);
|
||||
|
||||
query.findList();
|
||||
|
||||
if (isH2() || isPostgres()) {
|
||||
assertThat(sqlOf(query)).contains(" limit 10 offset 20");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void partialAndLazyLoad() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select id, name from o_customer where id > ?";
|
||||
|
||||
List<Customer> customers = DB.findNative(Customer.class, nativeSql)
|
||||
.setParameter(1)
|
||||
.findList();
|
||||
|
||||
for (Customer customer : customers) {
|
||||
customer.getStatus();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Oracle does not support getTableName() via JDBC resultSet meta data
|
||||
*/
|
||||
@Test
|
||||
@IgnorePlatform({Platform.ORACLE})
|
||||
public void partialAssoc() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
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 = DB.findNative(Order.class, nativeSql)
|
||||
.findList();
|
||||
|
||||
for (Order order : orders) {
|
||||
order.getStatus();
|
||||
order.getCustomer().getName();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void partialAssocIncludingOracle() {
|
||||
ResetBasicData.reset();
|
||||
DB.cacheManager().clearAll();
|
||||
|
||||
String nativeSql = "select o.id, o.status, o.kcustomer_id from o_order o";
|
||||
List<Order> orders = DB.findNative(Order.class, nativeSql)
|
||||
.findList();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
for (Order order : orders) {
|
||||
order.getStatus();
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
assertThat(sql).isEmpty();
|
||||
|
||||
for (Order order : orders) {
|
||||
order.getCustomer().getName();
|
||||
}
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertSql(sql.get(0)).contains(" from o_customer t0 where ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchQuery() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select * from o_customer where id > ?";
|
||||
List<Customer> customers =
|
||||
DB.findNative(Customer.class, nativeSql)
|
||||
.setParameter(1)
|
||||
.fetchQuery("contacts")
|
||||
.findList();
|
||||
|
||||
assertThat(customers).isNotEmpty();
|
||||
|
||||
BeanState beanState = DB.beanState(customers.get(0));
|
||||
assertThat(beanState.loadedProps().size()).isGreaterThan(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void columnAlias() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String nativeSql = "select c.id, 'foo' as name from o_customer c";
|
||||
List<Customer> customers =
|
||||
DB.findNative(Customer.class, nativeSql)
|
||||
.findList();
|
||||
|
||||
assertThat(customers).isNotEmpty();
|
||||
|
||||
BeanState beanState = DB.beanState(customers.get(0));
|
||||
assertThat(beanState.loadedProps()).contains("id", "name");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.tests.rawsql.nativesql;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.embedded.EAddress;
|
||||
import org.tests.model.embedded.EPerson;
|
||||
import org.tests.model.json.PlainBean;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestNativeWithEmbedded extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Map<String, Object> rawMap = new LinkedHashMap<>();
|
||||
rawMap.put("a","1");
|
||||
EPerson person = new EPerson();
|
||||
person.setName("Frank");
|
||||
EAddress address = new EAddress();
|
||||
address.setStreet("1 foo st");
|
||||
address.setCity("barv");
|
||||
address.setJbean(new PlainBean("hi", 3));
|
||||
address.setJraw(rawMap);
|
||||
person.setAddress(address);
|
||||
|
||||
DB.save(person);
|
||||
|
||||
String sql = "select id, name, street, suburb, addr_city, addr_status, addr_jbean, jraw from eperson where id = ?";
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Query<EPerson> query = DB.findNative(EPerson.class, sql);
|
||||
query.setParameter(person.getId());
|
||||
EPerson one = query.findOne();
|
||||
|
||||
assertThat(one.getName()).isEqualTo("Frank");
|
||||
assertThat(one.getAddress().getStreet()).isEqualTo("1 foo st");
|
||||
assertThat(one.getAddress().getJbean().getName()).isEqualTo("hi");
|
||||
assertThat(one.getAddress().getJraw().get("a")).isEqualTo("1");
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user