-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathAutomatedDataDictionary.cs
253 lines (214 loc) · 8.97 KB
/
AutomatedDataDictionary.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
// Initial Parameters
string dataSource = @""; // Enter the name of the data source in the model that the Data Dictionary will use.
string serverName = @""; // Enter the server name (where the Data Dictionary table will reside in the data warehouse).
string databaseName = ""; // Enter the database name (where the Data Dictionary table will reside in the data warehouse).
string schemaName = ""; // Enter the schema name (ensure the schema exists).
string dbTableName = ""; // Enter the table name for the Data Dictionary in the data warehouse.
string modelName = Model.Database.Name;
// Error check for parameters
if (!Model.DataSources.Any(a=>a.Name == dataSource))
{
Error("Must enter a valid 'Data Source' in the dataSource parameter");
return;
}
if (serverName.Length == 0)
{
Error("Must enter a valid 'Server' in the serverName parameter");
return;
}
if (databaseName.Length == 0)
{
Error("Must enter a valid 'Database' in the databaseName parameter");
return;
}
if (schemaName.Length == 0)
{
Error("Must enter a valid 'Schema' in the schemaName parameter");
return;
}
if (dbTableName.Length == 0)
{
Error("Must enter a valid 'Table Name' in the dbTableName parameter");
return;
}
if (modelName.Length == 0)
{
Error("Must enter a valid 'Model Name' in the modelName parameter");
return;
}
// Create Data Dictionary table within the model (if it does not already exist)
string ddTableName = "Data Dictionary";
string[] sourceColName = {"ModelName","TableName","ObjectType","ObjectName","HiddenFlag","Description","DisplayFolder","MeasureFormula"};
string[] colName = {"Model","Table","Object Type","Object","Hidden Flag","Description","Display Folder","Measure Formula"};
if (Model.Tables.Any(a => a.Name == ddTableName) == false)
{
var t = Model.AddTable(ddTableName);
t.Partitions[0].DataSource = Model.DataSources[dataSource];
t.Partitions[0].Query = "SELECT * FROM ["+schemaName+"].["+dbTableName+"]";
for (int i=0;i<colName.Length; i++)
{
var c = t.AddDataColumn(colName[i]);
c.SourceColumn = sourceColName[i];
c.DataType = DataType.String;
}
}
// Create Data Dictionary table within the Data Warehouse
string newLine = Environment.NewLine;
string connectionString = @"Data Source="+serverName+";Initial Catalog="+databaseName+";Integrated Security=True";
string sql = "DROP TABLE IF EXISTS ["+schemaName+"].["+dbTableName+"] "+newLine+
"CREATE TABLE ["+schemaName+"].["+dbTableName+"] " +newLine+
"(" +newLine+
" [ModelName] VARCHAR(100)" +newLine+
",[TableName] VARCHAR(200)" +newLine+
",[ObjectType] VARCHAR(30)" +newLine+
",[ObjectName] VARCHAR(250)" +newLine+
",[HiddenFlag] VARCHAR(10)" +newLine+
",[Description] VARCHAR(MAX)" +newLine+
",[DisplayFolder] VARCHAR(150)" +newLine+
",[MeasureFormula] VARCHAR(MAX)" +newLine+
")";
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
// Set up the insert statement for sending the metadata to the data warehouse
string insertSQL;
var sb_InsertSQL = new System.Text.StringBuilder();
sb_InsertSQL.Append("INSERT INTO ["+schemaName+"].["+dbTableName+"]");
sb_InsertSQL.Append(Environment.NewLine);
// Extract model metadata in the data dictionary format
foreach (var t in Model.Tables.Where(a => a.ObjectType.ToString() != "CalculationGroupTable" && a.Name != ddTableName).OrderBy(a => a.Name).ToList())
{
string tableName = t.Name;
string tableDesc = t.Description.Replace("'","''");
string objectType = "Table";
string hiddenFlag;
string expr;
if (t.IsHidden)
{
hiddenFlag = "Yes";
}
else
{
hiddenFlag = "No";
}
if (t.SourceType.ToString() == "Calculated")
{
expr = (Model.Tables[tableName] as CalculatedTable).Expression;
// Remove tabs and new lines
expr = expr.Replace("\n"," ");
expr = expr.Replace("\t"," ");
expr = expr.Replace("'","''");
sb_InsertSQL.Append("SELECT"+"'"+modelName+"','"+tableName+"','"+objectType+"','"+tableName+"','"+hiddenFlag+"','"+tableDesc+"','"+"','"+expr+"' UNION ALL ");
}
else
{
sb_InsertSQL.Append("SELECT"+"'"+modelName+"','"+tableName+"','"+objectType+"','"+tableName+"','"+hiddenFlag+"','"+tableDesc+"','"+"','"+"***N/A***' UNION ALL ");
}
sb_InsertSQL.Append(Environment.NewLine);
foreach (var o in t.Columns.OrderBy(a => a.Name).ToList())
{
string objectName = o.Name;
string objectDesc = o.Description.Replace("'","''");
string objectDF = o.DisplayFolder.Replace("'","''");
objectType = "Attribute";
if (o.IsHidden)
{
hiddenFlag = "Yes";
}
else
{
hiddenFlag = "No";
}
if (o.Type.ToString() == "Calculated")
{
expr = (Model.Tables[tableName].Columns[objectName] as CalculatedColumn).Expression;
// Remove tabs and new lines
expr = expr.Replace("\n"," ");
expr = expr.Replace("\t"," ");
expr = expr.Replace("'","''");
sb_InsertSQL.Append("SELECT"+"'"+modelName+"','"+tableName+"','"+objectType+"','"+objectName+"','"+hiddenFlag+"','"+objectDesc+"','"+objectDF+"','"+expr+"' UNION ALL ");
}
else
{
sb_InsertSQL.Append("SELECT"+"'"+modelName+"','"+tableName+"','"+objectType+"','"+objectName+"','"+hiddenFlag+"','"+objectDesc+"','"+objectDF+"','***N/A***' UNION ALL ");
}
sb_InsertSQL.Append(Environment.NewLine);
}
foreach (var o in t.Measures.OrderBy(a => a.Name).ToList())
{
string objectName = o.Name;
string objectDesc = o.Description.Replace("'","''");
string objectDF = o.DisplayFolder.Replace("'","''");
objectType = "Measure";
expr = o.Expression;
// Remove tabs and new lines
expr = expr.Replace("\n"," ");
expr = expr.Replace("\t"," ");
expr = expr.Replace("'","''");
if (o.IsHidden)
{
hiddenFlag = "Yes";
}
else
{
hiddenFlag = "No";
}
sb_InsertSQL.Append("SELECT"+"'"+modelName+"','"+tableName+"','"+objectType+"','"+objectName+"','"+hiddenFlag+"','"+objectDesc+"','"+objectDF+"','"+expr+"' UNION ALL ");
sb_InsertSQL.Append(Environment.NewLine);
}
foreach (var o in t.Hierarchies.OrderBy(a => a.Name).ToList())
{
string objectName = o.Name;
string objectDesc = o.Description.Replace("'","''");
string objectDF = o.DisplayFolder.Replace("'","''");
objectType = "Hierarchy";
if (o.IsHidden)
{
hiddenFlag = "Yes";
}
else
{
hiddenFlag = "No";
}
sb_InsertSQL.Append("SELECT"+"'"+modelName+"','"+tableName+"','"+objectType+"','"+objectName+"','"+hiddenFlag+"','"+objectDesc+"','"+objectDF+"','***N/A***' UNION ALL ");
sb_InsertSQL.Append(Environment.NewLine);
}
}
foreach (var o in Model.CalculationGroups.ToList())
{
string tableName = o.Name;
string tableDesc = o.Description.Replace("'","''");
string hiddenFlag;
if (o.IsHidden)
{
hiddenFlag = "Yes";
}
else
{
hiddenFlag = "No";
}
sb_InsertSQL.Append("SELECT"+"'"+modelName+"','"+tableName+"','Calculation Group','"+tableName+"','"+hiddenFlag+"','"+tableDesc+"','','***N/A***' UNION ALL ");
sb_InsertSQL.Append(Environment.NewLine);
foreach (var i in o.CalculationItems.ToList())
{
string objectName = i.Name;
string objectDesc = i.Description.Replace("'","''");
string expr = i.Expression;
// Remove tabs and new lines
expr = expr.Replace("\n"," ");
expr = expr.Replace("\t"," ");
expr = expr.Replace("'","''");
sb_InsertSQL.Append("SELECT"+"'"+modelName+"','"+tableName+"','Calculation Item','"+objectName+"','No','"+objectDesc+"','','"+expr+"' UNION ALL ");
sb_InsertSQL.Append(Environment.NewLine);
}
}
// Remove the extra comma
insertSQL = sb_InsertSQL.ToString().Trim();
insertSQL = insertSQL.Substring(0,insertSQL.Length-10);
// Insert the data dictionary metadata into the data warehouse
System.Data.SqlClient.SqlCommand cmdInsert = new System.Data.SqlClient.SqlCommand(insertSQL, con);
cmdInsert.ExecuteNonQuery();
cmdInsert.Connection.Close();
cmd.Connection.Close();
con.Close();