Beispiel #1
0
        internal DbCommand Create(ScopedContext scopedContext, QueryOptions queryOptions, object[] parameters)
        {
            if (scopedContext.ContextProvider != contextProvider)
            {
                throw new ArgumentException($"{nameof(scopedContext.ContextProvider)} presented in received {nameof(ScopedContext)} does not match "
                                            + $"stored in given {nameof(SimpleCommand)}",
                                            nameof(scopedContext));
            }

            var cmd = scopedContext.CreateCommand(CommandText);

            cmd.CommandType = CommandType;

            for (int i = 0; i < parameters.Length; i++)
            {
                if (parameters[i] is DbParameter)
                {
                    cmd.Parameters.Add(parameters[i]);
                }
                else
                {
                    contextProvider.AddParamWithValue(cmd, "@p" + i.ToString(), (object)parameters[i] ?? DBNull.Value);
                }
            }

            contextProvider.ApplyQueryOptions(cmd, scopedContext.PrepareQueryOptions(queryOptions));

            return(cmd);
        }
Beispiel #2
0
        internal DbCommand Create(ScopedContext scopedContext, T value, QueryOptions queryOptions)
        {
            if (scopedContext.ContextProvider != contextProvider)
            {
                throw new ArgumentException($"{nameof(scopedContext.ContextProvider)} presented in received {nameof(ScopedContext)} does not match "
                                            + $"stored in given {nameof(MappedCommand<T>)}",
                                            nameof(scopedContext));
            }

            var cmd = scopedContext.CreateCommand(CommandText);

            cmd.CommandType = CommandType;

            for (int i = 0; i < map.Length; i++)
            {
                var    m = map[i];
                object paramValue;
                if (m.Settings.DetermineNullByValue)
                {
                    var val = m.Settings.Getter(value);
                    paramValue = val == null?contextProvider.GetDbNull(m.Settings.FieldType) : val;
                }
                else
                {
                    paramValue = m.Settings.IsNullGetter(value) ? contextProvider.GetDbNull(m.Settings.FieldType) : m.Settings.Getter(value);
                }

                if (paramValue is DbParameter)
                {
                    cmd.Parameters.Add(paramValue);
                }
                else
                {
                    contextProvider.AddParamWithValue(cmd, m.ParameterName, paramValue);
                }
            }

            contextProvider.ApplyQueryOptions(cmd, scopedContext.PrepareQueryOptions(queryOptions));

            return(cmd);
        }