org.hotrod.runtime.livesql.dialects.MariaDBDialect 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 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.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;
public class MariaDBDialect extends SQLDialect {
public MariaDBDialect(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 case sensitive in MariaDB
return new IdentifierRenderer("[a-zA-Z][a-zA-Z0-9_]*", "`", "`", true);
}
// 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) {
throw new UnsupportedLiveSQLFeatureException("Full outer joins are not supported in MariaDB");
} 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 MariaDB database");
} else {
throw new UnsupportedLiveSQLFeatureException(
"Invalid join type (" + join.getClass().getSimpleName() + ") in MariaDB database");
}
}
};
}
// Pagination rendering
public PaginationRenderer getPaginationRenderer() {
return new PaginationRenderer() {
@Override
public PaginationType getPaginationType(final Integer offset, final Integer limit) {
return offset != null || limit != null ? PaginationType.BOTTOM : null;
}
@Override
public void renderTopPagination(final Integer offset, final Integer limit, final QueryWriter w) {
throw new UnsupportedLiveSQLFeatureException("Pagination can only be rendered at the bottom in MariaDB");
}
@Override
public void renderBottomPagination(final Integer offset, final Integer limit, final QueryWriter w) {
if (limit != null) {
if (offset != null) {
w.write("\nLIMIT " + limit + "\nOFFSET " + offset);
} else {
w.write("\nLIMIT " + limit);
}
} else {
w.write("\nOFFSET " + offset);
}
}
@Override
public void renderBeginEnclosingPagination(final Integer offset, final Integer limit, final QueryWriter w) {
throw new UnsupportedLiveSQLFeatureException("Pagination can only be rendered at the bottom in MariaDB");
}
@Override
public void renderEndEnclosingPagination(final Integer offset, final Integer limit, final QueryWriter w) {
throw new UnsupportedLiveSQLFeatureException("Pagination can only be rendered at the bottom in MariaDB");
}
};
}
// 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:
if (versionIsAtLeast(10, 3)) {
w.write("INTERSECT");
} else {
throw new UnsupportedLiveSQLFeatureException(
"This version of MariaDB does not support the INTERSECT set operation. It's supported starting on MariaDB 10.3. "
+ "Nevertheless, it can be simulated using a semi join");
}
break;
case INTERSECT_ALL:
throw new UnsupportedLiveSQLFeatureException("MariaDB does not support the INTERSECT ALL set operation. "
+ "Nevertheless, it can be simulated using a semi join");
case EXCEPT:
if (versionIsAtLeast(10, 3)) {
w.write("EXCEPT");
} else {
throw new UnsupportedLiveSQLFeatureException(
"This version of MariaDB does not support the EXCEPT set operation. It's supported starting on MariaDB 10.3. "
+ "Nevertheless, it can be simulated using an anti join");
}
break;
case EXCEPT_ALL:
throw new UnsupportedLiveSQLFeatureException("MariaDB does not support the EXCEPT ALL set operation. "
+ "Nevertheless, it can be simulated using an anti join");
default:
throw new InvalidLiveSQLStatementException("Invalid set operation '" + setOperation + "'.");
}
}
};
}
// Function rendering
@Override
public FunctionRenderer getFunctionRenderer() {
return new FunctionRenderer() {
// General purpose functions
// Arithmetic functions
@Override
public void trunc(final QueryWriter w, final NumberExpression x, final NumberExpression places) {
if (places == null) {
this.write(w, "truncate", x);
} else {
this.write(w, "truncate", x, places);
}
}
// String functions
// Date/Time functions
@Override
public void currentDate(final QueryWriter w) {
w.write("curdate()");
}
@Override
public void currentTime(final QueryWriter w) {
w.write("curtime()");
}
@Override
public void currentDateTime(final QueryWriter w) {
w.write("now()");
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy