/// <summary> /// 将表达式所表示的SQL片断写入 SQL 生成器 /// </summary> /// <param name="builder">SQL 语句生成器</param> /// <param name="newLine">表示是否需要强制换行</param> public void Write(ISqlBuilder builder, bool newLine) { if (_orderBy.Count > 0) { base._builder = builder; if (base._methodVisitor == null) { base._methodVisitor = _provider.CreateMethodVisitor(this); } if (newLine) { _builder.AppendNewLine(); } _builder.Append("ORDER BY "); for (int i = 0; i < _orderBy.Count; i++) { this.VisitWithoutRemark(x => this.Visit(_orderBy[i].Expressions[0])); if (_orderBy[i].DbExpressionType == DbExpressionType.OrderByDescending || _orderBy[i].DbExpressionType == DbExpressionType.ThenByDescending) { builder.Append(" DESC"); } if (i < _orderBy.Count - 1) { builder.Append(','); } } } }
/// <summary> /// 写入SQL片断 /// </summary> public override void Write(ISqlBuilder builder) { base._builder = builder; if (base._methodVisitor == null) { base._methodVisitor = _provider.CreateMethodVisitor(this); } foreach (DbExpression qj in _qJoin) { builder.AppendNewLine(); // [INNER/LEFT JOIN] if (qj.DbExpressionType == DbExpressionType.GroupJoin || qj.DbExpressionType == DbExpressionType.Join || qj.DbExpressionType == DbExpressionType.GroupRightJoin) { JoinType joinType = JoinType.InnerJoin; if (qj.DbExpressionType == DbExpressionType.GroupJoin) { joinType = JoinType.LeftJoin; } else if (qj.DbExpressionType == DbExpressionType.GroupRightJoin) { joinType = JoinType.RightJoin; } this.AppendJoinType(builder, joinType); this.AppendLfInJoin(builder, qj, _aliases); } else if (qj.DbExpressionType == DbExpressionType.SelectMany) { this.AppendJoinType(builder, JoinType.CrossJoin); this.AppendCrossJoin(builder, qj, _aliases); } } }
/// <summary> /// 写入SQL片断 /// </summary> public void Write(NpgNavigationCommand cmd) { ISqlBuilder jf = cmd.JoinFragment; ISqlBuilder on = cmd.OnPhrase; base._builder = on; if (base._methodVisitor == null) { base._methodVisitor = _provider.CreateMethodVisitor(this); } if (_qJoin != null && _qJoin.Count > 0) { for (int i = 0; i < _qJoin.Count; i++) { DbExpression qj = _qJoin[i]; if (qj.DbExpressionType == DbExpressionType.GroupJoin || qj.DbExpressionType == DbExpressionType.Join || qj.DbExpressionType == DbExpressionType.GroupRightJoin) { this.AppendLfInJoin(jf, on, qj, _aliases); } else if (qj.DbExpressionType == DbExpressionType.SelectMany) { this.AppendCrossJoin(jf, qj, _aliases); } } } }
/// <summary> /// 将表达式所表示的SQL片断写入SQL构造器 /// </summary> public override void Write(ISqlBuilder builder) { if (base.Expression != null) { base._builder = builder; _builder.AppendNewLine(); _startLength = _builder.Length; if (base._methodVisitor == null) { base._methodVisitor = _provider.CreateMethodVisitor(this); } // SELECT 表达式解析 if (base.Expression.NodeType != ExpressionType.Constant) { base.Write(builder); } else { // if have no select syntax Type type = (base.Expression as ConstantExpression).Value as Type; this.VisitAllMember(type, "t0"); } // Include 表达式解析<导航属性> this.VisitInclude(); // 去掉空白字符 _builder.TrimEnd(' ', ','); } }
/// <summary> /// 将表达式所表示的SQL片断写入SQL构造器 /// </summary> /// <param name="builder">SQL 语句生成器</param> public override void Write(ISqlBuilder builder) { base._builder = builder; if (base._methodVisitor == null) { base._methodVisitor = _provider.CreateMethodVisitor(this); } if (_aggregate != null) { Expression exp = _aggregate.DbExpressionType == DbExpressionType.Count ? Expression.Constant(1) : base.Expression; if (exp.NodeType == ExpressionType.Lambda) { exp = (exp as LambdaExpression).Body; } // q.Average(a => a); // 这种情况下g.Key 一定是单个字段,否则解析出来的SQL执行不了 if (exp.NodeType == ExpressionType.Parameter) { exp = _groupBy.Expressions[0]; } builder.Append(_aggregateMethods[_aggregate.DbExpressionType]); builder.Append("("); base.Visit(exp); builder.Append(")"); } }
/// <summary> /// 将表达式所表示的SQL片断写入SQL构造器 /// </summary> /// <param name="builder">SQL 语句生成器</param> public virtual void Write(ISqlBuilder builder) { _builder = builder; if (_methodVisitor == null) { _methodVisitor = _provider.CreateMethodVisitor(this); } if (_expression != null) { this.Visit(_expression); } }