=> dbType != EnumerableMultiParameter;     // just in case called with non-nullable

        /// <summary>
        /// Add all the parameters needed to the command just before it executes
        /// </summary>
        /// <param name="command">The raw command prior to execution</param>
        /// <param name="identity">Information about the query</param>
        protected void AddParameters(IDbCommand command, SqlMapper.Identity identity)
        {
            var literals = SqlMapper.GetLiteralTokens(identity.sql);

            if (templates != null)
            {
                foreach (var template in templates)
                {
                    var newIdent = identity.ForDynamicParameters(template.GetType());
                    Action <IDbCommand, object> appender;

                    lock (paramReaderCache)
                    {
                        if (!paramReaderCache.TryGetValue(newIdent, out appender))
                        {
                            appender = SqlMapper.CreateParamInfoGenerator(newIdent, true, RemoveUnused, literals);
                            paramReaderCache[newIdent] = appender;
                        }
                    }

                    appender(command, template);
                }

                // The parameters were added to the command, but not the
                // DynamicParameters until now.
                foreach (IDbDataParameter param in command.Parameters)
                {
                    // If someone makes a DynamicParameters with a template,
                    // then explicitly adds a parameter of a matching name,
                    // it will already exist in 'parameters'.
                    if (!parameters.ContainsKey(param.ParameterName))
                    {
                        parameters.Add(param.ParameterName, new ParamInfo
                        {
                            AttachedParam      = param,
                            CameFromTemplate   = true,
                            DbType             = param.DbType,
                            Name               = param.ParameterName,
                            ParameterDirection = param.Direction,
                            Size               = param.Size,
                            Value              = param.Value
                        });
                    }
                }

                // Now that the parameters are added to the command, let's place our output callbacks
                var tmp = outputCallbacks;
                if (tmp != null)
                {
                    foreach (var generator in tmp)
                    {
                        generator();
                    }
                }
            }

            foreach (var param in parameters.Values)
            {
                if (param.CameFromTemplate)
                {
                    continue;
                }

                var    dbType = param.DbType;
                var    val    = param.Value;
                string name   = Clean(param.Name);
                var    isCustomQueryParameter = val is SqlMapper.ICustomQueryParameter;

                SqlMapper.ITypeHandler handler = null;
                if (dbType == null && val != null && !isCustomQueryParameter)
                {
#pragma warning disable 618
                    dbType = SqlMapper.LookupDbType(val.GetType(), name, true, out handler);
#pragma warning disable 618
                }
                if (isCustomQueryParameter)
                {
                    ((SqlMapper.ICustomQueryParameter)val).AddParameter(command, name);
                }
                else if (dbType == EnumerableMultiParameter)
                {
#pragma warning disable 612, 618
                    SqlMapper.PackListParameters(command, name, val);
#pragma warning restore 612, 618
                }
                else
                {
                    bool             add = !command.Parameters.Contains(name);
                    IDbDataParameter p;
                    if (add)
                    {
                        p = command.CreateParameter();
                        p.ParameterName = name;
                    }
                    else
                    {
                        p = (IDbDataParameter)command.Parameters[name];
                    }

                    p.Direction = param.ParameterDirection;
                    if (handler == null)
                    {
#pragma warning disable 0618
                        p.Value = SqlMapper.SanitizeParameterValue(val);
#pragma warning restore 0618
                        if (ShouldSetDbType(dbType) && p.DbType != dbType.GetValueOrDefault())
                        {
                            p.DbType = dbType.GetValueOrDefault();
                        }
                        var s = val as string;
                        if (s?.Length <= DbString.DefaultLength)
                        {
                            p.Size = DbString.DefaultLength;
                        }
                        if (param.Size != null)
                        {
                            p.Size = param.Size.Value;
                        }
                        if (param.Precision != null)
                        {
                            p.Precision = param.Precision.Value;
                        }
                        if (param.Scale != null)
                        {
                            p.Scale = param.Scale.Value;
                        }
                    }
                    else
                    {
                        if (ShouldSetDbType(dbType))
                        {
                            p.DbType = dbType.GetValueOrDefault();
                        }
                        if (param.Size != null)
                        {
                            p.Size = param.Size.Value;
                        }
                        if (param.Precision != null)
                        {
                            p.Precision = param.Precision.Value;
                        }
                        if (param.Scale != null)
                        {
                            p.Scale = param.Scale.Value;
                        }
                        handler.SetValue(p, val ?? DBNull.Value);
                    }

                    if (add)
                    {
                        command.Parameters.Add(p);
                    }
                    param.AttachedParam = p;
                }
            }

            // note: most non-privileged implementations would use: this.ReplaceLiterals(command);
            if (literals.Count != 0)
            {
                SqlMapper.ReplaceLiterals(this, command, literals);
            }
        }
Пример #2
0
        // Token: 0x0600003A RID: 58 RVA: 0x00002DA0 File Offset: 0x00000FA0
        protected void AddParameters(IDbCommand command, SqlMapper.Identity identity)
        {
            IList <SqlMapper.LiteralToken> literalTokens = SqlMapper.GetLiteralTokens(identity.sql);

            if (this.templates != null)
            {
                foreach (object obj in this.templates)
                {
                    SqlMapper.Identity identity2 = identity.ForDynamicParameters(obj.GetType());
                    Dictionary <SqlMapper.Identity, Action <IDbCommand, object> > obj2 = DynamicParameters.paramReaderCache;
                    Action <IDbCommand, object> action;
                    lock (obj2)
                    {
                        if (!DynamicParameters.paramReaderCache.TryGetValue(identity2, out action))
                        {
                            action = SqlMapper.CreateParamInfoGenerator(identity2, true, this.RemoveUnused, literalTokens);
                            DynamicParameters.paramReaderCache[identity2] = action;
                        }
                    }
                    action(command, obj);
                }
                foreach (object obj3 in command.Parameters)
                {
                    IDbDataParameter dbDataParameter = (IDbDataParameter)obj3;
                    if (!this.parameters.ContainsKey(dbDataParameter.ParameterName))
                    {
                        this.parameters.Add(dbDataParameter.ParameterName, new DynamicParameters.ParamInfo
                        {
                            AttachedParam      = dbDataParameter,
                            CameFromTemplate   = true,
                            DbType             = new DbType?(dbDataParameter.DbType),
                            Name               = dbDataParameter.ParameterName,
                            ParameterDirection = dbDataParameter.Direction,
                            Size               = new int?(dbDataParameter.Size),
                            Value              = dbDataParameter.Value
                        });
                    }
                }
                List <Action> list = this.outputCallbacks;
                if (list != null)
                {
                    foreach (Action action2 in list)
                    {
                        action2();
                    }
                }
            }
            foreach (DynamicParameters.ParamInfo paramInfo in this.parameters.Values)
            {
                if (!paramInfo.CameFromTemplate)
                {
                    DbType?dbType = paramInfo.DbType;
                    object value  = paramInfo.Value;
                    string text   = DynamicParameters.Clean(paramInfo.Name);
                    bool   flag2  = value is SqlMapper.ICustomQueryParameter;
                    SqlMapper.ITypeHandler typeHandler = null;
                    if (dbType == null && value != null && !flag2)
                    {
                        dbType = new DbType?(SqlMapper.LookupDbType(value.GetType(), text, true, out typeHandler));
                    }
                    if (flag2)
                    {
                        ((SqlMapper.ICustomQueryParameter)value).AddParameter(command, text);
                    }
                    else if (dbType == (DbType)(-1))
                    {
                        SqlMapper.PackListParameters(command, text, value);
                    }
                    else
                    {
                        bool             flag3 = !command.Parameters.Contains(text);
                        IDbDataParameter dbDataParameter2;
                        if (flag3)
                        {
                            dbDataParameter2 = command.CreateParameter();
                            dbDataParameter2.ParameterName = text;
                        }
                        else
                        {
                            dbDataParameter2 = (IDbDataParameter)command.Parameters[text];
                        }
                        dbDataParameter2.Direction = paramInfo.ParameterDirection;
                        if (typeHandler == null)
                        {
                            dbDataParameter2.Value = SqlMapper.SanitizeParameterValue(value);
                            if (dbType != null && dbDataParameter2.DbType != dbType)
                            {
                                dbDataParameter2.DbType = dbType.Value;
                            }
                            string text2 = value as string;
                            if (text2 != null && text2.Length <= 4000)
                            {
                                dbDataParameter2.Size = 4000;
                            }
                            if (paramInfo.Size != null)
                            {
                                dbDataParameter2.Size = paramInfo.Size.Value;
                            }
                            if (paramInfo.Precision != null)
                            {
                                dbDataParameter2.Precision = paramInfo.Precision.Value;
                            }
                            if (paramInfo.Scale != null)
                            {
                                dbDataParameter2.Scale = paramInfo.Scale.Value;
                            }
                        }
                        else
                        {
                            if (dbType != null)
                            {
                                dbDataParameter2.DbType = dbType.Value;
                            }
                            if (paramInfo.Size != null)
                            {
                                dbDataParameter2.Size = paramInfo.Size.Value;
                            }
                            if (paramInfo.Precision != null)
                            {
                                dbDataParameter2.Precision = paramInfo.Precision.Value;
                            }
                            if (paramInfo.Scale != null)
                            {
                                dbDataParameter2.Scale = paramInfo.Scale.Value;
                            }
                            typeHandler.SetValue(dbDataParameter2, value ?? DBNull.Value);
                        }
                        if (flag3)
                        {
                            command.Parameters.Add(dbDataParameter2);
                        }
                        paramInfo.AttachedParam = dbDataParameter2;
                    }
                }
            }
            if (literalTokens.Count != 0)
            {
                SqlMapper.ReplaceLiterals(this, command, literalTokens);
            }
        }