1 //---------------------------------------------------------------------
2 // <copyright file="TopClause.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
11 using System
.Collections
.Generic
;
12 using System
.Diagnostics
;
13 using System
.Globalization
;
16 using System
.Data
.SqlClient
;
17 using System
.Data
.Metadata
.Edm
;
18 using System
.Data
.Common
.CommandTrees
;
20 namespace System
.Data
.SqlClient
.SqlGen
23 /// TopClause represents the a TOP expression in a SqlSelectStatement.
24 /// It has a count property, which indicates how many TOP rows should be selected and a
25 /// boolen WithTies property.
27 class TopClause
: ISqlFragment
29 ISqlFragment topCount
;
33 /// Do we need to add a WITH_TIES to the top statement
35 internal bool WithTies
37 get { return withTies; }
41 /// How many top rows should be selected.
43 internal ISqlFragment TopCount
45 get { return topCount; }
49 /// Creates a TopClause with the given topCount and withTies.
51 /// <param name="topCount"></param>
52 /// <param name="withTies"></param>
53 internal TopClause(ISqlFragment topCount
, bool withTies
)
55 this.topCount
= topCount
;
56 this.withTies
= withTies
;
60 /// Creates a TopClause with the given topCount and withTies.
62 /// <param name="topCount"></param>
63 /// <param name="withTies"></param>
64 internal TopClause(int topCount
, bool withTies
)
66 SqlBuilder sqlBuilder
= new SqlBuilder();
67 sqlBuilder
.Append(topCount
.ToString(CultureInfo
.InvariantCulture
));
68 this.topCount
= sqlBuilder
;
69 this.withTies
= withTies
;
72 #region ISqlFragment Members
75 /// Write out the TOP part of sql select statement
76 /// It basically writes TOP (X) [WITH TIES].
77 /// The brackets around X are ommited for Sql8.
79 /// <param name="writer"></param>
80 /// <param name="sqlGenerator"></param>
81 public void WriteSql(SqlWriter writer
, SqlGenerator sqlGenerator
)
85 if (sqlGenerator
.SqlVersion
!= SqlVersion
.Sql8
)
90 this.TopCount
.WriteSql(writer
, sqlGenerator
);
92 if (sqlGenerator
.SqlVersion
!= SqlVersion
.Sql8
)
101 writer
.Write("WITH TIES ");