Exemple #1
0
        protected string GenerateSubquery(PersistentClass model, IMapping mapping)
        {
            Dialect.Dialect dialect  = Factory.Dialect;
            Settings        settings = Factory.Settings;

            if (!model.HasSubclasses)
            {
                return(model.Table.GetQualifiedName(dialect, settings.DefaultCatalogName, settings.DefaultSchemaName));
            }

            HashedSet <Column> columns = new HashedSet <Column>();

            foreach (Table table in model.SubclassTableClosureIterator)
            {
                if (!table.IsAbstractUnionTable)
                {
                    foreach (Column column in table.ColumnIterator)
                    {
                        columns.Add(column);
                    }
                }
            }

            StringBuilder buf = new StringBuilder().Append("( ");
            IEnumerable <PersistentClass> siter =
                new JoinedEnumerable <PersistentClass>(new SingletonEnumerable <PersistentClass>(model),
                                                       new SafetyEnumerable <PersistentClass>(model.SubclassIterator));

            foreach (PersistentClass clazz in siter)
            {
                Table table = clazz.Table;
                if (!table.IsAbstractUnionTable)
                {
                    buf.Append("select ");
                    foreach (Column col in columns)
                    {
                        if (!table.ContainsColumn(col))
                        {
                            SqlType sqlType = col.GetSqlTypeCode(mapping);
                            buf.Append(dialect.GetSelectClauseNullString(sqlType)).Append(" as ");
                        }
                        buf.Append(col.Name);
                        buf.Append(StringHelper.CommaSpace);
                    }
                    buf.Append(clazz.SubclassId).Append(" as clazz_");
                    buf.Append(" from ").Append(table.GetQualifiedName(dialect, settings.DefaultCatalogName, settings.DefaultSchemaName));
                    buf.Append(" union ");
                    if (dialect.SupportsUnionAll)
                    {
                        buf.Append("all ");
                    }
                }
            }

            if (buf.Length > 2)
            {
                //chop the last union (all)
                buf.Length -= (dialect.SupportsUnionAll ? 11 : 7);                 //" union " : "all "
            }

            return(buf.Append(" )").ToString());
        }