internal static int SyncDescriptions(Dimension d, bool bPromptForProperties, IServiceProvider provider, bool bIsTabular) { int iUpdatedDescriptions = 0; DataSource dataSource = d.DataSource; ColumnBinding colDimensionKey = null; #if DENALI || SQL2014 if (d.KeyAttribute.KeyColumns[0].Source is RowNumberBinding) { foreach (DimensionAttribute a in d.Attributes) { if (a.KeyColumns != null && a.KeyColumns.Count > 0 && a.KeyColumns[0].Source is ColumnBinding) { colDimensionKey = GetColumnBindingForDataItem(a.KeyColumns[0]); break; } } if (colDimensionKey == null) { throw new Exception("Couldn't find an attribute with a ColumnBinding, so couldn't find DSV table."); } } else { colDimensionKey = GetColumnBindingForDataItem(d.KeyAttribute.KeyColumns[0]); } #else colDimensionKey = GetColumnBindingForDataItem(d.KeyAttribute.KeyColumns[0]); #endif DataTable oDimensionKeyTable = d.DataSourceView.Schema.Tables[colDimensionKey.TableID]; //if this is a Tabular model, the Dimension.DataSource may point at the default data source for the data source view if (oDimensionKeyTable.ExtendedProperties.ContainsKey("DataSourceID")) { dataSource = d.Parent.DataSources[oDimensionKeyTable.ExtendedProperties["DataSourceID"].ToString()]; } IServiceProvider settingService = dataSource.Site; if (settingService == null) { settingService = provider; } Microsoft.DataWarehouse.Design.DataSourceConnection openedDataSourceConnection = Microsoft.DataWarehouse.DataWarehouseUtilities.GetOpenedDataSourceConnection((object)null, dataSource.ID, dataSource.Name, dataSource.ManagedProvider, dataSource.ConnectionString, settingService, false); try { if (d.MiningModelID != null) { return(iUpdatedDescriptions); } try { if (openedDataSourceConnection != null) { openedDataSourceConnection.QueryTimeOut = (int)dataSource.Timeout.TotalSeconds; } } catch { } if (openedDataSourceConnection == null) { throw new Exception("Unable to connect to data source [" + d.DataSource.Name + "]."); } else { sq = openedDataSourceConnection.Cartridge.IdentStartQuote; fq = openedDataSourceConnection.Cartridge.IdentEndQuote; DBServerName = openedDataSourceConnection.DBServerName; cartridge = openedDataSourceConnection.Cartridge; if (DBServerName != "Microsoft SQL Server") { MessageBox.Show("Data source [" + d.DataSource.Name + "] connects to " + DBServerName + " which may not be supported."); } String sql = "select distinct Name from sys.extended_properties order by Name"; if (bPromptForProperties) { DataSet dsExtendedProperties = new DataSet(); openedDataSourceConnection.Fill(dsExtendedProperties, sql); BIDSHelper.SSAS.SyncDescriptionsForm form = new BIDSHelper.SSAS.SyncDescriptionsForm(); form.cmbDescriptionProperty.DataSource = dsExtendedProperties.Tables[0]; form.cmbDescriptionProperty.DisplayMember = "Name"; form.cmbDescriptionProperty.ValueMember = "Name"; foreach (DataRow row in dsExtendedProperties.Tables[0].Rows) { form.listOtherProperties.Items.Add(row["Name"].ToString()); } DialogResult result = form.ShowDialog(); if (result != DialogResult.OK) { return(iUpdatedDescriptions); } DescriptionPropertyName = form.cmbDescriptionProperty.GetItemText(form.cmbDescriptionProperty.SelectedItem); List <string> listOtherProperties = new List <string>(); for (int i = 0; i < form.listOtherProperties.CheckedItems.Count; i++) { listOtherProperties.Add(form.listOtherProperties.GetItemText(form.listOtherProperties.CheckedItems[i])); } OtherPropertyNamesToInclude = listOtherProperties.ToArray(); OverwriteExistingDescriptions = form.chkOverwriteExistingDescriptions.Checked; } if ((string.IsNullOrEmpty(d.Description) || OverwriteExistingDescriptions) && (!oDimensionKeyTable.ExtendedProperties.ContainsKey("QueryDefinition") || bIsTabular) && //Tabular always has a QueryDefinition, even when it's just a table binding oDimensionKeyTable.ExtendedProperties.ContainsKey("DbTableName") && oDimensionKeyTable.ExtendedProperties.ContainsKey("DbSchemaName")) { sql = "SELECT PropertyName = p.name" + "\r\n" + ",PropertyValue = CAST(p.value AS sql_variant)" + "\r\n" + "FROM sys.all_objects AS tbl" + "\r\n" + "INNER JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id" + "\r\n" + "INNER JOIN sys.extended_properties AS p ON p.major_id=tbl.object_id AND p.minor_id=0 AND p.class=1" + "\r\n" + "where sch.name = '" + oDimensionKeyTable.ExtendedProperties["DbSchemaName"].ToString().Replace("'", "''") + "'\r\n" + "and tbl.name = '" + oDimensionKeyTable.ExtendedProperties["DbTableName"].ToString().Replace("'", "''") + "'\r\n" + "order by p.name"; string sNewDimensionDescription = ""; DataSet dsTableProperties = new DataSet(); openedDataSourceConnection.Fill(dsTableProperties, sql); foreach (DataRow row in dsTableProperties.Tables[0].Rows) { if (string.Compare((string)row["PropertyName"], DescriptionPropertyName, true) == 0) { sNewDimensionDescription = (string)row["PropertyValue"]; } } foreach (DataRow row in dsTableProperties.Tables[0].Rows) { foreach (string sProp in OtherPropertyNamesToInclude) { if (string.Compare((string)row["PropertyName"], sProp, true) == 0 && !string.IsNullOrEmpty((string)row["PropertyValue"])) { if (sNewDimensionDescription.Length > 0) { sNewDimensionDescription += "\r\n"; } sNewDimensionDescription += (string)row["PropertyName"] + ": " + (string)row["PropertyValue"]; } } } if (!string.IsNullOrEmpty(sNewDimensionDescription)) { d.Description = sNewDimensionDescription; iUpdatedDescriptions++; } } foreach (DimensionAttribute a in d.Attributes) { ColumnBinding col = null; #if DENALI || SQL2014 if (a.Type == AttributeType.RowNumber) { continue; } #endif if (a.NameColumn != null) { if (!(a.NameColumn.Source is ColumnBinding)) { continue; } col = GetColumnBindingForDataItem(a.NameColumn); } else if (a.KeyColumns.Count == 1) { if (!(a.KeyColumns[0].Source is ColumnBinding)) { continue; } col = GetColumnBindingForDataItem(a.KeyColumns[0]); } else { continue; //skip this attribute since we don't know which column to use } DataTable oDsvTable = d.DataSourceView.Schema.Tables[col.TableID]; if ((string.IsNullOrEmpty(a.Description) || OverwriteExistingDescriptions) && (!oDsvTable.ExtendedProperties.ContainsKey("QueryDefinition") || bIsTabular) && oDsvTable.ExtendedProperties.ContainsKey("DbTableName") && oDsvTable.ExtendedProperties.ContainsKey("DbSchemaName")) { sql = "SELECT PropertyName = p.name" + "\r\n" + ",PropertyValue = CAST(p.value AS sql_variant)" + "\r\n" + "FROM sys.all_objects AS tbl" + "\r\n" + "INNER JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id" + "\r\n" + "INNER JOIN sys.all_columns AS clmns ON clmns.object_id=tbl.object_id" + "\r\n" + "INNER JOIN sys.extended_properties AS p ON p.major_id=clmns.object_id AND p.minor_id=clmns.column_id AND p.class=1" + "\r\n" + "where sch.name = '" + oDsvTable.ExtendedProperties["DbSchemaName"].ToString().Replace("'", "''") + "'\r\n" + "and tbl.name = '" + oDsvTable.ExtendedProperties["DbTableName"].ToString().Replace("'", "''") + "'\r\n" + "and clmns.name = '" + oDsvTable.Columns[col.ColumnID].ColumnName.Replace("'", "''") + "'\r\n" + "order by p.name"; string sNewDescription = ""; DataSet dsProperties = new DataSet(); openedDataSourceConnection.Fill(dsProperties, sql); foreach (DataRow row in dsProperties.Tables[0].Rows) { if (string.Compare((string)row["PropertyName"], DescriptionPropertyName, true) == 0) { sNewDescription = (string)row["PropertyValue"]; } } foreach (DataRow row in dsProperties.Tables[0].Rows) { foreach (string sProp in OtherPropertyNamesToInclude) { if (string.Compare((string)row["PropertyName"], sProp, true) == 0 && !string.IsNullOrEmpty((string)row["PropertyValue"])) { if (sNewDescription.Length > 0) { sNewDescription += "\r\n"; } sNewDescription += (string)row["PropertyName"] + ": " + (string)row["PropertyValue"]; } } } if (!string.IsNullOrEmpty(sNewDescription)) { a.Description = sNewDescription; iUpdatedDescriptions++; } } } if (d.Site != null) //if not Tabular { //mark dimension as dirty IComponentChangeService changesvc = (IComponentChangeService)d.Site.GetService(typeof(IComponentChangeService)); changesvc.OnComponentChanging(d, null); changesvc.OnComponentChanged(d, null, null, null); } } } finally { try { cartridge = null; openedDataSourceConnection.Close(); } catch { } } return(iUpdatedDescriptions); }
internal static int SyncDescriptions(Dimension d, bool bPromptForProperties, IServiceProvider provider, bool bIsTabular) { int iUpdatedDescriptions = 0; DataSource dataSource = d.DataSource; ColumnBinding colDimensionKey = null; #if DENALI || SQL2014 if (d.KeyAttribute.KeyColumns[0].Source is RowNumberBinding) { foreach (DimensionAttribute a in d.Attributes) { if (a.KeyColumns != null && a.KeyColumns.Count > 0 && a.KeyColumns[0].Source is ColumnBinding) { colDimensionKey = GetColumnBindingForDataItem(a.KeyColumns[0]); break; } } if (colDimensionKey == null) { throw new Exception("Couldn't find an attribute with a ColumnBinding, so couldn't find DSV table."); } } else { colDimensionKey = GetColumnBindingForDataItem(d.KeyAttribute.KeyColumns[0]); } #else colDimensionKey = GetColumnBindingForDataItem(d.KeyAttribute.KeyColumns[0]); #endif DataTable oDimensionKeyTable = d.DataSourceView.Schema.Tables[colDimensionKey.TableID]; //if this is a Tabular model, the Dimension.DataSource may point at the default data source for the data source view if (oDimensionKeyTable.ExtendedProperties.ContainsKey("DataSourceID")) { dataSource = d.Parent.DataSources[oDimensionKeyTable.ExtendedProperties["DataSourceID"].ToString()]; } IServiceProvider settingService = dataSource.Site; if (settingService == null) { settingService = provider; } Microsoft.DataWarehouse.Design.DataSourceConnection openedDataSourceConnection = Microsoft.DataWarehouse.DataWarehouseUtilities.GetOpenedDataSourceConnection((object)null, dataSource.ID, dataSource.Name, dataSource.ManagedProvider, dataSource.ConnectionString, settingService, false); try { if (d.MiningModelID != null) return iUpdatedDescriptions; try { if (openedDataSourceConnection != null) { openedDataSourceConnection.QueryTimeOut = (int)dataSource.Timeout.TotalSeconds; } } catch { } if (openedDataSourceConnection == null) { throw new Exception("Unable to connect to data source [" + d.DataSource.Name + "]."); } else { sq = openedDataSourceConnection.Cartridge.IdentStartQuote; fq = openedDataSourceConnection.Cartridge.IdentEndQuote; DBServerName = openedDataSourceConnection.DBServerName; cartridge = openedDataSourceConnection.Cartridge; if (DBServerName != "Microsoft SQL Server") { MessageBox.Show("Data source [" + d.DataSource.Name + "] connects to " + DBServerName + " which may not be supported."); } String sql = "select distinct Name from sys.extended_properties order by Name"; if (bPromptForProperties) { DataSet dsExtendedProperties = new DataSet(); openedDataSourceConnection.Fill(dsExtendedProperties, sql); BIDSHelper.SSAS.SyncDescriptionsForm form = new BIDSHelper.SSAS.SyncDescriptionsForm(); form.cmbDescriptionProperty.DataSource = dsExtendedProperties.Tables[0]; form.cmbDescriptionProperty.DisplayMember = "Name"; form.cmbDescriptionProperty.ValueMember = "Name"; foreach (DataRow row in dsExtendedProperties.Tables[0].Rows) { form.listOtherProperties.Items.Add(row["Name"].ToString()); } DialogResult result = form.ShowDialog(); if (result != DialogResult.OK) return iUpdatedDescriptions; DescriptionPropertyName = form.cmbDescriptionProperty.GetItemText(form.cmbDescriptionProperty.SelectedItem); List<string> listOtherProperties = new List<string>(); for (int i = 0; i < form.listOtherProperties.CheckedItems.Count; i++) { listOtherProperties.Add(form.listOtherProperties.GetItemText(form.listOtherProperties.CheckedItems[i])); } OtherPropertyNamesToInclude = listOtherProperties.ToArray(); OverwriteExistingDescriptions = form.chkOverwriteExistingDescriptions.Checked; } if ((string.IsNullOrEmpty(d.Description) || OverwriteExistingDescriptions) && (!oDimensionKeyTable.ExtendedProperties.ContainsKey("QueryDefinition") || bIsTabular) //Tabular always has a QueryDefinition, even when it's just a table binding && oDimensionKeyTable.ExtendedProperties.ContainsKey("DbTableName") && oDimensionKeyTable.ExtendedProperties.ContainsKey("DbSchemaName")) { sql = "SELECT PropertyName = p.name" + "\r\n" + ",PropertyValue = CAST(p.value AS sql_variant)" + "\r\n" + "FROM sys.all_objects AS tbl" + "\r\n" + "INNER JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id" + "\r\n" + "INNER JOIN sys.extended_properties AS p ON p.major_id=tbl.object_id AND p.minor_id=0 AND p.class=1" + "\r\n" + "where sch.name = '" + oDimensionKeyTable.ExtendedProperties["DbSchemaName"].ToString().Replace("'", "''") + "'\r\n" + "and tbl.name = '" + oDimensionKeyTable.ExtendedProperties["DbTableName"].ToString().Replace("'", "''") + "'\r\n" + "order by p.name"; string sNewDimensionDescription = ""; DataSet dsTableProperties = new DataSet(); openedDataSourceConnection.Fill(dsTableProperties, sql); foreach (DataRow row in dsTableProperties.Tables[0].Rows) { if (string.Compare((string)row["PropertyName"], DescriptionPropertyName, true) == 0) { sNewDimensionDescription = (string)row["PropertyValue"]; } } foreach (DataRow row in dsTableProperties.Tables[0].Rows) { foreach (string sProp in OtherPropertyNamesToInclude) { if (string.Compare((string)row["PropertyName"], sProp, true) == 0 && !string.IsNullOrEmpty((string)row["PropertyValue"])) { if (sNewDimensionDescription.Length > 0) sNewDimensionDescription += "\r\n"; sNewDimensionDescription += (string)row["PropertyName"] + ": " + (string)row["PropertyValue"]; } } } if (!string.IsNullOrEmpty(sNewDimensionDescription)) { d.Description = sNewDimensionDescription; iUpdatedDescriptions++; } } foreach (DimensionAttribute a in d.Attributes) { ColumnBinding col = null; #if DENALI || SQL2014 if (a.Type == AttributeType.RowNumber) { continue; } #endif if (a.NameColumn != null) { if (!(a.NameColumn.Source is ColumnBinding)) { continue; } col = GetColumnBindingForDataItem(a.NameColumn); } else if (a.KeyColumns.Count == 1) { if (!(a.KeyColumns[0].Source is ColumnBinding)) { continue; } col = GetColumnBindingForDataItem(a.KeyColumns[0]); } else { continue; //skip this attribute since we don't know which column to use } DataTable oDsvTable = d.DataSourceView.Schema.Tables[col.TableID]; if ((string.IsNullOrEmpty(a.Description) || OverwriteExistingDescriptions) && (!oDsvTable.ExtendedProperties.ContainsKey("QueryDefinition") || bIsTabular) && oDsvTable.ExtendedProperties.ContainsKey("DbTableName") && oDsvTable.ExtendedProperties.ContainsKey("DbSchemaName")) { sql = "SELECT PropertyName = p.name" + "\r\n" + ",PropertyValue = CAST(p.value AS sql_variant)" + "\r\n" + "FROM sys.all_objects AS tbl" + "\r\n" + "INNER JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id" + "\r\n" + "INNER JOIN sys.all_columns AS clmns ON clmns.object_id=tbl.object_id" + "\r\n" + "INNER JOIN sys.extended_properties AS p ON p.major_id=clmns.object_id AND p.minor_id=clmns.column_id AND p.class=1" + "\r\n" + "where sch.name = '" + oDsvTable.ExtendedProperties["DbSchemaName"].ToString().Replace("'", "''") + "'\r\n" + "and tbl.name = '" + oDsvTable.ExtendedProperties["DbTableName"].ToString().Replace("'", "''") + "'\r\n" + "and clmns.name = '" + oDsvTable.Columns[col.ColumnID].ColumnName.Replace("'", "''") + "'\r\n" + "order by p.name"; string sNewDescription = ""; DataSet dsProperties = new DataSet(); openedDataSourceConnection.Fill(dsProperties, sql); foreach (DataRow row in dsProperties.Tables[0].Rows) { if (string.Compare((string)row["PropertyName"], DescriptionPropertyName, true) == 0) { sNewDescription = (string)row["PropertyValue"]; } } foreach (DataRow row in dsProperties.Tables[0].Rows) { foreach (string sProp in OtherPropertyNamesToInclude) { if (string.Compare((string)row["PropertyName"], sProp, true) == 0 && !string.IsNullOrEmpty((string)row["PropertyValue"])) { if (sNewDescription.Length > 0) sNewDescription += "\r\n"; sNewDescription += (string)row["PropertyName"] + ": " + (string)row["PropertyValue"]; } } } if (!string.IsNullOrEmpty(sNewDescription)) { a.Description = sNewDescription; iUpdatedDescriptions++; } } } if (d.Site != null) //if not Tabular { //mark dimension as dirty IComponentChangeService changesvc = (IComponentChangeService)d.Site.GetService(typeof(IComponentChangeService)); changesvc.OnComponentChanging(d, null); changesvc.OnComponentChanged(d, null, null, null); } } } finally { try { cartridge = null; openedDataSourceConnection.Close(); } catch { } } return iUpdatedDescriptions; }