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> /// Visit conditional /// </summary> /// <param name="expression"></param> /// <returns></returns> protected override Expression VisitConditional(ConditionalExpression expression) { Check.NotNull(expression, nameof(expression)); // Boolean valid return type in Cypher otherwise use case if (expression.Type == typeof(bool)) { Visit(expression.Test); } else { _commandBuilder.AppendLine("CASE"); using (_commandBuilder.Indent()) { _commandBuilder.Append("WHEN "); Visit(expression.Test); _commandBuilder.AppendLine(); _commandBuilder.Append("THEN "); // when true is bool or other if (expression.IfTrue.RemoveConvert() is ConstantExpression constantIfTrue && !(constantIfTrue.Value is null) && constantIfTrue.Type.UnwrapNullableType() == typeof(bool)) { _commandBuilder.Append( (bool)constantIfTrue.Value ? TypedTrueLiteral : TypedFalseLiteral ); }
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); }
/// <summary> /// Starts a new indentation block, so all 'Append...' calls until the /// block is disposed will be indented one level more than the current level. /// </summary> /// <returns> The object to dispose to indicate that the indentation should go back up a level. </returns> public virtual IDisposable Indent() => _commandBuilder.Indent();
/// <summary> /// 缩进 /// </summary> /// <returns></returns> public override IDisposable Indent() { return(_commandBuilder.Indent()); }