#1955 - mapToScalar - Add SqlQuery.mapToScalar( <class> ).findOne() / findList() ... to deprecate findSingleAttribute()

This commit is contained in:
rob bygrave
2020-02-22 10:09:44 +13:00
parent 907d36123d
commit 92995b807e
6 changed files with 116 additions and 38 deletions
@@ -7,11 +7,12 @@ import org.junit.Test;
import org.tests.model.basic.Customer;
import java.sql.Types;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class TestTransient extends BaseTestCase {
@@ -65,34 +66,34 @@ public class TestTransient extends BaseTestCase {
int rows = DB.createUpdate(Customer.class, updateStmt)
.setParameter("id", custId).execute();
assertEquals(1, rows);
assertEquals("testTrans2", findNote(custId));
assertEquals("testTrans2", findNote(custId).get());
rows = DB.createUpdate(Customer.class, "update customer set smallnote = ? where id = ?")
.setNull(1, Types.VARCHAR)
.setParameter(2, custId).execute();
assertEquals(1, rows);
assertNull(findNote(custId));
assertThat(findNote(custId)).isEmpty();
rows = DB.createUpdate(Customer.class, "update customer set smallnote = ? where id = ?")
.setParameter(1, "Foo")
.setParameter(2, custId).execute();
assertEquals(1, rows);
assertEquals("Foo", findNote(custId));
assertEquals("Foo", findNote(custId).get());
rows = DB.createUpdate(Customer.class, "update customer set smallnote = :name where id = :id")
.setNullParameter("name", Types.VARCHAR)
.setParameter("id", custId).execute();
assertEquals(1, rows);
assertNull(findNote(custId));
assertThat(findNote(custId)).isEmpty();
// cleanup
DB.delete(Customer.class, custId);
}
private String findNote(Integer custId) {
private Optional<String> findNote(Integer custId) {
return DB.sqlQuery("select smallnote from o_customer where id = ?")
.setParameter(custId)
.findSingleAttribute(String.class);
.mapToScalar(String.class).findOneOrEmpty();
}
}
@@ -39,7 +39,7 @@ public class TestSqlRowUUID extends BaseTestCase {
SqlQuery q2 = DB.sqlQuery("select id from tuuid_entity where id = :id");
q2.setParameter("id", e.getId());
UUID value = q2.findSingleAttribute(UUID.class);
UUID value = q2.mapToScalar(UUID.class).findOne();
assertThat(value).isEqualTo(e.getId());
@@ -34,7 +34,8 @@ public class SqlQueryTests extends BaseTestCase {
List<BigDecimal> lineAmounts = DB.sqlQuery(sql)
.setParameter(3)
.findSingleAttributeList(BigDecimal.class);
.mapToScalar(BigDecimal.class)
.findList();
assertThat(lineAmounts).isNotEmpty();
}
@@ -48,7 +49,8 @@ public class SqlQueryTests extends BaseTestCase {
BigDecimal maxPrice = DB.sqlQuery(sql)
.setParameter(1, 2)
.findSingleDecimal();
.mapToScalar(BigDecimal.class)
.findOne();
assertThat(maxPrice).isNotNull();
}
@@ -62,7 +64,8 @@ public class SqlQueryTests extends BaseTestCase {
BigDecimal maxPrice = DB.sqlQuery(sql)
.setParameter(2)
.findSingleAttribute(BigDecimal.class);
.mapToScalar(BigDecimal.class)
.findOne();
assertThat(maxPrice).isNotNull();
}
@@ -76,7 +79,8 @@ public class SqlQueryTests extends BaseTestCase {
long count = DB.sqlQuery(sql)
.setParameter(1, 2)
.findSingleLong();
.mapToScalar(Long.class)
.findOne();
assertThat(count).isGreaterThan(0);
}
@@ -91,7 +95,7 @@ public class SqlQueryTests extends BaseTestCase {
long count = DB.sqlQuery(sql)
.setParameter(1, 2)
.findSingleAttribute(Long.class);
.mapToScalar(Long.class).findOne();
assertThat(count).isGreaterThan(0);
}
@@ -105,7 +109,7 @@ public class SqlQueryTests extends BaseTestCase {
OffsetDateTime minCreated = DB.sqlQuery(sql)
.setParameter(1, 2)
.findSingleAttribute(OffsetDateTime.class);
.mapToScalar(OffsetDateTime.class).findOne();
assertThat(minCreated).isBefore(OffsetDateTime.now());
}
@@ -27,7 +27,8 @@ public class TestSqlUpdateExceptions extends BaseTestCase {
UUID foundId = DB.sqlQuery("select id from uuone where id = ?")
.setParameters(id)
.findSingleAttribute(UUID.class);
.mapToScalar(UUID.class)
.findOne();
assertThat(foundId).isEqualTo(id);
}