private void AddParameterIfNotExists(IDbCommand dbCommand, string paramName, object paramVal, ref HashSet <string> parameters, ITypeHandler typeHandler = null) { if (parameters.Contains(paramName) == false) { IDbDataParameter cmdParameter = dbCommand.CreateParameter(); cmdParameter.ParameterName = paramName; if (typeHandler != null) { typeHandler.SetParameter(cmdParameter, paramVal); } else if (paramVal == null) { cmdParameter.Value = DBNull.Value; } else { //if (paramVal is Enum) //{ // paramVal = paramVal.GetHashCode(); //} cmdParameter.Value = paramVal; try { var dbtype = cmdParameter.DbType; } catch { cmdParameter.Value = paramVal?.ToString(); } } dbCommand.Parameters.Add(cmdParameter); parameters.Add(paramName); } }
private void AddParameterIfNotExists(RequestContext context, IDbCommand dbCommand, string paramName, object paramVal, ITypeHandler typeHandler = null) { if (!dbCommand.Parameters.Contains(paramName)) { var cmdParameter = dbCommand.CreateParameter(); cmdParameter.ParameterName = paramName; if (paramVal == null) { cmdParameter.Value = DBNull.Value; } else { if (typeHandler != null) { typeHandler.SetParameter(cmdParameter, paramVal); } else { if (paramVal is Enum) { paramVal = paramVal.GetHashCode(); } cmdParameter.Value = paramVal; } } dbCommand.Parameters.Add(cmdParameter); } }
private void AddDbParameter(IDbCommand dbCommand, DbParameter dbParameter, ITypeHandler typeHandler = null) { if (dbCommand.Parameters.Contains(dbParameter.Name)) { return; } var sourceParam = dbCommand.CreateParameter(); sourceParam.ParameterName = GetParameterName(dbParameter.Name); var paramVal = dbParameter.Value; if (paramVal == null) { sourceParam.Value = DBNull.Value; } else { if (typeHandler != null) { typeHandler.SetParameter(sourceParam, paramVal); } else { if (paramVal is Enum) { paramVal = paramVal.GetHashCode(); } sourceParam.Value = paramVal; } } dbParameter.SourceParameter = sourceParam; if (dbParameter.DbType.HasValue) { sourceParam.DbType = dbParameter.DbType.Value; } if (dbParameter.Direction.HasValue) { sourceParam.Direction = dbParameter.Direction.Value; } if (dbParameter.Precision.HasValue) { sourceParam.Precision = dbParameter.Precision.Value; } if (dbParameter.Scale.HasValue) { sourceParam.Scale = dbParameter.Scale.Value; } if (dbParameter.Size.HasValue) { sourceParam.Size = dbParameter.Size.Value; } dbCommand.Parameters.Add(sourceParam); }
public void SetParameter(ParameterProperty mapping, IDataParameter dataParameter, object parameterValue) { object data = this._dataExchange.GetData(mapping, parameterValue); ITypeHandler typeHandler = mapping.TypeHandler; if (mapping.HasNullValue && typeHandler.Equals(data, mapping.NullValue)) { data = null; } typeHandler.SetParameter(dataParameter, data, mapping.DbType); }
/// <summary> /// Performs processing on a value before it is used to set /// the parameter of a IDbCommand. /// </summary> /// <param name="dataParameter"></param> /// <param name="parameterValue">The value to be set</param> /// <param name="dbType">Data base type</param> public override void SetParameter(IDataParameter dataParameter, object parameterValue, string dbType) { if (parameterValue != null) { ITypeHandler handler = _factory.GetTypeHandler(parameterValue.GetType(), dbType); handler.SetParameter(dataParameter, parameterValue, dbType); } else { // When sending a null parameter value to the server, // the user must specify DBNull, not null. dataParameter.Value = System.DBNull.Value; } }
private void BuildDbParameters(AbstractRequestContext reqConetxt) { var dbParameterNames = _sqlParamAnalyzer.Analyse(reqConetxt.RealSql); if (reqConetxt.CommandType == CommandType.StoredProcedure) { foreach (var sqlParameter in reqConetxt.Parameters.Values) { var sourceParam = _dbProviderFactory.CreateParameter(); sourceParam.ParameterName = sqlParameter.Name; sourceParam.Value = sqlParameter.Value; sqlParameter.SourceParameter = sourceParam; InitSourceDbParameter(sourceParam, sqlParameter); } } else { foreach (var paramName in dbParameterNames) { var parameter = reqConetxt.ParameterMap?.GetParameter(paramName); var propertyName = paramName; ITypeHandler typeHandler = null; if (parameter != null) { propertyName = parameter.Property; typeHandler = parameter.Handler; } if (!reqConetxt.Parameters.TryGetValue(propertyName, out var sqlParameter)) { continue; } var sourceParam = _dbProviderFactory.CreateParameter(); sourceParam.ParameterName = paramName; if (typeHandler == null) { typeHandler = sqlParameter.TypeHandler ?? _typeHandlerFactory.GetTypeHandler(sqlParameter.ParameterType); } typeHandler.SetParameter(sourceParam, sqlParameter.Value); sqlParameter.SourceParameter = sourceParam; InitSourceDbParameter(sourceParam, sqlParameter); } } }
private void BuildDbParameters(AbstractRequestContext reqConetxt) { if (reqConetxt.CommandType == CommandType.StoredProcedure) { foreach (var sqlParameter in reqConetxt.Parameters.Values) { if (sqlParameter.SourceParameter != null) { sqlParameter.OnSetSourceParameter.Invoke(sqlParameter); continue; } var sourceParam = _dbProviderFactory.CreateParameter(); InitSourceDbParameter(sourceParam, sqlParameter); sourceParam.ParameterName = sqlParameter.Name; sourceParam.Value = sqlParameter.Value; sqlParameter.TypeHandler?.SetParameter(sourceParam, sqlParameter.Value); sqlParameter.SourceParameter = sourceParam; } } else { reqConetxt.RealSql = _sqlParamAnalyzer.Replace(reqConetxt.RealSql, (paramName, nameWithPrefix) => { if (!reqConetxt.Parameters.TryGetValue(paramName, out var sqlParameter)) { return(nameWithPrefix); } ITypeHandler typeHandler = (reqConetxt.ParameterMap?.GetParameter(paramName)?.Handler ?? sqlParameter.TypeHandler) ?? _typeHandlerFactory.GetTypeHandler(sqlParameter.ParameterType); var sourceParam = _dbProviderFactory.CreateParameter(); InitSourceDbParameter(sourceParam, sqlParameter); sourceParam.ParameterName = sqlParameter.Name; typeHandler.SetParameter(sourceParam, sqlParameter.Value); sqlParameter.SourceParameter = sourceParam; if (sqlParameter.Name != paramName) { return ($"{reqConetxt.ExecutionContext.SmartSqlConfig.Database.DbProvider.ParameterPrefix}{sqlParameter.Name}"); } return(nameWithPrefix); }); } }
/// <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); }