Example #1
0
 public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     if (this.NeedsRenaming)
     {
         int num;
         if (sqlGenerator.AllColumnNames.TryGetValue(this.NewName, out num))
         {
             string key;
             do
             {
                 ++num;
                 key = this.NewName + num.ToString((IFormatProvider)CultureInfo.InvariantCulture);
             }while (sqlGenerator.AllColumnNames.ContainsKey(key));
             sqlGenerator.AllColumnNames[this.NewName] = num;
             this.NewName = key;
         }
         sqlGenerator.AllColumnNames[this.NewName] = 0;
         this.NeedsRenaming = false;
     }
     writer.Write(SqlGenerator.QuoteIdentifier(this.NewName));
 }
 public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
 {
     if (this._sqlFragments == null)
     {
         return;
     }
     foreach (object sqlFragment1 in this._sqlFragments)
     {
         string str = sqlFragment1 as string;
         if (str != null)
         {
             writer.Write(str);
         }
         else
         {
             ISqlFragment sqlFragment2 = sqlFragment1 as ISqlFragment;
             if (sqlFragment2 == null)
             {
                 throw new InvalidOperationException();
             }
             sqlFragment2.WriteSql(writer, sqlGenerator);
         }
     }
 }
        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;
            if (this.IsTopMost && this.Top != null && (this.orderBy != null && !this.OrderBy.IsEmpty) || !this.IsTopMost && this.orderBy != null && !this.OrderBy.IsEmpty)
            {
                writer.Write("SELECT * ");
                writer.WriteLine();
                writer.Write("FROM ( ");
                writer.WriteLine();
            }
            writer.Write("SELECT ");
            if (this.IsDistinct)
            {
                writer.Write("DISTINCT ");
            }
            if (this.select == null || this.Select.IsEmpty)
            {
                writer.Write("*");
            }
            else
            {
                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);
                writer.Write(")");
                if (this.Top != null && (this.orderBy == null || this.orderBy != null && this.OrderBy.IsEmpty))
                {
                    writer.Write(" AND (");
                    this.Top.WriteSql(writer, sqlGenerator);
                    writer.Write(")");
                }
            }
            else if (this.Top != null && (this.orderBy == null || this.orderBy != null && this.OrderBy.IsEmpty))
            {
                writer.WriteLine();
                writer.Write("WHERE (");
                this.Top.WriteSql(writer, sqlGenerator);
                writer.Write(")");
            }
            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.Top != null))
            {
                writer.WriteLine();
                writer.Write("ORDER BY ");
                this.OrderBy.WriteSql(writer, sqlGenerator);
            }
            if (this.Top != null && this.orderBy != null && !this.OrderBy.IsEmpty)
            {
                SqlSelectStatement.Top_s = this.Top;
            }
            if (this.IsTopMost || this.orderBy != null && !this.OrderBy.IsEmpty)
            {
                if (this.IsTopMost && this.Top != null && (this.orderBy != null && !this.OrderBy.IsEmpty) || !this.IsTopMost && this.orderBy != null && !this.OrderBy.IsEmpty)
                {
                    writer.WriteLine();
                    writer.Write(")");
                }
                if (SqlSelectStatement.Top_s != null)
                {
                    writer.WriteLine();
                    writer.Write("WHERE (");
                    SqlSelectStatement.Top_s.WriteSql(writer, sqlGenerator);
                    writer.Write(")");
                    SqlSelectStatement.Top_s = (TopClause)null;
                }
            }
            --writer.Indent;
        }