Example #1
0
 def compile_query(self):
     query = compile_query(self.reiz_ql, limit=None, offset=0)
     query.filters = IR.combine_filters(
         query.filters,
         IR.filter(
             IR.attribute(IR.attribute(None, "_module"), "filename"),
             IR.literal(self.expected_filename),
             "=",
         ),
     )
     return IR.construct(query)
Example #2
0
def compile_reference(node, state):
    obtained_type = state.field_info.type

    if pointer := state.scope.lookup(node.name):
        expected_type = pointer.field_info.type
        state.ensure(node, expected_type is obtained_type)

        left = state.compute_path()
        right = pointer.compute_path()

        if issubclass(expected_type, ast.expr):
            left = IR.attribute(left, "_tag")
            right = IR.attribute(right, "_tag")

        return IR.filter(left, right, "=")
Example #3
0
def serialize_project(node, context):
    return IR.select(
        node.kind_name,
        filters=IR.filter(
            IR.attribute(None, "name"), IR.literal(node.name), "="
        ),
        limit=1,
    )
Example #4
0
    def compute_path(self, allow_missing=False):
        parent, *parents = self.get_ordered_parents()

        def get_pointer(state, allow_missing):
            pointer = state.pointer
            if allow_missing:
                pointer = IR.optional(pointer)
            return pointer

        base = get_pointer(parent, allow_missing)
        if not parent.is_flag_set("in for loop"):
            base = IR.attribute(None, base)

        for parent in parents:
            base = IR.typed(base, parent.match)
            base = IR.attribute(base, get_pointer(parent, allow_missing))

        return base
Example #5
0
def serialize_sequence(sequence, context):
    ir_set = IR.set([serialize(value, context) for value in sequence])

    if all(isinstance(item, _BASIC_SET_TYPES) for item in sequence):
        # {1, 2, 3} / {<ast::op>'Add', <ast::op>'Sub', ...}
        return ir_set
    else:
        # Inserting a sequence of AST objects would require special
        # attention to calculate the index property.
        target = IR.name("item")
        scope = IR.namespace({"items": ir_set})
        loop = IR.loop(
            target,
            IR.call("enumerate", [IR.name("items")]),
            IR.select(
                IR.attribute(target, 1),
                selections=[
                    IR.assign(IR.property("index"), IR.attribute(target, 0))
                ],
            ),
        )
        return IR.add_namespace(scope, loop)
Example #6
0
def aggregate_array(state):
    # If we are in a nested list search (e.g: Call(args=[Call(args=[Name()])]))
    # we can't directly use `ORDER BY @index` since the EdgeDB can't quite infer
    # which @index are we talking about.
    if len(state.parents) >= 1:
        path = IR.attribute(
            IR.typed(IR.name(_COMPILER_WORKAROUND_FOR_TARGET), state.match),
            state.pointer,
        )
        body = IR.loop(
            IR.name(_COMPILER_WORKAROUND_FOR_TARGET),
            state.parents[-1].compute_path(allow_missing=True),
            IR.select(path, order=IR.property("index")),
        )
    else:
        body = IR.select(state.compute_path(), order=IR.property("index"))

    return IR.call("array_agg", [body])
Example #7
0
@guarded(Insertion.FAILED, ignored_exceptions=(InternalDatabaseError, ))
def insert_file(context):
    if context.is_cached():
        return Insertion.CACHED

    if not (tree := context.as_ast()):
        return Insertion.SKIPPED

    with context.connection.transaction():
        module = apply_ast(tree, context)
        module_select = IR.select(tree.kind_name,
                                  filters=IR.object_ref(module),
                                  limit=1)

        update_filter = IR.filter(
            IR.attribute(None, "id"),
            IR.call("array_unpack",
                    [IR.cast("array<uuid>", IR.variable("ids"))]),
            "IN",
        )
        for base_type in Schema.module_annotated_types:
            update = IR.update(
                base_type.kind_name,
                filters=update_filter,
                assignments={"_module": module_select},
            )
            context.connection.query(IR.construct(update),
                                     ids=context.reference_pool)

    logger.info("%r has been inserted successfully", context.filename)
    context.cache()