Ejemplo n.º 1
0
        /// <summary>
        ///     Write out a SQL select statement as a string.
        ///     We have to
        ///     <list type="number">
        ///         <item>Check whether the aliases extents we use in this statement have
        ///             to be renamed.
        ///             We first create a list of all the aliases used by the outer extents.
        ///             For each of the FromExtents( or AllJoinExtents if it is non-null),
        ///             rename it if it collides with the previous list.</item>
        ///         <item>Write each of the clauses (if it exists) as a string</item>
        ///     </list>
        /// </summary>
        /// <param name="writer"> </param>
        /// <param name="sqlGenerator"> </param>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            #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 = sqlGenerator.AllExtentNames[fromAlias.Name];
                        string newName;
                        do
                        {
                            ++i;
                            newName = fromAlias.Name + i.ToString(CultureInfo.InvariantCulture);
                        }
                        while (sqlGenerator.AllExtentNames.ContainsKey(newName));
                        sqlGenerator.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)
                        sqlGenerator.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

            @select.WriteSql(writer, sqlGenerator);

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

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

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

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

            --writer.Indent;
        }
Ejemplo n.º 2
0
        // <summary>
        // Write out a SQL select statement as a string.
        // We have to
        // <list type="number">
        //     <item>
        //         Check whether the aliases extents we use in this statement have
        //         to be renamed.
        //         We first create a list of all the aliases used by the outer extents.
        //         For each of the FromExtents( or AllJoinExtents if it is non-null),
        //         rename it if it collides with the previous list.
        //     </item>
        //     <item>Write each of the clauses (if it exists) as a string</item>
        // </list>
        // </summary>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            #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 = sqlGenerator.AllExtentNames[fromAlias.Name];
                        string newName;
                        do
                        {
                            ++i;
                            newName = fromAlias.Name + i.ToString(CultureInfo.InvariantCulture);
                        }while (sqlGenerator.AllExtentNames.ContainsKey(newName));
                        sqlGenerator.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)
                        sqlGenerator.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

            @select.WriteSql(writer, sqlGenerator);

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

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

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

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

            if (null != Select.Skip)
            {
                writer.WriteLine();
                WriteOffsetFetch(writer, Select.Top, Select.Skip, sqlGenerator); // Write OFFSET, FETCH clause.
            }

            --writer.Indent;
        }
Ejemplo n.º 3
0
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            List <string> stringList = (List <string>)null;

            if (this.outerExtents != null && 0 < this.outerExtents.Count)
            {
                foreach (Symbol key in this.outerExtents.Keys)
                {
                    JoinSymbol joinSymbol = key as JoinSymbol;
                    if (joinSymbol != null)
                    {
                        foreach (Symbol flattenedExtent in joinSymbol.FlattenedExtentList)
                        {
                            if (stringList == null)
                            {
                                stringList = new List <string>();
                            }
                            stringList.Add(flattenedExtent.NewName);
                        }
                    }
                    else
                    {
                        if (stringList == null)
                        {
                            stringList = new List <string>();
                        }
                        stringList.Add(key.NewName);
                    }
                }
            }
            List <Symbol> symbolList = this.AllJoinExtents ?? this.fromExtents;

            if (symbolList != null)
            {
                foreach (Symbol symbol in symbolList)
                {
                    if (stringList != null && stringList.Contains(symbol.Name))
                    {
                        int    allExtentName = sqlGenerator.AllExtentNames[symbol.Name];
                        string key;
                        do
                        {
                            ++allExtentName;
                            key = symbol.Name + allExtentName.ToString((IFormatProvider)CultureInfo.InvariantCulture);
                        }while (sqlGenerator.AllExtentNames.ContainsKey(key));
                        sqlGenerator.AllExtentNames[symbol.Name] = allExtentName;
                        symbol.NewName = key;
                        sqlGenerator.AllExtentNames[key] = 0;
                    }
                    if (stringList == null)
                    {
                        stringList = new List <string>();
                    }
                    stringList.Add(symbol.NewName);
                }
            }
            ++writer.Indent;
            this.select.WriteSql(writer, sqlGenerator);
            writer.WriteLine();
            writer.Write("FROM ");
            this.From.WriteSql(writer, sqlGenerator);
            if (this.where != null && !this.Where.IsEmpty)
            {
                writer.WriteLine();
                writer.Write("WHERE ");
                this.Where.WriteSql(writer, sqlGenerator);
            }
            if (this.groupBy != null && !this.GroupBy.IsEmpty)
            {
                writer.WriteLine();
                writer.Write("GROUP BY ");
                this.GroupBy.WriteSql(writer, sqlGenerator);
            }
            if (this.orderBy != null && !this.OrderBy.IsEmpty && (this.IsTopMost || this.Select.Top != null || this.Select.Skip != null))
            {
                writer.WriteLine();
                writer.Write("ORDER BY ");
                this.OrderBy.WriteSql(writer, sqlGenerator);
            }
            if (this.Select.Skip != null)
            {
                writer.WriteLine();
                SqlSelectStatement.WriteOffsetFetch(writer, this.Select.Top, this.Select.Skip, sqlGenerator);
            }
            --writer.Indent;
        }