Example #1
0
def derive_ptr(ptr: s_pointers.Pointer,
               source: s_sources.Source,
               target: Optional[s_types.Type] = None,
               *qualifiers: str,
               derived_name: Optional[sn.SchemaName] = None,
               derived_name_quals: Optional[Sequence[str]] = (),
               derived_name_base: Optional[str] = None,
               preserve_shape: bool = False,
               preserve_path_id: bool = False,
               is_insert: bool = False,
               is_update: bool = False,
               inheritance_merge: bool = True,
               attrs: Optional[Dict[str, Any]] = None,
               ctx: context.ContextLevel) -> s_pointers.Pointer:

    if derived_name is None and ctx.derived_target_module:
        derived_name = derive_view_name(stype=ptr,
                                        derived_name_quals=derived_name_quals,
                                        derived_name_base=derived_name_base,
                                        ctx=ctx)

    if ptr.get_name(ctx.env.schema) == derived_name:
        qualifiers = qualifiers + (ctx.aliases.get('d'), )

    ctx.env.schema, derived = ptr.derive_ref(
        ctx.env.schema,
        source,
        *qualifiers,
        target=target,
        name=derived_name,
        inheritance_merge=inheritance_merge,
        inheritance_refdicts={'pointers'},
        mark_derived=True,
        transient=True,
        preserve_path_id=preserve_path_id,
        attrs=attrs)

    if not ptr.generic(ctx.env.schema):
        if isinstance(derived, s_sources.Source):
            ptr = cast(s_links.Link, ptr)
            scls_pointers = ptr.get_pointers(ctx.env.schema)
            derived_own_pointers = derived.get_pointers(ctx.env.schema)

            for pn, ptr in derived_own_pointers.items(ctx.env.schema):
                # This is a view of a view.  Make sure query-level
                # computable expressions for pointers are carried over.
                src_ptr = scls_pointers.get(ctx.env.schema, pn)
                # mypy somehow loses the type argument in the
                # "pointers" ObjectIndex.
                assert isinstance(src_ptr, s_pointers.Pointer)
                computable_data = ctx.source_map.get(src_ptr)
                if computable_data is not None:
                    ctx.source_map[ptr] = computable_data

    if preserve_shape and ptr in ctx.env.view_shapes:
        ctx.env.view_shapes[derived] = ctx.env.view_shapes[ptr]

    ctx.env.created_schema_objects.add(derived)

    return derived
Example #2
0
def derive_ptr(ptr: s_pointers.Pointer,
               source: s_sources.Source,
               target: typing.Optional[s_types.Type] = None,
               *qualifiers,
               derived_name: typing.Optional[sn.SchemaName] = None,
               derived_name_quals: typing.Optional[typing.Sequence[str]] = (),
               derived_name_base: typing.Optional[str] = None,
               preserve_shape: bool = False,
               preserve_path_id: bool = False,
               is_insert: bool = False,
               is_update: bool = False,
               inheritance_merge: bool = True,
               attrs: typing.Optional[dict] = None,
               ctx: context.ContextLevel) -> s_pointers.Pointer:

    if derived_name is None and ctx.derived_target_module:
        derived_name = derive_view_name(stype=ptr,
                                        derived_name_quals=derived_name_quals,
                                        derived_name_base=derived_name_base,
                                        ctx=ctx)

    if ptr.get_name(ctx.env.schema) == derived_name:
        qualifiers = qualifiers + (ctx.aliases.get('d'), )

    ctx.env.schema, derived = ptr.derive_ref(
        ctx.env.schema,
        source,
        target,
        *qualifiers,
        name=derived_name,
        inheritance_merge=inheritance_merge,
        refdict_whitelist={'pointers'},
        mark_derived=True,
        preserve_path_id=preserve_path_id,
        attrs=attrs)

    if not ptr.generic(ctx.env.schema):
        if isinstance(derived, s_sources.Source):
            ptr = typing.cast(s_links.Link, ptr)
            scls_pointers = ptr.get_pointers(ctx.env.schema)
            derived_own_pointers = derived.get_pointers(ctx.env.schema)

            for pn, ptr in derived_own_pointers.items(ctx.env.schema):
                # This is a view of a view.  Make sure query-level
                # computable expressions for pointers are carried over.
                src_ptr = scls_pointers.get(ctx.env.schema, pn)
                computable_data = ctx.source_map.get(src_ptr)
                if computable_data is not None:
                    ctx.source_map[ptr] = computable_data

    if preserve_shape and ptr in ctx.env.view_shapes:
        ctx.env.view_shapes[derived] = ctx.env.view_shapes[ptr]

    return derived
Example #3
0
    def from_pointer(
            cls,
            schema: s_schema.Schema,
            pointer: s_pointers.Pointer,
            *,
            namespace: AbstractSet[AnyNamespace] = frozenset(),
    ) -> PathId:
        """Return a ``PathId`` instance for a given link or property.

        The specified *pointer* argument must be a concrete link or property.
        The returned ``PathId`` instance describes a set variable of all
        objects represented by the pointer (i.e, for a link, a set of all
        link targets).

        Args:
            schema:
                A schema instance where the type *t* is defined.
            pointer:
                An instance of a concrete link or property.
            namespace:
                Optional namespace in which the variable is defined.

        Returns:
            A ``PathId`` instance.
        """
        if pointer.generic(schema):
            raise ValueError(f'invalid PathId: {pointer} is not concrete')

        source = pointer.get_source(schema)
        if isinstance(source, s_pointers.Pointer):
            prefix = cls.from_pointer(schema, source, namespace=namespace)
            prefix = prefix.ptr_path()
        elif isinstance(source, s_types.Type):
            prefix = cls.from_type(schema, source, namespace=namespace)
        else:
            raise AssertionError(f'unexpected pointer source: {source!r}')

        ptrref = typeutils.ptrref_from_ptrcls(schema=schema, ptrcls=pointer)
        return prefix.extend(ptrref=ptrref)