Merge branch 'master-rob2' into issue-2774/encryption-support-for-in

This commit is contained in:
Roland Praml committed 2022-11-02 08:15:10 +01:00
commit 5170b9f329
23 files changed
+412 -26

No files matched your search

@@ -181,6 +181,23 @@ public interface ExpressionFactory {
*/ */
Expression inRangeWith(String lowProperty, String highProperty, Object value); Expression inRangeWith(String lowProperty, String highProperty, Object value);
/**
* A Property is in Range between 2 properties.
*
* <pre>{@code
*
* .orderDate.inRangeWith(QOrder.Alias.product.startDate, QOrder.Alias.product.endDate)
*
* // which equates to
* product.startDate <= orderDate and (orderDate < product.endDate or product.endDate is null)
*
* }</pre>
*
* <p>
* This is a convenience expression combining a number of simple expressions.
*/
Expression inRangeWithProperties(String propertyName, String lowProperty, String highProperty);
/** /**
* Between - property between the two given values. * Between - property between the two given values.
*/ */
@@ -890,6 +890,23 @@ public interface ExpressionList<T> {
*/ */
ExpressionList<T> inRangeWith(String lowProperty, String highProperty, Object value); ExpressionList<T> inRangeWith(String lowProperty, String highProperty, Object value);
/**
* A Property is in Range between 2 properties.
*
* <pre>{@code
*
* .orderDate.inRangeWith(QOrder.Alias.product.startDate, QOrder.Alias.product.endDate)
*
* // which equates to
* product.startDate <= orderDate and (orderDate < product.endDate or product.endDate is null)
*
* }</pre>
*
* <p>
* This is a convenience expression combining a number of simple expressions.
*/
ExpressionList<T> inRangeWithProperties(String propertyName, String lowProperty, String highProperty);
/** /**
* In Range - {@code property >= value1 and property < value2}. * In Range - {@code property >= value1 and property < value2}.
* <p> * <p>
@@ -121,7 +121,7 @@ public interface SpiOrmQueryRequest<T> extends BeanQueryRequest<T>, DocQueryRequ
<K> Map<K, T> findMap(); <K> Map<K, T> findMap();
/** /**
* Execute the findSingleAttributeList query. * Execute the findSingleAttributeCollection query.
*/ */
<A extends Collection<?>> A findSingleAttributeCollection(A collection); <A extends Collection<?>> A findSingleAttributeCollection(A collection);
@@ -237,6 +237,11 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
return and(le(lowProperty, value), gtOrNull(highProperty, value)); return and(le(lowProperty, value), gtOrNull(highProperty, value));
} }
@Override
public Expression inRangeWithProperties(String propertyName, String lowProperty, String highProperty) {
return raw(lowProperty + " <= " + propertyName + " and (" + propertyName + " < " + highProperty + " or " + highProperty + " is null)");
}
/** /**
* Between - property between the two given values. * Between - property between the two given values.
*/ */
@@ -833,6 +833,11 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
return add(expr.inRangeWith(lowProperty, highProperty, value)); return add(expr.inRangeWith(lowProperty, highProperty, value));
} }
@Override
public ExpressionList<T> inRangeWithProperties(String propertyName, String lowProperty, String highProperty) {
return add(expr.inRangeWithProperties(propertyName, lowProperty, highProperty));
}
@Override @Override
public ExpressionList<T> inRange(String propertyName, Object value1, Object value2) { public ExpressionList<T> inRange(String propertyName, Object value1, Object value2) {
return add(expr.inRange(propertyName, value1, value2)); return add(expr.inRange(propertyName, value1, value2));
@@ -279,6 +279,11 @@ final class JunctionExpression<T> implements SpiJunction<T>, SpiExpression, Expr
return exprList.inRangeWith(lowProperty, highProperty, value); return exprList.inRangeWith(lowProperty, highProperty, value);
} }
@Override
public ExpressionList<T> inRangeWithProperties(String propertyName, String lowProperty, String highProperty) {
return exprList.inRangeWithProperties(propertyName, lowProperty, highProperty);
}
@Override @Override
public ExpressionList<T> inRange(String propertyName, Object value1, Object value2) { public ExpressionList<T> inRange(String propertyName, Object value1, Object value2) {
return exprList.inRange(propertyName, value1, value2); return exprList.inRange(propertyName, value1, value2);
@@ -106,7 +106,7 @@ public class UuidV1RndIdGenerator implements PlatformIdGenerator {
delta = current - last; delta = current - last;
if (delta < -10000 * 20000) { if (delta < -10000 * 20000) {
log.log(INFO, "Clock skew of {} ms detected", delta / -10000); log.log(INFO, "Clock skew of {0} ms detected", delta / -10000);
// The clock was adjusted back about 2 seconds, or we were generating a lot of ids too fast // The clock was adjusted back about 2 seconds, or we were generating a lot of ids too fast
// if so, we try to set the current as last and also increment the clockSeq. // if so, we try to set the current as last and also increment the clockSeq.
lock.lock(); lock.lock();
@@ -885,6 +885,9 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
public final void preCommit() { public final void preCommit() {
internalBatchFlush(); internalBatchFlush();
firePreCommit(); firePreCommit();
// we must flush the batch queue again, because the callback can
// modify current transaction
internalBatchFlush();
} }
/** /**
@@ -61,7 +61,7 @@ final class ScalarTypeCalendar extends ScalarTypeBaseDateTime<Calendar> {
@Override @Override
protected String toJsonNanos(Calendar value) { protected String toJsonNanos(Calendar value) {
return String.valueOf(value.getTime()); return String.valueOf(value.getTime().getTime());
} }
@Override @Override
@@ -11,6 +11,7 @@ import java.sql.Timestamp;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeParseException;
/** /**
* ScalarType for java.sql.Timestamp. * ScalarType for java.sql.Timestamp.
@@ -48,7 +49,7 @@ final class ScalarTypeLocalDateTime extends ScalarTypeBaseDateTime<LocalDateTime
@Override @Override
public LocalDateTime jsonRead(JsonParser parser) throws IOException { public LocalDateTime jsonRead(JsonParser parser) throws IOException {
return LocalDateTime.parse(parser.getText()); return parse(parser.getText());
} }
@Override @Override
@@ -63,7 +64,11 @@ final class ScalarTypeLocalDateTime extends ScalarTypeBaseDateTime<LocalDateTime
@Override @Override
public LocalDateTime parse(String value) { public LocalDateTime parse(String value) {
return LocalDateTime.parse(value); try {
return LocalDateTime.parse(value);
} catch (DateTimeParseException pe) {
return super.parse(value);
}
} }
@Override @Override
@@ -2,15 +2,20 @@ package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.TimeZone;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*; 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 ScalarTypeLocalDateTimeTest { public class ScalarTypeLocalDateTimeTest {
@@ -123,4 +128,35 @@ public class ScalarTypeLocalDateTimeTest {
LocalDateTime value = typeIso.fromJsonISO8601(asJson); LocalDateTime value = typeIso.fromJsonISO8601(asJson);
assertThat(localDateTime).isEqualToIgnoringNanos(value); assertThat(localDateTime).isEqualToIgnoringNanos(value);
} }
@Test
public void testParseEbean11() throws IOException {
ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
JsonFactory factory = new JsonFactory();
JsonParser parser11 = factory.createParser("1517627106000"); // its a number!
JsonParser parser13 = factory.createParser("\"2022-01-01T01:00:00\"");
// test parsing an ebean 11/13 timestamp, we do not expect an exception
LocalDateTime p = type.parse("1517627106000");
parser11.nextToken();
LocalDateTime q = type.jsonRead(parser11);
assertThat(p).isEqualTo(q);
p = type.parse("2022-01-01T01:00:00");
parser13.nextToken();
q = type.jsonRead(parser13);
assertThat(p).isEqualTo(q);
TimeZone tz = TimeZone.getDefault();
try {
// Adjust Timezone to run on build server
// Note Millis and Iso have a time offset according to TZ
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
LocalDateTime fromMillis = type.parse("0");
LocalDateTime fromIso = type.parse("1970-01-01T01:00");
assertThat(fromMillis).isEqualToIgnoringNanos(fromIso);
} finally {
TimeZone.setDefault(tz);
}
}
} }
@@ -11,10 +11,11 @@ import java.io.Reader;
import java.net.URL; import java.net.URL;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Locale;
class CsvReaderTest { class CsvReaderTest {
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy"); final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH);
@Test @Test
void test() throws Exception { void test() throws Exception {
@@ -42,7 +42,7 @@ public class ExtraDdlXmlReader {
StringBuilder sb = new StringBuilder(300); StringBuilder sb = new StringBuilder(300);
for (DdlScript script : read.getDdlScript()) { for (DdlScript script : read.getDdlScript()) {
if (script.isDrop() == drops && matchPlatform(platform, script.getPlatforms())) { if (script.isDrop() == drops && matchPlatform(platform, script.getPlatforms())) {
logger.log(DEBUG, "include script {}", script.getName()); logger.log(DEBUG, "include script {0}", script.getName());
String value = script.getValue(); String value = script.getValue();
sb.append(value); sb.append(value);
if (value.lastIndexOf(';') == -1) { if (value.lastIndexOf(';') == -1) {
@@ -40,6 +40,17 @@ public class PBaseCompareable<R, T> extends PBaseValueEqual<R, T> {
return _root; return _root;
} }
/**
* Greater than other property.
*
* @param other the other property to compare
* @return the root query bean instance
*/
public final R gt(TQProperty<?> other) {
expr().raw(_name + " > " + other.propertyName());
return _root;
}
/** /**
* Greater than OR Null. * Greater than OR Null.
* *
@@ -62,6 +73,17 @@ public class PBaseCompareable<R, T> extends PBaseValueEqual<R, T> {
return _root; return _root;
} }
/**
* Greater than or Equal to other property.
*
* @param other the other property to compare
* @return the root query bean instance
*/
public final R ge(TQProperty<?> other) {
expr().raw(_name + " >= " + other.propertyName());
return _root;
}
/** /**
* Greater than or Equal to OR Null. * Greater than or Equal to OR Null.
* *
@@ -84,6 +106,17 @@ public class PBaseCompareable<R, T> extends PBaseValueEqual<R, T> {
return _root; return _root;
} }
/**
* Less than other property.
*
* @param other the other property to compare
* @return the root query bean instance
*/
public final R lt(TQProperty<?> other) {
expr().raw(_name + " < " + other.propertyName());
return _root;
}
/** /**
* Less than OR Null. * Less than OR Null.
* *
@@ -106,6 +139,17 @@ public class PBaseCompareable<R, T> extends PBaseValueEqual<R, T> {
return _root; return _root;
} }
/**
* Less than or Equal to other property.
*
* @param other the other property to compare
* @return the root query bean instance
*/
public final R le(TQProperty<?> other) {
expr().raw(_name + " <= " + other.propertyName());
return _root;
}
/** /**
* Less than or Equal to. * Less than or Equal to.
* *
@@ -156,6 +200,29 @@ public class PBaseCompareable<R, T> extends PBaseValueEqual<R, T> {
return _root; return _root;
} }
/**
* A Property is in Range between 2 other properties.
*
* <pre>{@code
* var o = QOrder.alias();
*
* new QOrder()
* .orderDate.inRangeWith(o.product.startDate, o.product.endDate)
* .findList();
*
* // which equates to
* product.startDate <= orderDate and (product.endDate > orderDate or product.endDate is null)
*
* }</pre>
*
* <p>
* This is a convenience expression combining a number of simple expressions.
*/
public final R inRangeWith(TQProperty<?> lowProperty, TQProperty<?> highProperty) {
expr().inRangeWithProperties(_name, lowProperty.propertyName(), highProperty.propertyName());
return _root;
}
/** /**
* Between lower and upper values. * Between lower and upper values.
* *
@@ -83,6 +83,17 @@ public abstract class PBaseValueEqual<R, T> extends TQPropertyBase<R> {
return _root; return _root;
} }
/**
* Is equal to another property.
*
* @param other the other property to compare
* @return the root query bean instance
*/
public final R eq(TQProperty<?> other) {
expr().raw(_name + " = " + other.propertyName());
return _root;
}
/** /**
* Is equal to if value is non-null and otherwise no expression is added to the query. * Is equal to if value is non-null and otherwise no expression is added to the query.
* <p> * <p>
@@ -135,6 +146,17 @@ public abstract class PBaseValueEqual<R, T> extends TQPropertyBase<R> {
return _root; return _root;
} }
/**
* Is not equal to another property.
*
* @param other the other property to compare
* @return the root query bean instance
*/
public final R ne(TQProperty<?> other) {
expr().raw(_name + " <> " + other.propertyName());
return _root;
}
/** /**
* Is in a list of values. * Is in a list of values.
* *
@@ -693,6 +693,22 @@ public abstract class TQRootBean<T, R> {
return root; return root;
} }
/**
* Add EXISTS sub-query predicate.
*/
public R exists(Query<?> subQuery) {
query.where().exists(subQuery);
return root;
}
/**
* Add NOT EXISTS sub-query predicate.
*/
public R notExists(Query<?> subQuery) {
query.where().notExists(subQuery);
return root;
}
/** /**
* Execute using "for update" clause which results in the DB locking the record. * Execute using "for update" clause which results in the DB locking the record.
*/ */
@@ -155,6 +155,39 @@ public class QCustomerTest {
} }
} }
@Test
void equalTo_byProperty() {
Query<Customer> query = new QCustomer()
.select(QCustomer.Alias.id)
.billingAddress.city.eq(QCustomer.Alias.shippingAddress.city)
.query();
query.findList();
String generatedSql = query.getGeneratedSql();
// select t0.id from be_customer t0
// left join o_address t2 on t2.id = t0.shipping_address_id
// left join o_address t1 on t1.id = t0.billing_address_id
// where t1.city = t2.city
assertThat(generatedSql).contains("where t1.city = t2.city");
}
@Test
void notEqual_byProperty() {
Query<Customer> query = new QCustomer()
.select(QCustomer.Alias.id)
.billingAddress.city.ne(QCustomer.Alias.shippingAddress.city)
.query();
query.findList();
String generatedSql = query.getGeneratedSql();
// select t0.id from be_customer t0
// left join o_address t2 on t2.id = t0.shipping_address_id
// left join o_address t1 on t1.id = t0.billing_address_id
// where t1.city <> t2.city
assertThat(generatedSql).contains("where t1.city <> t2.city");
}
@Test @Test
public void isEmpty() { public void isEmpty() {
@@ -421,6 +454,7 @@ public class QCustomerTest {
assertContains(new QCustomer().registered.lt(new Date()).query(), " where t0.registered < ?"); assertContains(new QCustomer().registered.lt(new Date()).query(), " where t0.registered < ?");
assertContains(new QCustomer().registered.before(new Date()).query(), " where t0.registered < ?"); assertContains(new QCustomer().registered.before(new Date()).query(), " where t0.registered < ?");
assertContains(new QCustomer().registered.lessThan(new Date()).query(), " where t0.registered < ?"); assertContains(new QCustomer().registered.lessThan(new Date()).query(), " where t0.registered < ?");
assertContains(new QCustomer().registered.lt(QCustomer.Alias.whenUpdated).query(), " where t0.registered < t0.when_updated");
} }
@Test @Test
@@ -428,6 +462,7 @@ public class QCustomerTest {
assertContains(new QCustomer().registered.le(new Date()).query(), " where t0.registered <= ?"); assertContains(new QCustomer().registered.le(new Date()).query(), " where t0.registered <= ?");
assertContains(new QCustomer().registered.lessOrEqualTo(new Date()).query(), " where t0.registered <= ?"); assertContains(new QCustomer().registered.lessOrEqualTo(new Date()).query(), " where t0.registered <= ?");
assertContains(new QCustomer().registered.le(QCustomer.Alias.whenUpdated).query(), " where t0.registered <= t0.when_updated");
} }
@Test @Test
@@ -435,6 +470,7 @@ public class QCustomerTest {
assertContains(new QCustomer().registered.after(new Date()).query(), " where t0.registered > ?"); assertContains(new QCustomer().registered.after(new Date()).query(), " where t0.registered > ?");
assertContains(new QCustomer().registered.gt(new Date()).query(), " where t0.registered > ?"); assertContains(new QCustomer().registered.gt(new Date()).query(), " where t0.registered > ?");
assertContains(new QCustomer().registered.gt(QCustomer.Alias.whenUpdated).query(), " where t0.registered > t0.when_updated");
assertContains(new QCustomer().registered.greaterThan(new Date()).query(), " where t0.registered > ?"); assertContains(new QCustomer().registered.greaterThan(new Date()).query(), " where t0.registered > ?");
} }
@@ -444,6 +480,7 @@ public class QCustomerTest {
assertContains(new QCustomer().registered.ge(new Date()).query(), " where t0.registered >= ?"); assertContains(new QCustomer().registered.ge(new Date()).query(), " where t0.registered >= ?");
assertContains(new QCustomer().registered.greaterOrEqualTo(new Date()).query(), " where t0.registered >= ?"); assertContains(new QCustomer().registered.greaterOrEqualTo(new Date()).query(), " where t0.registered >= ?");
assertContains(new QCustomer().registered.ge(QCustomer.Alias.whenUpdated).query(), " where t0.registered >= t0.when_updated");
} }
private void assertContains(Query<Customer> query, String match) { private void assertContains(Query<Customer> query, String match) {
@@ -628,6 +665,22 @@ public class QCustomerTest {
.findList(); .findList();
} }
@Test
void query_inRangeWithOtherProperties() {
var c = QCustomer.alias();
Query<Customer> query = new QCustomer()
.select(c.id)
.name.inRangeWith(c.contacts.firstName, c.contacts.lastName)
.query();
// select distinct t0.id from be_customer t0
// left join be_contact t1 on t1.customer_id = t0.id
// where t1.first_name <= t0.name and (t0.name < t1.last_name or t1.last_name is null)
query.findList();
assertThat(query.getGeneratedSql()).contains(" where t1.first_name <= t0.name and (t0.name < t1.last_name or t1.last_name is null)");
}
@Test @Test
public void query_exists() { public void query_exists() {
@@ -2,12 +2,15 @@ package org.querytest;
import io.ebean.DB; import io.ebean.DB;
import io.ebean.FetchGroup; import io.ebean.FetchGroup;
import io.ebean.Query;
import io.ebean.test.LoggedSql; import io.ebean.test.LoggedSql;
import org.example.domain.Customer; import org.example.domain.Customer;
import org.example.domain.Order; import org.example.domain.Order;
import org.example.domain.OrderDetail;
import org.example.domain.otherpackage.PhoneNumber; import org.example.domain.otherpackage.PhoneNumber;
import org.example.domain.query.QCustomer; import org.example.domain.query.QCustomer;
import org.example.domain.query.QOrder; import org.example.domain.query.QOrder;
import org.example.domain.query.QOrderDetail;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -214,6 +217,62 @@ public class QOrderTest {
} }
@Test
void propertyCompare() {
Query<Order> query = new QOrder()
.orderDate.lt(QOrder.Alias.customer.registered)
.query();
query.findList();
assertThat(query.getGeneratedSql()).contains(" from o_order t0 join be_customer t1 on t1.id = t0.customer_id where t0.order_date < t1.registered");
}
@Test
void subQueryExists() {
Query<OrderDetail> subQuery = new QOrderDetail()
.alias("od")
.orderQty.gt(10)
.id.eq(QOrder.Alias.details.id)
//.where().raw("details.id = od.id")
.query();
Query<Order> query = new QOrder()
.exists(subQuery)
.orderBy("orderDate").query();
query.findList();
// select distinct t0.id, t0.status, t0.order_date, t0.ship_date, t0.version, t0.when_created, t0.when_updated, t0.customer_id, t0.shipping_address_id, t0.order_date
// from o_order t0
// left join o_order_detail t1 on t1.order_id = t0.id
// where exists (select 1 from o_order_detail od where od.order_qty > ? and od.id = t1.id) order by t0.order_date
String sql = query.getGeneratedSql();
assertThat(sql).contains(" exists (select 1 from o_order_detail od where od.order_qty > ? and od.id = t1.id)");
}
@Test
void subQueryNotExists() {
Query<OrderDetail> subQuery = new QOrderDetail()
.alias("od")
.orderQty.gt(10)
.id.eq(QOrder.Alias.details.id)
//.where().raw("details.id = od.id")
.query();
Query<Order> query = new QOrder()
.notExists(subQuery)
.orderBy("orderDate")
.query();
query.findList();
// select distinct t0.id, t0.status, t0.order_date, t0.ship_date, t0.version, t0.when_created, t0.when_updated, t0.customer_id, t0.shipping_address_id, t0.order_date
// from o_order t0
// left join o_order_detail t1 on t1.order_id = t0.id
// where not exists (select 1 from o_order_detail od where od.order_qty > ? and od.id = t1.id) order by t0.order_date
String sql = query.getGeneratedSql();
assertThat(sql).contains(" not exists (select 1 from o_order_detail od where od.order_qty > ? and od.id = t1.id)");
}
private static void setupData() { private static void setupData() {
customer = new Customer(); customer = new Customer();
customer.name = "Fred"; customer.name = "Fred";
@@ -105,4 +105,25 @@ public class JdbcTransactionTest {
assertThat(postCommitCallCount.get()).isEqualTo(2); // postcommit executed twice assertThat(postCommitCallCount.get()).isEqualTo(2); // postcommit executed twice
} }
} }
@Test
public void testFlushInCallback() {
try (Transaction transaction = DB.beginTransaction()) {
transaction.setBatchMode(true);
DB.currentTransaction().register(
new TransactionCallbackAdapter() {
@Override
public void preCommit() {
EBasic basic = new EBasic("binner1");
DB.save(basic);
}
}
);
EBasic basic = new EBasic("bouter1");
DB.save(basic);
transaction.commit(); // transaction will fail if recursive post-commit is failing
}
assertThat(DB.find(EBasic.class).where().eq("name", "binner1").exists()).isTrue();
}
} }
+17 -17
View File
@@ -91,12 +91,12 @@ public class TestQueryCache extends BaseTestCase {
// ensure that findCount & findSingleAttribute use different // ensure that findCount & findSingleAttribute use different
// slots in cache. If not a "Cannot cast List to int" should happen. // slots in cache. If not a "Cannot cast List to int" should happen.
int count = DB int count = DB
.find(EColAB.class) .find(EColAB.class)
.setUseQueryCache(true) .setUseQueryCache(true)
.select("columnA") .select("columnA")
.where() .where()
.eq("columnB", "SingleAttribute") .eq("columnB", "SingleAttribute")
.findCount(); .findCount();
assertThat(count).isEqualTo(2); assertThat(count).isEqualTo(2);
} }
@@ -305,7 +305,7 @@ public class TestQueryCache extends BaseTestCase {
assertSame(list, list2B); assertSame(list, list2B);
List<Customer> list3 = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(false).where() List<Customer> list3 = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(false).where()
.ilike("name", "Rob").findList(); .ilike("name", "Rob").findList();
assertNotSame(list, list3); assertNotSame(list, list3);
BeanCollection<Customer> bc3 = (BeanCollection<Customer>) list3; BeanCollection<Customer> bc3 = (BeanCollection<Customer>) list3;
@@ -349,10 +349,10 @@ public class TestQueryCache extends BaseTestCase {
// and now, ensure that we hit the database // and now, ensure that we hit the database
LoggedSql.start(); LoggedSql.start();
colA_second = DB.find(EColAB.class) colA_second = DB.find(EColAB.class)
.setUseQueryCache(CacheMode.PUT) .setUseQueryCache(CacheMode.PUT)
.where() .where()
.eq("columnB", "someId") .eq("columnB", "someId")
.findIds(); .findIds();
sql = LoggedSql.stop(); sql = LoggedSql.stop();
assertThat(sql).hasSize(1); assertThat(sql).hasSize(1);
@@ -361,15 +361,15 @@ public class TestQueryCache extends BaseTestCase {
@Test @Test
public void findCountDifferentQueriesBit() { public void findCountDifferentQueriesBit() {
DB.getDefault().pluginApi().cacheManager().clearAll(); DB.getDefault().pluginApi().cacheManager().clearAll();
differentFindCount(q->q.bitwiseAny("id",1), q->q.bitwiseAny("id",0)); differentFindCount(q -> q.bitwiseAny("id", 1), q -> q.bitwiseAny("id", 0));
differentFindCount(q->q.bitwiseAll("id",1), q->q.bitwiseAll("id",0)); differentFindCount(q -> q.bitwiseAll("id", 1), q -> q.bitwiseAll("id", 0));
// differentFindCount(q->q.bitwiseNot("id",1), q->q.bitwiseNot("id",0)); NOT 1 == AND 1 = 0 // differentFindCount(q->q.bitwiseNot("id",1), q->q.bitwiseNot("id",0)); NOT 1 == AND 1 = 0
differentFindCount(q->q.bitwiseAnd("id",1, 0), q->q.bitwiseAnd("id",1, 1)); differentFindCount(q -> q.bitwiseAnd("id", 1, 0), q -> q.bitwiseAnd("id", 1, 1));
differentFindCount(q->q.bitwiseAnd("id",2, 0), q->q.bitwiseAnd("id",4, 0)); differentFindCount(q -> q.bitwiseAnd("id", 2, 0), q -> q.bitwiseAnd("id", 4, 0));
differentFindCount(q->q.bitwiseAnd("id",2, 1), q->q.bitwiseAnd("id",4, 1)); differentFindCount(q -> q.bitwiseAnd("id", 2, 1), q -> q.bitwiseAnd("id", 4, 1));
// Will produce hash collision // Will produce hash collision
differentFindCount(q->q.bitwiseAnd("id",10, 0), q->q.bitwiseAnd("id",0, 928210)); differentFindCount(q -> q.bitwiseAnd("id", 10, 0), q -> q.bitwiseAnd("id", 0, 928210));
} }
+14
View File
@@ -17,6 +17,20 @@
<version>13.10.1-SNAPSHOT</version> <version>13.10.1-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-test-containers</artifactId>
<version>6.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.0</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>io.avaje</groupId> <groupId>io.avaje</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
@@ -62,7 +62,9 @@ public class PostgresPlatformProvider implements DatabasePlatformProvider {
} }
} }
} finally { } finally {
connection.rollback(); if (!connection.getAutoCommit()) {
connection.rollback();
}
} }
if (majorVersion <= 9) { if (majorVersion <= 9) {
return new Postgres9Platform(); return new Postgres9Platform();
@@ -5,8 +5,14 @@ import io.ebean.config.PlatformConfig;
import io.ebean.config.dbplatform.DatabasePlatform; import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.DbPlatformType; import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.config.dbplatform.DbType; import io.ebean.config.dbplatform.DbType;
import io.ebean.test.containers.PostgresContainer;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import static io.ebean.Query.LockType.*; import static io.ebean.Query.LockType.*;
import static io.ebean.Query.LockWait.*; import static io.ebean.Query.LockWait.*;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@@ -14,6 +20,38 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class PostgresPlatformTest { class PostgresPlatformTest {
/**
* Let's just run this test manually.
*/
@Disabled
@Test
void platformDetection() throws SQLException {
PostgresContainer container = PostgresContainer.builder("15")
.containerName("pg15_ebeanTest")
.dbName("unit")
.port(0) // use a random port
.build();
container.startWithDropCreate();
try (Connection connection = container.createConnection()) {
connection.setAutoCommit(true);
DatabaseMetaData metaData = connection.getMetaData();
PostgresPlatformProvider platformProvider = new PostgresPlatformProvider();
DatabasePlatform platform = platformProvider.create(15, 0, metaData, connection);
assertThat(platform.name()).isEqualTo("postgres");
connection.setAutoCommit(false);
DatabasePlatform platform2 = platformProvider.create(15, 0, metaData, connection);
assertThat(platform2.name()).isEqualTo("postgres");
} finally {
container.stopRemove();
}
}
@Test @Test
void testUuidType() { void testUuidType() {
PostgresPlatform platform = new PostgresPlatform(); PostgresPlatform platform = new PostgresPlatform();