Esempio n. 1
0
def compile_Parameter(
        expr: irast.Parameter, *,
        ctx: context.CompilerContextLevel) -> pgast.BaseExpr:

    result: pgast.BaseParamRef
    if expr.name.isdecimal():
        index = int(expr.name) + 1
        result = pgast.ParamRef(number=index)
    else:
        if ctx.env.use_named_params:
            result = pgast.NamedParamRef(name=expr.name)
        else:
            if expr.name in ctx.argmap:
                index = ctx.argmap[expr.name]
            else:
                index = len(ctx.argmap) + 1
                ctx.argmap[expr.name] = index

            result = pgast.ParamRef(number=index)

    return pgast.TypeCast(
        arg=result,
        type_name=pgast.TypeName(
            name=pg_types.pg_type_from_ir_typeref(expr.typeref)
        )
    )
Esempio n. 2
0
def fini_toplevel(stmt: pgast.Query,
                  ctx: context.CompilerContextLevel) -> None:

    scan_check_ctes(stmt, ctx.env.check_ctes, ctx=ctx)

    # Type rewrites go first.
    if stmt.ctes is None:
        stmt.ctes = []
    stmt.ctes[:0] = list(ctx.type_ctes.values())

    stmt.argnames = argmap = ctx.argmap

    if not ctx.env.use_named_params:
        # Adding unused parameters into a CTE
        targets = []
        for param in ctx.env.query_params:
            pgparam = argmap[param.name]
            if pgparam.used:
                continue
            targets.append(
                pgast.ResTarget(val=pgast.TypeCast(
                    arg=pgast.ParamRef(number=pgparam.index),
                    type_name=pgast.TypeName(
                        name=pg_types.pg_type_from_ir_typeref(
                            param.ir_type)))))
        if targets:
            stmt.append_cte(
                pgast.CommonTableExpr(
                    name="__unused_vars",
                    query=pgast.SelectStmt(target_list=targets)))
Esempio n. 3
0
def fini_stmt(stmt: pgast.Query, ctx: context.CompilerContextLevel,
              parent_ctx: context.CompilerContextLevel) -> None:

    if stmt is ctx.toplevel_stmt:
        stmt.argnames = argmap = ctx.argmap

        if not ctx.env.use_named_params:
            # Adding unused parameters into a CTE
            targets = []
            for param in ctx.env.query_params:
                if param.name in argmap:
                    continue
                if param.name.isdecimal():
                    idx = int(param.name) + 1
                else:
                    idx = len(argmap) + 1
                argmap[param.name] = pgast.Param(
                    index=idx,
                    required=param.required,
                )
                targets.append(
                    pgast.ResTarget(val=pgast.TypeCast(
                        arg=pgast.ParamRef(number=idx),
                        type_name=pgast.TypeName(
                            name=pg_types.pg_type_from_ir_typeref(
                                param.ir_type)))))
            if targets:
                ctx.toplevel_stmt.ctes.append(
                    pgast.CommonTableExpr(
                        name="__unused_vars",
                        query=pgast.SelectStmt(target_list=targets)))
Esempio n. 4
0
def compile_Parameter(expr: irast.Parameter, *,
                      ctx: context.CompilerContextLevel) -> pgast.BaseExpr:

    result: pgast.BaseParamRef
    is_decimal: bool = expr.name.isdecimal()

    if not is_decimal and ctx.env.use_named_params:
        result = pgast.NamedParamRef(
            name=expr.name,
            nullable=not expr.required,
        )
    else:
        try:
            index = ctx.argmap[expr.name].index
        except KeyError:
            if expr.name in ctx.argmap:
                index = ctx.argmap[expr.name].index
            else:
                if expr.name.startswith('__edb_arg_'):
                    index = int(expr.name[10:]) + 1
                elif is_decimal:
                    index = int(expr.name) + 1
                else:
                    index = next(ctx.next_argument)
                ctx.argmap[expr.name] = pgast.Param(
                    index=index,
                    required=expr.required,
                )
        result = pgast.ParamRef(number=index, nullable=not expr.required)

    return pgast.TypeCast(
        arg=result,
        type_name=pgast.TypeName(
            name=pg_types.pg_type_from_ir_typeref(expr.typeref)))
Esempio n. 5
0
def compile_Parameter(expr: irast.Parameter, *,
                      ctx: context.CompilerContextLevel) -> pgast.BaseExpr:

    result: pgast.BaseParamRef
    is_decimal: bool = expr.name.isdecimal()

    if not is_decimal and ctx.env.use_named_params:
        result = pgast.NamedParamRef(
            name=expr.name,
            nullable=not expr.required,
        )
    else:
        index = ctx.argmap[expr.name].index
        ctx.argmap[expr.name].used = True

        result = pgast.ParamRef(number=index, nullable=not expr.required)

    return pgast.TypeCast(
        arg=result,
        type_name=pgast.TypeName(
            name=pg_types.pg_type_from_ir_typeref(expr.typeref)))