Ejemplo n.º 1
0
        public RelationshipGraph(AMO.Database tabularDatabase)
        {
            foreach (TableInfo tableInfo in AMO2Tabular.TablesEnumerateFull(tabularDatabase))
            {
                using (AMO.MeasureGroup currentMeasureGroup = tabularDatabase.Cubes[0].MeasureGroups[tableInfo.DataSourceName])
                {
                    foreach (AMO.MeasureGroupDimension measureGroupDimension in currentMeasureGroup.Dimensions)
                    {
                        if (measureGroupDimension is AMO.ReferenceMeasureGroupDimension)
                        {
                            AMO.ReferenceMeasureGroupDimension referencedTable = measureGroupDimension as AMO.ReferenceMeasureGroupDimension;

                            string foreignTableId     = referencedTable.IntermediateCubeDimensionID;
                            string foreignColumnId    = referencedTable.IntermediateGranularityAttributeID;
                            string primaryKeyTableId  = referencedTable.CubeDimensionID;
                            string primaryKeyColumnId = string.Empty;
                            foreach (AMO.MeasureGroupAttribute attribute in referencedTable.Attributes)
                            {
                                if (attribute.Type == AMO.MeasureGroupAttributeType.Granularity)
                                {
                                    primaryKeyColumnId = attribute.AttributeID;
                                    break;
                                }
                            }

                            FullName         foreignKeyEnd = new FullName(foreignTableId, foreignColumnId);
                            FullName         primaryKeyEnd = new FullName(primaryKeyTableId, primaryKeyColumnId);
                            RelationshipPair relationship  = new RelationshipPair(primaryKeyEnd, foreignKeyEnd);
                            AddForeignKeyUpPair(primaryKeyTableId, relationship);
                            AddPrimaryKeyDownPair(foreignTableId, relationship);
                        }
                    }
                }
            }
        }
        public static void HierarchyAdd(AMO.Database tabularDatabase,
                                        string tableName,
                                        string hierarchyName,
                                        bool updateInstance = true,
                                        params LevelInfo[] levelInfo)
        {
            //  Major steps in adding a Hierarchy to a table in the database
            //
            //  - Validate required input arguments
            //  - Other Initial preparations
            //  - Adding 'Empty' Hierarchy to dimension
            //  - Adding Levels to Hierarchy

            //
            //  Note: There are no validations for duplicated names, invalid names or
            //  similar scenarios. It is expected the server will take care of them and
            //  throw exceptions on any invalid situation.
            //
            //  Note:   In AMO, strings as indexers refer to the ID of the object, not the Name of the object
            //
            //  Note:   Only one DataSourceView is used in Tabular Models
            //          ==> tabularDatabase.DataSourceViews[0] represents the DSV of the model
            //
            //  Note:   Only one Cube is used in Tabular Models
            //          ==> tabularDatabase.Cubes[0] represents the cube in the model
            //
            //  Note:   Microsoft design tools use the following pattern to keep track of the
            //          datasource matching elements:
            //          DataSourceView->TableName <---> Dimension.ID, MeasureGroup.ID
            //          DataSourceView->ColumnName <---> Dimension->ColumnID, MeasureGroup.DegeneratedDimension->CoumnID
            //          So far, this sample follows the same pattern.
            //
            //          WARNING:    Breaking the above pattern when creating your
            //                      own AMO to Tabular functions might lead to
            //                      unpredictable behavior when using Microsoft
            //                      Design tools in your models.


            #region Validate input arguments and other initial preparations
            //  Validate required input arguments
            if (tabularDatabase == null)
            {
                throw new ArgumentNullException(TabularDatabaseStringName);
            }
            if (tableName.IsNullOrEmptyOrWhitespace())
            {
                throw new ArgumentNullException(TableStringName);
            }
            if (hierarchyName.IsNullOrEmptyOrWhitespace())
            {
                throw new ArgumentNullException(HierarchyStringName);
            }
            if (levelInfo == null || levelInfo.Length == 0)
            {
                throw new ArgumentNullException(LevelInfo);
            }
            if (!IsDatabaseCompatibilityLevelCorrect(tabularDatabase))
            {
                throw new InvalidOperationException(Resources.InvalidCompatibilityLevelOperationException);
            }

            //  Other initial preparations
            //  -   Cleaning and preparing name variables
            tableName     = tableName.Trim();
            hierarchyName = hierarchyName.Trim();

            //  -   Obtain table name in DSV
            string datasourceTableName = tabularDatabase.Dimensions.GetByName(tableName).ID;
            #endregion

            //  Add 'empty' hierarchy
            using (AMO.Hierarchy currentHierarchy = tabularDatabase.Dimensions[datasourceTableName].Hierarchies.Add(hierarchyName, hierarchyName))
            {
                currentHierarchy.AllMemberName = string.Format(CultureInfo.InvariantCulture, "(All of {0})", hierarchyName);
            }

            //  Add levels
            AMO2Tabular.HierarchyAlterLevelsAdd(tabularDatabase, tableName, hierarchyName, false, levelInfo);

            //  Update server instance
            if (updateInstance)
            {
                tabularDatabase.Update(AMO.UpdateOptions.ExpandFull, AMO.UpdateMode.UpdateOrCreate);
            }
        }