1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
//---------------------------------------------------------------------
// <copyright file="TopClause.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Data.SqlClient;
using System.Data.Metadata.Edm;
using System.Data.Common.CommandTrees;
namespace System.Data.SqlClient.SqlGen
{
/// <summary>
/// TopClause represents the a TOP expression in a SqlSelectStatement.
/// It has a count property, which indicates how many TOP rows should be selected and a
/// boolen WithTies property.
/// </summary>
class TopClause : ISqlFragment
{
ISqlFragment topCount;
bool withTies;
/// <summary>
/// Do we need to add a WITH_TIES to the top statement
/// </summary>
internal bool WithTies
{
get { return withTies; }
}
/// <summary>
/// How many top rows should be selected.
/// </summary>
internal ISqlFragment TopCount
{
get { return topCount; }
}
/// <summary>
/// Creates a TopClause with the given topCount and withTies.
/// </summary>
/// <param name="topCount"></param>
/// <param name="withTies"></param>
internal TopClause(ISqlFragment topCount, bool withTies)
{
this.topCount = topCount;
this.withTies = withTies;
}
/// <summary>
/// Creates a TopClause with the given topCount and withTies.
/// </summary>
/// <param name="topCount"></param>
/// <param name="withTies"></param>
internal TopClause(int topCount, bool withTies)
{
SqlBuilder sqlBuilder = new SqlBuilder();
sqlBuilder.Append(topCount.ToString(CultureInfo.InvariantCulture));
this.topCount = sqlBuilder;
this.withTies = withTies;
}
#region ISqlFragment Members
/// <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 ");
}
}
#endregion
}
}
|