org.hotrod.runtime.livesql.dialects.DB2Dialect Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hotrod-livesql Show documentation
Show all versions of hotrod-livesql Show documentation
HotRod is an ORM for Java, Spring and SpringBoot.
package org.hotrod.runtime.livesql.dialects;
import java.util.List;
import org.hotrod.runtime.livesql.exceptions.InvalidLiveSQLStatementException;
import org.hotrod.runtime.livesql.exceptions.UnsupportedLiveSQLFeatureException;
import org.hotrod.runtime.livesql.expressions.numbers.NumberExpression;
import org.hotrod.runtime.livesql.expressions.strings.StringExpression;
import org.hotrod.runtime.livesql.ordering.OrderingTerm;
import org.hotrod.runtime.livesql.queries.select.CrossJoin;
import org.hotrod.runtime.livesql.queries.select.FullOuterJoin;
import org.hotrod.runtime.livesql.queries.select.InnerJoin;
import org.hotrod.runtime.livesql.queries.select.Join;
import org.hotrod.runtime.livesql.queries.select.LeftOuterJoin;
import org.hotrod.runtime.livesql.queries.select.NaturalFullOuterJoin;
import org.hotrod.runtime.livesql.queries.select.NaturalInnerJoin;
import org.hotrod.runtime.livesql.queries.select.NaturalLeftOuterJoin;
import org.hotrod.runtime.livesql.queries.select.NaturalRightOuterJoin;
import org.hotrod.runtime.livesql.queries.select.QueryWriter;
import org.hotrod.runtime.livesql.queries.select.RightOuterJoin;
import org.hotrod.runtime.livesql.queries.select.UnionJoin;
import org.hotrodorm.hotrod.utils.Separator;
public class DB2Dialect extends SQLDialect {
public DB2Dialect(final String productName, final String productVersion, final int majorVersion,
final int minorVersion) {
super(productName, productVersion, majorVersion, minorVersion);
}
// Identifier rendering
@Override
public IdentifierRenderer getIdentifierRenderer() {
// Identifier names are by default upper case in DB2
return new IdentifierRenderer("[A-Z][A-Z0-9_]*", "\"", "\"", false);
}
// Join rendering
@Override
public JoinRenderer getJoinRenderer() {
return new JoinRenderer() {
@Override
public String renderJoinKeywords(final Join join) throws UnsupportedLiveSQLFeatureException {
if (join instanceof InnerJoin) {
return "JOIN";
} else if (join instanceof LeftOuterJoin) {
return "LEFT OUTER JOIN";
} else if (join instanceof RightOuterJoin) {
return "RIGHT OUTER JOIN";
} else if (join instanceof FullOuterJoin) {
return "FULL OUTER JOIN";
} else if (join instanceof CrossJoin) {
return "CROSS JOIN";
} else if (join instanceof NaturalInnerJoin) {
return "NATURAL JOIN";
} else if (join instanceof NaturalLeftOuterJoin) {
return "NATURAL LEFT OUTER JOIN";
} else if (join instanceof NaturalRightOuterJoin) {
return "NATURAL RIGHT OUTER JOIN";
} else if (join instanceof NaturalFullOuterJoin) {
return "NATURAL FULL OUTER JOIN";
} else if (join instanceof UnionJoin) {
throw new UnsupportedLiveSQLFeatureException("Union joins are not supported in DB2 database");
} else {
throw new UnsupportedLiveSQLFeatureException(
"Invalid join type (" + join.getClass().getSimpleName() + ") in DB2 database");
}
}
};
}
// Pagination rendering
public PaginationRenderer getPaginationRenderer() {
return new PaginationRenderer() {
@Override
public PaginationType getPaginationType(final Integer offset, final Integer limit) {
if (offset != null) {
if (!versionIsAtLeast(11, 1)) {
throw new UnsupportedLiveSQLFeatureException("This version of DB2 (" + renderVersion()
+ ") does not support the OFFSET clause. DB2 versions 11.1 and newer do");
}
}
return PaginationType.BOTTOM;
}
@Override
public void renderTopPagination(final Integer offset, final Integer limit, final QueryWriter w) {
throw new UnsupportedLiveSQLFeatureException("In DB2 pagination cannot be rendered at the top");
}
@Override
public void renderBottomPagination(final Integer offset, final Integer limit, final QueryWriter w) {
if (offset != null) {
if (!versionIsAtLeast(11, 1)) {
throw new UnsupportedLiveSQLFeatureException("This version of DB2 (" + renderVersion()
+ ") does not support the OFFSET clause. Support starts in DB2 11.1");
}
}
if (offset != null) {
w.write("\nOFFSET " + offset + " ROWS");
}
if (limit != null) {
w.write("\nFETCH NEXT " + limit + " ROWS ONLY");
}
}
@Override
public void renderBeginEnclosingPagination(final Integer offset, final Integer limit, final QueryWriter w) {
throw new UnsupportedLiveSQLFeatureException("In DB2 pagination cannot be rendered in an enclosing way");
}
@Override
public void renderEndEnclosingPagination(final Integer offset, final Integer limit, final QueryWriter w) {
throw new UnsupportedLiveSQLFeatureException("In DB2 pagination cannot be rendered in an enclosing way");
}
};
}
// Set operation rendering
@Override
public SetOperationRenderer getSetOperationRenderer() {
return new SetOperationRenderer() {
@Override
public void render(final SetOperation setOperation, final QueryWriter w) {
switch (setOperation) {
case UNION:
w.write("UNION");
break;
case UNION_ALL:
w.write("UNION ALL");
break;
case INTERSECT:
w.write("INTERSECT");
break;
case INTERSECT_ALL:
w.write("INTERSECT ALL");
break;
case EXCEPT:
w.write("EXCEPT");
break;
case EXCEPT_ALL:
w.write("EXCEPT ALL");
break;
default:
throw new InvalidLiveSQLStatementException("Invalid set operation '" + setOperation + "'.");
}
}
};
}
// Function rendering
@Override
public FunctionRenderer getFunctionRenderer() {
return new FunctionRenderer() {
// General purpose functions
@Override
public void groupConcat(final QueryWriter w, final boolean distinct, final StringExpression value,
final List ordering, final StringExpression separator) {
if (distinct) {
throw new UnsupportedLiveSQLFeatureException(
"DB2 does not support DISTINCT on the GROUP_CONCAT() function (listagg())");
}
w.write("listagg(");
value.renderTo(w);
if (separator != null) {
w.write(", ");
separator.renderTo(w);
}
w.write(")");
if (ordering != null && !ordering.isEmpty()) {
w.write(" withing group (ORDER BY ");
Separator sep = new Separator();
for (OrderingTerm t : ordering) {
w.write(sep.render());
t.renderTo(w);
}
w.write(")");
}
}
// Arithmetic functions
@Override
public void logarithm(final QueryWriter w, final NumberExpression x, final NumberExpression base) {
if (base == null) {
this.write(w, "ln", x);
} else {
w.write("(");
this.write(w, "ln", x);
w.write(" / ");
this.write(w, "ln", base);
w.write(")");
}
}
// String functions
// Date/Time functions
@Override
public void currentDate(final QueryWriter w) {
w.write("current date");
}
@Override
public void currentTime(final QueryWriter w) {
w.write("current time");
}
@Override
public void currentDateTime(final QueryWriter w) {
w.write("current timestamp");
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy