示例#1
0
def compile_sequence(node, state):
    total_length = len(node.items)
    verify_call = IR.call("count", [state.compute_path()])

    length_verifier = IR.filter(verify_call, total_length, "=")

    if total := node.items.count(grammar.Expand):
        state.ensure(node, total == 1)
        length_verifier = IR.filter(verify_call, total_length - 1, ">=")
示例#2
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,
    )
示例#3
0
def compile_constant(node, state):
    expr = IR.literal(node.value)

    # Constants are represented as repr(obj) in the
    # serialization part, so we have to re-cast it.
    if state.match == "Constant":
        expr.value = repr(expr.value)

    return IR.filter(state.compute_path(), expr, "=")
示例#4
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)
示例#5
0
def metadata_parent(parent_node, state):
    state.ensure(parent_node, len(parent_node.filters) == 1)

    parent_field, filter_value = parent_node.filters.popitem()
    state.ensure(parent_node, filter_value is grammar.Ignore)

    with state.temp_pointer("_parent_types"):
        return IR.filter(
            IR.tuple(
                [parent_node.bound_node.type_id,
                 IR.literal(parent_field)]),
            state.compute_path(),
            "IN",
        )
示例#6
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, "=")
示例#7
0
def convert_length(node, state, arguments):
    state.ensure(node, any((arguments.min, arguments.max)))

    count = IR.call("count", [state.compute_path()])
    filters = None
    for value, operator in [
        (arguments.min, IR.as_operator(">=")),
        (arguments.max, IR.as_operator("<=")),
    ]:
        if value is None:
            continue

        state.ensure(value, isinstance(value, grammar.Constant))
        state.ensure(value, isinstance(value.value, int))
        filters = IR.combine_filters(
            filters, IR.filter(count, IR.literal(value.value), operator))

    assert filters is not None
    return filters
示例#8
0
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()
    return Insertion.INSERTED
示例#9
0
def convert_logical_operation(node, state):
    return IR.filter(
        state.codegen(node.left),
        state.codegen(node.right),
        state.codegen(node.operator),
    )
示例#10
0
def compile_match_string(node, state):
    expr = IR.literal(node.value)
    return IR.filter(state.compute_path(), expr, "LIKE")
示例#11
0
def convert_match_enum(node, state):
    expr = IR.enum_member(node.base, node.name)
    return IR.filter(state.compute_path(), expr, "=")
示例#12
0
            continue

        if right_filter := state.compile(key, value):
            filters = IR.combine_filters(filters, right_filter)

    if state.is_root:
        state.scope.exit()
        if state_filters := IR.unpack_filters(state.filters):
            filters = IR.combine_filters(filters, state_filters)
        if state.variables:
            namespace = IR.namespace(state.variables)
            filters = IR.add_namespace(namespace, IR.select(filters))
        return IR.select(state.match, filters=filters)

    if filters is None:
        filters = IR.filter(state.parents[-1].compute_path(),
                            IR.wrap(state.match), "IS")

    return filters


@codegen.register(grammar.MatchEnum)
def convert_match_enum(node, state):
    expr = IR.enum_member(node.base, node.name)
    return IR.filter(state.compute_path(), expr, "=")


@codegen.register(grammar.Constant)
def compile_constant(node, state):
    expr = IR.literal(node.value)

    # Constants are represented as repr(obj) in the
示例#13
0
def convert_intensive(node, state, arguments):
    match_str = arguments.match_str
    state.ensure(node, isinstance(match_str, grammar.MatchString))
    return IR.filter(state.compute_path(), IR.literal(match_str.value),
                     "ILIKE")