private static string BuildInsertQuery(IStatement statement) { StringBuilder builder = new StringBuilder(); Insert insert = (Insert)statement; int count = statement.ParameterMap.PropertiesList.Count; builder.Append("INSERT INTO " + insert.Generate.Table + " ("); for (int i = 0; i < count; i++) { ParameterProperty property = statement.ParameterMap.PropertiesList[i]; if (i < (count - 1)) { builder.Append("\t" + property.ColumnName + ","); } else { builder.Append("\t" + property.ColumnName + ""); } } builder.Append(") VALUES ("); for (int j = 0; j < count; j++) { ParameterProperty property1 = statement.ParameterMap.PropertiesList[j]; if (j < (count - 1)) { builder.Append("\t?,"); } else { builder.Append("\t?"); } } builder.Append(")"); return(builder.ToString()); }
private void CreateParameter(AnimationTreeParameter parameter) { string type = "float"; if (parameter.Type == AnimationTreeParameterType.Integer) { type = "int"; } string initialValue = parameter.DefaultValue.ToString(); if (parameter.Type == AnimationTreeParameterType.Float) { initialValue += "f"; } Variable v = new Variable(type, parameter.Name, initialValue); Add(v); ParameterProperty p = new ParameterProperty(type, parameter.Name, Variable.GetName(parameter.Name), _Tree) { Comment = parameter.Comment, Modifier = Modifiers.Public, SubMethod = SubMethod.None }; Add(p); }
/// <summary>Shows the Messaging application.</summary> public void Show() { if (!ChooserHelper.NavigationInProgressGuard(delegate { this.Show(); } )) { return; } ParameterPropertyBag parameterPropertyBag = new ParameterPropertyBag(); string to = this.To; if (!string.IsNullOrEmpty(to)) { ParameterProperty parameterProperty = parameterPropertyBag.CreateProperty("To"); parameterProperty.StringValue = to; } string body = this.Body; if (!string.IsNullOrEmpty(body)) { ParameterProperty parameterProperty2 = parameterPropertyBag.CreateProperty("Body"); parameterProperty2.StringValue = body; } ParameterProperty parameterProperty3 = parameterPropertyBag.CreateProperty("Account"); parameterProperty3.StringValue = "{FD39DA85-C18F-4c0c-AEAC-75867CEA7876}"; Uri appUri = new Uri("app://5B04B775-356B-4AA0-AAF8-6491FFEA5614/ShareContent", UriKind.Absolute); ChooserHelper.Navigate(appUri, parameterPropertyBag); }
private void EvaluateParameterMap() { string delimiters = "?"; string current = null; int num = 0; string parameterName = string.Empty; StringTokenizer tokenizer = new StringTokenizer(this._commandText, delimiters, true); StringBuilder builder = new StringBuilder(); IEnumerator enumerator = tokenizer.GetEnumerator(); while (enumerator.MoveNext()) { current = (string)enumerator.Current; if (delimiters.Equals(current)) { ParameterProperty property = this._request.ParameterMap.Properties[num]; IDataParameter parameter = null; if (this._session.DataSource.DbProvider.UsePositionalParameters) { if (this._parameterPrefix.Equals(":")) { parameterName = ":" + num; } else { parameterName = "?"; } } else { parameter = (IDataParameter)this._propertyDbParameterMap[property]; if (this._session.DataSource.DbProvider.UseParameterPrefixInParameter) { if (this._session.DataSource.DbProvider.Name.IndexOf("ByteFx") >= 0) { parameterName = this._parameterPrefix + parameter.ParameterName; } else { parameterName = parameter.ParameterName; } } else { parameterName = this._parameterPrefix + parameter.ParameterName; } } builder.Append(" "); builder.Append(parameterName); parameterName = string.Empty; num++; } else { builder.Append(current); } } this._preparedStatement.PreparedSql = builder.ToString(); }
public override object GetData(ParameterProperty mapping, object parameterObject) { if (!mapping.IsComplexMemberName && (this._parameterClass == parameterObject.GetType())) { return(mapping.GetAccessor.Get(parameterObject)); } return(ObjectProbe.GetMemberValue(parameterObject, mapping.PropertyName, base.DataExchangeFactory.AccessorFactory)); }
public override object GetData(ParameterProperty mapping, object parameterObject) { if (mapping.IsComplexMemberName) { return(ObjectProbe.GetMemberValue(parameterObject, mapping.PropertyName, base.DataExchangeFactory.AccessorFactory)); } return(parameterObject); }
/// <summary> /// Creates an select SQL command text for a specified statement /// </summary> /// <param name="statement">The statement to build the SQL command text.</param> /// <returns>The SQL command text for the statement.</returns> private static string BuildSelectQuery(IStatement statement) { StringBuilder output = new StringBuilder(); Select select = (Select)statement; int columnCount = statement.ParameterMap.PropertiesList.Count; output.Append("SELECT "); // Create the list of columns for (int i = 0; i < columnCount; i++) { ParameterProperty property = (ParameterProperty)statement.ParameterMap.PropertiesList[i]; if (i < (columnCount - 1)) { output.Append("\t" + property.ColumnName + " as " + property.PropertyName + ","); } else { output.Append("\t" + property.ColumnName + " as " + property.PropertyName); } } output.Append(" FROM "); output.Append("\t" + select.Generate.Table + ""); // Create the where clause string [] compositeKeyList = select.Generate.By.Split(new Char [] { ',' }); if (compositeKeyList.Length > 0 && select.Generate.By.Length > 0) { output.Append(" WHERE "); int length = compositeKeyList.Length; for (int i = 0; i < length; i++) { string columnName = compositeKeyList[i]; if (i > 0) { output.Append("\tAND " + columnName + " = ?"); } else { output.Append("\t" + columnName + " = ?"); } } } // 'Select All' case if (statement.ParameterClass == null) { // The ParameterMap is just used to build the query // to avoid problems later, we set it to null statement.ParameterMap = null; } return(output.ToString()); }
private void CreateParametersForTextCommand() { string str = string.Empty; string parameterDbTypeProperty = this._session.DataSource.DbProvider.ParameterDbTypeProperty; Type parameterDbType = this._session.DataSource.DbProvider.ParameterDbType; ParameterPropertyCollection properties = null; if (this._session.DataSource.DbProvider.UsePositionalParameters) { properties = this._request.ParameterMap.Properties; } else { properties = this._request.ParameterMap.PropertiesList; } this._preparedStatement.DbParameters = new IDbDataParameter[properties.Count]; for (int i = 0; i < properties.Count; i++) { ParameterProperty key = properties[i]; if (this._session.DataSource.DbProvider.UseParameterPrefixInParameter) { str = this._parameterPrefix + "param" + i; } else { str = "param" + i; } IDbDataParameter parameter = this._session.CreateDataParameter(); if ((key.DbType != null) && (key.DbType.Length > 0)) { object memberValue = Enum.Parse(parameterDbType, key.DbType, true); ObjectProbe.SetMemberValue(parameter, parameterDbTypeProperty, memberValue, this._request.DataExchangeFactory.ObjectFactory, this._request.DataExchangeFactory.AccessorFactory); } if (this._session.DataSource.DbProvider.SetDbParameterSize && (key.Size != -1)) { parameter.Size = key.Size; } if (this._session.DataSource.DbProvider.SetDbParameterPrecision) { parameter.Precision = key.Precision; } if (this._session.DataSource.DbProvider.SetDbParameterScale) { parameter.Scale = key.Scale; } parameter.Direction = key.Direction; parameter.ParameterName = str; this._preparedStatement.DbParametersName.Add(key.PropertyName); this._preparedStatement.DbParameters[i] = parameter; if (!this._session.DataSource.DbProvider.UsePositionalParameters) { this._propertyDbParameterMap.Add(key, parameter); } } }
public override void SetData(ref object target, ParameterProperty mapping, object dataBaseValue) { if (mapping.IsComplexMemberName) { ObjectProbe.SetMemberValue(target, mapping.PropertyName, dataBaseValue, base.DataExchangeFactory.ObjectFactory, base.DataExchangeFactory.AccessorFactory); } else { base.DataExchangeFactory.AccessorFactory.SetAccessorFactory.CreateSetAccessor(this._parameterClass, mapping.PropertyName).Set(target, dataBaseValue); } }
private 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++) { ParameterProperty mapping = request.ParameterMap.GetProperty(i); if ((mapping.Direction == ParameterDirection.Output) || (mapping.Direction == ParameterDirection.InputOutput)) { string columnName = string.Empty; if (!session.DataSource.DbProvider.UseParameterPrefixInParameter) { columnName = mapping.ColumnName; } else { columnName = session.DataSource.DbProvider.ParameterPrefix + mapping.ColumnName; } if (mapping.TypeHandler == null) { lock (mapping) { if (mapping.TypeHandler == null) { Type memberTypeForGetter = ObjectProbe.GetMemberTypeForGetter(result, mapping.PropertyName); mapping.TypeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(memberTypeForGetter); } } } IDataParameter parameter = (IDataParameter)command.Parameters[columnName]; object obj2 = parameter.Value; object dataBaseValue = null; if (obj2 == DBNull.Value) { if (mapping.HasNullValue) { dataBaseValue = mapping.TypeHandler.ValueOf(mapping.GetAccessor.MemberType, mapping.NullValue); } else { dataBaseValue = mapping.TypeHandler.NullValue; } } else { dataBaseValue = mapping.TypeHandler.GetDataBaseValue(parameter.Value, result.GetType()); } request.IsRowDataFound = request.IsRowDataFound || (dataBaseValue != null); request.ParameterMap.SetOutputParameter(ref result, mapping, dataBaseValue); } } } }
/// <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> /// Creates an update SQL command text for a specified statement /// </summary> /// <param name="statement">The statement to build the SQL command text.</param> /// <returns>The SQL command text for the statement.</returns> private static string BuildUpdateQuery(IStatement statement) { StringBuilder output = new StringBuilder(); Update update = (Update)statement; int columnCount = statement.ParameterMap.PropertiesList.Count; string[] keysList = update.Generate.By.Split(','); output.Append("UPDATE "); output.Append("\t" + update.Generate.Table + " "); output.Append("SET "); // Create the set statement for (int i = 0; i < columnCount; i++) { ParameterProperty property = (ParameterProperty)statement.ParameterMap.PropertiesList[i]; // Ignore key columns if (update.Generate.By.IndexOf(property.ColumnName) < 0) { if (i < (columnCount - keysList.Length - 1)) { output.Append("\t" + property.ColumnName + " = ?,"); } else { output.Append("\t" + property.ColumnName + " = ? "); } } } output.Append(" WHERE "); // Create the where clause int length = keysList.Length; for (int i = 0; i < length; i++) { string columnName = keysList[i]; if (i > 0) { output.Append("\tAND " + columnName + " = ?"); } else { output.Append("\t " + columnName + " = ?"); } } return(output.ToString()); }
/// <summary> /// Creates an insert SQL command text for a specified statement /// </summary> /// <param name="statement">The statement to build the SQL command text.</param> /// <returns>The SQL command text for the statement.</returns> private static string BuildInsertQuery(IStatement statement) { StringBuilder output = new StringBuilder(); Insert insert = (Insert)statement; int columnCount = statement.ParameterMap.PropertiesList.Count; output.Append("INSERT INTO " + insert.Generate.Table + " ("); // Create the parameter list for (int i = 0; i < columnCount; i++) { ParameterProperty property = (ParameterProperty)statement.ParameterMap.PropertiesList[i]; // Append the column name as a parameter of the insert statement if (i < (columnCount - 1)) { output.Append("\t" + property.ColumnName + ","); } else { output.Append("\t" + property.ColumnName + ""); } } output.Append(") VALUES ("); // Create the values list for (int i = 0; i < columnCount; i++) { ParameterProperty property = (ParameterProperty)statement.ParameterMap.PropertiesList[i]; // Append the necessary line breaks and commas if (i < (columnCount - 1)) { output.Append("\t?,"); } else { output.Append("\t?"); } } output.Append(")"); return(output.ToString()); }
private static string BuildSelectQuery(IStatement statement) { StringBuilder builder = new StringBuilder(); Select select = (Select)statement; int count = statement.ParameterMap.PropertiesList.Count; builder.Append("SELECT "); for (int i = 0; i < count; i++) { ParameterProperty property = statement.ParameterMap.PropertiesList[i]; if (i < (count - 1)) { builder.Append("\t" + property.ColumnName + " as " + property.PropertyName + ","); } else { builder.Append("\t" + property.ColumnName + " as " + property.PropertyName); } } builder.Append(" FROM "); builder.Append("\t" + select.Generate.Table + ""); string[] strArray = select.Generate.By.Split(new char[] { ',' }); if ((strArray.Length > 0) && (select.Generate.By.Length > 0)) { builder.Append(" WHERE "); int length = strArray.Length; for (int j = 0; j < length; j++) { string str = strArray[j]; if (j > 0) { builder.Append("\tAND " + str + " = ?"); } else { builder.Append("\t" + str + " = ?"); } } } if (statement.ParameterClass == null) { statement.ParameterMap = null; } return(builder.ToString()); }
/// <summary>Shows the Web Search application.</summary> public void Show() { if (!ChooserHelper.NavigationInProgressGuard(delegate { this.Show(); } )) { return; } ParameterPropertyBag parameterPropertyBag = new ParameterPropertyBag(); ParameterProperty parameterProperty = parameterPropertyBag.CreateProperty("QueryString"); parameterProperty.StringValue = this.SearchQuery; Uri appUri = new Uri("app://5B04B775-356B-4AA0-AAF8-6491FFEA5661/SearchResults", UriKind.Absolute); ChooserHelper.Navigate(appUri, parameterPropertyBag); }
public static ParameterProperty Deserialize(XmlNode node, ConfigurationScope configScope) { ParameterProperty property = new ParameterProperty(); NameValueCollection attributes = NodeUtils.ParseAttributes(node, configScope.Properties); configScope.ErrorContext.MoreInfo = "ParameterPropertyDeSerializer"; property.CallBackName = NodeUtils.GetStringAttribute(attributes, "typeHandler"); property.CLRType = NodeUtils.GetStringAttribute(attributes, "type"); property.ColumnName = NodeUtils.GetStringAttribute(attributes, "column"); property.DbType = NodeUtils.GetStringAttribute(attributes, "dbType", null); property.DirectionAttribute = NodeUtils.GetStringAttribute(attributes, "direction"); property.NullValue = attributes["nullValue"]; property.PropertyName = NodeUtils.GetStringAttribute(attributes, "property"); property.Precision = NodeUtils.GetByteAttribute(attributes, "precision", 0); property.Scale = NodeUtils.GetByteAttribute(attributes, "scale", 0); property.Size = NodeUtils.GetIntAttribute(attributes, "size", -1); return(property); }
private static string BuildUpdateQuery(IStatement statement) { StringBuilder builder = new StringBuilder(); Update update = (Update)statement; int count = statement.ParameterMap.PropertiesList.Count; string[] strArray = update.Generate.By.Split(new char[] { ',' }); builder.Append("UPDATE "); builder.Append("\t" + update.Generate.Table + " "); builder.Append("SET "); for (int i = 0; i < count; i++) { ParameterProperty property = statement.ParameterMap.PropertiesList[i]; if (update.Generate.By.IndexOf(property.ColumnName) < 0) { if (i < ((count - strArray.Length) - 1)) { builder.Append("\t" + property.ColumnName + " = ?,"); } else { builder.Append("\t" + property.ColumnName + " = ? "); } } } builder.Append(" WHERE "); int length = strArray.Length; for (int j = 0; j < length; j++) { string str = strArray[j]; if (j > 0) { builder.Append("\tAND " + str + " = ?"); } else { builder.Append("\t " + str + " = ?"); } } return(builder.ToString()); }
/// <summary> /// Builds the parameter properties. /// </summary> /// <param name="parameterMap">The parameter map.</param> /// <param name="parameterMapConfig">The parameter map config.</param> private void BuildParameterProperties(ParameterMap parameterMap, IConfiguration parameterMapConfig) { ConfigurationCollection parametersConfig = parameterMapConfig.Children.Find(ConfigConstants.ELEMENT_PARAMETER); for (int i = 0; i < parametersConfig.Count; i++) { IConfiguration parameterConfig = parametersConfig[i]; ParameterProperty property = null; try { property = ParameterPropertyDeSerializer.Deserialize(modelStore.DataExchangeFactory, parameterMap.Class, parameterConfig); } catch (Exception e) { throw new DataMapperException("In ParameterMap (" + parameterMap.Id + ") can't build the parameter property: " + ConfigurationUtils.GetStringAttribute(parameterConfig.Attributes, ConfigConstants.ATTRIBUTE_PROPERTY) + ". Cause " + e.Message, e); } parameterMap.AddParameterProperty(property); } }
/// <summary>Shows the contacts application.</summary> public override void Show() { if (!ChooserHelper.NavigationInProgressGuard(delegate { this.Show(); } )) { return; } ParameterPropertyBag parameterPropertyBag = new ParameterPropertyBag(); ParameterProperty parameterProperty = parameterPropertyBag.CreateProperty("PhoneNumberToSave"); parameterProperty.StringValue = this.PhoneNumber; byte[] buffer = ChooserHelper.Serialize(parameterPropertyBag); Uri appUri = new Uri("app://5B04B775-356B-4AA0-AAF8-6491FFEA5615/SaveANumberToAddressBook", UriKind.Absolute); base.Show(); ChooserHelper.Invoke(appUri, buffer, this._genericChooser); }
internal override void OnInvokeReturned(byte[] outputBuffer, Delegate fireThisHandlerOnly) { bool flag = false; if (outputBuffer.Length > 0) { ParameterPropertyBag parameterPropertyBag = new ParameterPropertyBag(outputBuffer, (uint)outputBuffer.Length); ParameterProperty property = parameterPropertyBag.GetProperty("SelectedFilePath"); if (property.ValueType == ParameterPropertyValueType.ValueType_String && !string.IsNullOrEmpty(property.StringValue)) { flag = true; PhotoResult photoResult = ChooserHelper.PathParameterToPhotoResult(property); photoResult.TaskResult = TaskResult.OK; base.FireCompleted(this, photoResult, fireThisHandlerOnly); } } if (!flag) { base.FireCompleted(this, new PhotoResult(TaskResult.Cancel), fireThisHandlerOnly); } }
internal override void OnInvokeReturned(byte[] outputBuffer, Delegate fireThisHandlerOnly) { bool flag = false; if (outputBuffer.Length > 0) { ParameterPropertyBag parameterPropertyBag = new ParameterPropertyBag(outputBuffer, (uint)outputBuffer.Length); ParameterProperty property = parameterPropertyBag.GetProperty("PickerPropertyValue"); if (property.ValueType == ParameterPropertyValueType.ValueType_String && !string.IsNullOrEmpty(property.StringValue)) { flag = true; base.FireCompleted(this, new PhoneNumberResult(TaskResult.OK) { PhoneNumber = property.StringValue }, fireThisHandlerOnly); } } if (!flag) { base.FireCompleted(this, new PhoneNumberResult(TaskResult.Cancel), fireThisHandlerOnly); } }
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> /// 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> /// Retrieve the output parameter and map them on the result object. /// This routine is only use is you specified a ParameterMap and some output attribute /// or if you use a store procedure with output parameter... /// </summary> /// <param name="request"></param> /// <param name="session">The current session.</param> /// <param name="result">The result object.</param> /// <param name="command">The command sql.</param> private static void RetrieveOutputParameters(RequestScope request, ISession session, IDbCommand command, object result) { if (request.ParameterMap != null && request.ParameterMap.HasOutputParameter) { int count = request.ParameterMap.PropertiesList.Count; for (int i = 0; i < count; i++) { ParameterProperty mapping = request.ParameterMap.GetProperty(i); if (mapping.Direction == ParameterDirection.Output || mapping.Direction == ParameterDirection.InputOutput) { string parameterName = string.Empty; if (session.SessionFactory.DataSource.DbProvider.UseParameterPrefixInParameter == false) { parameterName = mapping.ColumnName; } else { parameterName = session.SessionFactory.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); } } } }
/// <summary> /// Parse sql command text. /// </summary> private void EvaluateParameterMap() { string delimiter = "?"; string token = null; int index = 0; string sqlParamName = string.Empty; StringTokenizer parser = new StringTokenizer(_commandText, delimiter, true); StringBuilder newCommandTextBuffer = new StringBuilder(); IEnumerator enumerator = parser.GetEnumerator(); while (enumerator.MoveNext()) { token = (string)enumerator.Current; if (delimiter.Equals(token)) // ? { ParameterProperty property = _request.ParameterMap.Properties[index]; IDataParameter dataParameter = null; if (_session.DataSource.DbProvider.UsePositionalParameters) { // TODO Refactor? if (_parameterPrefix.Equals(":")) { // ODP.NET uses positional parameters by default // but uses ":0" or ":1" instead of "?" sqlParamName = ":" + index; } else { // OLEDB/OBDC doesn't support named parameters !!! sqlParamName = "?"; } } else { dataParameter = (IDataParameter)_propertyDbParameterMap[property]; // 5 May 2004 // Need to check UseParameterPrefixInParameter here // since CreateParametersForStatementText now does // a check for UseParameterPrefixInParameter before // creating the parameter name! if (_session.DataSource.DbProvider.UseParameterPrefixInParameter) { // Fix ByteFX.Data.MySqlClient.MySqlParameter // who strip prefix in Parameter Name ?! if (_session.DataSource.DbProvider.Name.IndexOf("ByteFx") >= 0) { sqlParamName = _parameterPrefix + dataParameter.ParameterName; } else { sqlParamName = dataParameter.ParameterName; } } else { sqlParamName = _parameterPrefix + dataParameter.ParameterName; } } newCommandTextBuffer.Append(" "); newCommandTextBuffer.Append(sqlParamName); sqlParamName = string.Empty; index++; } else { newCommandTextBuffer.Append(token); } } _preparedStatement.PreparedSql = newCommandTextBuffer.ToString(); }
/// <summary> /// Create IDataParameters for procedure statement. /// </summary> private void CreateParametersForProcedureCommand() { string sqlParamName = string.Empty; string dbTypePropertyName = _session.DataSource.DbProvider.ParameterDbTypeProperty; Type enumDbType = _session.DataSource.DbProvider.ParameterDbType; ParameterPropertyCollection list = null; if (_session.DataSource.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 (_session.DataSource.DbProvider.UseParameterPrefixInParameter) { sqlParamName = _parameterPrefix + property.ColumnName; } else //obdc/oledb { sqlParamName = property.ColumnName; } IDbDataParameter dataParameter = _session.CreateCommand(_statement.CommandType).CreateParameter(); // Manage dbType attribute if any if (property.DbType != null && property.DbType.Length > 0) { // 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 (_session.DataSource.DbProvider.SetDbParameterSize) { if (property.Size != -1) { dataParameter.Size = property.Size; } } if (_session.DataSource.DbProvider.SetDbParameterPrecision) { dataParameter.Precision = property.Precision; } if (_session.DataSource.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 (_session.DataSource.DbProvider.UsePositionalParameters == false) { _propertyDbParameterMap.Add(property, dataParameter); } } }
/// <summary> /// Create IDataParameters for command text statement. /// </summary> private void CreateParametersForTextCommand() { string sqlParamName = string.Empty; string dbTypePropertyName = _session.DataSource.DbProvider.ParameterDbTypeProperty; Type enumDbType = _session.DataSource.DbProvider.ParameterDbType; ParameterPropertyCollection list = null; if (_session.DataSource.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 (_session.DataSource.DbProvider.UseParameterPrefixInParameter) { // From Ryan Yao: JIRA-27, used "param" + i++ for sqlParamName sqlParamName = _parameterPrefix + "param" + i; } else { sqlParamName = "param" + i; } IDbDataParameter dataParameter = _session.CreateDataParameter(); // Manage dbType attribute if any if (property.DbType != null && property.DbType.Length > 0) { // 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 (_session.DataSource.DbProvider.SetDbParameterSize) { if (property.Size != -1) { dataParameter.Size = property.Size; } } if (_session.DataSource.DbProvider.SetDbParameterPrecision) { dataParameter.Precision = property.Precision; } if (_session.DataSource.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 (_session.DataSource.DbProvider.UsePositionalParameters == false) { _propertyDbParameterMap.Add(property, dataParameter); } } }
public void AddParameterMapping(ParameterProperty mapping) { this._parameterMappings.Add(mapping); }
/// <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> /// /// </summary> /// <param name="session"></param> /// <param name="command"></param> /// <param name="request"></param> /// <param name="statement"></param> /// <param name="parameterObject"></param> protected override void ApplyParameterMap (IDalSession session, IDbCommand command, RequestScope request, IStatement statement, object parameterObject) { ArrayList properties = request.PreparedStatement.DbParametersName; ArrayList parameters = request.PreparedStatement.DbParameters; object parameterValue = null; int count = properties.Count; for (int i = 0; i < count; ++i) { IDataParameter sqlParameter = (IDataParameter)parameters[i]; string propertyName = (string)properties[i]; if (command.CommandType == CommandType.Text) { if (propertyName != "value") // Inline Parameters && Parameters via ParameterMap { // ParameterProperty property = request.ParameterMap.GetProperty(i); // parameterValue = request.ParameterMap.GetValueOfProperty(parameterObject, // property.PropertyName); } else // 'value' parameter { parameterValue = parameterObject; } } else // CommandType.StoredProcedure { // 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 { ParameterProperty property = request.ParameterMap.GetProperty(i); if (property.DirectionAttribute.Length == 0) { property.Direction = sqlParameter.Direction; } // IDbDataParameter dataParameter = (IDbDataParameter)parameters[i]; // property.Precision = dataParameter.Precision; // property.Scale = dataParameter.Scale; // property.Size = dataParameter.Size; sqlParameter.Direction = property.Direction; // parameterValue = request.ParameterMap.GetValueOfProperty(parameterObject, property.PropertyName); } } IDataParameter parameterCopy = command.CreateParameter(); // Fix JIRA 20 sqlParameter.Value = parameterValue; parameterCopy.Value = parameterValue; parameterCopy.Direction = sqlParameter.Direction; // With a ParameterMap, we could specify the ParameterDbTypeProperty if (statement.ParameterMap != null) { if (request.ParameterMap.GetProperty(i).DbType != null && request.ParameterMap.GetProperty(i).DbType.Length > 0) { string dbTypePropertyName = session.DataSource.DbProvider.ParameterDbTypeProperty; ObjectProbe.SetPropertyValue(parameterCopy, dbTypePropertyName, ObjectProbe.GetPropertyValue(sqlParameter, dbTypePropertyName)); } else { //parameterCopy.DbType = sqlParameter.DbType; } } else { //parameterCopy.DbType = sqlParameter.DbType; } // JIRA-49 Fixes (size, precision, and scale) if (session.DataSource.DbProvider.SetDbParameterSize) { if (((IDbDataParameter)sqlParameter).Size > 0) { ((IDbDataParameter)parameterCopy).Size = ((IDbDataParameter)sqlParameter).Size; } } if (session.DataSource.DbProvider.SetDbParameterPrecision) { ((IDbDataParameter)parameterCopy).Precision = ((IDbDataParameter)sqlParameter).Precision; } if (session.DataSource.DbProvider.SetDbParameterScale) { ((IDbDataParameter)parameterCopy).Scale = ((IDbDataParameter)sqlParameter).Scale; } parameterCopy.ParameterName = sqlParameter.ParameterName; command.Parameters.Add(parameterCopy); // NOTE: // Code from Oleksa Borodie to embed parameter values // into command text/sql statement. // NEED TO MERGE WITH ABOVE AFTER INITIAL TESTING // TO REMOVE REDUNDANT LOOPING! // replace parameter names with parameter values // only for parameters with names, parameterMaps will be ignored IDataParameter p; for (int iCnt = command.Parameters.Count - 1; iCnt >= 0; iCnt--) { p = (IDataParameter)command.Parameters[iCnt]; if (p.Direction == ParameterDirection.Input && command.CommandText.IndexOf(p.ParameterName) > 0) { switch (p.DbType) { case DbType.String: case DbType.AnsiString: case DbType.AnsiStringFixedLength: case DbType.StringFixedLength: command.CommandText = command.CommandText.Replace(p.ParameterName, "\'" + p.Value.ToString().Replace("\'", "\'\'") + "\'"); break; case DbType.Date: case DbType.DateTime: DateTime v = Convert.ToDateTime(p.Value); command.CommandText = command.CommandText.Replace(p.ParameterName, String.Format("\'{0}.{1}.{2} {3}:{4}:{5}.{6}\'", v.Year, v.Month, v.Day, v.Hour, v.Minute, v.Second, v.Millisecond)); // command.CommandText = command.CommandText.Replace(p.ParameterName, "\'" + p.Value.ToString() + "\'"); break; case DbType.Double: case DbType.Decimal: case DbType.Currency: case DbType.Single: command.CommandText = command.CommandText.Replace(p.ParameterName, p.Value.ToString().Replace(',', '.')); break; default: command.CommandText = command.CommandText.Replace(p.ParameterName, p.Value.ToString()); break; } command.Parameters.RemoveAt(iCnt); } } } }