-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathTableSelectStatement.cs
147 lines (124 loc) · 4.21 KB
/
TableSelectStatement.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
#if MICROSOFTDATA
#else
using System.Data.SqlClient;
#endif
using System.Diagnostics;
using System.Globalization;
namespace Microsoft.SqlServer.Management.SqlScriptPublish
{
/// <summary>
/// Gets select statement for a table
/// </summary>
internal class TableSelectStatement
{
#region Private Fields
private Table table;
private bool hasWritableColumns;
private bool hasUserDefinedType;
private string columnNames;
private string tableName;
#endregion
#region Constructor
/// <summary>
/// Creates an instance of TableSelect
/// </summary>
/// <param name="table">Table whose data is to be enumerated as INSERT strings</param>
public TableSelectStatement(Table table)
{
this.table = table;
this.tableName = table.FullQualifiedName;
this.hasWritableColumns = false;
StringBuilder columnNameSQL = new StringBuilder();
bool firstColumn = true;
foreach (Column col in this.table.Columns)
{
// we need to ignore timestamp values because it gets
// automatically populated when a row is inserted or updated
if (col.DataType.SqlDataType == SqlDataType.Timestamp ||
col.Computed)
{
continue;
}
if (firstColumn == false)
{
// Append the commas after existing columnName and selectSql statements
columnNameSQL.Append(", ");
}
firstColumn = false;
//we need to know if table has a UDT column because these kind of column cannot be published
//inside a dataset. (Passing a dataset with table and UDT column over a web service will failed.)
if (col.DataType.SqlDataType == SqlDataType.UserDefinedType)
{
this.hasUserDefinedType = true;
}
columnNameSQL.Append(String.Format(CultureInfo.InvariantCulture, "[{0}]", col.Name));
}
// If there are no columns which can be read then set hasWritableColumns to false
//
this.hasWritableColumns = (columnNameSQL.Length > 0);
if (this.hasWritableColumns)
{
this.columnNames = columnNameSQL.ToString();
}
}
#endregion
#region Internal Methods
/// <summary>
/// Returns whether or not we have a user defined type.
/// </summary>
internal bool HasUserDefinedType
{
get
{
return this.hasUserDefinedType;
}
}
/// <summary>
/// Returns whether or not there's anything to be scripted from this table.
/// </summary>
internal bool HasWritableColumns
{
get
{
return this.hasWritableColumns;
}
}
/// <summary>
/// Returns the table name
/// </summary>
internal string TableName
{
get
{
return this.tableName;
}
}
/// <summary>
/// Returns a SqlBulkCopy object representing the data
/// for the table.
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException">
/// If there are no Writable column in the table
/// </exception>
///
internal string GetSelectStatement()
{
if (!this.HasWritableColumns)
{
Debug.Assert(false, "This method should not have been called when there is no writable column");
throw new InvalidOperationException();
}
return string.Format(CultureInfo.InvariantCulture,
"SELECT {0} FROM {1}",
this.columnNames,
this.tableName);
}
#endregion
}
}