/// <summary> /// Gets the data to be set into a IDataParameter. /// </summary> /// <param name="mapping"></param> /// <param name="parameterObject"></param> public override object GetData(ParameterProperty mapping, object parameterObject) { if (mapping.IsComplexMemberName) { return ObjectProbe.GetMemberValue(parameterObject, mapping.PropertyName, DataExchangeFactory.AccessorFactory); } return parameterObject; }
/// <summary> /// Gets the data to be set into a IDataParameter. /// </summary> /// <param name="mapping"></param> /// <param name="parameterObject"></param> public override object GetData(ParameterProperty mapping, object parameterObject) { if (mapping.IsComplexMemberName || parameterClass!=parameterObject.GetType()) { return ObjectProbe.GetMemberValue(parameterObject, mapping.PropertyName, DataExchangeFactory.AccessorFactory); } return mapping.GetAccessor.Get(parameterObject); }
/// <summary> /// Determines whether the specified <see cref="System.Object"></see> is equal to the current <see cref="System.Object"></see>. /// </summary> /// <param name="obj">The <see cref="System.Object"></see> to compare with the current <see cref="System.Object"></see>.</param> /// <returns> /// true if the specified <see cref="System.Object"></see> is equal to the current <see cref="System.Object"></see>; otherwise, false. /// </returns> public override bool Equals(object obj) { //Check for null and compare run-time types. if (obj == null || GetType() != obj.GetType()) { return(false); } ParameterProperty p = (ParameterProperty)obj; return(PropertyName == p.PropertyName); }
/// <summary> /// Gets the data to be set into a IDataParameter. /// </summary> /// <param name="mapping"></param> /// <param name="parameterObject"></param> public override object GetData(ParameterProperty mapping, object parameterObject) { if (parameterObject!=null) { if (DataExchangeFactory.TypeHandlerFactory.IsSimpleType(parameterObject.GetType())) { return parameterObject; } return ObjectProbe.GetMemberValue(parameterObject, mapping.PropertyName, DataExchangeFactory.AccessorFactory); } return null; }
/// <summary> /// Set parameter value, replace the null value if any. /// </summary> /// <param name="mapping"></param> /// <param name="dataParameter"></param> /// <param name="parameterValue"></param> public void SetParameter(ParameterProperty mapping, IDataParameter dataParameter, object parameterValue) { object value = dataExchange.GetData(mapping, parameterValue); ITypeHandler typeHandler = mapping.TypeHandler; // Apply Null Value if (mapping.HasNullValue) { if (typeHandler.Equals(value, mapping.NullValue)) { value = null; } } typeHandler.SetParameter(dataParameter, value, mapping.DbType); }
/// <summary> /// Insert a ParameterProperty ine the ParameterProperty list at the specified index.. /// </summary> /// <param name="index"> /// The zero-based index at which ParameterProperty should be inserted. /// </param> /// <param name="property">The ParameterProperty to insert. </param> public void InsertParameterProperty(int index, ParameterProperty property) { if (property.Direction == ParameterDirection.Output || property.Direction == ParameterDirection.InputOutput) { HasOutputParameter = true; } // These mappings will replace any mappings that this map // had for any of the keys currently in the specified map. propertiesMap[property.PropertyName] = property; properties.Insert(index, property); if (propertiesList.Contains(property) == false) { propertiesList.Insert(index, property); } }
/// <summary> /// Set parameter value, replace the null value if any. /// </summary> /// <param name="mapping"></param> /// <param name="dataParameter"></param> /// <param name="parameterValue"></param> public void SetParameter(ParameterProperty mapping, IDataParameter dataParameter, object parameterValue) { object value = dataExchange.GetData(mapping, parameterValue); ITypeHandler typeHandler = mapping.TypeHandler; // Apply Null Value if (mapping.HasNullValue) { if (typeHandler.Equals(value, mapping.NullValue)) { value = null; } } typeHandler.SetParameter(dataParameter, value, mapping.DbType); }
/// <summary> /// Set output parameter value. /// </summary> /// <param name="target">The target.</param> /// <param name="mapping">The mapping.</param> /// <param name="dataBaseValue">The data base value.</param> public void SetOutputParameter(ref object target, ParameterProperty mapping, object dataBaseValue ) { dataExchange.SetData(ref target, mapping, dataBaseValue); }
/// <summary> /// Insert a ParameterProperty ine the ParameterProperty list at the specified index.. /// </summary> /// <param name="index"> /// The zero-based index at which ParameterProperty should be inserted. /// </param> /// <param name="property">The ParameterProperty to insert. </param> public void InsertParameterProperty(int index, ParameterProperty property) { if (property.Direction == ParameterDirection.Output || property.Direction == ParameterDirection.InputOutput) { HasOutputParameter = true; } // These mappings will replace any mappings that this map // had for any of the keys currently in the specified map. propertiesMap[property.PropertyName] = property; properties.Insert( index, property ); if (propertiesList.Contains(property) == false) { propertiesList.Insert( index, property ); } }
/// <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 abstract void SetData(ref object target, ParameterProperty mapping, object dataBaseValue);
/// <summary> /// Gets the data to be set into a IDataParameter. /// </summary> /// <param name="mapping"></param> /// <param name="parameterObject"></param> public abstract object GetData(ParameterProperty mapping, object parameterObject);
/// <summary> /// Set output parameter value. /// </summary> /// <param name="target">The target.</param> /// <param name="mapping">The mapping.</param> /// <param name="dataBaseValue">The data base value.</param> public void SetOutputParameter(ref object target, ParameterProperty mapping, object dataBaseValue) { dataExchange.SetData(ref target, mapping, dataBaseValue); }
/// <summary> /// Parse Inline ParameterMap /// 对statement insert update delete select 语句的参数进行分析 /// </summary> /// <param name="dataExchangeFactory">The data exchange factory.</param> /// <param name="statementId">The statement id.</param> /// <param name="statement">The statement.</param> /// <param name="sqlStatement">The SQL statement.</param> /// <returns>A new sql command text.</returns> public static SqlText ParseInlineParameterMap(DataExchangeFactory dataExchangeFactory, string statementId, IStatement statement, string sqlStatement) { string newSql = sqlStatement; //赋值得到一条SQL部分语句 List <ParameterProperty> mappingList = new List <ParameterProperty>(); Type parameterClassType = null; if (statement != null) { parameterClassType = statement.ParameterClass; //当前节点属性中的参数类 会不会有多个参数类的情况??? } if (sqlStatement.Contains(NEW_BEGIN_TOKEN))//如果SQL语句包含"@{" { // V3 parameter syntax //@{propertyName,column=string,type=string,dbype=string,direction=[Input/Output/InputOutput],nullValue=string,handler=string} /*以@{开头的例子 * <procedure id="InsertAccountViaSPWithDynamicParameter" parameterClass="Account" > * ps_InsertAccountWithDefault * @{Id,column=Account_ID}//每一个@对应一个ParameterProperty类 * ,@{FirstName,column=Account_FirstName} * ,@{LastName,column=Account_LastName} * ,@{EmailAddress,column=Account_Email,[email protected]} * <isNotNull property="NullBannerOption"> * ,@{NullBannerOption,column=Account_Banner_Option,dbType=Varchar,type=bool} * </isNotNull> * @{CartOption,column=Account_Cart_Option,handler=HundredsBool} * </procedure> */ if (newSql != null) { string toAnalyse = newSql; int start = toAnalyse.IndexOf(NEW_BEGIN_TOKEN); //@{的下标 int end = toAnalyse.IndexOf(NEW_END_TOKEN); //"}"的下标 StringBuilder newSqlBuffer = new StringBuilder(); while (start > -1 && end > start) { //将语句以 @{ ****** }拆分成3份字符串 string prepend = toAnalyse.Substring(0, start); //@{部分 string append = toAnalyse.Substring(end + NEW_END_TOKEN.Length); //}.....部分 //EmailAddress,column=string,type=string,dbType=Varchar,[email protected] string parameter = toAnalyse.Substring(start + NEW_BEGIN_TOKEN.Length, end - start - NEW_BEGIN_TOKEN.Length);//第一个中间内容部分 ParameterProperty mapping = NewParseMapping(parameter, parameterClassType, dataExchangeFactory, statementId); mappingList.Add(mapping); //将解析后得到的ParameterProperty放入列表中 newSqlBuffer.Append(prepend); //将”@{"加入 或者是 "},@{"部分加入 newSqlBuffer.Append(MARK_TOKEN); //加入 "?"标志 //可能有多个部分 所以循环处理准备 toAnalyse = append; start = toAnalyse.IndexOf(NEW_BEGIN_TOKEN); end = toAnalyse.IndexOf(NEW_END_TOKEN); } /*while 循环完成后的形势是 * ps_InsertAccountWithDefault * @{?},@{?}的大概格式 保存到newSql中 */ newSqlBuffer.Append(toAnalyse); newSql = newSqlBuffer.ToString(); } } else { #region old syntax /* * <insert id="InsertAccountViaInlineParameters" parameterClass="Account" > * insert into Accounts * (Account_ID, Account_FirstName, Account_LastName, Account_Email) * values * (#Id#, #FirstName#, #LastName#, #EmailAddress:VarChar:[email protected]#) * </insert> */ //每# #之间就是一个ParameterProperty类 StringTokenizer parser = new StringTokenizer(sqlStatement, PARAMETER_TOKEN, true);//"#"区分 true表示 如果有#则返回 StringBuilder newSqlBuffer = new StringBuilder(); string token = null; string lastToken = null; IEnumerator enumerator = parser.GetEnumerator(); while (enumerator.MoveNext()) //获取当前符号到下一符号之间的字符串 { token = (string)enumerator.Current; //Current真正保存了获取当前符号到下一符号之间的字符串 或 # if (PARAMETER_TOKEN.Equals(lastToken)) //如果上一次是# { // Double token ## = # //先处理# if (PARAMETER_TOKEN.Equals(token)) //如果当前也是# { newSqlBuffer.Append(PARAMETER_TOKEN); token = null; } else //如果当前不是# 也即是两个#之间的字符串 { ParameterProperty mapping = null; //判断# #之间的内容是否含有“:” if (token.IndexOf(PARAM_DELIM) > -1) //如果字符串中含有”:"符号 则进入该函数 { mapping = OldParseMapping(token, parameterClassType, dataExchangeFactory); } else //不含有":",则再次默认","为分隔符 { mapping = NewParseMapping(token, parameterClassType, dataExchangeFactory, statementId); } mappingList.Add(mapping); newSqlBuffer.Append(MARK_TOKEN + " ");//添加"?"作为标志符号 enumerator.MoveNext(); token = (string)enumerator.Current; if (!PARAMETER_TOKEN.Equals(token)) { throw new DataMapperException("Unterminated inline parameter in mapped statement (" + statementId + ")."); } token = null; } } else { if (!PARAMETER_TOKEN.Equals(token)) { newSqlBuffer.Append(token); } } lastToken = token; } /*处理后的sql语句格式应该为 * insert into Accounts * (Account_ID, Account_FirstName, Account_LastName, Account_Email) * values * (?, ?, ?, ?) *?对应的参数保存在 mappingList中 *将这两部分 放到SqlText类中 */ newSql = newSqlBuffer.ToString(); #endregion } //将List内容转化为数组的形式 ParameterProperty[] mappingArray = mappingList.ToArray(); //将处理后的简化SQL语句 和 参数列表 保存到 SqlText中 SqlText sqlText = new SqlText(); sqlText.Text = newSql; sqlText.Parameters = mappingArray; return(sqlText); }
/// <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, DataExchangeFactory.ObjectFactory, DataExchangeFactory.AccessorFactory); }
private IDbDataParameter Search(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 (dbProvider.UseParameterPrefixInParameter) { if (parameterName.StartsWith(dbProvider.ParameterPrefix)) { int prefixLength = 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."); } return parameters[index]; }
/// <summary> /// Adds the parameter mapping. /// </summary> /// <param name="mapping">The mapping.</param> public void AddParameterMapping(ParameterProperty mapping) { _parameterMappings.Add(mapping); }
/// <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) { target = dataBaseValue; }
/// <summary> /// Parse Inline ParameterMap /// </summary> /// <param name="dataExchangeFactory">The data exchange factory.</param> /// <param name="statementId">The statement id.</param> /// <param name="statement">The statement.</param> /// <param name="sqlStatement">The SQL statement.</param> /// <returns>A new sql command text.</returns> internal static SqlText ParseInlineParameterMap(DataExchangeFactory dataExchangeFactory, string statementId, IStatement statement, string sqlStatement) { string newSql = sqlStatement; List <ParameterProperty> mappingList = new List <ParameterProperty>(); Type parameterClassType = null; if (statement != null) { parameterClassType = statement.ParameterClass; } if (sqlStatement.Contains(NEW_BEGIN_TOKEN)) { // V3 parameter syntax //@{propertyName,column=string,type=string,dbtype=string,direction=[Input/Output/InputOutput],nullValue=string,handler=string} if (newSql != null) { string toAnalyse = newSql; int start = toAnalyse.IndexOf(NEW_BEGIN_TOKEN); int end = toAnalyse.IndexOf(NEW_END_TOKEN); StringBuilder newSqlBuffer = new StringBuilder(); while (start > -1 && end > start) { string prepend = toAnalyse.Substring(0, start); string append = toAnalyse.Substring(end + NEW_END_TOKEN.Length); //EmailAddress,column=string,type=string,dbType=Varchar,[email protected] string parameter = toAnalyse.Substring(start + NEW_BEGIN_TOKEN.Length, end - start - NEW_BEGIN_TOKEN.Length); ParameterProperty mapping = NewParseMapping(parameter, parameterClassType, dataExchangeFactory, statementId); mappingList.Add(mapping); newSqlBuffer.Append(prepend); newSqlBuffer.Append(MARK_TOKEN); toAnalyse = append; start = toAnalyse.IndexOf(NEW_BEGIN_TOKEN); end = toAnalyse.IndexOf(NEW_END_TOKEN); } newSqlBuffer.Append(toAnalyse); newSql = newSqlBuffer.ToString(); } } else { #region old syntax StringTokenizer parser = new StringTokenizer(sqlStatement, PARAMETER_TOKEN, true); StringBuilder newSqlBuffer = new StringBuilder(); string token = null; string lastToken = null; IEnumerator enumerator = parser.GetEnumerator(); while (enumerator.MoveNext()) { token = (string)enumerator.Current; if (PARAMETER_TOKEN.Equals(lastToken)) { // Double token ## = # if (PARAMETER_TOKEN.Equals(token)) { newSqlBuffer.Append(PARAMETER_TOKEN); token = null; } else { ParameterProperty mapping = null; if (token.IndexOf(PARAM_DELIM) > -1) { mapping = OldParseMapping(token, parameterClassType, dataExchangeFactory); } else { mapping = NewParseMapping(token, parameterClassType, dataExchangeFactory, statementId); } mappingList.Add(mapping); newSqlBuffer.Append(MARK_TOKEN + " "); enumerator.MoveNext(); token = (string)enumerator.Current; if (!PARAMETER_TOKEN.Equals(token)) { throw new DataMapperException("Unterminated inline parameter in mapped statement (" + statementId + ")."); } token = null; } } else { if (!PARAMETER_TOKEN.Equals(token)) { newSqlBuffer.Append(token); } } lastToken = token; } newSql = newSqlBuffer.ToString(); #endregion old syntax } ParameterProperty[] mappingArray = mappingList.ToArray(); SqlText sqlText = new SqlText(); sqlText.Text = newSql; sqlText.Parameters = mappingArray; return(sqlText); }
/// <summary> /// Adds the parameter mapping. /// </summary> /// <param name="mapping">The mapping.</param> private void AddParameterMapping(ParameterProperty mapping) { _parameterMappings.Add(mapping.Clone()); }
/// <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) { if (mapping.IsComplexMemberName) { ObjectProbe.SetMemberValue(target, mapping.PropertyName, dataBaseValue, DataExchangeFactory.ObjectFactory, DataExchangeFactory.AccessorFactory); } else { ISetAccessorFactory setAccessorFactory = DataExchangeFactory.AccessorFactory.SetAccessorFactory; ISetAccessor _setAccessor = setAccessorFactory.CreateSetAccessor(parameterClass, mapping.PropertyName); _setAccessor.Set(target, dataBaseValue); } }