Пример #1
0
    def FromReference(cls, referencing_element, referenced_element):
        """Creates a relationship from two elements"""

        if getattr(referencing_element, "backref_is_one_to_one", False):
            relationship_type = cls.RelationshipType.OneToOne
        elif (referencing_element.TypeInfo.Arity.IsOneOrMore
              or referencing_element.TypeInfo.Arity.IsZeroOrMore):
            relationship_type = cls.RelationshipType.ManyToMany
        else:
            relationship_type = cls.RelationshipType.OneToMany

        if getattr(referencing_element, "backref", False):
            backref_name = getattr(
                referencing_element,
                "backref_name",
                referencing_element.Parent.Object.SingularSnakeName
                if relationship_type == cls.RelationshipType.OneToOne else
                referencing_element.Parent.Object.PluralSnakeName,
            )

            is_backref_optional = True
            is_backref_mutable = referencing_element.mutable and relationship_type == cls.RelationshipType.ManyToMany
        else:
            backref_name = None
            is_backref_optional = None
            is_backref_mutable = None

        return cls(
            referencing_element,
            relationship_type,
            referencing_element.Parent.Object,
            referenced_element.Object,
            is_optional=referencing_element.TypeInfo.Arity.Min == 0,
            is_mutable=getattr(referencing_element, "mutable", False),
            is_parent_child=False,
            reference_name=StringHelpers.ToSnakeCase(
                referencing_element.Name if relationship_type ==
                cls.RelationshipType.ManyToMany else referencing_element.Name),
            backref_name=backref_name,
            is_backref_optional=is_backref_optional,
            is_backref_mutable=is_backref_mutable,
        )
Пример #2
0
    def PreprocessContext(cls, context):

        # Augment all elements with their corresponding relational elements
        all_objects = []

        all_elements = pickle.loads(context["pickled_elements"])

        # Pass 1: Create objects
        for element in _EnumCompoundElements(all_elements):
            element.Object = Object.FromElement(element)
            all_objects.append(element.Object)

        # Pass 2: Relationships
        for element in _EnumCompoundElements(all_elements):
            if element.Parent:
                element.Object.Add(Relationship.FromChild(element))

            for child in cls._EnumerateChildren(
                    element,
                    include_definitions=False,
            ):
                if isinstance(child, FundamentalElement):
                    element.Object.Add(
                        Fundamental(
                            child,
                            StringHelpers.ToSnakeCase(child.Name),
                            child.TypeInfo,
                            is_identity=False,
                            is_mutable=child.mutable,
                            is_index=child.index,
                            is_unique=child.unique,
                        ), )

                elif isinstance(child, CompoundElement):
                    # Nothing to do here, as the child will create a reference to
                    # this element when created.
                    pass

                elif isinstance(child, ExtensionElement):
                    content = child.PositionalArguments[0]

                    if content.startswith('"'): content = content[1:]
                    if content.endswith('"'): content = content[:-1]

                    element.Object.Add(content)

                elif isinstance(child, ReferenceElement):
                    resolved_element = child.Resolve()

                    if isinstance(resolved_element, FundamentalElement):
                        element.Object.Add(
                            Fundamental(
                                child,
                                StringHelpers.ToSnakeCase(child.Name),
                                child.TypeInfo,
                                is_identity=False,
                                is_mutable=getattr(child, "mutable", False),
                                is_index=getattr(child, "index", False),
                                is_unique=getattr(child, "unique", False),
                            ), )
                    elif isinstance(resolved_element, CompoundElement):
                        element.Object.Add(
                            Relationship.FromReference(child,
                                                       resolved_element))
                    else:
                        assert False, resolved_element

                elif isinstance(child, ListElement):
                    assert isinstance(child.Reference,
                                      CompoundElement), child.Reference
                    element.Object.Add(
                        Relationship.FromReference(child, child.Reference))

                else:
                    assert False, child

        cls.AllElements = all_elements
        cls.AllObjects = all_objects

        return context
Пример #3
0
    def __init__(
        self,
        identity,
        unique_name,
        name,
        element_or_source_line_column_tuple,
    ):
        super(Object, self).__init__(element_or_source_line_column_tuple)

        self.Identity = identity

        self.SingularName = name
        self.PluralName = inflect.plural(name)
        self.UniqueName = unique_name

        self.SingularPascalName = StringHelpers.ToPascalCase(self.SingularName)
        self.PluralPascalName = StringHelpers.ToPascalCase(self.PluralName)
        self.UniquePascalName = StringHelpers.ToPascalCase(self.UniqueName)

        self.SingularSnakeName = StringHelpers.ToSnakeCase(self.SingularName)
        self.PluralSnakeName = StringHelpers.ToSnakeCase(self.PluralName)
        self.UniqueSnakeName = StringHelpers.ToSnakeCase(self.UniqueName)

        self.constraints = []
        self.children = []

        # Add the identity info
        self.Add(
            Fundamental(
                (_script_name, inspect.currentframe().f_lineno, 0),
                "id",
                IntTypeInfo(min=0) if self.Identity
                == self.IdentityType.Integer else GuidTypeInfo(),
                is_identity=True,
                is_mutable=False,
                is_index=True,
                is_unique=True,
            ), )

        if self.Identity in [
                self.IdentityType.Simple, self.IdentityType.Deletable
        ]:
            self.Add(
                Fundamental(
                    (_script_name, inspect.currentframe().f_lineno, 0),
                    "created",
                    DateTimeTypeInfo(),
                    is_identity=True,
                    is_mutable=False,
                    is_index=False,
                    is_unique=False,
                ), )

        if self.Identity == self.IdentityType.Deletable:
            for name in ["deleted", "restored"]:
                self.Add(
                    Fundamental(
                        (_script_name, inspect.currentframe().f_lineno, 0),
                        name,
                        DateTimeTypeInfo(arity="?", ),
                        is_identity=True,
                        is_mutable=False,
                        is_index=False,
                        is_unique=False,
                    ), )