@@ -61,31 +61,12 @@ public virtual SqlString Render(IList args, ISessionFactoryImplementor factory)
6161 throw new QueryException ( "cast() requires two arguments" ) ;
6262 }
6363 string typeName = args [ 1 ] . ToString ( ) ;
64- string sqlType ;
65- IType hqlType = TypeFactory . HeuristicType ( typeName ) ;
6664
67- if ( hqlType != null )
68- {
69- SqlType [ ] sqlTypeCodes = hqlType . SqlTypes ( factory ) ;
70- if ( sqlTypeCodes . Length != 1 )
71- {
72- throw new QueryException ( "invalid NHibernate type for cast(), was:" + typeName ) ;
73- }
74-
75- sqlType = factory . Dialect . GetCastTypeName ( sqlTypeCodes [ 0 ] ) ;
76- //{
77- // //trim off the length/precision/scale
78- // int loc = sqlType.IndexOf('(');
79- // if (loc>-1)
80- // {
81- // sqlType = sqlType.Substring(0, loc);
82- // }
83- //}
84- }
85- else
86- {
87- throw new QueryException ( string . Format ( "invalid Hibernate type for cast(): type {0} not found" , typeName ) ) ;
88- }
65+ IType hqlType =
66+ TypeFactory . HeuristicType ( typeName )
67+ ?? throw new QueryException ( string . Format ( "invalid Hibernate type for cast(): type {0} not found" , typeName ) ) ;
68+
69+ string sqlType = GetCastTypeName ( factory , hqlType , typeName ) ;
8970
9071 // TODO 6.0: Remove pragma block with its content
9172#pragma warning disable 618
@@ -117,6 +98,20 @@ protected virtual SqlString Render(object expression, string sqlType, ISessionFa
11798 return new SqlString ( "cast(" , expression , " as " , sqlType , ")" ) ;
11899 }
119100
101+ internal SqlString Render ( IList args , IType expectedType , ISessionFactoryImplementor factory )
102+ {
103+ return Render ( args [ 0 ] , GetCastTypeName ( factory , expectedType , expectedType . Name ) , factory ) ;
104+ }
105+
106+ private static string GetCastTypeName ( ISessionFactoryImplementor factory , IType hqlType , string typeName )
107+ {
108+ SqlType [ ] sqlTypeCodes = hqlType . SqlTypes ( factory ) ;
109+ if ( sqlTypeCodes . Length != 1 )
110+ throw new QueryException ( "invalid NHibernate type for cast(), was:" + typeName ) ;
111+
112+ return factory . Dialect . GetCastTypeName ( sqlTypeCodes [ 0 ] ) ;
113+ }
114+
120115 #region IFunctionGrammar Members
121116
122117 bool IFunctionGrammar . IsSeparator ( string token )
0 commit comments