Skip to content

Commit

Permalink
Added missing EXTRACT(QUARTER FROM date/datetime) for Derby platform.…
Browse files Browse the repository at this point in the history
… Covered EXTRACT queries with jUnit.

Signed-off-by: Tomas Kraus <tomas.kraus@oracle.com>
  • Loading branch information
Tomas-Kraus authored and lukasj committed Mar 9, 2022
1 parent da512f0 commit 72cf9f8
Show file tree
Hide file tree
Showing 4 changed files with 544 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Vector;

import org.eclipse.persistence.exceptions.ValidationException;
Expand All @@ -41,6 +42,7 @@
import org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition;
import org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter;
import org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter;
import org.eclipse.persistence.internal.expressions.LiteralExpression;
import org.eclipse.persistence.internal.expressions.SQLSelectStatement;
import org.eclipse.persistence.internal.helper.ClassConstants;
import org.eclipse.persistence.internal.helper.DatabaseField;
Expand Down Expand Up @@ -345,7 +347,7 @@ protected void initializePlatformOperators() {
addOperator(ExpressionOperator.simpleFunction(ExpressionOperator.ToNumber, "DOUBLE"));
// LocalTime should be processed as TIMESTAMP
addOperator(ExpressionOperator.simpleFunctionNoParentheses(ExpressionOperator.LocalTime, "CAST(CURRENT_TIME AS TIMESTAMP)"));
addOperator(extractOperator());
addOperator(derbyExtractOperator());
addOperator(derbyPowerOperator());
addOperator(derbyRoundOperator());
}
Expand Down Expand Up @@ -495,12 +497,74 @@ public void printJavaCollection(List items, ExpressionJavaPrinter printer) {
* INTERNAL:
* Derby does not support EXTRACT, but does have YEAR, MONTH, DAY, etc.
*/
public static ExpressionOperator extractOperator() {
ExpressionOperator exOperator = new ExpressionOperator();
public static ExpressionOperator derbyExtractOperator() {

ExpressionOperator exOperator = new ExpressionOperator() {

// QUARTER emulation: ((MONTH(:first)+2)/3)
private final String[] QUARTER_STRINGS = new String[] {"((MONTH(", ")+2)/3)"};

private void printQuarterSQL(final Expression first, final ExpressionSQLPrinter printer) {
printer.printString(QUARTER_STRINGS[0]);
first.printSQL(printer);
printer.printString(QUARTER_STRINGS[1]);
}

private void printQuarterJava(final Expression first, final ExpressionJavaPrinter printer) {
printer.printString(QUARTER_STRINGS[0]);
first.printJava(printer);
printer.printString(QUARTER_STRINGS[1]);
}

@Override
public void printDuo(Expression first, Expression second, ExpressionSQLPrinter printer) {
if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression)second).getValue().toUpperCase())) {
printQuarterSQL(first, printer);
} else {
super.printDuo(first, second, printer);
}
}

@Override
public void printCollection(List items, ExpressionSQLPrinter printer) {
if (items.size() == 2) {
Expression first = (Expression)items.get(0);
Expression second = (Expression)items.get(1);
if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression)second).getValue().toUpperCase())) {
printQuarterSQL(first, printer);
return;
}
}
super.printCollection(items, printer);
}

@Override
public void printJavaDuo(Expression first, Expression second, ExpressionJavaPrinter printer) {
if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression)second).getValue().toUpperCase())) {
printQuarterJava(first, printer);
} else {
super.printJavaDuo(first, second, printer);
}
}

@Override
public void printJavaCollection(List items, ExpressionJavaPrinter printer) {
if (items.size() == 2) {
Expression first = (Expression)items.get(0);
Expression second = (Expression)items.get(1);
if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression)second).getValue().toUpperCase())) {
printQuarterJava(first, printer);
return;
}
}
super.printJavaCollection(items, printer);
}
};

exOperator.setType(ExpressionOperator.FunctionOperator);
exOperator.setSelector(ExpressionOperator.Extract);
exOperator.setName("EXTRACT");
List<String> v = new ArrayList<>(5);
List<String> v = new ArrayList<>(3);
v.add("");
v.add("(");
v.add(")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Contributors:
// 02/01/2022: Tomas Kraus
// - Issue 1442: Implement New JPA API 3.1.0 Features
package org.eclipse.persistence.jpa.test.criteria.model;
package org.eclipse.persistence.jpa.test.criteria;

import java.time.Duration;
import java.time.LocalDate;
Expand All @@ -29,6 +29,7 @@
import jakarta.persistence.criteria.CriteriaUpdate;
import jakarta.persistence.criteria.Root;

import org.eclipse.persistence.jpa.test.criteria.model.DateTimeEntity;
import org.eclipse.persistence.jpa.test.framework.DDLGen;
import org.eclipse.persistence.jpa.test.framework.Emf;
import org.eclipse.persistence.jpa.test.framework.EmfRunner;
Expand Down Expand Up @@ -227,9 +228,6 @@ public void testCriteriaQueryWhereLocalTime() {
cq.where(cb.and(cb.lessThan(entity.get("time"), cb.localTime()), cb.equal(entity.get("id"), 4)));
em.createQuery(cq).getSingleResult();
em.getTransaction().commit();
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand All @@ -255,9 +253,6 @@ public void testCriteriaQueryWhereLocalTimeReturnsEmpty() {
List<DateTimeEntity> data = em.createQuery(cq).getResultList();
em.getTransaction().commit();
MatcherAssert.assertThat(data.size(), Matchers.equalTo(0));
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand All @@ -281,9 +276,6 @@ public void testCriteriaQueryWhereLocalDate() {
cq.where(cb.and(cb.lessThan(entity.get("date"), cb.localDate()), cb.equal(entity.get("id"), 4)));
em.createQuery(cq).getSingleResult();
em.getTransaction().commit();
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand All @@ -309,9 +301,6 @@ public void testCriteriaQueryWhereLocalDateReturnsEmpty() {
List<DateTimeEntity> data = em.createQuery(cq).getResultList();
em.getTransaction().commit();
MatcherAssert.assertThat(data.size(), Matchers.equalTo(0));
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand All @@ -335,9 +324,6 @@ public void testCriteriaQueryWhereLocalDateTime() {
cq.where(cb.and(cb.lessThan(entity.get("datetime"), cb.localDateTime()), cb.equal(entity.get("id"), 4)));
em.createQuery(cq).getSingleResult();
em.getTransaction().commit();
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand All @@ -363,9 +349,6 @@ public void testCriteriaQueryWhereLocalDateTimeReturnsEmpty() {
List<DateTimeEntity> data = em.createQuery(cq).getResultList();
em.getTransaction().commit();
MatcherAssert.assertThat(data.size(), Matchers.equalTo(0));
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand Down Expand Up @@ -395,9 +378,6 @@ public void testCriteriaQuerySelectLocalTime() {
} else {
MatcherAssert.assertThat(86400000L + diffMilis, Matchers.lessThan(30000L));
}
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand All @@ -423,9 +403,6 @@ public void testCriteriaQuerySelectLocalDate() {
MatcherAssert.assertThat(diff.getYears(), Matchers.equalTo(0));
MatcherAssert.assertThat(diff.getMonths(), Matchers.equalTo(0));
MatcherAssert.assertThat(diff.getDays(), Matchers.lessThan(2));
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand All @@ -449,9 +426,6 @@ public void testCriteriaQuerySelectLocalDateTime() {
em.getTransaction().commit();
long diffMilis = Duration.between(datetime, LocalDateTime.now()).toMillis();
MatcherAssert.assertThat(diffMilis, Matchers.lessThan(30000L));
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand Down
Loading

0 comments on commit 72cf9f8

Please sign in to comment.