예제 #1
0
파일: stmtctx.py 프로젝트: mcaramma/edgedb
def init_context(
        *,
        schema: s_schema.Schema,
        arg_types: typing.Optional[typing.Iterable[s_obj.Object]]=None,
        modaliases: typing.Optional[typing.Dict[str, str]]=None,
        anchors: typing.Optional[typing.Dict[str, s_obj.Object]]=None,
        security_context: typing.Optional[str]=None,
        derived_target_module: typing.Optional[str]=None,
        result_view_name: typing.Optional[str]=None,
        implicit_id_in_shapes: bool=False) -> \
        context.ContextLevel:
    stack = context.CompilerContext()
    ctx = stack.current
    ctx.schema = schema.get_overlay(extra=ctx.view_nodes)

    if modaliases:
        ctx.modaliases.update(modaliases)

    if arg_types:
        ctx.arguments.update(arg_types)

    if anchors:
        with ctx.newscope(fenced=True) as subctx:
            populate_anchors(anchors, ctx=subctx)

    ctx.derived_target_module = derived_target_module
    ctx.toplevel_result_view_name = result_view_name
    ctx.implicit_id_in_shapes = implicit_id_in_shapes

    return ctx
예제 #2
0
파일: types.py 프로젝트: versada/edgedb
def pg_type_from_object(schema: s_schema.Schema,
                        obj: s_obj.Object,
                        topbase: bool = False) -> typing.Tuple[str, ...]:

    if isinstance(obj, s_scalars.ScalarType):
        return pg_type_from_scalar(schema, obj, topbase=topbase)

    elif isinstance(obj, s_types.Tuple):
        return ('record', )

    elif isinstance(obj, s_types.Array):
        if obj.element_type.name == 'std::any':
            return ('anyarray', )
        else:
            st = schema.get(obj.element_type.name)
            tp = pg_type_from_scalar(schema, st, topbase=True)
            if len(tp) == 1:
                return (tp[0] + '[]', )
            else:
                return (tp[0], tp[1] + '[]')

    elif isinstance(obj, s_objtypes.ObjectType):
        return ('uuid', )

    else:
        raise ValueError(f'could not determine PG type for {obj!r}')
예제 #3
0
파일: utils.py 프로젝트: mcaramma/edgedb
def get_id_path_id(path_id: irast.PathId, *,
                   schema: s_schema.Schema) -> irast.PathId:
    """For PathId representing an object, return (PathId).(std::id)."""
    source: s_sources.Source = path_id.target
    assert isinstance(source, s_objtypes.ObjectType)
    return path_id.extend(source.resolve_pointer(schema, 'std::id'),
                          s_pointers.PointerDirection.Outbound,
                          schema.get('std::uuid'))
예제 #4
0
def init_context(
        *,
        schema: s_schema.Schema,
        func: typing.Optional[s_func.Function]=None,
        modaliases: typing.Optional[typing.Dict[str, str]]=None,
        anchors: typing.Optional[typing.Dict[str, s_obj.Object]]=None,
        singletons: typing.Optional[typing.Iterable[s_types.Type]]=None,
        security_context: typing.Optional[str]=None,
        derived_target_module: typing.Optional[str]=None,
        result_view_name: typing.Optional[str]=None,
        schema_view_mode: bool=False,
        implicit_id_in_shapes: bool=False) -> \
        context.ContextLevel:
    stack = context.CompilerContext()
    ctx = stack.current
    if not schema.get('__derived__', None):
        schema, _ = s_mod.Module.create_in_schema(schema, name='__derived__')
    ctx.env = context.Environment(schema=schema,
                                  path_scope=irast.new_scope_tree(),
                                  schema_view_mode=schema_view_mode)

    if singletons:
        # The caller wants us to treat these type references
        # as singletons for the purposes of the overall expression
        # cardinality inference, so we set up the scope tree in
        # the necessary fashion.
        for singleton in singletons:
            path_id = pathctx.get_path_id(singleton, ctx=ctx)
            ctx.env.path_scope.attach_path(path_id)

        ctx.path_scope = ctx.env.path_scope.attach_fence()

    if modaliases:
        ctx.modaliases.update(modaliases)

    if anchors:
        with ctx.newscope(fenced=True) as subctx:
            populate_anchors(anchors, ctx=subctx)

    ctx.func = func
    ctx.derived_target_module = derived_target_module
    ctx.toplevel_result_view_name = result_view_name
    ctx.implicit_id_in_shapes = implicit_id_in_shapes

    return ctx
예제 #5
0
def _analyse_filter_clause(result_set: irast.Set, filter_clause: irast.Set,
                           scope_tree: typing.Set[irast.PathId],
                           schema: s_schema.Schema) -> irast.Cardinality:

    filtered_ptrs = _extract_filters(result_set, filter_clause, scope_tree,
                                     schema)

    if filtered_ptrs:
        unique_constr = schema.get('std::unique')

        for ptr in filtered_ptrs:
            is_unique = (ptr.is_id_pointer() or any(
                c.issubclass(unique_constr) for c in ptr.constraints.values()))
            if is_unique:
                # Bingo, got an equality filter on a link with a
                # unique constraint
                return ONE

    return MANY