Beispiel #1
0
        /// <summary>
        /// Build the PreparedStatement
        /// </summary>
        /// <param name="session"></param>
        /// <param name="sqlStatement"></param>
        public void BuildPreparedStatement(ISqlMapSession session, string sqlStatement)
        {
            RequestScope request = new RequestScope( _dataExchangeFactory, session, _statement);

            PreparedStatementFactory factory = new PreparedStatementFactory( session, request, _statement, sqlStatement);
            _preparedStatement = factory.Prepare();
        }
        /// <summary>
        /// Processes the specified <see cref="IDataReader"/> 
        /// when a ResultMap is specified on the statement.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object outObject = resultObject;

            IResultMap resultMap = request.CurrentResultMap.ResolveSubMap(reader);

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

                outObject = resultMap.CreateInstanceOfResult(parameters);
            }

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

            return outObject;
        }
        ///<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>
        /// Processes the specified <see cref="IDataReader"/> 
        /// when a ResultClass is specified on the statement and
        /// the ResultClass is a SimpleType.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object outObject = resultObject;
            AutoResultMap resultMap = request.CurrentResultMap as AutoResultMap;

            if (outObject == null)
            {
                outObject = resultMap.CreateInstanceOfResultClass();
            }

            if (!resultMap.IsInitalized)
            {
                lock(resultMap)
                {
                    if (!resultMap.IsInitalized)
                    {
                        // Create a ResultProperty
                        ResultProperty property = new ResultProperty();
                        property.PropertyName = "value";
                        property.ColumnIndex = 0;
                        property.TypeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(outObject.GetType());
                        property.PropertyStrategy = PropertyStrategyFactory.Get(property);

                        resultMap.Properties.Add(property);
                        resultMap.DataExchange = request.DataExchangeFactory.GetDataExchangeForClass(typeof(int));// set the PrimitiveDataExchange
                        resultMap.IsInitalized = true;
                    }
                }
            }

            resultMap.Properties[0].PropertyStrategy.Set(request, resultMap, resultMap.Properties[0], ref outObject, reader, null);

            return outObject;
        }
        /// <summary>
        /// Processes the specified <see cref="IDataReader"/> 
        /// a an auto result map is used.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object outObject = resultObject;

            if (outObject == null)
            {
                outObject = (request.CurrentResultMap as AutoResultMap).CreateInstanceOfResultClass();
            }

            AutoResultMap resultMap = InitializeAutoResultMap(request, ref reader, ref outObject);

            // En configuration initialiser des AutoResultMap (IResultMap) avec uniquement leur class name et class et les mettres
            // ds Statement.ResultsMap puis ds AutoMapStrategy faire comme AutoResultMap ds Java
            // tester si la request.CurrentResultMap [AutoResultMap (IResultMap)] est initialisée
            // [if (allowRemapping || getResultMappings() == null) {initialize(rs);] java
            // si ( request.Statement.AllowRemapping || (request.CurrentResultMap as AutoResultMap).IsInitalized) ....

            for (int index = 0; index < resultMap.Properties.Count; index++)
            {
                ResultProperty property = resultMap.Properties[index];
                resultMap.SetValueOfProperty(ref outObject, property,
                                             property.GetDataBaseValue(reader));
            }

            return outObject;
        }
 /// <summary>
 /// Executes the specified <see cref="PostBindind"/>.
 /// </summary>
 /// <param name="postSelect">The <see cref="PostBindind"/>.</param>
 /// <param name="request">The <see cref="RequestScope"/></param>
 public void Execute(PostBindind postSelect, RequestScope request)
 {
     IFactory factory =  request.DataExchangeFactory.ObjectFactory.CreateFactory(postSelect.ResultProperty.SetAccessor.MemberType, Type.EmptyTypes);
     object values = factory.CreateInstance(null);
     postSelect.Statement.ExecuteQueryForList(request.Session, postSelect.Keys, (IList)values);
     postSelect.ResultProperty.SetAccessor.Set(postSelect.Target, values);
 }
        /// <summary>
        /// Processes the specified <see cref="IDataReader"/> 
        /// when a 'resultClass' attribute is specified on the statement and
        /// the 'resultClass' attribute is a <see cref="IDictionary"/>.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object outObject = resultObject;
            AutoResultMap resultMap = request.CurrentResultMap as AutoResultMap;

            if (outObject == null)
            {
                outObject = resultMap.CreateInstanceOfResultClass();
            }

            int count = reader.FieldCount;
            IDictionary dictionary = (IDictionary) outObject;
            for (int i = 0; i < count; i++)
            {
                ResultProperty property = new ResultProperty();
                property.PropertyName = "value";
                property.ColumnIndex = i;
                property.TypeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(i));
                dictionary.Add(
                    reader.GetName(i),
                    property.GetDataBaseValue(reader));
            }

            return outObject;
        }
        /// <summary>
        /// Gets the value of an argument constructor.
        /// </summary>
        /// <param name="request">The current <see cref="RequestScope"/>.</param>
        /// <param name="mapping">The <see cref="ResultProperty"/> with the argument infos.</param>
        /// <param name="reader">The current <see cref="IDataReader"/>.</param>
        /// <param name="keys">The keys</param>
        /// <returns>The paremeter value.</returns>
        public object GetValue(RequestScope request, ResultProperty mapping, 
            ref IDataReader reader, object keys)
        {
            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>
        /// Processes the specified <see cref="IDataReader"/> 
        /// when no resultClass or resultMap attribute are specified.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object outObject = resultObject;

            if (reader.FieldCount == 1)
            {
                ResultProperty property = new ResultProperty();
                property.PropertyName = "value";
                property.ColumnIndex = 0;
                property.TypeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(0));
                outObject = property.GetDataBaseValue(reader);
            }
            else if (reader.FieldCount > 1)
            {
                object[] newOutObject = new object[reader.FieldCount];
                int count = reader.FieldCount;
                for (int i = 0; i < count; i++)
                {
                    ResultProperty property = new ResultProperty();
                    property.PropertyName = "value";
                    property.ColumnIndex = i;
                    property.TypeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(i));
                    newOutObject[i] = property.GetDataBaseValue(reader);
                }

                outObject = newOutObject;
            }
            else
            {
                // do nothing if 0 fields
            }

            return outObject;
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="session"></param>
 /// <param name="statement"></param>
 /// <param name="commandText"></param>
 /// <param name="request"></param>
 public PreparedStatementFactory(ISqlMapSession session, RequestScope request, IStatement statement, string commandText)
 {
     _session = session;
     _request = request;
     _statement = statement;
     _commandText = commandText;
 }
        /// <summary>
        /// Executes the specified <see cref="PostBindind"/>.
        /// </summary>
        /// <param name="postSelect">The <see cref="PostBindind"/>.</param>
        /// <param name="request">The <see cref="RequestScope"/></param>
        public void Execute(PostBindind postSelect, RequestScope request)
        {
            // How to: Examine and Instantiate Generic Types with Reflection
            // http://msdn2.microsoft.com/en-us/library/b8ytshk6.aspx

            Type[] typeArgs = postSelect.ResultProperty.SetAccessor.MemberType.GetGenericArguments();
            Type genericList = typeof(IList<>);
            Type constructedType = genericList.MakeGenericType(typeArgs);
            Type elementType = postSelect.ResultProperty.SetAccessor.MemberType.GetGenericArguments()[0];

            Type mappedStatementType = postSelect.Statement.GetType();

            Type[] typeArguments = { typeof(SqlMapSession), typeof(object) };

            MethodInfo[] mis = mappedStatementType.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance);
            MethodInfo mi = null;
            foreach (MethodInfo m in mis)
            {
                if (m.IsGenericMethod &&
                    m.Name == "ExecuteQueryForList" &&
                    m.GetParameters().Length == 2)
                {
                    mi = m;
                    break;
                }
            }

            MethodInfo miConstructed = mi.MakeGenericMethod(elementType);

            // Invoke the method.
            object[] args = { request.Session, postSelect.Keys };
            object values = miConstructed.Invoke(postSelect.Statement, args);

            postSelect.ResultProperty.SetAccessor.Set(postSelect.Target, values);
        }
        /// <summary>
        /// Gets the value of an argument constructor.
        /// </summary>
        /// <param name="request">The current <see cref="RequestScope"/>.</param>
        /// <param name="mapping">The <see cref="ResultProperty"/> with the argument infos.</param>
        /// <param name="reader">The current <see cref="IDataReader"/>.</param>
        /// <param name="keys">The keys</param>
        /// <returns>The paremeter value.</returns>
        public object GetValue(RequestScope request, ResultProperty mapping, 
            ref IDataReader reader, object keys)
        {
            // Get the select statement
            IMappedStatement selectStatement = request.MappedStatement.SqlMap.GetMappedStatement(mapping.Select);

            reader = DataReaderTransformer.Transform(reader, request.Session.DataSource.DbProvider);
            return selectStatement.ExecuteQueryForObject(request.Session, keys);
        }
Beispiel #13
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>The result object</returns>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object outObject = resultObject;

            IResultMap resultMap = request.CurrentResultMap.ResolveSubMap(reader);

            string uniqueKey = GetUniqueKey(resultMap, request, reader);
            // Gets the [key, result object] already build
            IDictionary buildObjects = request.GetUniqueKeys(resultMap);

            if (buildObjects != null && buildObjects.Contains(uniqueKey))
            {
                // Unique key is already known, so get the existing result object and process additional results.
                outObject = buildObjects[uniqueKey];
                // process resulMapping attribute wich point to a groupBy attribute
                for (int index = 0; index < resultMap.Properties.Count; index++)
                {
                    ResultProperty resultProperty = resultMap.Properties[index];
                    if (resultProperty.PropertyStrategy is PropertStrategy.GroupByStrategy)
                    {
                        resultProperty.PropertyStrategy.Set(request, resultMap, resultProperty, ref outObject, reader, null);
                    }
                }
                outObject = SKIP;
            }
            else if (uniqueKey == null || buildObjects == null || !buildObjects.Contains(uniqueKey))
            {
                // Unique key is NOT known, so create a new result object and process additional results.

                // Fix IBATISNET-241
                if (outObject == null)
                {
                    // temp ?, we don't support constructor tag with groupBy attribute
                    outObject = resultMap.CreateInstanceOfResult(null);
                }

                for (int index = 0; index < resultMap.Properties.Count; index++)
                {
                    ResultProperty resultProperty = resultMap.Properties[index];
                    resultProperty.PropertyStrategy.Set(request, resultMap, resultProperty, ref outObject, reader, null);
                }

                if (buildObjects == null)
                {
                    buildObjects = new Hashtable();
                    request.SetUniqueKeys(resultMap, buildObjects);
                }
                buildObjects[uniqueKey] = outObject;
            }

            return outObject;
        }
Beispiel #14
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>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            IResultMap resultMap = request.CurrentResultMap.ResolveSubMap(reader);

            if (resultMap.GroupByPropertyNames.Count>0)
            {
                return _groupByStrategy.Process(request, ref reader, resultObject);
            }
            else
            {
                return _resultMapStrategy.Process(request, ref reader, resultObject);
            }
        }
        /// <summary>
        /// Create an IDbCommand for the SqlMapSession and the current SQL Statement
        /// and fill IDbCommand IDataParameter's with the parameterObject.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="session">The SqlMapSession</param>
        /// <param name="statement">The IStatement</param>
        /// <param name="parameterObject">
        /// The parameter object that will fill the sql parameter
        /// </param>
        /// <returns>An IDbCommand with all the IDataParameter filled.</returns>
        public void Create(RequestScope request, ISqlMapSession session, IStatement statement, object parameterObject )
        {
            // the IDbConnection & the IDbTransaction are assign in the CreateCommand
            request.IDbCommand = new DbCommandDecorator(session.CreateCommand(statement.CommandType), request);

            request.IDbCommand.CommandText = request.PreparedStatement.PreparedSql;

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("Statement Id: [" + statement.Id + "] PreparedStatement : [" + request.IDbCommand.CommandText + "]");
            }

            ApplyParameterMap( session, request.IDbCommand, request, statement, parameterObject  );
        }
        /// <summary>
        /// Executes the specified <see cref="PostBindind"/>.
        /// </summary>
        /// <param name="postSelect">The <see cref="PostBindind"/>.</param>
        /// <param name="request">The <see cref="RequestScope"/></param>
        public void Execute(PostBindind postSelect, RequestScope request)
        {
            IList values = postSelect.Statement.ExecuteQueryForList(request.Session, postSelect.Keys);
            Type elementType = postSelect.ResultProperty.SetAccessor.MemberType.GetElementType();

            Array array = Array.CreateInstance(elementType, values.Count);
            int count = values.Count;
            for(int i=0;i<count;i++)
            {
                array.SetValue(values[i],i);
            }

            postSelect.ResultProperty.SetAccessor.Set(postSelect.Target, array);
        }
Beispiel #17
0
 /// <summary>
 /// Build the PreparedStatement
 /// </summary>
 /// <param name="session"></param>
 /// <param name="commandText"></param>
 /// <param name="request"></param>
 public PreparedStatement BuildPreparedStatement(ISqlMapSession session, RequestScope request, string commandText)
 {
     if ( _preparedStatement == null )
     {
         lock(_synRoot)
         {
             if (_preparedStatement==null)
             {
                 PreparedStatementFactory factory = new PreparedStatementFactory( session, request, _statement, commandText);
                 _preparedStatement = factory.Prepare();
             }
         }
     }
     return _preparedStatement;
 }
Beispiel #18
0
 public override bool Equals(object obj)
 {
     if (this != obj)
     {
         if (!(obj is RequestScope))
         {
             return(false);
         }
         RequestScope scope = (RequestScope)obj;
         if (this._id != scope._id)
         {
             return(false);
         }
     }
     return(true);
 }
        /// <summary>
        /// Gets the value of an argument constructor.
        /// </summary>
        /// <param name="request">The current <see cref="RequestScope"/>.</param>
        /// <param name="mapping">The <see cref="ResultProperty"/> with the argument infos.</param>
        /// <param name="reader">The current <see cref="IDataReader"/>.</param>
        /// <param name="keys">The keys</param>
        /// <returns>The paremeter value.</returns>
        public object GetValue(RequestScope request, ResultProperty mapping, 
            ref IDataReader reader, object keys)
        {
            // Get the select statement
            IMappedStatement selectStatement = request.MappedStatement.SqlMap.GetMappedStatement(mapping.Select);

            reader = DataReaderTransformer.Transform(reader, request.Session.DataSource.DbProvider);
            IList values = selectStatement.ExecuteQueryForList(request.Session, keys);

            Type elementType = mapping.MemberType.GetElementType();
            Array array = Array.CreateInstance(elementType, values.Count);
            int count = values.Count;
            for(int i=0;i<count;i++)
            {
                array.SetValue(values[i],i);
            }
            return array;
        }
        ///<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.SqlMap.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.QueueSelect.Enqueue(postSelect);
        }
        /// <summary>
        /// Gets the value of an argument constructor.
        /// </summary>
        /// <param name="request">The current <see cref="RequestScope"/>.</param>
        /// <param name="mapping">The <see cref="ResultProperty"/> with the argument infos.</param>
        /// <param name="reader">The current <see cref="IDataReader"/>.</param>
        /// <param name="keys">The keys</param>
        /// <returns>The paremeter value.</returns>
        public object GetValue(RequestScope request, ResultProperty mapping, 
		                       ref IDataReader reader, object keys)
        {
            // Get the select statement
            IMappedStatement selectStatement = request.MappedStatement.SqlMap.GetMappedStatement(mapping.Select);

            if (mapping.MemberType == typeof(IList))
            {
                reader = DataReaderTransformer.Transform(reader, request.Session.DataSource.DbProvider);
                return selectStatement.ExecuteQueryForList(request.Session, keys);
            }
            else // Strongly typed List
            {
                reader = DataReaderTransformer.Transform(reader, request.Session.DataSource.DbProvider);
                IFactory factory = request.DataExchangeFactory.ObjectFactory.CreateFactory(mapping.MemberType, Type.EmptyTypes);
                object values = factory.CreateInstance(null);
                selectStatement.ExecuteQueryForList(request.Session, keys, (IList)values);
                return values;
            }
        }
 /// <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>
 public object Process(RequestScope request, ref IDataReader reader, object resultObject)
 {
     // Check if the ResultClass is a 'primitive' Type
     if (request.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(request.CurrentResultMap.Class))
     {
         return _simpleTypeStrategy.Process(request, ref reader, resultObject);
     }
     else if (typeof(IDictionary).IsAssignableFrom(request.CurrentResultMap.Class))
     {
         return _dictionaryStrategy.Process(request, ref reader, resultObject);
     }
     else if (typeof(IList).IsAssignableFrom(request.CurrentResultMap.Class))
     {
         return _listStrategy.Process(request, ref reader, resultObject);
     }
     else
     {
         return _autoMapStrategy.Process(request, ref reader, resultObject);
     }
 }
        /// <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.ArgumentStrategy.GetValue(request, resultProperty, 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 (this.FillObjectWithReaderAndResultMap(request, reader, resultMapping, ref obj) == false)
                {
                    obj = null;
                }
            }

            return obj;
        }
        /// <summary>
        /// Gets the value of an argument constructor.
        /// </summary>
        /// <param name="request">The current <see cref="RequestScope"/>.</param>
        /// <param name="mapping">The <see cref="ResultProperty"/> with the argument infos.</param>
        /// <param name="reader">The current <see cref="IDataReader"/>.</param>
        /// <param name="keys">The keys</param>
        /// <returns>The paremeter value.</returns>
        public object GetValue(RequestScope request, ResultProperty mapping, 
            ref IDataReader reader, object keys)
        {
            // Get the select statement
            IMappedStatement selectStatement = request.MappedStatement.SqlMap.GetMappedStatement(mapping.Select);

            reader = DataReaderTransformer.Transform(reader, request.Session.DataSource.DbProvider);

            Type[] typeArgs = mapping.MemberType.GetGenericArguments();
            Type genericList = typeof(IList<>);
            Type constructedType = genericList.MakeGenericType(typeArgs);
            Type elementType = mapping.MemberType.GetGenericArguments()[0];

            Type mappedStatementType = selectStatement.GetType();

            Type[] typeArguments = { typeof(SqlMapSession), typeof(object) };

            MethodInfo[] mis = mappedStatementType.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance);
            MethodInfo mi = null;
            foreach (MethodInfo m in mis)
            {
                if (m.IsGenericMethod &&
                    m.Name == "ExecuteQueryForList" &&
                    m.GetParameters().Length == 2)
                {
                    mi = m;
                    break;
                }
            }

            MethodInfo miConstructed = mi.MakeGenericMethod(elementType);

            // Invoke the method.
            object[] args = { request.Session, keys };
            object values = miConstructed.Invoke(selectStatement, args);

            return values;
        }
        /// <summary>
        /// Auto-map the reader to the result object.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        /// <returns>The AutoResultMap use to map the resultset.</returns>
        private AutoResultMap InitializeAutoResultMap(RequestScope request, ref IDataReader reader, ref object resultObject)
        {
            AutoResultMap resultMap  = request.CurrentResultMap as AutoResultMap;

            if (request.Statement.AllowRemapping)
            {
                resultMap = resultMap.Clone();

                ResultPropertyCollection properties = ReaderAutoMapper.Build(
                    request.DataExchangeFactory,
                    reader,
                    ref resultObject);

                resultMap.Properties.AddRange(properties);
            }
            else
            {
                if (!resultMap.IsInitalized)
                {
                    lock (resultMap)
                    {
                        if (!resultMap.IsInitalized)
                        {
                            ResultPropertyCollection properties = ReaderAutoMapper.Build(
                               request.DataExchangeFactory,
                               reader,
                               ref resultObject);

                            resultMap.Properties.AddRange(properties);
                            resultMap.IsInitalized = true;
                        }
                    }
                }

            }

            return resultMap;
        }
        /// <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 #27
0
 /// <summary>
 /// Executes the specified <see cref="PostBindind"/>.
 /// </summary>
 /// <param name="postSelect">The <see cref="PostBindind"/>.</param>
 /// <param name="request">The <see cref="RequestScope"/></param>
 public void Execute(PostBindind postSelect, RequestScope request)
 {
     object value = postSelect.Statement.ExecuteQueryForObject(request.Session, postSelect.Keys);
     postSelect.ResultProperty.SetAccessor.Set(postSelect.Target, value);
 }
 /// <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>
 /// Initializes a new instance of the <see cref="DataReaderDecorator"/> class.
 /// </summary>
 /// <param name="dataReader">The data reader.</param>
 /// <param name="request">The request scope</param>
 public DataReaderDecorator(IDataReader dataReader, RequestScope request)
 {
     _innerDataReader = dataReader;
     _request = request;
 }
        /// <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
        }
Beispiel #31
0
        /// <summary>
        /// Builds a new <see cref="RequestScope"/> and the sql command text to execute.
        /// </summary>
        /// <param name="parameterObject">The parameter object (used in DynamicSql)</param>
        /// <param name="session">The current session</param>
        /// <param name="mappedStatement">The <see cref="IMappedStatement"/>.</param>
        /// <returns>A new <see cref="RequestScope"/>.</returns>
        public RequestScope GetRequestScope(IMappedStatement mappedStatement, 
            object parameterObject, ISqlMapSession session)
        {
            RequestScope request = new RequestScope(_dataExchangeFactory, session, _statement);

            request.PreparedStatement = BuildPreparedStatement(session, request, _sqlStatement);
            request.MappedStatement = mappedStatement;

            return request;
        }