Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 3
0
def _link_has_shape(ptrcls: s_pointers.Pointer, *,
                    ctx: context.ContextLevel) -> bool:
    if not isinstance(ptrcls, s_links.Link):
        return False

    for p in ptrcls.get_pointers(ctx.env.schema).objects(ctx.env.schema):
        if (p.is_special_pointer(ctx.env.schema)
                or p not in ctx.env.view_shapes[ptrcls]):
            continue
        else:
            return True

    return False