Example #1
0
        public AdomdClient.AdomdConnection GetAnalyzeConnection()
        {
#if ASQASSAS11 || ASQASSAS12
            if (_currentConnectionID != null)
            {
                throw new ApplicationException("Connection already created");
            }

            var connection = new AdomdClient.AdomdConnection(ConnectionString);
            {
                connection.Open();
                connection.Disposed += (s, e) => _currentConnectionID = null;

                var connectionId = AdomdClientHelper.ExecuteScalar(ConnectionString, CommandDiscoverSessionConnectionId.FormatWith(connection.SessionID));
                if (connectionId == null)
                {
                    throw new ApplicationException("session_connection_id is null");
                }

                _currentConnectionID = Convert.ToString(connectionId);
            }
#else
            var connection = new AdomdClient.AdomdConnection(ConnectionString);
            {
                connection.Open();
            }
#endif
            return(connection);
        }
        public static void ValidateStatement(this ProcedureContext procedureContext)
        {
            #region Argument exceptions

            if (procedureContext == null)
            {
                throw new ArgumentNullException("procedureContext");
            }

            #endregion

            if (procedureContext.IsCancellationRequested)
            {
                return;
            }

            EventsNotifier.Instance.Notify(ProcedureEvents.ProcedureAnalyzeStepValidateStatement);
            AdomdClientHelper.ExecutePrepare(procedureContext.ConnectionString, procedureContext.Statement);
        }
        /// <summary>
        /// Force reload MDX script code in the cube executing a query that return an empty cellset.
        /// </summary>
        public static void LoadCubeScript(this ProcedureContext procedureContext)
        {
            #region Argument exceptions

            if (procedureContext == null)
            {
                throw new ArgumentNullException("procedureContext");
            }

            #endregion

            if (procedureContext.IsCancellationRequested)
            {
                return;
            }

            if (procedureContext.ClearCacheMode != ClearCacheMode.Nothing)
            {
                EventsNotifier.Instance.Notify(ProcedureEvents.ProcedureAnalyzeStepLoadCubeScript);
                AdomdClientHelper.ExecuteNonQuery(procedureContext.ConnectionString, "SELECT {{}} ON 0 FROM [{0}]".FormatWith(procedureContext.CubeName));
            }
        }
        public static string RetrieveCubeMetadata(ProcedureContext procedureContext)
        {
            #region Argument exception

            if (procedureContext == null)
            {
                throw new ArgumentNullException("procedureContext");
            }

            #endregion

            var path     = default(string);
            var document = new XDocument(new XDeclaration("1.0", "utf-8", standalone: null), new XElement("CubeMetadata"));

            using (var server = new Server())
            {
                server.Connect(procedureContext.ConnectionString);

                #region Find database/cube/dimension

                var database = server.Databases.FindByName(procedureContext.DatabaseName);
                if (database == null)
                {
                    throw new ApplicationException("Database not found [{0}]".FormatWith(procedureContext.DatabaseName));
                }

                var cube = database.Cubes.FindByName(procedureContext.CubeName);
                if (cube == null)
                {
                    throw new ApplicationException("Cube not found [{0}]".FormatWith(procedureContext.CubeName));
                }

                var dimension = cube.Dimensions.Cast <CubeDimension>().FirstOrDefault((d) => d.Visible);
                if (dimension == null)
                {
                    throw new ApplicationException("CubeDimension for metadata not found");
                }

                #endregion

                path = "[{0}]".FormatWith(dimension.Name);

                var attribute = dimension.Attributes.Cast <CubeAttribute>().FirstOrDefault((a) => a.AttributeHierarchyVisible && a.AttributeHierarchyEnabled);
                if (attribute != null)
                {
                    path += ".[{0}]".FormatWith(attribute.Attribute.Name);
                }
                else
                {
                    var hierarchy = dimension.Hierarchies.Cast <CubeHierarchy>().FirstOrDefault((h) => h.Visible && h.Enabled);
                    if (hierarchy == null)
                    {
                        throw new ApplicationException("CubeAttribute/CubeHierarchy for metadata not found");
                    }

                    path += ".[{0}]".FormatWith(hierarchy.Hierarchy.Name);
                }

                var mdxScript = cube.MdxScripts.Cast <MdxScript>().First();

                document.Root.Add(new XElement("Cube",
                                               new XAttribute("ID", cube.ID ?? string.Empty),
                                               new XAttribute("Name", cube.Name ?? string.Empty),
                                               new XAttribute("LastSchemaUpdate", cube.LastSchemaUpdate.ToUniversalTime().ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture)),
                                               new XAttribute("EstimatedRows", cube.EstimatedRows),
                                               new XAttribute("StorageMode", cube.StorageMode),
                                               new XAttribute("DefaultMeasure", cube.DefaultMeasure ?? string.Empty),
                                               new XElement("MdxScript",
                                                            new XAttribute("ID", mdxScript.ID ?? string.Empty),
                                                            new XAttribute("Name", mdxScript.Name ?? string.Empty),
                                                            new XAttribute("DefaultScript", mdxScript.DefaultScript),
                                                            new XElement("Commands", mdxScript.Commands.Cast <Command>()
                                                                         .Select((c) => { var e = new XElement("Command"); e.Value = c.Text; return(e); }))),
                                               new XElement("CubeDimensions", cube.Dimensions.Cast <CubeDimension>()
                                                            .Select((d) => new XElement("CubeDimension",
                                                                                        new XAttribute("ID", d.ID ?? string.Empty),
                                                                                        new XAttribute("Name", d.Name ?? string.Empty),
                                                                                        new XAttribute("Visible", d.Visible),
                                                                                        new XAttribute("AttributesCount", d.Attributes.Count),
                                                                                        new XAttribute("HierarchiesCount", d.Hierarchies.Count),
                                                                                        new XAttribute("DimensionID", d.DimensionID ?? string.Empty)))),
                                               new XElement("MeasureGroups", cube.MeasureGroups.Cast <MeasureGroup>()
                                                            .Select((m) => new XElement("MeasureGroup",
                                                                                        new XAttribute("ID", m.ID ?? string.Empty),
                                                                                        new XAttribute("Name", m.Name),
                                                                                        new XAttribute("Type", m.Type),
                                                                                        new XAttribute("StorageMode", m.StorageMode),
                                                                                        new XAttribute("EstimatedRows", m.EstimatedRows),
                                                                                        new XAttribute("EstimatedSize", m.EstimatedSize),
                                                                                        new XAttribute("DataAggregation", m.DataAggregation),
                                                                                        new XAttribute("ProcessingMode", m.ProcessingMode),
                                                                                        new XElement("AggregationDesigns", m.AggregationDesigns.Cast <AggregationDesign>()
                                                                                                     .Select((a) => new XElement("AggregationDesign",
                                                                                                                                 new XAttribute("ID", a.ID ?? string.Empty),
                                                                                                                                 new XAttribute("Name", a.Name),
                                                                                                                                 new XAttribute("EstimatedRows", a.EstimatedRows),
                                                                                                                                 new XAttribute("EstimatedPerformanceGain", a.EstimatedPerformanceGain)))),
                                                                                        new XElement("Partitions", m.Partitions.Cast <Partition>()
                                                                                                     .Select((p) => new XElement("Partition",
                                                                                                                                 new XAttribute("ID", p.ID ?? string.Empty),
                                                                                                                                 new XAttribute("Name", p.Name),
                                                                                                                                 new XAttribute("EstimatedRows", p.EstimatedRows),
                                                                                                                                 new XAttribute("EstimatedSize", p.EstimatedSize),
                                                                                                                                 new XAttribute("ProcessingMode", p.ProcessingMode),
                                                                                                                                 new XAttribute("Slice", p.Slice ?? string.Empty),
                                                                                                                                 new XAttribute("StorageMode", p.StorageMode),
                                                                                                                                 new XAttribute("Type", p.Type),
                                                                                                                                 new XAttribute("AggregationDesignID", p.AggregationDesignID ?? string.Empty)))),
                                                                                        new XElement("Measures", m.Measures.Cast <Measure>()
                                                                                                     .Select((s) => new XElement("Measure",
                                                                                                                                 new XAttribute("ID", s.ID ?? string.Empty),
                                                                                                                                 new XAttribute("Name", s.Name),
                                                                                                                                 new XAttribute("Visible", s.Visible),
                                                                                                                                 new XAttribute("DataType", s.DataType),
                                                                                                                                 new XAttribute("FormatString", s.FormatString ?? string.Empty),
                                                                                                                                 new XAttribute("MeasureExpression", s.MeasureExpression ?? string.Empty),
                                                                                                                                 new XAttribute("SourceNullProcessing", s.Source.NullProcessing),
                                                                                                                                 new XAttribute("SourceNullDataType", s.Source.DataType)))))))));
            }

            using (var table = AdomdClientHelper.ExecuteDataTable(procedureContext.ConnectionString, commandText: MdxMeasuresMetadata.FormatWith(procedureContext.CubeName, path)))
            {
                if (table.Columns.Count != 3)
                {
                    throw new ApplicationException("Invalid columns for MdxMeasuresMetadata");
                }

                table.Columns[0].ColumnName = "Name";
                table.Columns[1].ColumnName = "IDOfCurrentMember";
                table.Columns[2].ColumnName = "ID";

                document.Root.Add(new XElement("MeasuresExtended",
                                               table.AsEnumerable().Skip(1)
                                               .Select((r) => new XElement("Measure", table.Columns.Cast <DataColumn>().Select((c) => new XAttribute(c.ColumnName, r[c])))))
                                  );
            }

            return(document.ToString(SaveOptions.DisableFormatting));
        }