Re-introduce the regex pattern for the format pattern check

This commit is contained in:
Christian Beikov 2022-01-18 16:19:28 +01:00
parent d0f6c3302c
commit 181217ef1f
1 changed files with 25 additions and 14 deletions

View File

@ -6,8 +6,7 @@
*/
package org.hibernate.query.sqm.tree.expression;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.regex.Pattern;
import org.hibernate.query.SemanticException;
import org.hibernate.query.sqm.NodeBuilder;
@ -20,23 +19,35 @@ import org.hibernate.query.sqm.SqmExpressable;
* @author Gavin King
*/
public class SqmFormat extends SqmLiteral<String> {
// G era
// y year in era
// Y week year (ISO)
// M month in year
// w week in year (ISO)
// W week in month
// E day name in week
// e day number in week (*very* inconsistent across DBs)
// d day in month
// D day in year
// a AM/PM
// H hour of day (0-23)
// h clock hour of am/pm (1-12)
// m minute of hour
// s second of minute
// S fraction of second
// z time zone name e.g. PST
// x zone offset e.g. +03, +0300, +03:00
// Z zone offset e.g. +0300
// see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
private static final Pattern FORMAT = Pattern.compile( "('[^']+'|[:;/,.!@#$^&?~`|()\\[\\]{}<>\\-+*=]|\\s|G{1,2}|[yY]{1,4}|M{1,4}|w{1,2}|W|E{3,4}|e{1,2}|d{1,2}|D{1,3}|a|[Hhms]{1,2}|S{1,6}|[zZx]{1,3})*");
public SqmFormat(
String value,
SqmExpressable<String> inherentType,
NodeBuilder nodeBuilder) {
super(value, inherentType, nodeBuilder);
try {
DateTimeFormatter.ofPattern( value );
}
catch (IllegalArgumentException ex) {
throw new SemanticException(
String.format(
Locale.ROOT,
"Format [%s] does not conform to the expected format of java.time.DateTimeFormatter!",
value
),
ex
);
if (!FORMAT.matcher(value).matches()) {
throw new SemanticException("illegal format pattern '" + value + "'");
}
}