public MigrationCommandListBuilder([NotNull] IRelationalCommandBuilderFactory commandBuilderFactory)
        {
            Check.NotNull(commandBuilderFactory, nameof(commandBuilderFactory));

            _commandBuilderFactory = commandBuilderFactory;
            _commandBuilder = commandBuilderFactory.Create();
        }
        public virtual MigrationCommandListBuilder EndCommand(bool suppressTransaction = false)
        {
            if (_commandBuilder.GetLength() != 0)
            {
                _commands.Add(new MigrationCommand(_commandBuilder.Build(), suppressTransaction));
                _commandBuilder = _commandBuilderFactory.Create();
            }

            return this;
        }
        public virtual RelationalCommandListBuilder EndCommand()
        {
            if (_commandBuilder.GetLength() != 0)
            {
                _commands.Add(_commandBuilder.BuildRelationalCommand());
                _commandBuilder = _commandBuilderFactory.Create();
            }

            return this;
        }
        /// <summary>
        ///     Gets a relational command for a <see cref="SelectExpression"/>.
        /// </summary>
        /// <param name="selectExpression"> A select expression to print in command text. </param>
        /// <returns> A relational command with a SQL represented by the select expression. </returns>
        public virtual IRelationalCommand GetCommand([NotNull] SelectExpression selectExpression)
        {
            Check.NotNull(selectExpression, nameof(selectExpression));

            _relationalCommandBuilder = _relationalCommandBuilderFactory.Create();

            GenerateTagsHeaderComment(selectExpression);

            if (selectExpression.IsNonComposedFromSql())
            {
                GenerateFromSql((FromSqlExpression)selectExpression.Tables[0]);
            }
            else
            {
                VisitSelect(selectExpression);
            }

            return(_relationalCommandBuilder.Build());
        }
        public static IRelationalCommandBuilder AddParameter(
            [NotNull] this IRelationalCommandBuilder commandBuilder,
            [NotNull] string name,
            [CanBeNull] object value,
            [NotNull] IProperty property)
        {
            Check.NotNull(commandBuilder, nameof(commandBuilder));
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(property, nameof(property));

            commandBuilder.AddParameter(
                name,
                value,
                t => t.GetMapping(property),
                property.IsNullable,
                invariantName: null);

            return(commandBuilder);
        }
        /// <summary>
        ///     Adds a parameter.
        /// </summary>
        /// <param name="commandBuilder"> The command builder. </param>
        /// <param name="invariantName">
        ///     The key that identifies this parameter. Note that <see cref="IRelationalParameter" /> just represents a
        ///     placeholder for a parameter and not the actual value. This is because the same command can be
        ///     reused multiple times with different parameter values.
        /// </param>
        /// <param name="name">
        ///     The name to be used for the parameter when the command is executed against the database.
        /// </param>
        /// <param name="relationalTypeMapping"> The relational type mapping for this parameter. </param>
        /// <param name="nullable"> A value indicating whether the parameter could contain a null value. </param>
        /// <returns> The same builder instance so that multiple calls can be chained. </returns>
        public static IRelationalCommandBuilder AddParameter(
            [NotNull] this IRelationalCommandBuilder commandBuilder,
            [NotNull] string invariantName,
            [NotNull] string name,
            [NotNull] RelationalTypeMapping relationalTypeMapping,
            bool?nullable)
        {
            Check.NotNull(commandBuilder, nameof(commandBuilder));
            Check.NotEmpty(invariantName, nameof(invariantName));
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(relationalTypeMapping, nameof(relationalTypeMapping));

            return(commandBuilder.AddParameter(
                       new TypeMappedRelationalParameter(
                           invariantName,
                           name,
                           relationalTypeMapping,
                           nullable)));
        }
        /// <summary>
        ///     Adds a parameter that is ultimately represented as multiple <see cref="DbParameter" />s in the
        ///     final command.
        /// </summary>
        /// <param name="commandBuilder"> The command builder. </param>
        /// <param name="invariantName">
        ///     The key that identifies this parameter. Note that <see cref="IRelationalParameter" /> just represents a
        ///     placeholder for a parameter and not the actual value. This is because the same command can be
        ///     reused multiple times with different parameter values.
        /// </param>
        /// <param name="subParameters"> The parameters to include in the composite. </param>
        /// <returns> The same builder instance so that multiple calls can be chained. </returns>
        public static IRelationalCommandBuilder AddCompositeParameter(
            [NotNull] this IRelationalCommandBuilder commandBuilder,
            [NotNull] string invariantName,
            [NotNull] IReadOnlyList <IRelationalParameter> subParameters)
        {
            Check.NotNull(commandBuilder, nameof(commandBuilder));
            Check.NotEmpty(invariantName, nameof(invariantName));
            Check.NotNull(subParameters, nameof(subParameters));

            if (subParameters.Count > 0)
            {
                commandBuilder.AddParameter(
                    new CompositeRelationalParameter(
                        invariantName,
                        subParameters));
            }

            return(commandBuilder);
        }
        private IRelationalCommand CreateCommand(Object dataContext, String sql, IReadOnlyList <KeyValuePair <String, Object> > parameters, out Dictionary <String, Object> parameterValues)
        {
            var dbContext             = (DbContext)dataContext;
            var commandBuilderFactory = dbContext.GetService <IRelationalCommandBuilderFactory>();
            IRelationalCommandBuilder commandBuilder = commandBuilderFactory.Create();

            commandBuilder.Append(sql);

            var parameterNameGenerator = dbContext.GetService <IParameterNameGeneratorFactory>().Create();
            var sqlHelper = dbContext.GetService <ISqlGenerationHelper>();

            parameterValues = new Dictionary <String, Object>(parameters.Count);
            for (int i = 0; i < parameters.Count; i++)
            {
                String invariantName = parameterNameGenerator.GenerateNext();
                String name          = sqlHelper.GenerateParameterName(invariantName);

                commandBuilder.AddParameter(invariantName, name);
                parameterValues.Add(invariantName, GetParameterCore(parameters[i], name, i));
            }

            return(commandBuilder.Build());
        }
        /// <summary>
        ///     Appends an object, that contains multiple lines of text, to the command text.
        ///     Each line read from the object is appended on a new line.
        /// </summary>
        /// <param name="commandBuilder"> The command builder. </param>
        /// <param name="value"> The object to be written. </param>
        /// <param name="skipFinalNewline"> If <see langword="true" />, then the final newline character is skipped. </param>
        /// <returns> The same builder instance so that multiple calls can be chained. </returns>
        public static IRelationalCommandBuilder AppendLines(
            this IRelationalCommandBuilder commandBuilder,
            string value,
            bool skipFinalNewline = false)
        {
            Check.NotNull(commandBuilder, nameof(commandBuilder));
            Check.NotNull(value, nameof(value));

            using (var reader = new StringReader(value))
            {
                var    first = true;
                string?line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        commandBuilder.AppendLine();
                    }

                    if (line.Length != 0)
                    {
                        commandBuilder.Append(line);
                    }
                }
            }

            if (!skipFinalNewline)
            {
                commandBuilder.AppendLine();
            }

            return(commandBuilder);
        }
        public static IRelationalCommandBuilder AddParameter(
            [NotNull] this IRelationalCommandBuilder commandBuilder,
            [NotNull] string name,
            [CanBeNull] object value,
            [NotNull] Type type)
        {
            Check.NotNull(commandBuilder, nameof(commandBuilder));
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(type, nameof(type));

            bool?isNullable = null;

            if (type.IsNullableType())
            {
                isNullable = true;
                type       = type.UnwrapNullableType();
            }

            return(commandBuilder.AddParameter(
                       name,
                       value,
                       t => t.GetMapping(type),
                       isNullable));
        }
예제 #11
0
        public Indenter(IRelationalCommandBuilder builder)
        {
            _builder = builder;

            _builder.IncrementIndent();
        }
예제 #12
0
 /// <summary>
 ///     Adds a parameter.
 /// </summary>
 /// <param name="commandBuilder">The command builder.</param>
 /// <param name="invariantName">
 ///     The key that identifies this parameter. Note that <see cref="IRelationalParameter" /> just represents a
 ///     placeholder for a parameter and not the actual value. This is because the same command can be
 ///     reused multiple times with different parameter values.
 /// </param>
 /// <param name="dbParameter">The DbParameter being added.</param>
 /// <returns>The same builder instance so that multiple calls can be chained.</returns>
 public static IRelationalCommandBuilder AddRawParameter(
     this IRelationalCommandBuilder commandBuilder,
     string invariantName,
     DbParameter dbParameter)
 => commandBuilder.AddParameter(
     new RawRelationalParameter(invariantName, dbParameter));
 /// <summary>
 ///     Gets the length of the command text.
 /// </summary>
 /// <param name="commandBuilder"> The command builder. </param>
 /// <returns> The length of the command text. </returns>
 public static int GetLength([NotNull] this IRelationalCommandBuilder commandBuilder)
 => Check.NotNull(commandBuilder, nameof(commandBuilder)).Instance.Length;
 /// <summary>
 ///     Increases the indent of the command text.
 /// </summary>
 /// <param name="commandBuilder"> The command builder. </param>
 /// <returns> The same builder instance so that multiple calls can be chained. </returns>
 public static IDisposable Indent([NotNull] this IRelationalCommandBuilder commandBuilder)
 => Check.NotNull(commandBuilder, nameof(commandBuilder)).Instance.Indent();
 /// <summary>
 ///     Increases the indent of the command text.
 /// </summary>
 /// <param name="commandBuilder"> The command builder. </param>
 /// <returns> The same builder instance so that multiple calls can be chained. </returns>
 public static IDisposable Indent(this IRelationalCommandBuilder commandBuilder)
 => new Indenter(Check.NotNull(commandBuilder, nameof(commandBuilder)));
예제 #16
0
 /// <summary>
 ///     Increases the indent of the command text.
 /// </summary>
 /// <param name="commandBuilder">The command builder.</param>
 /// <returns>The same builder instance so that multiple calls can be chained.</returns>
 public static IDisposable Indent(this IRelationalCommandBuilder commandBuilder)
 => new Indenter(commandBuilder);
        public static Expression VisitSelect(IZackQuerySqlGenerator sqlGenerator, ISqlGenerationHelper _sqlGenerationHelper, SelectExpression selectExpression)
        {
            if (BatchUtils.IsNonComposedSetOperation(selectExpression))
            {
                sqlGenerator.P_GenerateSetOperation((SetOperationBase)selectExpression.Tables[0]);
                return(selectExpression);
            }

            IRelationalCommandBuilder Sql = sqlGenerator.P_Sql;

            IDisposable disposable = null;

            if (selectExpression.Alias != null)
            {
                Sql.AppendLine("(");
                disposable = sqlGenerator.P_Sql.Indent();
            }
            Sql.Append("SELECT ");
            if (selectExpression.IsDistinct)
            {
                Sql.Append("DISTINCT ");
            }
            sqlGenerator.P_GenerateTop(selectExpression);
            if (selectExpression.Projection.Any())
            {
                BatchUtils.GenerateList(selectExpression.Projection, Sql, delegate(ProjectionExpression e)
                {
                    var oldSQL = Sql.Build().CommandText;                             //zack's code
                    sqlGenerator.Visit(e);
                    string column = BatchUtils.Diff(oldSQL, Sql.Build().CommandText); //zack's code
                    sqlGenerator.ProjectionSQL.Add(column);                           //zack's code
                });
            }
            else
            {
                Sql.Append("1");
                sqlGenerator.ProjectionSQL.Add("1");                //zack's code
            }
            if (selectExpression.Tables.Any())
            {
                Sql.AppendLine().Append("FROM ");
                BatchUtils.GenerateList(selectExpression.Tables, Sql, delegate(TableExpressionBase e)
                {
                    sqlGenerator.Visit(e);
                }, delegate(IRelationalCommandBuilder sql)
                {
                    sql.AppendLine();
                });
            }
            else
            {
                sqlGenerator.P_GeneratePseudoFromClause();
            }
            if (selectExpression.Predicate != null)
            {
                Sql.AppendLine().Append("WHERE ");
                var oldSQL = Sql.Build().CommandText;                                         //zack's code
                sqlGenerator.Visit(selectExpression.Predicate);
                sqlGenerator.PredicateSQL = BatchUtils.Diff(oldSQL, Sql.Build().CommandText); //zack's code
            }
            if (selectExpression.GroupBy.Count > 0)
            {
                Sql.AppendLine().Append("GROUP BY ");
                BatchUtils.GenerateList(selectExpression.GroupBy, Sql, delegate(SqlExpression e)
                {
                    sqlGenerator.Visit(e);
                });
            }
            if (selectExpression.Having != null)
            {
                Sql.AppendLine().Append("HAVING ");
                sqlGenerator.Visit(selectExpression.Having);
            }
            sqlGenerator.P_GenerateOrderings(selectExpression);
            sqlGenerator.P_GenerateLimitOffset(selectExpression);
            if (selectExpression.Alias != null)
            {
                disposable.Dispose();
                Sql.AppendLine().Append(")" + sqlGenerator.P_AliasSeparator + _sqlGenerationHelper.DelimitIdentifier(selectExpression.Alias));
            }
            return(selectExpression);
        }
예제 #18
0
 public static IRelationalCommandBuilder AddParameter(
     this IRelationalCommandBuilder commandBuilder,
     string invariantName,
     string name)
 => throw new InvalidOperationException("Use overload which takes TypeMapping argument.");
예제 #19
0
 public MyRelationalCommandBuilderFactory(IRelationalCommandBuilder builder)
 {
     _builder = builder;
 }