/// <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;
		}
Пример #2
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();
        }
Пример #3
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;
		}
		/// <summary>
		/// Store the specified session.
		/// </summary>
		/// <param name="session">The session to store</param>
        public override void Store(ISqlMapSession session)
		{
            HttpContext currentContext = HttpContext.Current;
            if (currentContext == null)
            {
                CallContext.SetData(sessionName, session);
            }
		    else
            {
                currentContext.Items[sessionName] = session;
            }
		}
Пример #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RequestScope"/> class.
        /// </summary>
        /// <param name="dataExchangeFactory">The data exchange factory.</param>
        /// <param name="session">The session.</param>
        /// <param name="statement">The statement</param>
        public RequestScope(
            DataExchangeFactory dataExchangeFactory,
            ISqlMapSession session,
            IStatement statement
            )
        {
            _errorContext = new ErrorContext();

            _statement = statement;
            _parameterMap = statement.ParameterMap;
            _session = session;
            _dataExchangeFactory = dataExchangeFactory;
            _id = GetNextId();
        }
        /// <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  );
        }
Пример #7
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;
 }
		/// <summary>
		/// For store procedure, auto discover IDataParameters for stored procedures at run-time.
		/// </summary>
		/// <param name="session">The current session.</param>
		private void DiscoverParameter(ISqlMapSession session)
		{
			// pull the parameters for this stored procedure from the parameter cache 
			// (or discover them & populate the cache)
            IDataParameter[] commandParameters = session.SqlMapper.DBHelperParameterCache.GetSpParameterSet(session, _commandText);

            _preparedStatement.DbParameters = new IDbDataParameter[commandParameters.Length];

			int start = session.DataSource.DbProvider.ParameterPrefix.Length;
			for(int i=0; i< commandParameters.Length;i++)
			{
				IDbDataParameter dataParameter = (IDbDataParameter)commandParameters[i];

				if (session.DataSource.DbProvider.UseParameterPrefixInParameter == false)
				{
					if (dataParameter.ParameterName.StartsWith(session.DataSource.DbProvider.ParameterPrefix)) 
					{
						dataParameter.ParameterName = dataParameter.ParameterName.Substring(start);
					}
				}
				_preparedStatement.DbParametersName.Add( dataParameter.ParameterName );
				_preparedStatement.DbParameters[i] = dataParameter;
			}
		    
            // Re-sort DbParameters to match order used in the parameterMap
            IDbDataParameter[] sortedDbParameters = new IDbDataParameter[commandParameters.Length];
            for (int i = 0; i < _statement.ParameterMap.Properties.Count; i++)
            {
                sortedDbParameters[i] = Search(session, _preparedStatement.DbParameters, _statement.ParameterMap.Properties[i], i);
            }
            _preparedStatement.DbParameters = sortedDbParameters;
		}
Пример #9
0
 /// <summary>
 /// Execute an update statement. Also used for delete statement.
 /// Return the number of row effected.
 /// </summary>
 /// <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>
 /// <returns>The number of row effected.</returns>
 public int ExecuteUpdate(ISqlMapSession session, object parameterObject)
 {
     return(_mappedStatement.ExecuteUpdate(session, parameterObject));
 }
Пример #10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <returns></returns>
 public override int ExecuteUpdate(ISqlMapSession session, object parameterObject)
 {
     throw new DataMapperException("Insert statements cannot be executed as a update query.");
 }
        /// <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);
            }
        }
 /// <summary>
 /// Store the specified session.
 /// </summary>
 /// <param name="session">The session to store</param>
 public override void Store(ISqlMapSession session)
 {
     CallContext.SetData(sessionName, session);
 }
Пример #13
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <returns></returns>
 public override IList ExecuteQueryForList(ISqlMapSession session, object parameterObject )
 {
     throw new DataMapperException("Insert statements cannot be executed as a query for list.");
 }
Пример #14
0
 /// <summary>
 /// Executes an SQL statement that returns a single row as an Object.
 /// </summary>
 /// <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>
 /// <returns>The object</returns>
 public T ExecuteQueryForObject <T>(ISqlMapSession session, object parameterObject)
 {
     return(this.ExecuteQueryForObject <T>(session, parameterObject, default(T)));
 }
Пример #15
0
 /// <summary>
 /// Executes an SQL statement that returns a single row as an Object.
 /// </summary>
 /// <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>
 /// <returns>The object</returns>
 public object ExecuteQueryForObject(ISqlMapSession session, object parameterObject)
 {
     return(this.ExecuteQueryForObject(session, parameterObject, null));
 }
Пример #16
0
 /// <summary>
 /// Executes the SQL and retuns all rows selected. This is exactly the same as
 /// calling ExecuteQueryForList(session, parameterObject, NO_SKIPPED_RESULTS, NO_MAXIMUM_RESULTS).
 /// </summary>
 /// <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>
 /// <returns>A List of result objects.</returns>
 public IList <T> ExecuteQueryForList <T>(ISqlMapSession session, object parameterObject)
 {
     return(this.ExecuteQueryForList <T>(session, parameterObject, MappedStatement.NO_SKIPPED_RESULTS, MappedStatement.NO_MAXIMUM_RESULTS));
 }
Пример #17
0
 /// <summary>
 /// Executes the SQL and and fill a strongly typed collection.
 /// </summary>
 /// <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="resultObject">A strongly typed collection of result objects.</param>
 public void ExecuteQueryForList <T>(ISqlMapSession session, object parameterObject, IList <T> resultObject)
 {
     _mappedStatement.ExecuteQueryForList(session, parameterObject, resultObject);
 }
Пример #18
0
 /// <summary>
 /// Execute an insert statement. Fill the parameter object with
 /// the ouput parameters if any, also could return the insert generated key
 /// </summary>
 /// <param name="session">The session</param>
 /// <param name="parameterObject">The parameter object used to fill the statement.</param>
 /// <returns>Can return the insert generated key.</returns>
 public object ExecuteInsert(ISqlMapSession session, object parameterObject)
 {
     return(_mappedStatement.ExecuteInsert(session, parameterObject));
 }
Пример #19
0
 /// <summary>
 /// Store the specified session.
 /// </summary>
 /// <param name="session">The session to store</param>
 public override void Store(ISqlMapSession session)
 {
     WcfSessionItemsInstanceExtension.Current.Items.Set(sessionName, session);
 }
 public TransactionToPersistanceTransaction(IsolationLevel level, ISqlMapper session)
 {
     _session = session;
     _tran    = _session.BeginTransaction(level);
 }
        public RequestScope GetRequestScope(IMappedStatement mappedStatement, object parameterObject, ISqlMapSession session)
        {
            RequestScope scope;
            string       sqlStatement = this.ProcessDynamicElements(parameterObject);

            return(new RequestScope(this._dataExchangeFactory, session, this._statement)
            {
                PreparedStatement = this.BuildPreparedStatement(session, scope, sqlStatement), MappedStatement = mappedStatement
            });
        }
Пример #22
0
 /// <summary>
 /// Runs a query with a custom object that gets a chance
 /// to deal with each row as it is processed.
 /// </summary>
 /// <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="rowDelegate"></param>
 public IList <T> ExecuteQueryForRowDelegate <T>(ISqlMapSession session, object parameterObject, RowDelegate <T> rowDelegate)
 {
     return(_mappedStatement.ExecuteQueryForRowDelegate <T>(session, parameterObject, rowDelegate));
 }
Пример #23
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="session"></param>
 /// <param name="request"></param>
 /// <param name="sqlStatement"></param>
 /// <returns></returns>
 private PreparedStatement BuildPreparedStatement(ISqlMapSession session, RequestScope request, string sqlStatement)
 {
     PreparedStatementFactory factory = new PreparedStatementFactory( session, request, _statement, sqlStatement);
     return factory.Prepare();
 }
        /// <summary>
        /// Build the PreparedStatement
        /// </summary>
        /// <param name="session"></param>
        /// <param name="request"></param>
        /// <param name="sqlStatement"></param>
        private PreparedStatement BuildPreparedStatement(ISqlMapSession session, RequestScope request, string sqlStatement)
        {
            PreparedStatementFactory factory = new PreparedStatementFactory(session, request, _statement, sqlStatement);

            return(factory.Prepare());
        }
Пример #25
0
        /// <summary>
        /// Store the specified session.
        /// </summary>
        /// <param name="session">The session to store</param>
        public override void Store(ISqlMapSession session)
        {
            HttpContext currentContext = ObtainSessionContext();

            currentContext.Items[sessionName] = session;
        }
 /// <summary>
 /// Store the specified session.
 /// </summary>
 /// <param name="session">The session to store</param>
 public abstract void Store(ISqlMapSession session);
 public TransactionToPersistanceTransaction(ISqlMapper session)
 {
     _session = session;
     _tran    = _session.BeginTransaction();
 }
Пример #28
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <param name="resultObject"></param>
 /// <returns></returns>
 public override object ExecuteQueryForObject(ISqlMapSession session, object parameterObject, object resultObject)
 {
     throw new DataMapperException("Delete statements cannot be executed as a query for object.");
 }
Пример #29
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <returns></returns>
 public override object ExecuteInsert(ISqlMapSession session, object parameterObject)
 {
     throw new DataMapperException("Update statements cannot be executed as a query insert.");
 }
Пример #30
0
 /// <summary>
 /// Store the specified session.
 /// </summary>
 /// <param name="session">The session to store</param>
 public override void Store(ISqlMapSession session)
 {
     StaticSessions[sessionName] = session;
 }
Пример #31
0
 /// <summary>
 /// Runs a query with a custom object that gets a chance
 /// to deal with each row as it is processed.
 /// </summary>
 /// <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"></param>
 /// <returns>A hashtable of object containing the rows keyed by keyProperty.</returns>
 /// <exception cref="IBatisNet.DataMapper.Exceptions.DataMapperException">If a transaction is not in progress, or the database throws an exception.</exception>
 public IDictionary <K, V> ExecuteQueryForDictionary <K, V>(ISqlMapSession session, object parameterObject, string keyProperty, string valueProperty, DictionaryRowDelegate <K, V> rowDelegate)
 {
     return(_mappedStatement.ExecuteQueryForDictionary <K, V>(session, parameterObject, keyProperty, valueProperty, rowDelegate));
 }
Пример #32
0
 /// <summary>
 /// Runs a query with a custom object that gets a chance 
 /// to deal with each row as it is processed.
 /// </summary>
 /// <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"></param>
 /// <returns>A hashtable 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>
 public override IDictionary ExecuteQueryForMapWithRowDelegate(ISqlMapSession session, object parameterObject, string keyProperty, string valueProperty, DictionaryRowDelegate rowDelegate)
 {
     throw new DataMapperException("Update statement cannot be executed as a query for row delegate.");
 }
        /// <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
        }
Пример #34
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <param name="resultObject"></param>
 public override void ExecuteQueryForList(ISqlMapSession session, object parameterObject, IList resultObject)
 {
     throw new DataMapperException("Update statements cannot be executed as a query for list.");
 }
        private IDbDataParameter Search(ISqlMapSession session,IDbDataParameter[] parameters, ParameterProperty property, int index)
	    {
            if (property.ColumnName.Length>0)
            {
                for (int i = 0; i < parameters.Length; i++)
                {
                    string parameterName = parameters[i].ParameterName;
                    if (session.DataSource.DbProvider.UseParameterPrefixInParameter)
                    {
                        if (parameterName.StartsWith(session.DataSource.DbProvider.ParameterPrefix))
                        {
                           int prefixLength = session.DataSource.DbProvider.ParameterPrefix.Length;
                           parameterName = parameterName.Substring(prefixLength);
                        }
                    }
                    if (property.ColumnName.Equals(parameterName))
                    {
                        return parameters[i];
                    }
                }
                throw new IndexOutOfRangeException("The parameter '" + property.ColumnName + "' does not exist in the stored procedure '" +_statement.Id+"'. Check your parameterMap.");                
            }
            else
            {
                return parameters[index];
            }

	    }
Пример #36
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <param name="rowDelegate"></param>
 /// <returns></returns>
 public override IList ExecuteQueryForRowDelegate(ISqlMapSession session, object parameterObject, RowDelegate rowDelegate )
 {
     throw new DataMapperException("Insert statements cannot be executed as a query for row delegate.");
 }
Пример #37
0
        private static void RetrieveOutputParameters(RequestScope request, ISqlMapSession session, IDbCommand command, object result)
        {
            if (request.ParameterMap != null)
            {
                int count = request.ParameterMap.PropertiesList.Count;
                for (int i = 0; i < count; i++)
                {
                    IBatisNet.DataMapper.Configuration.ParameterMapping.ParameterProperty mapping = request.ParameterMap.GetProperty(i);
                    if (mapping.Direction == ParameterDirection.Output ||
                        mapping.Direction == ParameterDirection.InputOutput)
                    {
                        string parameterName = string.Empty;
                        if (session.DataSource.DbProvider.UseParameterPrefixInParameter == false)
                        {
                            parameterName = mapping.ColumnName;
                        }
                        else
                        {
                            parameterName = session.DataSource.DbProvider.ParameterPrefix +
                                            mapping.ColumnName;
                        }

                        if (mapping.TypeHandler == null)                        // Find the TypeHandler
                        {
                            lock (mapping)
                            {
                                if (mapping.TypeHandler == null)
                                {
                                    Type propertyType = ObjectProbe.GetMemberTypeForGetter(result, mapping.PropertyName);

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

                        // Fix IBATISNET-239
                        //"Normalize" System.DBNull parameters
                        IDataParameter dataParameter = (IDataParameter)command.Parameters[parameterName];
                        object         dbValue       = dataParameter.Value;

                        object value = null;

                        bool wasNull = (dbValue == DBNull.Value);
                        if (wasNull)
                        {
                            if (mapping.HasNullValue)
                            {
                                value = mapping.TypeHandler.ValueOf(mapping.GetAccessor.MemberType, mapping.NullValue);
                            }
                            else
                            {
                                value = mapping.TypeHandler.NullValue;
                            }
                        }
                        else
                        {
                            value = mapping.TypeHandler.GetDataBaseValue(dataParameter.Value, result.GetType());
                        }

                        request.IsRowDataFound = request.IsRowDataFound || (value != null);

                        request.ParameterMap.SetOutputParameter(ref result, mapping, value);
                    }
                }
            }
        }
Пример #38
0
 /// <summary>
 /// Begin
 /// </summary>
 public void BeginTransaction()
 {
     Session = sqlMapper.BeginTransaction();
 }
Пример #39
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <returns></returns>
 public override object ExecuteInsert(ISqlMapSession session, object parameterObject)
 {
     throw new DataMapperException("Update statements cannot be executed as a query insert.");
 }
Пример #40
0
 /// <summary>
 /// Runs a query with a custom object that gets a chance
 /// to deal with each row as it is processed.
 /// </summary>
 /// <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"></param>
 /// <returns>A hashtable 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>
 public override IDictionary ExecuteQueryForMapWithRowDelegate(ISqlMapSession session, object parameterObject, string keyProperty, string valueProperty, DictionaryRowDelegate rowDelegate)
 {
     throw new DataMapperException("Update statement cannot be executed as a query for row delegate.");
 }
Пример #41
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <param name="skipResults"></param>
 /// <param name="maxResults"></param>
 /// <returns></returns>
 public override IList ExecuteQueryForList(ISqlMapSession session, object parameterObject, int skipResults, int maxResults)
 {
     throw new DataMapperException("Update statements cannot be executed as a query for list.");
 }
Пример #42
0
 /// <summary>
 /// Store the specified session.
 /// </summary>
 /// <param name="session">The session to store</param>
 public override void Store(ISqlMapSession session)
 {
     CallContext.SetData(sessionName, session);
 }
Пример #43
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <returns></returns>
 public override int ExecuteUpdate(ISqlMapSession session, object parameterObject )
 {
     throw new DataMapperException("Insert statements cannot be executed as a update query.");
 }
Пример #44
0
 public override IList ExecuteQueryForList(ISqlMapSession session, object parameterObject)
 {
     throw new DataMapperException("Delete statements cannot be executed as a query for list.");
 }
Пример #45
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <param name="keyProperty"></param>
 /// <param name="valueProperty"></param>
 /// <returns></returns>
 public override IDictionary ExecuteQueryForMap(ISqlMapSession session, object parameterObject, string keyProperty, string valueProperty )
 {
     throw new DataMapperException("Insert statements cannot be executed as a query for map.");
 }
Пример #46
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <param name="skipResults"></param>
 /// <param name="maxResults"></param>
 /// <returns></returns>
 public override IList ExecuteQueryForList(ISqlMapSession session, object parameterObject, int skipResults, int maxResults)
 {
     throw new DataMapperException("Insert statements cannot be executed as a query for list.");
 }
Пример #47
0
        /// <summary>
        /// Builds a new <see cref="RequestScope"/> and the <see cref="IDbCommand"/> 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);

            _paramParser = new InlineParameterMapParser();

            string sqlStatement = Process(request, parameterObject);
            request.PreparedStatement = BuildPreparedStatement(session, request, sqlStatement);
            request.MappedStatement = mappedStatement;

            return request;
        }
Пример #48
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <param name="rowDelegate"></param>
 /// <returns></returns>
 public override IList ExecuteQueryForRowDelegate(ISqlMapSession session, object parameterObject, RowDelegate rowDelegate)
 {
     throw new DataMapperException("Insert statements cannot be executed as a query for row delegate.");
 }
Пример #49
0
 /// <summary>
 /// Store the specified session.
 /// </summary>
 /// <param name="session">The session to store</param>
 public abstract void Store(ISqlMapSession session);
Пример #50
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <returns></returns>
 public override object ExecuteQueryForObject(ISqlMapSession session, object parameterObject)
 {
     throw new DataMapperException("Insert statements cannot be executed as a query for object.");
 }
Пример #51
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);

                #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
        }
Пример #52
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <param name="keyProperty"></param>
 /// <param name="valueProperty"></param>
 /// <returns></returns>
 public override IDictionary ExecuteQueryForMap(ISqlMapSession session, object parameterObject, string keyProperty, string valueProperty)
 {
     throw new DataMapperException("Insert statements cannot be executed as a query for map.");
 }
Пример #53
0
 /// <summary>
 /// Store the specified session.
 /// </summary>
 /// <param name="session">The session to store</param>
 public override void Store(ISqlMapSession session)
 {
     HttpContext currentContext = ObtainSessionContext();
     currentContext.Items[sessionName] = session;
 }
Пример #54
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="session"></param>
 /// <param name="parameterObject"></param>
 /// <param name="resultObject"></param>
 public override void ExecuteQueryForList(ISqlMapSession session, object parameterObject, IList resultObject)
 {
     throw new DataMapperException("Insert statements cannot be executed as a query for list.");
 }