Skip to content

Commit

Permalink
[Blazebit#723] Workaround EclipseLink issue with null precedence rend…
Browse files Browse the repository at this point in the history
…ering on platforms that don't support it and switch the docker image for oracle
  • Loading branch information
beikov committed Jan 22, 2019
1 parent 7465aa7 commit 8494cc4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docker_db.bat
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ goto:EOF

:oracle
docker rm -f oracle || true
docker run --shm-size=1536m --name oracle -d -p 1521:1521 alexeiled/docker-oracle-xe-11g
docker run --shm-size=1536m --name oracle -d -p 1521:1521 wnameless/oracle-xe-11g
goto:EOF
2 changes: 1 addition & 1 deletion docker_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mssql() {

oracle() {
docker rm -f oracle || true
docker run --shm-size=1536m --name oracle -d -p 1521:1521 alexeiled/docker-oracle-xe-11g
docker run --shm-size=1536m --name oracle -d -p 1521:1521 wnameless/oracle-xe-11g
}

if [ -z ${1} ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,24 @@ public class EclipseLinkJpaProvider implements JpaProvider {

private static final String[] EMPTY = {};
private final PersistenceUnitUtil persistenceUnitUtil;
private final DB db;

public EclipseLinkJpaProvider(PersistenceUnitUtil persistenceUnitUtil) {
/**
* @author Christian Beikov
* @since 1.4.0
*/
private static enum DB {
OTHER,
DB2;
}

public EclipseLinkJpaProvider(PersistenceUnitUtil persistenceUnitUtil, String dbms) {
this.persistenceUnitUtil = persistenceUnitUtil;
if ("db2".equals(dbms)) {
this.db = DB.DB2;
} else {
this.db = DB.OTHER;
}
}

@Override
Expand Down Expand Up @@ -120,12 +135,36 @@ public boolean supportsNullPrecedenceExpression() {

@Override
public void renderNullPrecedence(StringBuilder sb, String expression, String resolvedExpression, String order, String nulls) {
sb.append(expression);
if (order != null) {
sb.append(' ').append(order);
if (db == DB.DB2) {
// We need this workaround since EclipseLink doesn't handle null precedence properly on DB2..
if ("FIRST".equals(nulls) && "DESC".equalsIgnoreCase(order) || "LAST".equals(nulls) && "ASC".equalsIgnoreCase(order)) {
// According to DB2 docs, the following are the defaults
// ASC + NULLS LAST
// DESC + NULLS FIRST
sb.append(expression);
sb.append(" ").append(order);
return;
} else {
sb.append("CASE WHEN ").append(resolvedExpression != null ? resolvedExpression : expression).append(" IS NULL THEN ");
if ("FIRST".equals(nulls)) {
sb.append("0 ELSE 1");
} else {
sb.append("1 ELSE 0");
}
sb.append(" END, ");
}
sb.append(expression);
if (order != null) {
sb.append(' ').append(order);
}
} else {
sb.append(expression);
if (order != null) {
sb.append(' ').append(order);

if (nulls != null) {
sb.append(" NULLS ").append(nulls);
if (nulls != null) {
sb.append(" NULLS ").append(nulls);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public JpaProvider createJpaProvider(EntityManager em) {
if (persistenceUnitUtil == null && em != null) {
persistenceUnitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil();
}
return new EclipseLinkJpaProvider(persistenceUnitUtil);
return new EclipseLinkJpaProvider(persistenceUnitUtil, getDbms(entityManagerFactory));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,32 +306,28 @@ public void renderNullPrecedence(StringBuilder sb, String expression, String res
if (nulls != null) {
if (db != DB.OTHER) {
if (db == DB.DB2) {
if (("FIRST".equals(nulls) && "DESC".equalsIgnoreCase(order)) || ("LAST".equals(nulls) && "ASC".equalsIgnoreCase(order))) {
// The following are ok according to DB2 docs
if (("FIRST".equals(nulls) && "DESC".equalsIgnoreCase(order)) || "LAST".equals(nulls) && "ASC".equalsIgnoreCase(order)) {
// According to DB2 docs, the following are the defaults
// ASC + NULLS LAST
// DESC + NULLS FIRST
sb.append(expression);
if (order != null) {
sb.append(" ").append(order).append(" NULLS ").append(nulls);
}
sb.append(" ").append(order);
return;
}
} else if (db == DB.MSSQL) {
if (("ASC".equalsIgnoreCase(order) && "FIRST".equals(nulls)) || ("DESC".equalsIgnoreCase(order) && "LAST".equals(nulls))) {
if ("ASC".equalsIgnoreCase(order) && "FIRST".equals(nulls) || "DESC".equalsIgnoreCase(order) && "LAST".equals(nulls)) {
// The following are the defaults, so just let them through
// ASC + NULLS FIRST
// DESC + NULLS LAST
sb.append(expression);
if (order != null) {
sb.append(" ").append(order);
}
sb.append(" ").append(order);
return;
}
}

// According to the following, MySQL sorts NULLS FIRST when ASC and NULLS LAST when DESC, so we only need the expression for opposite cases
// https://dev.mysql.com/doc/refman/8.0/en/working-with-null.html
if (db != DB.MY_SQL || ("ASC".equalsIgnoreCase(order) && "LAST".equals(nulls)) || ("DESC".equalsIgnoreCase(order) && "FIRST".equals(nulls))) {
if (db != DB.MY_SQL || "ASC".equalsIgnoreCase(order) && "LAST".equals(nulls) || "DESC".equalsIgnoreCase(order) && "FIRST".equals(nulls)) {
// Unfortunately we have to take care of that our selves because the SQL generation has a bug for MySQL: HHH-10241
sb.append("CASE WHEN ").append(resolvedExpression != null ? resolvedExpression : expression).append(" IS NULL THEN ");
if ("FIRST".equals(nulls)) {
Expand Down
2 changes: 1 addition & 1 deletion travis/before_script_oracle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Sets up environment for Blaze-Persistence backend MSSQL at travis-ci.org
#

docker run --shm-size=1536m --name oracle -d -p 1521:1521 alexeiled/docker-oracle-xe-11g
docker run --shm-size=1536m --name oracle -d -p 1521:1521 wnameless/oracle-xe-11g

docker cp oracle:/u01/app/oracle/product/11.2.0/xe/jdbc/lib/ojdbc6.jar ojdbc.jar
mvn install:install-file -Dfile=ojdbc.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -DgeneratePom=true

0 comments on commit 8494cc4

Please sign in to comment.