1 package org.hibernate.dialect.function; 3 4 import java.util.List ; 5 6 import org.hibernate.QueryException; 7 import org.hibernate.engine.Mapping; 8 import org.hibernate.engine.SessionFactoryImplementor; 9 import org.hibernate.type.Type; 10 import org.hibernate.type.TypeFactory; 11 12 17 public class CastFunction implements SQLFunction { 18 19 public Type getReturnType(Type columnType, Mapping mapping) throws QueryException { 20 return columnType; } 22 23 public boolean hasArguments() { 24 return true; 25 } 26 27 public boolean hasParenthesesIfNoArguments() { 28 return true; 29 } 30 31 public String render(List args, SessionFactoryImplementor factory) throws QueryException { 32 if ( args.size()!=2 ) { 33 throw new QueryException("cast() requires two arguments"); 34 } 35 String type = (String ) args.get(1); 36 int[] sqlTypeCodes = TypeFactory.heuristicType(type).sqlTypes(factory); 37 if ( sqlTypeCodes.length!=1 ) { 38 throw new QueryException("invalid Hibernate type for cast()"); 39 } 40 String sqlType = factory.getDialect().getCastTypeName( sqlTypeCodes[0] ); 41 if (sqlType==null) { 42 sqlType = type; 44 } 45 52 return "cast(" + args.get(0) + " as " + sqlType + ')'; 53 } 54 55 } 56 | Popular Tags |