///<summary>
        /// Sets value of the specified <see cref="ResultProperty"/> on the target object
        /// when a 'select' attribute exists and fills an <see cref="IList"/> property
        /// on the <see cref="ResultProperty"/> are empties.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultMap">The result map.</param>
        /// <param name="mapping">The ResultProperty.</param>
        /// <param name="target">The target.</param>
        /// <param name="reader">The current <see cref="IDataReader"/></param>
        /// <param name="keys">The keys</param>
        public void Set(RequestScope request, IResultMap resultMap,
            ResultProperty mapping, ref object target, IDataReader reader, object keys)
        {
            // Get the select statement
            IMappedStatement selectStatement = request.MappedStatement.ModelStore.GetMappedStatement(mapping.Select);

            PostBindind postSelect = new PostBindind();
            postSelect.Statement = selectStatement;
            postSelect.Keys = keys;
            postSelect.Target = target;
            postSelect.ResultProperty = mapping;

            if (mapping.IsLazyLoad)
            {
                object values = mapping.LazyFactory.CreateProxy(request.MappedStatement.ModelStore.DataMapper, selectStatement, keys, target, mapping.SetAccessor);
                //为target类的mapping属性设置值为values
                mapping.Set(target, values);
            }
            else
            {
                if (mapping.SetAccessor.MemberType.GetGenericTypeDefinition() == typeof(IList<>))
                {
                    postSelect.Method = PostBindind.ExecuteMethod.ExecuteQueryForGenericIList;
                }
                //将postSelect类对象添加到队列中
                request.DelayedLoad.Enqueue(postSelect);
            }

        }
        /// <summary>
        /// Gets the value of the specified <see cref="ResultProperty"/> that must be set on the target object.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultMap">The result map.</param>
        /// <param name="mapping">The mapping.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="target">The target object</param>
        public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
        {
            if (mapping.TypeHandler == null ||mapping.TypeHandler is UnknownTypeHandler) // Find the TypeHandler
            {
                lock (mapping)
                {
                    if (mapping.TypeHandler == null || mapping.TypeHandler is UnknownTypeHandler)
                    {
                        int columnIndex = 0;
                        if (mapping.ColumnIndex == ResultProperty.UNKNOWN_COLUMN_INDEX)
                        {
                            columnIndex = reader.GetOrdinal(mapping.ColumnName);
                        }
                        else
                        {
                            columnIndex = mapping.ColumnIndex;
                        }
                        Type systemType = ((IDataRecord)reader).GetFieldType(columnIndex);

                        mapping.TypeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(systemType);
                    }
                }
            }

            object dataBaseValue = mapping.GetDataBaseValue(reader);
            request.IsRowDataFound = request.IsRowDataFound || (dataBaseValue != null);
            return dataBaseValue;
        }
        ///<summary>
        /// Sets value of the specified <see cref="ResultProperty"/> on the target object
        /// when a 'select' attribute exists and fills an <see cref="IList"/> property
        /// on the <see cref="ResultProperty"/> are empties.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultMap">The result map.</param>
        /// <param name="mapping">The ResultProperty.</param>
        /// <param name="target">The target.</param>
        /// <param name="reader">The current <see cref="IDataReader"/></param>
        /// <param name="keys">The keys</param>
        public void Set(RequestScope request, IResultMap resultMap, 
            ResultProperty mapping, ref object target, IDataReader reader, object keys)
        {
            // Get the select statement
            IMappedStatement selectStatement = request.MappedStatement.SqlMap.GetMappedStatement(mapping.Select);

            PostBindind postSelect = new PostBindind();
            postSelect.Statement = selectStatement;
            postSelect.Keys = keys;
            postSelect.Target = target;
            postSelect.ResultProperty = mapping;

            if (mapping.IsLazyLoad)
            {
                object values = mapping.LazyFactory.CreateProxy(selectStatement, keys, target, mapping.SetAccessor);
                mapping.SetAccessor.Set(target, values);
            }
            else
            {
                if (mapping.SetAccessor.MemberType == typeof(IList))
                {
                    postSelect.Method = PostBindind.ExecuteMethod.ExecuteQueryForIList;
                }
                else
                {
                    postSelect.Method = PostBindind.ExecuteMethod.ExecuteQueryForStrongTypedIList;
                }
                request.QueueSelect.Enqueue(postSelect);
            }
        }
		///<summary>
		/// Sets value of the specified <see cref="ResultProperty"/> on the target object
		/// when a 'select' attribute exists and fills an object property.
		/// on the <see cref="ResultProperty"/> are empties.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <param name="resultMap">The result map.</param>
		/// <param name="mapping">The ResultProperty.</param>
		/// <param name="target">The target.</param>
		/// <param name="reader">The current <see cref="IDataReader"/></param>
		/// <param name="keys">The keys</param>
		public void Set(RequestScope request, IResultMap resultMap, 
			ResultProperty mapping, ref object target, IDataReader reader, object keys)
		{
			// Get the select statement
			IMappedStatement selectStatement = request.MappedStatement.ModelStore.GetMappedStatement(mapping.Select);

			PostBindind postSelect = new PostBindind();
			postSelect.Statement = selectStatement;
			postSelect.Keys = keys;
			postSelect.Target = target;
			postSelect.ResultProperty = mapping;

			if (mapping.IsLazyLoad)
			{
				object values = mapping.LazyFactory.CreateProxy(
                    request.MappedStatement.ModelStore.DataMapper,
                    selectStatement, keys, target, mapping.SetAccessor);
				mapping.Set(target, values);
			}
			else
			{
				postSelect.Method = PostBindind.ExecuteMethod.ExecuteQueryForObject;
				request.DelayedLoad.Enqueue(postSelect);
			}
		}
		/// <summary>
		/// Sets value of the specified <see cref="ResultProperty"/> on the target object
		/// when a 'resultMapping' attribute exists
		/// on the <see cref="ResultProperty"/>.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <param name="resultMap">The result map.</param>
		/// <param name="mapping">The ResultProperty.</param>
		/// <param name="target">The target.</param>
		/// <param name="reader">The reader.</param>
		/// <param name="keys">The keys</param>
		public void Set(RequestScope request, IResultMap resultMap, 
			ResultProperty mapping, ref object target, IDataReader reader, object keys)
		{
            object obj = Get(request, resultMap, mapping, ref target, reader);
			// Sets created object on the property
			resultMap.SetValueOfProperty( ref target, mapping, obj );		
		}
        /// <summary>
        /// Gets the value of the specified <see cref="ResultProperty"/> that must be set on the target object.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultMap">The result map.</param>
        /// <param name="mapping">The mapping.</param>
        /// <param name="target">The target.</param>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
        {
            object result = null;

            IResultMap propertyRresultMap = mapping.NestedResultMap.ResolveSubMap(reader);

            string circularKey = GetCircularKey(propertyRresultMap, reader);
            // Gets the [key, result object] already build
            IDictionary<string, object> buildObjects = request.GetCirularKeys(propertyRresultMap);

            if (buildObjects != null && buildObjects.ContainsKey(circularKey))
            {
                // circular key is already known, so get the existing result object
                result = buildObjects[circularKey];
            }
            else if (circularKey == null || buildObjects == null || !buildObjects.ContainsKey(circularKey))
            {
                // circular key is NOT known, so create a new result object.
                result = resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);

                if (buildObjects == null)
                {
                    buildObjects = new Dictionary<string, object>();
                    request.SetCirularKeys(propertyRresultMap, buildObjects);
                }
                buildObjects[circularKey] = result;
            }

            return result;
        }
Beispiel #7
0
	    /// <summary>
        /// Calculte a unique key which identify the resukt object build by this <see cref="IResultMap"/>
	    /// </summary>
	    /// <param name="resultMap"></param>
	    /// <param name="reader"></param>
	    /// <returns></returns>
        protected static string GetUniqueKey(IResultMap resultMap, IDataReader reader)
        {
            if (resultMap.GroupByProperties.Count > 0)
            {
                StringBuilder keyBuffer = new StringBuilder();

                for (int i = 0; i < resultMap.GroupByProperties.Count; i++)
                {
                    ResultProperty resultProperty = resultMap.GroupByProperties[i];
                    //获取分组名对应的值
                    keyBuffer.Append(resultProperty.GetDataBaseValue(reader));
                    keyBuffer.Append('-');
                }

                if (keyBuffer.Length < 1)
                {
                    // we should never go here
                    return null;
                }
                // separator value not likely to appear in a database
                keyBuffer.Append(KEY_SEPARATOR);
                return keyBuffer.ToString();
            }
	        // we should never go here
	        return null;
        }
Beispiel #8
0
		///<summary>
		/// Sets value of the specified <see cref="ResultProperty"/> on the target object
		/// when the 'select' and 'resultMap' attributes 
		/// on the <see cref="ResultProperty"/> are empties.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <param name="resultMap">The result map.</param>
		/// <param name="mapping">The ResultProperty.</param>
		/// <param name="target">The target.</param>
		/// <param name="reader">The reader.</param>
		/// <param name="keys">The keys</param>
		public void Set(RequestScope request, IResultMap resultMap, 
			ResultProperty mapping, ref object target, IDataReader reader, object keys)
		{
            //获取属性mapping的值
            object obj = Get(request, resultMap, mapping, ref target, reader);
            //将mapping的值放到类中对应的属性里
            resultMap.SetValueOfProperty(ref target, mapping, obj);
		}
Beispiel #9
0
 /// <summary>
 /// Gets the circular keys.
 /// </summary>
 /// <param name="map">The ResultMap.</param>
 /// <returns>
 /// Returns [string key, object result] which holds the result objects that have
 /// already been build during this request with this <see cref="IResultMap"/>
 /// </returns>
 public IDictionary<string, object> GetCirularKeys(IResultMap map)
 {
     if (cirularKeys == null)
     {
         return null;
     }
     IDictionary<string, object> keys = null;
     cirularKeys.TryGetValue(map, out keys);
     return keys;
 }
		///<summary>
		/// Sets value of the specified <see cref="ResultProperty"/> on the target object
		/// when a 'select' attribute exists and fills an Array property
		/// on the <see cref="ResultProperty"/> are empties.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <param name="resultMap">The result map.</param>
		/// <param name="mapping">The ResultProperty.</param>
		/// <param name="target">The target.</param>
		/// <param name="reader">The <see cref="IDataReader"/></param>
		/// <param name="keys">The keys</param>
		public void Set(RequestScope request, IResultMap resultMap, 
			ResultProperty mapping, ref object target, IDataReader reader, object keys)
		{
			// Get the select statement
            IMappedStatement selectStatement = request.MappedStatement.ModelStore.GetMappedStatement(mapping.Select);

			PostBindind postSelect = new PostBindind();
			postSelect.Statement = selectStatement;
			postSelect.Keys = keys;
			postSelect.Target = target;
			postSelect.ResultProperty = mapping;
		
			if (mapping.IsLazyLoad)
			{
				throw new NotImplementedException("Lazy load no supported for System.Array property:" + mapping.SetAccessor.Name);
			}
			postSelect.Method = PostBindind.ExecuteMethod.ExecuteQueryForArrayList;
			request.DelayedLoad.Enqueue(postSelect);
		}
	    /// <summary>
        /// Gets the value of the specified <see cref="ResultProperty"/> that must be set on the target object.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultMap">The result map.</param>
        /// <param name="mapping">The mapping.</param>
        /// <param name="reader">The reader.</param>
		/// <param name="target">The target object</param>
		public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
        {
            object[] parameters = null;
            bool isParameterFound = false;

            IResultMap resultMapping = mapping.NestedResultMap.ResolveSubMap(reader);

            if (resultMapping.Parameters.Count > 0)
            {
                parameters = new object[resultMapping.Parameters.Count];
                // Fill parameters array
                for (int index = 0; index < resultMapping.Parameters.Count; index++)
                {
                    ResultProperty resultProperty = resultMapping.Parameters[index];
                    parameters[index] = resultProperty.GetValue(request, ref reader, null);
                    request.IsRowDataFound = request.IsRowDataFound || (parameters[index] != null);
                    isParameterFound = isParameterFound || (parameters[index] != null);
                }
            }

            object obj = null;
            // If I have a constructor tag and all argumments values are null, the obj is null
            if (resultMapping.Parameters.Count > 0 && isParameterFound == false)
            {
                obj = null;
            }
            else
            {
                obj = resultMapping.CreateInstanceOfResult(parameters);

                // Fills properties on the new object
                if (FillObjectWithReaderAndResultMap(request, reader, resultMapping, ref obj) == false)
                {
                    obj = null;
                }
            }
	        
	        return obj;
        }
        /// <summary>
        /// Fills the object with reader and result map.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultMap">The result map.</param>
        /// <param name="resultObject">The result object.</param>
        /// <returns>Indicates if we have found a row.</returns>
        protected bool FillObjectWithReaderAndResultMap(RequestScope request,IDataReader reader,
                                                        IResultMap resultMap, ref object resultObject)
        {
            bool dataFound = false;

            if (resultMap.Properties.Count>0)
            {
             			    // For each Property in the ResultMap, set the property in the object
                for(int index=0; index< resultMap.Properties.Count; index++)
                {
                    request.IsRowDataFound = false;
                    ResultProperty property = resultMap.Properties[index];
                    property.PropertyStrategy.Set(request, resultMap, property, ref resultObject, reader, null);
                    dataFound = dataFound || request.IsRowDataFound;
                }

                request.IsRowDataFound = dataFound;
                return dataFound;
            }
            else
            {
                return true;
            }
        }
Beispiel #13
0
 /// <summary>
 /// Sets the cirular keys.
 /// </summary>
 /// <param name="map">The map.</param>
 /// <param name="keys">The keys.</param>
 public void SetCirularKeys(IResultMap map, IDictionary<string, object> keys)
 {
     if (cirularKeys == null)
     {
         cirularKeys = new Dictionary<IResultMap, IDictionary<string, object>>();
     }
     cirularKeys.Add(map, keys);
 }
Beispiel #14
0
 /// <summary>
 /// Gets the unique keys.
 /// </summary>
 /// <param name="map">The ResultMap.</param>
 /// <returns>
 /// Returns [key, result object] which holds the result objects that have  
 /// already been build during this request with this <see cref="IResultMap"/>
 /// </returns>
 public IDictionary GetUniqueKeys(IResultMap map)
 {
     if (_uniqueKeys == null)
     {
         return null;
     }
     return (IDictionary)_uniqueKeys[map];
 }
Beispiel #15
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ResultMap"/> class.
        /// </summary>
        /// <param name="id">Identifier used to identify the resultMap amongst the others.</param>
        /// <param name="className">The output class name of the resultMap.</param>
        /// <param name="extendMap">The extend result map bame.</param>
        /// <param name="groupBy">The groupBy properties</param>
        /// <param name="keyColumns">The key columns.</param>
        /// <param name="type">The result type.</param>
        /// <param name="dataExchange">The data exchange.</param>
        /// <param name="objectFactory">The object factory.</param>
        /// <param name="typeHandlerFactory">The type handler factory.</param>
        /// <param name="properties">The properties.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="discriminator">The discriminator.</param>
        public ResultMap(
            string id, 
            string className, 
            string extendMap, 
            string groupBy,
            string keyColumns,
            Type type,
            IDataExchange dataExchange,
            IFactory objectFactory,
            TypeHandlerFactory typeHandlerFactory,
            ResultPropertyCollection properties,
            ArgumentPropertyCollection parameters,
            Discriminator discriminator)
        {
            Contract.Require.That(id, Is.Not.Null & Is.Not.Empty).When("retrieving argument id in ResultMap constructor");
            Contract.Require.That(className, Is.Not.Null & Is.Not.Empty).When("retrieving argument className in ResultMap constructor");
            Contract.Require.That(type, Is.Not.Null).When("retrieving argument type in ResultMap constructor");
            Contract.Require.That(typeHandlerFactory, Is.Not.Null).When("retrieving argument typeHandlerFactory in ResultMap constructor");

            nullResultMap = new NullResultMap();

            this.id = id;
            this.className = className;
            this.extendMap = extendMap;
            this.type = type;
            this.dataExchange = dataExchange;
            this.properties = properties;
            this.parameters = parameters;
            this.discriminator = discriminator;
            this.objectFactory = objectFactory;
            isSimpleType = typeHandlerFactory.IsSimpleType(type);

            if (!string.IsNullOrEmpty(groupBy))
            {
                string[] props = groupBy.Split(',');
                for (int i = 0; i < props.Length; i++)
                {
                    string memberName = props[i].Trim();
                    groupByPropertyNames.Add(memberName);
                }

                InitializeGroupByProperties();
                CheckGroupBy();
            }

            if (!string.IsNullOrEmpty(keyColumns))
            {
                string[] columns = keyColumns.Split(',');
                for (int i = 0; i < columns.Length; i++)
                {
                    string column = columns[i].Trim();
                    keyPropertyNames.Add(column);
                }

                InitializeKeysProperties();
                CheckKeysProperties();
            }

       }
Beispiel #16
0
 /// <summary>
 /// Sets the unique keys.
 /// </summary>
 /// <param name="map">The map.</param>
 /// <param name="keys">The keys.</param>
 public void SetUniqueKeys(IResultMap map, IDictionary keys)
 {
     if (_uniqueKeys == null)
     {
         _uniqueKeys = new Hashtable();
     }
     _uniqueKeys.Add(map, keys);
 }
Beispiel #17
0
        public void Set(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader, object keys)
        {
            object dataBaseValue = this.Get(request, resultMap, mapping, ref target, reader);

            resultMap.SetValueOfProperty(ref target, mapping, dataBaseValue);
        }
 /// <summary>
 /// Remove a ResultMap of the collection.
 /// </summary>
 public void Remove(IResultMap value)
 {
     for (int i = 0; i < _count; i++)
     {
         if (_innerList[i].Id == value.Id)
         {
             RemoveAt(i);
             return;
         }
     }
 }
 /// <summary>
 /// Indicate if a ResultMap is in the collection
 /// </summary>
 /// <param name="value">A ResultMap</param>
 /// <returns>True fi is in</returns>
 public bool Contains(IResultMap value)
 {
     for (int i = 0; i < _count; i++)
     {
         if (_innerList[i].Id == value.Id)
         {
             return true;
         }
     }
     return false;
 }
        /// <summary>
        /// Add an ResultMap
        /// </summary>
        /// <param name="value"></param>
        /// <returns>Index</returns>
        public int Add(IResultMap value)
        {
            Resize(_count + 1);
            int index = _count++;
            _innerList[index] = value;

            return index;
        }
Beispiel #21
0
 /// <summary>
 /// Gets the unique keys, used to resolve groupBy
 /// </summary>
 /// <param name="map">The ResultMap.</param>
 /// <returns>
 /// Returns [string key, object result] which holds the result objects that have  
 /// already been build during this request with this <see cref="IResultMap"/>
 /// </returns>
 public IDictionary<string, object> GetUniqueKeys(IResultMap map)
 {
     if (uniqueKeys == null)
     {
         return null;
     }
     IDictionary<string, object> keys = null;
     uniqueKeys.TryGetValue(map, out keys);
     return keys;
 }
Beispiel #22
0
 /// <summary>
 /// Sets the unique keys.
 /// </summary>
 /// <param name="map">The map.</param>
 /// <param name="keys">The keys.</param>
 public void SetUniqueKeys(IResultMap map, IDictionary<string, object> keys)
 {
     if (uniqueKeys == null)
     {
         uniqueKeys = new Dictionary<IResultMap, IDictionary<string, object>>();
     }
     uniqueKeys.Add(map, keys);
 }
Beispiel #23
0
 /// <summary>
 /// Sets value of the specified <see cref="ResultProperty"/> on the target object
 /// when a 'resultMapping' attribute exists
 /// on the <see cref="ResultProperty"/>.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="resultMap">The result map.</param>
 /// <param name="mapping">The ResultProperty.</param>
 /// <param name="target">The target.</param>
 /// <param name="reader">The reader.</param>
 /// <param name="keys">The keys</param>
 public void Set(RequestScope request, IResultMap resultMap,
     ResultProperty mapping, ref object target, IDataReader reader, object keys)
 {
     Get(request, resultMap, mapping, ref target, reader);
 }
Beispiel #24
0
        /// <summary>
        /// Gets the value of the specified <see cref="ResultProperty"/> that must be set on the target object.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultMap">The result map.</param>
        /// <param name="mapping">The mapping.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="target">The target object</param>
        public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
        {
            // The property is a IList
            IList list = null;

            // Get the IList property
            object property = ObjectProbe.GetMemberValue(target, mapping.PropertyName,
                                       request.DataExchangeFactory.AccessorFactory);

            if (property == null)// Create the list if need
            {
                property = mapping.ListFactory.CreateInstance(null);
                mapping.SetAccessor.Set(target, property);
            }
            list = (IList)property;

            object result = null;
            IResultMap propertyRresultMap = mapping.NestedResultMap.ResolveSubMap(reader);

            if (propertyRresultMap.GroupByProperties.Count > 0)
            {
                 string uniqueKey = GetUniqueKey(propertyRresultMap, request, reader);

                // Gets the [key, result object] already build
                IDictionary buildObjects = request.GetUniqueKeys(propertyRresultMap);

                if (buildObjects != null && buildObjects.Contains(uniqueKey))
                {
                    // Unique key is already known, so get the existing result object and process additional results.
                    result = buildObjects[uniqueKey];

                    //In some cases (nested groupings) our object may be null, so there is
                    //no point in going on
                    if (result != null)
                    {
                        // process resulMapping attribute which point to a groupBy attribute
                        for (int index = 0; index < propertyRresultMap.Properties.Count; index++)
                        {
                            ResultProperty resultProperty = propertyRresultMap.Properties[index];
                            if (resultProperty.PropertyStrategy is PropertStrategy.GroupByStrategy)
                            {
                                resultProperty.PropertyStrategy.Set(request, propertyRresultMap, resultProperty, ref result, reader, null);
                            }
                        }
                    }
                    result = SKIP;
                }
                else if (uniqueKey == null || buildObjects == null || !buildObjects.Contains(uniqueKey))
                {
                    // Unique key is NOT known, so create a new result object and then process additional results.
                    result = _resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);

                    if (buildObjects == null)
                    {
                        buildObjects = new Hashtable();
                        request.SetUniqueKeys(propertyRresultMap, buildObjects);
                    }
                    buildObjects[uniqueKey] = result;
                }
            }
            else // Last resultMap have no groupBy attribute
            {
                result = _resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);
            }

            if ((result != null) && (result != SKIP))
            {
                list.Add(result);
            }

            return result;
        }
Beispiel #25
0
        /// <summary>
        /// Processes the specified <see cref="IDataReader"/>.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        /// <returns></returns>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            IResultMap resultMap = request.CurrentResultMap.ResolveSubMap(reader);
            DataRow    dataRow   = (DataRow)resultObject;
            DataTable  dataTable = null;

            if (dataRow == null)
            {
                dataTable = new DataTable();
            }
            else
            {
                dataTable = dataRow.Table;
            }

            if (dataTable.Columns.Count == 0)
            {
                // Builds and adss the columns
                dataTable.TableName = resultMap.Id;
                if (resultMap is AutoResultMap)
                {
                    //自动生成表的元素列
                    for (int index = 0; index < reader.FieldCount; index++)
                    {
                        string columnName = reader.GetName(index);
                        Type   type       = reader.GetFieldType(index);

                        DataColumn column = new DataColumn();
                        column.DataType   = type;
                        column.ColumnName = columnName;
                        dataTable.Columns.Add(column);
                    }
                }
                else
                {
                    //根据resultMap.Properties的属性生成列元素
                    for (int index = 0; index < resultMap.Properties.Count; index++)
                    {
                        ResultProperty property = resultMap.Properties[index];
                        DataColumn     column   = new DataColumn();
                        if (property.CLRType.Length > 0)
                        {
                            column.DataType = request.DataExchangeFactory.TypeHandlerFactory.GetType(property.CLRType);
                        }
                        else
                        {
                            object value = property.GetDataBaseValue(reader);
                            if (value == null)
                            {
                                int columnIndex = reader.GetOrdinal(property.ColumnName);
                                column.DataType = reader.GetFieldType(columnIndex);
                            }
                            else
                            {
                                column.DataType = value.GetType();
                            }
                        }
                        column.ColumnName = property.PropertyName;

                        dataTable.Columns.Add(column);
                    }
                }
            }


            if (resultMap is AutoResultMap)
            {
                //将reader当前行中的所有字段加入到DataRow对象中
                for (int index = 0; index < reader.FieldCount; index++)
                {
                    string       propertyName = reader.GetName(index);
                    int          columnIndex  = index;
                    ITypeHandler typeHandler  = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(index));

                    ResultProperty property = new ResultProperty(
                        propertyName,
                        string.Empty,
                        columnIndex,
                        string.Empty,
                        string.Empty,
                        string.Empty,
                        false,
                        string.Empty,
                        null,
                        string.Empty,
                        typeof(DataRow),
                        request.DataExchangeFactory,
                        typeHandler);

                    object value = property.GetDataBaseValue(reader);
                    if (value == null)
                    {
                        value = DBNull.Value;
                    }
                    dataRow[property.PropertyName] = value;
                }
            }
            else
            {
                for (int index = 0; index < resultMap.Properties.Count; index++)
                {
                    ResultProperty property = resultMap.Properties[index];
                    object         value    = property.GetDataBaseValue(reader);
                    if (value == null)
                    {
                        value = DBNull.Value;
                    }
                    dataRow[property.PropertyName] = value;
                }
            }

            return(null);
        }
 /// <summary>
 /// Gets the value of the specified <see cref="ResultProperty"/> that must be set on the target object.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="resultMap">The result map.</param>
 /// <param name="mapping">The mapping.</param>
 /// <param name="reader">The reader.</param>
 /// <param name="target">The target object</param>
 public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
 {
     throw new NotSupportedException("Get method on ResultMapStrategy is not supported");
 }
Beispiel #27
0
 /// <summary>
 /// Sets value of the specified <see cref="ResultProperty"/> on the target object
 /// when a 'resultMapping' attribute exists
 /// on the <see cref="ResultProperty"/>.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="resultMap">The result map.</param>
 /// <param name="mapping">The ResultProperty.</param>
 /// <param name="target">The target.</param>
 /// <param name="reader">The reader.</param>
 /// <param name="keys">The keys</param>
 public void Set(RequestScope request, IResultMap resultMap,
                 ResultProperty mapping, ref object target, IDataReader reader, object keys)
 {
     Get(request, resultMap, mapping, ref target, reader);
 }
 /// <summary>
 /// Adds a (named) ResultMap
 /// </summary>
 /// <param name="resultMap">The ResultMap to add</param>
 public void AddResultMap(IResultMap resultMap)
 {
     if (resultMaps.ContainsKey(resultMap.Id))
     {
         throw new DataMapperException("The DataMapper already contains an ResultMap named " + resultMap.Id);
     }
     resultMaps.Add(resultMap.Id, resultMap);
 }
Beispiel #29
0
        /// <summary>
        /// Gets the value of the specified <see cref="ResultProperty"/> that must be set on the target object.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultMap">The result map.</param>
        /// <param name="mapping">The mapping.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="target">The target object</param>
        public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
        {
            // The property is a IList
            IList list = null;

            // Get the IList property
            object property = ObjectProbe.GetMemberValue(target, mapping.PropertyName,
                                                         request.DataExchangeFactory.AccessorFactory);

            if (property == null)// Create the list if need
            {
                property = mapping.ListFactory.CreateInstance(null);
                mapping.SetAccessor.Set(target, property);
            }
            list = (IList)property;

            object     result             = null;
            IResultMap propertyRresultMap = mapping.NestedResultMap.ResolveSubMap(reader);

            if (propertyRresultMap.GroupByProperties.Count > 0)
            {
                string uniqueKey = GetUniqueKey(propertyRresultMap, request, reader);

                // Gets the [key, result object] already build
                IDictionary buildObjects = request.GetUniqueKeys(propertyRresultMap);

                if (buildObjects != null && buildObjects.Contains(uniqueKey))
                {
                    // Unique key is already known, so get the existing result object and process additional results.
                    result = buildObjects[uniqueKey];

                    //In some cases (nested groupings) our object may be null, so there is
                    //no point in going on
                    if (result != null)
                    {
                        // process resulMapping attribute which point to a groupBy attribute
                        for (int index = 0; index < propertyRresultMap.Properties.Count; index++)
                        {
                            ResultProperty resultProperty = propertyRresultMap.Properties[index];
                            if (resultProperty.PropertyStrategy is PropertStrategy.GroupByStrategy)
                            {
                                resultProperty.PropertyStrategy.Set(request, propertyRresultMap, resultProperty, ref result, reader, null);
                            }
                        }
                    }
                    result = SKIP;
                }
                else if (uniqueKey == null || buildObjects == null || !buildObjects.Contains(uniqueKey))
                {
                    // Unique key is NOT known, so create a new result object and then process additional results.
                    result = _resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);

                    if (buildObjects == null)
                    {
                        buildObjects = new Hashtable();
                        request.SetUniqueKeys(propertyRresultMap, buildObjects);
                    }
                    buildObjects[uniqueKey] = result;
                }
            }
            else // Last resultMap have no groupBy attribute
            {
                result = _resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);
            }


            if ((result != null) && (result != SKIP))
            {
                list.Add(result);
            }

            return(result);
        }
 /// <summary>
 /// Add a list of ResultMap to the collection
 /// </summary>
 /// <param name="value"></param>
 public void AddRange(IResultMap[] value)
 {
     for (int i = 0; i < value.Length; i++)
     {
         Add(value[i]);
     }
 }
 /// <summary>
 /// Gets the value of the specified <see cref="ResultProperty"/> that must be set on the target object.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="resultMap">The result map.</param>
 /// <param name="mapping">The mapping.</param>
 /// <param name="reader">The reader.</param>
 /// <param name="target">The target object</param>
 public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
 {
     throw new NotSupportedException("Get method on ResultMapStrategy is not supported");
 }
        /// <summary>
        /// Insert a ResultMap in the collection.
        /// </summary>
        /// <param name="index">Index where to insert.</param>
        /// <param name="value">A ResultMap</param>
        public void Insert(int index, IResultMap value)
        {
            if (index < 0 || index > _count)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            Resize(_count + 1);
            Array.Copy(_innerList, index, _innerList, index + 1, _count - index);
            _innerList[index] = value;
            _count++;
        }
Beispiel #33
0
		///<summary>
		/// Sets value of the specified <see cref="ResultProperty"/> on the target object
		/// when a 'select' attribute exists
		/// on the <see cref="ResultProperty"/> are empties.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <param name="resultMap">The result map.</param>
		/// <param name="mapping">The ResultProperty.</param>
		/// <param name="target">The target.</param>
		/// <param name="reader">The reader.</param>
		/// <param name="selectKeys">The keys</param>
		public void Set(RequestScope request, IResultMap resultMap, 
			ResultProperty mapping, ref object target, IDataReader reader, object selectKeys)
		{
			string paramString = mapping.ColumnName;
			object keys = null;
			bool wasNull = false;

			#region Finds the select keys.
			if (paramString.IndexOf(',')>0 || paramString.IndexOf('=')>0) // composite parameters key
			{
				IDictionary keyMap = new Hashtable();
				keys = keyMap;
				// define which character is seperating fields
				char[] splitter  = {'=',','};

				string[] paramTab = paramString.Split(splitter);
				if (paramTab.Length % 2 != 0) 
				{
					throw new DataMapperException("Invalid composite key string format in '"+mapping.PropertyName+". It must be: property1=column1,property2=column2,..."); 
				}
				IEnumerator enumerator = paramTab.GetEnumerator();
				while (!wasNull && enumerator.MoveNext()) 
				{
					string hashKey = ((string)enumerator.Current).Trim();
                    if (paramString.Contains("="))// old 1.x style multiple params
                    {
                        enumerator.MoveNext();
                    }
                    object hashValue = reader.GetValue(reader.GetOrdinal(((string)enumerator.Current).Trim()));

					keyMap.Add(hashKey, hashValue );
					wasNull = (hashValue == DBNull.Value);
				}
			} 
			else // single parameter key
			{
				keys = reader.GetValue(reader.GetOrdinal(paramString));
				wasNull = reader.IsDBNull(reader.GetOrdinal(paramString));
			}
			#endregion

			if (wasNull) 
			{
				// set the value of an object property to null
                mapping.Set(target, null);
			} 
			else // Collection object or .Net object
			{
				selectStrategy.Set(request, resultMap, mapping, ref target, reader, keys);
			}		
		}
Beispiel #34
0
        /// <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);
                 }
             }
        }
Beispiel #35
0
 public NonNullViolation(FieldNode selection, Path path, IResultMap parent)
 {
     Selection = selection;
     Path      = path;
     Parent    = parent;
 }