Compare commits

...
6 changed files with 129 additions and 6 deletions
+2 -2
View File
@@ -9,7 +9,7 @@
<groupId>io.ebean</groupId>
<artifactId>ebean</artifactId>
<version>11.22.7</version>
<version>11.22.8</version>
<packaging>jar</packaging>
<name>ebean</name>
@@ -22,7 +22,7 @@
<scm>
<developerConnection>scm:git:git@github.com:ebean-orm/ebean.git</developerConnection>
<tag>ebean-11.22.7</tag>
<tag>ebean-11.22.8</tag>
</scm>
<profiles>
@@ -65,7 +65,7 @@ import java.util.List;
*/
public class DefaultDbMigration implements DbMigration {
protected static final Logger logger = LoggerFactory.getLogger(DefaultDbMigration.class);
protected static final Logger logger = LoggerFactory.getLogger("io.ebean.GenerateMigration");
private static final String initialVersion = "1.0";
@@ -148,7 +148,10 @@ class SqlTreeNodeBean implements SqlTreeNode {
public ScalarType<?> getSingleAttributeScalarType() {
if (properties == null || properties.length == 0) {
// if we have no property ask first children (in a distinct select with join)
// if we have also no children, NPE happens anyway.
if (children.length == 0) {
// expected to be a findIds query
return desc.getIdBinder().getBeanProperty().getScalarType();
}
return children[0].getSingleAttributeScalarType();
}
if (properties[0] instanceof STreePropertyAssocOne) {
@@ -769,6 +769,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
copy.mapKey = mapKey;
copy.id = id;
copy.label = label;
copy.nativeSql = nativeSql;
copy.useBeanCache = useBeanCache;
copy.useQueryCache = useQueryCache;
copy.readOnly = readOnly;
@@ -1050,7 +1051,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
CQueryPlanKey createQueryPlanKey() {
if (isNativeSql()) {
queryPlanKey = new NativeSqlQueryPlanKey(nativeSql + "-" + firstRow + "-" + maxRows);
queryPlanKey = new NativeSqlQueryPlanKey(type.ordinal() + nativeSql + "-" + firstRow + "-" + maxRows);
} else {
queryPlanKey = new OrmQueryPlanKey(planDescription(), maxRows, firstRow, rawSql);
}
@@ -36,6 +36,9 @@ create or replace function _partition_create(meta partition_meta, extra text)
language plpgsql
set timezone to 'UTC'
as $$
declare
idx_col text;
idx_name text;
begin
execute format('create table if not exists %I partition of %I for values from (''%s'') TO (''%s'')', meta.part_name, meta.base_name, meta.period_start, meta.period_end);
@@ -45,7 +48,12 @@ begin
end if;
if (length(meta.index_column) > 0) then
execute format('create index if not exists ix_%I_%s ON %I (%I)', meta.part_name, meta.index_column, meta.part_name, meta.index_column);
-- delimited for multiple indexes
foreach idx_col in array regexp_split_to_array(meta.index_column,';')
loop
idx_name = replace(idx_col, ',', '_');
execute format('create index if not exists ix_%I_%s ON %I (%s)', meta.part_name, idx_name, meta.part_name, idx_col);
end loop;
end if;
if (length(extra) > 0) then
@@ -1,6 +1,9 @@
package org.tests.query;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.PagedList;
import org.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import org.tests.model.basic.Contact;
import org.tests.model.basic.Customer;
@@ -13,6 +16,114 @@ import static org.assertj.core.api.Assertions.assertThat;
public class TestQueryFindNative extends BaseTestCase {
@Test
public void findCount() {
ResetBasicData.reset();
String sql = "select n.id from contact n where n.first_name like ?";
LoggedSqlCollector.start();
int rowCount = server()
.findNative(Contact.class, sql)
.setParameter(1, "J%")
.findCount();
List<Integer> nativeIds =
server()
.findNative(Contact.class, sql)
.setParameter(1, "J%")
.findIds();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(nativeIds).hasSize(rowCount);
assertThat(loggedSql).hasSize(2);
assertThat(loggedSql.get(0)).contains("select count(*) from ( select n.id from contact n where n.first_name like ?)");
assertThat(loggedSql.get(1)).startsWith("select n.id from contact n where n.first_name like ?");
}
@Test
public void findPagedList() {
ResetBasicData.reset();
String sql = "select n.id, n.first_name from contact n where n.first_name like ?";
PagedList<Contact> pagedList = server()
.findNative(Contact.class, sql)
.setParameter(1, "J%")
.setMaxRows(100)
.findPagedList();
LoggedSqlCollector.start();
int listSize = pagedList.getList().size();
int totalCount = pagedList.getTotalCount();
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(listSize).isEqualTo(totalCount);
assertThat(loggedSql).hasSize(2);
assertThat(loggedSql.get(0)).startsWith("select n.id, n.first_name from contact n where n.first_name like ?");
assertThat(loggedSql.get(1)).contains("select count(*) from ( select n.id, n.first_name from contact n where n.first_name like ?)");
}
@Test
public void findPagedList_withColumnAlias() {
ResetBasicData.reset();
String sql = "select n.id, 'SillyName' first_name from contact n where n.id < ? ";
PagedList<Contact> pagedList = server()
.findNative(Contact.class, sql)
.setParameter(1, 100)
.setMaxRows(100)
.findPagedList();
int listSize = pagedList.getList().size();
int totalCount = pagedList.getTotalCount();
assertThat(listSize).isEqualTo(totalCount);
for (Contact contact : pagedList.getList()) {
assertThat(contact.getFirstName()).isEqualTo("SillyName");
}
}
@Test
public void findIds() {
ResetBasicData.reset();
String sql = "select c.id from contact c where c.first_name like ? ";
List<Integer> ids = Ebean.createSqlQuery(sql)
.setParameter(1, "J%")
.findSingleAttributeList(Integer.class);
List<Integer> idsScalar =
server()
.findNative(Contact.class, sql)
.setParameter(1, "J%")
.findSingleAttributeList();
List<Integer> nativeIds =
server()
.findNative(Contact.class, sql)
.setParameter(1, "J%")
.findIds();
assertThat(nativeIds).isNotEmpty();
assertThat(nativeIds).containsAll(ids);
assertThat(idsScalar).containsAll(ids);
}
@Test
public void joinFromManyToOne() {