Skip to content

Commit cb1ead6

Browse files
peter1123581321gavinking
authored andcommitted
HHH-9812 @formula with SQL cast function
1 parent cb45dc9 commit cb1ead6

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

hibernate-core/src/main/java/org/hibernate/sql/Template.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ public static String renderWhereStringTemplate(
164164
boolean inFromClause = false;
165165
boolean afterFromTable = false;
166166
boolean inExtractOrTrim = false;
167+
boolean inCast = false;
168+
boolean afterCastAs = false;
167169

168170
boolean hasMore = tokens.hasMoreTokens();
169171
String nextToken = hasMore ? tokens.nextToken() : null;
@@ -237,6 +239,14 @@ else if ( FUNCTION_WITH_FROM_KEYWORDS.contains(lcToken) && "(".equals( nextToken
237239
result.append(token);
238240
inExtractOrTrim = true;
239241
}
242+
else if ( "cast".equals( lcToken ) ) {
243+
result.append( token );
244+
inCast = true;
245+
}
246+
else if ( inCast && ("as".equals( lcToken ) || afterCastAs) ) {
247+
result.append( token );
248+
afterCastAs = true;
249+
}
240250
else if ( !inFromClause // don't want to append alias to tokens inside the FROM clause
241251
&& isIdentifier( token )
242252
&& !isFunctionOrKeyword( lcToken, nextToken, dialect, typeConfiguration )
@@ -248,6 +258,8 @@ && isIdentifier( token )
248258
else {
249259
if ( ")".equals( lcToken) ) {
250260
inExtractOrTrim = false;
261+
inCast = false;
262+
afterCastAs = false;
251263
}
252264
else if ( !inExtractOrTrim
253265
&& BEFORE_TABLE_KEYWORDS.contains(lcToken) ) {

hibernate-core/src/test/java/org/hibernate/orm/test/sql/TemplateTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ public void templateLiterals(SessionFactoryScope scope) {
7474
"where {@}.foo>10 and {@}.bar is not null", factory );
7575
assertWhereStringTemplate("select t.foo, o.bar from table as t left join other as o on t.id = o.id where t.foo>10 and o.bar is not null order by o.bar",
7676
"select t.foo, o.bar from table as t left join other as o on t.id = o.id where t.foo>10 and o.bar is not null order by o.bar", factory );
77-
77+
assertWhereStringTemplate( "CAST(foo AS unsigned)",
78+
"CAST({@}.foo AS unsigned)", factory );
79+
assertWhereStringTemplate( "CAST(foo AS signed)",
80+
"CAST({@}.foo AS signed)", factory );
7881
}
7982

8083
private static void assertWhereStringTemplate(String sql, SessionFactoryImplementor sf) {

0 commit comments

Comments
 (0)