示例#1
0
        /// <summary>
        /// Returns a <see cref="DataSet"/> built based on the specified <see cref="DataSetDescriptor"/>
        /// </summary>
        /// <param name="dataSetDescriptor">The <see cref="DataSetDescriptor"/> to use</param>
        /// <param name="databaseBridge">The <see cref="IDatabaseBridge"/> to use</param>
        /// <returns>A <see cref="DataSet"/> built based on the specified <see cref="DataSetDescriptor"/></returns>
        public static DataSet BuilDataSet(DataSetDescriptor dataSetDescriptor, IDatabaseBridge databaseBridge)
        {
            if (dataSetDescriptor == null)
            {
                throw new ArgumentNullException(nameof(dataSetDescriptor));
            }
            if (databaseBridge == null)
            {
                throw new ArgumentNullException(nameof(databaseBridge));
            }

            return(new DataSetBuilder(dataSetDescriptor, databaseBridge).Build());
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataSetBuilder"/> class
        /// </summary>
        /// <param name="dataSetDescriptor">The data set descriptor</param>
        /// <param name="databaseBridge">The database bridge</param>
        private DataSetBuilder(DataSetDescriptor dataSetDescriptor, IDatabaseBridge databaseBridge)
        {
            if (dataSetDescriptor == null)
            {
                throw new ArgumentNullException(nameof(dataSetDescriptor));
            }
            if (databaseBridge == null)
            {
                throw new ArgumentNullException(nameof(databaseBridge));
            }

            _dataSetDescriptor = dataSetDescriptor;
            _databaseBridge    = databaseBridge;
        }
        /// <summary>
        /// Returns a new <see cref="TOLAPCube"/> built based on the specified parameters
        /// </summary>
        /// <param name="config">The cube constructor config document</param>
        /// <param name="dataSetDescriptor">The data set descriptor</param>
        /// <param name="dataSet">The data set</param>
        /// <returns>A new <see cref="TOLAPCube"/> built based on the specified parameters</returns>
        public static TOLAPCube BuildCube(XElement config, DataSetDescriptor dataSetDescriptor, DataSet dataSet)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (dataSetDescriptor == null)
            {
                throw new ArgumentNullException(nameof(dataSetDescriptor));
            }
            if (dataSet == null)
            {
                throw new ArgumentNullException(nameof(dataSet));
            }

            return(new OlapCubeBuilder(config, dataSetDescriptor, dataSet).Build());
        }
        /// <summary>
        /// Returns a <see cref="DataSetDescriptor"/> read from the specified XML document
        /// </summary>
        /// <param name="doc">The XML document to read a <see cref="DataSetDescriptor"/> from</param>
        /// <returns>A <see cref="DataSetDescriptor"/> read from the specified XML document</returns>
        private static DataSetDescriptor CreateDataSetDescriptor(XElement doc)
        {
            var connectionString = (string)doc.Attribute("connectionString");

            if (connectionString == null)
            {
                throw new InvalidOperationException("Attribute 'connectionString' is missing in element 'Cube'");
            }

            var userName = (string)doc.Attribute("userName");

            if (userName == null)
            {
                throw new InvalidOperationException("Attribute 'userName' is missing in element 'Cube'");
            }

            var dataSetDesc = new DataSetDescriptor(connectionString, userName);

            var ns = doc.Name.Namespace;

            var dataSetTag = doc.Element(ns + "DataSet");

            if (dataSetTag == null)
            {
                throw new InvalidOperationException("Element 'DataSet' is missing in element 'Cube'");
            }

            foreach (var mj in dataSetTag.Elements(ns + "MultiJoin"))
            {
                var multiJoin = ReadMultiJoinDescriptor(ns, mj);

                dataSetDesc.AddTable(multiJoin);
            }

            foreach (var t in dataSetTag.Elements(ns + "Table"))
            {
                var table = ReadTableDescriptor(ns, t);

                dataSetDesc.AddTable(table);
            }

            ValidateReferences(dataSetDesc);

            return(dataSetDesc);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="OlapCubeBuilder"/> class
        /// </summary>
        /// <param name="config">The cube constructor config document</param>
        /// <param name="dataSetDescriptor">The data set descriptor</param>
        /// <param name="dataSet">The data set</param>
        private OlapCubeBuilder(XElement config, DataSetDescriptor dataSetDescriptor, DataSet dataSet)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (dataSetDescriptor == null)
            {
                throw new ArgumentNullException(nameof(dataSetDescriptor));
            }
            if (dataSet == null)
            {
                throw new ArgumentNullException(nameof(dataSet));
            }

            _config            = config;
            _dataSetDescriptor = dataSetDescriptor;
            _dataSet           = dataSet;
        }
        /// <summary>
        /// Validates the 'references' attributes of the specified <see cref="DataSetDescriptor"/>
        /// </summary>
        /// <param name="dataSetDescriptor">The <see cref="DataSetDescriptor"/> to validate</param>
        private static void ValidateReferences(DataSetDescriptor dataSetDescriptor)
        {
            foreach (var kvp in dataSetDescriptor.Tables)
            {
                var multijoin = kvp.Value as MultiJoinDescriptor;
                if (multijoin != null)
                {
                    foreach (var td in multijoin.Tables)
                    {
                        foreach (var fd in td.GetForeignKeys())
                        {
                            if (!dataSetDescriptor.Tables.ContainsKey(fd.References))
                            {
                                throw new InvalidOperationException(
                                          $"Field '{fd}' of table '{td}' in multijoin '{multijoin.Name}' references table '{fd.References}' which does not exist");
                            }

                            if (dataSetDescriptor.Tables[fd.References].GetPrimaryKey() == null)
                            {
                                throw new InvalidOperationException(
                                          $"Field '{fd}' of table '{td}' in multijoin '{multijoin.Name}' references table '{fd.References}' which does not have the PK");
                            }
                        }
                    }

                    //var mainTable = multijoin
                    //    .Tables
                    //    .SingleOrDefault(
                    //        td => td.Name.Equals(multijoin.PrimaryKey, StringComparison.OrdinalIgnoreCase));
                    //if (mainTable == null)
                    //    throw new InvalidOperationException(
                    //        $"Table '{multijoin.PrimaryKey}' does not exist in multijoin '{multijoin.Name}' to use its PK");

                    //if (mainTable.GetPrimaryKey() == null)
                    //    throw new InvalidOperationException(
                    //        $"Table '{mainTable}' does not have the PK to use it in multijoin '{multijoin.Name}'");
                    if (multijoin
                        .Tables
                        .SelectMany(td => td.Fields.Values)
                        .Any(fd => fd.AliasOrName.Equals(multijoin.PrimaryKey, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new InvalidOperationException(
                                  $"PK column name '{multijoin.PrimaryKey}' in multijoin '{multijoin.Name}' causes a name conflict");
                    }
                }
                else
                {
                    foreach (var fd in kvp.Value.GetForeignKeys())
                    {
                        if (!dataSetDescriptor.Tables.ContainsKey(fd.References))
                        {
                            throw new InvalidOperationException(
                                      $"Field '{fd}' of table '{kvp.Value.Name}' references table '{fd.References}' which does not exist");
                        }

                        if (dataSetDescriptor.Tables[fd.References].GetPrimaryKey() == null)
                        {
                            throw new InvalidOperationException(
                                      $"Field '{fd}' of table '{kvp.Value.Name}' references table '{fd.References}' which does not have the PK");
                        }
                    }
                }
            }
        }