예제 #1
0
        private AxTable FindTableInDataSource(AxView view, string dataSourceName)
        {
            foreach (AxQuerySimpleRootDataSource ds in view.ViewMetadata.DataSources)
            {
                if (string.Compare(dataSourceName, ds.Name, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return(this.MetadataProvider.Tables.Read(ds.Table));
                }

                return(FindTableInEmbeddedDataSources(ds.DataSources, dataSourceName));
            }

            return(null);
        }
예제 #2
0
        /// <summary>
        /// Generate the SQL command selecting the given fields from the underlying table.
        /// </summary>
        /// <param name="fields">The list of fields to select</param>
        /// <returns>The string containing the SQL command.</returns>
        private StringBuilder GenerateFromViewFieldList(IEnumerable <ViewsAutomation.IViewBaseField> fields)
        {
            var result = new StringBuilder();

            result.AppendLine(string.Format(CultureInfo.InvariantCulture, "use {0}", BusinessDatabaseName));
            result.AppendLine("go");

            if (!fields.Any())
            {
                return(result);
            }

            ViewsAutomation.IView selectedView1 = fields.FirstOrDefault().View;
            AxView view = this.MetadataProvider.Views.Read(selectedView1.Name);

            // Expand the developer documentation, if any
            if (!string.IsNullOrEmpty(view.DeveloperDocumentation))
            {
                result.Append("-- " + view.Name);
                result.AppendLine(" : " + this.ResolveLabel(view.DeveloperDocumentation));
            }
            else
            {
                result.AppendLine();
            }

            result.AppendLine("select");
            bool first = true;

            foreach (ViewsAutomation.IViewField field in fields.OfType <ViewsAutomation.IViewField>())
            {
                this.AddField(result, view.Name, field.Name, null, ref first);

                // The field name refers to a name on the datasource. Find the datasource
                // and the underlying table.
                AxTable table = this.FindTableInDataSource(view, field.DataSource);
                table = this.SuperTables(table.Name).First();

                if (table != null)
                {
                    AxTableField tableField = table.Fields[field.DataField];
                    if (tableField != null)
                    {
                        var edt = tableField.ExtendedDataType;

                        if (!string.IsNullOrWhiteSpace(edt))
                        {
                            // See if it happens to be an array field. If so, the first index
                            // does not have a suffix ([<n>]), and has already been written.
                            if (this.MetadataProvider.Edts.Exists(edt))
                            {
                                AxEdt typeDefinition = this.metadataProvider.Edts.Read(edt);
                                for (int i = 2; i <= typeDefinition.ArrayElements.Count + 1; i++)
                                {
                                    var fn = field.Name + "[" + i.ToString(CultureInfo.InvariantCulture) + "]";
                                    this.AddField(result, view.Name, fn, null, ref first);
                                }
                            }
                        }
                    }
                }
            }

            // Now deal with computed columns.
            foreach (ViewsAutomation.IViewComputedColumn computedColumn in fields.OfType <ViewsAutomation.IViewComputedColumn>())
            {
                this.AddField(result, view.Name, computedColumn.Name, null, ref first);
            }

            result.AppendLine("from " + SqlNameMangling.GetSqlTableName(view.Name));

            return(result);
        }