Пример #1
0
        void ISqlFormattable.AppendTo(SqlStringBuilder builder)
        {
            if (IsEmpty)
            {
                return;
            }

            builder.Append("FROM ");

            var tables = AllTables.ToList();

            for (int i = 0; i < tables.Count; i++)
            {
                var source = tables[i];

                JoinPart joinPart = null;

                if (i > 0 && joinParts.Count > 0)
                {
                    joinPart = GetJoinPart(i - 1);
                    if (joinPart != null &&
                        joinPart.OnExpression != null)
                    {
                        if (joinPart.JoinType == JoinType.Inner)
                        {
                            builder.Append(" INNER JOIN ");
                        }
                        else if (joinPart.JoinType == JoinType.Right)
                        {
                            builder.Append(" RIGHT OUTER JOIN ");
                        }
                        else if (joinPart.JoinType == JoinType.Left)
                        {
                            builder.Append(" LEFT OUTER JOIN ");
                        }
                        else if (joinPart.JoinType == JoinType.Full)
                        {
                            builder.Append(" FULL OUTER JOINT ");
                        }
                    }
                }

                if (i > 0 &&
                    (joinPart == null || joinPart.OnExpression == null))
                {
                    builder.Append(", ");
                }

                if (source.IsSubQuery)
                {
                    builder.Append("(");
                    source.SubQuery.AppendTo(builder);
                    builder.Append(")");
                }
                else
                {
                    builder.Append(source.Name);
                }

                if (!String.IsNullOrEmpty(source.Alias))
                {
                    builder.Append(" AS ");
                    builder.Append(source.Alias);
                }

                if (joinPart != null &&
                    joinPart.OnExpression != null)
                {
                    builder.Append(" ON ");
                    joinPart.OnExpression.AppendTo(builder);
                }
            }
        }