HHH-9100 - Improve CAST function support
(cherry picked from commit 137c2daf81
)
Conflicts:
hibernate-core/src/main/java/org/hibernate/hql/internal/ast/SqlGenerator.java
This commit is contained in:
parent
6cef805a32
commit
cfc0327637
|
@ -628,7 +628,7 @@ functionCall
|
|||
processFunction( #functionCall, inSelect );
|
||||
inFunctionCall=false;
|
||||
}
|
||||
| #(CAST {inFunctionCall=true;} expr pathAsIdent) {
|
||||
| #(CAST {inFunctionCall=true;} exprOrSubquery pathAsIdent) {
|
||||
processCastFunction( #functionCall, inSelect );
|
||||
inFunctionCall=false;
|
||||
}
|
||||
|
|
|
@ -92,8 +92,8 @@ options {
|
|||
out(")");
|
||||
}
|
||||
|
||||
protected void commaBetweenParameters(String comma) {
|
||||
out(comma);
|
||||
protected void betweenFunctionArguments() {
|
||||
out( ", " );
|
||||
}
|
||||
|
||||
protected void captureExpressionStart() {
|
||||
|
@ -465,11 +465,15 @@ methodCall
|
|||
: #(m:METHOD_CALL i:METHOD_NAME { beginFunctionTemplate(m,i); }
|
||||
( #(EXPR_LIST (arguments)? ) )?
|
||||
{ endFunctionTemplate(m); } )
|
||||
| #( c:CAST { beginFunctionTemplate(c,c); } expr castTargetType { endFunctionTemplate(c); } )
|
||||
| #( c:CAST { beginFunctionTemplate(c,c); } castExpression {betweenFunctionArguments();} castTargetType { endFunctionTemplate(c); } )
|
||||
;
|
||||
|
||||
arguments
|
||||
: expr ( { commaBetweenParameters(", "); } expr )*
|
||||
: expr ( { betweenFunctionArguments(); } expr )*
|
||||
;
|
||||
|
||||
castExpression
|
||||
: selectExpr
|
||||
;
|
||||
|
||||
castTargetType
|
||||
|
|
|
@ -54,7 +54,7 @@ public class CastFunction implements SQLFunction {
|
|||
|
||||
public String render(Type columnType, List args, SessionFactoryImplementor factory) throws QueryException {
|
||||
if ( args.size()!=2 ) {
|
||||
throw new QueryException("cast() requires two arguments");
|
||||
throw new QueryException( "cast() requires two arguments; found : " + args.size() );
|
||||
}
|
||||
String type = (String) args.get(1);
|
||||
int[] sqlTypeCodes = factory.getTypeResolver().heuristicType(type).sqlTypes(factory);
|
||||
|
|
|
@ -43,6 +43,7 @@ import org.hibernate.hql.internal.ast.tree.ParameterNode;
|
|||
import org.hibernate.hql.internal.ast.util.ASTPrinter;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.internal.util.StringHelper;
|
||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||
import org.hibernate.param.ParameterSpecification;
|
||||
import org.hibernate.type.Type;
|
||||
import org.jboss.logging.Logger;
|
||||
|
@ -138,8 +139,8 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void commaBetweenParameters(String comma) {
|
||||
writer.commaBetweenParameters( comma );
|
||||
protected void betweenFunctionArguments() {
|
||||
writer.betweenFunctionArguments();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -234,14 +235,7 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
|
|||
interface SqlWriter {
|
||||
void clause(String clause);
|
||||
|
||||
/**
|
||||
* todo remove this hack
|
||||
* The parameter is either ", " or " , ". This is needed to pass sql generating tests as the old
|
||||
* sql generator uses " , " in the WHERE and ", " in SELECT.
|
||||
*
|
||||
* @param comma either " , " or ", "
|
||||
*/
|
||||
void commaBetweenParameters(String comma);
|
||||
void betweenFunctionArguments();
|
||||
}
|
||||
|
||||
interface FunctionArgumentsCollectingWriter extends SqlWriter {
|
||||
|
@ -265,7 +259,8 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
|
|||
}
|
||||
}
|
||||
|
||||
public void commaBetweenParameters(String comma) {
|
||||
@Override
|
||||
public void betweenFunctionArguments() {
|
||||
++argInd;
|
||||
}
|
||||
|
||||
|
@ -279,20 +274,44 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
|
|||
* which catches function arguments.
|
||||
*/
|
||||
class CastFunctionArguments implements FunctionArgumentsCollectingWriter {
|
||||
private final List<String> args = new ArrayList<String>( 3 );
|
||||
private String castExpression;
|
||||
private String castTargetType;
|
||||
|
||||
private boolean startedType;
|
||||
|
||||
@Override
|
||||
public void clause(String clause) {
|
||||
args.add( clause );
|
||||
if ( startedType ) {
|
||||
if ( castTargetType == null ) {
|
||||
castTargetType = clause;
|
||||
}
|
||||
else {
|
||||
castTargetType += clause;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( castExpression == null ) {
|
||||
castExpression = clause;
|
||||
}
|
||||
else {
|
||||
castExpression += clause;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commaBetweenParameters(String comma) {
|
||||
// todo : should this be an exception? Its not likely to end well if this method is called here...
|
||||
public void betweenFunctionArguments() {
|
||||
if ( startedType ) {
|
||||
throw new QueryException( "CAST function should only have 2 arguments" );
|
||||
}
|
||||
startedType = true;
|
||||
}
|
||||
|
||||
public List getArgs() {
|
||||
return args;
|
||||
List<String> rtn = CollectionHelper.arrayList( 2 );
|
||||
rtn.add( castExpression );
|
||||
rtn.add( castTargetType );
|
||||
return rtn;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -304,8 +323,9 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
|
|||
getStringBuilder().append( clause );
|
||||
}
|
||||
|
||||
public void commaBetweenParameters(String comma) {
|
||||
getStringBuilder().append( comma );
|
||||
@Override
|
||||
public void betweenFunctionArguments() {
|
||||
getStringBuilder().append( ", " );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue