internal override void WriteSql(StringBuilder sqlText) { sqlText.Append("SELECT "); if (Distinct) { sqlText.Append("DISTINCT "); } if (Projection != null) { Projection.WriteSql(sqlText); } else { if (ColumnsToProject.Count == 0) { sqlText.Append("1"); // Could be arbitrary, let's pick 1 } else { var first = true; foreach (var column in ColumnsToProject) { if (!first) { sqlText.Append(", "); } else { first = false; } sqlText.Append(SqlBaseGenerator.QuoteIdentifier(column.Key.Item1)); sqlText.Append("."); sqlText.Append(SqlBaseGenerator.QuoteIdentifier(column.Key.Item2)); if (column.Key.Item2 != column.Value) { sqlText.Append(" AS "); sqlText.Append(SqlBaseGenerator.QuoteIdentifier(column.Value)); } } } } sqlText.Append(" FROM "); From.WriteSql(sqlText); Where?.WriteSql(sqlText); GroupBy?.WriteSql(sqlText); OrderBy?.WriteSql(sqlText); Skip?.WriteSql(sqlText); Limit?.WriteSql(sqlText); base.WriteSql(sqlText); }
internal override void WriteSql(StringBuilder sqlText) { base.WriteSql(sqlText); if (Where != null) { Where.WriteSql(sqlText); } if (GroupBy != null) { GroupBy.WriteSql(sqlText); } if (OrderBy != null) { OrderBy.WriteSql(sqlText); } if (Skip != null) { Skip.WriteSql(sqlText); } if (Limit != null) { Limit.WriteSql(sqlText); } }
// <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; }
public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator) { 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); } } } 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; sqlGenerator.AllExtentNames[newName] = 0; } if (null == outerExtentAliases) { outerExtentAliases = new List <string>(); } outerExtentAliases.Add(fromAlias.NewName); } } writer.Indent += 1; @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); } --writer.Indent; }
/// <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 = AllJoinExtents ?? _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 writer.Write("SELECT "); if (IsDistinct) { writer.Write("DISTINCT "); } if (First != null) { First.WriteSql(writer, sqlGenerator); } if (Skip != null) { Skip.WriteSql(writer, sqlGenerator); } if ((_select == null) || Select.IsEmpty) { Debug.Assert(false); // we have removed all possibilities of SELECT *. writer.Write("*"); } else { Select.WriteSql(writer, sqlGenerator); } writer.WriteLine(); writer.Write("FROM "); From.WriteSql(writer, sqlGenerator); if ((_where != null) && !Where.IsEmpty) { writer.WriteLine(); writer.Write("WHERE "); Where.WriteSql(writer, sqlGenerator); } if ((_groupBy != null) && !GroupBy.IsEmpty) { writer.WriteLine(); writer.Write("GROUP BY "); GroupBy.WriteSql(writer, sqlGenerator); } if ((_orderBy != null) && !OrderBy.IsEmpty && (IsTopMost || First != null || Skip != null)) { writer.WriteLine(); writer.Write("ORDER BY "); OrderBy.WriteSql(writer, sqlGenerator); } --writer.Indent; }
/// <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); } } }