Exemple #1
0
 /// <summary>
 /// We delegate the writing of the fragment to the appropriate type.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="sqlGenerator"></param>
 public virtual void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     if (null != _sqlFragments)
     {
         foreach (object o in _sqlFragments)
         {
             string str = (o as String);
             if (null != str)
             {
                 writer.Write(str);
             }
             else
             {
                 ISqlFragment sqlFragment = (o as ISqlFragment);
                 if (null != sqlFragment)
                 {
                     sqlFragment.WriteSql(writer, sqlGenerator);
                 }
                 else
                 {
                     throw new InvalidOperationException();
                 }
             }
         }
     }
 }
        /// <summary>
        /// Write this symbol out as a string for sql.  This is just
        /// the new name of the symbol (which could be the same as the old name).
        ///
        /// We rename columns here if necessary.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="sqlGenerator"></param>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            if (this.NeedsRenaming)
            {
                int i;

                if (sqlGenerator.AllColumnNames.TryGetValue(this.NewName, out i))
                {
                    string newNameCandidate;
                    do
                    {
                        ++i;
                        newNameCandidate = this.NewName + i.ToString(System.Globalization.CultureInfo.InvariantCulture);
                    } while (sqlGenerator.AllColumnNames.ContainsKey(newNameCandidate));

                    sqlGenerator.AllColumnNames[this.NewName] = i;

                    this.NewName = newNameCandidate;
                }

                // Add this column name to list of known names so that there are no subsequent
                // collisions
                sqlGenerator.AllColumnNames[this.NewName] = 0;

                // Prevent it from being renamed repeatedly.
                this.NeedsRenaming = false;
            }
            writer.Write(SqlGenerator.QuoteIdentifier(this.NewName));
        }
Exemple #3
0
 /// <summary>
 /// Writes that fragment that represents the optional column
 /// if the usage manager says it is used.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="sqlGenerator"></param>
 public bool WriteSqlIfUsed(SqlWriter writer, SqlGenerator sqlGenerator, string separator)
 {
     if (m_usageManager.IsUsed(m_symbol))
     {
         writer.Write(separator);
         m_builder.WriteSql(writer, sqlGenerator);
         return(true);
     }
     return(false);
 }
 /// <summary>
 /// Writes that fragment that represents the optional column 
 /// if the usage manager says it is used.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="sqlGenerator"></param>
 public bool WriteSqlIfUsed(SqlWriter writer, SqlGenerator sqlGenerator, string separator)
 {
     if (m_usageManager.IsUsed(m_symbol))
     {
         writer.Write(separator);
         m_builder.WriteSql(writer, sqlGenerator);
         return true;
     }
     return false;
 }
Exemple #5
0
        /// <summary>
        /// Writes the string representing the Select statement:
        ///
        /// SELECT (DISTINCT) (TOP topClause) (optionalColumns) (requiredColumns)
        ///
        /// If Distinct is specified or this is part of a top most statement
        /// all optional columns are marked as used.
        ///
        /// Optional columns are only written if marked as used.
        /// In addition, if no required columns are specified and no optional columns are
        /// marked as used, the first optional column is written.
        ///
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="sqlGenerator"></param>
        public override void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            writer.Write("SELECT ");
            if (IsDistinct)
            {
                writer.Write("DISTINCT ");
            }

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

            if (this.IsEmpty)
            {
                Debug.Assert(false);  // we have removed all possibilities of SELECT *.
                writer.Write("*");
            }
            else
            {
                //Print the optional columns if any
                bool printedAny = WriteOptionalColumns(writer, sqlGenerator);

                if (!base.IsEmpty)
                {
                    if (printedAny)
                    {
                        writer.Write(", ");
                    }
                    base.WriteSql(writer, sqlGenerator);
                }
                //If no optional columns were printed and there were no other columns,
                // print at least the first optional column
                else if (!printedAny)
                {
                    this.m_optionalColumns[0].MarkAsUsed();
                    m_optionalColumns[0].WriteSqlIfUsed(writer, sqlGenerator, "");
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Write out the TOP part of sql select statement
        /// It basically writes TOP (X) [WITH TIES].
        /// The brackets around X are ommited for Sql8.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="sqlGenerator"></param>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            writer.Write("TOP ");

            if (sqlGenerator.SqlVersion != SqlVersion.Sql8)
            {
                writer.Write("(");
            }

            this.TopCount.WriteSql(writer, sqlGenerator);

            if (sqlGenerator.SqlVersion != SqlVersion.Sql8)
            {
                writer.Write(")");
            }

            writer.Write(" ");

            if (this.WithTies)
            {
                writer.Write("WITH TIES ");
            }
        }
        /// <summary>
        /// Write out the TOP part of sql select statement 
        /// It basically writes TOP (X) [WITH TIES].
        /// The brackets around X are ommited for Sql8.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="sqlGenerator"></param>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            writer.Write("TOP ");

            if (sqlGenerator.SqlVersion != SqlVersion.Sql8)
            {
                writer.Write("(");
            }

            this.TopCount.WriteSql(writer, sqlGenerator);

            if (sqlGenerator.SqlVersion != SqlVersion.Sql8)
            {
                writer.Write(")");
            }

            writer.Write(" ");

            if (this.WithTies)
            {
                writer.Write("WITH TIES ");
            }
        }
        /// <summary>
        /// Writes the string representing the Select statement:
        /// 
        /// SELECT (DISTINCT) (TOP topClause) (optionalColumns) (requiredColumns)
        /// 
        /// If Distinct is specified or this is part of a top most statement 
        /// all optional columns are marked as used.
        /// 
        /// Optional columns are only written if marked as used. 
        /// In addition, if no required columns are specified and no optional columns are 
        /// marked as used, the first optional column is written.
        /// 
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="sqlGenerator"></param>
        public override void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            writer.Write("SELECT ");
            if (IsDistinct)
            {
                writer.Write("DISTINCT ");
            }

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

            if (this.IsEmpty)
            {
                Debug.Assert(false);  // we have removed all possibilities of SELECT *.
                writer.Write("*");
            }
            else
            {
                //Print the optional columns if any
                bool printedAny = WriteOptionalColumns(writer, sqlGenerator);

                if (!base.IsEmpty)
                {
                    if (printedAny)
                    {
                        writer.Write(", ");
                    }
                    base.WriteSql(writer, sqlGenerator);
                }
                //If no optional columns were printed and there were no other columns, 
                // print at least the first optional column
                else if (!printedAny)
                {
                    this.m_optionalColumns[0].MarkAsUsed();
                    m_optionalColumns[0].WriteSqlIfUsed(writer, sqlGenerator, "");
                }
            }
        }
        /// <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 (Symbol outerExtent in outerExtents.Keys)
                {
                    JoinSymbol joinSymbol = outerExtent as JoinSymbol;
                    if (joinSymbol != null)
                    {
                        foreach (Symbol 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.
            List<Symbol> extentList = this.AllJoinExtents ?? this.fromExtents;
            if (null != extentList)
            {
                foreach (Symbol fromAlias in extentList)
                {
                    if ((null != outerExtentAliases) && outerExtentAliases.Contains(fromAlias.Name))
                    {
                        int i = sqlGenerator.AllExtentNames[fromAlias.Name];
                        string newName;
                        do
                        {
                            ++i;
                            newName = fromAlias.Name + i.ToString(System.Globalization.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

            this.select.WriteSql(writer, sqlGenerator);

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

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

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

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

            --writer.Indent;
        }
Exemple #10
0
 /// <summary>
 /// We delegate the writing of the fragment to the appropriate type.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="sqlGenerator"></param>
 public virtual void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     if (null != _sqlFragments)
     {
         foreach (object o in _sqlFragments)
         {
             string str = (o as String);
             if (null != str)
             {
                 writer.Write(str);
             }
             else
             {
                 ISqlFragment sqlFragment = (o as ISqlFragment);
                 if (null != sqlFragment)
                 {
                     sqlFragment.WriteSql(writer, sqlGenerator);
                 }
                 else
                 {
                     throw new InvalidOperationException();
                 }
             }
         }
     }
 }
Exemple #11
0
        /// <summary>
        /// Write this symbol out as a string for sql.  This is just
        /// the new name of the symbol (which could be the same as the old name).
        ///
        /// We rename columns here if necessary.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="sqlGenerator"></param>
        public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
        {
            if (this.NeedsRenaming)
            {
                int i;

                if (sqlGenerator.AllColumnNames.TryGetValue(this.NewName, out i))
                {
                    string newNameCandidate;
                    do
                    {
                        ++i;
                        newNameCandidate = this.NewName + i.ToString(System.Globalization.CultureInfo.InvariantCulture);
                    } while (sqlGenerator.AllColumnNames.ContainsKey(newNameCandidate));

                    sqlGenerator.AllColumnNames[this.NewName] = i;

                    this.NewName = newNameCandidate;
                }

                // Add this column name to list of known names so that there are no subsequent
                // collisions
                sqlGenerator.AllColumnNames[this.NewName] = 0;

                // Prevent it from being renamed repeatedly.
                this.NeedsRenaming = false;
            }
            writer.Write(SqlGenerator.QuoteIdentifier(this.NewName));
        }
Exemple #12
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 (Symbol outerExtent in outerExtents.Keys)
                {
                    JoinSymbol joinSymbol = outerExtent as JoinSymbol;
                    if (joinSymbol != null)
                    {
                        foreach (Symbol 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.
            List <Symbol> extentList = this.AllJoinExtents ?? this.fromExtents;
            if (null != extentList)
            {
                foreach (Symbol fromAlias in extentList)
                {
                    if ((null != outerExtentAliases) && outerExtentAliases.Contains(fromAlias.Name))
                    {
                        int    i = sqlGenerator.AllExtentNames[fromAlias.Name];
                        string newName;
                        do
                        {
                            ++i;
                            newName = fromAlias.Name + i.ToString(System.Globalization.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

            this.select.WriteSql(writer, sqlGenerator);

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

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

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

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

            --writer.Indent;
        }