Example #1
0
    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual string Create(DbCommand command)
    {
        if (command.Parameters.Count == 0)
        {
            return(command.CommandText);
        }

        var builder = new StringBuilder();

        foreach (DbParameter parameter in command.Parameters)
        {
            var typeName    = TypeNameBuilder.CreateTypeName(parameter);
            var typeMapping = _typeMapper.FindMapping(typeName);

            builder
            .Append("DECLARE ")
            .Append(parameter.ParameterName)
            .Append(' ')
            .Append(typeName)
            .Append(" = ")
            .Append(
                (parameter.Value == DBNull.Value ||
                 parameter.Value == null)
                        ? "NULL"
                        : parameter.Value is SqlBytes sqlBytes
                            ? new SqlServerByteArrayTypeMapping(typeName).GenerateSqlLiteral(sqlBytes.Value)
                            : typeMapping != null
                                ? typeMapping.GenerateSqlLiteral(parameter.Value)
                                : parameter.Value.ToString())
            .AppendLine(";");
        }

        return(builder
               .AppendLine()
               .Append(command.CommandText).ToString());
    }