HHH-9100 - Improve CAST function support

This commit is contained in:
Steve Ebersole 2014-04-04 16:00:45 -05:00
parent 1dd12ec9e9
commit 137c2daf81
4 changed files with 46 additions and 24 deletions

View File

@ -629,7 +629,7 @@ functionCall
processFunction( #functionCall, inSelect );
inFunctionCall=false;
}
| #(CAST {inFunctionCall=true;} expr pathAsIdent) {
| #(CAST {inFunctionCall=true;} exprOrSubquery pathAsIdent) {
processCastFunction( #functionCall, inSelect );
inFunctionCall=false;
}

View File

@ -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

View File

@ -60,7 +60,7 @@ public class CastFunction implements SQLFunction {
@Override
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() );
}
final String type = (String) args.get( 1 );
final int[] sqlTypeCodes = factory.getTypeResolver().heuristicType( type ).sqlTypes( factory );

View File

@ -44,6 +44,7 @@ import org.hibernate.hql.internal.ast.util.ASTPrinter;
import org.hibernate.internal.CoreLogging;
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;
@ -145,8 +146,8 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
}
@Override
protected void commaBetweenParameters(String comma) {
writer.commaBetweenParameters( comma );
protected void betweenFunctionArguments() {
writer.betweenFunctionArguments();
}
@Override
@ -241,14 +242,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 {
@ -274,7 +268,7 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
}
@Override
public void commaBetweenParameters(String comma) {
public void betweenFunctionArguments() {
++argInd;
}
@ -288,20 +282,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;
}
}
@ -315,8 +333,8 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
}
@Override
public void commaBetweenParameters(String comma) {
getStringBuilder().append( comma );
public void betweenFunctionArguments() {
getStringBuilder().append( ", " );
}
}