/// <summary> /// Create IDataParameters for procedure statement. /// </summary> private void CreateParametersForProcedureCommand() { string sqlParamName = string.Empty; string dbTypePropertyName = dbProvider.ParameterDbTypeProperty; Type enumDbType = dbProvider.ParameterDbType; ParameterPropertyCollection list = null; if (dbProvider.UsePositionalParameters) //obdc/oledb { list = request.ParameterMap.Properties; } else { list = request.ParameterMap.PropertiesList; } preparedStatement.DbParameters = new IDbDataParameter[list.Count]; // ParemeterMap are required for procedure and we tested existance in Prepare() method // so we don't have to test existence here. // A ParameterMap used in CreateParametersForProcedureText must // have property and column attributes set. // The column attribute is the name of a procedure parameter. for (int i = 0; i < list.Count; i++) { ParameterProperty property = list[i]; if (dbProvider.UseParameterPrefixInParameter) { sqlParamName = parameterPrefix + property.ColumnName; } else //obdc/oledb { sqlParamName = property.ColumnName; } IDbDataParameter dataParameter = dbProvider.CreateCommand().CreateParameter(); // Manage dbType attribute if any if (!string.IsNullOrEmpty(property.DbType)) { // Exemple : Enum.parse(System.Data.SqlDbType, 'VarChar') object dbType = Enum.Parse(enumDbType, property.DbType, true); // Exemple : ObjectHelper.SetProperty(sqlparameter, 'SqlDbType', SqlDbType.Int); ObjectProbe.SetMemberValue(dataParameter, dbTypePropertyName, dbType, request.DataExchangeFactory.ObjectFactory, request.DataExchangeFactory.AccessorFactory); } // Set IDbDataParameter // JIRA-49 Fixes (size, precision, and scale) if (dbProvider.SetDbParameterSize) { if (property.Size != -1) { dataParameter.Size = property.Size; } } if (dbProvider.SetDbParameterPrecision) { dataParameter.Precision = property.Precision; } if (dbProvider.SetDbParameterScale) { dataParameter.Scale = property.Scale; } // Set as direction parameter dataParameter.Direction = property.Direction; dataParameter.ParameterName = sqlParamName; preparedStatement.DbParametersName.Add(property.PropertyName); preparedStatement.DbParameters[i] = dataParameter; if (dbProvider.UsePositionalParameters == false) { propertyDbParameterMap.Add(property, dataParameter); } } }
/// <summary> /// Sets the value to the parameter property. /// </summary> /// <remarks>Use to set value on output parameter</remarks> /// <param name="mapping"></param> /// <param name="target"></param> /// <param name="dataBaseValue"></param> public override void SetData(ref object target, ParameterProperty mapping, object dataBaseValue) { ObjectProbe.SetMemberValue(target, mapping.PropertyName, dataBaseValue, this.DataExchangeFactory.ObjectFactory, this.DataExchangeFactory.AccessorFactory); }
/// <summary> /// Create IDataParameters for command text statement. /// </summary> private void CreateParametersForTextCommand() { string sqlParamName = string.Empty; string dbTypePropertyName = dbProvider.ParameterDbTypeProperty; Type enumDbType = dbProvider.ParameterDbType; ParameterPropertyCollection list = null; if (dbProvider.UsePositionalParameters) //obdc/oledb { list = request.ParameterMap.Properties; } else { list = request.ParameterMap.PropertiesList; } preparedStatement.DbParameters = new IDbDataParameter[list.Count]; for (int i = 0; i < list.Count; i++) { ParameterProperty property = list[i]; if (dbProvider.UseParameterPrefixInParameter) { // From Ryan Yao: JIRA-27, used "param" + i++ for sqlParamName sqlParamName = parameterPrefix + "param" + i; } else { sqlParamName = "param" + i; } IDbDataParameter dataParameter = dbProvider.CreateDataParameter(); // Manage dbType attribute if any if (!string.IsNullOrEmpty(property.DbType)) { // Exemple : Enum.parse(System.Data.SqlDbType, 'VarChar') object dbType = Enum.Parse(enumDbType, property.DbType, true); // Exemple : ObjectHelper.SetProperty(sqlparameter, 'SqlDbType', SqlDbType.Int); ObjectProbe.SetMemberValue(dataParameter, dbTypePropertyName, dbType, request.DataExchangeFactory.ObjectFactory, request.DataExchangeFactory.AccessorFactory); } // Set IDbDataParameter // JIRA-49 Fixes (size, precision, and scale) if (dbProvider.SetDbParameterSize) { if (property.Size != -1) { dataParameter.Size = property.Size; } } if (dbProvider.SetDbParameterPrecision) { dataParameter.Precision = property.Precision; } if (dbProvider.SetDbParameterScale) { dataParameter.Scale = property.Scale; } // Set as direction parameter dataParameter.Direction = property.Direction; dataParameter.ParameterName = sqlParamName; preparedStatement.DbParametersName.Add(property.PropertyName); preparedStatement.DbParameters[i] = dataParameter; if (dbProvider.UsePositionalParameters == false) { propertyDbParameterMap.Add(property, dataParameter); } } }
/// <summary> /// Applies the parameter map. /// </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) { 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 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); parameterCopy.ParameterName = sqlParameter.ParameterName; 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 }
/// <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> /// 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 virtual object ExecuteInsert(ISession session, object parameterObject) { return(Execute(PreInsertEventKey, PostInsertEventKey, session, parameterObject, (r, p) => { #region RunInsert object generatedKey = null; SelectKey selectKeyStatement = null; if (statement is Insert) { selectKeyStatement = ((Insert)statement).SelectKey; } if (selectKeyStatement != null && !selectKeyStatement.isAfter) { IMappedStatement mappedStatement = modelStore.GetMappedStatement(selectKeyStatement.Id); generatedKey = mappedStatement.ExecuteQueryForObject(session, p, null); ObjectProbe.SetMemberValue(p, selectKeyStatement.PropertyName, generatedKey, r.DataExchangeFactory.ObjectFactory, r.DataExchangeFactory.AccessorFactory); } preparedCommand.Create(r, session, Statement, p); using (IDbCommand command = r.IDbCommand) { if (statement is Insert) { command.ExecuteNonQuery(); } // Retrieve output parameter if the result class is specified else if (statement is Procedure && (statement.ResultClass != null) && modelStore.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(statement.ResultClass)) { IDataParameter returnValueParameter = command.CreateParameter(); returnValueParameter.Direction = ParameterDirection.ReturnValue; command.Parameters.Add(returnValueParameter); command.ExecuteNonQuery(); generatedKey = returnValueParameter.Value; ITypeHandler typeHandler = modelStore.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(statement.ResultClass); generatedKey = typeHandler.GetDataBaseValue(generatedKey, statement.ResultClass); } else { generatedKey = command.ExecuteScalar(); if ((statement.ResultClass != null) && modelStore.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(statement.ResultClass)) { ITypeHandler typeHandler = modelStore.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(statement.ResultClass); generatedKey = typeHandler.GetDataBaseValue(generatedKey, statement.ResultClass); } } if (selectKeyStatement != null && selectKeyStatement.isAfter) { IMappedStatement mappedStatement = modelStore.GetMappedStatement(selectKeyStatement.Id); generatedKey = mappedStatement.ExecuteQueryForObject(session, p, null); ObjectProbe.SetMemberValue(p, selectKeyStatement.PropertyName, generatedKey, r.DataExchangeFactory.ObjectFactory, r.DataExchangeFactory.AccessorFactory); } RetrieveOutputParameters(r, session, command, p); } // ??? return generatedKey; #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> /// 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; #region Logging if (_logger.IsDebugEnabled) { _paramLogList.Remove(0, _paramLogList.Length); _typeLogList.Remove(0, _typeLogList.Length); } #endregion 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 }