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