-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDesignerContextMenuAddIn.cs
928 lines (784 loc) · 38.9 KB
/
DesignerContextMenuAddIn.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
namespace Microsoft.Dynamics.Samples.AddIns.SSMSAddin
{
using System;
using System.ComponentModel.Composition;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
// These namespaces contain the support for the VS metadata API, the APIs
// describing what is currently open in VS.
using Microsoft.Dynamics.Framework.Tools.Extensibility;
using Microsoft.Dynamics.Framework.Tools.MetaModel.Automation;
using Microsoft.Dynamics.Framework.Tools.MetaModel.Core;
using Microsoft.Dynamics.Framework.Tools.Configuration;
// Convenience prefixes
using TablesAutomation = Microsoft.Dynamics.Framework.Tools.MetaModel.Automation.Tables;
using ViewsAutomation = Microsoft.Dynamics.Framework.Tools.MetaModel.Automation.Views;
// These namespaces contain the general purpose metadata APIs that work
// irrespective of the state of VS.
using Metadata = Microsoft.Dynamics.AX.Metadata;
using Microsoft.Dynamics.AX.Metadata.MetaModel;
/// <summary>
/// This addin opens a window in Microsoft SQL Management studio that allows
/// the user to query the table that the user has selected in VS.
/// </summary>
/// <remarks>Since the class derives from DesignerMenuBase, this addin is
/// available from the designer. The attributes below tell VS to include this addin when the given types are
/// selected in the designer.
/// </remarks>
[Export(typeof(IDesignerMenu))]
[DesignerMenuExportMetadata(AutomationNodeType = typeof(TablesAutomation.ITable))]
[DesignerMenuExportMetadata(AutomationNodeType = typeof(TablesAutomation.ITableExtension))]
[DesignerMenuExportMetadata(AutomationNodeType = typeof(TablesAutomation.IBaseField))]
[DesignerMenuExportMetadata(AutomationNodeType = typeof(TablesAutomation.IFieldGroup))]
[DesignerMenuExportMetadata(AutomationNodeType = typeof(TablesAutomation.ITableHasRelations))]
[DesignerMenuExportMetadata(AutomationNodeType = typeof(TablesAutomation.IRelation))]
[DesignerMenuExportMetadata(AutomationNodeType = typeof(ViewsAutomation.IView))]
[DesignerMenuExportMetadata(AutomationNodeType = typeof(ViewsAutomation.IViewBaseField))]
[DesignerMenuExportMetadata(AutomationNodeType = typeof(ViewsAutomation.IFieldGroup))]
public class SelectInSSMSDesignerAddIn : DesignerMenuBase
{
private static string BusinessDatabaseName
{
get
{
return ConfigurationHelper.CurrentConfiguration.BusinessDatabaseName;
}
}
#region Properties
/// <summary>
/// Caption for the menu item. This is what users would see in the add-in menu.
/// </summary>
public override string Caption
{
get
{
return SSMSSampleAddin.AddinResources.SelectInSSMSDesignerAddinCaption;
}
}
/// <summary>
/// Unique name of the add-in
/// </summary>
public override string Name
{
get
{
return "Select in SQL Server Management Studio";
}
}
/// <summary>
/// Backing field for the metadata provider that is useful for retrieving
/// metadata that is not loaded in the VS instance.
/// </summary>
private Metadata.Providers.IMetadataProvider metadataProvider = null;
/// <summary>
/// Gets a singleton instance of the metadata provider that can access the metadata repository.
/// Any metadata, irrespective of whether it is part of what is being edited by VS, is available
/// through this provider.
/// </summary>
public Metadata.Providers.IMetadataProvider MetadataProvider
{
get
{
if (this.metadataProvider == null)
{
this.metadataProvider = DesignMetaModelService.Instance.CurrentMetadataProvider;
}
return this.metadataProvider;
}
}
#endregion
/// <summary>
/// Resolve the given label to the text it represents in the default language. If a non
/// label is passed, that text is returned unchanged.
/// </summary>
/// <param name="label">The label to look up.</param>
/// <returns>The text for the label resolved to the current language.</returns>
private string ResolveLabel(string label)
{
var labelResolver = CoreUtility.ServiceProvider.GetService(typeof(Microsoft.Dynamics.Framework.Tools.Integration.Interfaces.ILabelResolver)) as Microsoft.Dynamics.Framework.Tools.Integration.Interfaces.ILabelResolver;
if (labelResolver != null)
{
return labelResolver.GetLabelText(label);
}
return label;
}
/// <summary>
/// Get the label provided in the field metadata. If no such label is provided, check
/// if the label's type is an EDT, that possibly has a label. If that fails, return null.
/// </summary>
/// <param name="field">The metadata instance describing the field</param>
/// <returns>The field label, if one is found, or null.</returns>
private string GetFieldLabel(AxTableField field)
{
// if the field has a label, use it.
var fieldLabel = this.ResolveLabel(field.Label);
if (!string.IsNullOrWhiteSpace(fieldLabel))
{
return fieldLabel;
}
// if not, use the label on the EDT, if there is one
if (!string.IsNullOrWhiteSpace(field.ExtendedDataType))
{
// Fetch the EDT through the metadata provider
AxEdt edt = this.MetadataProvider.Edts.Read(field.ExtendedDataType);
if (edt != null)
{
var edtLabel = this.ResolveLabel(edt.Label);
if (!string.IsNullOrWhiteSpace(edtLabel))
{
return edtLabel;
}
}
}
return null;
}
/// <summary>
/// Get the label from the metadata describing a field on a view.
/// </summary>
/// <param name="field">The view field metadata</param>
/// <returns>The label, if one is defined. Otherwise, returns null</returns>
private string GetFieldLabel(AxViewField field)
{
// if the field has a label, use it.
var fieldLabel = this.ResolveLabel(field.Label);
if (!string.IsNullOrWhiteSpace(fieldLabel))
{
return fieldLabel;
}
return null;
}
#region Utility functions for adding lines to the file
/// <summary>
/// Append the field in the given tabular object to the output, one field per line.
/// </summary>
/// <param name="result">The string containing the result</param>
/// <param name="tableName">The unmangled name of the tabular object</param>
/// <param name="fieldName">The unmangled name of the field</param>
/// <param name="fieldLabel">An optional label to use as the prompt. If this is null, the name is used.</param>
/// <param name="first">Determines whether the first field has been written or not.</param>
private void AddField(StringBuilder result, string tableName, string fieldName, string label, ref bool first)
{
// Fields are indented to be positioned under the select statement
result.Append(" ");
if (!first)
{
result.Append(",");
}
result.Append(SqlNameMangling.GetSqlTableName(tableName) + ".[" + SqlNameMangling.GetValidSqlNameForField(fieldName) + "]");
result.AppendLine(string.Format(CultureInfo.InvariantCulture, " as '{0}'", label ?? fieldName));
first = false;
}
/// <summary>
/// Add the commaseparated fields in the collection by calling AddField on each one.
/// If the field is an array field, all the array elements are added.
/// </summary>
/// <param name="result">The result where the fields are added</param>
/// <param name="fields">The collection of fields to add</param>
/// <param name="first">A parameter specifying whether or not this is the first field.</param>
private void AddFields(StringBuilder result, AxTable table, IEnumerable<AxTableField> fields, ref bool first)
{
foreach (AxTableField field in fields)
{
if (field.SaveContents == Metadata.Core.MetaModel.NoYes.Yes)
{
var label = this.GetFieldLabel(field);
this.AddField(result, table.Name, field.Name, label, ref first);
var edt = field.ExtendedDataType;
if (!string.IsNullOrWhiteSpace(edt))
{
// See if it happens to be an array field. If so, the first index
// does not have a suffix ([<n>]), and has already been written.
if (this.MetadataProvider.Edts.Exists(edt))
{
AxEdt typeDefinition = this.metadataProvider.Edts.Read(edt);
for (int i = 2; i <= typeDefinition.ArrayElements.Count + 1; i++)
{
var fn = field.Name + "[" + i.ToString(CultureInfo.InvariantCulture) + "]";
this.AddField(result, table.Name, fn, null, ref first);
}
}
}
}
}
}
/// <summary>
/// Add the system fields to the output file, if the metadata describing the table
/// indicates that the field is used.
/// </summary>
/// <param name="result">The string builder that contains the output</param>
/// <param name="table">The metadata describing the table.</param>
/// <param name="first">A parameter specifying whether or not this is the first field.</param>
private void AddSystemFields(StringBuilder result, AxTable table, ref bool first)
{
if (table.CreatedBy == Metadata.Core.MetaModel.NoYes.Yes)
{
this.AddField(result, table.Name, "CreatedBy", null, ref first);
}
if (table.CreatedDateTime == Metadata.Core.MetaModel.NoYes.Yes)
{
this.AddField(result, table.Name, "CreatedDateTime", null, ref first);
}
if (table.ModifiedBy == Metadata.Core.MetaModel.NoYes.Yes)
{
this.AddField(result, table.Name, "ModifiedBy", null, ref first);
}
if (table.ModifiedDateTime == Metadata.Core.MetaModel.NoYes.Yes)
{
this.AddField(result, table.Name, "ModifiedDateTime", null, ref first);
}
if (table.SaveDataPerPartition == Metadata.Core.MetaModel.NoYes.Yes)
{
this.AddField(result, table.Name, "Partition", null, ref first);
}
if (table.SaveDataPerCompany == Metadata.Core.MetaModel.NoYes.Yes)
{
this.AddField(result, table.Name, "DataAreaId", null, ref first);
}
this.AddField(result, table.Name, "RecId", null, ref first);
}
#endregion
/// <summary>
/// Find the sequence of tables from the table to its root in the inheritance chain.
/// If the indicated table is not part of a supertype subtype relationship, the
/// result contains just the single table.
/// </summary>
/// <param name="leafTableName">The most derived table.</param>
/// <returns>The sequence of tables, with the root at the top of the stack.</returns>
private Stack<AxTable> SuperTables(string leafTableName)
{
Stack<AxTable> result = new Stack<AxTable>();
AxTable table = this.MetadataProvider.Tables.Read(leafTableName);
while (table.SupportInheritance == Metadata.Core.MetaModel.NoYes.Yes
&& !string.IsNullOrWhiteSpace(table.Extends))
{
result.Push(table);
table = this.MetadataProvider.Tables.Read(table.Extends);
}
result.Push(table); // stack the root.
return result;
}
/// <summary>
/// The method is called when the user has selected to view a single table.
/// The method generates a select on all fields, with no joins.
/// </summary>
/// <param name="selectedTable">The designer metadata designating the table.</param>
/// <returns>The string containing the SQL command.</returns>
private StringBuilder GenerateFromTable(TablesAutomation.ITable selectedTable)
{
var result = new StringBuilder();
// It is indeed a table. Look at the properties
if (!selectedTable.IsKernelTable)
{
bool first = true;
result.AppendLine(string.Format(CultureInfo.InvariantCulture, "use {0}", BusinessDatabaseName));
result.AppendLine("go");
Stack<AxTable> tables = this.SuperTables(selectedTable.Name);
// List any developer documentation as a SQL comment:
if (!string.IsNullOrEmpty(selectedTable.DeveloperDocumentation))
{
result.Append("-- " + selectedTable.Name);
result.AppendLine(" : " + this.ResolveLabel(selectedTable.DeveloperDocumentation));
}
else
{
result.AppendLine();
}
result.AppendLine("select ");
this.AddFields(result, tables.First(), tables.First().Fields, ref first);
this.AddSystemFields(result, tables.First(), ref first);
result.AppendLine("from " + SqlNameMangling.GetSqlTableName(tables.First().Name));
// If this table saves data per company, then add the where clause for
// the user to fill out or ignore.
if (tables.First().SaveDataPerCompany == Metadata.Core.MetaModel.NoYes.Yes)
{
result.AppendLine("-- where " + SqlNameMangling.GetValidSqlNameForField("DataAreaId") + " = 'DAT'");
}
}
return result;
}
/// <summary>
/// The method is called when the user has selected to view a single view.
/// The method generates a select on all fields, with no joins.
/// </summary>
/// <param name="selectedTable">The designer metadata designating the view.</param>
/// <returns>The string containing the SQL command.</returns>
private StringBuilder GenerateFromView(ViewsAutomation.IView selectedView)
{
var result = new StringBuilder();
result.AppendLine(string.Format(CultureInfo.InvariantCulture, "use {0}", BusinessDatabaseName));
result.AppendLine("go");
if (!string.IsNullOrEmpty(selectedView.DeveloperDocumentation))
{
result.Append("-- " + selectedView.Name);
result.AppendLine(" : " + this.ResolveLabel(selectedView.DeveloperDocumentation));
}
else
{
result.AppendLine();
}
result.AppendLine("select * ");
result.AppendLine("from " + SqlNameMangling.GetSqlTableName(selectedView.Name));
return result;
}
/// <summary>
/// The method is called when the user has selected to view a table extension instance.
/// The method generates a select on all fields, with no joins.
/// </summary>
/// <param name="selectedExtensionTable">The designer metadata designating the view.</param>
/// <returns>The string containing the SQL command.</returns>
private StringBuilder GenerateFromTableExtension(TablesAutomation.ITableExtension selectedExtensionTable)
{
var result = new StringBuilder();
bool first = true;
result.AppendLine(string.Format(CultureInfo.InvariantCulture, "use {0}", BusinessDatabaseName));
result.AppendLine("go");
result.AppendLine();
AxTableExtension extension = this.MetadataProvider.TableExtensions.Read(selectedExtensionTable.Name);
var baseTableName = selectedExtensionTable.Name.Split('.').First();
var tables = this.SuperTables(baseTableName);
HashSet<string> extendedFields = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var extendedField in extension.Fields)
{
extendedFields.Add(extendedField.Name);
}
result.AppendLine("select ");
// List the extension fields first...
this.AddFields(result, tables.First(), extension.Fields, ref first);
// Then the normal ones...
this.AddFields(result, tables.First(), tables.First().Fields.Where(f => !extendedFields.Contains(f.Name)), ref first);
// And then system fields
this.AddSystemFields(result, tables.First(), ref first);
result.AppendLine("from " + SqlNameMangling.GetSqlTableName(tables.First().Name));
if (tables.First().SaveDataPerCompany == Metadata.Core.MetaModel.NoYes.Yes)
{
result.AppendLine("-- where " + SqlNameMangling.GetValidSqlNameForField("DataAreaId") + " = 'DAT'");
}
return result;
}
/// <summary>
/// Generate the SQL command selecting the given fields from the underlying table.
/// </summary>
/// <param name="fields">The list of fields to select</param>
/// <returns>The string containing the SQL command.</returns>
private StringBuilder GenerateFromTableFieldList(IEnumerable<TablesAutomation.IBaseField> fields)
{
var result = new StringBuilder();
bool first = true;
result.AppendLine(string.Format(CultureInfo.InvariantCulture, "use {0}", BusinessDatabaseName));
result.AppendLine("go");
if (!fields.Any())
{
return result;
}
var tableName = string.Empty;
TablesAutomation.ITable selectedTable = fields.First().Table;
if (selectedTable != null)
{
tableName = selectedTable.Name;
}
else
{
var extensionTableName = fields.First().TableExtension.Name;
tableName = extensionTableName.Split('.').First();
}
Stack<AxTable> tables = this.SuperTables(tableName);
// Expand the developer documentation, if any
if (!string.IsNullOrEmpty(tables.First().DeveloperDocumentation))
{
result.Append("-- " + tables.First().Name);
result.AppendLine(" : " + this.ResolveLabel(tables.First().DeveloperDocumentation));
}
else
{
result.AppendLine();
}
result.AppendLine("select");
// Calculate the union of the fields in all the tables in the hierarchy:
var fieldUnion = new Dictionary<string, AxTableField>(StringComparer.OrdinalIgnoreCase);
foreach (var table in tables)
{
foreach (var field in table.Fields)
{
fieldUnion[field.Name] = field;
}
}
// Now pick the fields that the user actually wants to see
var selectedFields = new List<AxTableField>();
foreach (var field in fields)
{
selectedFields.Add(fieldUnion[field.Name]);
}
this.AddFields(result, tables.First(), selectedFields, ref first);
this.AddSystemFields(result, tables.First(), ref first);
result.AppendLine("from " + SqlNameMangling.GetSqlTableName(tables.First().Name));
return result;
}
/// <summary>
/// Generate the SQL command selecting the given fields from the underlying table.
/// </summary>
/// <param name="fields">The list of fields to select</param>
/// <returns>The string containing the SQL command.</returns>
private StringBuilder GenerateFromTableFieldListWithWhere(IEnumerable<TablesAutomation.IBaseField> fields)
{
var result = this.GenerateFromTableFieldList(fields);
if (fields.Any())
{
var tables = this.SuperTables(fields.First().Table.Name);
// If this table saves data per company, then add the where clause for
// the user to fill out.
if (tables.First().SaveDataPerCompany == Metadata.Core.MetaModel.NoYes.Yes)
{
// Note: There is no way to get to the actual company, since the server
// is not involved in this at all.
result.AppendLine("-- where " + SqlNameMangling.GetValidSqlNameForField("DataAreaId") + " = 'DAT'");
}
}
return result;
}
private AxTable FindTableInEmbeddedDataSources(IEnumerable<AxQuerySimpleEmbeddedDataSource> embeddedDataSources, string dataSourceName)
{
foreach (AxQuerySimpleEmbeddedDataSource dataSource in embeddedDataSources)
{
if (string.Compare(dataSourceName, dataSourceName, StringComparison.OrdinalIgnoreCase) == 0)
{
return this.MetadataProvider.Tables.Read(dataSource.Table);
}
return FindTableInEmbeddedDataSources(dataSource.DataSources, dataSourceName);
}
return null;
}
private AxTable FindTableInDataSource(AxView view, string dataSourceName)
{
foreach (AxQuerySimpleRootDataSource ds in view.ViewMetadata.DataSources)
{
if (string.Compare(dataSourceName, ds.Name, StringComparison.OrdinalIgnoreCase) == 0)
{
return this.MetadataProvider.Tables.Read(ds.Table);
}
return FindTableInEmbeddedDataSources(ds.DataSources, dataSourceName);
}
return null;
}
/// <summary>
/// Generate the SQL command selecting the given fields from the underlying table.
/// </summary>
/// <param name="fields">The list of fields to select</param>
/// <returns>The string containing the SQL command.</returns>
private StringBuilder GenerateFromViewFieldList(IEnumerable<ViewsAutomation.IViewBaseField> fields)
{
var result = new StringBuilder();
result.AppendLine(string.Format(CultureInfo.InvariantCulture, "use {0}", BusinessDatabaseName));
result.AppendLine("go");
if (!fields.Any())
{
return result;
}
ViewsAutomation.IView selectedView1 = fields.FirstOrDefault().View;
AxView view = this.MetadataProvider.Views.Read(selectedView1.Name);
// Expand the developer documentation, if any
if (!string.IsNullOrEmpty(view.DeveloperDocumentation))
{
result.Append("-- " + view.Name);
result.AppendLine(" : " + this.ResolveLabel(view.DeveloperDocumentation));
}
else
{
result.AppendLine();
}
result.AppendLine("select");
bool first = true;
foreach (ViewsAutomation.IViewField field in fields.OfType<ViewsAutomation.IViewField>())
{
this.AddField(result, view.Name, field.Name, null, ref first);
// The field name refers to a name on the datasource. Find the datasource
// and the underlying table.
AxTable table = this.FindTableInDataSource(view, field.DataSource);
table = this.SuperTables(table.Name).First();
if (table != null)
{
AxTableField tableField = table.Fields[field.DataField];
if (tableField != null)
{
var edt = tableField.ExtendedDataType;
if (!string.IsNullOrWhiteSpace(edt))
{
// See if it happens to be an array field. If so, the first index
// does not have a suffix ([<n>]), and has already been written.
if (this.MetadataProvider.Edts.Exists(edt))
{
AxEdt typeDefinition = this.metadataProvider.Edts.Read(edt);
for (int i = 2; i <= typeDefinition.ArrayElements.Count + 1; i++)
{
var fn = field.Name + "[" + i.ToString(CultureInfo.InvariantCulture) + "]";
this.AddField(result, view.Name, fn, null, ref first);
}
}
}
}
}
}
// Now deal with computed columns.
foreach (ViewsAutomation.IViewComputedColumn computedColumn in fields.OfType<ViewsAutomation.IViewComputedColumn>())
{
this.AddField(result, view.Name, computedColumn.Name, null, ref first);
}
result.AppendLine("from " + SqlNameMangling.GetSqlTableName(view.Name));
return result;
}
/// <summary>
/// Generate the SQL command selecting the set of fields from the given field groups
/// from the underlying table.
/// </summary>
/// <param name="selectedFieldGroups">The list of field groups to select fields from.</param>
/// <returns>The string containing the SQL command.</returns>
private StringBuilder GenerateFromTableFieldGroups(IEnumerable<TablesAutomation.IFieldGroup> selectedFieldGroups)
{
var result = new StringBuilder();
if (!selectedFieldGroups.Any())
{
return result;
}
// We are implicitly assuming that all the field groups belong to the same table.
TablesAutomation.ITable selectedTable = selectedFieldGroups.FirstOrDefault().Table;
// Used to capture the set of fields containing the union of the fields
// from the selected field groups.
var fields = new HashSet<string>();
// Unwrap the fields in all of the selected field groups, using the set
// to weed out duplicate fields (that occur in multiple field groups)
foreach (var fieldGroup in selectedFieldGroups)
{
var refs = fieldGroup.FieldGroupFieldReferences;
foreach (IFieldGroupFieldReference reference in refs)
{
fields.Add(reference.DataField);
}
}
// Now the (unique) set of fields is captured.
// Generate the SQL query from the IBaseField references.
IList<TablesAutomation.IBaseField> fieldsToSelect = new List<TablesAutomation.IBaseField>();
foreach (TablesAutomation.IBaseField child in selectedTable.BaseFields)
{
if (fields.Contains(child.Name))
fieldsToSelect.Add(child);
}
result = this.GenerateFromTableFieldList(fieldsToSelect);
// If this table saves data per company, then add the where clause for
// the user to fill out.
if (selectedTable.SaveDataPerCompany == Metadata.Core.MetaModel.NoYes.Yes)
{
// Note: There is no way to get to the actual company, since the server
// is not involved in this at all.
result.AppendLine("-- where " + SqlNameMangling.GetValidSqlNameForField("DataAreaId") + " = 'DAT'");
}
return result;
}
/// <summary>
/// Generate the SQL command selecting the set of fields from the given field groups
/// from the underlying view.
/// </summary>
/// <param name="selectedFieldGroups">The list of field groups to select fields from.</param>
/// <returns>The string containing the SQL command.</returns>
private StringBuilder GenerateFromViewFieldGroups(IEnumerable<ViewsAutomation.IFieldGroup> selectedFieldGroups)
{
var result = new StringBuilder();
if (!selectedFieldGroups.Any())
{
return result;
}
// Used to capture the set of fields containing the union of the fields
// from the selected field groups.
var fields = new HashSet<string>();
ViewsAutomation.IView selectedView = selectedFieldGroups.FirstOrDefault().View;
foreach (var fieldGroup in selectedFieldGroups)
{
var refs = fieldGroup.FieldGroupFieldReferences;
foreach (IFieldGroupFieldReference reference in refs)
{
fields.Add(reference.DataField);
}
}
// Now the (unique) set of fields is captured.
// Generate the SQL query from the IBaseField references.
IList<ViewsAutomation.IViewBaseField> fieldsToSelect = new List<ViewsAutomation.IViewBaseField>();
foreach (ViewsAutomation.IViewBaseField child in selectedView.ViewBaseFields)
{
if (fields.Contains(child.Name))
fieldsToSelect.Add(child);
}
return this.GenerateFromViewFieldList(fieldsToSelect);
}
/// <summary>
/// Generate the SQL command selecting the fields from the selected table, performing
/// joins on the tables indicated by the relations selected.
/// </summary>
/// <param name="selectedRelations">The list of field groups to select fields from.</param>
/// <returns>The string containing the SQL command.</returns>
private StringBuilder GenerateFromTableRelations(IEnumerable<TablesAutomation.IRelation> selectedRelations)
{
TablesAutomation.ITable table = selectedRelations.First().Table;
Stack<AxTable> tables = this.SuperTables(table.Name);
IList<TablesAutomation.IBaseField> fields = new List<TablesAutomation.IBaseField>();
foreach (TablesAutomation.IBaseField field in table.BaseFields)
{
if (field.SaveContents == Metadata.Core.MetaModel.NoYes.Yes)
{
fields.Add(field);
}
}
var result = this.GenerateFromTableFieldList(fields);
int disambiguation = 1;
// Now add the joins. Assume that multiple relations can be
// selected; joins will be added for all of them.
foreach (var relation in selectedRelations)
{
var relatedTabularObjectName = relation.RelatedTable;
// TODO: In principle this could be a view. Assuming table for now
result.AppendLine(
string.Format(CultureInfo.InvariantCulture, "inner join {0} as t{1}", SqlNameMangling.GetSqlTableName(relatedTabularObjectName), disambiguation));
result.Append("on ");
bool first = true;
foreach (TablesAutomation.IRelationConstraint constraint in relation.RelationConstraints)
{
if (!first)
result.Append(" and ");
if (constraint is TablesAutomation.IRelationConstraintField)
{ // Table.field = RelatedTable.relatedField
var fieldConstraint = constraint as TablesAutomation.IRelationConstraintField;
result.Append(SqlNameMangling.GetSqlTableName(table.Name) + ".[" + SqlNameMangling.GetValidSqlNameForField(fieldConstraint.Field) + "]");
result.Append(" = ");
result.AppendLine(string.Format(CultureInfo.InvariantCulture, "t{0}", disambiguation) + "." + SqlNameMangling.GetValidSqlNameForField(fieldConstraint.RelatedField));
}
else if (constraint is TablesAutomation.IRelationConstraintFixed)
{ // Table.field = value
var fixedConstraint = constraint as TablesAutomation.IRelationConstraintFixed;
result.Append(SqlNameMangling.GetSqlTableName(table.Name) + ".[" + SqlNameMangling.GetValidSqlNameForField(fixedConstraint.Field) + "]");
result.Append(" = ");
result.AppendLine(fixedConstraint.Value.ToString(CultureInfo.InvariantCulture));
}
else if (constraint is TablesAutomation.IRelationConstraintRelatedFixed)
{ // Value = RelatedTable.field
var relatedFixedConstraint = constraint as TablesAutomation.IRelationConstraintRelatedFixed;
result.Append(relatedFixedConstraint.Value);
result.Append(" = ");
result.AppendLine(string.Format(CultureInfo.InvariantCulture, "t{0}", disambiguation) + ".[" + SqlNameMangling.GetValidSqlNameForField(relatedFixedConstraint.RelatedField) + "]");
}
first = false;
}
disambiguation += 1;
}
// If this table saves data per company, then add the where clause for
// the user to fill out.
if (tables.First().SaveDataPerCompany == Metadata.Core.MetaModel.NoYes.Yes)
{
// Note: There is no way to get to the actual company, since the server
// is not involved in this at all.
result.AppendLine("-- where " + SqlNameMangling.GetValidSqlNameForField("DataAreaId") + " = 'DAT'");
}
return result;
}
#region Callbacks
/// <summary>
/// Called when user clicks on the add-in menu
/// </summary>
/// <param name="e">The context of the VS tools and metadata</param>
public override void OnClick(AddinDesignerEventArgs e)
{
try
{
StringBuilder result = null;
TablesAutomation.ITable selectedTable;
if ((selectedTable = e.SelectedElement as TablesAutomation.ITable) != null)
{
result = this.GenerateFromTable(selectedTable);
}
if (result == null)
{
ViewsAutomation.IView selectedView;
if ((selectedView = e.SelectedElement as ViewsAutomation.IView) != null)
{
result = this.GenerateFromView(selectedView);
}
}
if (result == null)
{
TablesAutomation.ITableExtension selectedTableExtension;
if ((selectedTableExtension = e.SelectedElement as TablesAutomation.ITableExtension) != null)
{
result = this.GenerateFromTableExtension(selectedTableExtension);
}
}
// Individually selected table fields.
if (result == null)
{
var selectedFields = e.SelectedElements.OfType<TablesAutomation.IBaseField>();
if (selectedFields.Any())
{
result = this.GenerateFromTableFieldListWithWhere(selectedFields);
}
}
// Individually selected view fields.
if (result == null)
{
var selectedFields = e.SelectedElements.OfType<ViewsAutomation.IViewBaseField>();
if (selectedFields.Any())
{
result = this.GenerateFromViewFieldList(selectedFields);
}
}
// Table field groups
if (result == null)
{
var selectedFieldsGroups = e.SelectedElements.OfType<TablesAutomation.IFieldGroup>();
if (selectedFieldsGroups.Any())
{
result = this.GenerateFromTableFieldGroups(selectedFieldsGroups);
}
}
// View field groups
if (result == null)
{
var selectedFieldsGroups = e.SelectedElements.OfType<ViewsAutomation.IFieldGroup>();
if (selectedFieldsGroups.Any())
{
result = this.GenerateFromViewFieldGroups(selectedFieldsGroups);
}
}
// Table relations
if (result == null)
{
var selectedRelations = e.SelectedElements.OfType<TablesAutomation.IRelation>();
if (selectedRelations.Any())
{
result = this.GenerateFromTableRelations(selectedRelations);
}
}
if (result != null)
{
// Save the SQL file and open it in SQL management studio.
string temporaryFileName = Path.GetTempFileName();
// Rename and move
var sqlFileName = temporaryFileName.Replace(".tmp", ".sql");
File.Move(temporaryFileName, sqlFileName);
// Store the script in the file
File.AppendAllText(sqlFileName, result.ToString());
//var dte = CoreUtility.ServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
//dte.ExecuteCommand("File.OpenFile", sqlFileName);
Process sqlManagementStudio = new Process();
sqlManagementStudio.StartInfo.FileName = sqlFileName;
sqlManagementStudio.StartInfo.UseShellExecute = true;
sqlManagementStudio.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
sqlManagementStudio.Start();
}
}
catch (Exception ex)
{
CoreUtility.HandleExceptionWithErrorMessage(ex);
}
}
#endregion
}
}