private ITypeHandler ResolveTypeHandler(TypeHandlerFactory typeHandlerFactory, Type parameterClassType, string propertyName, string propertyType, string dbType) { if (parameterClassType == null) { return(typeHandlerFactory.GetUnkownTypeHandler()); } if (typeof(IDictionary).IsAssignableFrom(parameterClassType)) { if ((propertyType == null) || (propertyType.Length == 0)) { return(typeHandlerFactory.GetUnkownTypeHandler()); } try { Type type = TypeUtils.ResolveType(propertyType); return(typeHandlerFactory.GetTypeHandler(type, dbType)); } catch (Exception exception) { throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + exception.Message, exception); } } if (typeHandlerFactory.GetTypeHandler(parameterClassType, dbType) != null) { return(typeHandlerFactory.GetTypeHandler(parameterClassType, dbType)); } Type memberTypeForGetter = ObjectProbe.GetMemberTypeForGetter(parameterClassType, propertyName); return(typeHandlerFactory.GetTypeHandler(memberTypeForGetter, dbType)); }
public void Initialize(IScope scope, Type parameterClass) { if (this._directionAttribute.Length > 0) { this._direction = (ParameterDirection)Enum.Parse(typeof(ParameterDirection), this._directionAttribute, true); } if ((!typeof(IDictionary).IsAssignableFrom(parameterClass) && (parameterClass != null)) && !scope.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(parameterClass)) { if (!this._isComplexMemberName) { this._getAccessor = scope.DataExchangeFactory.AccessorFactory.GetAccessorFactory.CreateGetAccessor(parameterClass, this._propertyName); } else { string name = this._propertyName.Substring(this._propertyName.LastIndexOf('.') + 1); string memberName = this._propertyName.Substring(0, this._propertyName.LastIndexOf('.')); Type memberTypeForGetter = ObjectProbe.GetMemberTypeForGetter(parameterClass, memberName); this._getAccessor = scope.DataExchangeFactory.AccessorFactory.GetAccessorFactory.CreateGetAccessor(memberTypeForGetter, name); } } scope.ErrorContext.MoreInfo = "Check the parameter mapping typeHandler attribute '" + this.CallBackName + "' (must be a ITypeHandlerCallback implementation)."; if (this.CallBackName.Length > 0) { try { ITypeHandlerCallback callback = (ITypeHandlerCallback)Activator.CreateInstance(scope.DataExchangeFactory.TypeHandlerFactory.GetType(this.CallBackName)); this._typeHandler = new CustomTypeHandler(callback); return; } catch (Exception exception) { throw new ConfigurationException("Error occurred during custom type handler configuration. Cause: " + exception.Message, exception); } } if (this.CLRType.Length == 0) { if ((this._getAccessor != null) && scope.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(this._getAccessor.MemberType)) { this._typeHandler = scope.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(this._getAccessor.MemberType, this._dbType); } else { this._typeHandler = scope.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } } else { Type type = TypeUtils.ResolveType(this.CLRType); if (scope.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(type)) { this._typeHandler = scope.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, this._dbType); } else { type = ObjectProbe.GetMemberTypeForGetter(type, this.PropertyName); this._typeHandler = scope.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, this._dbType); } } }
private void RetrieveOutputParameters(RequestScope request, ISqlMapSession session, IDbCommand command, object result) { if (request.ParameterMap != null) { int count = request.ParameterMap.PropertiesList.Count; for (int i = 0; i < count; i++) { ParameterProperty mapping = request.ParameterMap.GetProperty(i); if ((mapping.Direction == ParameterDirection.Output) || (mapping.Direction == ParameterDirection.InputOutput)) { string columnName = string.Empty; if (!session.DataSource.DbProvider.UseParameterPrefixInParameter) { columnName = mapping.ColumnName; } else { columnName = session.DataSource.DbProvider.ParameterPrefix + mapping.ColumnName; } if (mapping.TypeHandler == null) { lock (mapping) { if (mapping.TypeHandler == null) { Type memberTypeForGetter = ObjectProbe.GetMemberTypeForGetter(result, mapping.PropertyName); mapping.TypeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(memberTypeForGetter); } } } IDataParameter parameter = (IDataParameter)command.Parameters[columnName]; object obj2 = parameter.Value; object dataBaseValue = null; if (obj2 == DBNull.Value) { if (mapping.HasNullValue) { dataBaseValue = mapping.TypeHandler.ValueOf(mapping.GetAccessor.MemberType, mapping.NullValue); } else { dataBaseValue = mapping.TypeHandler.NullValue; } } else { dataBaseValue = mapping.TypeHandler.GetDataBaseValue(parameter.Value, result.GetType()); } request.IsRowDataFound = request.IsRowDataFound || (dataBaseValue != null); request.ParameterMap.SetOutputParameter(ref result, mapping, dataBaseValue); } } } }
public ITypeHandler ResolveTypeHandler(Type clazz, string memberName, string clrType, string dbType, bool forSetter) { ITypeHandler typeHandler = null; if (clazz == null) { return(this.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler()); } if (typeof(IDictionary).IsAssignableFrom(clazz)) { if ((clrType == null) || (clrType.Length == 0)) { return(this.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler()); } try { Type type = TypeUtils.ResolveType(clrType); return(this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType)); } catch (Exception exception) { throw new ConfigurationErrorsException("Error. Could not set TypeHandler. Cause: " + exception.Message, exception); } } if (this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(clazz, dbType) != null) { return(this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(clazz, dbType)); } if ((clrType == null) || (clrType.Length == 0)) { Type memberTypeForSetter = null; if (forSetter) { memberTypeForSetter = ObjectProbe.GetMemberTypeForSetter(clazz, memberName); } else { memberTypeForSetter = ObjectProbe.GetMemberTypeForGetter(clazz, memberName); } return(this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(memberTypeForSetter, dbType)); } try { Type type3 = TypeUtils.ResolveType(clrType); typeHandler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type3, dbType); } catch (Exception exception2) { throw new ConfigurationErrorsException("Error. Could not set TypeHandler. Cause: " + exception2.Message, exception2); } return(typeHandler); }
/// <summary> /// Resolve TypeHandler /// </summary> /// <param name="parameterClassType"></param> /// <param name="propertyName"></param> /// <param name="propertyType"></param> /// <param name="dbType"></param> /// <param name="typeHandlerFactory"></param> /// <returns></returns> private ITypeHandler ResolveTypeHandler(TypeHandlerFactory typeHandlerFactory, Type parameterClassType, string propertyName, string propertyType, string dbType) { ITypeHandler handler = null; if (parameterClassType == null) { handler = typeHandlerFactory.GetUnkownTypeHandler(); } else if (typeof(IDictionary).IsAssignableFrom(parameterClassType)) { if (propertyType == null || propertyType.Length == 0) { handler = typeHandlerFactory.GetUnkownTypeHandler(); } else { try { Type typeClass = TypeUtils.ResolveType(propertyType); handler = typeHandlerFactory.GetTypeHandler(typeClass, dbType); } catch (Exception e) { throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); } } } else if (typeHandlerFactory.GetTypeHandler(parameterClassType, dbType) != null) { handler = typeHandlerFactory.GetTypeHandler(parameterClassType, dbType); } else { Type typeClass = ObjectProbe.GetMemberTypeForGetter(parameterClassType, propertyName); handler = typeHandlerFactory.GetTypeHandler(typeClass, dbType); } return(handler); }
/// <summary> /// Retrieve the output parameter and map them on the result object. /// This routine is only use is you specified a ParameterMap and some output attribute /// or if you use a store procedure with output parameter... /// </summary> /// <param name="request"></param> /// <param name="session">The current session.</param> /// <param name="result">The result object.</param> /// <param name="command">The command sql.</param> private static void RetrieveOutputParameters(RequestScope request, ISession session, IDbCommand command, object result) { if (request.ParameterMap != null && request.ParameterMap.HasOutputParameter) { int count = request.ParameterMap.PropertiesList.Count; for (int i = 0; i < count; i++) { ParameterProperty mapping = request.ParameterMap.GetProperty(i); if (mapping.Direction == ParameterDirection.Output || mapping.Direction == ParameterDirection.InputOutput) { string parameterName = string.Empty; if (session.SessionFactory.DataSource.DbProvider.UseParameterPrefixInParameter == false) { parameterName = mapping.ColumnName; } else { parameterName = session.SessionFactory.DataSource.DbProvider.ParameterPrefix + mapping.ColumnName; } if (mapping.TypeHandler == null) // Find the TypeHandler { lock (mapping) { if (mapping.TypeHandler == null) { Type propertyType = ObjectProbe.GetMemberTypeForGetter(result, mapping.PropertyName); mapping.TypeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(propertyType); } } } // Fix IBATISNET-239 //"Normalize" System.DBNull parameters IDataParameter dataParameter = (IDataParameter)command.Parameters[parameterName]; object dbValue = dataParameter.Value; object value = null; bool wasNull = (dbValue == DBNull.Value); if (wasNull) { if (mapping.HasNullValue) { value = mapping.TypeHandler.ValueOf(mapping.GetAccessor.MemberType, mapping.NullValue); } else { value = mapping.TypeHandler.NullValue; } } else { value = mapping.TypeHandler.GetDataBaseValue(dataParameter.Value, result.GetType()); } request.IsRowDataFound = request.IsRowDataFound || (value != null); request.ParameterMap.SetOutputParameter(ref result, mapping, value); } } } }
/// <summary> /// Initializes a new instance of the <see cref="ParameterProperty"/> class. /// </summary> /// <param name="propertyName">Name of the property.</param> /// <param name="columnName">Name of the column.</param> /// <param name="callBackName">Name of the call back.</param> /// <param name="clrType">Type of the CLR.</param> /// <param name="dbType">Type of the db.</param> /// <param name="directionAttribute">The direction attribute.</param> /// <param name="nullValue">The null value.</param> /// <param name="precision">The precision.</param> /// <param name="scale">The scale.</param> /// <param name="size">The size.</param> /// <param name="parameterClass">The parameter class.</param> /// <param name="dataExchangeFactory">The data exchange factory.</param> public ParameterProperty( string propertyName, string columnName, string callBackName, string clrType, string dbType, string directionAttribute, string nullValue, Byte precision, Byte scale, int size, Type parameterClass, DataExchangeFactory dataExchangeFactory ) { Contract.Require.That(propertyName, Is.Not.Null & Is.Not.Empty).When("retrieving argument propertyName in ParameterProperty constructor"); this.columnName = columnName; this.callBackName = callBackName; this.clrType = clrType; this.dbType = dbType; this.nullValue = nullValue; this.precision = precision; this.scale = scale; this.size = size; this._parameterClass = parameterClass; this._dataExchangeFactory = dataExchangeFactory; #region Direction if (directionAttribute.Length > 0) { direction = (ParameterDirection)Enum.Parse(typeof(ParameterDirection), directionAttribute, true); this.directionAttribute = direction.ToString(); } #endregion Direction #region Property Name this._currentPropertyName = propertyName; this._propertyPlaceholder = propertyName; if (propertyName.IndexOf('.') < 0) { isComplexMemberName = false; } else // complex member name FavouriteLineItem.Id { isComplexMemberName = true; } #endregion Property Name #region GetAccessor if (!typeof(IDictionary).IsAssignableFrom(parameterClass) && // Hashtable parameter map parameterClass != null && // value property !dataExchangeFactory.TypeHandlerFactory.IsSimpleType(parameterClass)) // value property { if (!isComplexMemberName) { IGetAccessorFactory getAccessorFactory = dataExchangeFactory.AccessorFactory.GetAccessorFactory; getAccessor = getAccessorFactory.CreateGetAccessor(parameterClass, propertyName); } else // complex member name FavouriteLineItem.Id { string memberName = propertyName.Substring(propertyName.LastIndexOf('.') + 1); string parentName = propertyName.Substring(0, propertyName.LastIndexOf('.')); Type parentType = ObjectProbe.GetMemberTypeForGetter(parameterClass, parentName); IGetAccessorFactory getAccessorFactory = dataExchangeFactory.AccessorFactory.GetAccessorFactory; getAccessor = getAccessorFactory.CreateGetAccessor(parentType, memberName); } } #endregion GetAccessor #region TypeHandler if (CallBackName.Length > 0) { try { Type type = dataExchangeFactory.TypeHandlerFactory.GetType(CallBackName); ITypeHandlerCallback typeHandlerCallback = (ITypeHandlerCallback)Activator.CreateInstance(type); typeHandler = new CustomTypeHandler(typeHandlerCallback); } catch (Exception e) { throw new ConfigurationException("Error occurred during custom type handler configuration. Cause: " + e.Message, e); } } else { if (CLRType.Length == 0) // Unknown { if (getAccessor != null && dataExchangeFactory.TypeHandlerFactory.IsSimpleType(getAccessor.MemberType)) { // Primitive typeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(getAccessor.MemberType, dbType); } else { typeHandler = dataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } } else // If we specify a CLR type, use it { Type type = TypeUtils.ResolveType(CLRType); if (dataExchangeFactory.TypeHandlerFactory.IsSimpleType(type)) { // Primitive typeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } else { // .NET object type = ObjectProbe.GetMemberTypeForGetter(parameterClass, PropertyName); typeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } } } #endregion TypeHandler }
/// <summary> /// Initializes the parameter property /// </summary> /// <param name="scope">The scope.</param> /// <param name="parameterClass">The parameter class.</param> public void Initialize(IScope scope, Type parameterClass) { if (_directionAttribute.Length > 0) { _direction = (ParameterDirection)Enum.Parse(typeof(ParameterDirection), _directionAttribute, true); } if (!typeof(IDictionary).IsAssignableFrom(parameterClass) && // Hashtable parameter map parameterClass != null && // value property !scope.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(parameterClass)) // value property { if (!_isComplexMemberName) { IGetAccessorFactory getAccessorFactory = scope.DataExchangeFactory.AccessorFactory.GetAccessorFactory; _getAccessor = getAccessorFactory.CreateGetAccessor(parameterClass, _propertyName); } else // complex member name FavouriteLineItem.Id { string memberName = _propertyName.Substring(_propertyName.LastIndexOf('.') + 1); string parentName = _propertyName.Substring(0, _propertyName.LastIndexOf('.')); Type parentType = ObjectProbe.GetMemberTypeForGetter(parameterClass, parentName); IGetAccessorFactory getAccessorFactory = scope.DataExchangeFactory.AccessorFactory.GetAccessorFactory; _getAccessor = getAccessorFactory.CreateGetAccessor(parentType, memberName); } } scope.ErrorContext.MoreInfo = "Check the parameter mapping typeHandler attribute '" + this.CallBackName + "' (must be a ITypeHandlerCallback implementation)."; if (this.CallBackName.Length > 0) { try { Type type = scope.DataExchangeFactory.TypeHandlerFactory.GetType(this.CallBackName); ITypeHandlerCallback typeHandlerCallback = (ITypeHandlerCallback)Activator.CreateInstance(type); _typeHandler = new CustomTypeHandler(typeHandlerCallback); } catch (Exception e) { throw new ConfigurationException("Error occurred during custom type handler configuration. Cause: " + e.Message, e); } } else { if (this.CLRType.Length == 0) // Unknown { if (_getAccessor != null && scope.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(_getAccessor.MemberType)) { // Primitive _typeHandler = scope.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(_getAccessor.MemberType, _dbType); } else { _typeHandler = scope.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } } else // If we specify a CLR type, use it { Type type = TypeUtils.ResolveType(this.CLRType); if (scope.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(type)) { // Primitive _typeHandler = scope.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, _dbType); } else { // .NET object type = ObjectProbe.GetMemberTypeForGetter(type, this.PropertyName); _typeHandler = scope.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, _dbType); } } } }
/// <summary> /// Resolves the type handler. /// </summary> /// <param name="clazz">The clazz.</param> /// <param name="memberName">Name of the member.</param> /// <param name="clrType">Type of the CLR.</param> /// <param name="dbType">Type of the db.</param> /// <param name="forSetter">if set to <c>true</c> [for setter].</param> /// <returns></returns> public ITypeHandler ResolveTypeHandler(Type clazz, string memberName, string clrType, string dbType, bool forSetter) { ITypeHandler handler = null; if (clazz == null) { handler = this.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else if (typeof(IDictionary).IsAssignableFrom(clazz)) { // IDictionary if (clrType == null || clrType.Length == 0) { handler = this.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else { try { Type type = TypeUtils.ResolveType(clrType); handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } catch (Exception e) { #if dotnet2 throw new ConfigurationErrorsException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #else throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #endif } } } else if (this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(clazz, dbType) != null) { // Primitive handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(clazz, dbType); } else { // .NET object if (clrType == null || clrType.Length == 0) { Type type = null; if (forSetter) { type = ObjectProbe.GetMemberTypeForSetter(clazz, memberName); } else { type = ObjectProbe.GetMemberTypeForGetter(clazz, memberName); } handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } else { try { Type type = TypeUtils.ResolveType(clrType); handler = this.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } catch (Exception e) { #if dotnet2 throw new ConfigurationErrorsException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #else throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); #endif } } } return(handler); }
/// <summary> /// Resolves the type handler for Get/Set members. /// </summary> /// <param name="classType">Type of the class.</param> /// <param name="memberName">Name of the member.</param> /// <param name="memberType">Type of the member.</param> /// <param name="dbType">Type of the db.</param> /// <param name="forSetter">if set to <c>true</c> [for setter].</param> /// <returns></returns> public ITypeHandler ResolveTypeHandler( Type classType, string memberName, string memberType, string dbType, bool forSetter) { ITypeHandler handler = null; if (classType == null) { handler = GetUnkownTypeHandler(); } else if (typeof(IDictionary).IsAssignableFrom(classType) || typeof(DataRow).IsAssignableFrom(classType)) { // IDictionary or DataTable if (string.IsNullOrEmpty(memberType)) { handler = GetUnkownTypeHandler(); } else { try { Type type = TypeUtils.ResolveType(memberType); handler = GetTypeHandler(type, dbType); } catch (Exception e) { throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); } } } else if (GetTypeHandler(classType, dbType) != null) { // Primitive handler = GetTypeHandler(classType, dbType); } else { // .NET object if (string.IsNullOrEmpty(memberType)) { Type type = null; if (forSetter) { type = ObjectProbe.GetMemberTypeForSetter(classType, memberName); } else { type = ObjectProbe.GetMemberTypeForGetter(classType, memberName); } handler = GetTypeHandler(type, dbType); } else { try { Type type = TypeUtils.ResolveType(memberType); handler = GetTypeHandler(type, dbType); } catch (Exception e) { throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); } } } return(handler); }