/// <summary> /// Initializes a new instance of the <see cref="DynamicSql"/> class. /// </summary> /// <param name="configScope">The config scope.</param> /// <param name="statement">The statement.</param> internal DynamicSql(ConfigurationScope configScope, IStatement statement) { _statement = statement; _usePositionalParameters = configScope.DataSource.DbProvider.UsePositionalParameters; _dataExchangeFactory = configScope.DataExchangeFactory; }
/// <summary> /// /// </summary> /// <param name="configurationScope">The scope of the configuration</param> override internal void Initialize(ConfigurationScope configurationScope) { base.Initialize( configurationScope ); if (this.ParameterMap == null) { //throw new ConfigurationException("The parameterMap attribute is required in the procedure tag named '"+ this.Id +"'."); this.ParameterMap = configurationScope.SqlMapper.GetParameterMap(ConfigurationScope.EMPTY_PARAMETER_MAP); } }
/// <summary> /// Initialize the PropertyInfo of the result property. /// </summary> /// <param name="resultClass"></param> /// <param name="configScope"></param> public void Initialize( ConfigurationScope configScope, Type resultClass ) { if ( _propertyName.Length>0 && _propertyName != "value" && !typeof(IDictionary).IsAssignableFrom(resultClass) ) { if (!_isComplexMemberName) { _setAccessor = configScope.DataExchangeFactory.AccessorFactory.SetAccessorFactory.CreateSetAccessor(resultClass, _propertyName); } else // complex member name FavouriteLineItem.Id { MemberInfo propertyInfo = ObjectProbe.GetMemberInfoForSetter(resultClass, _propertyName); string memberName = _propertyName.Substring( _propertyName.LastIndexOf('.')+1); _setAccessor = configScope.DataExchangeFactory.AccessorFactory.SetAccessorFactory.CreateSetAccessor(propertyInfo.ReflectedType, memberName); } _isGenericIList = TypeUtils.IsImplementGenericIListInterface(this.MemberType); _isIList = typeof(IList).IsAssignableFrom(this.MemberType); // set the list factory if (_isGenericIList) { if (this.MemberType.IsArray) { _listFactory = _arrayListFactory; } else { Type[] typeArgs = this.MemberType.GetGenericArguments(); if (typeArgs.Length == 0)// Custom collection which derive from List<T> { _listFactory = configScope.DataExchangeFactory.ObjectFactory.CreateFactory(this.MemberType, Type.EmptyTypes); } else { Type genericIList = typeof(IList<>); Type interfaceListType = genericIList.MakeGenericType(typeArgs); Type genericList = typeof(List<>); Type listType = genericList.MakeGenericType(typeArgs); if ((interfaceListType == this.MemberType) || (listType == this.MemberType)) { Type constructedType = genericList.MakeGenericType(typeArgs); _listFactory = configScope.DataExchangeFactory.ObjectFactory.CreateFactory( constructedType, Type.EmptyTypes); } else // Custom collection which derive from List<T> { _listFactory = configScope.DataExchangeFactory.ObjectFactory.CreateFactory(this.MemberType, Type.EmptyTypes); } } } } else if (_isIList) { if (this.MemberType.IsArray) { _listFactory = _arrayListFactory; } else { if (this.MemberType == typeof(IList)) { _listFactory = _arrayListFactory; } else // custom collection { _listFactory = configScope.DataExchangeFactory.ObjectFactory.CreateFactory(this.MemberType, Type.EmptyTypes); } } } } if (this.CallBackName!=null && this.CallBackName.Length >0) { configScope.ErrorContext.MoreInfo = "Result property '"+_propertyName+"' check the typeHandler attribute '" + this.CallBackName + "' (must be a ITypeHandlerCallback implementation)."; try { Type type = configScope.SqlMapper.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 { configScope.ErrorContext.MoreInfo = "Result property '"+_propertyName+"' set the typeHandler attribute."; _typeHandler = configScope.ResolveTypeHandler( resultClass, _propertyName, _clrType, _dbType, true); } if (this.IsLazyLoad) { _lazyFactory = new LazyFactoryBuilder().GetLazyFactory(_setAccessor.MemberType); } }
/// <summary> /// /// </summary> /// <param name="configurationScope">The scope of the configuration</param> override internal void Initialize(ConfigurationScope configurationScope) { // the propertyName attribute on the selectKey node is optional if (PropertyName.Length > 0) { // Id is equal to the parent <select> node's "id" attribute IMappedStatement insert = configurationScope.SqlMapper.GetMappedStatement(Id); Type insertParameterClass = insert.Statement.ParameterClass; // make sure the PropertyName is a valid settable property of the <insert> node's parameterClass if (insertParameterClass != null && ObjectProbe.IsSimpleType(insertParameterClass) == false) { configurationScope.ErrorContext.MoreInfo = String.Format("Looking for settable property named '{0}' on type '{1}' for selectKey node of statement id '{2}'.", PropertyName, // 0 insert.Statement.ParameterClass.Name, // 1 Id); // 2 // we expect this to throw an exception if the property cannot be found; GetSetter is // called instead of HasWriteableProperty becuase we want the same wording for // property not found exceptions; GetSetter and HasWritableProperty both use the // same internal cache for looking up the ProperyInfo object ReflectionInfo.GetInstance(insert.Statement.ParameterClass).GetSetter(PropertyName); } } base.Initialize(configurationScope); }
/// <summary> /// Get the result properties and the subMap properties. /// </summary> /// <param name="configScope"></param> private void GetChildNode(ConfigurationScope configScope) { ResultProperty mapping = null; SubMap subMap = null; #region Load the parameters constructor XmlNodeList nodeList = configScope.NodeContext.SelectNodes( DomSqlMapBuilder.ApplyMappingNamespacePrefix(XML_CONSTRUCTOR_ARGUMENT), configScope.XmlNamespaceManager); if (nodeList.Count>0) { Type[] parametersType= new Type[nodeList.Count]; string[] parametersName = new string[nodeList.Count]; for( int i =0; i<nodeList.Count; i++) { ArgumentProperty argumentMapping = ArgumentPropertyDeSerializer.Deserialize( nodeList[i], configScope ); _parameters.Add( argumentMapping ); parametersName[i] = argumentMapping.ArgumentName; } ConstructorInfo constructorInfo = this.GetConstructor( _class, parametersName ); for(int i=0;i<_parameters.Count;i++) { ArgumentProperty argumentMapping = (ArgumentProperty)_parameters[i]; configScope.ErrorContext.MoreInfo = "initialize argument property : " + argumentMapping.ArgumentName; argumentMapping.Initialize( configScope, constructorInfo); parametersType[i] = argumentMapping.MemberType; } // Init the object factory _objectFactory = configScope.SqlMapper.ObjectFactory.CreateFactory(_class, parametersType); } else { if (Type.GetTypeCode(_class) == TypeCode.Object) { _objectFactory = configScope.SqlMapper.ObjectFactory.CreateFactory(_class, Type.EmptyTypes); } } #endregion #region Load the Result Properties foreach ( XmlNode resultNode in configScope.NodeContext.SelectNodes( DomSqlMapBuilder.ApplyMappingNamespacePrefix(XML_RESULT), configScope.XmlNamespaceManager) ) { mapping = ResultPropertyDeSerializer.Deserialize( resultNode, configScope ); configScope.ErrorContext.MoreInfo = "initialize result property: "+mapping.PropertyName; mapping.Initialize( configScope, _class ); _properties.Add( mapping ); } #endregion #region Load the Discriminator Property XmlNode discriminatorNode = configScope.NodeContext.SelectSingleNode(DomSqlMapBuilder.ApplyMappingNamespacePrefix(XML_DISCRIMNATOR), configScope.XmlNamespaceManager); if (discriminatorNode != null) { configScope.ErrorContext.MoreInfo = "initialize discriminator"; this.Discriminator = DiscriminatorDeSerializer.Deserialize(discriminatorNode, configScope); this.Discriminator.SetMapping( configScope, _class ); } #endregion #region Load the SubMap Properties if (configScope.NodeContext.SelectNodes(DomSqlMapBuilder.ApplyMappingNamespacePrefix(XML_SUBMAP), configScope.XmlNamespaceManager).Count>0 && this.Discriminator==null) { throw new ConfigurationException("The discriminator is null, but somehow a subMap was reached. This is a bug."); } foreach ( XmlNode resultNode in configScope.NodeContext.SelectNodes(DomSqlMapBuilder.ApplyMappingNamespacePrefix(XML_SUBMAP), configScope.XmlNamespaceManager) ) { configScope.ErrorContext.MoreInfo = "initialize subMap"; subMap = SubMapDeSerializer.Deserialize(resultNode, configScope); this.Discriminator.Add( subMap ); } #endregion }
/// <summary> /// Initialize the resultMap from an xmlNode.. /// </summary> /// <param name="configScope"></param> public void Initialize( ConfigurationScope configScope ) { try { _class = configScope.SqlMapper.TypeHandlerFactory.GetType(_className); _dataExchange = _dataExchangeFactory.GetDataExchangeForClass(_class); // Load the child node GetChildNode(configScope); // Verify that that each groupBy element correspond to a class member // of one of result property for (int i = 0; i < _groupByProperties.Count; i++) { string memberName = GroupByPropertyNames[i]; if (!_properties.Contains(memberName)) { throw new ConfigurationException( string.Format( "Could not configure ResultMap named \"{0}\". Check the groupBy attribute. Cause: there's no result property named \"{1}\".", _id, memberName)); } } } catch(Exception e) { throw new ConfigurationException( string.Format("Could not configure ResultMap named \"{0}\", Cause: {1}", _id, e.Message) , e); } }
/// <summary> /// Initializes a new instance of the <see cref="ResultMap"/> class. /// </summary> /// <param name="configScope">The config scope.</param> /// <param name="className">The output class name of the resultMap.</param> /// <param name="extendMap">The extend result map bame.</param> /// <param name="id">Identifier used to identify the resultMap amongst the others.</param> /// <param name="groupBy">The groupBy properties</param> public ResultMap(ConfigurationScope configScope, string id, string className, string extendMap, string groupBy) { _nullResultMap = new NullResultMap(); _dataExchangeFactory = configScope.DataExchangeFactory; _sqlMapNameSpace = configScope.SqlMapNamespace; if ((id == null) || (id.Length < 1)) { throw new ArgumentNullException("The id attribute is mandatory in a ResultMap tag."); } _id = configScope.ApplyNamespace(id); if ((className == null) || (className.Length < 1)) { throw new ArgumentNullException("The class attribute is mandatory in the ResultMap tag id:"+_id); } _className = className; _extendMap = extendMap; if (groupBy != null && groupBy.Length>0) { string[] groupByProperties = groupBy.Split(','); for (int i = 0; i < groupByProperties.Length; i++) { string memberName = groupByProperties[i].Trim(); _groupByPropertyNames.Add(memberName); } } }
/// <summary> /// /// </summary> /// <param name="configScope"></param> /// <param name="argumenType">The argument type</param> /// <param name="clrType"></param> /// <param name="dbType"></param> /// <returns></returns> public ITypeHandler ResolveTypeHandler(ConfigurationScope configScope, Type argumenType, string clrType, string dbType) { ITypeHandler handler = null; if (argumenType==null) { handler = configScope.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else if (typeof(IDictionary).IsAssignableFrom(argumenType)) { // IDictionary if (clrType ==null ||clrType.Length == 0) { handler = configScope.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else { try { Type type = TypeUtils.ResolveType(clrType); handler = configScope.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } catch (Exception e) { throw new CommonExceptions.ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); } } } else if (configScope.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(argumenType, dbType) != null) { // Primitive handler = configScope.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(argumenType, dbType); } else { // .NET object if (clrType ==null || clrType.Length == 0) { handler = configScope.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else { try { Type type = TypeUtils.ResolveType(clrType); handler = configScope.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } catch (Exception e) { throw new CommonExceptions.ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e); } } } return handler; }
/// <summary> /// Initialize the argument property. /// </summary> /// <param name="constructorInfo"></param> /// <param name="configScope"></param> public void Initialize( ConfigurationScope configScope, ConstructorInfo constructorInfo ) { // Search argument by his name to set his type ParameterInfo[] parameters = constructorInfo.GetParameters(); bool found = false; for(int i =0; i< parameters.Length; i++) { found = (parameters[ i ].Name == _argumentName); if( found ) { _argumentType = parameters[ i ].ParameterType; break; } } if (this.CallBackName!=null && this.CallBackName.Length >0) { configScope.ErrorContext.MoreInfo = "Argument property ("+_argumentName+"), check the typeHandler attribute '" + this.CallBackName + "' (must be a ITypeHandlerCallback implementation)."; try { Type type = configScope.SqlMapper.TypeHandlerFactory.GetType(this.CallBackName); ITypeHandlerCallback typeHandlerCallback = (ITypeHandlerCallback) Activator.CreateInstance( type ); this.TypeHandler = new CustomTypeHandler(typeHandlerCallback); } catch (Exception e) { throw new CommonExceptions.ConfigurationException("Error occurred during custom type handler configuration. Cause: " + e.Message, e); } } else { configScope.ErrorContext.MoreInfo = "Argument property ("+_argumentName+") set the typeHandler attribute."; this.TypeHandler = this.ResolveTypeHandler(configScope, _argumentType, this.CLRType, this.DbType); } }
/// <summary> /// Initialize the Discriminator /// </summary> /// <param name="configScope"></param> public void Initialize(ConfigurationScope configScope) { // Set the ResultMaps int count = _subMaps.Count; for(int index=0; index<count; index++) { SubMap subMap = _subMaps[index] as SubMap; _resultMaps.Add(subMap.DiscriminatorValue, configScope.SqlMapper.GetResultMap( subMap.ResultMapName ) ); } }
/// <summary> /// Initilaize the underlying mapping /// </summary> /// <param name="configScope"></param> /// <param name="resultClass"></param> public void SetMapping(ConfigurationScope configScope, Type resultClass) { configScope.ErrorContext.MoreInfo = "Initialize discriminator mapping"; _mapping = new ResultProperty(); _mapping.ColumnName = _columnName; _mapping.ColumnIndex = _columnIndex; _mapping.CLRType = _clrType; _mapping.CallBackName = _callBackName; _mapping.DbType = _dbType; _mapping.NullValue = _nullValue; _mapping.Initialize( configScope, resultClass ); }