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 void AddPrimaryKeyDownPair(string tableId, RelationshipPair relationshipPair)
 {
     if (!graph.ContainsKey(tableId))
     {
         AddTableNode(tableId);
     }
     if (!graph[tableId].PrimaryKeyDown.Contains(relationshipPair))
     {
         graph[tableId].PrimaryKeyDown.Add(relationshipPair);
     }
 }
 public void AddForeignKeyUpPair(string tableId, RelationshipPair relationshipPair)
 {
     if (!graph.ContainsKey(tableId))
     {
         AddTableNode(tableId);
     }
     if (!graph[tableId].ForeignKeyUp.Contains(relationshipPair))
     {
         graph[tableId].ForeignKeyUp.Add(relationshipPair);
     }
 }
Esempio n. 4
0
        private static void RelationshipAddReferenceMeasureGroupDimension(AMO.Database tabularDatabase,
                                                                          string currentMeasureGroupId,
                                                                          RelationshipPair relationshipPair,
                                                                          string relationshipId = null)
        {
            string foreignTableId  = relationshipPair.ForeignKeyEnd.TableId;
            string foreignColumnId = relationshipPair.ForeignKeyEnd.ColumnId;
            string pkTableId       = relationshipPair.PrimaryKeyEnd.TableId;
            string pkColumnId      = relationshipPair.PrimaryKeyEnd.ColumnId;

            //  Creating the ReferenceMeasureGroupDimension that defines the 'Activeness' of a relationship
            using (AMO.Cube modelCube = tabularDatabase.Cubes[0])
                using (AMO.MeasureGroup currentMG = modelCube.MeasureGroups[currentMeasureGroupId])
                    using (AMO.ReferenceMeasureGroupDimension newReferenceMGDim = new AMO.ReferenceMeasureGroupDimension())
                    {
                        newReferenceMGDim.CubeDimensionID                    = pkTableId;
                        newReferenceMGDim.IntermediateCubeDimensionID        = foreignTableId;
                        newReferenceMGDim.IntermediateGranularityAttributeID = foreignColumnId;
                        //  Replicating attributes (columns) from dimension
                        foreach (AMO.CubeAttribute PKAttribute in modelCube.Dimensions[pkTableId].Attributes)
                        {
                            using (AMO.MeasureGroupAttribute PKMGAttribute = newReferenceMGDim.Attributes.Add(PKAttribute.AttributeID))
                                using (AMO.DataItem dataItem = new AMO.DataItem(pkTableId, PKAttribute.AttributeID, PKAttribute.Attribute.KeyColumns[0].DataType))
                                    using (AMO.ColumnBinding columnBinding = new AMO.ColumnBinding(pkTableId, PKAttribute.AttributeID))
                                    {
                                        PKMGAttribute.KeyColumns.Add(dataItem);
                                        PKMGAttribute.KeyColumns[0].Source = columnBinding;
                                    }
                        }
                        newReferenceMGDim.Attributes[pkColumnId].Type = AMO.MeasureGroupAttributeType.Granularity;

                        //  If relationship is not null or empty then this is a direct relationship and
                        //  has to have Materialization.Regular
                        if (!relationshipId.IsNullOrEmptyOrWhitespace())
                        {
                            newReferenceMGDim.Materialization = AMO.ReferenceDimensionMaterialization.Regular;
                            newReferenceMGDim.RelationshipID  = relationshipId;
                        }
                        else
                        {
                            newReferenceMGDim.Materialization = AMO.ReferenceDimensionMaterialization.Indirect;
                        }

                        //  Adding the ReferenceMeasureGroupDimension to the measure group
                        currentMG.Dimensions.Add(newReferenceMGDim);
                    }
        }