Esempio n. 1
0
def range_for_material_objtype(objtype: s_objtypes.ObjectType,
                               path_id: irast.PathId,
                               *,
                               include_overlays: bool = True,
                               env: context.Environment) -> pgast.BaseRangeVar:

    from . import pathctx  # XXX: fix cycle

    objtype = objtype.material_type(env.schema)
    objtype_name = objtype.get_name(env.schema)

    table_schema_name, table_name = common.get_backend_name(env.schema,
                                                            objtype,
                                                            catenate=False)

    if objtype_name.module == 'schema':
        # Redirect all queries to schema tables to edgedbss
        table_schema_name = 'edgedbss'

    relation = pgast.Relation(
        schemaname=table_schema_name,
        name=table_name,
        path_id=path_id,
    )

    rvar = pgast.RangeVar(
        relation=relation,
        alias=pgast.Alias(aliasname=env.aliases.get(objtype_name.name)))

    overlays = env.rel_overlays.get(objtype_name)
    if overlays and include_overlays:
        set_ops = []

        qry = pgast.SelectStmt()
        qry.from_clause.append(rvar)
        pathctx.put_path_value_rvar(qry, path_id, rvar, env=env)
        pathctx.put_path_bond(qry, path_id)

        set_ops.append(('union', qry))

        for op, cte in overlays:
            rvar = pgast.RangeVar(
                relation=cte,
                alias=pgast.Alias(aliasname=env.aliases.get(hint=cte.name)))

            qry = pgast.SelectStmt(from_clause=[rvar], )

            pathctx.put_path_value_rvar(qry, path_id, rvar, env=env)
            pathctx.put_path_bond(qry, path_id)

            if op == 'replace':
                op = 'union'
                set_ops = []

            set_ops.append((op, qry))

        rvar = range_from_queryset(set_ops, objtype, env=env)

    return rvar
Esempio n. 2
0
def range_for_objtype(objtype: s_objtypes.ObjectType,
                      path_id: irast.PathId,
                      *,
                      include_overlays: bool = True,
                      env: context.Environment) -> pgast.BaseRangeVar:
    from . import pathctx  # XXX: fix cycle

    if not objtype.get_is_virtual(env.schema):
        rvar = range_for_material_objtype(objtype,
                                          path_id,
                                          include_overlays=include_overlays,
                                          env=env)
    else:
        # Union object types are represented as a UNION of selects
        # from their children, which is, for most purposes, equivalent
        # to SELECTing from a parent table.
        children = frozenset(objtype.children(env.schema))

        set_ops = []

        for child in children:
            c_rvar = range_for_objtype(child,
                                       path_id=path_id,
                                       include_overlays=include_overlays,
                                       env=env)

            qry = pgast.SelectStmt(from_clause=[c_rvar], )

            pathctx.put_path_value_rvar(qry, path_id, c_rvar, env=env)
            pathctx.put_path_bond(qry, path_id)

            set_ops.append(('union', qry))

        rvar = range_from_queryset(set_ops, objtype, env=env)

    rvar.query.path_id = path_id

    return rvar
Esempio n. 3
0
def add_rel_overlay(stype: s_objtypes.ObjectType, op: str,
                    rel: pgast.BaseRelation, *,
                    env: context.Environment) -> None:
    overlays = env.rel_overlays[stype.get_name(env.schema)]
    overlays.append((op, rel))