/// <summary> /// Visit match expression /// </summary> /// <param name="matchExpression"></param> /// <returns></returns> public Expression VisitMatch([NotNull] MatchExpression expression) { Check.NotNull(expression, nameof(expression)); var optional = expression.Optional ? "OPTIONAL " : String.Empty; _commandBuilder .Append($"{optional}MATCH "); Visit(expression.Pattern); if (!(expression.Where is null)) { // TODO: optimize where _commandBuilder .AppendLine() .Append("WHERE "); Visit(expression.Where); } return(expression); }
protected override Expression VisitSelect(SelectExpression selectExpression) { IDisposable subQueryIndent = null; if (selectExpression.Alias != null) { _relationalCommandBuilder.AppendLine("("); subQueryIndent = _relationalCommandBuilder.Indent(); } if (selectExpression.IsSetOperation) { GenerateSetOperation(selectExpression); } else { GenerateSelect(selectExpression); } if (selectExpression.Alias != null) { subQueryIndent.Dispose(); _relationalCommandBuilder.AppendLine() .Append(") AS " + _sqlGenerationHelper.DelimitIdentifier(selectExpression.Alias)); } return(selectExpression); }
/// <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) { 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); }
/// <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 <code>true</code>, 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( [NotNull] this IRelationalCommandBuilder commandBuilder, [NotNull] object value, bool skipFinalNewline = false) { Check.NotNull(commandBuilder, nameof(commandBuilder)); Check.NotNull(value, nameof(value)); using (var reader = new StringReader(value.ToString())) { 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 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(); } } }
protected override Expression VisitSelect(SelectExpression selectExpression) { Check.NotNull(selectExpression, nameof(selectExpression)); if (IsNonComposedSetOperation(selectExpression)) { // Naked set operation GenerateSetOperation((SetOperationBase)selectExpression.Tables[0]); return(selectExpression); } IDisposable subQueryIndent = null; if (selectExpression.Alias != null) { _relationalCommandBuilder.AppendLine("("); subQueryIndent = _relationalCommandBuilder.Indent(); } _relationalCommandBuilder.Append("SELECT "); if (selectExpression.IsDistinct) { _relationalCommandBuilder.Append("DISTINCT "); } GenerateTop(selectExpression); if (selectExpression.Projection.Any()) { GenerateList(selectExpression.Projection, e => Visit(e)); } else { _relationalCommandBuilder.Append("1"); } if (selectExpression.Tables.Any()) { _relationalCommandBuilder.AppendLine().Append("FROM "); GenerateList(selectExpression.Tables, e => Visit(e), sql => sql.AppendLine()); } else { GeneratePseudoFromClause(); } if (selectExpression.Predicate != null) { _relationalCommandBuilder.AppendLine().Append("WHERE "); Visit(selectExpression.Predicate); } if (selectExpression.GroupBy.Count > 0) { _relationalCommandBuilder.AppendLine().Append("GROUP BY "); GenerateList(selectExpression.GroupBy, e => Visit(e)); } if (selectExpression.Having != null) { _relationalCommandBuilder.AppendLine().Append("HAVING "); Visit(selectExpression.Having); } GenerateOrderings(selectExpression); GenerateLimitOffset(selectExpression); if (selectExpression.Alias != null) { subQueryIndent.Dispose(); _relationalCommandBuilder.AppendLine() .Append(")" + AliasSeparator + _sqlGenerationHelper.DelimitIdentifier(selectExpression.Alias)); } return(selectExpression); }
protected override Expression VisitSelect(SelectExpression selectExpression) { IDisposable subQueryIndent = null; if (!string.IsNullOrEmpty(selectExpression.Alias)) { _relationalCommandBuilder.AppendLine("("); subQueryIndent = _relationalCommandBuilder.Indent(); } _relationalCommandBuilder.Append("SELECT "); if (selectExpression.IsDistinct) { _relationalCommandBuilder.Append("DISTINCT "); } GenerateTop(selectExpression); if (selectExpression.Projection.Any()) { GenerateList(selectExpression.Projection, e => Visit(e)); } else { _relationalCommandBuilder.Append("1"); } if (selectExpression.Tables.Any()) { _relationalCommandBuilder.AppendLine() .Append("FROM "); GenerateList(selectExpression.Tables, e => Visit(e), sql => sql.AppendLine()); } if (selectExpression.Predicate != null) { _relationalCommandBuilder.AppendLine() .Append("WHERE "); Visit(selectExpression.Predicate); } if (selectExpression.Orderings.Any()) { var orderings = selectExpression.Orderings.ToList(); if (selectExpression.Limit == null && selectExpression.Offset == null) { orderings.RemoveAll(oe => oe.Expression is SqlConstantExpression || oe.Expression is SqlParameterExpression); } if (orderings.Count > 0) { _relationalCommandBuilder.AppendLine() .Append("ORDER BY "); GenerateList(orderings, e => Visit(e)); } } else if (selectExpression.Offset != null) { _relationalCommandBuilder.AppendLine() .Append("ORDER BY (SELECT 1)"); } GenerateLimitOffset(selectExpression); if (!string.IsNullOrEmpty(selectExpression.Alias)) { subQueryIndent.Dispose(); _relationalCommandBuilder.AppendLine() .Append(") AS " + _sqlGenerationHelper.DelimitIdentifier(selectExpression.Alias)); } return(selectExpression); }
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> /// Starts a new line on the command being built. /// </summary> /// <returns> This builder so that additional calls can be chained. </returns> public virtual MigrationCommandListBuilder AppendLine() { _commandBuilder.AppendLine(); return(this); }
public virtual RelationalCommandListBuilder AppendLine() { _commandBuilder.AppendLine(); return(this); }
/// <summary> /// 添加行 /// </summary> /// <returns></returns> public override MigrationCommandListBuilder AppendLine() { _commandBuilder.AppendLine(); return(this); }