示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="tag"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        public override int DoStartFragment(SqlTagContext ctx, SqlTag tag, object parameterObject)
        {
            IterateContext iterate = (IterateContext)ctx.GetAttribute(tag);

            if (iterate == null)
            {
                string propertyName = ((BaseTag)tag).Property;
                object collection;
                if (!string.IsNullOrEmpty(propertyName))
                {
                    collection = ObjectProbe.GetMemberValue(parameterObject, propertyName, AccessorFactory);
                }
                else
                {
                    collection = parameterObject;
                }
                iterate = new IterateContext(collection);
                ctx.AddAttribute(tag, iterate);
            }
            if (iterate.HasNext)
            {
                return(INCLUDE_BODY);
            }
            return(SKIP_BODY);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="tag"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        public override bool IsCondition(SqlTagContext ctx, SqlTag tag, object parameterObject)
        {
            if (parameterObject == null)
            {
                return(true);
            }
            string propertyName = ((BaseTag)tag).Property;
            object value        = null;

            if (!string.IsNullOrEmpty(propertyName))
            {
                value = ObjectProbe.GetMemberValue(parameterObject, propertyName, AccessorFactory);
            }
            else
            {
                value = parameterObject;
            }
            if (value is ICollection)
            {
                return((value == null) || (((ICollection)value).Count < 1));
            }
            if (value != null && typeof(Array).IsAssignableFrom(value.GetType()))         //value.GetType().IsArray
            {
                return(((Array)value).GetLength(0) == 0);
            }
            return((value == null) || (Convert.ToString(value).Equals("")));
        }
示例#3
0
 public override bool IsCondition(SqlTagContext ctx, SqlTag tag, object parameterObject)
 {
     if (parameterObject != null)
     {
         string property = ((BaseTag)tag).Property;
         object obj2     = null;
         if ((property != null) && (property.Length > 0))
         {
             obj2 = ObjectProbe.GetMemberValue(parameterObject, property, base.AccessorFactory);
         }
         else
         {
             obj2 = parameterObject;
         }
         if (obj2 is ICollection)
         {
             if (obj2 != null)
             {
                 return(((ICollection)obj2).Count < 1);
             }
             return(true);
         }
         if ((obj2 != null) && typeof(Array).IsAssignableFrom(obj2.GetType()))
         {
             return(((Array)obj2).GetLength(0) == 0);
         }
         if (obj2 != null)
         {
             return(Convert.ToString(obj2).Equals(""));
         }
     }
     return(true);
 }
示例#4
0
        public override int DoStartFragment(SqlTagContext ctx, SqlTag tag, object parameterObject)
        {
            IterateContext attribute = (IterateContext)ctx.GetAttribute(tag);

            if (attribute == null)
            {
                object obj2;
                string property = ((BaseTag)tag).Property;
                if ((property != null) && (property.Length > 0))
                {
                    obj2 = ObjectProbe.GetMemberValue(parameterObject, property, base.AccessorFactory);
                }
                else
                {
                    obj2 = parameterObject;
                }
                attribute = new IterateContext(obj2);
                ctx.AddAttribute(tag, attribute);
            }
            if ((attribute != null) && attribute.HasNext)
            {
                return(1);
            }
            return(0);
        }
示例#5
0
        /// <summary>
        /// Executes the SQL and retuns all rows selected in a map that is keyed on the property named
        /// in the keyProperty parameter.  The value at each key will be the value of the property specified
        /// in the valueProperty parameter.  If valueProperty is null, the entire result object will be entered.
        /// </summary>
        /// <param name="request">The request scope.</param>
        /// <param name="session">The session used to execute the statement</param>
        /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
        /// <param name="keyProperty">The property of the result object to be used as the key.</param>
        /// <param name="valueProperty">The property of the result object to be used as the value (or null)</param>
        /// <param name="rowDelegate">A delegate called once per row in the QueryForMapWithRowDelegate method</param>
        /// <returns>A IDictionary of object containing the rows keyed by keyProperty.</returns>
        ///<exception cref="DataMapperException">If a transaction is not in progress, or the database throws an exception.</exception>
        internal IDictionary <TKey, TValue> RunQueryForDictionary <TKey, TValue>(RequestScope request,
                                                                                 ISession session,
                                                                                 object parameterObject,
                                                                                 string keyProperty,
                                                                                 string valueProperty,
                                                                                 DictionaryRowDelegate <TKey, TValue> rowDelegate)
        {
            IDictionary <TKey, TValue> map = new Dictionary <TKey, TValue>();

            using (IDbCommand command = request.IDbCommand)
            {
                IDataReader reader = command.ExecuteReader();
                try
                {
                    if (rowDelegate == null)
                    {
                        while (reader.Read())
                        {
                            object obj   = resultStrategy.Process(request, ref reader, null);
                            TKey   key   = (TKey)ObjectProbe.GetMemberValue(obj, keyProperty, request.DataExchangeFactory.AccessorFactory);
                            TValue value = default(TValue);
                            if (valueProperty != null)
                            {
                                value = (TValue)ObjectProbe.GetMemberValue(obj, valueProperty, request.DataExchangeFactory.AccessorFactory);
                            }
                            else
                            {
                                value = (TValue)obj;
                            }
                            map.Add(key, value);
                        }
                    }
                    else
                    {
                        while (reader.Read())
                        {
                            object obj   = resultStrategy.Process(request, ref reader, null);
                            TKey   key   = (TKey)ObjectProbe.GetMemberValue(obj, keyProperty, request.DataExchangeFactory.AccessorFactory);
                            TValue value = default(TValue);
                            if (valueProperty != null)
                            {
                                value = (TValue)ObjectProbe.GetMemberValue(obj, valueProperty, request.DataExchangeFactory.AccessorFactory);
                            }
                            else
                            {
                                value = (TValue)obj;
                            }
                            rowDelegate(key, value, parameterObject, map);
                        }
                    }
                }
                finally
                {
                    reader.Close();
                    reader.Dispose();
                }
                ExecuteDelayedLoad(request);
            }
            return(map);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="tag"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        public override int DoStartFragment(SqlTagContext ctx, SqlTag tag, object parameterObject)
        {
            IterateContext iterate = (IterateContext)ctx.GetAttribute(tag);

            if (iterate == null)
            {
                string propertyName = ((BaseTag)tag).Property;
                object collection;
                if (propertyName != null && propertyName.Length > 0)
                {
                    collection = ObjectProbe.GetMemberValue(parameterObject, propertyName, this.AccessorFactory);
                }
                else
                {
                    collection = parameterObject;
                }
                iterate = new IterateContext(collection);
                ctx.AddAttribute(tag, iterate);
            }
            if (iterate != null && iterate.HasNext)
            {
                return(BaseTagHandler.INCLUDE_BODY);
            }
            else
            {
                return(BaseTagHandler.SKIP_BODY);
            }
        }
示例#7
0
 public override object GetData(ParameterProperty mapping, object parameterObject)
 {
     if (!mapping.IsComplexMemberName && (this._parameterClass == parameterObject.GetType()))
     {
         return(mapping.GetAccessor.Get(parameterObject));
     }
     return(ObjectProbe.GetMemberValue(parameterObject, mapping.PropertyName, base.DataExchangeFactory.AccessorFactory));
 }
 public override object GetData(ParameterProperty mapping, object parameterObject)
 {
     if (mapping.IsComplexMemberName)
     {
         return(ObjectProbe.GetMemberValue(parameterObject, mapping.PropertyName, base.DataExchangeFactory.AccessorFactory));
     }
     return(parameterObject);
 }
示例#9
0
        internal IDictionary RunQueryForMap(RequestScope request, ISqlMapSession session, object parameterObject, string keyProperty, string valueProperty, DictionaryRowDelegate rowDelegate)
        {
            IDictionary dictionary = new Hashtable();

            using (IDbCommand command = request.IDbCommand)
            {
                IDataReader reader = command.ExecuteReader();
                try
                {
                    object obj5;
                    if (rowDelegate != null)
                    {
                        goto Label_00C4;
                    }
                    while (reader.Read())
                    {
                        object obj2 = this._resultStrategy.Process(request, ref reader, null);
                        object obj3 = ObjectProbe.GetMemberValue(obj2, keyProperty, request.DataExchangeFactory.AccessorFactory);
                        object obj4 = obj2;
                        if (valueProperty != null)
                        {
                            obj4 = ObjectProbe.GetMemberValue(obj2, valueProperty, request.DataExchangeFactory.AccessorFactory);
                        }
                        dictionary.Add(obj3, obj4);
                    }
                    goto Label_00E0;
Label_0072:
                    obj5 = this._resultStrategy.Process(request, ref reader, null);
                    object key  = ObjectProbe.GetMemberValue(obj5, keyProperty, request.DataExchangeFactory.AccessorFactory);
                    object obj7 = obj5;
                    if (valueProperty != null)
                    {
                        obj7 = ObjectProbe.GetMemberValue(obj5, valueProperty, request.DataExchangeFactory.AccessorFactory);
                    }
                    rowDelegate(key, obj7, parameterObject, dictionary);
Label_00C4:
                    if (reader.Read())
                    {
                        goto Label_0072;
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    reader.Close();
                    reader.Dispose();
                }
Label_00E0:
                this.ExecutePostSelect(request);
            }
            return(dictionary);
        }
示例#10
0
        public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
        {
            IList  list = null;
            object obj2 = ObjectProbe.GetMemberValue(target, mapping.PropertyName, request.DataExchangeFactory.AccessorFactory);

            if (obj2 == null)
            {
                obj2 = mapping.ListFactory.CreateInstance(null);
                mapping.SetAccessor.Set(target, obj2);
            }
            list = (IList)obj2;
            object     sKIP = null;
            IResultMap map  = mapping.NestedResultMap.ResolveSubMap(reader);

            if (map.GroupByProperties.Count > 0)
            {
                string      key        = base.GetUniqueKey(map, request, reader);
                IDictionary uniqueKeys = request.GetUniqueKeys(map);
                if ((uniqueKeys != null) && uniqueKeys.Contains(key))
                {
                    sKIP = uniqueKeys[key];
                    if (sKIP != null)
                    {
                        for (int i = 0; i < map.Properties.Count; i++)
                        {
                            ResultProperty property = map.Properties[i];
                            if (property.PropertyStrategy is GroupByStrategy)
                            {
                                property.PropertyStrategy.Set(request, map, property, ref sKIP, reader, null);
                            }
                        }
                    }
                    sKIP = BaseStrategy.SKIP;
                }
                else if (((key == null) || (uniqueKeys == null)) || !uniqueKeys.Contains(key))
                {
                    sKIP = _resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);
                    if (uniqueKeys == null)
                    {
                        uniqueKeys = new Hashtable();
                        request.SetUniqueKeys(map, uniqueKeys);
                    }
                    uniqueKeys[key] = sKIP;
                }
            }
            else
            {
                sKIP = _resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);
            }
            if ((sKIP != null) && (sKIP != BaseStrategy.SKIP))
            {
                list.Add(sKIP);
            }
            return(sKIP);
        }
 /// <summary>
 /// Gets the data to be set into a IDataParameter.
 /// </summary>
 /// <param name="mapping"></param>
 /// <param name="parameterObject"></param>
 public override object GetData(ParameterProperty mapping, object parameterObject)
 {
     if (parameterObject != null)
     {
         if (DataExchangeFactory.TypeHandlerFactory.IsSimpleType(parameterObject.GetType()))
         {
             return(parameterObject);
         }
         return(ObjectProbe.GetMemberValue(parameterObject, mapping.PropertyName, DataExchangeFactory.AccessorFactory));
     }
     return(null);
 }
        private string ProcessDynamicElements(object parameterObject)
        {
            StringTokenizer tokenizer  = new StringTokenizer(this._simpleSqlStatement, "$", true);
            StringBuilder   builder    = new StringBuilder();
            string          current    = null;
            string          str2       = null;
            IEnumerator     enumerator = tokenizer.GetEnumerator();

            while (enumerator.MoveNext())
            {
                current = (string)enumerator.Current;
                if ("$".Equals(str2))
                {
                    if ("$".Equals(current))
                    {
                        builder.Append("$");
                        current = null;
                    }
                    else
                    {
                        object obj2 = null;
                        if (parameterObject != null)
                        {
                            if (this._dataExchangeFactory.TypeHandlerFactory.IsSimpleType(parameterObject.GetType()))
                            {
                                obj2 = parameterObject;
                            }
                            else
                            {
                                obj2 = ObjectProbe.GetMemberValue(parameterObject, current, this._dataExchangeFactory.AccessorFactory);
                            }
                        }
                        if (obj2 != null)
                        {
                            builder.Append(obj2.ToString());
                        }
                        enumerator.MoveNext();
                        current = (string)enumerator.Current;
                        if (!"$".Equals(current))
                        {
                            throw new DataMapperException("Unterminated dynamic element in sql (" + this._simpleSqlStatement + ").");
                        }
                        current = null;
                    }
                }
                else if (!"$".Equals(current))
                {
                    builder.Append(current);
                }
                str2 = current;
            }
            return(builder.ToString());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="sqlTag"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected long Compare(SqlTagContext ctx, SqlTag sqlTag, object parameterObject)
        {
            var    tag                 = (Conditional)sqlTag;
            string propertyName        = tag.Property;
            string literal             = tag.Literal;
            string comparePropertyName = tag.CompareProperty;
            string compareValue        = tag.CompareValue;

            object value1 = null;
            Type   type   = null;

            if (propertyName != null && propertyName.Length > 0)
            {
                // check to see if we are a list
                //if (fpropertyName.StartsWith("["))
                //{

                //}

                value1 = ObjectProbe.GetMemberValue(parameterObject, propertyName, AccessorFactory);
                type   = value1.GetType();
            }
            else if (!String.IsNullOrEmpty(literal))
            {
                value1 = literal;
                type   = value1.GetType();
            }
            else
            {
                value1 = parameterObject;
                if (value1 != null)
                {
                    type = parameterObject.GetType();
                }
                else
                {
                    type = typeof(object);
                }
            }
            if (comparePropertyName != null && comparePropertyName.Length > 0)
            {
                object value2 = ObjectProbe.GetMemberValue(parameterObject, comparePropertyName, AccessorFactory);
                return(CompareValues(type, value1, value2));
            }
            else if (!String.IsNullOrEmpty(compareValue))
            {
                return(CompareValues(type, value1, compareValue));
            }
            else
            {
                throw new DataMapperException("Error comparing in conditional fragment.  Uknown 'compare to' values.");
            }
        }
示例#14
0
        protected object GetBindValue(SqlTagContext ctx, string bindName, object parameterObject)
        {
            var bindingExpression = ctx.GetBindExpression(bindName);

            if (bindingExpression == null)
            {
                return(null);
            }

            if (String.IsNullOrEmpty(bindingExpression.FullPropertyName))
            {
                return(bindingExpression.Value);
            }

            return(ObjectProbe.GetMemberValue(parameterObject, bindingExpression.FullPropertyName, AccessorFactory));
        }
示例#15
0
        public object GetMemberValue(SqlTagContext ctx, BaseTag tag, string propertyName, object parameterObject)
        {
            var iteratorContext = FindParentIteratorContext(ctx, tag);

            if (iteratorContext != null)
            {
                var indexOfIndexer = propertyName.IndexOf(ReflectionMapper.THIS_ENUMERATOR_PLACEHOLDER);

                if (indexOfIndexer == 0)
                {
                    parameterObject = iteratorContext.Current;
                    propertyName    = propertyName.Substring(indexOfIndexer + ReflectionMapper.THIS_ENUMERATOR_PLACEHOLDER.Length);
                }
            }

            return(ObjectProbe.GetMemberValue(parameterObject, propertyName, _accessorFactory));
        }
示例#16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="tag"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        public override bool IsCondition(SqlTagContext ctx, SqlTag tag, object parameterObject)
        {
            if (parameterObject == null)
            {
                return(true);
            }
            string propertyName = ((BaseTag)tag).Property;
            object value;

            if (!string.IsNullOrEmpty(propertyName))
            {
                value = ObjectProbe.GetMemberValue(parameterObject, propertyName, AccessorFactory);
            }
            else
            {
                value = parameterObject;
            }
            return(value == null);
        }
        public override bool IsCondition(SqlTagContext ctx, SqlTag tag, object parameterObject)
        {
            object obj2;

            if (parameterObject == null)
            {
                return(true);
            }
            string property = ((BaseTag)tag).Property;

            if ((property != null) && (property.Length > 0))
            {
                obj2 = ObjectProbe.GetMemberValue(parameterObject, property, base.AccessorFactory);
            }
            else
            {
                obj2 = parameterObject;
            }
            return(obj2 == null);
        }
示例#18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="sqlTag"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        protected long Compare(SqlTagContext ctx, SqlTag sqlTag, object parameterObject)
        {
            Conditional tag                 = (Conditional)sqlTag;
            string      propertyName        = tag.Property;
            string      comparePropertyName = tag.CompareProperty;
            string      compareValue        = tag.CompareValue;

            object value1 = null;
            Type   type   = null;

            if (propertyName != null && propertyName.Length > 0)
            {
                value1 = ObjectProbe.GetMemberValue(parameterObject, propertyName, this.AccessorFactory);
                type   = value1.GetType();
            }
            else
            {
                value1 = parameterObject;
                if (value1 != null)
                {
                    type = parameterObject.GetType();
                }
                else
                {
                    type = typeof(object);
                }
            }
            if (comparePropertyName != null && comparePropertyName.Length > 0)
            {
                object value2 = ObjectProbe.GetMemberValue(parameterObject, comparePropertyName, this.AccessorFactory);
                return(CompareValues(type, value1, value2));
            }
            else if (compareValue != null && compareValue != "")
            {
                return(CompareValues(type, value1, compareValue));
            }
            else
            {
                throw new DataMapperException("Error comparing in conditional fragment.  Uknown 'compare to' values.");
            }
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="ctx"></param>
 /// <param name="tag"></param>
 /// <param name="parameterObject"></param>
 /// <returns></returns>
 public override bool IsCondition(SqlTagContext ctx, SqlTag tag, object parameterObject)
 {
     if (parameterObject == null)
     {
         return(true);
     }
     else
     {
         string propertyName = ((BaseTag)tag).Property;
         object value;
         if (propertyName != null && propertyName.Length > 0)
         {
             value = ObjectProbe.GetMemberValue(parameterObject, propertyName, this.AccessorFactory);
         }
         else
         {
             value = parameterObject;
         }
         return(value == null);
     }
 }
        protected long Compare(SqlTagContext ctx, SqlTag sqlTag, object parameterObject)
        {
            Conditional conditional     = (Conditional)sqlTag;
            string      property        = conditional.Property;
            string      compareProperty = conditional.CompareProperty;
            string      compareValue    = conditional.CompareValue;
            object      obj2            = null;
            Type        type            = null;

            if ((property != null) && (property.Length > 0))
            {
                obj2 = ObjectProbe.GetMemberValue(parameterObject, property, base.AccessorFactory);
                type = obj2.GetType();
            }
            else
            {
                obj2 = parameterObject;
                if (obj2 != null)
                {
                    type = parameterObject.GetType();
                }
                else
                {
                    type = typeof(object);
                }
            }
            if ((compareProperty != null) && (compareProperty.Length > 0))
            {
                object obj3 = ObjectProbe.GetMemberValue(parameterObject, compareProperty, base.AccessorFactory);
                return(this.CompareValues(type, obj2, obj3));
            }
            switch (compareValue)
            {
            case null:
            case "":
                throw new DataMapperException("Error comparing in conditional fragment.  Uknown 'compare to' values.");
            }
            return(this.CompareValues(type, obj2, compareValue));
        }
        public override string ProcessToken(string tokenValue)
        {
            object value = null;

            if (_parameterObject != null)
            {
                if (_dataExchangeFactory.TypeHandlerFactory.IsSimpleType(_parameterObject.GetType()))
                {
                    value = _parameterObject;
                }
                else
                {
                    value = ObjectProbe.GetMemberValue(_parameterObject, tokenValue, _dataExchangeFactory.AccessorFactory);
                }
            }

            if (value != null)
            {
                return(value.ToString());
            }

            return(null);
        }
示例#22
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];
                    // 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);
        }
示例#23
0
        /// <summary>
        /// Applies the parameter map.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="command">The command.</param>
        /// <param name="request">The request.</param>
        /// <param name="statement">The statement.</param>
        /// <param name="parameterObject">The parameter object.</param>
        protected virtual void ApplyParameterMap
            (ISqlMapSession session, IDbCommand command,
            RequestScope request, IStatement statement, object parameterObject)
        {
            StringCollection properties = request.PreparedStatement.DbParametersName;

            IDbDataParameter[] parameters   = request.PreparedStatement.DbParameters;
            StringBuilder      paramLogList = new StringBuilder(); // Log info
            StringBuilder      typeLogList  = new StringBuilder(); // Log info

            int count = properties.Count;

            for (int i = 0; i < count; ++i)
            {
                IDbDataParameter  sqlParameter  = parameters[i];
                IDbDataParameter  parameterCopy = command.CreateParameter();
                ParameterProperty property      = request.ParameterMap.GetProperty(i);

                if (command.CommandType == CommandType.StoredProcedure)
                {
                    #region store procedure command

                    // A store procedure must always use a ParameterMap
                    // to indicate the mapping order of the properties to the columns
                    if (request.ParameterMap == null)                     // Inline Parameters
                    {
                        throw new DataMapperException("A procedure statement tag must alway have a parameterMap attribute, which is not the case for the procedure '" + statement.Id + "'.");
                    }
                    else                     // Parameters via ParameterMap
                    {
                        if (property.DirectionAttribute.Length == 0)
                        {
                            property.Direction = sqlParameter.Direction;
                        }

                        sqlParameter.Direction = property.Direction;
                    }
                    #endregion
                }

                request.ParameterMap.SetParameter(property, parameterCopy, parameterObject);

                parameterCopy.Direction = sqlParameter.Direction;

                // With a ParameterMap, we could specify the ParameterDbTypeProperty
                if (request.ParameterMap != null)
                {
                    if (property.DbType != null && property.DbType.Length > 0)
                    {
                        string dbTypePropertyName = session.DataSource.DbProvider.ParameterDbTypeProperty;
                        object propertyValue      = ObjectProbe.GetMemberValue(sqlParameter, dbTypePropertyName, request.DataExchangeFactory.AccessorFactory);
                        ObjectProbe.SetMemberValue(parameterCopy, dbTypePropertyName, propertyValue,
                                                   request.DataExchangeFactory.ObjectFactory, request.DataExchangeFactory.AccessorFactory);
                    }
                    else
                    {
                        //parameterCopy.DbType = sqlParameter.DbType;
                    }
                }
                else
                {
                    //parameterCopy.DbType = sqlParameter.DbType;
                }


                // JIRA-49 Fixes (size, precision, and scale)
                if (session.DataSource.DbProvider.SetDbParameterSize)
                {
                    if (sqlParameter.Size > 0)
                    {
                        parameterCopy.Size = sqlParameter.Size;
                    }
                }

                if (session.DataSource.DbProvider.SetDbParameterPrecision)
                {
                    parameterCopy.Precision = sqlParameter.Precision;
                }

                if (session.DataSource.DbProvider.SetDbParameterScale)
                {
                    parameterCopy.Scale = sqlParameter.Scale;
                }

                parameterCopy.ParameterName = sqlParameter.ParameterName;

                command.Parameters.Add(parameterCopy);
            }
        }
示例#24
0
        internal IDictionary <K, V> RunQueryForDictionary <K, V>(RequestScope request, ISqlMapSession session, object parameterObject, string keyProperty, string valueProperty, DictionaryRowDelegate <K, V> rowDelegate)
        {
            IDictionary <K, V> dictionary = new Dictionary <K, V>();

            using (IDbCommand command = request.IDbCommand)
            {
                IDataReader reader = command.ExecuteReader();
                try
                {
                    object obj3;
                    if (rowDelegate != null)
                    {
                        goto Label_00F6;
                    }
                    while (reader.Read())
                    {
                        object obj2   = this._resultStrategy.Process(request, ref reader, null);
                        K      local  = (K)ObjectProbe.GetMemberValue(obj2, keyProperty, request.DataExchangeFactory.AccessorFactory);
                        V      local2 = default(V);
                        if (valueProperty != null)
                        {
                            local2 = (V)ObjectProbe.GetMemberValue(obj2, valueProperty, request.DataExchangeFactory.AccessorFactory);
                        }
                        else
                        {
                            local2 = (V)obj2;
                        }
                        dictionary.Add(local, local2);
                    }
                    goto Label_0112;
Label_008B:
                    obj3 = this._resultStrategy.Process(request, ref reader, null);
                    K key    = (K)ObjectProbe.GetMemberValue(obj3, keyProperty, request.DataExchangeFactory.AccessorFactory);
                    V local4 = default(V);
                    if (valueProperty != null)
                    {
                        local4 = (V)ObjectProbe.GetMemberValue(obj3, valueProperty, request.DataExchangeFactory.AccessorFactory);
                    }
                    else
                    {
                        local4 = (V)obj3;
                    }
                    rowDelegate(key, local4, parameterObject, dictionary);
Label_00F6:
                    if (reader.Read())
                    {
                        goto Label_008B;
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    reader.Close();
                    reader.Dispose();
                }
Label_0112:
                this.ExecutePostSelect(request);
            }
            return(dictionary);
        }
示例#25
0
        /// <summary>
        /// Applies the parameter map.
        /// 此处实现SQL语句中所需数据到对应参数(如@parame0)的一一对应 最后一次对SQL语句的分析了
        /// </summary>
        /// <param name="dbProvider">The dbProvider.</param>
        /// <param name="command">The command.</param>
        /// <param name="request">The request.</param>
        /// <param name="statement">The statement.</param>
        /// <param name="parameterObject">The parameter object.</param>
        protected virtual void ApplyParameterMap
            (IDbProvider dbProvider, IDbCommand command,
            RequestScope request, IStatement statement, object parameterObject)
        {
            //properties和parameters以及ParameterMap.Properties三者是一一对应的关系
            StringCollection properties = request.PreparedStatement.DbParametersName;

            IDbDataParameter[] parameters = request.PreparedStatement.DbParameters;

            StringBuilder paramLogList = new StringBuilder(); // Log info
            StringBuilder typeLogList  = new StringBuilder(); // Log info

            int count = properties.Count;

            for (int i = 0; i < count; ++i)
            {
                IDbDataParameter  sqlParameter  = parameters[i];
                IDbDataParameter  parameterCopy = command.CreateParameter();
                ParameterProperty property      = request.ParameterMap.GetProperty(i);

                #region Logging
                if (log.IsDebugEnabled)
                {
                    paramLogList.Append(sqlParameter.ParameterName);
                    paramLogList.Append("=[");
                    typeLogList.Append(sqlParameter.ParameterName);
                    typeLogList.Append("=[");
                }
                #endregion

                if (command.CommandType == CommandType.StoredProcedure)
                {
                    #region store procedure command

                    // A store procedure must always use a ParameterMap
                    // to indicate the mapping order of the properties to the columns
                    if (request.ParameterMap == null)                     // Inline Parameters
                    {
                        throw new DataMapperException("A procedure statement tag must alway have a parameterMap attribute, which is not the case for the procedure '" + statement.Id + "'.");
                    }
                    // Parameters via ParameterMap
                    if (property.DirectionAttribute.Length == 0)
                    {
                        property.Direction = sqlParameter.Direction;
                    }

                    sqlParameter.Direction = property.Direction;
                    #endregion
                }

                #region Logging
                if (log.IsDebugEnabled)
                {
                    paramLogList.Append(property.PropertyName);
                    paramLogList.Append(",");
                }
                #endregion

                //把parameterObject类中的property属性值赋值到parameterCopy参数中
                request.ParameterMap.SetParameter(property, parameterCopy, parameterObject);

                parameterCopy.Direction = sqlParameter.Direction;

                // With a ParameterMap, we could specify the ParameterDbTypeProperty
                if (request.ParameterMap != null)
                {
                    if (!string.IsNullOrEmpty(property.DbType))
                    {
                        string dbTypePropertyName = dbProvider.ParameterDbTypeProperty;
                        object propertyValue      = ObjectProbe.GetMemberValue(sqlParameter, dbTypePropertyName, request.DataExchangeFactory.AccessorFactory);
                        ObjectProbe.SetMemberValue(parameterCopy, dbTypePropertyName, propertyValue,
                                                   request.DataExchangeFactory.ObjectFactory, request.DataExchangeFactory.AccessorFactory);
                    }
                }

                #region Logging
                if (log.IsDebugEnabled)
                {
                    if (parameterCopy.Value == DBNull.Value)
                    {
                        paramLogList.Append("null");
                        paramLogList.Append("], ");
                        typeLogList.Append("System.DBNull, null");
                        typeLogList.Append("], ");
                    }
                    else
                    {
                        paramLogList.Append(parameterCopy.Value.ToString());
                        paramLogList.Append("], ");

                        // sqlParameter.DbType could be null (as with Npgsql)
                        // if PreparedStatementFactory did not find a dbType for the parameter in:
                        // line 225: "if (property.DbType.Length >0)"
                        // Use parameterCopy.DbType

                        //typeLogList.Append( sqlParameter.DbType.ToString() );
                        typeLogList.Append(parameterCopy.DbType.ToString());
                        typeLogList.Append(", ");
                        typeLogList.Append(parameterCopy.Value.GetType().ToString());
                        typeLogList.Append("], ");
                    }
                }
                #endregion

                ApplyDbProviderParameterSettings(dbProvider, sqlParameter, parameterCopy);

                //确定当前参数对应SQL语句中的哪一个参数
                parameterCopy.ParameterName = sqlParameter.ParameterName;

                //加入分析完成的一个参数类到Command对象中
                command.Parameters.Add(parameterCopy);
            }

            #region Logging

            if (log.IsDebugEnabled && properties.Count > 0)
            {
                log.Debug("Statement Id: [" + statement.Id + "] Parameters: [" + paramLogList.ToString(0, paramLogList.Length - 2) + "]");
                log.Debug("Statement Id: [" + statement.Id + "] Types: [" + typeLogList.ToString(0, typeLogList.Length - 2) + "]");
            }
            #endregion
        }
        protected virtual void ApplyParameterMap(ISqlMapSession session, IDbCommand command, RequestScope request, IStatement statement, object parameterObject)
        {
            StringCollection dbParametersName = request.PreparedStatement.DbParametersName;

            IDbDataParameter[] dbParameters = request.PreparedStatement.DbParameters;
            StringBuilder      builder      = new StringBuilder();
            StringBuilder      builder2     = new StringBuilder();
            int count = dbParametersName.Count;

            for (int i = 0; i < count; i++)
            {
                IDbDataParameter  parameter     = dbParameters[i];
                IDbDataParameter  dataParameter = command.CreateParameter();
                ParameterProperty mapping       = request.ParameterMap.GetProperty(i);
                if (_logger.IsDebugEnabled)
                {
                    builder.Append(parameter.ParameterName);
                    builder.Append("=[");
                    builder2.Append(parameter.ParameterName);
                    builder2.Append("=[");
                }
                if (command.CommandType == CommandType.StoredProcedure)
                {
                    if (request.ParameterMap == null)
                    {
                        throw new DataMapperException("A procedure statement tag must alway have a parameterMap attribute, which is not the case for the procedure '" + statement.Id + "'.");
                    }
                    if (mapping.DirectionAttribute.Length == 0)
                    {
                        mapping.Direction = parameter.Direction;
                    }
                    parameter.Direction = mapping.Direction;
                }
                if (_logger.IsDebugEnabled)
                {
                    builder.Append(mapping.PropertyName);
                    builder.Append(",");
                }
                request.ParameterMap.SetParameter(mapping, dataParameter, parameterObject);
                dataParameter.Direction = parameter.Direction;
                if (((request.ParameterMap != null) && (mapping.DbType != null)) && (mapping.DbType.Length > 0))
                {
                    string parameterDbTypeProperty = session.DataSource.DbProvider.ParameterDbTypeProperty;
                    object memberValue             = ObjectProbe.GetMemberValue(parameter, parameterDbTypeProperty, request.DataExchangeFactory.AccessorFactory);
                    ObjectProbe.SetMemberValue(dataParameter, parameterDbTypeProperty, memberValue, request.DataExchangeFactory.ObjectFactory, request.DataExchangeFactory.AccessorFactory);
                }
                if (_logger.IsDebugEnabled)
                {
                    if (dataParameter.Value == DBNull.Value)
                    {
                        builder.Append("null");
                        builder.Append("], ");
                        builder2.Append("System.DBNull, null");
                        builder2.Append("], ");
                    }
                    else
                    {
                        builder.Append(dataParameter.Value.ToString());
                        builder.Append("], ");
                        builder2.Append(dataParameter.DbType.ToString());
                        builder2.Append(", ");
                        builder2.Append(dataParameter.Value.GetType().ToString());
                        builder2.Append("], ");
                    }
                }
                if (session.DataSource.DbProvider.SetDbParameterSize && (parameter.Size > 0))
                {
                    dataParameter.Size = parameter.Size;
                }
                if (session.DataSource.DbProvider.SetDbParameterPrecision)
                {
                    dataParameter.Precision = parameter.Precision;
                }
                if (session.DataSource.DbProvider.SetDbParameterScale)
                {
                    dataParameter.Scale = parameter.Scale;
                }
                dataParameter.ParameterName = parameter.ParameterName;
                command.Parameters.Add(dataParameter);
            }
            if (_logger.IsDebugEnabled && (dbParametersName.Count > 0))
            {
                _logger.Debug("Statement Id: [" + statement.Id + "] Parameters: [" + builder.ToString(0, builder.Length - 2) + "]");
                _logger.Debug("Statement Id: [" + statement.Id + "] Types: [" + builder2.ToString(0, builder2.Length - 2) + "]");
            }
        }
 /// <summary>
 /// Gets the data to be set into a IDataParameter.
 /// </summary>
 /// <param name="mapping"></param>
 /// <param name="parameterObject"></param>
 public override object GetData(ParameterProperty mapping, object parameterObject)
 {
     return(ObjectProbe.GetMemberValue(parameterObject, mapping.PropertyName,
                                       this.DataExchangeFactory.AccessorFactory));
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        private string ProcessDynamicElements(object parameterObject)
        {
            // define which character is seperating fields

            StringTokenizer parser = new StringTokenizer(_simpleSqlStatement, ELEMENT_TOKEN, true);

            StringBuilder newSql = new StringBuilder();

            string token     = null;
            string lastToken = null;

            IEnumerator enumerator = parser.GetEnumerator();

            while (enumerator.MoveNext())
            {
                token = ((string)enumerator.Current);

                if (ELEMENT_TOKEN.Equals(lastToken))
                {
                    if (ELEMENT_TOKEN.Equals(token))
                    {
                        newSql.Append(ELEMENT_TOKEN);
                        token = null;
                    }
                    else
                    {
                        object value = null;
                        if (parameterObject != null)
                        {
                            if (_dataExchangeFactory.TypeHandlerFactory.IsSimpleType(parameterObject.GetType()) == true)
                            {
                                value = parameterObject;
                            }
                            else
                            {
                                value = ObjectProbe.GetMemberValue(parameterObject, token, _dataExchangeFactory.AccessorFactory);
                            }
                        }
                        if (value != null)
                        {
                            newSql.Append(value.ToString());
                        }

                        enumerator.MoveNext();
                        token = ((string)enumerator.Current);

                        if (!ELEMENT_TOKEN.Equals(token))
                        {
                            throw new DataMapperException("Unterminated dynamic element in sql (" + _simpleSqlStatement + ").");
                        }
                        token = null;
                    }
                }
                else
                {
                    if (!ELEMENT_TOKEN.Equals(token))
                    {
                        newSql.Append(token);
                    }
                }

                lastToken = token;
            }

            return(newSql.ToString());
        }
示例#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 获取target类属性mapping.PropertyName的值
            object property = ObjectProbe.GetMemberValue(target, mapping.PropertyName,
                                                         request.DataExchangeFactory.AccessorFactory);

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

            object result = null;
            //返回的结果中包含其他类对象的情况
            IResultMap propertyRresultMap = mapping.NestedResultMap.ResolveSubMap(reader);

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

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

                if (buildObjects != null && buildObjects.ContainsKey(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 GroupByStrategy)
                            {
                                // 跳到PropertyStrategy中的类
                                resultProperty.PropertyStrategy.Set(request, propertyRresultMap, resultProperty, ref result, reader, null);
                            }
                        }
                    }
                    result = SKIP;
                }
                else if (uniqueKey == null || buildObjects == null || !buildObjects.ContainsKey(uniqueKey))
                {
                    // Unique key is NOT known, so create a new result object and then process additional results.
                    //在ResultMapStrategy类中实现对target类mapping属性的赋值
                    result = resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);

                    if (buildObjects == null)
                    {
                        buildObjects = new Dictionary <string, object>();
                        request.SetUniqueKeys(propertyRresultMap, buildObjects);
                    }
                    buildObjects[uniqueKey] = result;
                }
            }
            else // Last resultMap have no groupBy attribute
            {
                if (propertyRresultMap.KeyPropertyNames.Count > 0)
                {
                    result = circularResultMapStrategy.Get(request, resultMap, mapping, ref target, reader);
                }
                else
                {
                    //在ResultMapStrategy类中实现对target类mapping属性的赋值
                    result = resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);
                }
            }


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

            return(result);
        }
        /// <summary>
        /// Applies the parameter map.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="command">The command.</param>
        /// <param name="request">The request.</param>
        /// <param name="statement">The statement.</param>
        /// <param name="parameterObject">The parameter object.</param>
        protected virtual void ApplyParameterMap
            (ISqlMapSession session, IDbCommand command,
            RequestScope request, IStatement statement, object parameterObject)
        {
            StringCollection properties = request.PreparedStatement.DbParametersName;

            IDbDataParameter[] parameters   = request.PreparedStatement.DbParameters;
            StringBuilder      paramLogList = new StringBuilder(); // Log info
            StringBuilder      typeLogList  = new StringBuilder(); // Log info

            int count = properties.Count;

            for (int i = 0; i < count; ++i)
            {
                IDbDataParameter  sqlParameter  = parameters[i];
                IDbDataParameter  parameterCopy = command.CreateParameter();
                ParameterProperty property      = request.ParameterMap.GetProperty(i);

                #region Logging
                if (_logger.IsDebugEnabled)
                {
                    paramLogList.Append(sqlParameter.ParameterName);
                    paramLogList.Append("=[");
                    typeLogList.Append(sqlParameter.ParameterName);
                    typeLogList.Append("=[");
                }
                #endregion

                if (command.CommandType == CommandType.StoredProcedure)
                {
                    #region store procedure command

                    // A store procedure must always use a ParameterMap
                    // to indicate the mapping order of the properties to the columns
                    if (request.ParameterMap == null) // Inline Parameters
                    {
                        throw new DataMapperException("A procedure statement tag must alway have a parameterMap attribute, which is not the case for the procedure '" + statement.Id + "'.");
                    }
                    else // Parameters via ParameterMap
                    {
                        if (property.DirectionAttribute.Length == 0)
                        {
                            property.Direction = sqlParameter.Direction;
                        }

                        sqlParameter.Direction = property.Direction;
                    }
                    #endregion
                }

                #region Logging
                if (_logger.IsDebugEnabled)
                {
                    paramLogList.Append(property.PropertyName);
                    paramLogList.Append(",");
                }
                #endregion

                request.ParameterMap.SetParameter(property, parameterCopy, parameterObject);

                parameterCopy.Direction = sqlParameter.Direction;

                // With a ParameterMap, we could specify the ParameterDbTypeProperty
                if (request.ParameterMap != null)
                {
                    if (property.DbType != null && property.DbType.Length > 0)
                    {
                        string dbTypePropertyName = session.DataSource.DbProvider.ParameterDbTypeProperty;
                        object propertyValue      = ObjectProbe.GetMemberValue(sqlParameter, dbTypePropertyName, request.DataExchangeFactory.AccessorFactory);
                        ObjectProbe.SetMemberValue(parameterCopy, dbTypePropertyName, propertyValue,
                                                   request.DataExchangeFactory.ObjectFactory, request.DataExchangeFactory.AccessorFactory);
                    }
                    else
                    {
                        //parameterCopy.DbType = sqlParameter.DbType;
                    }
                }
                else
                {
                    //parameterCopy.DbType = sqlParameter.DbType;
                }


                #region Logging
                if (_logger.IsDebugEnabled)
                {
                    if (parameterCopy.Value == DBNull.Value)
                    {
                        paramLogList.Append("null");
                        paramLogList.Append("], ");
                        typeLogList.Append("System.DBNull, null");
                        typeLogList.Append("], ");
                    }
                    else
                    {
                        paramLogList.Append(parameterCopy.Value.ToString());
                        paramLogList.Append("], ");

                        // sqlParameter.DbType could be null (as with Npgsql)
                        // if PreparedStatementFactory did not find a dbType for the parameter in:
                        // line 225: "if (property.DbType.Length >0)"
                        // Use parameterCopy.DbType

                        //typeLogList.Append( sqlParameter.DbType.ToString() );
                        typeLogList.Append(parameterCopy.DbType.ToString());
                        typeLogList.Append(", ");
                        typeLogList.Append(parameterCopy.Value.GetType().ToString());
                        typeLogList.Append("], ");
                    }
                }
                #endregion

                // JIRA-49 Fixes (size, precision, and scale)
                if (session.DataSource.DbProvider.SetDbParameterSize)
                {
                    if (sqlParameter.Size > 0)
                    {
                        parameterCopy.Size = sqlParameter.Size;
                    }
                }

                if (session.DataSource.DbProvider.SetDbParameterPrecision)
                {
                    parameterCopy.Precision = sqlParameter.Precision;
                }

                if (session.DataSource.DbProvider.SetDbParameterScale)
                {
                    parameterCopy.Scale = sqlParameter.Scale;
                }

                parameterCopy.ParameterName = sqlParameter.ParameterName;

                command.Parameters.Add(parameterCopy);
            }

            #region Logging

            if (_logger.IsDebugEnabled && properties.Count > 0)
            {
                _logger.Debug("Statement Id: [" + statement.Id + "] Parameters: [" + paramLogList.ToString(0, paramLogList.Length - 2) + "]");
                _logger.Debug("Statement Id: [" + statement.Id + "] Types: [" + typeLogList.ToString(0, typeLogList.Length - 2) + "]");
            }
            #endregion
        }