def constant_handler(self) -> Tuple[CustomFunction, ParameterValueType]:
     param = self.param
     if param.value.strip().startswith("{&"):
         result, value_type = self.match_function()
         return result, value_type
     else:
         return Term.wrap_constant(param.value), ParameterValueType.TEXT
Ejemplo n.º 2
0
    def resolve(self, model: "Type[Model]", table: Table) -> tuple:
        q_objects = self._resolve_q_objects()

        modifier = QueryModifier()
        for node in q_objects:
            modifier &= node.resolve(model, model._meta.basetable)

        if isinstance(self.then, Function):
            then = self.then.resolve(model, table)["field"]
        elif isinstance(self.then, Term):
            then = F.resolver_arithmetic_expression(model, self.then)[0]
        else:
            then = Term.wrap_constant(self.then)

        return modifier.where_criterion, then
Ejemplo n.º 3
0
def insensitive_ends_with(field: Term, value: str) -> Criterion:
    return Like(Upper(Cast(field, SqlTypes.VARCHAR)),
                field.wrap_constant(Upper(f"%{escape_like(value)}")))
Ejemplo n.º 4
0
def ends_with(field: Term, value: str) -> Criterion:
    return Like(Cast(field, SqlTypes.VARCHAR),
                field.wrap_constant(f"%{escape_like(value)}"))
Ejemplo n.º 5
0
    Case expression.

    :param args: When objects
    :param default: value for 'CASE WHEN ... THEN ... ELSE <default> END'
    """
    def __init__(
            self,
            *args: When,
            default: Union[str, F, ArithmeticExpression,
                           Function] = None) -> None:
        self.args = args
        self.default = default

    def resolve(self, model: "Type[Model]", table: Table) -> dict:
        case = PypikaCase()
        for arg in self.args:
            if not isinstance(arg, When):
                raise TypeError("expected When objects as args")
            criterion, term = arg.resolve(model, table)
            case = case.when(criterion, term)

        if isinstance(self.default, Function):
            case = case.else_(self.default.resolve(model, table)["field"])
        elif isinstance(self.default, Term):
            case = case.else_(
                F.resolver_arithmetic_expression(model, self.default)[0])
        else:
            case = case.else_(Term.wrap_constant(self.default))

        return {"joins": [], "field": case}