Exemplo n.º 1
0
        private string BuildSqlWithExclusiveLeafClasses(ExtentStatement statement)
        {
            if (statement.IsRoot)
            {
                var leafClasses = this.objectType.Classes.ToArray();
                for (var i = 0; i < leafClasses.Length; i++)
                {
                    var alias     = statement.CreateAlias();
                    var leafClass = leafClasses[i];

                    statement.Append("SELECT " + alias + "." + this.Schema.ObjectId);
                    if (statement.Sorter != null)
                    {
                        statement.Sorter.BuildSelect(statement);
                    }

                    statement.Append(" FROM " + this.Schema.Table(leafClass) + " " + alias);

                    statement.AddJoins(leafClass, alias);
                    statement.AddWhere(leafClass, alias);

                    if (this.filter != null)
                    {
                        this.filter.BuildWhere(statement, alias);
                    }

                    if (i < leafClasses.Length - 1)
                    {
                        statement.Append("\nUNION\n");
                    }
                }
            }
            else
            {
                var inStatement = (ExtentStatementChild)statement;

                if (inStatement.RoleType != null)
                {
                    var useUnion = false;
                    foreach (var leafClass in this.objectType.Classes)
                    {
                        var inRole         = inStatement.RoleType;
                        var inRelationType = inRole.RelationType;

                        if (!((IComposite)inRole.ObjectType).Classes.Contains(leafClass))
                        {
                            continue;
                        }

                        if (useUnion)
                        {
                            statement.Append("\nUNION\n");
                        }
                        else
                        {
                            useUnion = true;
                        }

                        var alias = statement.CreateAlias();

                        if (inRelationType.Multiplicity == Multiplicity.ManyToMany || !inRelationType.ExistExclusiveClasses)
                        {
                            statement.Append("SELECT " + inRole.AssociationType.SingularFullName + "_A." + this.Schema.AssociationId);
                        }
                        else
                        {
                            if (inRole.IsMany)
                            {
                                statement.Append("SELECT " + alias + "." + this.Schema.ObjectId);
                            }
                            else
                            {
                                statement.Append("SELECT " + inRole.AssociationType.SingularFullName + "_A." + this.Schema.Column(inRole.AssociationType));
                            }
                        }

                        statement.Append(" FROM " + this.Schema.Table(leafClass) + " " + alias);

                        statement.AddJoins(leafClass, alias);

                        var wherePresent = statement.AddWhere(leafClass, alias);
                        var filterUsed   = false;
                        if (this.filter != null)
                        {
                            filterUsed = this.filter.BuildWhere(statement, alias);
                        }

                        if (wherePresent || filterUsed)
                        {
                            statement.Append(" AND ");
                        }
                        else
                        {
                            statement.Append(" WHERE ");
                        }

                        if (inRelationType.Multiplicity == Multiplicity.ManyToMany || !inRelationType.ExistExclusiveClasses)
                        {
                            statement.Append(inRole.AssociationType.SingularFullName + "_A." + this.Schema.AssociationId + " IS NOT NULL ");
                        }
                        else
                        {
                            if (inRole.IsMany)
                            {
                                statement.Append(alias + "." + this.Schema.Column(inRole.AssociationType) + " IS NOT NULL ");
                            }
                            else
                            {
                                statement.Append(inRole.AssociationType.SingularFullName + "_A." + this.Schema.Column(inRole.AssociationType) + " IS NOT NULL ");
                            }
                        }
                    }
                }
                else
                {
                    var leafClasses = this.objectType.Classes.ToArray();
                    for (var i = 0; i < leafClasses.Length; i++)
                    {
                        var alias     = statement.CreateAlias();
                        var leafClass = leafClasses[i];

                        if (statement.IsRoot)
                        {
                            statement.Append("SELECT " + alias + "." + this.Schema.ObjectId);
                            if (statement.Sorter != null)
                            {
                                statement.Sorter.BuildSelect(statement);
                            }
                        }
                        else
                        {
                            statement.Append("SELECT " + alias + "." + this.Schema.ObjectId);
                        }

                        statement.Append(" FROM " + this.Schema.Table(leafClass) + " " + alias);

                        statement.AddJoins(leafClass, alias);
                        statement.AddWhere(leafClass, alias);

                        if (this.filter != null)
                        {
                            this.filter.BuildWhere(statement, alias);
                        }

                        if (i < leafClasses.Length - 1)
                        {
                            statement.Append("\nUNION\n");
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        private string BuildSqlWithExclusiveLeafClass(ExtentStatement statement)
        {
            var alias = statement.CreateAlias();
            var exclusiveLeafClass = this.objectType.ExclusiveClass;

            if (statement.IsRoot)
            {
                statement.Append("SELECT DISTINCT " + alias + "." + this.Schema.ObjectId);
                if (statement.Sorter != null)
                {
                    statement.Sorter.BuildSelect(statement, alias);
                }

                statement.Append(" FROM " + this.Schema.Table(exclusiveLeafClass) + " " + alias);
                statement.AddJoins(exclusiveLeafClass, alias);
                statement.AddWhere(exclusiveLeafClass, alias);
                if (this.filter != null)
                {
                    this.filter.BuildWhere(statement, alias);
                }
            }
            else
            {
                // ContainedIn
                var inStatement = (ExtentStatementChild)statement;

                if (inStatement.RoleType != null)
                {
                    var inRole         = inStatement.RoleType;
                    var inRelationType = inRole.RelationType;
                    if (inRelationType.Multiplicity == Multiplicity.ManyToMany || !inRelationType.ExistExclusiveClasses)
                    {
                        statement.Append("SELECT " + inRole.AssociationType.SingularFullName + "_A." + this.Schema.AssociationId);
                    }
                    else
                    {
                        if (inRole.IsMany)
                        {
                            statement.Append("SELECT " + alias + "." + this.Schema.ObjectId);
                        }
                        else
                        {
                            statement.Append("SELECT " + inRole.AssociationType.SingularFullName + "_A." + this.Schema.Column(inRole.AssociationType));
                        }
                    }

                    statement.Append(" FROM " + this.Schema.Table(exclusiveLeafClass) + " " + alias);
                    statement.AddJoins(exclusiveLeafClass, alias);

                    var wherePresent = statement.AddWhere(exclusiveLeafClass, alias);
                    var filterUsed   = false;
                    if (this.filter != null)
                    {
                        filterUsed = this.filter.BuildWhere(statement, alias);
                    }

                    if (wherePresent || filterUsed)
                    {
                        statement.Append(" AND ");
                    }
                    else
                    {
                        statement.Append(" WHERE ");
                    }

                    if (inRelationType.Multiplicity == Multiplicity.ManyToMany || !inRelationType.ExistExclusiveClasses)
                    {
                        statement.Append(inRole.AssociationType.SingularFullName + "_A." + this.Schema.AssociationId + " IS NOT NULL ");
                    }
                    else
                    {
                        if (inRole.IsMany)
                        {
                            statement.Append(alias + "." + this.Schema.Column(inRole.AssociationType) + " IS NOT NULL ");
                        }
                        else
                        {
                            statement.Append(inRole.AssociationType.SingularFullName + "_A." +
                                             this.Schema.Column(inRole.AssociationType) + " IS NOT NULL ");
                        }
                    }
                }
                else
                {
                    if (statement.IsRoot)
                    {
                        statement.Append("SELECT " + alias + "." + this.Schema.ObjectId);
                        if (statement.Sorter != null)
                        {
                            statement.Sorter.BuildSelect(statement);
                        }
                    }
                    else
                    {
                        statement.Append("SELECT " + alias + "." + this.Schema.ObjectId);
                    }

                    statement.Append(" FROM " + this.Schema.Table(exclusiveLeafClass) + " " + alias);

                    statement.AddJoins(exclusiveLeafClass, alias);
                    statement.AddWhere(exclusiveLeafClass, alias);

                    if (this.filter != null)
                    {
                        this.filter.BuildWhere(statement, alias);
                    }
                }
            }

            return(alias);
        }