-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSqlQueryHandler.cs
359 lines (316 loc) · 15.8 KB
/
SqlQueryHandler.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Diagnostics;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
namespace Microsoft.SqlServer.Management.SqlScriptPublish
{
/// <summary>
/// Helper class for database type and database metadata query
/// </summary>
internal class SqlQueryHandler
{
private static readonly QueryInfoCollection dicTypes;
/// <summary>
/// Static constructor
/// </summary>
static SqlQueryHandler()
{
dicTypes = new QueryInfoCollection();
InitializeSupportedObjectTypes();
}
private SqlScriptPublishModel model;
private Dictionary<DatabaseObjectType, List<KeyValuePair<string, string>>> cache;
/// <summary>
/// Constructor
/// </summary>
/// <param name="model"></param>
public SqlQueryHandler(SqlScriptPublishModel model)
{
if (model == null)
{
throw new ArgumentNullException("model");
}
this.model = model;
cache = new Dictionary<DatabaseObjectType, List<KeyValuePair<string, string>>>(dicTypes.Count);
}
#region Public methods
/// <summary>
/// Returns database object types currently existing in the database
/// If the type does not have any object, we do not want to show the object type.
/// </summary>
/// <returns>Database type names whose objects exist</returns>
public IEnumerable<DatabaseObjectType> GetDatabaseObjectTypes()
{
// populate the cache if we haven't yet
if (cache.Count == 0)
{
foreach (QueryInfo info in dicTypes)
{
// doing the enum loads the cache for each object type
// and then the Keys will only be those valid database object types
EnumChildrenForDatabaseObjectType(info.DatabaseObjectType);
}
}
return cache.Keys;
}
/// <summary>
/// Returns all children's object names and urns for the object type.
/// </summary>
/// <param name="objectType">Object type such as tables, views, etc</param>
/// <returns>Object names and urns for the object type</returns>
public IEnumerable<KeyValuePair<string, string>> EnumChildrenForDatabaseObjectType(DatabaseObjectType objectType)
{
List<KeyValuePair<string, string>> children = null;
// if this objectType isn't in the cache, go get it, other just return the list
if (!cache.TryGetValue(objectType, out children))
{
children = new List<KeyValuePair<string, string>>();
QueryInfo info = dicTypes[objectType];
SqlScriptPublishModelTraceHelper.Assert(info != null, "Unsupported object type:" + objectType);
if (info == null)
{
throw new SqlScriptPublishException(SR.InvalidObjectType(objectType.ToString()));
}
// Check to see if the object type is supported from the SQL version.
if (ValidObjectType(info))
{
SfcObjectQuery objQuery = new SfcObjectQuery(this.model.Server);
string fullUrn = string.Format(System.Globalization.CultureInfo.InvariantCulture,
"Server/Database[@Name='{0}']/{1}",
Urn.EscapeString(this.model.DatabaseName),
info.PartialUrn);
SfcQueryExpression expr = new SfcQueryExpression(fullUrn);
DataTable dataTable = objQuery.ExecuteDataTable(expr, info.Fields, info.OrderByList);
foreach (DataRow row in dataTable.Rows)
{
string name = row["Name"].ToString();
if (info.SchemaBased)
{
name = row["Schema"].ToString() + "." + name;
}
KeyValuePair<string, string> child = new KeyValuePair<string, string>(name, row["Urn"].ToString());
children.Add(child);
}
}
// we only put in children we found so the Keys collection only contains the valid objects
// if somebody keeps asking for the same db type over and over again we will keep hitting the server
// but this is an internal class so should not happen.
if (children.Count > 0)
{
cache.Add(objectType, children);
}
}
return children;
}
/// <summary>
/// Returns an enumeration of DatabaseObjectType that are NOT valid for the passed
/// in SMO cloud engine edition
/// </summary>
public IEnumerable<DatabaseObjectType> InvalidObjectTypesForAzure(DatabaseEngineEdition edition)
{
var myEdition = QueryInfo.CloudEngineEdition.SqlDatabase;
switch (edition)
{
case DatabaseEngineEdition.SqlOnDemand:
myEdition = QueryInfo.CloudEngineEdition.OnDemand;
break;
case DatabaseEngineEdition.SqlDataWarehouse:
myEdition = QueryInfo.CloudEngineEdition.OnDemand;
break;
}
return dicTypes.Where(d => (d.ValidEngineTypes & QueryInfo.EngineType.Cloud) == 0 || (d.Editions & myEdition) == 0).Select(d => d.DatabaseObjectType);
}
#endregion
/// <summary>
/// Check if the passed in QueryInfo for the ObjectType is valid for the current server
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
private bool ValidObjectType(QueryInfo info)
{
return model.Server.IsSupportedObject(info.SmoType);
}
/// <summary>
/// Build up the list of object types and the engine types that they are supported on.
/// This list is used to validate the list of objects returned from the EnumObjects call in the model
/// </summary>
private static void InitializeSupportedObjectTypes()
{
dicTypes.Add(new QueryInfo(DatabaseObjectType.Table, typeof(Table), "Table[@IsSystemObject=false()]"));
dicTypes.Add(new QueryInfo(DatabaseObjectType.View, typeof(View), "View[@IsSystemObject=false()]"));
dicTypes.Add(new QueryInfo(DatabaseObjectType.StoredProcedure, typeof(StoredProcedure), "StoredProcedure[@IsSystemObject=false()]"));
dicTypes.Add(new QueryInfo(DatabaseObjectType.UserDefinedFunction, typeof(UserDefinedFunction), "UserDefinedFunction[@IsSystemObject=false()]"));
dicTypes.Add(new QueryInfo(DatabaseObjectType.UserDefinedDataType, typeof(UserDefinedDataType), "UserDefinedDataType", true, QueryInfo.SQL80));
dicTypes.Add(new QueryInfo(DatabaseObjectType.User, typeof(User), "User[@IsSystemObject=false()]", false));
dicTypes.Add(new QueryInfo(DatabaseObjectType.Default, typeof(Default), "Default", true, QueryInfo.SQL80, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.Rule, typeof(Smo.Rule), "Rule", true, QueryInfo.SQL80, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.DatabaseRole, typeof(DatabaseRole), "Role[@IsFixedRole = false() and @Name != 'public']", false));
dicTypes.Add(new QueryInfo(DatabaseObjectType.ApplicationRole, typeof(ApplicationRole), "ApplicationRole", false, QueryInfo.SQL80, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.SqlAssembly, typeof(SqlAssembly), "SqlAssembly[@IsSystemObject=false()]", false, QueryInfo.SQL90, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.DdlTrigger, typeof(DatabaseDdlTrigger), "DdlTrigger[@IsSystemObject=false()]", false, QueryInfo.SQL90, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.Synonym, typeof(Synonym), "Synonym", true, QueryInfo.SQL90, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.XmlSchemaCollection, typeof(XmlSchemaCollection), "XmlSchemaCollection", false, QueryInfo.SQL90, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.Schema, typeof(Schema), "Schema[(@ID > 4 and @ID < 16384) or (@ID > 16400)]", false, QueryInfo.SQL90));
dicTypes.Add(new QueryInfo(DatabaseObjectType.PlanGuide, typeof(PlanGuide), "PlanGuide", false, QueryInfo.SQL90, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.UserDefinedType, typeof(UserDefinedType), "UserDefinedType", true, QueryInfo.SQL90, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.UserDefinedAggregate, typeof(UserDefinedAggregate), "UserDefinedAggregate", true, QueryInfo.SQL90, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.FullTextCatalog, typeof(FullTextCatalog), "FullTextCatalog", false, QueryInfo.SQL90, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.UserDefinedTableType, typeof(UserDefinedTableType), "UserDefinedTableType", true, QueryInfo.SQL100, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase));
dicTypes.Add(new QueryInfo(DatabaseObjectType.SecurityPolicy, typeof(SecurityPolicy), "SecurityPolicy",true,QueryInfo.SQL130, QueryInfo.EngineType.All, QueryInfo.CloudEngineEdition.SqlDatabase | QueryInfo.CloudEngineEdition.Synapse));
}
#region QueryInfo class
/// <summary>
/// A simple keyed collection
/// </summary>
class QueryInfoCollection : KeyedCollection<DatabaseObjectType, QueryInfo>
{
public QueryInfoCollection() : base() { }
protected override DatabaseObjectType GetKeyForItem(QueryInfo info)
{
return info.DatabaseObjectType;
}
}
/// <summary>
/// Internal class used to hold each the Query's for each of the possible Object Types
/// </summary>
class QueryInfo
{
/// <summary>
/// A [Flag] represenation of engine types for easy checking
/// </summary>
[Flags]
public enum EngineType
{
None = 0,
Singleton = 1,
Cloud = 2,
All = 0xffff
}
[Flags]
public enum CloudEngineEdition
{
None = 0,
SqlDatabase = 1,
Synapse = 2,
OnDemand = 4,
All = 0xffff
}
// major version numbers
public const int SQL70 = 7;
public const int SQL80 = 8;
public const int SQL90 = 9;
public const int SQL100 = 10;
public const int SQL110 = 11;
public const int SQL120 = 12;
public const int SQL130 = 13;
public const int SQL140 = 14;
public const int SQL150 = 15;
public const int SQL160 = 16;
private string partialUrn;
private bool schemaBased;
private int minimumSingletonVersion = SQL80;
private DatabaseObjectType dbObjectType;
private EngineType validTypes;
public QueryInfo(DatabaseObjectType type, Type smoType, string urn)
: this(type, smoType, urn, true, SQL80, EngineType.All, CloudEngineEdition.All)
{
}
public QueryInfo(DatabaseObjectType type, Type smoType, string urn, bool schemaBased)
: this(type, smoType, urn, schemaBased, SQL80, EngineType.All, CloudEngineEdition.All)
{
}
public QueryInfo(DatabaseObjectType type, Type smoType, string urn, bool schemaBased, int minimumSingletonVersion)
: this(type, smoType, urn, schemaBased, minimumSingletonVersion, EngineType.All, CloudEngineEdition.All)
{
}
public QueryInfo(DatabaseObjectType type, Type smoType, string urn, bool schemaBased, int minimumSingletonVersion, EngineType types, CloudEngineEdition editions)
{
dbObjectType = type;
partialUrn = urn;
this.schemaBased = schemaBased;
this.minimumSingletonVersion = minimumSingletonVersion;
validTypes = types;
SmoType = smoType;
Editions = editions;
}
public QueryInfo(DatabaseObjectType type, Type smoType, string urn, bool schemaBased, int minimumSingletonVersion, CloudEngineEdition editions)
{
dbObjectType = type;
partialUrn = urn;
this.schemaBased = schemaBased;
this.minimumSingletonVersion = minimumSingletonVersion;
validTypes = EngineType.All;
SmoType = smoType;
Editions = editions;
}
/// <summary>
/// When supported by Azure engine type, which editions it's supported for
/// </summary>
public CloudEngineEdition Editions
{
get;
private set;
}
/// <summary>
/// The Type to pass to Server.IsSupportedObject
/// </summary>
public Type SmoType
{
get;
private set;
}
public string PartialUrn
{
get { return this.partialUrn; }
}
public bool SchemaBased
{
get { return this.schemaBased; }
}
public DatabaseObjectType DatabaseObjectType
{
get { return this.dbObjectType; }
}
public EngineType ValidEngineTypes
{
get { return this.validTypes; }
}
public OrderBy[] OrderByList
{
get
{
if (this.schemaBased)
{
return new OrderBy[] { new OrderBy("Schema", OrderBy.Direction.Asc), new OrderBy("Name", OrderBy.Direction.Asc) };
}
else
{
return new OrderBy[] { new OrderBy("Name", OrderBy.Direction.Asc) };
}
}
}
public string[] Fields
{
get
{
if (this.schemaBased)
{
return new string[] { "Urn", "Name", "Schema" };
}
else
{
return new string[] { "Urn", "Name" };
}
}
}
}
#endregion
}
}