public override void WriteSql(SqlWriter writer, SqlVisitor visitor)
        {
            #region Check if FROM aliases need to be renamed

            // Create a list of the aliases used by the outer extents
            // JoinSymbols have to be treated specially.
            List <string> outerExtentAliases = null;
            if ((null != _outerExtents) && (0 < _outerExtents.Count))
            {
                foreach (var outerExtent in _outerExtents.Keys)
                {
                    var joinSymbol = outerExtent as JoinSymbol;

                    if (joinSymbol != null)
                    {
                        foreach (var symbol in joinSymbol.FlattenedExtentList)
                        {
                            if (null == outerExtentAliases)
                            {
                                outerExtentAliases = new List <string>();
                            }

                            outerExtentAliases.Add(symbol.NewName);
                        }
                    }
                    else
                    {
                        if (null == outerExtentAliases)
                        {
                            outerExtentAliases = new List <string>();
                        }

                        outerExtentAliases.Add(outerExtent.NewName);
                    }
                }
            }

            // An then rename each of the FromExtents we have
            // If AllJoinExtents is non-null - it has precedence.
            // The new name is derived from the old name - we append an increasing int.
            var extentList = AllJoinExtents ?? _fromExtents;

            if (null != extentList)
            {
                foreach (var fromAlias in extentList)
                {
                    if ((null != outerExtentAliases) && outerExtentAliases.Contains(fromAlias.Name))
                    {
                        var    i = visitor.AllExtentNames[fromAlias.Name];
                        string newName;

                        do
                        {
                            ++i;
                            newName = fromAlias.Name + i.ToString(System.Globalization.CultureInfo.InvariantCulture);
                        } while (visitor.AllExtentNames.ContainsKey(newName));

                        visitor.AllExtentNames[fromAlias.Name] = i;
                        fromAlias.NewName = newName;

                        // Add extent to list of known names (although i is always incrementing, "prefix11" can
                        // eventually collide with "prefix1" when it is extended)
                        visitor.AllExtentNames[newName] = 0;
                    }

                    // Add the current alias to the list, so that the extents
                    // that follow do not collide with me.
                    if (null == outerExtentAliases)
                    {
                        outerExtentAliases = new List <string>();
                    }

                    outerExtentAliases.Add(fromAlias.NewName);
                }
            }
            #endregion

            // Increase the indent, so that the Sql statement is nested by one tab.
            writer.Indent += 1; // ++ can be confusing in this context

            writer.Write("SELECT ");
            if (IsDistinct)
            {
                writer.Write("DISTINCT ");
            }

            if (Top != null)
            {
                Top.WriteSql(writer, visitor);
            }

            if ((null == Select) || Select.IsEmpty)
            {
                writer.Write("*");
            }
            else
            {
                Select.WriteSql(writer, visitor);
            }

            writer.WriteLine();
            writer.Write("FROM ");
            From.WriteSql(writer, visitor);

            if ((null != _where) && !Where.IsEmpty)
            {
                writer.WriteLine();
                writer.Write("WHERE ");
                Where.WriteSql(writer, visitor);
            }

            if ((null != _groupBy) && !GroupBy.IsEmpty)
            {
                writer.WriteLine();
                writer.Write("GROUP BY ");
                GroupBy.WriteSql(writer, visitor);
            }

            if ((null != _orderBy) && !OrderBy.IsEmpty && (IsTopMost || Top != null))
            {
                writer.WriteLine();
                writer.Write("ORDER BY ");
                OrderBy.WriteSql(writer, visitor);
            }

            --writer.Indent;
        }
Esempio n. 2
0
 public override void WriteSql(SqlWriter writer, SqlVisitor visitor)
 {
     writer.Write("TOP ");
     TopCount.WriteSql(writer, visitor);
     writer.Write(" ");
 }
Esempio n. 3
0
 public abstract void WriteSql(SqlWriter writer, SqlVisitor visitor);
 public override void WriteSql(SqlWriter writer, SqlVisitor visitor)
 {
     Left.WriteSql(writer, visitor);
     writer.Write(GetOperator(Kind));
     Right.WriteSql(writer, visitor);
 }
 public override void WriteSql(SqlWriter writer, SqlVisitor visitor)
 {
     // Symbol pair should never be part of a SqlBuilder.
     Debug.Assert(false);
 }