diff --git a/ebean-api/src/main/java/io/ebean/ExpressionFactory.java b/ebean-api/src/main/java/io/ebean/ExpressionFactory.java index 3bcd5cb39..d8f047908 100644 --- a/ebean-api/src/main/java/io/ebean/ExpressionFactory.java +++ b/ebean-api/src/main/java/io/ebean/ExpressionFactory.java @@ -181,6 +181,23 @@ public interface ExpressionFactory { */ Expression inRangeWith(String lowProperty, String highProperty, Object value); + /** + * A Property is in Range between 2 properties. + * + *
{@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)
+   *
+   * }
+ * + *

+ * 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. */ diff --git a/ebean-api/src/main/java/io/ebean/ExpressionList.java b/ebean-api/src/main/java/io/ebean/ExpressionList.java index 00f61a798..74426fe95 100644 --- a/ebean-api/src/main/java/io/ebean/ExpressionList.java +++ b/ebean-api/src/main/java/io/ebean/ExpressionList.java @@ -890,6 +890,23 @@ public interface ExpressionList { */ ExpressionList inRangeWith(String lowProperty, String highProperty, Object value); + /** + * A Property is in Range between 2 properties. + * + *

{@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)
+   *
+   * }
+ * + *

+ * This is a convenience expression combining a number of simple expressions. + */ + ExpressionList inRangeWithProperties(String propertyName, String lowProperty, String highProperty); + /** * In Range - {@code property >= value1 and property < value2}. *

diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/core/SpiOrmQueryRequest.java b/ebean-core/src/main/java/io/ebeaninternal/server/core/SpiOrmQueryRequest.java index 039158200..174392918 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/core/SpiOrmQueryRequest.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/core/SpiOrmQueryRequest.java @@ -121,7 +121,7 @@ public interface SpiOrmQueryRequest extends BeanQueryRequest, DocQueryRequ Map findMap(); /** - * Execute the findSingleAttributeList query. + * Execute the findSingleAttributeCollection query. */ > A findSingleAttributeCollection(A collection); diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionFactory.java b/ebean-core/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionFactory.java index 81f434762..9ce606961 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionFactory.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionFactory.java @@ -237,6 +237,11 @@ public class DefaultExpressionFactory implements SpiExpressionFactory { 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. */ diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java b/ebean-core/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java index 234fd28c2..408cdc4b8 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java @@ -833,6 +833,11 @@ public class DefaultExpressionList implements SpiExpressionList { return add(expr.inRangeWith(lowProperty, highProperty, value)); } + @Override + public ExpressionList inRangeWithProperties(String propertyName, String lowProperty, String highProperty) { + return add(expr.inRangeWithProperties(propertyName, lowProperty, highProperty)); + } + @Override public ExpressionList inRange(String propertyName, Object value1, Object value2) { return add(expr.inRange(propertyName, value1, value2)); diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/expression/JunctionExpression.java b/ebean-core/src/main/java/io/ebeaninternal/server/expression/JunctionExpression.java index 5bc19ca10..d3f968c22 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/expression/JunctionExpression.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/expression/JunctionExpression.java @@ -279,6 +279,11 @@ final class JunctionExpression implements SpiJunction, SpiExpression, Expr return exprList.inRangeWith(lowProperty, highProperty, value); } + @Override + public ExpressionList inRangeWithProperties(String propertyName, String lowProperty, String highProperty) { + return exprList.inRangeWithProperties(propertyName, lowProperty, highProperty); + } + @Override public ExpressionList inRange(String propertyName, Object value1, Object value2) { return exprList.inRange(propertyName, value1, value2); diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/idgen/UuidV1RndIdGenerator.java b/ebean-core/src/main/java/io/ebeaninternal/server/idgen/UuidV1RndIdGenerator.java index 21e34a8b3..adbf5e082 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/idgen/UuidV1RndIdGenerator.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/idgen/UuidV1RndIdGenerator.java @@ -106,7 +106,7 @@ public class UuidV1RndIdGenerator implements PlatformIdGenerator { delta = current - last; 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 // if so, we try to set the current as last and also increment the clockSeq. lock.lock(); diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/transaction/JdbcTransaction.java b/ebean-core/src/main/java/io/ebeaninternal/server/transaction/JdbcTransaction.java index 9fae2e21c..4b8408c27 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/transaction/JdbcTransaction.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/transaction/JdbcTransaction.java @@ -885,6 +885,9 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes { public final void preCommit() { internalBatchFlush(); firePreCommit(); + // we must flush the batch queue again, because the callback can + // modify current transaction + internalBatchFlush(); } /** diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeCalendar.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeCalendar.java index 8ac56e517..0331296cb 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeCalendar.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeCalendar.java @@ -61,7 +61,7 @@ final class ScalarTypeCalendar extends ScalarTypeBaseDateTime { @Override protected String toJsonNanos(Calendar value) { - return String.valueOf(value.getTime()); + return String.valueOf(value.getTime().getTime()); } @Override diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTime.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTime.java index 3de0184e1..eb84613ea 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTime.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalDateTime.java @@ -11,6 +11,7 @@ import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; +import java.time.format.DateTimeParseException; /** * ScalarType for java.sql.Timestamp. @@ -48,7 +49,7 @@ final class ScalarTypeLocalDateTime extends ScalarTypeBaseDateTime extends PBaseValueEqual { 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. * @@ -62,6 +73,17 @@ public class PBaseCompareable extends PBaseValueEqual { 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. * @@ -84,6 +106,17 @@ public class PBaseCompareable extends PBaseValueEqual { 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. * @@ -106,6 +139,17 @@ public class PBaseCompareable extends PBaseValueEqual { 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. * @@ -156,6 +200,29 @@ public class PBaseCompareable extends PBaseValueEqual { return _root; } + /** + * A Property is in Range between 2 other properties. + * + *

{@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)
+   *
+   * }
+ * + *

+ * 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. * diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java index 8fd7cf2d7..7d4edc0e9 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/PBaseValueEqual.java @@ -83,6 +83,17 @@ public abstract class PBaseValueEqual extends TQPropertyBase { 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. *

@@ -135,6 +146,17 @@ public abstract class PBaseValueEqual extends TQPropertyBase { 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. * diff --git a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java index ef3565711..5680185fb 100644 --- a/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java +++ b/ebean-querybean/src/main/java/io/ebean/typequery/TQRootBean.java @@ -693,6 +693,22 @@ public abstract class TQRootBean { 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. */ diff --git a/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java b/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java index 7cb4c630f..2db089274 100644 --- a/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java @@ -155,6 +155,39 @@ public class QCustomerTest { } } + @Test + void equalTo_byProperty() { + Query 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 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 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.before(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 @@ -428,6 +462,7 @@ public class QCustomerTest { 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.le(QCustomer.Alias.whenUpdated).query(), " where t0.registered <= t0.when_updated"); } @Test @@ -435,6 +470,7 @@ public class QCustomerTest { 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(QCustomer.Alias.whenUpdated).query(), " where t0.registered > t0.when_updated"); 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.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 query, String match) { @@ -628,6 +665,22 @@ public class QCustomerTest { .findList(); } + @Test + void query_inRangeWithOtherProperties() { + var c = QCustomer.alias(); + + Query 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 public void query_exists() { diff --git a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java index 7cc9d3127..3edfc9310 100644 --- a/ebean-querybean/src/test/java/org/querytest/QOrderTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QOrderTest.java @@ -2,12 +2,15 @@ package org.querytest; import io.ebean.DB; import io.ebean.FetchGroup; +import io.ebean.Query; import io.ebean.test.LoggedSql; import org.example.domain.Customer; import org.example.domain.Order; +import org.example.domain.OrderDetail; import org.example.domain.otherpackage.PhoneNumber; import org.example.domain.query.QCustomer; import org.example.domain.query.QOrder; +import org.example.domain.query.QOrderDetail; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -214,6 +217,62 @@ public class QOrderTest { } + @Test + void propertyCompare() { + Query 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 subQuery = new QOrderDetail() + .alias("od") + .orderQty.gt(10) + .id.eq(QOrder.Alias.details.id) + //.where().raw("details.id = od.id") + .query(); + + Query 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 subQuery = new QOrderDetail() + .alias("od") + .orderQty.gt(10) + .id.eq(QOrder.Alias.details.id) + //.where().raw("details.id = od.id") + .query(); + + Query 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() { customer = new Customer(); customer.name = "Fred"; diff --git a/ebean-test/src/test/java/io/ebean/xtest/internal/server/transaction/JdbcTransactionTest.java b/ebean-test/src/test/java/io/ebean/xtest/internal/server/transaction/JdbcTransactionTest.java index ce9e420b8..b306fef94 100644 --- a/ebean-test/src/test/java/io/ebean/xtest/internal/server/transaction/JdbcTransactionTest.java +++ b/ebean-test/src/test/java/io/ebean/xtest/internal/server/transaction/JdbcTransactionTest.java @@ -105,4 +105,25 @@ public class JdbcTransactionTest { 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(); + } } diff --git a/ebean-test/src/test/java/org/tests/cache/TestQueryCache.java b/ebean-test/src/test/java/org/tests/cache/TestQueryCache.java index feff29a68..6e254ea6c 100644 --- a/ebean-test/src/test/java/org/tests/cache/TestQueryCache.java +++ b/ebean-test/src/test/java/org/tests/cache/TestQueryCache.java @@ -91,12 +91,12 @@ public class TestQueryCache extends BaseTestCase { // ensure that findCount & findSingleAttribute use different // slots in cache. If not a "Cannot cast List to int" should happen. int count = DB - .find(EColAB.class) - .setUseQueryCache(true) - .select("columnA") - .where() - .eq("columnB", "SingleAttribute") - .findCount(); + .find(EColAB.class) + .setUseQueryCache(true) + .select("columnA") + .where() + .eq("columnB", "SingleAttribute") + .findCount(); assertThat(count).isEqualTo(2); } @@ -305,7 +305,7 @@ public class TestQueryCache extends BaseTestCase { assertSame(list, list2B); List list3 = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(false).where() - .ilike("name", "Rob").findList(); + .ilike("name", "Rob").findList(); assertNotSame(list, list3); BeanCollection bc3 = (BeanCollection) list3; @@ -349,10 +349,10 @@ public class TestQueryCache extends BaseTestCase { // and now, ensure that we hit the database LoggedSql.start(); colA_second = DB.find(EColAB.class) - .setUseQueryCache(CacheMode.PUT) - .where() - .eq("columnB", "someId") - .findIds(); + .setUseQueryCache(CacheMode.PUT) + .where() + .eq("columnB", "someId") + .findIds(); sql = LoggedSql.stop(); assertThat(sql).hasSize(1); @@ -361,15 +361,15 @@ public class TestQueryCache extends BaseTestCase { @Test public void findCountDifferentQueriesBit() { DB.getDefault().pluginApi().cacheManager().clearAll(); - 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.bitwiseAny("id", 1), q -> q.bitwiseAny("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.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, 1), q->q.bitwiseAnd("id",4, 1)); + 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)); // 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)); } diff --git a/platforms/postgres/pom.xml b/platforms/postgres/pom.xml index e2c37fae0..57a6a73e8 100644 --- a/platforms/postgres/pom.xml +++ b/platforms/postgres/pom.xml @@ -17,6 +17,20 @@ 13.10.1-SNAPSHOT + + io.ebean + ebean-test-containers + 6.3 + test + + + + org.postgresql + postgresql + 42.5.0 + test + + io.avaje junit diff --git a/platforms/postgres/src/main/java/io/ebean/platform/postgres/PostgresPlatformProvider.java b/platforms/postgres/src/main/java/io/ebean/platform/postgres/PostgresPlatformProvider.java index 86c65ab65..96d0b223f 100644 --- a/platforms/postgres/src/main/java/io/ebean/platform/postgres/PostgresPlatformProvider.java +++ b/platforms/postgres/src/main/java/io/ebean/platform/postgres/PostgresPlatformProvider.java @@ -62,7 +62,9 @@ public class PostgresPlatformProvider implements DatabasePlatformProvider { } } } finally { - connection.rollback(); + if (!connection.getAutoCommit()) { + connection.rollback(); + } } if (majorVersion <= 9) { return new Postgres9Platform(); diff --git a/platforms/postgres/src/test/java/io/ebean/platform/postgres/PostgresPlatformTest.java b/platforms/postgres/src/test/java/io/ebean/platform/postgres/PostgresPlatformTest.java index 7fe897f40..e9a604c82 100644 --- a/platforms/postgres/src/test/java/io/ebean/platform/postgres/PostgresPlatformTest.java +++ b/platforms/postgres/src/test/java/io/ebean/platform/postgres/PostgresPlatformTest.java @@ -5,8 +5,14 @@ import io.ebean.config.PlatformConfig; import io.ebean.config.dbplatform.DatabasePlatform; import io.ebean.config.dbplatform.DbPlatformType; 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 java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; + import static io.ebean.Query.LockType.*; import static io.ebean.Query.LockWait.*; import static org.assertj.core.api.Assertions.assertThat; @@ -14,6 +20,38 @@ import static org.junit.jupiter.api.Assertions.assertEquals; 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 void testUuidType() { PostgresPlatform platform = new PostgresPlatform();