Example #1
0
 public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     if (IsEmpty)
     {
         writer.Write("*");
     }
     else
     {
         var count = Columns.Count;
         var i     = 0;
         foreach (var column in Columns)
         {
             i += 1;
             column.WriteSql(writer, sqlGenerator);
             if (i < count)
             {
                 writer.WriteLine(",");
             }
         }
     }
 }
Example #2
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 (NeedsRenaming)
            {
                string newName;
                int    i = sqlGenerator.AllColumnNames[NewName];
                do
                {
                    ++i;
                    newName = SqlGenerator.Format("{0}{1}", Name, i);
                } while (sqlGenerator.AllColumnNames.ContainsKey(newName));
                sqlGenerator.AllColumnNames[NewName] = i;

                // Prevent it from being renamed repeatedly.
                NeedsRenaming = false;
                NewName       = newName;

                // Add this column name to list of known names so that there are no subsequent
                // collisions
                sqlGenerator.AllColumnNames[newName] = 0;
            }
            writer.Write(SqlGenerator.QuoteIdentifier(NewName));
        }
Example #3
0
 public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     // Symbol pair should never be part of a SqlBuilder.
     Debug.Assert(false);
 }
Example #4
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.
            var outerExtentAliases = GetOuterExtentAliases().ToList();

            // 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;
            foreach (Symbol extent in extentList)
            {
                if (outerExtentAliases.Contains(extent.Name))
                {
                    int i = sqlGenerator.AllExtentNames[extent.Name];

                    string newName;
                    do
                    {
                        ++i;
                        newName = SqlGenerator.Format("{0}{1}", extent.Name, i);
                    }while (sqlGenerator.AllExtentNames.ContainsKey(newName));

                    sqlGenerator.AllExtentNames[extent.Name] = i;
                    extent.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.
                outerExtentAliases.Add(extent.NewName);
            }
            #endregion

            // Increase the indent
            using (writer.Indent())
            {
                writer.Write("select ");
                if (IsDistinct)
                {
                    writer.Write("distinct ");
                }

                Select.WriteSql(writer, sqlGenerator);

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

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

                if (!this.GroupBy.IsEmpty)
                {
                    writer.WriteLine();
                    writer.Write("group by ");
                    GroupBy.WriteSql(writer, sqlGenerator);
                }

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

                if (!Top.IsEmpty)
                {
                    Top.WriteSql(writer, sqlGenerator);
                }
            }
        }
Example #5
0
 /// <summary>
 /// We delegate the writing of the fragment to the appropriate type.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="sqlGenerator"></param>
 public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     _sqlFragments.ForEach(sqlFragment => sqlFragment.WriteSql(writer, sqlGenerator));
 }
Example #6
0
 public IndentHandler(SqlWriter sqlWriter)
 {
     _sqlWriter   = sqlWriter;
     _indentCount = _sqlWriter.IndentCount;
 }