Exemplo n.º 1
0
        /// <summary>
        ///     Appends the given object (as a string) to the command being built.
        /// </summary>
        /// <param name="o"> The object to append. </param>
        /// <returns> This builder so that additional calls can be chained. </returns>
        public virtual MigrationCommandListBuilder Append([NotNull] object o)
        {
            Check.NotNull(o, nameof(o));

            _commandBuilder.Append(o);

            return(this);
        }
Exemplo n.º 2
0
        protected virtual void GenerateTagsHeaderComment(SelectExpression selectExpression)
        {
            if (selectExpression.Tags.Count > 0)
            {
                foreach (var tag in selectExpression.Tags)
                {
                    using (var reader = new StringReader(tag))
                    {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            _relationalCommandBuilder.Append(SingleLineCommentToken).Append(" ").AppendLine(line);
                        }
                    }

                    _relationalCommandBuilder.AppendLine();
                }
            }
        }
        /// <summary>
        ///     Appends an object to the command text on a new line.
        /// </summary>
        /// <param name="commandBuilder"> The command builder. </param>
        /// <param name="value"> The object to be written. </param>
        /// <returns> The same builder instance so that multiple calls can be chained. </returns>
        public static IRelationalCommandBuilder AppendLine(
            this IRelationalCommandBuilder commandBuilder,
            string value)
        {
            Check.NotNull(commandBuilder, nameof(commandBuilder));
            Check.NotNull(value, nameof(value));

            commandBuilder.Append(value).AppendLine();

            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);
        }
        protected override Expression VisitSqlFragment(SqlFragmentExpression sqlFragmentExpression)
        {
            _relationalCommandBuilder.Append(sqlFragmentExpression.Sql);

            return(sqlFragmentExpression);
        }
        protected override Expression VisitSqlFragment(SqlFragmentExpression sqlFragmentExpression)
        {
            Check.NotNull(sqlFragmentExpression, nameof(sqlFragmentExpression));

            _relationalCommandBuilder.Append(sqlFragmentExpression.Sql);

            return(sqlFragmentExpression);
        }
        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);
        }
        /// <summary>
        /// Visit read only expression
        /// </summary>
        /// <param name="readOnlyExpression"></param>
        /// <returns></returns>
        public Expression VisitReadOnly([NotNull] ReadOnlyExpression expression)
        {
            Check.NotNull(expression, nameof(expression));

            // TODO: nested read only expression

            // reading clauses
            if (expression.ReadingClauses.Count > 0)
            {
                IterateGrammer(expression.ReadingClauses);
            }
            else
            {
                CreatePseudoMatchClause();
            }


            // return items
            _commandBuilder.Append(" RETURN ");
            var returnItemsAdded = false;

            if (expression.IsReturnStar)
            {
                _commandBuilder
                .Append(
                    SqlGenerator.DelimitIdentifier(
                        expression.ReturnStarNode.Alias
                        )
                    )
                .Append(".*");

                returnItemsAdded = true;
            }

            if (expression.ReturnItems.Count > 0)
            {
                if (expression.IsReturnStar)
                {
                    _commandBuilder.Append(", ");
                }

                // TODO: Optimization visitors
                IterateGrammer(expression.ReturnItems, s => s.Append(", "));

                returnItemsAdded = true;
            }

            if (!returnItemsAdded)
            {
                _commandBuilder.Append("1");
            }


            // TODO: Order, Skip, Limit
            return(expression);
        }