Skip to content

Commit

Permalink
Cleanup vars, switches, pattern matching and text blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
velo committed Aug 21, 2024
1 parent 934738e commit 22ec9fe
Show file tree
Hide file tree
Showing 44 changed files with 387 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public class TemplateFactory {

private static final Pattern elementPattern =
Pattern.compile(
"\\{"
+ "(%?%?)"
+ "(\\d+)"
+ "(?:([+-/*])(?:(\\d+)|'(-?\\d+(?:\\.\\d+)?)'))?"
+ "([slu%]?%?)"
+ "\\}");
"""
\\{\
(%?%?)\
(\\d+)\
(?:([+-/*])(?:(\\d+)|'(-?\\d+(?:\\.\\d+)?)'))?\
([slu%]?%?)\
\\}""");

private final Map<String, Template> cache = Collections.synchronizedMap(new WeakHashMap<>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public String visit(FactoryExpression<?> e, Templates templates) {

@Override
public String visit(Operation<?> o, Templates templates) {
final Template template = templates.getTemplate(o.getOperator());
final var template = templates.getTemplate(o.getOperator());
if (template != null) {
final var precedence = templates.getPrecedence(o.getOperator());
final var builder = new StringBuilder();
Expand Down Expand Up @@ -90,7 +90,7 @@ public String visit(Path<?> p, Templates templates) {
final Path<?> parent = p.getMetadata().getParent();
final var elem = p.getMetadata().getElement();
if (parent != null) {
Template pattern = templates.getTemplate(p.getMetadata().getPathType());
var pattern = templates.getTemplate(p.getMetadata().getPathType());
if (pattern != null) {
final List<?> args = Arrays.asList(parent, elem);
final var builder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,11 @@ public static <T> List<T> unmodifiableList(List<T> list) {
if (isUnmodifiableType(list.getClass())) {
return list;
}
switch (list.size()) {
case 0:
return Collections.emptyList();
case 1:
return Collections.singletonList(list.get(0));
default:
return Collections.unmodifiableList(new ArrayList<>(list));
}
return switch (list.size()) {
case 0 -> Collections.emptyList();
case 1 -> Collections.singletonList(list.get(0));
default -> Collections.unmodifiableList(new ArrayList<>(list));
};
}

/**
Expand All @@ -127,22 +124,20 @@ public static <T> Set<T> unmodifiableSet(Set<T> set) {
if (isUnmodifiableType(set.getClass())) {
return set;
}
switch (set.size()) {
case 0:
return Collections.emptySet();
case 1:
return Collections.singleton(set.iterator().next());
default:
return Collections.unmodifiableSet(
(Set<T>)
(set instanceof LinkedHashSet
? ((LinkedHashSet<T>) set).clone()
: set instanceof TreeSet
? ((TreeSet<T>) set).clone()
: set instanceof HashSet
? ((HashSet<T>) set).clone()
: new LinkedHashSet<>(set)));
}
return switch (set.size()) {
case 0 -> Collections.emptySet();
case 1 -> Collections.singleton(set.iterator().next());
default ->
Collections.unmodifiableSet(
(Set<T>)
(set instanceof LinkedHashSet
? ((LinkedHashSet<T>) set).clone()
: set instanceof TreeSet
? ((TreeSet<T>) set).clone()
: set instanceof HashSet
? ((HashSet<T>) set).clone()
: new LinkedHashSet<>(set)));
};
}

public static <T> List<List<T>> partition(List<T> list, int batchSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,17 +517,18 @@ protected void visitOperation(
if (operator.getClass().getName().endsWith("SQLOps")) {
throw new IllegalArgumentException(
String.format(
"SQL Expressions like %s are not supported in JPQL - the query language for JPA."
+ " SQLExpressions.* can only be used in JPQL queries when these functions"
+ " are registered as custom function in your ORM.%n\tTo fix this issue, you"
+ " have three options:%n\t1) If you do want to use advanced, dialect"
+ " specific, SQL functions within JPQL, make sure to make these functions"
+ " available to your ORM through custom functions and register these with"
+ " your JPATemplates instance.%n\t2) Use JPASQLQuery instead. This allows"
+ " you to generate a pure SQL query based on your JPA metamodel.%n\t3)"
+ " Consider using the Blaze-Persistence QueryDSL integration."
+ " Blaze-Persistence is an extension on top of JPA that makes various SQL"
+ " specific functions like window functions available to JPQL.",
"""
SQL Expressions like %s are not supported in JPQL - the query language for JPA.\
SQLExpressions.* can only be used in JPQL queries when these functions\
are registered as custom function in your ORM.%n To fix this issue, you\
have three options:%n 1) If you do want to use advanced, dialect\
specific, SQL functions within JPQL, make sure to make these functions\
available to your ORM through custom functions and register these with\
your JPATemplates instance.%n 2) Use JPASQLQuery instead. This allows\
you to generate a pure SQL query based on your JPA metamodel.%n 3)\
Consider using the Blaze-Persistence QueryDSL integration.\
Blaze-Persistence is an extension on top of JPA that makes various SQL\
specific functions like window functions available to JPQL.""",
operator.name()),
e);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ public void nullsFirst() {
md.addOrderBy(cat.name.asc().nullsFirst());
serializer.serialize(md, false, null);
assertThat(serializer.toString())
.isEqualTo("select cat\n" + "from Cat cat\n" + "order by cat.name asc nulls first");
.isEqualTo("""
select cat
from Cat cat
order by cat.name asc nulls first""");
}

@Test
Expand All @@ -262,7 +265,10 @@ public void nullsLast() {
md.addOrderBy(cat.name.asc().nullsLast());
serializer.serialize(md, false, null);
assertThat(serializer.toString())
.isEqualTo("select cat\n" + "from Cat cat\n" + "order by cat.name asc nulls last");
.isEqualTo("""
select cat
from Cat cat
order by cat.name asc nulls last""");
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,11 @@ public String escapeLiteral(String str) {

@Override
public String getCastTypeNameForCode(int code) {
switch (code) {
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
case Types.BIGINT:
return "signed";
case Types.FLOAT:
case Types.DOUBLE:
case Types.REAL:
case Types.DECIMAL:
return "decimal";
case Types.VARCHAR:
return "char";
default:
return super.getCastTypeNameForCode(code);
}
return switch (code) {
case Types.TINYINT, Types.SMALLINT, Types.INTEGER, Types.BIGINT -> "signed";
case Types.FLOAT, Types.DOUBLE, Types.REAL, Types.DECIMAL -> "decimal";
case Types.VARCHAR -> "char";
default -> super.getCastTypeNameForCode(code);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,12 @@ protected SQLServerTemplates(Set<String> keywords, char escape, boolean quote) {

@Override
public String serialize(String literal, int jdbcType) {
switch (jdbcType) {
case Types.TIMESTAMP:
case TIMESTAMP_WITH_TIMEZONE:
return "{ts '" + literal + "'}";
case Types.DATE:
return "{d '" + literal + "'}";
case Types.TIME:
case TIME_WITH_TIMEZONE:
return "{t '" + literal + "'}";
default:
return super.serialize(literal, jdbcType);
}
return switch (jdbcType) {
case Types.TIMESTAMP, TIMESTAMP_WITH_TIMEZONE -> "{ts '" + literal + "'}";
case Types.DATE -> "{d '" + literal + "'}";
case Types.TIME, TIME_WITH_TIMEZONE -> "{t '" + literal + "'}";
default -> super.serialize(literal, jdbcType);
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,41 +447,34 @@ protected SQLTemplates(
}

public String serialize(String literal, int jdbcType) {
switch (jdbcType) {
case Types.TIMESTAMP:
case TIMESTAMP_WITH_TIMEZONE:
return "(timestamp '" + literal + "')";
case Types.DATE:
return "(date '" + literal + "')";
case Types.TIME:
case TIME_WITH_TIMEZONE:
return "(time '" + literal + "')";
case Types.CHAR:
case Types.CLOB:
case Types.LONGNVARCHAR:
case Types.LONGVARCHAR:
case Types.NCHAR:
case Types.NCLOB:
case Types.NVARCHAR:
case Types.VARCHAR:
return "'" + escapeLiteral(literal) + "'";
case Types.BIGINT:
case Types.BIT:
case Types.BOOLEAN:
case Types.DECIMAL:
case Types.DOUBLE:
case Types.FLOAT:
case Types.INTEGER:
case Types.NULL:
case Types.NUMERIC:
case Types.SMALLINT:
case Types.TINYINT:
return literal;
default:
// for other JDBC types the Type instance is expected to provide
// the necessary quoting
return literal;
}
return switch (jdbcType) {
case Types.TIMESTAMP, TIMESTAMP_WITH_TIMEZONE -> "(timestamp '" + literal + "')";
case Types.DATE -> "(date '" + literal + "')";
case Types.TIME, TIME_WITH_TIMEZONE -> "(time '" + literal + "')";
case Types.CHAR,
Types.CLOB,
Types.LONGNVARCHAR,
Types.LONGVARCHAR,
Types.NCHAR,
Types.NCLOB,
Types.NVARCHAR,
Types.VARCHAR ->
"'" + escapeLiteral(literal) + "'";
case Types.BIGINT,
Types.BIT,
Types.BOOLEAN,
Types.DECIMAL,
Types.DOUBLE,
Types.FLOAT,
Types.INTEGER,
Types.NULL,
Types.NUMERIC,
Types.SMALLINT,
Types.TINYINT ->
literal;
default -> /* for other JDBC types the Type instance is expected to provide */ /* the necessary quoting */
literal;
};
}

public String escapeLiteral(String str) {
Expand Down Expand Up @@ -590,20 +583,14 @@ public final String getJoin() {
}

public final String getJoinSymbol(JoinType joinType) {
switch (joinType) {
case JOIN:
return join;
case INNERJOIN:
return innerJoin;
case FULLJOIN:
return fullJoin;
case LEFTJOIN:
return leftJoin;
case RIGHTJOIN:
return rightJoin;
default:
return crossJoin;
}
return switch (joinType) {
case JOIN -> join;
case INNERJOIN -> innerJoin;
case FULLJOIN -> fullJoin;
case LEFTJOIN -> leftJoin;
case RIGHTJOIN -> rightJoin;
default -> crossJoin;
};
}

public final String getKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,12 @@ public SQLTemplates.Builder getBuilder(DatabaseMetaData md) throws SQLException
} else if (name.equals("postgresql")) {
return PostgreSQLTemplates.builder();
} else if (name.equals("microsoft sql server")) {
switch (md.getDatabaseMajorVersion()) {
case 13:
case 12:
case 11:
return SQLServer2012Templates.builder();
case 10:
return SQLServer2008Templates.builder();
case 9:
return SQLServer2005Templates.builder();
default:
return SQLServerTemplates.builder();
}
return switch (md.getDatabaseMajorVersion()) {
case 13, 12, 11 -> SQLServer2012Templates.builder();
case 10 -> SQLServer2008Templates.builder();
case 9 -> SQLServer2005Templates.builder();
default -> SQLServerTemplates.builder();
};
} else {
return new SQLTemplates.Builder() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public abstract class AbstractR2DBCDeleteClause<C extends AbstractR2DBCDeleteCla

protected static final ValidatingVisitor validatingVisitor =
new ValidatingVisitor(
"Undeclared path '%s'. "
+ "A delete operation can only reference a single table. "
+ "Consider this alternative: DELETE ... WHERE EXISTS (subquery)");
"""
Undeclared path '%s'. \
A delete operation can only reference a single table. \
Consider this alternative: DELETE ... WHERE EXISTS (subquery)""");

protected final RelationalPath<?> entity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ public void union() {
""");
} else {
assertThat(union.toString())
.isEqualTo("select 1 as col1)\n" + "union\n" + "select 2\n" + "union\n" + "select 3");
.isEqualTo("""
select 1 as col1)
union
select 2
union
select 3""");
}
} else {
var dummyTable = templates.getDummyTable();
Expand Down
Loading

0 comments on commit 22ec9fe

Please sign in to comment.